Read Turtle file and query the graph using SPARQL

Question:

I am in a learning phase of SPARQL so excuse if the question sounds basic. I am exploring SPARQLWrapper to connect to a SPARQL endpoint to fetch some data. Below is the query.

sparql = SPARQLWrapper("http://patho.phenomebrowser.net/sparql/") #endpoint link

sparql.setQuery("""

PREFIX SIO: <http://semanticscience.org/resource/SIO_>
PREFIX RO: <http://purl.obolibrary.org/obo/RO_>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT distinct ?PA_ID ?pathogen  ?D_ID ?disease ?disease_pathogen_evidence_code  ?P_ID ?phenotype ?disease_phenotype_evidence_code  
FROM <http://patho.phenomebrowser.net>
WHERE
  {
    ?D_ID SIO:000255 ?o1 .
    ?o1 RO:0002558 ?EC1_ID . 
    ?o1 RO:0002200 ?P_ID .
    ?D_ID SIO:000255 ?o .
    ?o RO:0002558 ?EC2_ID .
    ?o RO:0002556 ?PA_ID .
    ?PA_ID rdfs:label ?pathogen.
    ?P_ID rdfs:label ?phenotype .
    ?D_ID rdfs:label ?disease .
    ?EC1_ID rdfs:label ?disease_phenotype_evidence_code .
    ?EC2_ID rdfs:label ?disease_pathogen_evidence_code .

  } limit 50 
""")

sparql.setReturnFormat(JSON)
results = sparql.query().convert()

for result in results["results"]["bindings"]:
    print(result["pathogen"]["value"])

The query above work fine. Is there a way to load or read a Turtle file and then use SPARQLWrapper to query the graph?

Asked By: rshar

||

Answers:

To be able to query a graph with SPARQLWrapper, you need to load this graph in a SPARQL endpoint. Such an endpoint could be hosted locally, e.g.:

sparql = SPARQLWrapper("http://localhost/sparql")

The documentation lists how certain endpoints can be used with SPARQLWrapper: SPARQL Endpoint Implementations


Instead of using SPARQLWrapper, you could consider using RDFLib (which gets used by SPARQLWrapper, and comes from the same developers), which allows you to query endpoints (using SERVICE) as well as files (no need to load them in an endpoint).