# HG changeset patch # User mikel-egana-aranguren # Date 1398433351 -7200 # Node ID 24ad2706526c8fcef13bec51f230d1b7a27dd715 # Parent b8bf1af83841202b0719d43c18f8a7829371bce9 Reimplemented in Python, using RDFLib diff -r b8bf1af83841 -r 24ad2706526c README --- a/README Wed Dec 04 08:17:00 2013 +0100 +++ b/README Fri Apr 25 15:42:31 2014 +0200 @@ -1,107 +1,44 @@ -INSTALLATION VIA TOOL-SHED -========================== +SPARQL-Galaxy +============= -1.- Install through Admin >> Search and browse tool sheds +ABOUT +----- -2.- Copy SPARQLGalaxy.jar and SPARQLGalaxy_TEXT.jar to galaxy-dist/tool-data/shared/jars/ +Execute SPARQL queries against RDF datasets in Galaxy. -INSTALLATION VIA MERCURIAL -========================== +INSTALLATION +------------ -1.- Stop Galaxy. - -2.- Move to galaxy-dist/tools/ +1. Stop Galaxy. -3.- Clone the SPARQL-Galaxy repository with mercurial: +2. Download or clone with mercurial (`hg clone http://mikel-egana-aranguren@toolshed.g2.bx.psu.edu/repos/mikel-egana-aranguren/sparql_galaxy`). -hg clone http://mikel-egana-aranguren@toolshed.g2.bx.psu.edu/repos/mikel-egana-aranguren/sparql_galaxy +3. Copy everything under galaxy-dist/ to your server's galaxy-dist/ (i.e. recreate the tools/ and directory in your server). -4.- Move to galaxy-dist/tools/sparql_galaxy and copy SPARQLGalaxy.jar and SPARQLGalaxy_TEXT.jar to galaxy-dist/tool-data/shared/jars/ +4. Add the following lines to your server's /galaxy-dist/tool_conf.xml: -5.- Add the following lines to /galaxy-dist/tool_conf.xml: - +```
- -6.- Start Galaxy. - -For updates: - -1.- Stop Galaxy. - -2.- Move to galaxy-dist/tools/sparql_galaxy/ - -3.- Pull changes and update working copy with mercurial: - -hg pull -hg update - -4.- Copy SPARQLGalaxy.jar to galaxy-dist/tool-data/shared/jars/ - -5.- Only edit galaxy-dist/tool_conf.xml in case a tool (an XML file) has been added/removed. - -6.- Start Galaxy. - - - -MANUAL INSTALLATION (DIRECT DOWNLOAD) -===================================== - -Download and uncompress anywhere you like, and then: - -1.- Stop Galaxy. - -2.- Copy SPARQLGalaxy.jar and SPARQLGalaxy_TEXT.jar to galaxy-dist/tool-data/shared/jars/ - -3.- Create directory galaxy-dist/tools/sparql_galaxy/ - -4.- Copy SPARQLGalaxy.xml to galaxy-dist/tools/sparql_galaxy/ - -5.- Add the following lines to /galaxy-dist/tool_conf.xml: - -
- -
- -6.- Start Galaxy. - +``` + +5. Start Galaxy. CONTACT -======= +------- Please send any request or comment to mikel.egana.aranguren@gmail.com. - ACKNOWLEDGEMENTS -================ +---------------- This work is funded by the Marie Curie Cofund program of the EU, FP7, -and the Isaac Peral Program. +and the Genomic Resources group of the UPV-EHU. -COPYRIGHT AND LICENSE -===================== -Copyright (c) 2011 Mikel Egaña Aranguren, Alejandro Rodriguez Gonzalez - -SPARQL-Galaxy is free software: you can redistribute it and/or modify it under the -terms of the GNU General Public License as published by the Free -Software Foundation, either version 3 of the License, or (at your -option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . - -JENA has its own license. - - diff -r b8bf1af83841 -r 24ad2706526c SPARQLGalaxy.jar Binary file SPARQLGalaxy.jar has changed diff -r b8bf1af83841 -r 24ad2706526c SPARQLGalaxy.xml --- a/SPARQLGalaxy.xml Wed Dec 04 08:17:00 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,77 +0,0 @@ - - It executes an SPARQL query against the input RDF file and generates a two column tab file with the variables and bound entities - - #if $query_type.choose_input_query_type=="query_file" - java -Xmx3000M -Xms250M -DentityExpansionLimit=1000000000 -jar ${__tool_data_path__}/shared/jars/SPARQLGalaxy.jar $ontology $query_type.query_file_from_history > $output 2>/dev/null - #else - java -Xmx3000M -Xms250M -DentityExpansionLimit=1000000000 -jar ${__tool_data_path__}/shared/jars/SPARQLGalaxy_TEXT.jar $ontology "$query_type.pasted_query" > $output 2>/dev/null - #end if - - - - - - - - - - - - - - - - - - - - - - - - -**Usage** - - An RDF file (in any format that Jena can accept) and a plain text file (pasted or from history) with the SPARQL query are needed. See bundle for examples. - -**More information** - - Galaxy public instance with SPARQL-Galaxy pre-installed: - - http://biordf.org:8983/ - - RDF: - - http://www.w3.org/standards/techs/rdf - - 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. - - - - diff -r b8bf1af83841 -r 24ad2706526c SPARQLGalaxy_TEXT.jar Binary file SPARQLGalaxy_TEXT.jar has changed diff -r b8bf1af83841 -r 24ad2706526c examples/sadi_merge_output.sparql --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/sadi_merge_output.sparql Fri Apr 25 15:42:31 2014 +0200 @@ -0,0 +1,7 @@ +PREFIX sio_resource: +PREFIX sadi_pred: +SELECT DISTINCT ?protein ?SNP ?kegg_id +WHERE { + ?protein sadi_pred:isEncodedBy ?kegg_id . + ?protein sio_resource:SIO_000272 ?SNP . +} \ No newline at end of file diff -r b8bf1af83841 -r 24ad2706526c galaxy-dist/tools/sparql_galaxy/SPARQLGalaxy.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/galaxy-dist/tools/sparql_galaxy/SPARQLGalaxy.xml Fri Apr 25 15:42:31 2014 +0200 @@ -0,0 +1,45 @@ + + It executes an SPARQL query against the input RDF file and generates an HTML file with the variables and bound entities + + sparql.py $ontology "$pasted_query" > $output 2>/dev/null + + + + + + + + + + + + +**Important note** + + RDFLib must be installed on the system (http://github.com/RDFLib/rdflib). + +**Usage** + + An RDF file (in any format that RDFLib can accept) and a SPARQL query are needed. See bundle for examples. + +**More information** + + Galaxy public instance with SPARQL-Galaxy pre-installed: + + http://biordf.org:8983/ + + RDF: + + http://www.w3.org/standards/techs/rdf + + SPARQL: + + http://www.w3.org/standards/techs/sparql + +**Contact** + + Please send any request or comment to mikel.egana.aranguren@gmail.com. + + + + diff -r b8bf1af83841 -r 24ad2706526c galaxy-dist/tools/sparql_galaxy/__init__.py diff -r b8bf1af83841 -r 24ad2706526c galaxy-dist/tools/sparql_galaxy/sparql.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/galaxy-dist/tools/sparql_galaxy/sparql.py Fri Apr 25 15:42:31 2014 +0200 @@ -0,0 +1,27 @@ + + +import sys +import rdflib + +def main(argv): + + g = rdflib.Graph() + g.parse(argv[1]) + results = g.query(argv[2]) + print "" + print "" + for key in results.bindings[1].keys(): + print "" + print "" + for binding in results.bindings: + print "" + for value in binding.values(): + if isinstance(value, rdflib.term.URIRef): + print "" + else: + print "" + print "" + print "
" + key +"
"+ value +""+ value +"
" + +if __name__ == "__main__": + main(sys.argv) \ No newline at end of file diff -r b8bf1af83841 -r 24ad2706526c src/es/cbgp/galaxy/sparql/jena/OntologyManager.java --- a/src/es/cbgp/galaxy/sparql/jena/OntologyManager.java Wed Dec 04 08:17:00 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,32 +0,0 @@ -package es.cbgp.galaxy.sparql.jena; - -import java.io.File; - -import com.hp.hpl.jena.rdf.model.Model; -import com.hp.hpl.jena.rdf.model.ModelFactory; - -public class OntologyManager { - - private String ontFile; - private String sparqlFile; - private Model 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.createDefaultModel(); - 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); - } -} diff -r b8bf1af83841 -r 24ad2706526c src/es/cbgp/galaxy/sparql/jena/SPARQLQueryEngine.java --- a/src/es/cbgp/galaxy/sparql/jena/SPARQLQueryEngine.java Wed Dec 04 08:17:00 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,98 +0,0 @@ -package es.cbgp.galaxy.sparql.jena; - -import java.io.BufferedReader; -import java.io.FileReader; -import java.util.Iterator; - -import com.hp.hpl.jena.query.*; -import com.hp.hpl.jena.rdf.model.Model; -import com.hp.hpl.jena.rdf.model.RDFNode; -import com.hp.hpl.jena.shared.Lock; - -public class SPARQLQueryEngine { - - private Model queryModel; - private String sparqlFile; - - public SPARQLQueryEngine(Model 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 vars = qs.varNames(); - while (vars.hasNext()) { - String var = vars.next(); - res += "?" + var + "\t" + getValue(qs, var) + "\r\n"; - - } - } - return res; - } catch (Exception e) { - e.printStackTrace(); - } finally { - queryModel.leaveCriticalSection(); - if (qexec != null) { - qexec.close(); - } - } - return null; - } - - private String getValue(QuerySolution qs, String var) { - RDFNode n = qs.get(var); - return n.toString(); - -// System.out.println("RDFNode (" + var + "): " + n); -// try { -// if (!qs.getResource(var).isAnon()) { -// if (qs.getResource(var).isResource()) { -// return qs.getResource(var).toString(); -// } -// if (qs.getResource(var).isLiteral()) { -// return qs.getLiteral(var).toString(); -// } -// } -// } catch (Exception e) { -// -// } -// try { -// if (!qs.getLiteral(var).isAnon()) { -// if (qs.getResource(var).isResource()) { -// return qs.getResource(var).toString(); -// } -// if (qs.getResource(var).isLiteral()) { -// return qs.getLiteral(var).toString(); -// } -// } -// } catch (Exception e) { -// -// } -// return "Error"; - } - - 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; - } -} diff -r b8bf1af83841 -r 24ad2706526c src/es/cbgp/galaxy/sparql/main/Main.java --- a/src/es/cbgp/galaxy/sparql/main/Main.java Wed Dec 04 08:17:00 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,54 +0,0 @@ -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); - } - -} diff -r b8bf1af83841 -r 24ad2706526c src/es/cbgp/galaxy/sparql/main/Result.java --- a/src/es/cbgp/galaxy/sparql/main/Result.java Wed Dec 04 08:17:00 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,23 +0,0 @@ -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; - } -} diff -r b8bf1af83841 -r 24ad2706526c src/info/wilkinsonlab/galaxy/sparql/text/SPARQLGalaxy_TEXT.java --- a/src/info/wilkinsonlab/galaxy/sparql/text/SPARQLGalaxy_TEXT.java Wed Dec 04 08:17:00 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,51 +0,0 @@ -package info.wilkinsonlab.galaxy.sparql.text; - -import java.io.InputStream; -import java.util.Iterator; - -import com.hp.hpl.jena.query.Query; -import com.hp.hpl.jena.query.QueryExecution; -import com.hp.hpl.jena.query.QueryExecutionFactory; -import com.hp.hpl.jena.query.QueryFactory; -import com.hp.hpl.jena.query.QuerySolution; -import com.hp.hpl.jena.query.ResultSet; -import com.hp.hpl.jena.rdf.model.Model; -import com.hp.hpl.jena.rdf.model.ModelFactory; -import com.hp.hpl.jena.rdf.model.RDFNode; -import com.hp.hpl.jena.util.FileManager; - -public class SPARQLGalaxy_TEXT { - - /** - * @param input - * RDF - * @param SPARQL - * query - * - */ - public static void main(String[] args) { - String input_RDF_path = args[0]; - String queryString = args[1]; - Model model = ModelFactory.createOntologyModel(); - InputStream in = FileManager.get().open(input_RDF_path); - model.read(in, null); - Query query = QueryFactory.create(queryString); - QueryExecution qe = QueryExecutionFactory.create(query, model); - ResultSet results = qe.execSelect(); - String res = ""; - while (results.hasNext()) { - QuerySolution qs = results.next(); - Iterator vars = qs.varNames(); - while (vars.hasNext()) { - String var = vars.next(); - res += "?" + var + "\t" + getValue(qs, var) + "\r\n"; - System.out.println(res); - } - } - } - - private static String getValue(QuerySolution qs, String var) { - RDFNode n = qs.get(var); - return n.toString(); - } -}