Skip to content

Commit

Permalink
Merge pull request #247 from owlcollab/hotfix-get-identifier
Browse files Browse the repository at this point in the history
Revised getIdentifier method to use oboInOwl#id for non-obolibrary PURLs
  • Loading branch information
cmungall authored Apr 19, 2018
2 parents e637b4c + 46b6154 commit c498f85
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.util.regex.Pattern;

import org.obolibrary.obo2owl.Obo2OWLConstants.Obo2OWLVocabulary;
import org.apache.commons.lang.SerializationUtils;
import org.apache.log4j.Logger;
import org.geneontology.obographs.io.OgJsonGenerator;
import org.geneontology.obographs.model.GraphDocument;
import org.geneontology.obographs.owlapi.FromOwl;
Expand Down Expand Up @@ -47,6 +49,7 @@
import org.semanticweb.owlapi.model.OWLSymmetricObjectPropertyAxiom;
import org.semanticweb.owlapi.model.OWLTransitiveObjectPropertyAxiom;
import org.semanticweb.owlapi.model.parameters.Imports;
import org.semanticweb.owlapi.search.EntitySearcher;
import org.semanticweb.owlapi.util.OWLObjectVisitorExAdapter;
import org.semanticweb.owlapi.vocab.OWLRDFVocabulary;

Expand All @@ -65,7 +68,7 @@
* @see OWLGraphWrapperBasic
*/
public class OWLGraphWrapperExtended extends OWLGraphWrapperBasic {

private static final Logger LOG = Logger.getLogger(OWLGraphWrapperExtended.class);
private Map<String,OWLObject> altIdMap = null;

protected OWLGraphWrapperExtended(OWLOntology ontology) {
Expand Down Expand Up @@ -878,19 +881,48 @@ public String getIdentifier(OWLObject owlObject, List<String> sargs) {
return getIdentifier(owlObject);
}


/**
* gets the OBO-style ID of the specified object. E.g. "GO:0008150"
* gets the OBO-style ID of the specified object. e.g., "GO:0008150"
* SerializationUtils.clone is used to avoid memory leaks.
*
* @param iriId
* @return OBO-style identifier, using obo2owl mapping
* @return OBO-style identifier, using obo2owl mappings or the literals extracted from oboInowl#id.
*/
public String getIdentifier(IRI iriId) {
return Owl2Obo.getIdentifier(iriId);
if (iriId.toString().startsWith(Obo2OWLConstants.DEFAULT_IRI_PREFIX))
return (String) SerializationUtils.clone(Owl2Obo.getIdentifier(iriId));

final OWLAnnotationProperty oboIdInOwl = getDataFactory().getOWLAnnotationProperty(Obo2Owl.trTagToIRI(OboFormatTag.TAG_ID.getTag()));
OWLClass oc = getOWLClass(iriId);
for (OWLOntology o : getAllOntologies()) {
for (OWLAnnotation oa: EntitySearcher.getAnnotations(oc.getIRI(), o)) {
if (oa.getProperty().equals(oboIdInOwl) != true)
continue;

OWLAnnotationValue objValue = oa.getValue();
if (objValue.isLiteral() != true) {
LOG.warn(objValue + " is supposed to be an literal, but it is not?!");
continue;
}

Optional<OWLLiteral> literalOpt = objValue.asLiteral();
if (literalOpt.isPresent() != true) {
LOG.warn("Is the literal value of oboInOw#id, " + objValue + ", null?");
continue;
}

OWLLiteral literal = literalOpt.get();
return (String) SerializationUtils.clone(literal.getLiteral());
}
}

throw new RuntimeException("The IRI " + iriId + " does not start with the obolib prefix nor have any oboInOw#id?!");
}

public IRI getIRIByIdentifier(String id) {
return getIRIByIdentifier(id, false);
}

public IRI getIRIByIdentifier(String id, boolean isAutoResolve) {
if (isAutoResolve) {
OWLObject obj = this.getObjectByAltId(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,17 @@ public void testGetIRIByIdentifier() throws Exception {
thrown.expectMessage("Multiple candidate IRIs are found for id: dummy2. None of them are from BFO or RO.");
wrapper.getIRIByIdentifier("dummy2", false);
}

@Test
public void testGetIdentifier() throws Exception {
OWLGraphWrapper wrapper = getGraph("graph/dummy-ontology.owl");

String id1 = wrapper.getIdentifier(IRI.create("http://purl.obolibrary.org/obo/GO_0000001"));
String id2 = wrapper.getIdentifier(IRI.create("http://purl.obolibrary.org/obo/GO_0000002"));
String id3 = wrapper.getIdentifier(IRI.create("http://example.com/X_005"));

assertEquals(id1, "GO:0000001");
assertEquals(id2, "GO:0000002");
assertEquals(id3, "X:5");
}
}
31 changes: 31 additions & 0 deletions OWLTools-Core/src/test/resources/graph/dummy-ontology.owl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0"?>
<rdf:RDF xmlns="http://purl.obolibrary.org/obo/mondo.owl#"
xml:base="http://purl.obolibrary.org/obo/mondo.owl"
xmlns:mondo="http://purl.obolibrary.org/obo/mondo#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:oboInOwl="http://www.geneontology.org/formats/oboInOwl#"
xmlns:obo1="http://purl.obolibrary.org/obo/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:ncbitaxon="http://purl.obolibrary.org/obo/ncbitaxon#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:terms="http://purl.org/dc/terms/"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:ace_lexicon="http://attempto.ifi.uzh.ch/ace_lexicon#"
xmlns:obo="http://purl.obolibrary.org/obo/"
xmlns:foaf="http://xmlns.com/foaf/0.1/"
xmlns:dc="http://purl.org/dc/elements/1.1/">

<owl:AnnotationProperty rdf:about="http://www.geneontology.org/formats/oboInOwl#id"/>

<owl:Class rdf:about="http://purl.obolibrary.org/obo/GO_0000001">
</owl:Class>

<owl:Class rdf:about="http://purl.obolibrary.org/obo/GO_0000002">
<oboInOwl:id rdf:datatype="http://www.w3.org/2001/XMLSchema#string">FAKE:1234</oboInOwl:id>
</owl:Class>

<owl:Class rdf:about="http://example.com/X_005">
<oboInOwl:id rdf:datatype="http://www.w3.org/2001/XMLSchema#string">X:5</oboInOwl:id>
</owl:Class>
</rdf:RDF>

0 comments on commit c498f85

Please sign in to comment.