Taken from Sara Haines examples at http://code.google.com/p/ioostech/wiki/VocabularySearchTesting and http://nbviewer.ipython.org/urls/raw.github.com/nccoos/ioos-vocabulary-search/master/examples/example_sparql_wrapper.ipynb
example_sparql_wrapper. Send SPARQL query to SPARQL endpoint, store and output result using SPARQLWrapper. Modified from ORA "Learning SPARQL" by Bob DuCharme -- ex361.py
See also Sara Haines' SPARQL examples page, with links and dynamic SPARQL query forms: http://www.unc.edu/~haines/orrioos.html. See also http://mmisw.org/ont/sparql.html
8/9/2014. Emilio Mayorga. Added the last section, selecting CF standard names from a subset of IOOS Parameter terms. 5/8/2014. Emilio Mayorga. Extended the examples, including the use of narrowMatch.
In [1]:
import pandas as pd
from SPARQLWrapper import SPARQLWrapper, JSON
sparql = SPARQLWrapper("http://mmisw.org/sparql")
def sparqlqry_jsonresponse(sparql, queryString):
""" Convenience function
"""
sparql.setQuery(queryString)
sparql.setReturnFormat(JSON)
j = sparql.query().convert()
return j['results']['bindings']
In [2]:
queryString = """
PREFIX ioos: <http://mmisw.org/ont/ioos/parameter/>
SELECT DISTINCT ?p ?definition ?unit ?property ?value
WHERE {?p a ioos:Parameter .
?p ?property ?value .
?p ioos:Term ?term .
?p ioos:Definition ?definition .
?p ioos:Units ?unit .
FILTER (regex(str(?property), "narrowMatch", "i")
&& regex(str(?value), "ioos", "i") && regex(str(?p), "total_coliform", "i") )
}
ORDER BY ?p
"""
# FILTER (regex(str(?property), "narrowMatch", "i") && regex(str(?value), "ioos", "i") )
# FILTER (regex(str(?property), "(exactMatch|closeMatch)", "i") && regex(str(?value), "cf", "i") )
In [3]:
sparql.setQuery(queryString)
sparql.setReturnFormat(JSON)
j = sparql.query().convert()
j.keys()
Out[3]:
In [4]:
j['head'], j['results'].keys()
Out[4]:
In [5]:
jrb = j['results']['bindings']
type(jrb), len(jrb)
Out[5]:
In [6]:
jrb[-1]
Out[6]:
In [7]:
jrb[-1]['value']['value']
Out[7]:
In [8]:
print "parameter (broad) term: %s" % jrb[-1]['p']['value']
print "narrower ('children') terms:"
for jrbv in jrb:
print " %s" % jrbv['value']['value']
In [9]:
jrb[:2]
Out[9]:
In [10]:
# core_variable attributes: name & definition
queryString = """
PREFIX iooscore: <http://mmisw.org/ont/ioos/core_variable/>
SELECT DISTINCT ?p ?name ?definition ?property ?value
WHERE {?p a iooscore:Core_Variable .
?p iooscore:name ?name .
?p iooscore:definition ?definition .
?p ?property ?value .
FILTER (regex(str(?property), "narrowMatch", "i")
&& regex(str(?value), "ioos/parameter", "i")
&& regex(str(?name), "dissolved_nutrients", "i") )
}
ORDER BY ?p
"""
In [11]:
jrb2 = sparqlqry_jsonresponse(sparql, queryString)
In [12]:
print "parameter (broad) term: %s" % jrb2[-1]['name']['value']
print "%d narrower ('children') terms:" % len(jrb2)
for jrbv in jrb2:
print " %s" % jrbv['value']['value']
In [13]:
# List of IOOS Parameter terms selected in the previous query
ioosparam_sel_lst = [jrbv['value']['value'].split("/")[-1] for jrbv in jrb2]
In [14]:
queryString = """
PREFIX ioos: <http://mmisw.org/ont/ioos/parameter/>
SELECT DISTINCT ?term ?parameter ?definition ?unit ?property ?value
WHERE {?parameter a ioos:Parameter .
?parameter ?property ?value .
?parameter ioos:Term ?term .
?parameter ioos:Definition ?definition .
?parameter ioos:Units ?unit .
FILTER (regex(str(?property), "(narrowMatch|closeMatch|exactMatch)", "i")
&& regex(str(?value), "cf", "i")
&& regex(str(?term), "(%s)", "i") )
}
ORDER BY ?term ?parameter
""" % '|'.join(ioosparam_sel_lst)
jrb3 = sparqlqry_jsonresponse(sparql, queryString)
ioosparam_terms = list([ x["term"]["value"] for x in jrb3 ])
skos_match = list([ x["property"]["value"].split('#')[-1] for x in jrb3 ])
cf_standard_uris = list([ x["value"]["value"] for x in jrb3 ])
cf_standard_names = map(lambda x: x.split("/")[-1], cf_standard_uris)
In [15]:
# Note: cf_standard_uris can easily be added to this DataFrame, but I've skipped them for clarity
pd.DataFrame.from_records(zip(ioosparam_terms, skos_match, cf_standard_names),
columns=("IOOS Parameter Term", "SKOS Match", "CF Standard Name"))
Out[15]: