From e1ec5d66e24dff0f831213323a87a453cd3cc385 Mon Sep 17 00:00:00 2001 From: hdietze Date: Wed, 17 Dec 2014 17:34:09 -0800 Subject: [PATCH] update conversion of OBO alternate identifiers as discussed in https://github.com/owlcs/owlapi/issues/317#issuecomment-67412550 --- .../org/obolibrary/obo2owl/OWLAPIObo2Owl.java | 57 +++++++++++++++++++ .../org/obolibrary/obo2owl/OWLAPIOwl2Obo.java | 52 +++++++++++++++++ .../obolibrary/obo2owl/Obo2OWLConstants.java | 8 +++ 3 files changed, 117 insertions(+) diff --git a/oboformat/src/main/java/org/obolibrary/obo2owl/OWLAPIObo2Owl.java b/oboformat/src/main/java/org/obolibrary/obo2owl/OWLAPIObo2Owl.java index bc5204d078..4fa4970255 100644 --- a/oboformat/src/main/java/org/obolibrary/obo2owl/OWLAPIObo2Owl.java +++ b/oboformat/src/main/java/org/obolibrary/obo2owl/OWLAPIObo2Owl.java @@ -48,6 +48,7 @@ import org.semanticweb.owlapi.model.OWLClassExpression; import org.semanticweb.owlapi.model.OWLDataFactory; import org.semanticweb.owlapi.model.OWLDocumentFormat; +import org.semanticweb.owlapi.model.OWLEntity; import org.semanticweb.owlapi.model.OWLEquivalentClassesAxiom; import org.semanticweb.owlapi.model.OWLIndividual; import org.semanticweb.owlapi.model.OWLLiteral; @@ -719,6 +720,13 @@ public OWLClassExpression trTermFrame(@Nonnull Frame termFrame) { for (String t : termFrame.getTags()) { // System.out.println("tag:"+tag); Collection clauses = termFrame.getClauses(t); + if (OboFormatTag.TAG_ALT_ID.getTag().equals(t)) { + // Generate deprecated and replaced_by details for alternate identifier + Set axioms = translateAltIds(clauses, termFrame.getId(), true); + if (!axioms.isEmpty()) { + add(axioms); + } + } Set axioms = trTermFrameClauses(cls, clauses, t); if (!axioms.isEmpty()) { add(axioms); @@ -728,6 +736,55 @@ public OWLClassExpression trTermFrame(@Nonnull Frame termFrame) { } /** + * Generate axioms for the alternate identifiers of an {@link OWLClass} or {@link OWLObjectProperty}. + * + * @param clauses collection of alt_id clauses + * @param replacedBy OBO style ID + * @param isClass set to true if the alt_id is represents a class, false in case of an property + * @return set of axioms generated for the alt_id clauses + */ + @Nonnull + protected Set translateAltIds( + @Nonnull Collection clauses, @Nonnull String replacedBy, + boolean isClass) { + Set axioms = new HashSet<>(); + for(Clause clause : clauses) { + final String altId = clause.getValue(String.class); + if (altId != null) { + final OWLEntity altIdEntity; + if (isClass) { + altIdEntity = trClass(altId); + } + else { + IRI altIdIRI = oboIdToIRI(altId); + altIdEntity = fac.getOWLObjectProperty(altIdIRI); + } + // entity declaration axiom + axioms.add(fac.getOWLDeclarationAxiom(altIdEntity)); + // annotate as deprecated + axioms.add(fac.getOWLAnnotationAssertionAxiom( + altIdEntity.getIRI(), fac.getOWLAnnotation( + fac.getOWLDeprecated(), + fac.getOWLLiteral(true)))); + // annotate with replaced_by (IAO_0100001) + axioms.add(fac.getOWLAnnotationAssertionAxiom( + altIdEntity.getIRI(), + fac.getOWLAnnotation( fac.getOWLAnnotationProperty( + Obo2OWLVocabulary.IRI_IAO_0100001.iri), + fac.getOWLLiteral(replacedBy)))); + // annotate with obo:IAO_0000231=obo:IAO_0000227 + // 'has obsolescence reason' 'terms merged' + axioms.add(fac.getOWLAnnotationAssertionAxiom( + altIdEntity.getIRI(), fac.getOWLAnnotation( + fac.getOWLAnnotationProperty( + Obo2OWLConstants.IRI_IAO_0000231), + Obo2OWLConstants.IRI_IAO_0000227))); + } + } + return axioms; + } + + /** * Tr term frame clauses. * * @param cls diff --git a/oboformat/src/main/java/org/obolibrary/obo2owl/OWLAPIOwl2Obo.java b/oboformat/src/main/java/org/obolibrary/obo2owl/OWLAPIOwl2Obo.java index 34d00b60f5..c190638c0f 100644 --- a/oboformat/src/main/java/org/obolibrary/obo2owl/OWLAPIOwl2Obo.java +++ b/oboformat/src/main/java/org/obolibrary/obo2owl/OWLAPIOwl2Obo.java @@ -1605,6 +1605,14 @@ protected void tr(@Nonnull OWLDeclarationAxiom axiom) { if (set.isEmpty()) { return; } + // check whether the entity is an alt_id + Optional altIdOptional = isAltId(set, entity); + if (altIdOptional.isPresent()) { + // TODO make sure that the alt_id is declared in the appropriate frame + return; + } + + // translate Frame f = null; if (entity instanceof OWLClass) { f = getTermFrame(entity.asOWLClass()); @@ -1630,6 +1638,50 @@ protected void tr(@Nonnull OWLDeclarationAxiom axiom) { } } + /** + * Check the annotations of entity for axioms declaring it to be an + * obsolete entity, with 'obsolescence reason' being 'term merge', and a + * non-empty 'replaced by' literal. + * + * @param annotations set of annotations for the entity + * @param entity + * @return replaced_by if it is an alt_id + */ + @Nonnull + private Optional isAltId(Set annotations, OWLEntity entity) { + String altId = null; + boolean isMerged = false; + boolean isDeprecated = false; + for (OWLAnnotationAssertionAxiom axiom : annotations) { + OWLAnnotationProperty prop = axiom.getProperty(); + if (prop.isDeprecated()) { + isDeprecated = true; + } + else if (Obo2OWLConstants.IRI_IAO_0000231.equals(prop.getIRI())) { + OWLAnnotationValue value = axiom.getValue(); + Optional asIRI = value.asIRI(); + if (asIRI.isPresent()) { + isMerged = Obo2OWLConstants.IRI_IAO_0000227.equals(asIRI.get()); + } + } + else if (Obo2OWLVocabulary.IRI_IAO_0100001.iri.equals(prop.getIRI())) { + OWLAnnotationValue value = axiom.getValue(); + Optional asLiteral = value.asLiteral(); + if (asLiteral.isPresent()) { + altId = asLiteral.get().getLiteral(); + } + } + } + Optional result; + if (altId != null && isMerged && isDeprecated) { + result = Optional.of(altId); + } + else { + result = Optional.absent(); + } + return result; + } + /** * Gets the identifier. * diff --git a/oboformat/src/main/java/org/obolibrary/obo2owl/Obo2OWLConstants.java b/oboformat/src/main/java/org/obolibrary/obo2owl/Obo2OWLConstants.java index 045d57eb1f..76dec4b514 100644 --- a/oboformat/src/main/java/org/obolibrary/obo2owl/Obo2OWLConstants.java +++ b/oboformat/src/main/java/org/obolibrary/obo2owl/Obo2OWLConstants.java @@ -23,6 +23,14 @@ public class Obo2OWLConstants { @Nonnull public static final String OIOVOCAB_IRI_PREFIX = "http://www.geneontology.org/formats/oboInOwl#"; + /** IRI for the 'has obsolescence reason' annotation property */ + @Nonnull + public static final IRI IRI_IAO_0000231 = IRI.create(DEFAULT_IRI_PREFIX+"IAO_0000231"); + + /** IRI for the 'terms merged' individual */ + @Nonnull + public static final IRI IRI_IAO_0000227 = IRI.create(DEFAULT_IRI_PREFIX+"IAO_0000227"); + /** * @param d * date to format