Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add back annotation extension filtering based on evidence. #147

Merged
merged 4 commits into from
Oct 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ else if (opts.nextEq("--ontology")) {
CurieMappings localMappings = new CurieMappings.SimpleCurieMappings(Collections.singletonMap(modelIdcurie, modelIdPrefix));
CurieHandler curieHandler = new MappedCurieHandler(DefaultCurieHandler.loadDefaultMappings(), localMappings);
for (IRI modelIRI : m3.getAvailableModelIds()) {
String gpad = new GPADSPARQLExport(curieHandler, null, m3.getLegacyRelationShorthandIndex()).exportGPAD(m3.createInferredModel(modelIRI));
String gpad = new GPADSPARQLExport(curieHandler, m3.getLegacyRelationShorthandIndex()).exportGPAD(m3.createInferredModel(modelIRI));
String fileName = StringUtils.replaceOnce(modelIRI.toString(), modelIdPrefix, "") + ".gpad";
Writer writer = new OutputStreamWriter(new FileOutputStream(Paths.get(gpadOutputFolder, fileName).toFile()), StandardCharsets.UTF_8);
writer.write(gpad);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,18 @@

import org.geneontology.minerva.curie.CurieHandler;
import org.geneontology.minerva.legacy.sparql.GPADData.ConjunctiveExpression;
import org.geneontology.minerva.lookup.ExternalLookupService;
import org.semanticweb.owlapi.model.IRI;

public class GPADRenderer {

private final CurieHandler curieHandler;
private final Map<IRI, String> relationShorthandIndex;
//private final ExternalLookupService lookupService;

public static final String HEADER = "!gpa-version: 1.2";

public GPADRenderer(CurieHandler handler, ExternalLookupService lookup, Map<IRI, String> shorthandIndex) {
public GPADRenderer(CurieHandler handler, Map<IRI, String> shorthandIndex) {
this.curieHandler = handler;
this.relationShorthandIndex = shorthandIndex;
//this.lookupService = lookup;
}

public String renderAll(Collection<GPADData> data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.apache.log4j.Logger;
import org.geneontology.minerva.curie.CurieHandler;
import org.geneontology.minerva.legacy.sparql.GPADData.ConjunctiveExpression;
import org.geneontology.minerva.lookup.ExternalLookupService;
import org.geneontology.rules.engine.Explanation;
import org.geneontology.rules.engine.WorkingMemory;
import org.geneontology.rules.util.Bridge;
Expand All @@ -53,7 +52,7 @@ public class GPADSPARQLExport {
private static final String BP = "http://purl.obolibrary.org/obo/GO_0008150";
private static final String CC = "http://purl.obolibrary.org/obo/GO_0005575";
private static final Set<String> rootTerms = new HashSet<>(Arrays.asList(MF, BP, CC));

private static String mainQuery;
static {
try {
Expand All @@ -80,12 +79,10 @@ public class GPADSPARQLExport {
}
private final CurieHandler curieHandler;
private final Map<IRI, String> relationShorthandIndex;
private final ExternalLookupService lookupService;

public GPADSPARQLExport(CurieHandler handler, ExternalLookupService lookup, Map<IRI, String> shorthandIndex) {
public GPADSPARQLExport(CurieHandler handler, Map<IRI, String> shorthandIndex) {
this.curieHandler = handler;
this.relationShorthandIndex = shorthandIndex;
this.lookupService = lookup;
}

/*
Expand All @@ -107,6 +104,7 @@ public String exportGPAD(WorkingMemory wm) {
Set<AnnotationExtension> possibleExtensions = possibleExtensions(basicAnnotations, model);
Set<Triple> statementsToExplain = new HashSet<>();
basicAnnotations.forEach(ba -> statementsToExplain.add(Triple.create(ba.getObjectNode(), NodeFactory.createURI(ba.getQualifier().toString()), ba.getOntologyClassNode())));
possibleExtensions.forEach(ae -> statementsToExplain.add(ae.getTriple()));
Map<Triple, Set<Explanation>> allExplanations = statementsToExplain.stream().collect(Collectors.toMap(Function.identity(), s -> toJava(wm.explain(Bridge.tripleFromJena(s)))));
Map<Triple, Set<GPADEvidence>> allEvidences = evidencesForFacts(allExplanations.values().stream().flatMap(es -> es.stream()).flatMap(e -> toJava(e.facts()).stream().map(t -> Bridge.jenaFromTriple(t))).collect(Collectors.toSet()), model, modelID);
for (BasicGPADData annotation : basicAnnotations) {
Expand All @@ -124,7 +122,13 @@ public String exportGPAD(WorkingMemory wm) {
for (AnnotationExtension extension : possibleExtensions) {
if (extension.getTriple().getSubject().equals(annotation.getOntologyClassNode()) &&
!(extension.getTriple().getObject().equals(annotation.getObjectNode()))) {
goodExtensions.add(new DefaultConjunctiveExpression(IRI.create(extension.getTriple().getPredicate().getURI()), extension.getValueType()));
for (Explanation expl : allExplanations.get(extension.getTriple())) {
boolean allFactsOfExplanationHaveRefMatchingAnnotation = toJava(expl.facts()).stream().map(fact -> allEvidences.getOrDefault(Bridge.jenaFromTriple(fact), Collections.emptySet())).allMatch(evidenceSet ->
evidenceSet.stream().anyMatch(ev -> ev.getReference().equals(reference)));
if (allFactsOfExplanationHaveRefMatchingAnnotation) {
goodExtensions.add(new DefaultConjunctiveExpression(IRI.create(extension.getTriple().getPredicate().getURI()), extension.getValueType()));
}
}
}
}
final boolean rootViolation;
Expand All @@ -139,7 +143,7 @@ public String exportGPAD(WorkingMemory wm) {
}
}
}
return new GPADRenderer(curieHandler, lookupService, relationShorthandIndex).renderAll(annotations);
return new GPADRenderer(curieHandler, relationShorthandIndex).renderAll(annotations);
}

private Map<Triple, Set<GPADEvidence>> evidencesForFacts(Set<Triple> facts, Model model, String modelID) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.geneontology.minerva.legacy.sparql;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.reasoner.rulesys.Rule;
import org.apache.jena.system.JenaSystem;
import org.geneontology.jena.OWLtoRules;
import org.geneontology.minerva.curie.DefaultCurieHandler;
import org.geneontology.rules.engine.RuleEngine;
import org.geneontology.rules.engine.Triple;
import org.geneontology.rules.engine.WorkingMemory;
import org.geneontology.rules.util.Bridge;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.model.parameters.Imports;

import scala.collection.JavaConverters;

public class GPADSPARQLTest {

private static RuleEngine arachne;
private static GPADSPARQLExport exporter;

@BeforeClass
public static void setupRules() throws OWLOntologyCreationException {
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLOntology ont = manager.loadOntologyFromOntologyDocument(GPADSPARQLTest.class.getResourceAsStream("/ro-merged-2017-10-02.ofn"));
Set<Rule> rules = new HashSet<>();
rules.addAll(JavaConverters.setAsJavaSetConverter(OWLtoRules.translate(ont, Imports.INCLUDED, true, true, true, true)).asJava());
rules.addAll(JavaConverters.setAsJavaSetConverter(OWLtoRules.indirectRules(ont)).asJava());
arachne = new RuleEngine(Bridge.rulesFromJena(JavaConverters.asScalaSetConverter(rules).asScala()), true);
}

@BeforeClass
public static void setupExporter() {
JenaSystem.init();
exporter = new GPADSPARQLExport(DefaultCurieHandler.getDefaultHandler(), new HashMap<IRI, String>());
}

@Test
public void testGPADOutput() throws Exception {
Model model = ModelFactory.createDefaultModel();
model.read(this.getClass().getResourceAsStream("/581e072c00000473.ttl"), "", "ttl");
Set<Triple> triples = model.listStatements().toList().stream().map(s -> Bridge.tripleFromJena(s.asTriple())).collect(Collectors.toSet());
WorkingMemory mem = arachne.processTriples(JavaConverters.asScalaSetConverter(triples).asScala());
String gpad = exporter.exportGPAD(mem);
int lines = gpad.split("\n", -1).length;
//TODO test contents of annotations; dumb test for now
Assert.assertTrue("Should produce annotations", lines > 2);
}

}
722 changes: 722 additions & 0 deletions minerva-converter/src/test/resources/581e072c00000473.ttl

Large diffs are not rendered by default.

Loading