org.monarchinitiative.phenol.io.obographs.OboGraphTermFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of phenol-io Show documentation
Show all versions of phenol-io Show documentation
phenol-io contains the generic I/O functionality for ontologies
package org.monarchinitiative.phenol.io.obographs;
import java.lang.reflect.Field;
import java.util.List;
import com.google.common.collect.ImmutableList;
import org.geneontology.obographs.model.Meta;
import org.geneontology.obographs.model.Node;
import org.geneontology.obographs.model.meta.BasicPropertyValue;
import org.geneontology.obographs.model.meta.DefinitionPropertyValue;
import org.geneontology.obographs.model.meta.SynonymPropertyValue;
import org.geneontology.obographs.model.meta.XrefPropertyValue;
import org.monarchinitiative.phenol.ontology.data.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Factory class for constructing {@link Term} and {@link Relationship} objects from
* Obographs's Nodes.
*
* @author HyeongSik Kim
* @author Peter Robinson
*/
public class OboGraphTermFactory {
private static final Logger LOGGER = LoggerFactory.getLogger(OboGraphTermFactory.class);
public Term constructTerm(Node node, TermId termId) {
Term.Builder termBuilder = Term.builder();
termBuilder.id(termId);
String label = node.getLabel();
// labels for obsolete terms ids found in the alt_id section are null
termBuilder.name((label == null) ? "" : label);
Meta meta = node.getMeta();
if (meta == null) {
// this isn't really a problem
LOGGER.debug("No meta instance exists for node: {} {}", node.getId(), node.getLabel());
return termBuilder.build();
}
// 1. definition
DefinitionPropertyValue definitionPropertyValue = meta.getDefinition();
String definition = getDefinition(definitionPropertyValue);
termBuilder.definition(definition);
List simpleXrefs = convertToXrefs(definitionPropertyValue);
termBuilder.databaseXrefs(simpleXrefs);
// 2. comments
List comments = meta.getComments();
if (comments != null) {
termBuilder.comment(String.join(", ", comments));
}
// 3. subsets
List subsets = meta.getSubsets();
if (subsets != null) {
termBuilder.subsets(subsets);
}
// 4. synonyms
List termSynonyms = convertToSynonyms(meta.getSynonyms());
termBuilder.synonyms(termSynonyms);
// 5. xrefs
List xrefs = convertToDbXrefs(meta.getXrefs());
termBuilder.xrefs(xrefs);
// 6. obsolete; the obsolete/deprecated field in Meta is somehow not accessible,
// so we use Java reflection to pull the value of that field.
boolean isObsolete = isObsolete(meta);
termBuilder.obsolete(isObsolete);
// 7. altIds
List altIds = convertToAltIds(meta.getBasicPropertyValues());
termBuilder.altTermIds(altIds);
return termBuilder.build();
}
private String getDefinition(DefinitionPropertyValue definitionPropertyValue) {
return (definitionPropertyValue == null)? "" : definitionPropertyValue.getVal();
}
private List convertToXrefs(DefinitionPropertyValue definitionPropertyValue) {
if (definitionPropertyValue == null) {
return ImmutableList.of();
}
List xrefs = definitionPropertyValue.getXrefs();
if (xrefs == null) {
return ImmutableList.of();
}
ImmutableList.Builder simpleXrefBuilder = new ImmutableList.Builder<>();
for (String xref : xrefs) {
SimpleXref sxref = new SimpleXref(xref);
if (sxref.isValid()) { // Add Xrefs that we might want to reference later on
// this includes PMIDs and OMIMs
simpleXrefBuilder.add(sxref);
}
}
return simpleXrefBuilder.build();
}
/** @return list of synoynms (can be an empty list but cannot be null). */
private List convertToSynonyms(List spvs) {
if (spvs == null) return ImmutableList.of();
ImmutableList.Builder termSynonymBuilder = new ImmutableList.Builder<>();
for (SynonymPropertyValue spv : spvs) {
// Map the scope of Synonym
TermSynonymScope scope = null;
String pred = spv.getPred();
if (pred.equals(SynonymPropertyValue.PREDS.hasExactSynonym.toString())) {
scope = TermSynonymScope.EXACT;
} else if (pred.equals(SynonymPropertyValue.PREDS.hasBroadSynonym.toString())) {
scope = TermSynonymScope.BROAD;
} else if (pred.equals(SynonymPropertyValue.PREDS.hasNarrowSynonym.toString())) {
scope = TermSynonymScope.NARROW;
} else if (pred.equals(SynonymPropertyValue.PREDS.hasRelatedSynonym.toString())) {
scope = TermSynonymScope.RELATED;
}
String synonymType = spv.getSynonymType();
// Map the synonym's type name.
String synonymTypeName = String.join(", ", spv.getTypes());
// Map the synonym's cross-references.
List xrefs = spv.getXrefs();
List termXrefs = mapXref(xrefs);
TermSynonym its = new TermSynonym(spv.getVal(), scope, synonymTypeName, termXrefs, synonymType);
termSynonymBuilder.add(its);
}
return termSynonymBuilder.build();
}
/**
* We try to map the cross references to Curies, e.g., ORCID:0000-0000-0000-0123.
* If a cross-reference is not in CURIE for, we just ignore it. For now we
* use an empty string for the Description field of the cross-reference.
* @param xrefs list of cross references as Strings
* @return list of cross references as {@link TermXref} objects. Can be empty but not null.
*/
private List mapXref(List xrefs) {
ImmutableList.Builder termXrefBuilder = new ImmutableList.Builder<>();
for (String xref : xrefs) {
try {
TermId xrefTermId = TermId.of(xref);
TermXref trf = new TermXref(xrefTermId,"");
termXrefBuilder.add(trf);
} catch (Exception e) {
// ignore
}
}
return termXrefBuilder.build();
}
private List convertToDbXrefs(List xrefPropertyValues) {
if (xrefPropertyValues == null || xrefPropertyValues.isEmpty()) {
return ImmutableList.of();
}
ImmutableList.Builder dbxrefs = new ImmutableList.Builder<>();
for (XrefPropertyValue xrefPropertyValue : xrefPropertyValues) {
String val = xrefPropertyValue.getVal();
if (val == null) continue;
dbxrefs.add(new Dbxref(val, null, null));
}
return dbxrefs.build();
}
private boolean isObsolete(Meta meta) {
try {
Field f = Meta.class.getDeclaredField("deprecated");
f.setAccessible(true);
Boolean deprecated = (Boolean) f.get(meta);
if (deprecated == null) {
return false;
} else {
return deprecated;
}
} catch (Exception e) {
LOGGER.error(e.getMessage());
}
return false;
}
private List convertToAltIds(List basicPropertyValues) {
if (basicPropertyValues == null || basicPropertyValues.isEmpty()) {
return ImmutableList.of();
}
ImmutableList.Builder altIdsBuilder = new ImmutableList.Builder<>();
for (BasicPropertyValue bpv : basicPropertyValues) {
if ("http://www.geneontology.org/formats/oboInOwl#hasAlternativeId".equals(bpv.getPred())) {
String altId = bpv.getVal();
altIdsBuilder.add(TermId.of(altId));
}
}
return altIdsBuilder.build();
}
}