Search in sources :

Example 26 with OntologyTerm

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;
}
Also used : OntologyTerm(org.molgenis.ontology.core.model.OntologyTerm)

Example 27 with OntologyTerm

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);
}
Also used : Relation(org.molgenis.data.semantic.Relation) Ontology(org.molgenis.ontology.core.model.Ontology) OntologyTerm(org.molgenis.ontology.core.model.OntologyTerm) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Example 28 with OntologyTerm

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);
}
Also used : Ontology(org.molgenis.ontology.core.model.Ontology) SemanticTag(org.molgenis.data.semantic.SemanticTag) OntologyTag(org.molgenis.semanticsearch.semantic.OntologyTag) OntologyTerm(org.molgenis.ontology.core.model.OntologyTerm) SemanticTag(org.molgenis.data.semantic.SemanticTag) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Example 29 with OntologyTerm

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;
}
Also used : OntologyTerm(org.molgenis.ontology.core.model.OntologyTerm) OntologyTag(org.molgenis.semanticsearch.semantic.OntologyTag) Transactional(org.springframework.transaction.annotation.Transactional)

Example 30 with OntologyTerm

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;
}
Also used : NGramDistanceAlgorithm(org.molgenis.semanticsearch.string.NGramDistanceAlgorithm) java.util(java.util) StringDistance(org.apache.lucene.search.spell.StringDistance) Operator(org.molgenis.data.QueryRule.Operator) LoggerFactory(org.slf4j.LoggerFactory) SemanticSearchService(org.molgenis.semanticsearch.service.SemanticSearchService) QueryImpl(org.molgenis.data.support.QueryImpl) StringUtils(org.apache.commons.lang3.StringUtils) Attribute(org.molgenis.data.meta.model.Attribute) MetaDataService(org.molgenis.data.meta.MetaDataService) Lists(com.google.common.collect.Lists) FluentIterable(com.google.common.collect.FluentIterable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) OntologyTerm(org.molgenis.ontology.core.model.OntologyTerm) Objects.requireNonNull(java.util.Objects.requireNonNull) AttributeMetadata(org.molgenis.data.meta.model.AttributeMetadata) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) OntologyService(org.molgenis.ontology.core.service.OntologyService) Splitter(com.google.common.base.Splitter) ATTRIBUTE_META_DATA(org.molgenis.data.meta.model.AttributeMetadata.ATTRIBUTE_META_DATA) Hit(org.molgenis.semanticsearch.semantic.Hit) Stemmer(org.molgenis.semanticsearch.string.Stemmer) Explanation(org.apache.lucene.search.Explanation) Logger(org.slf4j.Logger) ElasticSearchExplainService(org.molgenis.semanticsearch.explain.service.ElasticSearchExplainService) ExplainedQueryString(org.molgenis.semanticsearch.explain.bean.ExplainedQueryString) EntityType(org.molgenis.data.meta.model.EntityType) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Ontology(org.molgenis.ontology.core.model.Ontology) Stream(java.util.stream.Stream) Ordering(com.google.common.collect.Ordering) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) DataService(org.molgenis.data.DataService) Query(org.molgenis.data.Query) QueryRule(org.molgenis.data.QueryRule) Joiner(com.google.common.base.Joiner) Entity(org.molgenis.data.Entity) Hit(org.molgenis.semanticsearch.semantic.Hit) Stemmer(org.molgenis.semanticsearch.string.Stemmer) ExplainedQueryString(org.molgenis.semanticsearch.explain.bean.ExplainedQueryString) OntologyTerm(org.molgenis.ontology.core.model.OntologyTerm)

Aggregations

OntologyTerm (org.molgenis.ontology.core.model.OntologyTerm)30 Test (org.testng.annotations.Test)13 Relation (org.molgenis.data.semantic.Relation)9 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)8 ExplainedAttribute (org.molgenis.semanticsearch.explain.bean.ExplainedAttribute)8 Attribute (org.molgenis.data.meta.model.Attribute)7 EntityType (org.molgenis.data.meta.model.EntityType)7 Ontology (org.molgenis.ontology.core.model.Ontology)7 QueryRule (org.molgenis.data.QueryRule)6 Entity (org.molgenis.data.Entity)5 Sets (com.google.common.collect.Sets)3 java.util (java.util)3 Objects.requireNonNull (java.util.Objects.requireNonNull)3 Collectors (java.util.stream.Collectors)3 StringUtils (org.apache.commons.lang3.StringUtils)3 QueryParser (org.apache.lucene.queryparser.classic.QueryParser)3 DataService (org.molgenis.data.DataService)3 Operator (org.molgenis.data.QueryRule.Operator)3 AttributeMetadata (org.molgenis.data.meta.model.AttributeMetadata)3 SemanticTag (org.molgenis.data.semantic.SemanticTag)3