Skip to content

Instantly share code, notes, and snippets.

@bwalsh
Created June 7, 2016 20:37
Show Gist options
  • Save bwalsh/56ec6e07a73fc41a9752ad4a5ec81c45 to your computer and use it in GitHub Desktop.
Save bwalsh/56ec6e07a73fc41a9752ad4a5ec81c45 to your computer and use it in GitHub Desktop.
python snippet to test sparql queries (use in REPL)
# python snippet to test sparql queries
# set up the graph
import rdflib
_rdfGraph = rdflib.ConjunctiveGraph()
_rdfGraph.parse('tests/data/datasets/dataset1/phenotypes/cgd/cgd-test.ttl', format='n3')
# substitute your query here
query = """
PREFIX OBAN: <http://purl.org/oban/>
PREFIX OBO: <http://purl.obolibrary.org/obo/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT
?association
?environment
?environment_label
?feature
?feature_label
?phenotype
?phenotype_label
(GROUP_CONCAT(?source; separator="|") AS ?sources)
?evidence_type
WHERE {
?association a OBAN:association .
?association OBO:RO_0002558 ?evidence_type .
?association OBO:RO_has_environment ?environment .
OPTIONAL { ?association dc:source ?source } .
?association OBAN:association_has_subject ?feature .
?association OBAN:association_has_object ?phenotype .
?environment rdfs:label ?environment_label .
?phenotype rdfs:label ?phenotype_label .
?feature rdfs:label ?feature_label .
FILTER (?phenotype = <http://ohsu.edu/cgd/30ebfd1a>)
}
GROUP BY ?association
ORDER BY ?association
"""
# query and print
associations = _rdfGraph.query(query)
for row in associations.bindings:
print(row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment