Sample SPARQL 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

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


/usr/anaconda/anaconda/envs/uwapl_em_1/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 terms to total_coliform, within IOOS Parameter Vocabulary


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]:
[u'head', u'results']

In [4]:
j['head'], j['results'].keys()


Out[4]:
({u'vars': [u'p', u'definition', u'unit', u'property', u'value']},
 [u'bindings'])

In [5]:
jrb = j['results']['bindings']
type(jrb), len(jrb)


Out[5]:
(list, 4)

In [6]:
jrb[-1]


Out[6]:
{u'definition': {u'type': u'literal',
  u'value': u'Coliforms are bacteria that are naturally present in the environment and used as an indicator that other, potentially harmful, bacteria may be present. Most Probable Number (MPN) per unit volume.'},
 u'p': {u'type': u'uri',
  u'value': u'http://mmisw.org/ont/ioos/parameter/total_coliform'},
 u'property': {u'type': u'uri',
  u'value': u'http://www.w3.org/2004/02/skos/core#narrowMatch'},
 u'unit': {u'type': u'literal', u'value': u'MPN ml-1'},
 u'value': {u'type': u'uri',
  u'value': u'http://mmisw.org/ont/ioos/parameter/fecal_coliform'}}

In [7]:
jrb[-1]['value']['value']


Out[7]:
u'http://mmisw.org/ont/ioos/parameter/fecal_coliform'

In [8]:
print "parameter (broad) term: %s" % jrb[-1]['p']['value']
print "narrower ('children') terms:"
for jrbv in jrb:
    print "    %s" % jrbv['value']['value']


parameter (broad) term: http://mmisw.org/ont/ioos/parameter/total_coliform
narrower ('children') terms:
    http://mmisw.org/ont/ioos/parameter/e_coli
    http://mmisw.org/ont/ioos/parameter/enterococcus
    http://mmisw.org/ont/ioos/parameter/clostridium_perfringens
    http://mmisw.org/ont/ioos/parameter/fecal_coliform

In [9]:
jrb[:2]


Out[9]:
[{u'definition': {u'type': u'literal',
   u'value': u'Coliforms are bacteria that are naturally present in the environment and used as an indicator that other, potentially harmful, bacteria may be present. Most Probable Number (MPN) per unit volume.'},
  u'p': {u'type': u'uri',
   u'value': u'http://mmisw.org/ont/ioos/parameter/total_coliform'},
  u'property': {u'type': u'uri',
   u'value': u'http://www.w3.org/2004/02/skos/core#narrowMatch'},
  u'unit': {u'type': u'literal', u'value': u'MPN ml-1'},
  u'value': {u'type': u'uri',
   u'value': u'http://mmisw.org/ont/ioos/parameter/e_coli'}},
 {u'definition': {u'type': u'literal',
   u'value': u'Coliforms are bacteria that are naturally present in the environment and used as an indicator that other, potentially harmful, bacteria may be present. Most Probable Number (MPN) per unit volume.'},
  u'p': {u'type': u'uri',
   u'value': u'http://mmisw.org/ont/ioos/parameter/total_coliform'},
  u'property': {u'type': u'uri',
   u'value': u'http://www.w3.org/2004/02/skos/core#narrowMatch'},
  u'unit': {u'type': u'literal', u'value': u'MPN ml-1'},
  u'value': {u'type': u'uri',
   u'value': u'http://mmisw.org/ont/ioos/parameter/enterococcus'}}]

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


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


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

3. 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 [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]:
IOOS Parameter Term SKOS Match CF Standard Name
0 ammonium narrowMatch mole_concentration_of_ammonium_in_sea_water
1 dissolved_inorganic_carbon closeMatch mole_concentration_of_dissolved_inorganic_carb...
2 dissolved_organic_carbon closeMatch mole_concentration_of_dissolved_organic_carbon...
3 dissolved_oxygen exactMatch mass_concentration_of_oxygen_in_sea_water
4 dissolved_oxygen_saturation exactMatch fractional_saturation_of_oxygen_in_sea_water
5 phosphate narrowMatch mole_concentration_of_phosphate_in_sea_water
6 phosphate narrowMatch mole_ratio_of_nitrate_to_phosphate_in_sea_water
7 phosphate narrowMatch moles_of_phosphate_per_unit_mass_in_sea_water
8 phosphate narrowMatch mass_concentration_of_phosphate_in_sea_water
9 total_nitrogen narrowMatch mass_concentration_of_inorganic_nitrogen_in_se...
10 total_phosphorus narrowMatch mole_concentration_of_particulate_organic_matt...

11 rows × 3 columns