changeset 2:b8bf1af83841 draft

Paste query added, query engine improved, examples added
author Mikel Egana Aranguren <mikel-egana-aranguren@toolshed.g2.bx.psu.edu>
date Wed, 04 Dec 2013 08:17:00 +0100
parents 117a4b4c002d
children 24ad2706526c
files README SPARQLGalaxy.jar SPARQLGalaxy.xml SPARQLGalaxy_TEXT.jar examples/jbmsquery.rdf examples/query.sparql examples/query_2.sparql src/es/cbgp/galaxy/sparql/jena/OntologyManager.java src/es/cbgp/galaxy/sparql/jena/SPARQLQueryEngine.java src/info/wilkinsonlab/galaxy/sparql/text/SPARQLGalaxy_TEXT.java
diffstat 10 files changed, 148 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/README	Thu Oct 25 18:35:25 2012 +0200
+++ b/README	Wed Dec 04 08:17:00 2013 +0100
@@ -1,5 +1,13 @@
-INSTALLATION VIA MERCURIAL (RECOMMENDED)
-========================================
+INSTALLATION VIA TOOL-SHED
+==========================
+
+1.- Install through Admin >> Search and browse tool sheds
+
+2.- Copy SPARQLGalaxy.jar and SPARQLGalaxy_TEXT.jar to galaxy-dist/tool-data/shared/jars/ 
+
+
+INSTALLATION VIA MERCURIAL
+==========================
 
 1.- Stop Galaxy.
 
@@ -9,7 +17,7 @@
 
 hg clone http://mikel-egana-aranguren@toolshed.g2.bx.psu.edu/repos/mikel-egana-aranguren/sparql_galaxy
 
-4.- Move to galaxy-dist/tools/sparql_galaxy and copy SPARQLGalaxy.jar to galaxy-dist/tool-data/shared/jars/
+4.- Move to galaxy-dist/tools/sparql_galaxy and copy SPARQLGalaxy.jar and SPARQLGalaxy_TEXT.jar to galaxy-dist/tool-data/shared/jars/
 
 5.- Add the following lines to /galaxy-dist/tool_conf.xml:
 
@@ -45,7 +53,7 @@
 
 1.- Stop Galaxy.
 
-2.- SPARQLGalaxy.jar to galaxy-dist/tool-data/shared/jars/
+2.- Copy SPARQLGalaxy.jar and SPARQLGalaxy_TEXT.jar to galaxy-dist/tool-data/shared/jars/
  
 3.- Create directory galaxy-dist/tools/sparql_galaxy/
 
Binary file SPARQLGalaxy.jar has changed
--- a/SPARQLGalaxy.xml	Thu Oct 25 18:35:25 2012 +0200
+++ b/SPARQLGalaxy.xml	Wed Dec 04 08:17:00 2013 +0100
@@ -1,11 +1,28 @@
-<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>
+<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>
-	  java -Xmx3000M -Xms250M -DentityExpansionLimit=1000000000 -jar ${__tool_data_path__}/shared/jars/SPARQLGalaxy.jar $ontology $query_file > $output 2>/dev/null 
+	  #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 ontology file"/>
-		<param name="query_file" type="data" label="Input SPARQL query file"/>
+		<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" />
@@ -23,13 +40,17 @@
 
 **Usage**
 
-  An OWL ontology in RDF/XML syntax and a plain text file with the SPARQL are needed. See bundle for examples.
+  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:8090/
+  http://biordf.org:8983/
+  
+  RDF:
+  
+  http://www.w3.org/standards/techs/rdf
   
   SPARQL: 
   
Binary file SPARQLGalaxy_TEXT.jar has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/jbmsquery.rdf	Wed Dec 04 08:17:00 2013 +0100
@@ -0,0 +1,11 @@
+<?xml version="1.0"?>
+<rdf:RDF
+xmlns:sio="http://semanticscience.org/resource/"
+xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
+
+<rdf:Description rdf:nodeID="A8">
+    <sio:SIO_000300 rdf:datatype="http://www.w3.org/2001/XMLSchema#string">MEEPQSDPSVEPPLSQETFSDLWKLLPENNVLSPLPSQAMDDLMLSPDDIEQWFTEDPGPDEAPRMPEAAPPVAPAPAAPTPAAPAPAPSWPLSSSVPSQKTYQGSYGFRLGFLHSGTAKSVTCTYSPALNKMFCQLAKTCPVQLWVDSTPPPGTRVRAMAIYKQSQHMTEVVRRCPHHERCSDSDGLAPPQHLIRVEGNLRVEYLDDRNTFRHSVVVPYEPPEVGSDCTTIHYNYMCNSSCMGGMNRRPILTIITLEDSSGNLLGRNSFEVRVCACPGRDRRTEEENLRKKGEPHHELPPGSTKRALPNNTSSSPQPKKKPLDGEYFTLQIRGRERFEMFRELNEALELKDAQAGKEPGGSRAHSSHLKSKKGQSTSRHKKLMFKTEGPDSD</sio:SIO_000300>
+    <rdf:type rdf:resource="http://semanticscience.org/resource/SIO_010015"/>
+ </rdf:Description>
+ 
+ </rdf:RDF>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/query.sparql	Wed Dec 04 08:17:00 2013 +0100
@@ -0,0 +1,4 @@
+SELECT ?o ?s
+WHERE {
+ ?s <http://semanticscience.org/resource/SIO_000300> ?o.
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/query_2.sparql	Wed Dec 04 08:17:00 2013 +0100
@@ -0,0 +1,1 @@
+SELECT * WHERE { ?s ?p ?o } LIMIT 10
\ No newline at end of file
--- a/src/es/cbgp/galaxy/sparql/jena/OntologyManager.java	Thu Oct 25 18:35:25 2012 +0200
+++ b/src/es/cbgp/galaxy/sparql/jena/OntologyManager.java	Wed Dec 04 08:17:00 2013 +0100
@@ -2,14 +2,14 @@
 
 import java.io.File;
 
-import com.hp.hpl.jena.ontology.OntModel;
+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 OntModel model;
+	private Model model;
 	private SPARQLQueryEngine sqe;
 
 	public OntologyManager(String of, String sf) throws Exception {
@@ -19,7 +19,7 @@
 	}
 
 	private void init() throws Exception {
-		this.model = ModelFactory.createOntologyModel();
+		this.model = ModelFactory.createDefaultModel();
 		this.model.read(new File(ontFile).toURI().toString());
 	}
 
--- a/src/es/cbgp/galaxy/sparql/jena/SPARQLQueryEngine.java	Thu Oct 25 18:35:25 2012 +0200
+++ b/src/es/cbgp/galaxy/sparql/jena/SPARQLQueryEngine.java	Wed Dec 04 08:17:00 2013 +0100
@@ -4,16 +4,17 @@
 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.rdf.model.Model;
+import com.hp.hpl.jena.rdf.model.RDFNode;
 import com.hp.hpl.jena.shared.Lock;
 
 public class SPARQLQueryEngine {
 
-	private OntModel queryModel;
+	private Model queryModel;
 	private String sparqlFile;
 
-	public SPARQLQueryEngine(OntModel om) {
+	public SPARQLQueryEngine(Model om) {
 		this.queryModel = om;
 	}
 
@@ -33,9 +34,8 @@
 				Iterator<String> vars = qs.varNames();
 				while (vars.hasNext()) {
 					String var = vars.next();
-					if (!qs.getResource(var).isAnon()) {
-						res += "?" + var + "\t" + qs.getResource(var) + "\n";
-					}
+					res += "?" + var + "\t" + getValue(qs, var) + "\r\n";
+
 				}
 			}
 			return res;
@@ -50,6 +50,38 @@
 		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));
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/info/wilkinsonlab/galaxy/sparql/text/SPARQLGalaxy_TEXT.java	Wed Dec 04 08:17:00 2013 +0100
@@ -0,0 +1,51 @@
+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();
+	}
+}