From 843e5d6a15e1a760227bf6ab165371fdceb9fee0 Mon Sep 17 00:00:00 2001 From: Jim Balhoff Date: Sun, 13 May 2018 11:07:05 -0400 Subject: [PATCH] Remove use of ModelWriterHelper to eliminate unneccessary Tbox axioms in model graphs. Related to #185. --- .../minerva/ModelReaderHelper.java | 11 +- .../minerva/ModelWriterHelper.java | 192 ------------------ .../minerva/server/StartUpTool.java | 2 - .../minerva/ModelDecorationTest.java | 95 --------- 4 files changed, 9 insertions(+), 291 deletions(-) delete mode 100644 minerva-server/src/main/java/org/geneontology/minerva/ModelWriterHelper.java delete mode 100644 minerva-server/src/test/java/org/geneontology/minerva/ModelDecorationTest.java diff --git a/minerva-server/src/main/java/org/geneontology/minerva/ModelReaderHelper.java b/minerva-server/src/main/java/org/geneontology/minerva/ModelReaderHelper.java index 31e98d3a..819a7bea 100644 --- a/minerva-server/src/main/java/org/geneontology/minerva/ModelReaderHelper.java +++ b/minerva-server/src/main/java/org/geneontology/minerva/ModelReaderHelper.java @@ -19,9 +19,16 @@ import org.semanticweb.owlapi.model.RemoveAxiom; import org.semanticweb.owlapi.model.RemoveOntologyAnnotation; +/** + * The axioms tagged with the lego:derived property are removed upon reading in the model. + * This system is not really used anymore, but this class should still be active while + * some models still contain these annotated axioms. + */ public class ModelReaderHelper implements PostLoadOntologyFilter { public static final ModelReaderHelper INSTANCE = new ModelReaderHelper(); + public static final IRI DERIVED_IRI = IRI.create("http://geneontology.org/lego/derived"); + public static final String DERIVED_VALUE = "true"; private ModelReaderHelper() { // no public constructor, use instance @@ -31,7 +38,7 @@ private ModelReaderHelper() { public OWLOntology filter(OWLOntology model) { final OWLOntologyManager m = model.getOWLOntologyManager(); final OWLDataFactory f = m.getOWLDataFactory(); - final OWLAnnotationProperty derivedProperty = f.getOWLAnnotationProperty(ModelWriterHelper.DERIVED_IRI); + final OWLAnnotationProperty derivedProperty = f.getOWLAnnotationProperty(DERIVED_IRI); List allChanges = new ArrayList(); @@ -81,7 +88,7 @@ public String visit(OWLLiteral literal) { return literal.getLiteral(); } }); - if (value != null && ModelWriterHelper.DERIVED_VALUE.equalsIgnoreCase(value)) { + if (value != null && DERIVED_VALUE.equalsIgnoreCase(value)) { return true; } } diff --git a/minerva-server/src/main/java/org/geneontology/minerva/ModelWriterHelper.java b/minerva-server/src/main/java/org/geneontology/minerva/ModelWriterHelper.java deleted file mode 100644 index b5674242..00000000 --- a/minerva-server/src/main/java/org/geneontology/minerva/ModelWriterHelper.java +++ /dev/null @@ -1,192 +0,0 @@ -package org.geneontology.minerva; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import org.geneontology.minerva.BlazegraphMolecularModelManager.PreFileSaveHandler; -import org.geneontology.minerva.MolecularModelManager.UnknownIdentifierException; -import org.geneontology.minerva.curie.CurieHandler; -import org.geneontology.minerva.json.JsonModel; -import org.geneontology.minerva.json.MolecularModelJsonRenderer; -import org.geneontology.minerva.lookup.ExternalLookupService; -import org.geneontology.minerva.lookup.ExternalLookupService.LookupEntry; -import org.geneontology.minerva.server.handler.OperationsTools; -import org.geneontology.minerva.taxon.FindTaxonTool; -import org.obolibrary.obo2owl.Obo2OWLConstants; -import org.semanticweb.owlapi.model.AddAxiom; -import org.semanticweb.owlapi.model.AddOntologyAnnotation; -import org.semanticweb.owlapi.model.AxiomType; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLAnnotation; -import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom; -import org.semanticweb.owlapi.model.OWLAnnotationProperty; -import org.semanticweb.owlapi.model.OWLAxiom; -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLClassAssertionAxiom; -import org.semanticweb.owlapi.model.OWLClassExpression; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLIndividual; -import org.semanticweb.owlapi.model.OWLNamedIndividual; -import org.semanticweb.owlapi.model.OWLObjectProperty; -import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom; -import org.semanticweb.owlapi.model.OWLOntology; -import org.semanticweb.owlapi.model.OWLOntologyChange; -import org.semanticweb.owlapi.model.OWLOntologyManager; -import org.semanticweb.owlapi.util.OWLClassExpressionVisitorAdapter; - -import owltools.util.OwlHelper; -import owltools.vocab.OBOUpperVocabulary; - -import com.google.common.base.Optional; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; - -public class ModelWriterHelper implements PreFileSaveHandler { - - public static final IRI DERIVED_IRI = IRI.create("http://geneontology.org/lego/derived"); - public static final String DERIVED_VALUE = "true"; - - private final CurieHandler curieHandler; - private final ExternalLookupService lookupService; - private final IRI shortIdPropIRI; - private final IRI enabledByIRI; - - public ModelWriterHelper(CurieHandler curieHandler, ExternalLookupService lookupService) { - this.curieHandler = curieHandler; - this.lookupService = lookupService; - shortIdPropIRI = IRI.create(Obo2OWLConstants.OIOVOCAB_IRI_PREFIX+"id"); - enabledByIRI = OBOUpperVocabulary.GOREL_enabled_by.getIRI(); - } - - List generateLabelsAndIds(OWLOntology model, ExternalLookupService lookupService) throws UnknownIdentifierException { - if (curieHandler == null && lookupService == null) { - return Collections.emptyList(); - } - final OWLOntologyManager m = model.getOWLOntologyManager(); - final OWLDataFactory df = m.getOWLDataFactory(); - IRI displayLabelPropIri = df.getRDFSLabel().getIRI(); - OWLAnnotationProperty shortIdProp = df.getOWLAnnotationProperty(shortIdPropIRI); - OWLAnnotationProperty displayLabelProp = df.getOWLAnnotationProperty(displayLabelPropIri); - OWLObjectProperty enabledByProp = df.getOWLObjectProperty(enabledByIRI); - // annotations to mark the axiom as generated - final OWLAnnotationProperty tagProperty = df.getOWLAnnotationProperty(DERIVED_IRI); - final Set tags = Collections.singleton(df.getOWLAnnotation(tagProperty, df.getOWLLiteral(DERIVED_VALUE))); - - // collect changes - List allChanges = new ArrayList(); - - // set model id and json model - if (curieHandler != null) { - Optional ontologyIRI = model.getOntologyID().getOntologyIRI(); - if (ontologyIRI.isPresent()) { - final String modelId = curieHandler.getCuri(ontologyIRI.get()); - final OWLAnnotation modelAnnotation = df.getOWLAnnotation(shortIdProp, df.getOWLLiteral(modelId), tags); - allChanges.add(new AddOntologyAnnotation(model, modelAnnotation)); - } - } - - // find relevant classes - final Set importsClosure = model.getImportsClosure(); - Set usedClasses = new HashSet(); - Set individuals = model.getIndividualsInSignature(); - for (OWLNamedIndividual individual : individuals) { - Set axioms = model.getClassAssertionAxioms(individual); - for (OWLClassAssertionAxiom axiom : axioms) { - usedClasses.addAll(axiom.getClassesInSignature()); - } - } - - // find entity classes - final Set bioentityClasses = new HashSet(); - Set candidateAxioms = model.getAxioms(AxiomType.OBJECT_PROPERTY_ASSERTION); - for (OWLObjectPropertyAssertionAxiom axiom : candidateAxioms) { - if (enabledByProp.equals(axiom.getProperty())) { - OWLIndividual object = axiom.getObject(); - if (object.isNamed()) { - // assume named object for enabled_by is a bioentity - OWLNamedIndividual o = object.asOWLNamedIndividual(); - Set types = OwlHelper.getTypes(o, model); - for (OWLClassExpression ce : types) { - ce.accept(new OWLClassExpressionVisitorAdapter(){ - - @Override - public void visit(OWLClass cls) { - bioentityClasses.add(cls); - } - }); - } - } - } - } - usedClasses.addAll(bioentityClasses); - FindTaxonTool taxonTool = new FindTaxonTool(curieHandler, model.getOWLOntologyManager().getOWLDataFactory()); - - // check label and ids for used classes - for (OWLClass cls : usedClasses) { - boolean hasLabelAxiom = false; - boolean hasShortIdAxiom = false; - Set existingAnnotations = new HashSet(); - for(OWLOntology ont : importsClosure) { - existingAnnotations.addAll(ont.getAnnotationAssertionAxioms(cls.getIRI())); - } - for (OWLAnnotationAssertionAxiom axiom : existingAnnotations) { - if (shortIdProp.equals(axiom.getProperty())) { - hasShortIdAxiom = true; - } - else if (displayLabelProp.equals(axiom.getProperty())) { - hasLabelAxiom = true; - } - } - if (bioentityClasses.contains(cls) && curieHandler != null && lookupService != null) { - // check for taxon axiom - String taxon = taxonTool.getEntityTaxon(curieHandler.getCuri(cls), model); - if (taxon == null) { - // find taxon via Golr - List lookup = lookupService.lookup(cls.getIRI()); - if (lookup != null && !lookup.isEmpty()) { - taxon = lookup.get(0).taxon; - if (taxon != null) { - OWLAxiom axiom = taxonTool.createTaxonAxiom(cls, taxon, model, tags); - allChanges.add(new AddAxiom(model, axiom)); - } - } - } - } - if (hasLabelAxiom == false && lookupService != null) { - // find label via Golr - List lookup = lookupService.lookup(cls.getIRI()); - if (lookup != null && !lookup.isEmpty()) { - String lbl = lookup.get(0).label; - if (lbl != null) { - OWLAxiom axiom = df.getOWLAnnotationAssertionAxiom(displayLabelProp, cls.getIRI(), df.getOWLLiteral(lbl), tags); - allChanges.add(new AddAxiom(model, axiom)); - } - } - } - if (hasShortIdAxiom == false && curieHandler != null) { - // id shorthand via curie - String curie = curieHandler.getCuri(cls); - OWLAxiom axiom = df.getOWLAnnotationAssertionAxiom(shortIdProp, cls.getIRI(), df.getOWLLiteral(curie), tags); - allChanges.add(new AddAxiom(model, axiom)); - } - } - if (allChanges.isEmpty() == false) { - // add declaration axioms for annotation properties - // this is a bug fix - allChanges.add(new AddAxiom(model, df.getOWLDeclarationAxiom(tagProperty))); - allChanges.add(new AddAxiom(model, df.getOWLDeclarationAxiom(shortIdProp))); - allChanges.add(new AddAxiom(model, df.getOWLDeclarationAxiom(displayLabelProp))); - } - return allChanges; - } - - @Override - public List handle(OWLOntology model) throws UnknownIdentifierException { - List allChanges = generateLabelsAndIds(model, lookupService); - model.getOWLOntologyManager().applyChanges(allChanges); - return allChanges; - } -} diff --git a/minerva-server/src/main/java/org/geneontology/minerva/server/StartUpTool.java b/minerva-server/src/main/java/org/geneontology/minerva/server/StartUpTool.java index 2da51478..9501903b 100644 --- a/minerva-server/src/main/java/org/geneontology/minerva/server/StartUpTool.java +++ b/minerva-server/src/main/java/org/geneontology/minerva/server/StartUpTool.java @@ -12,7 +12,6 @@ import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; import org.geneontology.minerva.ModelReaderHelper; -import org.geneontology.minerva.ModelWriterHelper; import org.geneontology.minerva.UndoAwareMolecularModelManager; import org.geneontology.minerva.curie.CurieHandler; import org.geneontology.minerva.curie.CurieMappings; @@ -331,7 +330,6 @@ public static Server startUp(final MinervaStartUpConfig conf) conf.curieHandler, conf.modelIdPrefix, conf.journalFile, conf.exportFolder); // set pre and post file handlers models.addPostLoadOntologyFilter(ModelReaderHelper.INSTANCE); - models.addPreFileSaveHandler(new ModelWriterHelper(conf.curieHandler, conf.lookupService)); // start server Server server = startUp(models, conf); diff --git a/minerva-server/src/test/java/org/geneontology/minerva/ModelDecorationTest.java b/minerva-server/src/test/java/org/geneontology/minerva/ModelDecorationTest.java deleted file mode 100644 index 63f795bc..00000000 --- a/minerva-server/src/test/java/org/geneontology/minerva/ModelDecorationTest.java +++ /dev/null @@ -1,95 +0,0 @@ -package org.geneontology.minerva; - -import static org.junit.Assert.assertEquals; - -import java.io.ByteArrayOutputStream; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Set; - -import org.geneontology.minerva.curie.CurieHandler; -import org.geneontology.minerva.curie.CurieMappings; -import org.geneontology.minerva.curie.DefaultCurieHandler; -import org.geneontology.minerva.curie.MappedCurieHandler; -import org.geneontology.minerva.lookup.ExternalLookupService; -import org.geneontology.minerva.lookup.ExternalLookupService.LookupEntry; -import org.geneontology.minerva.lookup.TableLookupService; -import org.junit.Test; -import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.formats.ManchesterSyntaxDocumentFormat; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLAnnotation; -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLNamedIndividual; -import org.semanticweb.owlapi.model.OWLOntology; -import org.semanticweb.owlapi.model.OWLOntologyChange; -import org.semanticweb.owlapi.model.OWLOntologyManager; - -/** - * Tests for {@link ModelReaderHelper} and {@link ModelWriterHelper}. - */ -public class ModelDecorationTest { - - @Test - public void testDecorate1() throws Exception { - OWLOntologyManager m = OWLManager.createOWLOntologyManager(); - OWLDataFactory f = m.getOWLDataFactory(); - - CurieMappings defaultMappings = DefaultCurieHandler.loadDefaultMappings(); - CurieMappings localMappings = new CurieMappings.SimpleCurieMappings(Collections.singletonMap("http://testmodel.geneontology.org/","testmodel")); - CurieHandler curieHandler = new MappedCurieHandler(defaultMappings, localMappings); - - OWLOntology model = m.createOntology(IRI.create("http://testmodel.geneontology.org/0001")); - - // add class without a label or id - IRI cIRI = IRI.create("http://purl.obolibrary.org/obo/GO_0001"); - OWLClass c = f.getOWLClass(cIRI); - m.addAxiom(model, f.getOWLDeclarationAxiom(c)); - - // add individual using the class as type - OWLNamedIndividual i = f.getOWLNamedIndividual(IRI.create("http://testmodel.geneontology.org/0001/0001")); - m.addAxiom(model, f.getOWLDeclarationAxiom(i)); - m.addAxiom(model, f.getOWLClassAssertionAxiom(c, i)); - - - List testEntries = new ArrayList(); - testEntries.add(new LookupEntry(cIRI, "TEST CLASS 1", "Class", null)); - ExternalLookupService lookup = new TableLookupService(testEntries); - - int originalAxiomCount = model.getAxiomCount(); - int originalAnnotationCount = model.getAnnotations().size(); - - ModelWriterHelper w = new ModelWriterHelper(curieHandler, lookup); - List changes = w.handle(model); - - // model annotations - // id, label for cls - assertEquals(6, changes.size()); // 3 + 3 declarations - - assertEquals(5+originalAxiomCount, model.getAxiomCount()); - final Set modelAnnotationsAfter = model.getAnnotations(); - assertEquals(1+originalAnnotationCount, modelAnnotationsAfter.size()); - - - //System.out.println(render(model)); - - ModelReaderHelper.INSTANCE.filter(model); - - assertEquals(originalAxiomCount+3, model.getAxiomCount()); // declarations are fine - assertEquals(originalAnnotationCount, model.getAnnotations().size()); - - //System.out.println(render(model)); - } - - static String render(OWLOntology o) throws Exception { - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - o.getOWLOntologyManager().saveOntology(o, new ManchesterSyntaxDocumentFormat(), outputStream); - outputStream.flush(); - outputStream.close(); - String s = outputStream.toString(); - return s; - } - -}