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

Revised solr document loader codes to deep-copy all relevant ontologies #254

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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 @@ -680,12 +680,13 @@ else if (obj instanceof OWLObjectProperty) {
}
return relation_map;
}

private void addIdLabelClosure(OWLClass c, boolean reflexive,
final Set<OWLObjectProperty> props, final Map<String,String> relation_map) {
final Set<OWLObjectProperty> props, final Map<String,String> relation_map) {
addPropertiesForMaterialization(props);
ExpressionMaterializingReasoner materializingReasoner = getMaterializingReasoner();
Set<OWLClassExpression> classExpressions = materializingReasoner.getSuperClassExpressions(c, false);

OWLEntity owlThing = this.getManager().getOWLDataFactory().getOWLThing();

// remove owl:Thing - although logically valid, it is not of use to the consumer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ public String getAnnotationValue(OWLObject c, OWLAnnotationProperty lap) {
else {
return null;
}

for (OWLAnnotation a : anns) {
if (a.getValue() instanceof OWLLiteral) {
OWLLiteral val = (OWLLiteral) a.getValue();
Expand Down Expand Up @@ -623,7 +624,6 @@ public OWLAnnotationProperty getAnnotationProperty(String tag){
*/
public String getNamespace(OWLObject c) {
OWLAnnotationProperty lap = getAnnotationProperty(OboFormatTag.TAG_NAMESPACE.getTag());

return getAnnotationValue(c, lap);
}

Expand Down Expand Up @@ -933,7 +933,7 @@ public String getIdentifier(IRI iriId) {
}

// In the case where the input class does not have oboInOwl#id, we return its original URL.
LOG.warn("Unable to retrieve the value of oboInOw#id as the identifier for " + iriId + "; we will use an original iri as the identifier.");
// LOG.warn("Unable to retrieve the value of oboInOw#id as the identifier for " + iriId + "; we will use an original iri as the identifier.");
return (String) SerializationUtils.clone(iriId.toString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public void testGetIdentifier() throws Exception {
String id2 = wrapper.getIdentifier(IRI.create("http://purl.obolibrary.org/obo/GO_0000002"));
String id3 = wrapper.getIdentifier(IRI.create("http://example.com/X_005"));
String id4 = wrapper.getIdentifier(IRI.create("http://example.com/X_010"));
String id5 = wrapper.getIdentifier(IRI.create("http://purl.obolibrary.org/obo/GR_protein_Q6K4D1"));

assertEquals(id1, "GO:0000001");
assertEquals(id2, "GO:0000002");
Expand Down
5 changes: 0 additions & 5 deletions OWLTools-Runner/.settings/org.eclipse.jdt.core.prefs

This file was deleted.

21 changes: 11 additions & 10 deletions OWLTools-Runner/src/main/java/owltools/cli/SolrCommandRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -593,8 +593,6 @@ private void loadEachModelAnnotation(ParserWrapper pwr, File legoFile, String ur
boolean exitIfUnsatisfiable, boolean exitIfLoadFails) throws IOException {
String fname = legoFile.getName();
OWLReasoner currentReasoner = null;
OWLOntologyManager manager = pwr.getManager();

OWLOntology model = null;
ModelAnnotationSolrDocumentLoader loader = null;

Expand All @@ -609,20 +607,24 @@ private void loadEachModelAnnotation(ParserWrapper pwr, File legoFile, String ur
}

/**
* DEEP-COPIES OWLOntology instance.
* DEEP-COPIES ALL OWLOntology instances in the manager of the model.
* Note that most serialization libraries such as Kryo or Cloner DO NOT work for
* copying OWLOntology or reasoner instance. In other words, deep-copying ontology
* should be done via "copyOntology" method. This allows preventing memory leaks, i.e.,
* running reasoners affects the model (OWLOntology instance) but the changes only happen
* over copied instance, which can be safely disposed without blowing out of memory.
* See also: https://github.com/owlcollab/owltools/issues/253#issuecomment-388415035
*/
OWLOntologyManager tempOWLManager = OWLManager.createOWLOntologyManager();
OWLOntology tModel = tempOWLManager.copyOntology(model, OntologyCopy.DEEP);
for(OWLOntology o: model.getOWLOntologyManager().getOntologies())
tempOWLManager.copyOntology(o, OntologyCopy.DEEP);

OWLOntology dcpModel = tempOWLManager.getOntology(model.getOntologyID());

// Some sanity checks--some of the generated ones are problematic.
// We need a consistent ontology for the closure calculations!
OWLReasonerFactory reasonerFactory = new ElkReasonerFactory();
currentReasoner = reasonerFactory.createReasoner(tModel);
currentReasoner = reasonerFactory.createReasoner(dcpModel);
boolean consistent = currentReasoner.isConsistent();
if(consistent == false){
LOG.warn("Skip since inconsistent: " + fname);
Expand All @@ -634,12 +636,12 @@ private void loadEachModelAnnotation(ParserWrapper pwr, File legoFile, String ur
String modelUrl = legoModelPrefix + fname;

if (url.equals("mock")) {
loader = new MockModelAnnotationSolrDocumentLoader(url, tModel, currentReasoner, modelUrl,
loader = new MockModelAnnotationSolrDocumentLoader(url, dcpModel, currentReasoner, modelUrl,
modelStateFilter, removeDeprecatedModels, removeTemplateModels);
isMock = true;
}
else {
loader = new ModelAnnotationSolrDocumentLoader(url, tModel, currentReasoner, modelUrl,
loader = new ModelAnnotationSolrDocumentLoader(url, dcpModel, currentReasoner, modelUrl,
modelStateFilter, removeDeprecatedModels, removeTemplateModels);
}

Expand All @@ -650,19 +652,18 @@ private void loadEachModelAnnotation(ParserWrapper pwr, File legoFile, String ur
}

currentReasoner.dispose();
tempOWLManager.removeOntology(tModel);
tempOWLManager.removeOntology(dcpModel);
} catch (Exception e) {
LOG.info("Complex annotation load of " + fname + " at " + url + " failed!");
e.printStackTrace();

if (exitIfLoadFails)
System.exit(1);
} finally {
manager.removeOntology(model);
if (loader != null) {
LOG.info("Finalizing the current loader...");
loader.close();
loader = null;
LOG.info("Closing the current loader...");
}
}
}
Expand Down
5 changes: 0 additions & 5 deletions OWLTools-Solr/.settings/org.eclipse.jdt.core.prefs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ public class ModelAnnotationSolrDocumentLoader extends AbstractSolrLoader implem
private boolean skipDeprecatedModels;
private boolean skipTemplateModels;

public ModelAnnotationSolrDocumentLoader(String golrUrl, OWLOntology model, OWLReasoner r, String modelUrl,
public ModelAnnotationSolrDocumentLoader(String golrUrl, OWLOntology model, OWLReasoner r, String modelUrl,
Set<String> modelFilter, boolean skipDeprecatedModels, boolean skipTemplateModels) throws MalformedURLException {
this(createDefaultServer(golrUrl), model, r, modelUrl, modelFilter, skipDeprecatedModels, skipTemplateModels);
}
public ModelAnnotationSolrDocumentLoader(SolrServer server, OWLOntology model, OWLReasoner r, String modelUrl,

public ModelAnnotationSolrDocumentLoader(SolrServer server, OWLOntology model, OWLReasoner r, String modelUrl,
Set<String> modelFilter, boolean skipDeprecatedModels, boolean skipTemplateModels) {
super(server);
this.model = model;
Expand All @@ -106,12 +106,12 @@ public ModelAnnotationSolrDocumentLoader(SolrServer server, OWLOntology model, O
OWLDataFactory df = graph.getDataFactory();
partOf = OBOUpperVocabulary.BFO_part_of.getObjectProperty(df);
occursIn = OBOUpperVocabulary.BFO_occurs_in.getObjectProperty(df);

defaultClosureRelations = new ArrayList<String>(1);
defaultClosureRelations.add(graph.getIdentifier(partOf));

enabledBy = OBOUpperVocabulary.GOREL_enabled_by.getObjectProperty(df);

title = df.getOWLAnnotationProperty(IRI.create("http://purl.org/dc/elements/1.1/title"));
source = df.getOWLAnnotationProperty(IRI.create("http://purl.org/dc/elements/1.1/source"));
contributor = df.getOWLAnnotationProperty(IRI.create("http://purl.org/dc/elements/1.1/contributor"));
Expand All @@ -123,29 +123,31 @@ public ModelAnnotationSolrDocumentLoader(SolrServer server, OWLOntology model, O
layoutHintX = df.getOWLAnnotationProperty(IRI.create("http://geneontology.org/lego/hint/layout/x"));
layoutHintY = df.getOWLAnnotationProperty(IRI.create("http://geneontology.org/lego/hint/layout/y"));
templatestate = df.getOWLAnnotationProperty(IRI.create("http://geneontology.org/lego/templatestate"));

displayLabelProp = df.getRDFSLabel();
shortIdProp = df.getOWLAnnotationProperty(IRI.create(Obo2OWLConstants.OIOVOCAB_IRI_PREFIX+"id"));

jsonProp = df.getOWLAnnotationProperty(IRI.create("http://geneontology.org/lego/json-model"));

bpSet = getAspect(graph, "biological_process");
}

static Set<OWLClass> getAspect(OWLGraphWrapper graph, String aspect) {
Set<OWLClass> getAspect(OWLGraphWrapper graph, String aspect) {
Set<OWLClass> result = new HashSet<OWLClass>();

// Literally computing all subclasses of the root class labeled with "biological process"
OWLClass c = model.getOWLOntologyManager().getOWLDataFactory().getOWLClass(IRI.create("http://purl.obolibrary.org/obo/GO_0008150"));
Set<OWLClass> subClassBPSet = reasoner.getSubClasses(c, false).getFlattened();

for(OWLClass cls : graph.getAllOWLClasses()) {
if (cls.isBuiltIn()) {
if (cls.isBuiltIn())
continue;
}

String id = graph.getIdentifier(cls);
if (id.startsWith("GO:") == false) {
if (id.startsWith("GO:") == false)
continue;
}
String namespace = graph.getNamespace(cls);
if (namespace != null && namespace.equals(aspect)) {

if (subClassBPSet.contains(cls))
result.add(cls);
}
}
return result;
}
Expand All @@ -166,7 +168,7 @@ public void close() throws IOException {
public void load() throws SolrServerException, IOException {
LOG.info("Loading complex annotation document...");
final OWLShuntGraph shuntGraph = createShuntGraph(graph);

String modelId = null;
String modelDate = null;
String title = null;
Expand Down Expand Up @@ -217,7 +219,7 @@ else if (p.isDeprecated()) {
// fallback
modelId = model.getOntologyID().getOntologyIRI().get().toString();
}

if (requiredModelStates != null && state != null) {
boolean contains = requiredModelStates.contains(state);
if (contains == false) {
Expand Down Expand Up @@ -249,6 +251,7 @@ else if (p.isDeprecated()) {
final Set<OWLAnnotation> mfAnnotations = getAnnotations(null, mfNamed);
Map<OWLClass, Pair<OWLNamedIndividual, Set<OWLAnnotation>>> processes = findProcesses(mfNamed);
Map<OWLClass, Pair<OWLNamedIndividual, Set<OWLAnnotation>>> locations = findLocations(mfNamed);

for(OWLClass gpType : gpTypes) {
for(OWLClass mfType : mfTypes) {
if (processes.isEmpty() == false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.reasoner.OWLReasoner;

Expand All @@ -17,17 +18,15 @@ public MockModelAnnotationSolrDocumentLoader(SolrServer server,
OWLOntology model, OWLReasoner r, String modelUrl,
Set<String> modelFilter, boolean skipDeprecatedModels,
boolean skipTemplateModels) {
super(server, model, r, modelUrl, modelFilter, skipDeprecatedModels,
skipTemplateModels);
super(server, model, r, modelUrl, modelFilter, skipDeprecatedModels, skipTemplateModels);
// TODO Auto-generated constructor stub
}

public MockModelAnnotationSolrDocumentLoader(String golrUrl,
OWLOntology model, OWLReasoner r, String modelUrl,
Set<String> modelFilter, boolean skipDeprecatedModels,
boolean skipTemplateModels) throws MalformedURLException {
super((SolrServer)null, model, r, modelUrl, modelFilter, skipDeprecatedModels,
skipTemplateModels);
super((SolrServer)null, model, r, modelUrl, modelFilter, skipDeprecatedModels, skipTemplateModels);
// TODO Auto-generated constructor stub
}

Expand Down