use of org.molgenis.ontology.core.model.OntologyTerm in project molgenis by molgenis.
the class OntologyTagServiceImpl method asTag.
private <SubjectType> SemanticTag<SubjectType, OntologyTerm, Ontology> asTag(SubjectType subjectType, Entity tagEntity) {
String identifier = tagEntity.getString(TagMetadata.ID);
Relation relation = asRelation(tagEntity);
Ontology ontology = asOntology(tagEntity);
OntologyTerm ontologyTerm = asOntologyTerm(tagEntity);
if (relation == null || ontologyTerm == null) {
return null;
}
return new SemanticTag<>(identifier, subjectType, relation, ontologyTerm, ontology);
}
use of org.molgenis.ontology.core.model.OntologyTerm in project molgenis by molgenis.
the class OntologyTagServiceImpl method addAttributeTag.
@Override
public OntologyTag addAttributeTag(String entity, String attribute, String relationIRI, List<String> ontologyTermIRIs) {
boolean added = false;
Entity attributeEntity = findAttributeEntity(entity, attribute);
Tag tag = new Tag(tagMetadata);
Stream<OntologyTerm> terms = ontologyTermIRIs.stream().map(ontologyService::getOntologyTerm);
OntologyTerm combinedOntologyTerm = OntologyTerm.and(terms.toArray(OntologyTerm[]::new));
Relation relation = Relation.forIRI(relationIRI);
tag.setId(idGenerator.generateId());
tag.setCodeSystem(null);
tag.setRelationIri(relation.getIRI());
tag.setRelationLabel(relation.getLabel());
tag.setLabel(combinedOntologyTerm.getLabel());
tag.setObjectIri(combinedOntologyTerm.getIRI());
dataService.add(TAG, tag);
Map<String, Entity> tags = Maps.newHashMap();
for (Entity attrTag : attributeEntity.getEntities(AttributeMetadata.TAGS)) {
tags.put(attrTag.get(TagMetadata.OBJECT_IRI).toString(), attrTag);
}
if (!tags.containsKey(tag.get(TagMetadata.OBJECT_IRI).toString())) {
tags.put(tag.get(TagMetadata.OBJECT_IRI).toString(), tag);
added = true;
}
attributeEntity.set(AttributeMetadata.TAGS, tags.values());
dataService.update(ATTRIBUTE_META_DATA, attributeEntity);
updateEntityTypeEntityWithNewAttributeEntity(entity, attribute, attributeEntity);
return added ? OntologyTag.create(combinedOntologyTerm, relation) : null;
}
use of org.molgenis.ontology.core.model.OntologyTerm in project molgenis by molgenis.
the class SemanticSearchServiceImpl method findTags.
@Override
public Map<Attribute, Hit<OntologyTerm>> findTags(String entity, List<String> ontologyIds) {
Map<Attribute, Hit<OntologyTerm>> result = new LinkedHashMap<>();
EntityType emd = metaDataService.getEntityType(entity);
for (Attribute amd : emd.getAtomicAttributes()) {
Hit<OntologyTerm> tag = findTags(amd, ontologyIds);
if (tag != null) {
result.put(amd, tag);
}
}
return result;
}
use of org.molgenis.ontology.core.model.OntologyTerm in project molgenis by molgenis.
the class SemanticSearchServiceImpl method bestMatchingSynonym.
/**
* Computes the best matching synonym which is closest to a set of search terms.<br/>
* Will stem the {@link OntologyTerm} 's synonyms and the search terms, and then compute the maximum
* {@link StringDistance} between them. 0 means disjunct, 1 means identical
*
* @param ontologyTerm the {@link OntologyTerm}
* @param searchTerms the search terms
* @return the maximum {@link StringDistance} between the ontologyterm and the search terms
*/
public Hit<String> bestMatchingSynonym(OntologyTerm ontologyTerm, Set<String> searchTerms) {
Stemmer stemmer = new Stemmer();
Optional<Hit<String>> bestSynonym = ontologyTerm.getSynonyms().stream().map(synonym -> Hit.create(synonym, distanceFrom(synonym, searchTerms, stemmer))).max(Comparator.naturalOrder());
return bestSynonym.get();
}
use of org.molgenis.ontology.core.model.OntologyTerm in project molgenis by molgenis.
the class SemanticSearchServiceHelperTest method testCollectQueryTermsFromOntologyTerm.
@Test
public void testCollectQueryTermsFromOntologyTerm() {
// Case 1
OntologyTerm ontologyTerm1 = OntologyTerm.create("http://onto/standingheight", "Standing height", "Description is not used", singletonList("body_length"));
List<String> actual_1 = semanticSearchServiceHelper.parseOntologyTermQueries(ontologyTerm1);
assertEquals(actual_1, asList("length body", "standing height"));
// Case 2
OntologyTerm ontologyTerm2 = OntologyTerm.create("http://onto/standingheight", "height", "Description is not used", emptyList());
OntologyTerm ontologyTerm3 = OntologyTerm.create("http://onto/standingheight-children", "length", singletonList("body_length"));
when(ontologyService.getChildren(ontologyTerm2)).thenReturn(singletonList(ontologyTerm3));
when(ontologyService.getOntologyTermDistance(ontologyTerm2, ontologyTerm3)).thenReturn(1);
List<String> actual_2 = semanticSearchServiceHelper.parseOntologyTermQueries(ontologyTerm2);
assertEquals(actual_2, asList("height", "length^0.5 body^0.5", "length^0.5"));
}
Aggregations