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

Eliminate imports #346

Merged
merged 6 commits into from
Aug 31, 2020
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
@@ -1,5 +1,6 @@
package org.geneontology.minerva.cli;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
Expand Down Expand Up @@ -76,6 +77,9 @@
import org.openrdf.rio.RDFParseException;
import org.semanticweb.elk.owlapi.ElkReasonerFactory;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.formats.TurtleDocumentFormat;
import org.semanticweb.owlapi.io.IRIDocumentSource;
import org.semanticweb.owlapi.model.AxiomType;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLAnnotation;
import org.semanticweb.owlapi.model.OWLAnnotationProperty;
Expand All @@ -84,6 +88,8 @@
import org.semanticweb.owlapi.model.OWLClassAssertionAxiom;
import org.semanticweb.owlapi.model.OWLClassExpression;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLDocumentFormat;
import org.semanticweb.owlapi.model.OWLImportsDeclaration;
import org.semanticweb.owlapi.model.OWLLiteral;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLOntology;
Expand Down Expand Up @@ -155,6 +161,13 @@ public static void main(String[] args) {
.build();
methods.addOption(add_taxon_metadata);

Option clean_gocams = Option.builder()
.longOpt("clean-gocams")
.desc("remove import statements, add property declarations, remove json-model annotation")
.hasArg(false)
.build();
methods.addOption(clean_gocams);

Option sparql = Option.builder()
.longOpt("sparql-update")
.desc("update the blazegraph journal with the given sparql statement")
Expand Down Expand Up @@ -202,6 +215,15 @@ public static void main(String[] args) {
String ontojournal = cmd.getOptionValue("ontojournal"); //--folder
addTaxonMetaData(journalFilePath, ontojournal);
}

if(cmd.hasOption("clean-gocams")) {
Options clean_options = new Options();
clean_options.addOption(clean_gocams);
clean_options.addOption("i", "input", true, "This is the directory of gocam files to clean.");
clean_options.addOption("o", "output", true, "This is the directory of cleaned gocam files that are produced.");
cmd = parser.parse(clean_options, args, false);
cleanGoCams(cmd.getOptionValue("i"), cmd.getOptionValue("o"));
}

if(cmd.hasOption("import-tbox-ontologies")) {
Options import_tbox_options = new Options();
Expand Down Expand Up @@ -1161,6 +1183,42 @@ public static void addTaxonMetaData(String go_cam_journal, String go_lego_journa
return;
}

public static void cleanGoCams(String input_dir, String output_dir) {
OWLOntologyManager m = OWLManager.createOWLOntologyManager();
File directory = new File(input_dir);
boolean ignore_imports = true;
if(directory.isDirectory()) {
for(File file : directory.listFiles()) {
if(file.getName().endsWith("ttl")) {
System.out.println("fixing "+file.getAbsolutePath());
final IRI modelFile = IRI.create(file.getAbsoluteFile());
OWLOntology o;
try {
o = CoreMolecularModelManager.loadOntologyDocumentSource(new IRIDocumentSource(modelFile), ignore_imports, m);
//in case the reader was confused by the missing import, fix declarations
o = CoreMolecularModelManager.fixBrokenObjectPropertiesAndAxioms(o);
//clean the model
OWLOntology cleaned_ont = CoreMolecularModelManager.removeDeadAnnotationsAndImports(o);
//saved the blessed ontology
OWLDocumentFormat owlFormat = new TurtleDocumentFormat();
m.setOntologyFormat(cleaned_ont, owlFormat);
String cleaned_ont_file = output_dir+file.getName();
try {
m.saveOntology(cleaned_ont, new FileOutputStream(cleaned_ont_file));
} catch (OWLOntologyStorageException | FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (OWLOntologyCreationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
}


public static void printVersion() throws Exception {
printManifestEntry("git-revision-sha1", "UNKNOWN");
printManifestEntry("git-revision-url", "UNKNOWN");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,39 +159,6 @@ private BigdataSailRepository initializeRepository(String pathToJournal) {
}
}

private void createImports(OWLOntology ont, OWLOntologyID tboxId,
METADATA metadata) throws OWLOntologyCreationException {
OWLOntologyManager m = ont.getOWLOntologyManager();
OWLDataFactory f = m.getOWLDataFactory();

// import T-Box
Optional<IRI> ontologyIRI = tboxId.getOntologyIRI();
if (ontologyIRI.isPresent()) {
OWLImportsDeclaration tBoxImportDeclaration = f
.getOWLImportsDeclaration(ontologyIRI.get());
m.applyChange(new AddImport(ont, tBoxImportDeclaration));
}

// import additional ontologies via IRI
for (IRI importIRI : additionalImports) {
OWLImportsDeclaration importDeclaration = f
.getOWLImportsDeclaration(importIRI);
// check that the import ontology is available
OWLOntology importOntology = m.getOntology(importIRI);
if (importOntology == null) {
// only try to load it, if it isn't already loaded
try {
m.loadOntology(importIRI);
} catch (OWLOntologyDocumentAlreadyExistsException e) {
// ignore
} catch (OWLOntologyAlreadyExistsException e) {
// ignore
}
}
m.applyChange(new AddImport(ont, importDeclaration));
}
}

/**
* Generates a blank model
*
Expand All @@ -216,10 +183,6 @@ public ModelContainer generateBlankModel(METADATA metadata)
ModelContainer model = null;
try {
abox = m.createOntology(modelId);

// add imports to T-Box and additional ontologies via IRI
createImports(abox, tbox.getOntologyID(), metadata);

// generate model
model = new ModelContainer(modelId, tbox, abox);
} catch (OWLOntologyCreationException exception) {
Expand Down Expand Up @@ -579,7 +542,6 @@ protected void loadModel(IRI modelId, boolean isOverride) throws OWLOntologyCrea
statements.close();
abox = postLoadFileFilter(abox);
ModelContainer model = addModel(modelId, abox);
updateImports(model);
} finally {
connection.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,36 @@ public String getLabel(String entity) throws IOException {
}
return label;
}

public boolean exists(String entity) throws IOException {
boolean exists = false;
String query = "select * "
+ "WHERE {" +
"{<"+entity+"> ?p ?o . } " +
"UNION " +
"{?s ?p <"+entity+"> . }" +
"} limit 1";
try {
BigdataSailRepositoryConnection connection = go_lego_repo.getReadOnlyConnection();
try {
TupleQuery tupleQuery = connection.prepareTupleQuery(QueryLanguage.SPARQL, query);
TupleQueryResult result = tupleQuery.evaluate();
if (result.hasNext()) {
exists = true;
return exists;
}
} catch (MalformedQueryException e) {
throw new IOException(e);
} catch (QueryEvaluationException e) {
throw new IOException(e);
} finally {
connection.close();
}
} catch (RepositoryException e) {
throw new IOException(e);
}
return exists;
}

public Map<String, String> getLabels(Set<String> entities) throws IOException {
Map<String, String> uri_label = new HashMap<String, String>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import org.semanticweb.owlapi.model.OWLOntologyStorageException;
import org.semanticweb.owlapi.model.OWLSubClassOfAxiom;
import org.semanticweb.owlapi.model.RemoveAxiom;
import org.semanticweb.owlapi.model.RemoveImport;
import org.semanticweb.owlapi.model.RemoveOntologyAnnotation;
import org.semanticweb.owlapi.model.SetOntologyID;
import org.semanticweb.owlapi.model.parameters.Imports;
Expand Down Expand Up @@ -961,9 +962,6 @@ public ModelContainer importModel(String modelData) throws OWLOntologyCreationEx
// add to internal model
ModelContainer newModel = addModel(modelId, modelOntology);

// update imports
updateImports(newModel);

return newModel;
}

Expand Down Expand Up @@ -1324,7 +1322,7 @@ protected OWLOntology loadOntologyDocumentSource(final OWLOntologyDocumentSource
return loadOntologyDocumentSource(source, minimal, tbox.getOWLOntologyManager());
}

static OWLOntology loadOntologyDocumentSource(final OWLOntologyDocumentSource source, boolean minimal, OWLOntologyManager manager) throws OWLOntologyCreationException {
public static OWLOntology loadOntologyDocumentSource(final OWLOntologyDocumentSource source, boolean minimal, OWLOntologyManager manager) throws OWLOntologyCreationException {
// silence the OBO parser in the OWL-API
java.util.logging.Logger.getLogger("org.obolibrary").setLevel(java.util.logging.Level.SEVERE);
final Set<OWLParserFactory> originalFactories = removeOBOParserFactories(manager);
Expand Down Expand Up @@ -1389,43 +1387,6 @@ private static OWLOntology loadOWLOntologyDocumentSource(final OWLOntologyDocume
return ontology;
}

/**
* This method will check the given model and update the import declarations.
* It will add missing IRIs and remove obsolete ones.
*
* @param model
* @see #additionalImports
* @see #addImports(Iterable)
*/
public void updateImports(ModelContainer model) {
updateImports(model.getAboxOntology(), tboxIRI, additionalImports);
}

static void updateImports(final OWLOntology aboxOntology, IRI tboxIRI, Set<IRI> additionalImports) {
List<OWLOntologyChange> changes = new ArrayList<OWLOntologyChange>();

Set<IRI> missingImports = new HashSet<IRI>();
missingImports.add(tboxIRI);
missingImports.addAll(additionalImports);
Set<OWLImportsDeclaration> importsDeclarations = aboxOntology.getImportsDeclarations();
for (OWLImportsDeclaration decl : importsDeclarations) {
IRI iri = decl.getIRI();
missingImports.remove(iri);
}
final OWLOntologyManager m = aboxOntology.getOWLOntologyManager();
if (!missingImports.isEmpty()) {
OWLDataFactory f = m.getOWLDataFactory();
for(IRI missingImport : missingImports) {
OWLImportsDeclaration decl = f.getOWLImportsDeclaration(missingImport);
changes.add(new AddImport(aboxOntology, decl));
}
}

if (!changes.isEmpty()) {
m.applyChanges(changes);
}
}

public OWLOntology getTbox() {
return tbox;
}
Expand Down Expand Up @@ -1453,33 +1414,49 @@ public static OWLOntology fixBrokenObjectPropertiesAndAxioms(OWLOntology ont) th
OWLAnnotationProperty title_prop = df.getOWLAnnotationProperty(IRI.create("http://purl.org/dc/elements/1.1/title"));
OWLDeclarationAxiom title_prop_declaration = df.getOWLDeclarationAxiom(title_prop);
newman.addAxiom(frank, title_prop_declaration);

OWLAnnotationProperty title_prop2 = df.getOWLAnnotationProperty(IRI.create("http://purl.org/dc/terms/title"));
OWLDeclarationAxiom title_prop2_declaration = df.getOWLDeclarationAxiom(title_prop2);
newman.addAxiom(frank, title_prop2_declaration);


newman.addAxiom(frank, title_prop2_declaration);
OWLAnnotationProperty skos_note = df.getOWLAnnotationProperty(IRI.create("http://www.w3.org/2004/02/skos/core#note"));
newman.addAxiom(frank, df.getOWLDeclarationAxiom(skos_note));
OWLAnnotationProperty version_info = df.getOWLAnnotationProperty(IRI.create(OWL.versionInfo.getURI()));
newman.addAxiom(frank, df.getOWLDeclarationAxiom(version_info));
OWLAnnotationProperty contributor_prop = df.getOWLAnnotationProperty(IRI.create("http://purl.org/dc/elements/1.1/contributor"));
newman.addAxiom(frank, df.getOWLDeclarationAxiom(contributor_prop));
OWLAnnotationProperty date_prop = df.getOWLAnnotationProperty(IRI.create("http://purl.org/dc/elements/1.1/date"));
newman.addAxiom(frank, df.getOWLDeclarationAxiom(date_prop));
OWLAnnotationProperty source_prop = df.getOWLAnnotationProperty(IRI.create("http://purl.org/dc/elements/1.1/source"));
newman.addAxiom(frank, df.getOWLDeclarationAxiom(source_prop));
OWLAnnotationProperty state_prop = df.getOWLAnnotationProperty(IRI.create("http://geneontology.org/lego/modelstate"));
newman.addAxiom(frank, df.getOWLDeclarationAxiom(state_prop));
OWLAnnotationProperty evidence_prop = df.getOWLAnnotationProperty(IRI.create("http://geneontology.org/lego/evidence"));
newman.addAxiom(frank, df.getOWLDeclarationAxiom(evidence_prop));
OWLAnnotationProperty provided_by_prop = df.getOWLAnnotationProperty(IRI.create("http://purl.org/pav/providedBy"));
newman.addAxiom(frank, df.getOWLDeclarationAxiom(provided_by_prop));
OWLAnnotationProperty x_prop = df.getOWLAnnotationProperty(IRI.create("http://geneontology.org/lego/hint/layout/x"));
newman.addAxiom(frank, df.getOWLDeclarationAxiom(x_prop));
OWLAnnotationProperty y_prop = df.getOWLAnnotationProperty(IRI.create("http://geneontology.org/lego/hint/layout/y"));
newman.addAxiom(frank, df.getOWLDeclarationAxiom(y_prop));
OWLAnnotationProperty rdfs_label = df.getOWLAnnotationProperty(OWLRDFVocabulary.RDFS_LABEL.getIRI());
newman.addAxiom(frank, df.getOWLDeclarationAxiom(rdfs_label));
OWLAnnotationProperty rdfs_comment = df.getOWLAnnotationProperty(OWLRDFVocabulary.RDFS_COMMENT.getIRI());
newman.addAxiom(frank, df.getOWLDeclarationAxiom(rdfs_comment));
OWLAnnotationProperty rdfs_seealso = df.getOWLAnnotationProperty(OWLRDFVocabulary.RDFS_SEE_ALSO.getIRI());
newman.addAxiom(frank, df.getOWLDeclarationAxiom(rdfs_seealso));
OWLAnnotationProperty skos_exact_match = df.getOWLAnnotationProperty(IRI.create("http://www.w3.org/2004/02/skos/core#exactMatch"));
newman.addAxiom(frank, df.getOWLDeclarationAxiom(skos_exact_match));
OWLAnnotationProperty skos_altlabel = df.getOWLAnnotationProperty(IRI.create("http://www.w3.org/2004/02/skos/core#altLabel"));
newman.addAxiom(frank, df.getOWLDeclarationAxiom(skos_altlabel));
OWLAnnotationProperty definition = df.getOWLAnnotationProperty(IRI.create("http://purl.obolibrary.org/obo/IAO_0000115"));
newman.addAxiom(frank, df.getOWLDeclarationAxiom(definition));
OWLAnnotationProperty database_cross_reference = df.getOWLAnnotationProperty(IRI.create("http://www.geneontology.org/formats/oboInOwl#hasDbXref"));
newman.addAxiom(frank, df.getOWLDeclarationAxiom(database_cross_reference));
OWLAnnotationProperty canonical_record = df.getOWLAnnotationProperty(IRI.create("http://geneontology.org/lego/canonical_record"));
newman.addAxiom(frank, df.getOWLDeclarationAxiom(canonical_record));
OWLAnnotationProperty iuphar_id = df.getOWLAnnotationProperty(IRI.create("http://geneontology.org/lego/iuphar_id"));
newman.addAxiom(frank, df.getOWLDeclarationAxiom(iuphar_id));
OWLAnnotationProperty in_taxon = df.getOWLAnnotationProperty(IRI.create("https://w3id.org/biolink/vocab/in_taxon"));

newman.addAxiom(frank, df.getOWLDeclarationAxiom(in_taxon));

//copy over ontology annotations
for(OWLAnnotation anno : ont.getAnnotations()) {
Expand Down Expand Up @@ -1522,19 +1499,24 @@ public static OWLOntology fixBrokenObjectPropertiesAndAxioms(OWLOntology ont) th
return frank;
}

public static boolean hasLegoImport(OWLOntology ont) {
Set<OWLOntology> imports = ont.getDirectImports();
for(OWLOntology i : imports) {
com.google.common.base.Optional<IRI> iri = i.getOntologyID().getOntologyIRI();
if(iri.isPresent()) {
String s_iri = iri.get().toString();
if(s_iri.equals("http://purl.obolibrary.org/obo/go/extensions/go-lego-reacto.owl")||
s_iri.equals("http://purl.obolibrary.org/obo/go/extensions/go-lego.owl")) {
return true;
}

public static OWLOntology removeDeadAnnotationsAndImports(OWLOntology ont) throws OWLOntologyCreationException {
OWLOntologyManager m = OWLManager.createOWLOntologyManager();
OWLAnnotationProperty json_model_prop = m.getOWLDataFactory().getOWLAnnotationProperty(IRI.create("http://geneontology.org/lego/json-model"));
//get rid of all imports
Set<OWLImportsDeclaration> imports = ont.getImportsDeclarations();
for(OWLImportsDeclaration import_declaration : imports) {
m.applyChange(new RemoveImport(ont, import_declaration));
}
//get rid of the json annotations lurking about
for(OWLAnnotation anno : ont.getAnnotations()) {
if(anno.getProperty().equals(json_model_prop)) {
RemoveOntologyAnnotation rm = new RemoveOntologyAnnotation(ont, anno);
m.applyChange(rm);
}
}
return false;
//purify of the json annotation property as well
OWLDeclarationAxiom json_prop_declaration = m.getOWLDataFactory().getOWLDeclarationAxiom(json_model_prop);
m.removeAxiom(ont, json_prop_declaration);
return ont;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,10 @@ private static HashMap<String, IRI> initAnnotationPropertyMap() {
*/
public OWLClass getOWLClass(IRI iri) {
OWLClass c = getDataFactory().getOWLClass(iri);
for (OWLOntology o : getAllOntologies()) {
if (o.getDeclarationAxioms(c).size() > 0) {
return c;
}
}
return null;
return c;
//there used to be a check here to ensure that the class IRI existed in a tbox ontology
//as there is no way to create a class using the UI without getting one out of the tbox ontology
//I think it is probably safe to remove this check. To add it, use BlazegraphOntologyManager.exists()
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -603,18 +603,5 @@ public OWLNamedIndividual removeDataProperties(ModelContainer model, OWLNamedInd
return i;
}

/**
* This method will check the given model and update the import declarations.
* It will add missing IRIs and remove obsolete ones.
*
* @param modelId
* @throws UnknownIdentifierException
* @see #additionalImports
* @see #addImports(Iterable)
*/
public void updateImports(IRI modelId) throws UnknownIdentifierException {
ModelContainer model = checkModelId(modelId);
updateImports(model);
}

}
Loading