From ac9b1a7f323df38e313bebbf62208e19ff3c65b5 Mon Sep 17 00:00:00 2001 From: HyeongSik Kim Date: Thu, 12 Apr 2018 15:34:14 -0700 Subject: [PATCH 1/3] fix getIdentifier method to use oboInOwl#id for non-purl ones --- .../graph/OWLGraphWrapperExtended.java | 42 ++++++++++++++++--- .../graph/OWLGraphWrapperExtendedTest.java | 13 ++++++ .../test/resources/graph/dummy-ontology.owl | 31 ++++++++++++++ 3 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 OWLTools-Core/src/test/resources/graph/dummy-ontology.owl diff --git a/OWLTools-Core/src/main/java/owltools/graph/OWLGraphWrapperExtended.java b/OWLTools-Core/src/main/java/owltools/graph/OWLGraphWrapperExtended.java index 44667a5b0..e0f3216c3 100644 --- a/OWLTools-Core/src/main/java/owltools/graph/OWLGraphWrapperExtended.java +++ b/OWLTools-Core/src/main/java/owltools/graph/OWLGraphWrapperExtended.java @@ -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; @@ -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; @@ -65,7 +68,7 @@ * @see OWLGraphWrapperBasic */ public class OWLGraphWrapperExtended extends OWLGraphWrapperBasic { - + private static final Logger LOG = Logger.getLogger(OWLGraphWrapperExtended.class); private Map altIdMap = null; protected OWLGraphWrapperExtended(OWLOntology ontology) { @@ -878,19 +881,48 @@ public String getIdentifier(OWLObject owlObject, List 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 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().trim()); + } + } + + 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); diff --git a/OWLTools-Core/src/test/java/owltools/graph/OWLGraphWrapperExtendedTest.java b/OWLTools-Core/src/test/java/owltools/graph/OWLGraphWrapperExtendedTest.java index e22e75e97..e17ee43cd 100644 --- a/OWLTools-Core/src/test/java/owltools/graph/OWLGraphWrapperExtendedTest.java +++ b/OWLTools-Core/src/test/java/owltools/graph/OWLGraphWrapperExtendedTest.java @@ -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"); + } } \ No newline at end of file diff --git a/OWLTools-Core/src/test/resources/graph/dummy-ontology.owl b/OWLTools-Core/src/test/resources/graph/dummy-ontology.owl new file mode 100644 index 000000000..338e9fbb8 --- /dev/null +++ b/OWLTools-Core/src/test/resources/graph/dummy-ontology.owl @@ -0,0 +1,31 @@ + + + + + + + + + + FAKE:1234 + + + + X:5 + + From 6603cda9a0d55a8de166ddcbfe5aeb9db4c4614d Mon Sep 17 00:00:00 2001 From: HyeongSik Kim Date: Mon, 16 Apr 2018 16:50:14 -0700 Subject: [PATCH 2/3] removed unnecessary calling of trim --- .../src/main/java/owltools/graph/OWLGraphWrapperExtended.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OWLTools-Core/src/main/java/owltools/graph/OWLGraphWrapperExtended.java b/OWLTools-Core/src/main/java/owltools/graph/OWLGraphWrapperExtended.java index e0f3216c3..68c5cbd58 100644 --- a/OWLTools-Core/src/main/java/owltools/graph/OWLGraphWrapperExtended.java +++ b/OWLTools-Core/src/main/java/owltools/graph/OWLGraphWrapperExtended.java @@ -912,7 +912,7 @@ public String getIdentifier(IRI iriId) { } OWLLiteral literal = literalOpt.get(); - return (String) SerializationUtils.clone(literal.getLiteral().trim()); + return (String) SerializationUtils.clone(literal.getLiteral()); } } From 46b61543df4da98ddd6fe36679fdf514188dc28b Mon Sep 17 00:00:00 2001 From: Chris Mungall Date: Wed, 18 Apr 2018 17:14:21 -0700 Subject: [PATCH 3/3] Update dummy-ontology.owl --- OWLTools-Core/src/test/resources/graph/dummy-ontology.owl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OWLTools-Core/src/test/resources/graph/dummy-ontology.owl b/OWLTools-Core/src/test/resources/graph/dummy-ontology.owl index 338e9fbb8..deeab08a5 100644 --- a/OWLTools-Core/src/test/resources/graph/dummy-ontology.owl +++ b/OWLTools-Core/src/test/resources/graph/dummy-ontology.owl @@ -12,7 +12,7 @@ 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://www.geneontology.org/formats/oboInOwl#http://purl.obolibrary.org/obo/" + xmlns:obo="http://purl.obolibrary.org/obo/" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:dc="http://purl.org/dc/elements/1.1/">