Sample SPARQL ontological queries against MMI IOOS Vocabularies

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']


/usr/anaconda/anaconda/envs/uwapl_em_3/lib/python2.7/site-packages/SPARQLWrapper/Wrapper.py:88: RuntimeWarning: JSON-LD disabled because no suitable support has been found
  warnings.warn("JSON-LD disabled because no suitable support has been found", RuntimeWarning)

1. Narrow Match of IOOS Parameter Vocabulary terms from IOOS Core Variable dissolved_nutrients term


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']


parameter (broad) term: dissolved_nutrients
9 narrower ('children') terms:
    http://mmisw.org/ont/ioos/parameter/ammonium
    http://mmisw.org/ont/ioos/parameter/dissolved_organic_carbon
    http://mmisw.org/ont/ioos/parameter/total_phosphorus
    http://mmisw.org/ont/ioos/parameter/dissolved_oxygen_saturation
    http://mmisw.org/ont/ioos/parameter/ammonia
    http://mmisw.org/ont/ioos/parameter/total_nitrogen
    http://mmisw.org/ont/ioos/parameter/phosphate
    http://mmisw.org/ont/ioos/parameter/dissolved_inorganic_carbon
    http://mmisw.org/ont/ioos/parameter/dissolved_oxygen

In [5]:
jrb2[1]


Out[5]:
{u'definition': {u'type': u'literal', u'value': u'Dissolved Nutrients'},
 u'name': {u'type': u'literal', u'value': u'dissolved_nutrients'},
 u'p': {u'type': u'uri',
  u'value': u'http://mmisw.org/ont/ioos/core_variable/dissolved_nutrients'},
 u'property': {u'type': u'uri',
  u'value': u'http://www.w3.org/2004/02/skos/core#narrowMatch'},
 u'value': {u'type': u'uri',
  u'value': u'http://mmisw.org/ont/ioos/parameter/dissolved_organic_carbon'}}

2. Find CF standard name matches for the IOOS Parameter terms found above as matching the IOOS Core Variable "dissolved_nutrients"

This match will be fairly generous, as it includes SKOS narrowMatch, closeMatch and exactMatch relationships.


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]:
IOOS Parameter Term SKOS Match CF Standard Name IOOS Sample Units
0 ammonium narrowMatch mole_concentration_of_ammonium_in_sea_water ug L-1 as N
1 dissolved_inorganic_carbon closeMatch mole_concentration_of_dissolved_inorganic_carb... umol kg-1
2 dissolved_organic_carbon closeMatch mole_concentration_of_dissolved_organic_carbon... umol l-1
3 dissolved_oxygen exactMatch mass_concentration_of_oxygen_in_sea_water mg L-1
4 dissolved_oxygen_saturation exactMatch fractional_saturation_of_oxygen_in_sea_water %
5 phosphate narrowMatch mole_concentration_of_phosphate_in_sea_water ug L-1 as P
6 phosphate narrowMatch mole_ratio_of_nitrate_to_phosphate_in_sea_water ug L-1 as P
7 phosphate narrowMatch moles_of_phosphate_per_unit_mass_in_sea_water ug L-1 as P
8 phosphate narrowMatch mass_concentration_of_phosphate_in_sea_water ug L-1 as P
9 total_nitrogen narrowMatch mass_concentration_of_inorganic_nitrogen_in_se... ug L-1 as N
10 total_phosphorus narrowMatch mole_concentration_of_particulate_organic_matt... ug L-1 as P

11 rows × 4 columns