Skip to content

Commit

Permalink
Merge pull request #259 from owlcollab/various
Browse files Browse the repository at this point in the history
WIP: Adding ability to tag inferrable SubClassOf axioms
  • Loading branch information
cmungall authored Jun 8, 2018
2 parents 708ba8b + 403aa16 commit 452b4aa
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 22 deletions.
2 changes: 2 additions & 0 deletions OWLTools-Core/src/main/java/owltools/mooncat/Mooncat.java
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,8 @@ public static void retainAxiomsInPropertySubset(OWLOntology ont, Set<OWLObjectPr
* given an ontology *ont* and a set of object properties *filterProps*, remove all axioms from ontology that
* have an object property P in their signature where P is not in *filterProps*
*
* use basic reasoning to map up more specific relationships
*
* @param ont
* @param filterProps
* @param reasoner
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package owltools.mooncat;

import java.util.Collections;
import java.util.Set;

import org.apache.log4j.Logger;
import org.semanticweb.owlapi.model.AxiomType;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLAnnotation;
import org.semanticweb.owlapi.model.OWLAnnotationProperty;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.model.OWLSubClassOfAxiom;
import org.semanticweb.owlapi.reasoner.NodeSet;
import org.semanticweb.owlapi.reasoner.OWLReasoner;

import owltools.graph.AxiomAnnotationTools.AxiomAnnotationsChanger;

public class RedundantAxiomTagger {
private static Logger LOG = Logger.getLogger(RedundantAxiomTagger.class);

public static void tagRedundantAxioms(OWLReasoner reasoner) {
OWLOntology ont = reasoner.getRootOntology();
OWLOntologyManager mgr = ont.getOWLOntologyManager();
OWLDataFactory df = mgr.getOWLDataFactory();
OWLAnnotationProperty anProp = df.getOWLAnnotationProperty(IRI.create("http://www.geneontology.org/formats/oboInOwl#source"));
for (OWLSubClassOfAxiom ax : ont.getAxioms(AxiomType.SUBCLASS_OF)) {
if (!ax.getSuperClass().isAnonymous()) {
OWLClass supc = (OWLClass) ax.getSuperClass();

mgr.removeAxiom(ont, ax);
reasoner.flush();
NodeSet<OWLClass> ancs = reasoner.getSuperClasses(ax.getSubClass(), false);
//LOG.info(ax + " ANCS="+ancs);
if (ancs.containsEntity( supc)) {
String direct = "indirect";
if (reasoner.getSuperClasses(ax.getSubClass(), true).containsEntity( supc)) {
direct = "direct";
}
LOG.info("SCA = "+ax+" D="+direct);
OWLAnnotation ann = df.getOWLAnnotation(anProp, df.getOWLLiteral(direct));
OWLAxiom newAxiom = changeAxiomAnnotations(ax, Collections.singleton(ann), df);
mgr.addAxiom(ont, newAxiom);
}
else {
// put it back
mgr.addAxiom(ont, ax);
}
}
}

}

/**
* Update the given axiom to the new set of axiom annotation. Recreates the
* axiom with the new annotations using the given factory.
*
* @param axiom
* @param annotations
* @param factory
* @return newAxiom
*/
public static OWLAxiom changeAxiomAnnotations(OWLAxiom axiom, Set<OWLAnnotation> annotations, OWLDataFactory factory) {
final AxiomAnnotationsChanger changer = new AxiomAnnotationsChanger(annotations, factory);
final OWLAxiom newAxiom = axiom.accept(changer);
return newAxiom;
}


}
46 changes: 24 additions & 22 deletions OWLTools-Runner/src/main/java/owltools/cli/CommandRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,13 @@
import java.util.Vector;
import java.util.stream.Collectors;

import javax.annotation.processing.SupportedOptions;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.eclipse.jetty.server.Server;
import org.forester.phylogeny.data.Annotation;
import org.geneontology.reasoner.ExpressionMaterializingReasoner;
import org.geneontology.reasoner.ExpressionMaterializingReasonerFactory;
import org.geneontology.reasoner.OWLExtendedReasoner;
Expand Down Expand Up @@ -130,6 +127,21 @@
import org.semanticweb.owlapi.vocab.OWL2Datatype;
import org.semanticweb.owlapi.vocab.OWLRDFVocabulary;

import com.clarkparsia.owlapi.explanation.DefaultExplanationGenerator;
import com.clarkparsia.owlapi.explanation.ExplanationGenerator;
import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

import de.derivo.sparqldlapi.Query;
import de.derivo.sparqldlapi.QueryArgument;
import de.derivo.sparqldlapi.QueryBinding;
import de.derivo.sparqldlapi.QueryEngine;
import de.derivo.sparqldlapi.QueryResult;
import de.derivo.sparqldlapi.exceptions.QueryEngineException;
import de.derivo.sparqldlapi.exceptions.QueryParserException;
import de.derivo.sparqldlapi.types.QueryArgumentType;
import owltools.InferenceBuilder.OWLClassFilter;
import owltools.RedundantInferences;
import owltools.RedundantInferences.RedundantAxiom;
Expand Down Expand Up @@ -178,6 +190,7 @@
import owltools.mooncat.PropertyViewOntologyBuilder;
import owltools.mooncat.ProvenanceReasonerWrapper;
import owltools.mooncat.QuerySubsetGenerator;
import owltools.mooncat.RedundantAxiomTagger;
import owltools.mooncat.SpeciesMergeUtil;
import owltools.mooncat.SpeciesSubsetterUtil;
import owltools.mooncat.TransformationUtils;
Expand All @@ -198,23 +211,6 @@
import uk.ac.manchester.cs.owlapi.modularity.ModuleType;
import uk.ac.manchester.cs.owlapi.modularity.SyntacticLocalityModuleExtractor;

import com.clarkparsia.owlapi.explanation.DefaultExplanationGenerator;
import com.clarkparsia.owlapi.explanation.ExplanationGenerator;
import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.common.collect.Sets.SetView;

import de.derivo.sparqldlapi.Query;
import de.derivo.sparqldlapi.QueryArgument;
import de.derivo.sparqldlapi.QueryBinding;
import de.derivo.sparqldlapi.QueryEngine;
import de.derivo.sparqldlapi.QueryResult;
import de.derivo.sparqldlapi.exceptions.QueryEngineException;
import de.derivo.sparqldlapi.exceptions.QueryParserException;
import de.derivo.sparqldlapi.types.QueryArgumentType;

/**
* An instance of this class can execute owltools commands in sequence.
*
Expand Down Expand Up @@ -709,7 +705,7 @@ else if (opts.nextEq("--remove-uninstantiated-classes")) {
g.getManager().removeAxioms(g.getSourceOntology(), rmAxioms);
}
else if (opts.nextEq("--make-subset-by-properties")) {
opts.info("PROPERTY-LIST",
opts.info("[-n] [-f] PROPERTY-LIST",
"make an ontology subset that excludes axioms that use properties not in the specified set.\n"+
" Note the ontology should be relaxed e.g. X=A and R some B ==> X SubClassOf A" +
" A property list is a space-separated list of object property OBO-IDs, shorthands, URIs, or labels.\n"+
Expand All @@ -720,9 +716,11 @@ else if (opts.nextEq("--make-subset-by-properties")) {
boolean isSuppressRemoveDangling = false;
while (opts.hasOpts()) {
if (opts.nextEq("-f|--force")) {
opts.info("", "do not removing dangling");
isForceRemoveDangling = true;
}
else if (opts.nextEq("-n|--no-remove-dangling")) {
opts.info("", "do not removing dangling");
isSuppressRemoveDangling = true;
}
else {
Expand Down Expand Up @@ -3930,7 +3928,7 @@ else if (opts.nextEq("--translate-undeclared-to-classes")) {
OWLClass c = g.getDataFactory().getOWLClass((IRI)sub);
OWLDeclarationAxiom ax = g.getDataFactory().getOWLDeclarationAxiom(c);
g.getManager().addAxiom(g.getSourceOntology(), ax);
}
}
}
}
}
Expand Down Expand Up @@ -5108,6 +5106,10 @@ public void assertAboxInferences(Opts opts) throws Exception {
mgr.addAxioms(g.getSourceOntology(), newAxioms);
}

@CLIMethod("--tag-entailed-axioms")
public void tagEntailedAxioms(Opts opts) throws Exception {
RedundantAxiomTagger.tagRedundantAxioms(reasoner);
}
@CLIMethod("--assert-inferred-subclass-axioms")
public void assertInferredSubClassAxioms(Opts opts) throws Exception {
opts.info("[--removeRedundant] [--keepRedundant] [--always-assert-super-classes] [--markIsInferred] [--useIsInferred] [--ignoreNonInferredForRemove] [--allowEquivalencies] [--reportProfile]",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,22 @@ public void testRunner() throws Exception {
System.setOut(out);
}

@Test
public void testRunnerOboIds() throws Exception {

PrintStream out = System.out;
System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("target/oboids.json"))));

String confpath = getResource("ont-config.yaml").getAbsolutePath();

init();
load("obo-ids-test.owl");
run("--reasoner elk");
run("--solr-url mock");
run("--solr-config "+ confpath);
run("--solr-load-ontology");
System.setOut(out);
System.out.println("Done!");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.net.MalformedURLException;

import org.apache.log4j.Logger;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.common.SolrInputDocument;

Expand All @@ -10,13 +11,16 @@

public class MockFlexSolrDocumentLoader extends FlexSolrDocumentLoader implements MockSolrDocumentLoader {

private static Logger LOG = Logger.getLogger(MockFlexSolrDocumentLoader.class);

public MockFlexSolrDocumentLoader(FlexCollection c) throws MalformedURLException {
super((SolrServer)null, c);
}

final MockSolrDocumentCollection documentCollection = new MockSolrDocumentCollection();
@Override
public void add(SolrInputDocument doc) {
LOG.info("Adding: "+doc);
documentCollection.add(doc);
}

Expand Down

0 comments on commit 452b4aa

Please sign in to comment.