SpeciesMergeUtil { public void merge() throws UnmergeableOntologyException { ont = graph.getSourceOntology(); mgr = ont.getOWLOntologyManager(); fac = mgr.getOWLDataFactory(); createMap(); for (OWLClass c : ssClasses) { if (c.isBottomEntity()) continue; if (!reasoner.isSatisfiable(c)) { throw new UnmergeableOntologyException("unsatisfiable class: "+c); } LOG.info("ssClass = " + c); Set<OWLAxiom> axioms = new HashSet<OWLAxiom>(); Set<OWLAxiom> newAxioms = new HashSet<OWLAxiom>(); axioms.addAll(ont.getAxioms(c, Imports.EXCLUDED)); axioms.addAll(ont.getAnnotationAssertionAxioms(c.getIRI())); for (OWLClass p : reasoner.getSuperClasses(c, true).getFlattened()) { axioms.add(fac.getOWLSubClassOfAxiom(c, p)); } for (OWLAxiom axiom : axioms) { OWLAxiom newAxiom; if (axiom instanceof OWLSubClassOfAxiom) newAxiom = tr((OWLSubClassOfAxiom) axiom); else if (axiom instanceof OWLEquivalentClassesAxiom) newAxiom = tr((OWLEquivalentClassesAxiom) axiom); else if (axiom instanceof OWLAnnotationAssertionAxiom) newAxiom = tr(c, (OWLAnnotationAssertionAxiom) axiom); else newAxiom = null; if (newAxiom != null && ecmap.containsKey(c)) { for (OWLClass sc : newAxiom.getClassesInSignature()) { if (isSkippable(sc)) { newAxiom = null; break; } } } if (newAxiom == null) continue; if (newAxiom != null) { if (!newAxiom.getClassesInSignature().contains( rootSpeciesSpecificClass)) newAxioms.add(newAxiom); } else { } } mgr.removeAxioms(ont, axioms); mgr.addAxioms(ont, newAxioms); } } SpeciesMergeUtil(OWLGraphWrapper g); void createMap(); void merge(); OWLAxiom tr(OWLSubClassOfAxiom ax); OWLAxiom tr(OWLEquivalentClassesAxiom ax); boolean isSkippable(OWLClass c); public Set<OWLObjectProperty> includedProperties; public OWLReasoner reasoner; public String suffix; public OWLObjectProperty viewProperty; public OWLClass taxClass; }
@Test public void testMergeSpecies() throws Exception { ParserWrapper p = new ParserWrapper(); OWLOntology owlOntology = p.parse(getResourceIRIString("speciesMergeTest.obo")); OWLGraphWrapper graph = new OWLGraphWrapper(owlOntology); OWLReasonerFactory rf = new ElkReasonerFactory(); OWLReasoner reasoner = rf.createReasoner(graph.getSourceOntology()); SpeciesMergeUtil smu = new SpeciesMergeUtil(graph); smu.viewProperty = graph.getOWLObjectPropertyByIdentifier("BFO:0000050"); smu.taxClass = graph.getOWLClassByIdentifier("T:1"); smu.reasoner = reasoner; smu.suffix = "coelocanth"; smu.merge(); p.saveOWL(smu.ont, new OBODocumentFormat(), "target/speciesMergeOut.obo"); }
@Test public void testMergeFly() throws Exception { ParserWrapper p = new ParserWrapper(); OWLOntology owlOntology = p.parse(getResourceIRIString("interneuron-fly.obo")); Set<OWLClass> clsBefore = owlOntology.getClassesInSignature(); OWLGraphWrapper graph = new OWLGraphWrapper(owlOntology); OWLReasonerFactory rf = new ElkReasonerFactory(); OWLReasoner reasoner = rf.createReasoner(graph.getSourceOntology()); SpeciesMergeUtil smu = new SpeciesMergeUtil(graph); smu.viewProperty = graph.getOWLObjectPropertyByIdentifier("BFO:0000050"); smu.taxClass = graph.getOWLClassByIdentifier("NCBITaxon:7227"); smu.reasoner = reasoner; smu.suffix = "fly"; smu.includedProperties = Collections.singleton(smu.viewProperty); smu.merge(); Set<OWLClass> clsAfter = smu.ont.getClassesInSignature(); LOG.info("Before: "+clsBefore.size()); LOG.info("After: "+clsAfter.size()); assertEquals(100, clsBefore.size()); assertEquals(90, clsAfter.size()); p.saveOWL(smu.ont, new OBODocumentFormat(), "target/flyMergeOut.obo"); }