use of org.molgenis.ontology.core.model.OntologyTerm in project molgenis by molgenis.
the class SemanticSearchServiceHelper method collectExpandedQueryMap.
public Map<String, String> collectExpandedQueryMap(Set<String> queryTerms, Collection<OntologyTerm> ontologyTerms) {
Map<String, String> expandedQueryMap = new LinkedHashMap<>();
queryTerms.stream().filter(StringUtils::isNotBlank).forEach(queryTerm -> expandedQueryMap.put(Stemmer.cleanStemPhrase(queryTerm), queryTerm));
for (OntologyTerm ontologyTerm : ontologyTerms) {
if (!ontologyTerm.getIRI().contains(COMMA_CHAR)) {
collectOntologyTermQueryMap(expandedQueryMap, ontologyTerm);
} else {
for (String ontologyTermIri : ontologyTerm.getIRI().split(COMMA_CHAR)) {
collectOntologyTermQueryMap(expandedQueryMap, ontologyService.getOntologyTerm(ontologyTermIri));
}
}
}
return expandedQueryMap;
}
use of org.molgenis.ontology.core.model.OntologyTerm in project molgenis by molgenis.
the class OntologyTagServiceTest method testGetTagsForAttribute.
@Test
public void testGetTagsForAttribute() {
EntityType emd = entityTypeFactory.create("org.molgenis.SNP");
Attribute attribute = attrFactory.create().setName("Chr");
attribute.setTags(asList(chromosomeNameTagEntity, geneAnnotationTagEntity));
Relation instanceOf = Relation.valueOf("instanceOf");
Ontology edamOntology = Ontology.create("EDAM", "http://edamontology.org", "The EDAM ontology.");
OntologyTerm chromosomeName = OntologyTerm.create("http://edamontology.org/data_0987", "Chromosome name", "Name of a chromosome.");
OntologyTerm geneAnnotation = OntologyTerm.create("http://edamontology.org/data_0919", "Gene annotation (chromosome)", "This includes basic information. e.g. chromosome number...");
when(ontologyService.getOntology("http://edamontology.org")).thenReturn(edamOntology);
when(ontologyService.getOntologyTerm("http://edamontology.org/data_0987")).thenReturn(chromosomeName);
when(ontologyService.getOntologyTerm("http://edamontology.org/data_0919")).thenReturn(geneAnnotation);
Multimap<Relation, OntologyTerm> expected = LinkedHashMultimap.create();
expected.put(instanceOf, chromosomeName);
expected.put(instanceOf, geneAnnotation);
assertEquals(ontologyTagService.getTagsForAttribute(emd, attribute), expected);
}
use of org.molgenis.ontology.core.model.OntologyTerm in project molgenis by molgenis.
the class OntologyTagServiceTest method testGetTagEntity.
@Test
public void testGetTagEntity() {
Tag expected = tagFactory.create();
expected.set(TagMetadata.ID, "1233");
expected.set(TagMetadata.OBJECT_IRI, "http://edamontology.org/data_3031");
expected.set(TagMetadata.LABEL, "Core data");
expected.set(TagMetadata.RELATION_IRI, "http://molgenis.org/biobankconnect/instanceOf");
expected.set(TagMetadata.RELATION_LABEL, "instanceOf");
expected.set(TagMetadata.CODE_SYSTEM, "http://edamontology.org");
OntologyTerm coreData = mock(OntologyTerm.class);
when(coreData.getIRI()).thenReturn("http://edamontology.org/data_3031");
when(coreData.getLabel()).thenReturn("Core data");
Ontology edamOntology = mock(Ontology.class);
when(edamOntology.getIRI()).thenReturn("http://edamontology.org");
SemanticTag<Object, OntologyTerm, Ontology> tag = new SemanticTag<>("1233", null, Relation.instanceOf, coreData, edamOntology);
when(tagRepository.getTagEntity("http://edamontology.org/data_3031", "Core data", Relation.instanceOf, "http://edamontology.org")).thenReturn(expected);
assertEquals(ontologyTagService.getTagEntity(tag), expected);
}
use of org.molgenis.ontology.core.model.OntologyTerm in project molgenis by molgenis.
the class OntologyTagServiceImpl method tagAttributesInEntity.
@Override
@Transactional
public Map<String, OntologyTag> tagAttributesInEntity(String entity, Map<Attribute, OntologyTerm> tags) {
Map<String, OntologyTag> result = new LinkedHashMap<>();
for (Entry<Attribute, OntologyTerm> tag : tags.entrySet()) {
OntologyTerm ontologyTerm = tag.getValue();
OntologyTag ontologyTag = addAttributeTag(entity, tag.getKey().getName(), Relation.isAssociatedWith.getIRI(), Collections.singletonList(ontologyTerm.getIRI()));
result.put(tag.getKey().getName(), ontologyTag);
}
return result;
}
use of org.molgenis.ontology.core.model.OntologyTerm in project molgenis by molgenis.
the class SemanticSearchServiceImpl method findTags.
@Override
public Hit<OntologyTerm> findTags(Attribute attribute, List<String> ontologyIds) {
String description = attribute.getDescription() == null ? attribute.getLabel() : attribute.getDescription();
Set<String> searchTerms = splitIntoTerms(description);
Stemmer stemmer = new Stemmer();
if (LOG.isDebugEnabled()) {
LOG.debug("findOntologyTerms({},{},{})", ontologyIds, searchTerms, MAX_NUM_TAGS);
}
List<OntologyTerm> candidates = ontologyService.findOntologyTerms(ontologyIds, searchTerms, MAX_NUM_TAGS);
if (LOG.isDebugEnabled()) {
LOG.debug("Candidates: {}", candidates);
}
List<Hit<OntologyTerm>> hits = candidates.stream().filter(ontologyTerm -> filterOntologyTerm(splitIntoTerms(Stemmer.stemAndJoin(searchTerms)), ontologyTerm, stemmer)).map(ontolgoyTerm -> Hit.create(ontolgoyTerm, bestMatchingSynonym(ontolgoyTerm, searchTerms).getScore())).sorted(Ordering.natural().reverse()).collect(Collectors.toList());
if (LOG.isDebugEnabled()) {
LOG.debug("Hits: {}", hits);
}
Hit<OntologyTerm> result = null;
String bestMatchingSynonym = null;
for (Hit<OntologyTerm> hit : hits) {
String bestMatchingSynonymForHit = bestMatchingSynonym(hit.getResult(), searchTerms).getResult();
if (result == null) {
result = hit;
bestMatchingSynonym = bestMatchingSynonymForHit;
} else {
Set<String> jointTerms = Sets.union(splitIntoTerms(bestMatchingSynonym), splitIntoTerms(bestMatchingSynonymForHit));
String joinedSynonyms = termJoiner.join(jointTerms);
Hit<OntologyTerm> joinedHit = Hit.create(OntologyTerm.and(result.getResult(), hit.getResult()), distanceFrom(joinedSynonyms, searchTerms, stemmer));
if (joinedHit.compareTo(result) > 0) {
result = joinedHit;
bestMatchingSynonym = bestMatchingSynonym + " " + bestMatchingSynonymForHit;
}
}
if (LOG.isDebugEnabled()) {
LOG.debug("result: {}", result);
}
}
if (result != null && result.getScore() >= CUTOFF) {
if (LOG.isDebugEnabled()) {
LOG.debug("Tag {} with {}", attribute, result);
}
return result;
}
return null;
}
Aggregations