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
10/28/2014 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, j['results']['bindings']
In [2]:
# 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 [3]:
j, jrb2 = sparqlqry_jsonresponse(sparql, queryString)
In [4]:
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 [5]:
jrb2[1]
Out[5]:
In [6]:
# List of IOOS Parameter terms selected in the previous query
ioosparam_sel_lst = [jrbv['value']['value'].split("/")[-1] for jrbv in jrb2]
In [11]:
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)
j, 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)
ioos_units = list([ x["unit"]["value"] for x in jrb3 ])
In [12]:
# 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, ioos_units),
columns=("IOOS Parameter Term", "SKOS Match", "CF Standard Name", "IOOS Sample Units"))
Out[12]: