use of org.geneontology.minerva.curie.CurieHandler in project minerva by geneontology.
the class CommandLineInterface method legoToAnnotationsSPARQL.
/**
* Output GPAD files via inference+SPARQL
* cli --lego-to-gpad-sparql
*
* @param modelIdPrefix
* @param modelIdcurie
* @param inputDB
* @param gpadOutputFolder
* @param ontologyIRI
* @throws Exception
*/
public static void legoToAnnotationsSPARQL(String modelIdPrefix, String modelIdcurie, String inputDB, String gpadOutputFolder, String ontologyIRI, String catalog, String go_lego_journal_file) throws Exception {
if (modelIdPrefix == null) {
modelIdPrefix = "http://model.geneontology.org/";
}
if (modelIdcurie == null) {
modelIdcurie = "gomodel";
}
if (inputDB == null) {
inputDB = "blazegraph.jnl";
}
if (gpadOutputFolder == null) {
gpadOutputFolder = null;
}
if (ontologyIRI == null) {
ontologyIRI = "http://purl.obolibrary.org/obo/go/extensions/go-lego.owl";
}
OWLOntologyManager ontman = OWLManager.createOWLOntologyManager();
if (catalog != null) {
LOGGER.info("using catalog: " + catalog);
ontman.setIRIMappers(Sets.newHashSet(new owltools.io.CatalogXmlIRIMapper(catalog)));
} else {
LOGGER.info("no catalog, resolving all ontology uris directly");
}
OWLOntology ontology = ontman.loadOntology(IRI.create(ontologyIRI));
CurieMappings localMappings = new CurieMappings.SimpleCurieMappings(Collections.singletonMap(modelIdcurie, modelIdPrefix));
CurieHandler curieHandler = new MappedCurieHandler(DefaultCurieHandler.loadDefaultMappings(), localMappings);
boolean loadTboxIntoOntJournal = (!(new File(go_lego_journal_file)).exists());
BlazegraphMolecularModelManager<Void> m3 = new BlazegraphMolecularModelManager<>(ontology, curieHandler, modelIdPrefix, inputDB, null, go_lego_journal_file, false);
if (loadTboxIntoOntJournal) {
m3.getGolego_repo().loadRepositoryFromOntology(ontology, "http://example.org/", true);
}
final String immutableModelIdPrefix = modelIdPrefix;
final String immutableGpadOutputFolder = gpadOutputFolder;
m3.getAvailableModelIds().stream().parallel().forEach(modelIRI -> {
try {
// TODO investigate whether changing to a neo-lite model has an impact on this - may need to make use of ontology journal
String gpad = new GPADSPARQLExport(curieHandler, m3.getLegacyRelationShorthandIndex(), m3.getTboxShorthandIndex(), m3.getGolego_repo().regulatorsToRegulated).exportGPAD(m3.createInferredModel(modelIRI), modelIRI);
String fileName = StringUtils.replaceOnce(modelIRI.toString(), immutableModelIdPrefix, "") + ".gpad";
Writer writer = new OutputStreamWriter(new FileOutputStream(Paths.get(immutableGpadOutputFolder, fileName).toFile()), StandardCharsets.UTF_8);
writer.write(gpad);
writer.close();
} catch (InconsistentOntologyException e) {
LOGGER.error("Inconsistent ontology: " + modelIRI);
} catch (IOException e) {
LOGGER.error("Couldn't export GPAD for: " + modelIRI, e);
}
});
m3.dispose();
}
use of org.geneontology.minerva.curie.CurieHandler in project minerva by geneontology.
the class CommandLineInterface method modelsToOWL.
/**
* Given a blazegraph journal with go-cams in it, write them all out as OWL files.
* cli --dump-owl-models
*
* @param journalFilePath
* @param outputFolder
* @param modelIdPrefix
* @throws Exception
*/
public static void modelsToOWL(String journalFilePath, String outputFolder, String modelIdPrefix) throws Exception {
if (modelIdPrefix == null) {
modelIdPrefix = "http://model.geneontology.org/";
}
// minimal inputs
if (journalFilePath == null) {
System.err.println("No journal file was configured.");
System.exit(-1);
return;
}
if (outputFolder == null) {
System.err.println("No output folder was configured.");
System.exit(-1);
return;
}
OWLOntology dummy = OWLManager.createOWLOntologyManager().createOntology(IRI.create("http://example.org/dummy"));
CurieHandler curieHandler = new MappedCurieHandler();
BlazegraphMolecularModelManager<Void> m3 = new BlazegraphMolecularModelManager<>(dummy, curieHandler, modelIdPrefix, journalFilePath, outputFolder, null, false);
m3.dumpAllStoredModels();
m3.dispose();
}
use of org.geneontology.minerva.curie.CurieHandler in project minerva by geneontology.
the class ModelSearchHandler method search.
// examples
// http://127.0.0.1:6800/search/?
// ?gp=http://identifiers.org/uniprot/P15822-3
// ?term=http://purl.obolibrary.org/obo/GO_0003677
//
//
// ?gp=http://identifiers.org/mgi/MGI:1328355
// &gp=http://identifiers.org/mgi/MGI:87986
// &term=http://purl.obolibrary.org/obo/GO_0030968
// &title=mouse
// &pmid=PMID:19911006
// &state=development&state=review {development, production, closed, review, delete} or operator
// &count
// 127.0.0.1:6800/search/?contributor=http://orcid.org/0000-0002-1706-4196
public ModelSearchResult search(Set<String> taxa, Set<String> gene_product_ids, Set<String> terms, String expand, Set<String> pmids, String title_search, Set<String> state_search, Set<String> contributor_search, Set<String> group_search, String exactdate, String date_search, String datend, int offset, int limit, String count, String debug, Set<String> id) {
ModelSearchResult r = new ModelSearchResult();
Set<String> go_type_ids = new HashSet<String>();
Set<String> gene_type_ids = new HashSet<String>();
if (gene_product_ids != null) {
gene_type_ids.addAll(gene_product_ids);
}
if (terms != null) {
go_type_ids.addAll(terms);
}
CurieHandler curie_handler = m3.getCuriHandler();
Set<String> go_type_uris = new HashSet<String>();
Set<String> gene_type_uris = new HashSet<String>();
for (String curi : go_type_ids) {
if (curi.startsWith("http")) {
go_type_uris.add(curi);
} else {
try {
IRI iri = curie_handler.getIRI(curi);
if (iri != null) {
go_type_uris.add(iri.toString());
}
} catch (UnknownIdentifierException e) {
r.error += e.getMessage() + " \n ";
e.printStackTrace();
return r;
}
}
}
for (String curi : gene_type_ids) {
if (curi.startsWith("http")) {
gene_type_uris.add(curi);
} else {
try {
IRI iri = curie_handler.getIRI(curi);
if (iri != null) {
gene_type_uris.add(iri.toString());
}
} catch (UnknownIdentifierException e) {
r.error += e.getMessage() + " \n ";
e.printStackTrace();
return r;
}
}
}
Map<String, ModelMeta> id_model = new LinkedHashMap<String, ModelMeta>();
String sparql = "";
try {
sparql = IOUtils.toString(ModelSearchHandler.class.getResourceAsStream("/ModelSearchQueryTemplate.rq"), StandardCharsets.UTF_8);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Map<String, String> ind_return = new HashMap<String, String>();
// <ind_return_list>
String ind_return_list = "";
// <types>
String types = "";
int n = 0;
for (String type_uri : gene_type_uris) {
n++;
ind_return.put("?ind" + n, type_uri);
ind_return_list = ind_return_list + " (GROUP_CONCAT(?ind" + n + " ; separator=\" \") AS ?inds" + n + ")";
types = types + "?ind" + n + " rdf:type <" + type_uri + "> . \n";
}
if (expand != null) {
for (String go_type_uri : go_type_uris) {
n++;
ind_return.put("?ind" + n, go_type_uri);
ind_return_list = ind_return_list + " (GROUP_CONCAT(?ind" + n + " ; separator=\" \") AS ?inds" + n + ")";
String expansion = "VALUES ?term" + n + " { ";
try {
Set<String> subclasses = go_lego.getAllSubClasses(go_type_uri);
for (String sub : subclasses) {
expansion += "<" + sub + "> \n";
}
expansion += "} . \n";
types = types + " " + expansion + " ?ind" + n + " rdf:type ?term" + n + " . \n";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} else {
for (String go_type_uri : go_type_uris) {
n++;
ind_return.put("?ind" + n, go_type_uri);
ind_return_list = ind_return_list + " (GROUP_CONCAT(?ind" + n + " ; separator=\" \") AS ?inds" + n + ")";
types = types + "?ind" + n + " rdf:type <" + go_type_uri + "> . \n";
}
}
String id_constraint = "";
if (id != null && id.size() > 0) {
String id_list = "";
for (String mid : id) {
if (!mid.contains("http")) {
String[] curie = mid.split(":");
if (curie != null && curie.length == 2) {
mid = "http://model.geneontology.org/" + curie[1];
}
// TODO figure this out and add it to standard curie collection
// try {
// IRI iri = curie_handler.getIRI(id);
// id = iri.toString();
// } catch (UnknownIdentifierException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
id_list += "<" + mid + "> ";
}
id_constraint = " values ?id { " + id_list + " } ";
}
// <pmid_constraints>
String pmid_constraints = "";
if (pmids != null) {
for (String pmid : pmids) {
n++;
ind_return.put("?ind" + n, pmid);
ind_return_list = ind_return_list + " (GROUP_CONCAT(?ind" + n + " ; separator=\" \") AS ?inds" + n + ")";
pmid_constraints = pmid_constraints + "?ind" + n + " <http://purl.org/dc/elements/1.1/source> ?pmid FILTER (?pmid=\"" + pmid + "\"^^xsd:string) .\n";
}
}
String taxa_constraint = "";
if (taxa != null && !taxa.isEmpty()) {
for (String taxon : taxa) {
if (taxon.startsWith("NCBITaxon:")) {
taxon = taxon.replace(":", "_");
taxon = "http://purl.obolibrary.org/obo/" + taxon;
} else if (!taxon.startsWith("http://purl.obolibrary.org/obo/NCBITaxon_")) {
taxon = "http://purl.obolibrary.org/obo/NCBITaxon_" + taxon;
}
taxa_constraint += "?id <" + BlazegraphOntologyManager.in_taxon_uri + "> <" + taxon + "> . \n";
}
}
// if(taxa!=null&&!taxa.isEmpty()) {
// String model_filter = " VALUES ?id { \n";
// for(String taxon : taxa) {
// if(taxon.startsWith("NCBITaxon:")) {
// taxon = taxon.replace(":", "_");
// taxon = "http://purl.obolibrary.org/obo/"+taxon;
// }
// else if(!taxon.startsWith("http://purl.obolibrary.org/obo/NCBITaxon_")) {
// taxon = "http://purl.obolibrary.org/obo/NCBITaxon_"+taxon;
// }
// Set<String> models = taxon_models.get(taxon);
// if(models!=null) {
// for(String model : models) {
// model_filter+="<"+model+"> \n";
// }
// }
// }
// model_filter += "} . \n";
// taxa_constraint = model_filter;
// }
String title_search_constraint = "";
if (title_search != null) {
title_search_constraint = "?title <http://www.bigdata.com/rdf/search#search> \"" + title_search + "\" .\n";
if (!title_search.contains("*")) {
title_search_constraint += " ?title <http://www.bigdata.com/rdf/search#matchAllTerms> \"" + "true" + "\" . \n";
}
// if(exact_match) {
// title_search_constraint+=" ?title <http://www.bigdata.com/rdf/search#matchExact> \""+"true"+"\" . \n";
// }
}
String state_search_constraint = "";
if (state_search != null && state_search.size() > 0) {
String allowed_states = "";
int c = 0;
for (String s : state_search) {
c++;
allowed_states += "\"" + s + "\"";
if (c < state_search.size()) {
allowed_states += ",";
}
}
// FILTER (?state IN ("production", , "development", "review", "closed", "delete" ))
state_search_constraint = "FILTER (?state IN (" + allowed_states + ")) . \n";
}
String contributor_search_constraint = "";
if (contributor_search != null && contributor_search.size() > 0) {
String allowed_contributors = "";
int c = 0;
for (String contributor : contributor_search) {
c++;
allowed_contributors += "\"" + contributor + "\"";
if (c < contributor_search.size()) {
allowed_contributors += ",";
}
}
contributor_search_constraint = " ?id <http://purl.org/dc/elements/1.1/contributor> ?test_contributor . \n" + " FILTER (?test_contributor IN (" + allowed_contributors + ")) . \n";
}
String group_search_constraint = "";
if (group_search != null && group_search.size() > 0) {
String allowed_group = "";
int c = 0;
for (String group : group_search) {
c++;
allowed_group += "\"" + group + "\"";
if (c < group_search.size()) {
allowed_group += ",";
}
}
group_search_constraint = " ?id <http://purl.org/pav/providedBy> ?test_group . \n" + "FILTER (?test_group IN (" + allowed_group + ")) . \n";
}
String date_constraint = "";
if (exactdate != null && exactdate.length() == 10) {
date_constraint = "FILTER (?date = '" + exactdate + "') \n";
} else if (date_search != null && date_search.length() == 10) {
// e.g. 2019-06-26
date_constraint = "FILTER (?date > '" + date_search + "') \n";
if (datend != null && datend.length() == 10) {
date_constraint = "FILTER (?date > '" + date_search + "' && ?date < '" + datend + "') \n";
}
}
String offset_constraint = "";
if (offset != 0) {
offset_constraint = "OFFSET " + offset + "\n";
}
String limit_constraint = "";
if (limit != 0) {
limit_constraint = "LIMIT " + limit + "\n";
}
if (offset == 0 && limit == 0) {
limit_constraint = "LIMIT 1000\n";
}
// default group by
String group_by_constraint = "GROUP BY ?id";
// default return block
// TODO investigate need to add DISTINCT to GROUP_CONCAT here
String return_block = "?id (MIN(?date) AS ?mindate) (MIN(?title) AS ?mintitle) (MIN(?state) AS ?minstate) <ind_return_list> (GROUP_CONCAT(DISTINCT ?contributor;separator=\";\") AS ?contributors) (GROUP_CONCAT(DISTINCT ?group;separator=\";\") AS ?groups)";
if (count != null) {
return_block = "(count(distinct ?id) as ?count)";
limit_constraint = "";
offset_constraint = "";
group_by_constraint = "";
}
sparql = sparql.replaceAll("<return_block>", return_block);
sparql = sparql.replaceAll("<id_constraint>", id_constraint);
sparql = sparql.replaceAll("<group_by_constraint>", group_by_constraint);
sparql = sparql.replaceAll("<ind_return_list>", ind_return_list);
sparql = sparql.replaceAll("<types>", types);
sparql = sparql.replaceAll("<pmid_constraints>", pmid_constraints);
sparql = sparql.replaceAll("<title_constraint>", title_search_constraint);
sparql = sparql.replaceAll("<state_constraint>", state_search_constraint);
sparql = sparql.replaceAll("<contributor_constraint>", contributor_search_constraint);
sparql = sparql.replaceAll("<group_constraint>", group_search_constraint);
sparql = sparql.replaceAll("<date_constraint>", date_constraint);
sparql = sparql.replaceAll("<limit_constraint>", limit_constraint);
sparql = sparql.replaceAll("<offset_constraint>", offset_constraint);
sparql = sparql.replaceAll("<taxa_constraint>", taxa_constraint);
if (debug != null) {
r.sparql = sparql;
} else {
r.sparql = "add 'debug' parameter to see sparql request";
}
TupleQueryResult result;
try {
result = (TupleQueryResult) m3.executeSPARQLQuery(sparql, 1000);
} catch (MalformedQueryException | QueryEvaluationException | RepositoryException e) {
if (e instanceof MalformedQueryException) {
r.message = "Malformed Query";
} else if (e instanceof QueryEvaluationException) {
r.message = "Query Evaluation Problem - probably a time out";
} else if (e instanceof RepositoryException) {
r.message = "Repository Exception";
}
r.error = e.getMessage();
e.printStackTrace();
return r;
}
String n_count = null;
try {
while (result.hasNext()) {
BindingSet bs = result.next();
if (count != null) {
n_count = bs.getBinding("count").getValue().stringValue();
} else {
// model meta
String model_iri_string = bs.getBinding("id").getValue().stringValue();
IRI model_iri = IRI.create(model_iri_string);
String model_curie = null;
try {
model_curie = curie_handler.getCuri(IRI.create(model_iri_string));
if (model_curie == null) {
model_curie = model_iri_string;
}
} catch (Exception e) {
r.error += e.getMessage() + " \n ";
e.printStackTrace();
return r;
}
String date = bs.getBinding("mindate").getValue().stringValue();
String title = bs.getBinding("mintitle").getValue().stringValue();
String contribs = bs.getBinding("contributors").getValue().stringValue();
// optional values (some are empty)
Binding state_binding = bs.getBinding("minstate");
String state = "";
if (state_binding != null) {
state = state_binding.getValue().stringValue();
}
Binding group_binding = bs.getBinding("groups");
String groups_ = "";
if (group_binding != null) {
groups_ = group_binding.getValue().stringValue();
}
Set<String> contributors = new HashSet<String>(Arrays.asList(contribs.split(";")));
Set<String> groups = new HashSet<String>();
if (groups_ != null) {
groups.addAll(Arrays.asList(groups_.split(";")));
}
ModelMeta mm = id_model.get(model_curie);
if (mm == null) {
// look up model in in-memory cache to check edit state
boolean is_modified = m3.isModelModified(model_iri);
mm = new ModelMeta(model_curie, date, title, state, contributors, groups, is_modified);
}
// matching
for (String ind : ind_return.keySet()) {
String bindingName = ind.replace("?ind", "inds");
String[] ind_class_matches = bs.getBinding(bindingName).getValue().stringValue().split(" ", -1);
for (String ind_class_match : ind_class_matches) {
Set<String> matching_inds = mm.query_match.get(ind_return.get(ind));
if (matching_inds == null) {
matching_inds = new HashSet<String>();
}
matching_inds.add(ind_class_match);
mm.query_match.put(ind_return.get(ind), matching_inds);
}
}
id_model.put(model_curie, mm);
}
}
} catch (QueryEvaluationException e) {
r.message = "Query Evaluation Problem - probably a time out";
r.error = e.getMessage();
e.printStackTrace();
return r;
}
if (n_count != null) {
r.n = Integer.parseInt(n_count);
} else {
r.n = id_model.size();
r.models = new LinkedHashSet<ModelMeta>(id_model.values());
}
try {
result.close();
} catch (QueryEvaluationException e) {
r.message = "Query Evaluation Problem - can't close result set";
r.error = e.getMessage();
e.printStackTrace();
return r;
}
// http://127.0.0.1:6800/modelsearch/?query=bla
return r;
}
use of org.geneontology.minerva.curie.CurieHandler in project minerva by geneontology.
the class GoCamModelTest method testRootTypesForComplements.
@Test
public void testRootTypesForComplements() throws Exception {
String ontologyJournalFile = "/tmp/test-go-lego-blazegraph-complements.jnl";
OWLOntologyManager man = OWLManager.createOWLOntologyManager();
OWLOntology tboxOntology = man.loadOntologyFromOntologyDocument(new File("src/test/resources/go-basic.obo"));
CurieHandler curieHandler = new MappedCurieHandler();
String inputDB = "/tmp/test-blazegraph-models-complements.jnl";
UndoAwareMolecularModelManager m3 = null;
m3 = new UndoAwareMolecularModelManager(tboxOntology, curieHandler, "gomodel", inputDB, null, ontologyJournalFile, true);
m3.importModelToDatabase(new File("src/test/resources/test-complement-roots.ttl"), true);
ModelContainer mc = m3.getModel(IRI.create("http://model.geneontology.org/61f3310500000003"));
OWLOntology gocam_via_mc = mc.getAboxOntology();
GoCamModel g = new GoCamModel(gocam_via_mc, m3);
assertTrue("Can get roots for classes and complements", g.ind_types.get(man.getOWLDataFactory().getOWLNamedIndividual(IRI.create("http://model.geneontology.org/61f3310500000003/61f3310500000004"))).contains("http://purl.obolibrary.org/obo/GO_0008150"));
assertTrue("Can get roots for classes and complements", g.ind_types.get(man.getOWLDataFactory().getOWLNamedIndividual(IRI.create("http://model.geneontology.org/61f3310500000003/61f3310500000005"))).contains("http://purl.obolibrary.org/obo/GO_0008150"));
m3.getGolego_repo().dispose();
}
use of org.geneontology.minerva.curie.CurieHandler in project minerva by geneontology.
the class GoCamModelTest method testGoModelStats.
@Test
public void testGoModelStats() throws Exception {
OWLOntologyManager man = OWLManager.createOWLOntologyManager();
OWLOntology tbox_ontology = man.loadOntology(IRI.create("http://purl.obolibrary.org/obo/go/extensions/go-lego.owl"));
CurieHandler curieHandler = new MappedCurieHandler();
String inputDB = "/tmp/test-blazegraph-models.jnl";
// load it into a journal and launch an m3
UndoAwareMolecularModelManager m3 = null;
File f = new File(gocam_dir);
if (f.isDirectory()) {
// remove anything that existed from previous runs
File bgdb = new File(inputDB);
if (bgdb.exists()) {
bgdb.delete();
}
// set it up with empty db
m3 = new UndoAwareMolecularModelManager(tbox_ontology, curieHandler, "gomodel", inputDB, null, ontology_journal_file, true);
onto_repo = m3.getGolego_repo();
// load the db
for (File file : f.listFiles()) {
if (file.getName().endsWith("ttl")) {
m3.importModelToDatabase(file, true);
}
}
}
// read it back out and check on stats
for (IRI modelIRI : m3.getAvailableModelIds()) {
ModelContainer mc = m3.getModel(modelIRI);
OWLOntology gocam_via_mc = mc.getAboxOntology();
GoCamModel g = new GoCamModel(gocam_via_mc, m3);
// testing for an issue with the OWL blazegraph loader
assertFalse("title not read out of M3 retrieved model " + modelIRI, (g.getTitle() == null));
// these reactions are not counted as activities, but causal relations coming from them are counted.
if (modelIRI.toString().contains("R-HSA-5654719")) {
// SHC-mediated cascade:FGFR4
assertTrue("wrong n activities " + g.getStats().n_activity_units, g.getStats().n_activity_units == 4);
assertTrue("wrong n complete activities " + g.getStats().n_complete_activity_units, g.getStats().n_complete_activity_units == 2);
assertTrue("wrong n unenabled activities " + g.getStats().n_no_enabler, g.getStats().n_no_enabler == 2);
assertTrue("wrong n causal relations " + g.getStats().n_causal_in_relation_assertions, g.getStats().n_causal_in_relation_assertions == 6);
} else if (modelIRI.toString().contains("R-HSA-201688")) {
// WNT mediated activation of DVL
assertTrue("wrong n activities " + g.getStats().n_activity_units, g.getStats().n_activity_units == 4);
assertTrue("wrong n complete activities " + g.getStats().n_complete_activity_units, g.getStats().n_complete_activity_units == 3);
assertTrue("wrong n unenabled activities " + g.getStats().n_no_enabler, g.getStats().n_no_enabler == 1);
assertTrue("wrong n causal relations " + g.getStats().n_causal_in_relation_assertions, g.getStats().n_causal_in_relation_assertions == 3);
} else if (modelIRI.toString().contains("R-HSA-5654733")) {
// Negative regulation of FGFR4 signaling
assertTrue("wrong n activities " + g.getStats().n_activity_units, g.getStats().n_activity_units == 3);
assertTrue("wrong n complete activities " + g.getStats().n_complete_activity_units, g.getStats().n_complete_activity_units == 2);
assertTrue("wrong n unenabled activities " + g.getStats().n_no_enabler, g.getStats().n_no_enabler == 1);
assertTrue("wrong n causal relations " + g.getStats().n_causal_in_relation_assertions, g.getStats().n_causal_in_relation_assertions == 3);
}
}
}
Aggregations