changeset 3:24ad2706526c draft

Reimplemented in Python, using RDFLib
author mikel-egana-aranguren <mikel.egana.aranguren@gmail.com>
date Fri, 25 Apr 2014 15:42:31 +0200
parents b8bf1af83841
children 6501986529b0
files README SPARQLGalaxy.jar SPARQLGalaxy.xml SPARQLGalaxy_TEXT.jar examples/sadi_merge_output.sparql galaxy-dist/tools/sparql_galaxy/SPARQLGalaxy.xml galaxy-dist/tools/sparql_galaxy/__init__.py galaxy-dist/tools/sparql_galaxy/sparql.py 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 src/info/wilkinsonlab/galaxy/sparql/text/SPARQLGalaxy_TEXT.java
diffstat 12 files changed, 97 insertions(+), 416 deletions(-) [+]
line wrap: on
line diff
--- 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:
-
+```
   <section name="SPARQL-Galaxy" id="SPARQLGalaxy">
     <tool file="sparql_galaxy/SPARQLGalaxy.xml"/>
   </section>
-
-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:
-
-  <section name="SPARQL-Galaxy" id="SPARQLGalaxy">
-    <tool file="sparql_galaxy/SPARQLGalaxy.xml"/>
-  </section>
-
-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 <http://www.gnu.org/licenses/>.
-
-JENA has its own license.
-
-
Binary file SPARQLGalaxy.jar has changed
--- 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 @@
-<tool id="SPARQLGalaxy" name="Execute an SPARQL query against an RDF file" version="1.0.1">
-	<description>It executes an SPARQL query against the input RDF file and generates a two column tab file with the variables and bound entities</description>
-	<command>
-	  #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
-	</command>
-	<inputs>
-		<param name="ontology" type="data" label="Input RDF file"/>		   
-		<conditional name="query_type">
-			<param name="choose_input_query_type" type="select" label="Choose input query type">
-			  <option value="query_file">Query file from history</option>
-			  <option value="paste_query">Paste query</option>
-			</param>
-			<when value="query_file">
-			  <param name="query_file_from_history" type="data" label="Input SPARQL query file"/>
-			</when>
-			<when value="paste_query">
-			  <param name="pasted_query" type="text" area="True" size="10x50" label="SPARQL Query" value="SELECT * WHERE { ?s ?p ?o }">
-			    <sanitizer sanitize="False"/>
-			  </param>
-			</when>
-		</conditional>
-	</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 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.
-
-	</help>
-
-</tool>
Binary file SPARQLGalaxy_TEXT.jar has changed
--- /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: <http://semanticscience.org/resource/>
+PREFIX sadi_pred:  <http://sadiframework.org/ontologies/predicates.owl#>
+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
--- /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 @@
+<tool id="SPARQLGalaxy" name="Execute an SPARQL query against an RDF file" version="1.0.1">
+	<description>It executes an SPARQL query against the input RDF file and generates an HTML file with the variables and bound entities</description>
+	<command interpreter="python">
+	  sparql.py $ontology "$pasted_query" > $output 2>/dev/null
+	</command>
+	<inputs>
+		<param name="ontology" type="data" format="rdf" label="Input RDF file"/>		   
+		<param name="pasted_query" type="text" area="True" size="10x50" label="SPARQL Query" value="SELECT * WHERE { ?s ?p ?o }">
+		  <sanitizer sanitize="False"/>
+		</param>
+	</inputs>
+	<outputs>
+		<data format="html" name="output" />
+	</outputs>
+	<help>
+	  
+**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.
+
+	</help>
+
+</tool>
--- /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 "<table>"
+    print "<tr>"
+    for key in results.bindings[1].keys():
+        print "<td><b>" + key +"</b></td>"
+    print "</tr>"
+    for binding in results.bindings:
+        print "<tr>"
+        for value in binding.values():  
+            if isinstance(value, rdflib.term.URIRef):
+                print "<td><a href=\"" + value +"\">"+ value +"</a></td>"
+            else:
+                print "<td>"+ value +"</td>"
+        print "</tr>"
+    print "</table>"
+    
+if __name__ == "__main__":
+    main(sys.argv)
\ No newline at end of file
--- 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);
-	}
-}
--- 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<String> 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;
-	}
-}
--- 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);
-	}
-
-}
--- 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;
-	}
-}
--- 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<String> 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();
-	}
-}