├── .gitignore ├── COPYING ├── README.md ├── java ├── .classpath ├── .project ├── README.md ├── lib │ ├── jena-arq │ │ ├── icu4j-3.4.4.jar │ │ ├── jena-arq-2.9.4-tests.jar │ │ ├── jena-arq-2.9.4.jar │ │ ├── jena-core-2.7.4-tests.jar │ │ ├── jena-core-2.7.4.jar │ │ ├── jena-iri-0.9.4.jar │ │ ├── junit-4.5.jar │ │ ├── log4j-1.2.14.jar │ │ ├── lucene-core-2.3.1.jar │ │ ├── slf4j-api-1.5.8.jar │ │ ├── slf4j-log4j12-1.5.8.jar │ │ ├── stax-api-1.0.1.jar │ │ ├── wstx-asl-3.2.9.jar │ │ └── xercesImpl-2.7.1.jar │ └── openrdf-alibaba │ │ ├── alibaba-repository-sparql-2.0-beta9-patched.jar │ │ ├── commons-cli-1.2.jar │ │ ├── commons-codec-1.4.jar │ │ ├── commons-dbcp-1.2.2.jar │ │ ├── commons-httpclient-3.1.jar │ │ ├── commons-logging-1.1.1.jar │ │ ├── commons-pool-1.3.jar │ │ ├── httpcore-4.1.3.jar │ │ ├── httpcore-nio-4.1.3.jar │ │ ├── javassist-3.11.0.GA.jar │ │ ├── openrdf-alibaba-2.0-beta12.jar │ │ ├── openrdf-sesame-2.4.2-onejar.jar │ │ ├── slf4j-api-1.5.10.jar │ │ └── slf4j-jdk14-1.5.10.jar └── src │ └── org │ └── ncbo │ └── stanford │ └── sparql │ └── examples │ ├── ClosureExample.java │ ├── GetAllTerms.java │ ├── JenaARQFederationExample.java │ ├── JenaARQTest.java │ ├── OpenRDFAlibabaTest.java │ └── SimpleTest.java ├── javascript ├── index.html ├── node_test.js └── sparql.js ├── perl ├── sparql.pm └── test.pl ├── python ├── all_props ├── complex.py ├── sparql1.py └── sparql2.py └── ruby ├── sparql1.rb └── sparql2.rb /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | *.pyc 3 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | Copyright 2005–2011, The Board of Trustees of Leland Stanford Junior University. All rights reserved. 2 | 3 | Redistribution and use in source and binary forms, with or without modification, are 4 | permitted provided that the following conditions are met: 5 | 6 | 1. Redistributions of source code must retain the above copyright notice, this list of 7 | conditions and the following disclaimer. 8 | 9 | 2. Redistributions in binary form must reproduce the above copyright notice, this list 10 | of conditions and the following disclaimer in the documentation and/or other materials 11 | provided with the distribution. 12 | 13 | THIS SOFTWARE IS PROVIDED BY ''AS IS'' AND ANY EXPRESS OR IMPLIED 14 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 15 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL OR 16 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 17 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 18 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 19 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 20 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 21 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 22 | 23 | The views and conclusions contained in the software and documentation are those of the 24 | authors and should not be interpreted as representing official policies, either expressed 25 | or implied, of The Board of Trustees of Leland Stanford Junior University. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This repository contains code examples to query sparql.bioontology.org. 2 | 3 | At sparql.bioontology.org we host two databases: 4 | 5 | * sparql.bioontology.org/ontologies/sparql to host ontology metadata and content. 6 | * sparql.bioontology.org/mappings/sparql to host BioPortal's mappings. 7 | 8 | By default queries pointing to sparql.bioontology.org/sparql use the ontologies endpoint. To issue queries that target data from both endpoints see related information and examples on SPARQL federation. 9 | 10 | Examples are provided for the following platforms/languages: 11 | - Java: 12 | * Java with no 3-party libs (SimpleTest.java) 13 | * Java with JenaARQ (JenaARQTest.java) 14 | * Java with OpenRDF [*] (OpenRDFAlibabaTest.java) 15 | - Python: 16 | * Python with no 3-party libs (sparql1.py) 17 | * Python with SPARQLWrapper [1] (sparql2.py) 18 | - Javascript: 19 | * Javacript with the SPARQLClient [2] lib (index.html) 20 | * Javascript with node.js. (node_test.js) 21 | - Perl using sparql.pm from [3] (test.pl) 22 | - Ruby. 23 | - TODO 24 | * C# 25 | * Scala 26 | 27 | [1] http://sparql-wrapper.sourceforge.net/ 28 | [2] http://thefigtrees.net/lee/sw/sparql.js (slightly modified to allow API keys) 29 | [3] https://github.com/swh/Perl-SPARQL-client-library (slightly modified to allow API keys) 30 | [*] The jar file alibaba-repository-sparql-2.0-beta9-patched.jar has been patched to allow API keys and GET HTTP requests. 31 | 32 | 33 | ------------ 34 | Extra notes 35 | ------------ 36 | 37 | * BSD 2-clause license ("Simplified BSD License" or "FreeBSD License") applies to this repository, see COPYING file. 38 | 39 | * If you do not have an account in BioPortal you'll need one to use the service. 40 | Register at http://bioportal.bioontology.org/accounts/new 41 | 42 | * Please visit the following links to get extra information about this service: 43 | - http://www.bioontology.org/wiki/index.php/SPARQL_BioPortal 44 | - http://www.bioontology.org/wiki/index.php/SPARQL_Release_Notes_And_Usage_Policy 45 | 46 | -------- 47 | Support 48 | -------- 49 | 50 | Contact us via email: support@bioontology.org 51 | 52 | ----------------- 53 | Acknowledgments 54 | ----------------- 55 | 56 | The National Center for Biomedical Ontology is one of the National Centers for Biomedical Computing supported by the NHGRI, the NHLBI, and the NIH Common Fund under grant U54-HG004028. 57 | 58 | 59 | -------------------------------------------------------------------------------- /java/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /java/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | sparql_examples 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /java/README.md: -------------------------------------------------------------------------------- 1 | This folder is an Eclipse Java project, see (.classpath and .project). Use the Eclipse import project tool to get it up and running. It is self-contained, no need to download and setup extra libraries. 2 | -------------------------------------------------------------------------------- /java/lib/jena-arq/icu4j-3.4.4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ncbo/sparql-code-examples/dfa2fa1583fd2f67fbd8045f0218e3f4dc578cd8/java/lib/jena-arq/icu4j-3.4.4.jar -------------------------------------------------------------------------------- /java/lib/jena-arq/jena-arq-2.9.4-tests.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ncbo/sparql-code-examples/dfa2fa1583fd2f67fbd8045f0218e3f4dc578cd8/java/lib/jena-arq/jena-arq-2.9.4-tests.jar -------------------------------------------------------------------------------- /java/lib/jena-arq/jena-arq-2.9.4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ncbo/sparql-code-examples/dfa2fa1583fd2f67fbd8045f0218e3f4dc578cd8/java/lib/jena-arq/jena-arq-2.9.4.jar -------------------------------------------------------------------------------- /java/lib/jena-arq/jena-core-2.7.4-tests.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ncbo/sparql-code-examples/dfa2fa1583fd2f67fbd8045f0218e3f4dc578cd8/java/lib/jena-arq/jena-core-2.7.4-tests.jar -------------------------------------------------------------------------------- /java/lib/jena-arq/jena-core-2.7.4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ncbo/sparql-code-examples/dfa2fa1583fd2f67fbd8045f0218e3f4dc578cd8/java/lib/jena-arq/jena-core-2.7.4.jar -------------------------------------------------------------------------------- /java/lib/jena-arq/jena-iri-0.9.4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ncbo/sparql-code-examples/dfa2fa1583fd2f67fbd8045f0218e3f4dc578cd8/java/lib/jena-arq/jena-iri-0.9.4.jar -------------------------------------------------------------------------------- /java/lib/jena-arq/junit-4.5.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ncbo/sparql-code-examples/dfa2fa1583fd2f67fbd8045f0218e3f4dc578cd8/java/lib/jena-arq/junit-4.5.jar -------------------------------------------------------------------------------- /java/lib/jena-arq/log4j-1.2.14.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ncbo/sparql-code-examples/dfa2fa1583fd2f67fbd8045f0218e3f4dc578cd8/java/lib/jena-arq/log4j-1.2.14.jar -------------------------------------------------------------------------------- /java/lib/jena-arq/lucene-core-2.3.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ncbo/sparql-code-examples/dfa2fa1583fd2f67fbd8045f0218e3f4dc578cd8/java/lib/jena-arq/lucene-core-2.3.1.jar -------------------------------------------------------------------------------- /java/lib/jena-arq/slf4j-api-1.5.8.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ncbo/sparql-code-examples/dfa2fa1583fd2f67fbd8045f0218e3f4dc578cd8/java/lib/jena-arq/slf4j-api-1.5.8.jar -------------------------------------------------------------------------------- /java/lib/jena-arq/slf4j-log4j12-1.5.8.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ncbo/sparql-code-examples/dfa2fa1583fd2f67fbd8045f0218e3f4dc578cd8/java/lib/jena-arq/slf4j-log4j12-1.5.8.jar -------------------------------------------------------------------------------- /java/lib/jena-arq/stax-api-1.0.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ncbo/sparql-code-examples/dfa2fa1583fd2f67fbd8045f0218e3f4dc578cd8/java/lib/jena-arq/stax-api-1.0.1.jar -------------------------------------------------------------------------------- /java/lib/jena-arq/wstx-asl-3.2.9.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ncbo/sparql-code-examples/dfa2fa1583fd2f67fbd8045f0218e3f4dc578cd8/java/lib/jena-arq/wstx-asl-3.2.9.jar -------------------------------------------------------------------------------- /java/lib/jena-arq/xercesImpl-2.7.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ncbo/sparql-code-examples/dfa2fa1583fd2f67fbd8045f0218e3f4dc578cd8/java/lib/jena-arq/xercesImpl-2.7.1.jar -------------------------------------------------------------------------------- /java/lib/openrdf-alibaba/alibaba-repository-sparql-2.0-beta9-patched.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ncbo/sparql-code-examples/dfa2fa1583fd2f67fbd8045f0218e3f4dc578cd8/java/lib/openrdf-alibaba/alibaba-repository-sparql-2.0-beta9-patched.jar -------------------------------------------------------------------------------- /java/lib/openrdf-alibaba/commons-cli-1.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ncbo/sparql-code-examples/dfa2fa1583fd2f67fbd8045f0218e3f4dc578cd8/java/lib/openrdf-alibaba/commons-cli-1.2.jar -------------------------------------------------------------------------------- /java/lib/openrdf-alibaba/commons-codec-1.4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ncbo/sparql-code-examples/dfa2fa1583fd2f67fbd8045f0218e3f4dc578cd8/java/lib/openrdf-alibaba/commons-codec-1.4.jar -------------------------------------------------------------------------------- /java/lib/openrdf-alibaba/commons-dbcp-1.2.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ncbo/sparql-code-examples/dfa2fa1583fd2f67fbd8045f0218e3f4dc578cd8/java/lib/openrdf-alibaba/commons-dbcp-1.2.2.jar -------------------------------------------------------------------------------- /java/lib/openrdf-alibaba/commons-httpclient-3.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ncbo/sparql-code-examples/dfa2fa1583fd2f67fbd8045f0218e3f4dc578cd8/java/lib/openrdf-alibaba/commons-httpclient-3.1.jar -------------------------------------------------------------------------------- /java/lib/openrdf-alibaba/commons-logging-1.1.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ncbo/sparql-code-examples/dfa2fa1583fd2f67fbd8045f0218e3f4dc578cd8/java/lib/openrdf-alibaba/commons-logging-1.1.1.jar -------------------------------------------------------------------------------- /java/lib/openrdf-alibaba/commons-pool-1.3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ncbo/sparql-code-examples/dfa2fa1583fd2f67fbd8045f0218e3f4dc578cd8/java/lib/openrdf-alibaba/commons-pool-1.3.jar -------------------------------------------------------------------------------- /java/lib/openrdf-alibaba/httpcore-4.1.3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ncbo/sparql-code-examples/dfa2fa1583fd2f67fbd8045f0218e3f4dc578cd8/java/lib/openrdf-alibaba/httpcore-4.1.3.jar -------------------------------------------------------------------------------- /java/lib/openrdf-alibaba/httpcore-nio-4.1.3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ncbo/sparql-code-examples/dfa2fa1583fd2f67fbd8045f0218e3f4dc578cd8/java/lib/openrdf-alibaba/httpcore-nio-4.1.3.jar -------------------------------------------------------------------------------- /java/lib/openrdf-alibaba/javassist-3.11.0.GA.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ncbo/sparql-code-examples/dfa2fa1583fd2f67fbd8045f0218e3f4dc578cd8/java/lib/openrdf-alibaba/javassist-3.11.0.GA.jar -------------------------------------------------------------------------------- /java/lib/openrdf-alibaba/openrdf-alibaba-2.0-beta12.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ncbo/sparql-code-examples/dfa2fa1583fd2f67fbd8045f0218e3f4dc578cd8/java/lib/openrdf-alibaba/openrdf-alibaba-2.0-beta12.jar -------------------------------------------------------------------------------- /java/lib/openrdf-alibaba/openrdf-sesame-2.4.2-onejar.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ncbo/sparql-code-examples/dfa2fa1583fd2f67fbd8045f0218e3f4dc578cd8/java/lib/openrdf-alibaba/openrdf-sesame-2.4.2-onejar.jar -------------------------------------------------------------------------------- /java/lib/openrdf-alibaba/slf4j-api-1.5.10.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ncbo/sparql-code-examples/dfa2fa1583fd2f67fbd8045f0218e3f4dc578cd8/java/lib/openrdf-alibaba/slf4j-api-1.5.10.jar -------------------------------------------------------------------------------- /java/lib/openrdf-alibaba/slf4j-jdk14-1.5.10.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ncbo/sparql-code-examples/dfa2fa1583fd2f67fbd8045f0218e3f4dc578cd8/java/lib/openrdf-alibaba/slf4j-jdk14-1.5.10.jar -------------------------------------------------------------------------------- /java/src/org/ncbo/stanford/sparql/examples/ClosureExample.java: -------------------------------------------------------------------------------- 1 | package org.ncbo.stanford.sparql.examples; 2 | 3 | import com.hp.hpl.jena.query.Query; 4 | import com.hp.hpl.jena.query.QueryExecutionFactory; 5 | import com.hp.hpl.jena.query.QueryFactory; 6 | import com.hp.hpl.jena.query.QuerySolution; 7 | import com.hp.hpl.jena.query.ResultSet; 8 | import com.hp.hpl.jena.query.ResultSetFormatter; 9 | import com.hp.hpl.jena.rdf.model.Literal; 10 | import com.hp.hpl.jena.rdf.model.RDFNode; 11 | import com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP; 12 | 13 | /** 14 | * This is an example built on top of the Jena ARQ library. 15 | * See: http://jena.sourceforge.net/ARQ/documentation.html 16 | */ 17 | public class ClosureExample { 18 | 19 | private String service = null; 20 | private String apikey = null; 21 | 22 | public ClosureExample(String service, String apikey) { 23 | this.service = service; 24 | this.apikey = apikey; 25 | } 26 | public ResultSet executeQuery(String queryString,String rules) throws Exception { 27 | Query query = QueryFactory.create(queryString) ; 28 | 29 | QueryEngineHTTP qexec = QueryExecutionFactory.createServiceRequest(this.service, query); 30 | qexec.addParam("apikey", this.apikey); 31 | if (rules != null) 32 | qexec.addParam("rules", rules); 33 | ResultSet results = qexec.execSelect() ; 34 | return results; 35 | 36 | } 37 | public ResultSet executeQuery(String queryString) throws Exception { 38 | return executeQuery(queryString,null); 39 | 40 | } 41 | public static void main(String[] args) throws Exception { 42 | String sparqlService = "http://sparql.bioontology.org/sparql"; 43 | String apikey = "YOUR API KEY"; 44 | 45 | /* 46 | * More query examples here: 47 | * http://sparql.bioontology.org/examples 48 | */ 49 | String query = "PREFIX rdfs: " + 50 | "SELECT DISTINCT * " + 51 | "FROM WHERE { " + 52 | " rdfs:subClassOf ?super . }"; 53 | 54 | ClosureExample test = new ClosureExample(sparqlService,apikey); 55 | 56 | /** 57 | * Direct super classes no rules are passed with the query 58 | */ 59 | ResultSet results = test.executeQuery(query); 60 | String textResults = ResultSetFormatter.asText(results); 61 | System.out.println(textResults); 62 | 63 | /** 64 | * Same query with hierarchy closure. 65 | * We need to add the parameter "rules" with the code SUBC 66 | * SUBC enables rdfs:subClassOf closures to the SPARQL query. 67 | */ 68 | results = test.executeQuery(query,"SUBC"); 69 | textResults = ResultSetFormatter.asText(results); 70 | System.out.println(textResults); 71 | 72 | /** 73 | * prefNames and closures. 74 | * This example uses the Nano Particle Ontology. 75 | * This ontology has its own property to record preferred names 76 | * that property is subPropertyOf skos:prefLabel 77 | * 78 | * For this query we need to enable to rules: 79 | * (1) SUBC for subClassOf hierarchies 80 | * (2) SUBP for subPropertyOf 81 | * Also we need to include the GLOBALS graph 82 | * Since the subPropertyOf statements are contained in it. 83 | */ 84 | String queryLabelsAndClosure = 85 | "PREFIX rdfs: " + 86 | "PREFIX skos: " + 87 | "SELECT DISTINCT * " + 88 | "FROM " + 89 | "FROM " + 90 | " WHERE { " + 91 | " rdfs:subClassOf ?super . " + 92 | "?super skos:prefLabel ?label }"; 93 | results = test.executeQuery(queryLabelsAndClosure,"SUBC+SUBP"); 94 | textResults = ResultSetFormatter.asText(results); 95 | System.out.println(textResults); 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /java/src/org/ncbo/stanford/sparql/examples/GetAllTerms.java: -------------------------------------------------------------------------------- 1 | package org.ncbo.stanford.sparql.examples; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Iterator; 5 | import java.util.LinkedList; 6 | import java.util.List; 7 | import java.util.Queue; 8 | 9 | import com.hp.hpl.jena.query.Query; 10 | import com.hp.hpl.jena.query.QueryExecutionFactory; 11 | import com.hp.hpl.jena.query.QueryFactory; 12 | import com.hp.hpl.jena.query.QuerySolution; 13 | import com.hp.hpl.jena.query.ResultSet; 14 | import com.hp.hpl.jena.rdf.model.Resource; 15 | import com.hp.hpl.jena.sparql.engine.http.HttpQuery; 16 | import com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP; 17 | 18 | public class GetAllTerms { 19 | 20 | 21 | 22 | private static String KEY = "YOUR KEY"; 23 | private static String SERVICE = "http://sparql.bioontology.org/sparql/"; 24 | 25 | /* warning: do not increment PAGE size until POST is supported in the SPARQL endpoint 26 | * if > 65 it can hit URL lenght limits. 27 | */ 28 | private static int PAGE_SIZE = 65; /* WARNING KEEP UNDER 65 UNTIL FURTHER NOTICE */ 29 | 30 | private static String PREFIXES = "PREFIX xsd: \n" + 31 | "PREFIX rdfs: \n" + 32 | "PREFIX owl: \n" + 33 | "PREFIX meta: \n" + 34 | "PREFIX skos: \n" + 35 | "PREFIX umls: \n"; 36 | 37 | private static String ALL_URIS = "SELECT DISTINCT ?uri \n" + 38 | "WHERE { GRAPH <$GRAPH> {\n" + 39 | " ?uri a owl:Class .\n" + 40 | "}}"; /* To play with a smaller extraction include 'LIMIT 500' */ 41 | 42 | 43 | public static ResultSet executeQuery(String queryString) throws Exception { 44 | Query query = QueryFactory.create(queryString) ; 45 | HttpQuery.urlLimit = 100000; 46 | QueryEngineHTTP qexec = QueryExecutionFactory.createServiceRequest(SERVICE, query); 47 | qexec.addParam("apikey", KEY); 48 | ResultSet results = qexec.execSelect() ; 49 | return results; 50 | } 51 | 52 | public static List getAllOntologyIRIs(String ontologyGraph) throws Exception { 53 | String query = PREFIXES + ALL_URIS.replace("$GRAPH", ontologyGraph); 54 | ResultSet rs = executeQuery(query); 55 | List uris = new ArrayList(); 56 | while (rs.hasNext()) { 57 | QuerySolution sol = rs.next(); 58 | uris.add(sol.get("uri").asResource()); 59 | } 60 | return uris; 61 | } 62 | 63 | 64 | public static String getQueryTermBatch(List resources, String[] properties) { 65 | 66 | StringBuilder query = new StringBuilder(); 67 | query.append(PREFIXES); 68 | query.append("SELECT * WHERE {\n"); 69 | 70 | 71 | /* property binding for the query */ 72 | for (String prop : properties) { 73 | String varName = prop.split(":")[1]; /* lets use the fragment after ':' as the var name */ 74 | query.append("?uri " + prop + " ?"+varName + " .\n"); 75 | } 76 | 77 | /* we limit the query to the resources in the batch. For that we use FILTER and || */ 78 | query.append("FILTER(\n"); 79 | for (int i = 0; i < resources.size(); i++) { 80 | query.append("?uri = <" + resources.get(i).toString() + "> "); 81 | if (i+1 < resources.size()) 82 | query.append(" ||\n"); 83 | } 84 | query.append(") }\n"); 85 | 86 | return query.toString(); 87 | } 88 | 89 | 90 | public static void getTermBatch(List resources, String[] properties) throws Exception { 91 | String queryBatch = getQueryTermBatch(resources,properties); 92 | 93 | ResultSet rs = executeQuery(queryBatch); 94 | while (rs.hasNext()) { 95 | QuerySolution sol = rs.next(); 96 | 97 | /** 98 | * TODO: your code here ... do something with this term. 99 | * 100 | * The sample code below just prints the term information. 101 | */ 102 | /* Remove comments to print resultset 103 | 104 | Iterator varnames = sol.varNames(); 105 | System.out.print("Term Data : "); 106 | while (varnames.hasNext()) { 107 | String var = varnames.next(); 108 | if (!sol.get(var).isLiteral()) 109 | System.out.print(var+"="+sol.get(var).toString()+" "); 110 | else 111 | System.out.print(var+"="+sol.get(var).asLiteral().getValue().toString()+" "); 112 | 113 | } 114 | System.out.println(); 115 | */ 116 | } 117 | } 118 | 119 | 120 | public static void traverseAllTerms(List uris,String[] properties) throws Exception { 121 | List batch = new ArrayList(); 122 | Queue queueResources = new LinkedList(uris); 123 | while(queueResources.size() > 0) { 124 | batch.add(queueResources.poll()); 125 | if (batch.size() % PAGE_SIZE == 0) { 126 | getTermBatch(batch,properties); 127 | batch = new ArrayList(); 128 | } 129 | if (queueResources.size() % 1000 == 0) { 130 | System.out.println("Count Down " + queueResources.size()); 131 | } 132 | } 133 | if (batch.size() > 0) { 134 | getTermBatch(batch,properties); 135 | } 136 | } 137 | 138 | public static void main(String[] args) throws Exception { 139 | 140 | long t0 = System.currentTimeMillis(); 141 | /** 142 | * This is the graph where SNOMEDCT is placed in the triple store. 143 | * The first example in http://sparql.bioontology.org/examples 144 | * shows a query that retrieves all the different graphs with their ontology IDs. 145 | */ 146 | String snomedGraph = "http://bioportal.bioontology.org/ontologies/SNOMEDCT"; 147 | 148 | /** 149 | * These are the two predicates we want to bind when we 150 | * traverse over all the terms in the ontology. 151 | * 152 | * Other properties can be included i.e: skos:prefLabel, 153 | * skos:altLabel, rdfs:subClassOf 154 | */ 155 | String[] properties = { "umls:cui", "skos:notation" }; 156 | 157 | 158 | /** 159 | * Step 1: Get all the ontology Term IRIs. 160 | */ 161 | List allURIs = getAllOntologyIRIs(snomedGraph); 162 | 163 | /** 164 | * Step 2: Get the property data for the term IRIs in batches of PAGE_SIZE 165 | */ 166 | traverseAllTerms(allURIs,properties); 167 | 168 | System.out.printf("%d terms retrieved in %.2f s\n",allURIs.size(),(System.currentTimeMillis() - t0)/1000.0); 169 | } 170 | } 171 | -------------------------------------------------------------------------------- /java/src/org/ncbo/stanford/sparql/examples/JenaARQFederationExample.java: -------------------------------------------------------------------------------- 1 | package org.ncbo.stanford.sparql.examples; 2 | 3 | import com.hp.hpl.jena.query.QueryExecution; 4 | import com.hp.hpl.jena.query.QueryExecutionFactory; 5 | import com.hp.hpl.jena.query.QueryFactory; 6 | import com.hp.hpl.jena.query.QuerySolution; 7 | import com.hp.hpl.jena.query.ResultSet; 8 | import com.hp.hpl.jena.rdf.model.ModelFactory; 9 | import com.hp.hpl.jena.rdf.model.RDFNode; 10 | import com.hp.hpl.jena.sparql.core.DatasetImpl; 11 | 12 | /** 13 | * This is an example built on top of the Jena ARQ library. 14 | * See: http://jena.sourceforge.net/ARQ/documentation.html 15 | */ 16 | public class JenaARQFederationExample { 17 | 18 | public JenaARQFederationExample() { 19 | } 20 | public ResultSet executeQuery(String queryString) throws Exception { 21 | 22 | QueryExecution exec = QueryExecutionFactory.create(QueryFactory.create(queryString), new 23 | DatasetImpl(ModelFactory.createDefaultModel())); 24 | return exec.execSelect(); 25 | 26 | } 27 | public static void main(String[] args) throws Exception { 28 | 29 | /** 30 | * For documentation please read "Federated SPARQL queries" 31 | * at http://www.bioontology.org/wiki/index.php/SPARQL_BioPortal 32 | */ 33 | JenaARQFederationExample test = new JenaARQFederationExample(); 34 | 35 | String query = "PREFIX map: \n" + 36 | "PREFIX rdfs: \n" + 37 | "PREFIX skos: \n" + 38 | "SELECT DISTINCT ?mappedParent WHERE {\n" + 39 | " SERVICE {\n" + 40 | " ?mapping map:target .\n" + 41 | " ?mapping map:source ?source .\n" + 42 | " }\n" + 43 | " SERVICE {\n" + 44 | " ?source rdfs:subClassOf ?mappedParent .\n" + 45 | " }\n" + 46 | "}"; 47 | 48 | ResultSet results = test.executeQuery(query); 49 | for ( ; results.hasNext() ; ) { 50 | QuerySolution soln = results.nextSolution() ; 51 | System.out.println(soln); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /java/src/org/ncbo/stanford/sparql/examples/JenaARQTest.java: -------------------------------------------------------------------------------- 1 | package org.ncbo.stanford.sparql.examples; 2 | 3 | import com.hp.hpl.jena.query.Query; 4 | import com.hp.hpl.jena.query.QueryExecutionFactory; 5 | import com.hp.hpl.jena.query.QueryFactory; 6 | import com.hp.hpl.jena.query.QuerySolution; 7 | import com.hp.hpl.jena.query.ResultSet; 8 | import com.hp.hpl.jena.rdf.model.Literal; 9 | import com.hp.hpl.jena.rdf.model.RDFNode; 10 | import com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP; 11 | 12 | /** 13 | * This is an example built on top of the Jena ARQ library. 14 | * See: http://jena.sourceforge.net/ARQ/documentation.html 15 | */ 16 | public class JenaARQTest { 17 | 18 | private String service = null; 19 | private String apikey = null; 20 | 21 | public JenaARQTest(String service, String apikey) { 22 | this.service = service; 23 | this.apikey = apikey; 24 | } 25 | public ResultSet executeQuery(String queryString) throws Exception { 26 | Query query = QueryFactory.create(queryString) ; 27 | 28 | QueryEngineHTTP qexec = QueryExecutionFactory.createServiceRequest(this.service, query); 29 | qexec.addParam("apikey", this.apikey); 30 | ResultSet results = qexec.execSelect() ; 31 | return results; 32 | 33 | } 34 | public static void main(String[] args) throws Exception { 35 | String sparqlService = "http://sparql.bioontology.org/sparql"; 36 | String apikey = "YOUR API KEY"; 37 | 38 | /* 39 | * More query examples here: 40 | * http://sparql.bioontology.org/examples 41 | */ 42 | String query = "PREFIX omv: " + 43 | "SELECT ?ont ?name ?acr " + 44 | "WHERE { ?ont a omv:Ontology; " + 45 | "omv:acronym ?acr; " + 46 | "omv:name ?name . " + 47 | "}"; 48 | 49 | JenaARQTest test = new JenaARQTest(sparqlService,apikey); 50 | ResultSet results = test.executeQuery(query); 51 | for ( ; results.hasNext() ; ) { 52 | QuerySolution soln = results.nextSolution() ; 53 | RDFNode ontUri = soln.get("ont") ; 54 | Literal name = soln.getLiteral("name") ; 55 | Literal acr = soln.getLiteral("acr") ; 56 | System.out.println(ontUri + " ---- " + name + " ---- " + acr); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /java/src/org/ncbo/stanford/sparql/examples/OpenRDFAlibabaTest.java: -------------------------------------------------------------------------------- 1 | package org.ncbo.stanford.sparql.examples; 2 | 3 | import org.openrdf.query.BindingSet; 4 | import org.openrdf.query.QueryLanguage; 5 | import org.openrdf.query.TupleQuery; 6 | import org.openrdf.query.TupleQueryResult; 7 | import org.openrdf.repository.RepositoryException; 8 | import org.openrdf.repository.sparql.SPARQLConnection; 9 | import org.openrdf.repository.sparql.SPARQLRepository; 10 | import org.openrdf.repository.sparql.config.SPARQLRepositoryConfig; 11 | import org.openrdf.repository.sparql.config.SPARQLRepositoryFactory; 12 | 13 | public class OpenRDFAlibabaTest { 14 | 15 | private String service = null; 16 | private String apikey = null; 17 | private SPARQLConnection conn = null; 18 | 19 | public OpenRDFAlibabaTest(String service, String apikey) throws Exception { 20 | this.service = service; 21 | this.apikey = apikey; 22 | 23 | SPARQLRepositoryConfig config = new SPARQLRepositoryConfig(); 24 | config.setMethod(SPARQLRepositoryConfig.HTTP_GET); 25 | config.setURL(this.service); 26 | config.addParameter("apikey", this.apikey); 27 | 28 | SPARQLRepositoryFactory factory = new SPARQLRepositoryFactory(); 29 | SPARQLRepository repo = factory.getRepository(config); 30 | this.conn = (SPARQLConnection) repo.getConnection(); 31 | } 32 | 33 | public TupleQueryResult executeQuery(String queryText) throws Exception { 34 | 35 | TupleQuery query = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryText); 36 | return query.evaluate(); 37 | } 38 | 39 | public void close() throws RepositoryException { 40 | this.conn.close(); 41 | } 42 | public static void main(String[] args) throws Exception { 43 | 44 | String sparqlService = "http://sparql.bioontology.org/sparql"; 45 | String apikey = "YOUR API KEY HERE"; 46 | 47 | /* 48 | * More query examples here: 49 | * http://sparql.bioontology.org/examples 50 | */ 51 | String query = "PREFIX omv: " + 52 | "SELECT ?ont ?name ?acr " + 53 | "WHERE { ?ont a omv:Ontology; " + 54 | "omv:acronym ?acr; " + 55 | "omv:name ?name . " + 56 | "}"; 57 | 58 | OpenRDFAlibabaTest test = new OpenRDFAlibabaTest(sparqlService, apikey); 59 | 60 | TupleQueryResult result = test.executeQuery(query); 61 | while(result.hasNext()) { 62 | BindingSet row = result.next(); 63 | System.out.println(row.getBinding("ont").getValue() + " ---- " + 64 | row.getBinding("name").getValue() + " ---- " + row.getBinding("acr").getValue()); 65 | } 66 | test.close(); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /java/src/org/ncbo/stanford/sparql/examples/SimpleTest.java: -------------------------------------------------------------------------------- 1 | package org.ncbo.stanford.sparql.examples; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.InputStream; 5 | import java.io.InputStreamReader; 6 | import java.net.HttpURLConnection; 7 | import java.net.URL; 8 | import java.net.URLEncoder; 9 | 10 | /** 11 | * This is a standalone test that uses BioPortal SPARQL Endpoint without any extra libraries. 12 | * The result set format is CSV. 13 | */ 14 | public class SimpleTest { 15 | 16 | private String service = null; 17 | private String apikey = null; 18 | 19 | public SimpleTest(String service, String apikey) { 20 | this.service = service; 21 | this.apikey = apikey; 22 | } 23 | 24 | public String executeQuery(String queryText, String acceptFormat) throws Exception { 25 | String httpQueryString = String.format("query=%s&apikey=%s", 26 | URLEncoder.encode(queryText, "UTF-8"), 27 | URLEncoder.encode(this.apikey, "UTF-8")); 28 | 29 | URL url = new URL(this.service + "?" + httpQueryString); 30 | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 31 | conn.setRequestMethod("GET"); 32 | conn.setRequestProperty("Accept", acceptFormat); 33 | 34 | conn.connect(); 35 | InputStream in = conn.getInputStream(); 36 | BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 37 | StringBuilder buff = new StringBuilder(); 38 | String line = null; 39 | while ((line = reader.readLine())!=null) { 40 | buff.append(line); 41 | buff.append("\n"); 42 | } 43 | conn.disconnect(); 44 | return buff.toString(); 45 | } 46 | 47 | public static void main(String[] args) throws Exception { 48 | 49 | String sparqlService = "http://sparql.bioontology.org/sparql"; 50 | String apikey = "YOUR API KEY HERE"; 51 | 52 | /* 53 | * More query examples here: 54 | * http://sparql.bioontology.org/examples 55 | */ 56 | String query = "PREFIX omv: " + 57 | "SELECT ?ont ?name ?acr " + 58 | "WHERE { ?ont a omv:Ontology; " + 59 | "omv:acronym ?acr; " + 60 | "omv:name ?name . " + 61 | "}"; 62 | 63 | SimpleTest test = new SimpleTest(sparqlService, apikey); 64 | 65 | //Accept formats can be: "text/plain", "application/json", 66 | // "application/rdfxml", "text/csv", text/tab-separated-values 67 | String response = test.executeQuery(query,"text/tab-separated-values"); 68 | System.out.println(response); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /javascript/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | BioPortal SPARQL Query Example 6 | 7 | 8 | 50 | 51 | 52 |

Simple HTML table populated from a SPARQL query

53 |
Fetching ontology data...
54 | 55 | 56 | -------------------------------------------------------------------------------- /javascript/node_test.js: -------------------------------------------------------------------------------- 1 | var http = require('http'); 2 | 3 | var query_string = "PREFIX omv: \n" + 4 | "SELECT ?ont ?name ?acr " + 5 | "WHERE { " + 6 | " ?ont a omv:Ontology . " + 7 | " ?ont omv:acronym ?acr ." + 8 | " ?ont omv:name ?name . " + 9 | " }"; 10 | var apikey = "Your API KEY!"; 11 | 12 | var options = { 13 | host: 'sparql.bioontology.org', 14 | port: 8080, 15 | path: '/sparql?query=' +encodeURIComponent(query_string) + "&apikey=" + encodeURIComponent(apikey), 16 | headers: { 17 | 'Accept': 'application/json', 18 | }, 19 | method: 'GET' 20 | }; 21 | 22 | var req = http.request(options, function(res) { 23 | 24 | console.log("Got response: " + res.statusCode); 25 | console.log('HEADERS: ' + JSON.stringify(res.headers)); 26 | 27 | var data = ""; 28 | 29 | res.on('data', function (chunk) { 30 | data += chunk; 31 | }); 32 | 33 | res.on('end', function () { 34 | var json_res = JSON.parse(data); 35 | var vars = json_res.head.vars; 36 | for (i in json_res.results.bindings) { 37 | var b = json_res.results.bindings[i]; 38 | console.log("\nrow "+i+" :"); 39 | for (j in vars) { 40 | var v = vars[j]; 41 | console.log(v+"="+b[v].value); 42 | } 43 | } 44 | }); 45 | 46 | }).on('error', function(e) { 47 | console.log("Got error: " + e.message); 48 | }); 49 | req.end(); 50 | -------------------------------------------------------------------------------- /javascript/sparql.js: -------------------------------------------------------------------------------- 1 | SPARQL = function(o) { 2 | this.query = function(q) { 3 | return $.ajax({ 4 | url: o.endpoint, 5 | accepts: {json: "application/sparql-results+json"}, 6 | data: {query: q, apikey: o.apikey}, 7 | dataType: "json" 8 | }); 9 | }; 10 | }; 11 | 12 | 13 | -------------------------------------------------------------------------------- /perl/sparql.pm: -------------------------------------------------------------------------------- 1 | # Obtained from https://github.com/swh/Perl-SPARQL-client-library 2 | # No license applies. There is no warranty for this free software. 3 | # About 4 | # 5 | # This module is an implementation of the SPARQL protocol and result format 6 | # with minimal dependencies. Specifically it should be possible to run this 7 | # module using only packages provided by your distribution, making it suitable 8 | # for corporate environments. 9 | # 10 | # The dependencies are: 11 | # LWP::UserAgent 12 | # LWP::ConnCache 13 | # URI::Escape 14 | # XML::Simple 15 | # 16 | # This module will also use Keepalive HTTP Connections where possible. 17 | # 18 | # Usage 19 | # 20 | # use sparql; 21 | # 22 | # my $sparql = sparql->new(); 23 | # 24 | # my $res = $sparql->query('http://dbpedia.org/sparql', 25 | # 'SELECT * WHERE { ?s ?p ?o } LIMIT 10'); 26 | # 27 | # for my $row (@{$res}) { 28 | # for my $col (keys %{$row}) { 29 | # print('?'.$col.' = '.$row->{$col}."\n"); 30 | # } 31 | # print("\n"); 32 | # } 33 | # 34 | # Methods 35 | # 36 | # new() 37 | # 38 | # Returns a SPARQL client object. 39 | # 40 | # query(endpoint, query string) 41 | # 42 | # Takes and endpoint URI, and a query, and returns a reference to an array of 43 | # hashes, for SELECT, and a string for CONSTRUCT etc. Each element in the 44 | # array (a hash) is a solution, the keys of the hash are the variable name 45 | # (without the ? or $), and the value is a Turtle literal representing the result 46 | # value. 47 | # 48 | # update(endpoint, update string) 49 | # 50 | # Takes an endpoint URI, and a query, and returns a string of the returned message. 51 | # 52 | # put(endpoint, graph uri, mime type, RDF text) 53 | # 54 | # Takes an endpoint URI, and graph to write to, a MIME type and some RDF 55 | # data, and writes the data into the graph at the endpoint. 56 | # 57 | # Example: $sparql->put('http://host.example/data', 'http://mygraph.example/data.rdf', 58 | # 'text/turtle', "

.\n"); 59 | # 60 | # Returns whatever text the endpoint returns as a result. 61 | # 62 | # chomp(turtle literal) 63 | # 64 | # Removes any Turtle-style gubbins from around a returned value, e.g. '"foo"' -> 'foo', 65 | # '' -> 'http://example.com/'. 66 | # 67 | # print(result) 68 | # 69 | # Formats query a result object and prints it to STDOUT. 70 | package sparql; 71 | 72 | use HTTP::Request::Common; 73 | use LWP::UserAgent; 74 | use LWP::ConnCache; 75 | use URI::Escape; 76 | use XML::Simple; 77 | use strict; 78 | 79 | sub new { 80 | my $class = shift; 81 | my $ua = LWP::UserAgent->new(keep_alive => 1); 82 | $ua->timeout(600); 83 | $ua->conn_cache(LWP::ConnCache->new()); 84 | $ua->conn_cache->total_capacity(10); 85 | $ua->agent("sparql.pm/1.1"); 86 | my $self = { 87 | "_ua" => $ua 88 | }; 89 | bless $self, $class; 90 | 91 | return $self 92 | } 93 | 94 | sub query { 95 | my ($self, $uri, $tquery) = @_; 96 | 97 | if (!$uri) { 98 | warn "sparql_query() called without endpoint"; 99 | return (); 100 | } 101 | 102 | my $query = uri_escape($tquery); 103 | my $ruri; 104 | if ($uri =~ /\?/) { 105 | $ruri = $uri.'&query='.$query; 106 | } else { 107 | $ruri = $uri.'?query='.$query; 108 | } 109 | 110 | my $ret; 111 | my $response = $self->{'_ua'}->get($ruri, 'Accept' => 'text/tab-separated-values'); 112 | if ($response->is_success) { 113 | $ret = $response->decoded_content; 114 | } elsif ($response->header("Client-Aborted")) { 115 | warn "Response timeout"; 116 | 117 | return (); 118 | } else { 119 | warn $response->status_line.": ".$tquery; 120 | 121 | return (); 122 | } 123 | 124 | my $tsv = ($response->header('Content-Type') =~ m/^text\/tab-separated-values/i); 125 | my $sr = ($response->header('Content-Type') =~ m/^application\/sparql-results\+xml/i); 126 | my @retrows = (); 127 | if ($tsv) { 128 | my @rows = split("\n", $ret); 129 | my $header = shift @rows; 130 | if (!$header) { 131 | warn("no header from query"); 132 | return (); 133 | } 134 | $header =~ s/^\s+//; 135 | my @cols = grep(s/^[\?\$]//, split("\t", $header)); 136 | while (my $row = shift @rows) { 137 | if ($row =~ /^#/) { 138 | warn("SPARQL warning: $row\n"); 139 | next; 140 | } 141 | next if ($row =~ /^\s*$/); 142 | my @data = split("\t", $row); 143 | my %row = (); 144 | for my $h (@cols) { 145 | my $cell = shift @data; 146 | $row{$h} = $cell; 147 | } 148 | push(@retrows, \%row); 149 | } 150 | 151 | return \@retrows; 152 | } elsif ($sr) { 153 | my $tree = XMLin($ret, ForceArray => 1); 154 | #print Dumper($tree)."\n"; 155 | my @retrows; 156 | 157 | my $rows = $tree->{'results'}->[0]->{'result'}; 158 | for my $row (@{ $rows }) { 159 | my %row; 160 | for my $binding (keys %{$row->{'binding'}}) { 161 | #print($binding."\n"); 162 | my $val = $row->{'binding'}->{$binding}; 163 | if ($val->{'uri'}) { 164 | $row{$binding} = '<'.$val->{'uri'}->[0].'>'; 165 | } elsif ($val->{'literal'}) { 166 | $row{$binding} = '"'.$val->{'literal'}->[0].'"'; 167 | } elsif ($val->{'bnode'}) { 168 | $row{$binding} = '_:'.$val->{'literal'}->[0]; 169 | } else { 170 | $row{$binding} = '???'; 171 | } 172 | } 173 | push(@retrows, \%row); 174 | } 175 | 176 | return \@retrows; 177 | } 178 | 179 | return $ret; 180 | } 181 | 182 | sub update { 183 | my ($self, $uri, $tupdate) = @_; 184 | 185 | if (!$uri) { 186 | warn "SPARQL::update() called without endpoint"; 187 | return (); 188 | } 189 | 190 | my $ret; 191 | my %form = ( 'update' => $tupdate ); 192 | my $response = $self->{'_ua'}->post($uri, \%form); 193 | if ($response->is_success) { 194 | $ret = $response->decoded_content; 195 | } elsif ($response->header("Client-Aborted")) { 196 | warn "Response timeout"; 197 | 198 | return undef; 199 | } else { 200 | warn $response->status_line.": ".$tupdate."\n".$response->decoded_content; 201 | 202 | return undef; 203 | } 204 | 205 | return $ret; 206 | } 207 | 208 | sub put { 209 | my ($self, $uri, $doc, $mime, $text) = @_; 210 | 211 | if (!$uri) { 212 | warn "SPARQL::update() called without endpoint"; 213 | return undef; 214 | } 215 | 216 | if ($uri =~ /\?/) { 217 | $uri .= '&graph='.uri_escape($doc); 218 | } else { 219 | $uri .= '?graph='.uri_escape($doc); 220 | } 221 | my $response = $self->{'_ua'}->request(PUT $uri, 'Content-Type' => $mime, 'Content-Length' => length($text), 'Content' => $text); 222 | if ($response->is_success) { 223 | return $response->decoded_content; 224 | } elsif ($response->header("Client-Aborted")) { 225 | warn "Response timeout"; 226 | 227 | return undef; 228 | } 229 | warn $response->status_line."\n".$response->decoded_content; 230 | 231 | return undef; 232 | } 233 | 234 | sub print { 235 | my ($self, $res) = @_; 236 | 237 | for my $row (@{$res}) { 238 | my $first = 1; 239 | for my $col (keys %{$row}) { 240 | if ($first) { 241 | $first = 0; 242 | } else { 243 | print(", "); 244 | } 245 | print('?'.$col.' = '.$row->{$col}); 246 | } 247 | print("\n"); 248 | } 249 | } 250 | 251 | sub chomp { 252 | my($self, $s) = @_; 253 | 254 | if (!$s) { 255 | return ""; 256 | } 257 | 258 | if ($s =~ /^<(.*)>$/) { 259 | return $1; 260 | } elsif ($s =~ /"(.*)"/) { 261 | return $1; 262 | } elsif ($s =~ /"(.*)"^^<.*>$/) { 263 | return $1; 264 | } 265 | 266 | return $s; 267 | } 268 | 269 | 1; 270 | 271 | # vi:set expandtab sts=4 sw=4: 272 | -------------------------------------------------------------------------------- /perl/test.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | 3 | use sparql; 4 | use strict; 5 | 6 | my $endpoint = "http://sparql.bioontology.org/sparql"; 7 | my $apikey = "YOUR API KEY HERE"; 8 | 9 | my $query_string = < 11 | SELECT ?ont ?name ?acr 12 | WHERE { 13 | ?ont a omv:Ontology . 14 | ?ont omv:acronym ?acr . 15 | ?ont omv:name ?name . 16 | } 17 | END 18 | 19 | my $sparql = sparql->new(); 20 | my $res = $sparql->query($endpoint.'?apikey='.$apikey,$query_string); 21 | 22 | for my $row (@{$res}) { 23 | for my $col (keys %{$row}) { 24 | print('?'.$col.' = '.$row->{$col}."\n"); 25 | } 26 | print("\n"); 27 | } 28 | -------------------------------------------------------------------------------- /python/all_props: -------------------------------------------------------------------------------- 1 | ?p 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 541 | 542 | 543 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | 565 | 566 | 567 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | 634 | 635 | 636 | 637 | 638 | 639 | 640 | 641 | 642 | 643 | 644 | 645 | 646 | 647 | 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 | 662 | 663 | 664 | 665 | 666 | 667 | 668 | 669 | 670 | 671 | 672 | 673 | 674 | 675 | 676 | 677 | 678 | 679 | 680 | 681 | 682 | 683 | 684 | 685 | 686 | 687 | 688 | 689 | 690 | 691 | 692 | 693 | 694 | 695 | 696 | 697 | 698 | 699 | 700 | 701 | 702 | 703 | 704 | 705 | 706 | 707 | 708 | 709 | 710 | 711 | 712 | 713 | 714 | 715 | 716 | 717 | 718 | 719 | 720 | 721 | 722 | 723 | 724 | 725 | 726 | 727 | 728 | 729 | 730 | 731 | 732 | 733 | 734 | 735 | 736 | 737 | 738 | 739 | 740 | 741 | 742 | 743 | 744 | 745 | 746 | 747 | 748 | 749 | 750 | 751 | 752 | 753 | 754 | 755 | 756 | 757 | 758 | 759 | 760 | 761 | 762 | 763 | 764 | 765 | 766 | 767 | 768 | 769 | 770 | 771 | 772 | 773 | 774 | 775 | 776 | 777 | 778 | 779 | 780 | 781 | 782 | 783 | 784 | 785 | 786 | 787 | 788 | 789 | 790 | 791 | 792 | 793 | 794 | 795 | 796 | 797 | 798 | 799 | 800 | 801 | 802 | 803 | 804 | 805 | 806 | 807 | 808 | 809 | 810 | 811 | 812 | 813 | 814 | 815 | 816 | 817 | 818 | 819 | 820 | 821 | 822 | 823 | 824 | 825 | 826 | 827 | 828 | 829 | 830 | 831 | 832 | 833 | 834 | 835 | 836 | 837 | 838 | 839 | 840 | 841 | 842 | 843 | 844 | 845 | 846 | 847 | 848 | 849 | 850 | 851 | 852 | 853 | 854 | 855 | 856 | 857 | 858 | 859 | 860 | 861 | 862 | 863 | 864 | 865 | 866 | 867 | 868 | 869 | 870 | 871 | 872 | 873 | 874 | 875 | 876 | 877 | 878 | 879 | 880 | 881 | 882 | 883 | 884 | 885 | 886 | 887 | 888 | 889 | 890 | 891 | 892 | 893 | 894 | 895 | 896 | 897 | 898 | 899 | 900 | 901 | 902 | 903 | 904 | 905 | 906 | 907 | 908 | 909 | 910 | 911 | 912 | 913 | 914 | 915 | 916 | 917 | 918 | 919 | 920 | 921 | 922 | 923 | 924 | 925 | 926 | 927 | 928 | 929 | 930 | 931 | 932 | 933 | 934 | 935 | 936 | 937 | 938 | 939 | 940 | 941 | 942 | 943 | 944 | 945 | 946 | 947 | 948 | 949 | 950 | 951 | 952 | 953 | 954 | 955 | 956 | 957 | 958 | 959 | 960 | 961 | 962 | 963 | 964 | 965 | 966 | 967 | 968 | 969 | 970 | 971 | 972 | 973 | 974 | 975 | 976 | 977 | 978 | 979 | 980 | 981 | 982 | 983 | 984 | 985 | 986 | 987 | 988 | 989 | 990 | 991 | 992 | 993 | 994 | 995 | 996 | 997 | 998 | 999 | 1000 | 1001 | 1002 | 1003 | 1004 | 1005 | 1006 | 1007 | 1008 | 1009 | 1010 | 1011 | 1012 | 1013 | 1014 | 1015 | 1016 | 1017 | 1018 | 1019 | 1020 | 1021 | 1022 | 1023 | 1024 | 1025 | 1026 | 1027 | 1028 | 1029 | 1030 | 1031 | 1032 | 1033 | 1034 | 1035 | 1036 | 1037 | 1038 | 1039 | 1040 | 1041 | 1042 | 1043 | 1044 | 1045 | 1046 | 1047 | 1048 | 1049 | 1050 | 1051 | 1052 | 1053 | 1054 | 1055 | 1056 | 1057 | 1058 | 1059 | 1060 | 1061 | 1062 | 1063 | 1064 | 1065 | 1066 | 1067 | 1068 | 1069 | 1070 | 1071 | 1072 | 1073 | 1074 | 1075 | 1076 | 1077 | 1078 | 1079 | 1080 | 1081 | 1082 | 1083 | 1084 | 1085 | 1086 | 1087 | 1088 | 1089 | 1090 | 1091 | 1092 | 1093 | 1094 | 1095 | 1096 | 1097 | 1098 | 1099 | 1100 | 1101 | 1102 | 1103 | 1104 | 1105 | 1106 | 1107 | 1108 | 1109 | 1110 | 1111 | 1112 | 1113 | 1114 | 1115 | 1116 | 1117 | 1118 | 1119 | 1120 | 1121 | 1122 | 1123 | 1124 | 1125 | 1126 | 1127 | 1128 | 1129 | 1130 | 1131 | 1132 | 1133 | 1134 | 1135 | 1136 | 1137 | 1138 | 1139 | 1140 | 1141 | 1142 | 1143 | 1144 | 1145 | 1146 | 1147 | 1148 | 1149 | 1150 | 1151 | 1152 | 1153 | 1154 | 1155 | 1156 | 1157 | 1158 | 1159 | 1160 | 1161 | 1162 | 1163 | 1164 | 1165 | 1166 | 1167 | 1168 | 1169 | 1170 | 1171 | 1172 | 1173 | 1174 | 1175 | 1176 | 1177 | 1178 | 1179 | 1180 | 1181 | 1182 | 1183 | 1184 | 1185 | 1186 | 1187 | 1188 | 1189 | 1190 | 1191 | 1192 | 1193 | 1194 | 1195 | 1196 | 1197 | 1198 | 1199 | 1200 | 1201 | 1202 | 1203 | 1204 | 1205 | 1206 | 1207 | 1208 | 1209 | 1210 | 1211 | 1212 | 1213 | 1214 | 1215 | 1216 | 1217 | 1218 | 1219 | 1220 | 1221 | 1222 | 1223 | 1224 | 1225 | 1226 | 1227 | 1228 | 1229 | 1230 | 1231 | 1232 | 1233 | 1234 | 1235 | 1236 | 1237 | 1238 | 1239 | 1240 | 1241 | 1242 | 1243 | 1244 | 1245 | 1246 | 1247 | 1248 | 1249 | 1250 | 1251 | 1252 | 1253 | 1254 | 1255 | 1256 | 1257 | 1258 | 1259 | 1260 | 1261 | 1262 | 1263 | 1264 | 1265 | 1266 | 1267 | 1268 | 1269 | 1270 | 1271 | 1272 | 1273 | 1274 | 1275 | 1276 | 1277 | 1278 | 1279 | 1280 | 1281 | 1282 | 1283 | 1284 | 1285 | 1286 | 1287 | 1288 | 1289 | 1290 | 1291 | 1292 | 1293 | 1294 | 1295 | 1296 | 1297 | 1298 | 1299 | 1300 | 1301 | 1302 | 1303 | 1304 | 1305 | 1306 | 1307 | 1308 | 1309 | 1310 | 1311 | 1312 | 1313 | 1314 | 1315 | 1316 | 1317 | 1318 | 1319 | 1320 | 1321 | 1322 | 1323 | 1324 | 1325 | 1326 | 1327 | 1328 | 1329 | 1330 | 1331 | 1332 | 1333 | 1334 | 1335 | 1336 | 1337 | 1338 | 1339 | 1340 | 1341 | 1342 | 1343 | 1344 | 1345 | 1346 | 1347 | 1348 | 1349 | 1350 | 1351 | 1352 | 1353 | 1354 | 1355 | 1356 | 1357 | 1358 | 1359 | 1360 | 1361 | 1362 | 1363 | 1364 | 1365 | 1366 | 1367 | 1368 | 1369 | 1370 | 1371 | 1372 | 1373 | 1374 | 1375 | 1376 | 1377 | 1378 | 1379 | 1380 | 1381 | 1382 | 1383 | 1384 | 1385 | 1386 | 1387 | 1388 | 1389 | 1390 | 1391 | 1392 | 1393 | 1394 | 1395 | 1396 | 1397 | 1398 | 1399 | 1400 | 1401 | 1402 | 1403 | 1404 | 1405 | 1406 | 1407 | 1408 | 1409 | 1410 | 1411 | 1412 | 1413 | 1414 | 1415 | 1416 | 1417 | 1418 | 1419 | 1420 | 1421 | 1422 | 1423 | 1424 | 1425 | 1426 | 1427 | 1428 | 1429 | 1430 | 1431 | 1432 | 1433 | 1434 | 1435 | 1436 | 1437 | 1438 | 1439 | 1440 | 1441 | 1442 | 1443 | 1444 | 1445 | 1446 | 1447 | 1448 | 1449 | 1450 | 1451 | 1452 | 1453 | 1454 | 1455 | 1456 | 1457 | 1458 | 1459 | 1460 | 1461 | 1462 | 1463 | 1464 | 1465 | 1466 | 1467 | 1468 | 1469 | 1470 | 1471 | 1472 | 1473 | 1474 | 1475 | 1476 | 1477 | 1478 | 1479 | 1480 | 1481 | 1482 | 1483 | 1484 | 1485 | 1486 | 1487 | 1488 | 1489 | 1490 | 1491 | 1492 | 1493 | 1494 | 1495 | 1496 | 1497 | 1498 | 1499 | 1500 | 1501 | 1502 | 1503 | 1504 | 1505 | 1506 | 1507 | 1508 | 1509 | 1510 | 1511 | 1512 | 1513 | 1514 | 1515 | 1516 | 1517 | 1518 | 1519 | 1520 | 1521 | 1522 | 1523 | 1524 | 1525 | 1526 | 1527 | 1528 | 1529 | 1530 | 1531 | 1532 | 1533 | 1534 | 1535 | 1536 | 1537 | 1538 | 1539 | 1540 | 1541 | 1542 | 1543 | 1544 | 1545 | 1546 | 1547 | 1548 | 1549 | 1550 | 1551 | 1552 | 1553 | 1554 | 1555 | 1556 | 1557 | 1558 | 1559 | 1560 | 1561 | 1562 | 1563 | 1564 | 1565 | 1566 | 1567 | 1568 | 1569 | 1570 | 1571 | 1572 | 1573 | 1574 | 1575 | 1576 | 1577 | 1578 | 1579 | 1580 | 1581 | 1582 | 1583 | 1584 | 1585 | 1586 | 1587 | 1588 | 1589 | 1590 | 1591 | 1592 | 1593 | 1594 | 1595 | 1596 | 1597 | 1598 | 1599 | 1600 | 1601 | 1602 | 1603 | 1604 | 1605 | 1606 | 1607 | 1608 | 1609 | 1610 | 1611 | 1612 | 1613 | 1614 | 1615 | 1616 | 1617 | 1618 | 1619 | 1620 | 1621 | 1622 | 1623 | 1624 | 1625 | 1626 | 1627 | 1628 | 1629 | 1630 | 1631 | 1632 | 1633 | 1634 | 1635 | 1636 | 1637 | 1638 | 1639 | 1640 | 1641 | 1642 | 1643 | 1644 | 1645 | 1646 | 1647 | 1648 | 1649 | 1650 | 1651 | 1652 | 1653 | 1654 | 1655 | 1656 | 1657 | 1658 | 1659 | 1660 | 1661 | 1662 | 1663 | 1664 | 1665 | 1666 | 1667 | 1668 | 1669 | 1670 | 1671 | 1672 | 1673 | 1674 | 1675 | 1676 | 1677 | 1678 | 1679 | 1680 | 1681 | 1682 | 1683 | 1684 | 1685 | 1686 | 1687 | 1688 | 1689 | 1690 | 1691 | 1692 | 1693 | 1694 | 1695 | 1696 | 1697 | 1698 | 1699 | 1700 | 1701 | 1702 | 1703 | 1704 | 1705 | 1706 | 1707 | 1708 | 1709 | 1710 | 1711 | 1712 | 1713 | 1714 | 1715 | 1716 | 1717 | 1718 | 1719 | 1720 | 1721 | 1722 | 1723 | 1724 | -------------------------------------------------------------------------------- /python/complex.py: -------------------------------------------------------------------------------- 1 | """ Simple Python script to query "http://alphasparql.bioontology.org/sparql/" 2 | No extra libraries required. 3 | """ 4 | 5 | import json 6 | import urllib2 7 | import urllib 8 | import traceback 9 | import sys 10 | 11 | def query(q,apikey,epr,f='application/json'): 12 | """Function that uses urllib/urllib2 to issue a SPARQL query. 13 | By default it requests json as data format for the SPARQL resultset""" 14 | 15 | try: 16 | params = {'query': q, 'apikey': apikey} 17 | params = urllib.urlencode(params) 18 | opener = urllib2.build_opener(urllib2.HTTPHandler) 19 | request = urllib2.Request(epr+'?'+params) 20 | request.add_header('Accept', f) 21 | request.get_method = lambda: 'GET' 22 | url = opener.open(request) 23 | return url.read() 24 | except Exception, e: 25 | traceback.print_exc(file=sys.stdout) 26 | raise e 27 | 28 | if __name__ == "__main__": 29 | #Eventually alphasparql will be moved to http://sparql.bioontology.org/sparql/ 30 | sparql_service = "http://alphasparql.bioontology.org/sparql/" 31 | 32 | #To get your API key register at http://bioportal.bioontology.org/accounts/new 33 | api_key = "YOUR API KEY" 34 | 35 | 36 | query_string = """SELECT DISTINCT ?p WHERE { ?s ?p ?o }""" 37 | 38 | #defaults 39 | #PREFERED NAME PROPERTY 40 | #default: http://www.w3.org/2000/01/rdf-schema#label 41 | #SYNONYM PROPERTY 42 | #default: http://www.w3.org/2004/02/skos/core#altLabel 43 | #DEFINITION PROPERTY 44 | #default: http://www.w3.org/2004/02/skos/core#definition 45 | #AUTHOR PROPERTY 46 | #default: http://purl.org/dc/elements/1.1/creator 47 | 48 | synonymProperty = "synonymProperty" 49 | documentationProperty = "documentationProperty" 50 | authorProperty = "authorProperty" 51 | preferredNameProperty = "preferredNameProperty" 52 | 53 | query_string_p = """SELECT DISTINCT ?p WHERE { 54 | GRAPH { 55 | ?s ?p ?o 56 | } 57 | } 58 | """ 59 | query_string = """SELECT DISTINCT * WHERE { 60 | GRAPH { 61 | ?s ?o 62 | } 63 | } 64 | """ 65 | query_string = """SELECT DISTINCT * WHERE { 66 | ?s ?p . 67 | } 68 | """ 69 | 70 | query_string_d = "DESCRIBE " 71 | 72 | query_string = """ 73 | PREFIX omv: 74 | PREFIX owl: 75 | PREFIX bio: 76 | 77 | SELECT * 78 | WHERE { 79 | ?ont a omv:Ontology . 80 | ?ont bio:isVersionOfVirtualOntology ?vrt . 81 | GRAPH ?vrt { 82 | ?s a owl:Class . 83 | } 84 | } LIMIT 10 85 | """ 86 | json_string = query(query_string, api_key, sparql_service,f="text/plain") 87 | print json_string 88 | #resultset=json.loads(json_string) 89 | #print json.dumps(resultset,indent=1) 90 | -------------------------------------------------------------------------------- /python/sparql1.py: -------------------------------------------------------------------------------- 1 | """ Simple Python script to query "http://sparql.bioontology.org/sparql/" 2 | No extra libraries required. 3 | """ 4 | 5 | import json 6 | import urllib2 7 | import urllib 8 | import traceback 9 | import sys 10 | 11 | def query(q,apikey,epr,f='application/json'): 12 | """Function that uses urllib/urllib2 to issue a SPARQL query. 13 | By default it requests json as data format for the SPARQL resultset""" 14 | 15 | try: 16 | params = {'query': q, 'apikey': apikey} 17 | params = urllib.urlencode(params) 18 | opener = urllib2.build_opener(urllib2.HTTPHandler) 19 | request = urllib2.Request(epr+'?'+params) 20 | request.add_header('Accept', f) 21 | request.get_method = lambda: 'GET' 22 | url = opener.open(request) 23 | return url.read() 24 | except Exception, e: 25 | traceback.print_exc(file=sys.stdout) 26 | raise e 27 | 28 | if __name__ == "__main__": 29 | sparql_service = "http://sparql.bioontology.org/sparql/" 30 | 31 | #To get your API key register at http://bioportal.bioontology.org/accounts/new 32 | api_key = "YOUR API KEY HERE" 33 | 34 | #Some sample query. 35 | query_string = """ 36 | PREFIX omv: 37 | 38 | SELECT ?ont ?name ?acr 39 | WHERE { ?ont a omv:Ontology; 40 | omv:acronym ?acr; 41 | omv:name ?name . 42 | } 43 | """ 44 | json_string = query(query_string, api_key, sparql_service) 45 | resultset=json.loads(json_string) 46 | 47 | #Printing the json object. 48 | print json.dumps(resultset,indent=1) 49 | -------------------------------------------------------------------------------- /python/sparql2.py: -------------------------------------------------------------------------------- 1 | """ Simple Python script to query "http://sparql.bioontology.org/sparql/" 2 | This script uses the SPARQLWrapper Python lib. Download and install from: 3 | http://sparql-wrapper.sourceforge.net/ 4 | """ 5 | 6 | from SPARQLWrapper import SPARQLWrapper, JSON, XML, N3, RDF 7 | import pdb 8 | 9 | if __name__ == "__main__": 10 | sparql_service = "http://sparql.bioontology.org/sparql/" 11 | 12 | #To get your API key register at http://bioportal.bioontology.org/accounts/new 13 | api_key = "YOUR API KEY" 14 | 15 | #Some sample query. 16 | query_string = """ 17 | PREFIX omv: 18 | 19 | SELECT ?ont ?name ?acr 20 | WHERE { ?ont a omv:Ontology; 21 | omv:acronym ?acr; 22 | omv:name ?name . 23 | } 24 | """ 25 | sparql = SPARQLWrapper(sparql_service) 26 | sparql.addCustomParameter("apikey",api_key) 27 | sparql.setQuery(query_string) 28 | sparql.setReturnFormat(JSON) 29 | results = sparql.query().convert() 30 | for result in results["results"]["bindings"]: 31 | print result["ont"]["value"], result["name"]["value"], result["acr"]["value"] 32 | -------------------------------------------------------------------------------- /ruby/sparql1.rb: -------------------------------------------------------------------------------- 1 | # Ruby querying of the BioPortal triplestore using standard Ruby libraries and the JSON rubygem 2 | 3 | require 'rubygems' 4 | require 'json' 5 | require 'open-uri' 6 | require 'cgi' 7 | 8 | apikey = "" 9 | endpoint = "http://sparql.bioontology.org/sparql/" 10 | query = <<-EOS 11 | PREFIX omv: 12 | 13 | SELECT ?ont ?name ?acr 14 | WHERE { ?ont a omv:Ontology; 15 | omv:acronym ?acr; 16 | omv:name ?name . 17 | } LIMIT 100 18 | EOS 19 | 20 | # Returns a JSON object with the results of a SPARQL query 21 | def query(query, apikey, endpoint, accept = "application/json") 22 | json = open("#{endpoint}?query=#{CGI.escape(query)}&apikey=#{apikey}", "Accept" => accept).read 23 | JSON.parse(json) 24 | 25 | end 26 | 27 | puts JSON.pretty_generate(query(query, apikey, endpoint)) 28 | -------------------------------------------------------------------------------- /ruby/sparql2.rb: -------------------------------------------------------------------------------- 1 | # Ruby querying of the BioPortal triplestore using standard the RDF.rb family of libraries 2 | # Documentation for working with the RDF.rb SPARQL client can be found here: https://github.com/ruby-rdf/sparql-client 3 | 4 | require 'rubygems' 5 | require 'sparql/client' 6 | 7 | apikey = "" 8 | 9 | endpoint = SPARQL::Client.new("http://sparql.bioontology.org/sparql/?apikey=#{apikey}", {method: :get}) 10 | 11 | query = <<-EOS 12 | PREFIX omv: 13 | 14 | SELECT ?ont ?name ?acr 15 | WHERE { ?ont a omv:Ontology; 16 | omv:acronym ?acr; 17 | omv:name ?name . 18 | } LIMIT 100 19 | EOS 20 | 21 | results = endpoint.query(query) 22 | 23 | results.each do |statement| 24 | puts statement.inspect 25 | end 26 | --------------------------------------------------------------------------------