Mercurial > repos > mikel-egana-aranguren > sparql_galaxy
changeset 0:137f9a4a6337 draft
First version to init the repo, still README etc to add but it works
author | mikel-egana-aranguren |
---|---|
date | Thu, 25 Oct 2012 12:17:40 -0400 |
parents | |
children | 117a4b4c002d |
files | SPARQLGalaxy.jar SPARQLGalaxy.xml src/es/cbgp/galaxy/sparql/jena/OntologyManager.java src/es/cbgp/galaxy/sparql/jena/SPARQLQueryEngine.java src/es/cbgp/galaxy/sparql/main/Main.java src/es/cbgp/galaxy/sparql/main/Result.java |
diffstat | 6 files changed, 231 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SPARQLGalaxy.xml Thu Oct 25 12:17:40 2012 -0400 @@ -0,0 +1,56 @@ +<tool id="SPARQLGalaxy" name="Execute an SPARQL query against an OWL ontology" version="1.0.1"> + <description>It executes an SPARQL query against the input OWL ontology and generates a two column tab file with the variables and bound entities</description> + <command> + java -Xmx3000M -Xms250M -DentityExpansionLimit=1000000000 -jar ${__tool_data_path__}/shared/jars/SPARQLGalaxy.jar $ontology $query_file > $output 2>/dev/null + </command> + <inputs> + <param name="ontology" type="data" label="Input ontology file"/> + <param name="query_file" type="data" label="Input SPARQL query file"/> + </inputs> + <outputs> + <data format="text" name="output" /> + </outputs> + <!--<tests> + <test> + <param name="input" value="test.owl"/> + <param name="OPPL" value="test.oppl"/> + <param name="format" value="OWL"/> + <param name="reasoner" value="Pellet"/> + <output name="out_file" file="test_new.owl"/> + </test> + </tests>--> + <help> + +**Usage** + + An OWL ontology in RDF/XML syntax and a plain text file with the SPARQL are needed. See bundle for examples. + +**More information** + + Galaxy public instance with SPARQL-Galaxy pre-installed: + + http://biordf.org:8090/ + + SPARQL: + + http://www.w3.org/standards/techs/sparql + + JENA: + + http://jena.apache.org/ + +**Authors and funding** + + Alejandro Rodríguez González is funded by the Isaac Peral Programme and developed the Java wrapper for JENA. + + Mikel Egaña Aranguren is funded by the Marie Curie Cofund programme and developed the XML file for this tool. + + They both belong to the Biological Informatics group of the CBGP, Spain (http://wilkinsonlab.info/). + +**Contact** + + Please send any request or comment to mikel.egana.aranguren@gmail.com. + + </help> + +</tool>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/es/cbgp/galaxy/sparql/jena/OntologyManager.java Thu Oct 25 12:17:40 2012 -0400 @@ -0,0 +1,32 @@ +package es.cbgp.galaxy.sparql.jena; + +import java.io.File; + +import com.hp.hpl.jena.ontology.OntModel; +import com.hp.hpl.jena.rdf.model.ModelFactory; + +public class OntologyManager { + + private String ontFile; + private String sparqlFile; + private OntModel model; + private SPARQLQueryEngine sqe; + + public OntologyManager(String of, String sf) throws Exception { + this.ontFile = of; + this.sparqlFile = sf; + init(); + } + + private void init() throws Exception { + this.model = ModelFactory.createOntologyModel(); + this.model.read(new File(ontFile).toURI().toString()); + } + + public void executeQuery() throws Exception { + this.sqe = new SPARQLQueryEngine(model); + this.sqe.setQueryFile(sparqlFile); + String ret = sqe.executeQuery(); + System.out.println(ret); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/es/cbgp/galaxy/sparql/jena/SPARQLQueryEngine.java Thu Oct 25 12:17:40 2012 -0400 @@ -0,0 +1,66 @@ +package es.cbgp.galaxy.sparql.jena; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.util.Iterator; + +import com.hp.hpl.jena.ontology.OntModel; +import com.hp.hpl.jena.query.*; +import com.hp.hpl.jena.shared.Lock; + +public class SPARQLQueryEngine { + + private OntModel queryModel; + private String sparqlFile; + + public SPARQLQueryEngine(OntModel om) { + this.queryModel = om; + } + + public String executeQuery() throws Exception { + String finalQuery = loadQueryFromFile(); + // System.out.println("Final query: " + finalQuery); + Query query = null; + QueryExecution qexec = null; + try { + queryModel.enterCriticalSection(Lock.READ); + query = QueryFactory.create(finalQuery); + qexec = QueryExecutionFactory.create(query, queryModel); + ResultSet results = qexec.execSelect(); + String res = ""; + while (results.hasNext()) { + QuerySolution qs = results.next(); + Iterator<String> vars = qs.varNames(); + while (vars.hasNext()) { + String var = vars.next(); + if (!qs.getResource(var).isAnon()) { + res += "?" + var + "\t" + qs.getResource(var) + "\n"; + } + } + } + return res; + } catch (Exception e) { + e.printStackTrace(); + } finally { + queryModel.leaveCriticalSection(); + if (qexec != null) { + qexec.close(); + } + } + return null; + } + + private String loadQueryFromFile() throws Exception { + String query = ""; + BufferedReader bL = new BufferedReader(new FileReader(this.sparqlFile)); + while (bL.ready()) { + query += bL.readLine(); + } + bL.close(); + return query; + } + + public void setQueryFile(String sparqlFile) { + this.sparqlFile = sparqlFile; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/es/cbgp/galaxy/sparql/main/Main.java Thu Oct 25 12:17:40 2012 -0400 @@ -0,0 +1,54 @@ +package es.cbgp.galaxy.sparql.main; + +import java.io.File; + +import es.cbgp.galaxy.sparql.jena.OntologyManager; + +public class Main { + + public Main(String args[]) { + Result r = check(args); + if (r.getBoolValue()) { + String ontFile = args[0]; + String sparqlFile = args[1]; + run(ontFile, sparqlFile); + } else { + System.err.println("Error: " + r.getMessage()); + } + } + + private void run(String ontFile, String sparqlFile) { + try { + OntologyManager om = new OntologyManager(ontFile, sparqlFile); + om.executeQuery(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private Result check(String[] args) { + if (args.length == 2) { + String fo = args[0]; + String fs = args[1]; + if (new File(fo).exists()) { + if (new File(fs).exists()) { + return new Result(true); + } else { + return new Result(false, + "Ontology file exists. SPARQL file not!"); + } + } + return new Result(false, "Ontology file not exists!"); + } + return new Result(false, + "Incorrect number of parameters. Necessary 2: " + args.length); + } + + public static void main(String[] args) { + /* + * Input: ontologia "SELECT .... blala" + */ + new Main(args); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/es/cbgp/galaxy/sparql/main/Result.java Thu Oct 25 12:17:40 2012 -0400 @@ -0,0 +1,23 @@ +package es.cbgp.galaxy.sparql.main; + +public class Result { + private boolean boolValue; + private String message; + + public Result(boolean b, String m) { + this.boolValue = b; + this.message = m; + } + + public Result(boolean b) { + this.boolValue = b; + } + + public String getMessage() { + return this.message; + } + + public boolean getBoolValue() { + return this.boolValue; + } +}