Skip to content

Commit

Permalink
Merge pull request #216 from Knowledge-Graph-Hub/add_example_sparql_q…
Browse files Browse the repository at this point in the history
…ueries

Refactor query command to execute SPARQL queries described in YAML files
  • Loading branch information
justaddcoffee committed Jun 12, 2020
2 parents 9cab8b4 + 0937f51 commit 86dbb34
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 404 deletions.
40 changes: 31 additions & 9 deletions kg_covid_19/query.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
import logging

from kg_covid_19.query_utils.target_candidates.target_candidates \
import TargetCandidates
import yaml
from SPARQLWrapper import SPARQLWrapper, JSON, XML, TURTLE, N3, RDF, RDFXML, CSV, TSV # type: ignore

QUERIES = {
'TargetCandidates': TargetCandidates
}

def run_query(query: str, endpoint: str, return_format=JSON) -> dict:
sparql = SPARQLWrapper(endpoint)
sparql.setQuery(query)
sparql.setReturnFormat(return_format)
results = sparql.query().convert()

def run_query(query: str, input_dir: str, output_dir: str) -> None:
logging.info(f"Running query {query}")
t = QUERIES[query](input_dir, output_dir)
t.run()
return results


def parse_query_yaml(yaml_file) -> dict:
return yaml.load(open(yaml_file))


def result_dict_to_tsv(result_dict: dict, outfile: str) -> None:
with open(outfile, 'wt') as f:
# header
f.write("\t".join(result_dict['head']['vars']) + "\n")
for row in result_dict['results']['bindings']:
row_items = []
for col in result_dict['head']['vars']:
try:
row_items.append(row[col]['value'])
except KeyError:
logging.error('Problem retrieving result for col %s in row %s' %
(col, "\t".join(row)))
row_items.append('ERROR')
try:
f.write("\t".join(row_items) + "\n")
except:
pass
Empty file.
285 changes: 0 additions & 285 deletions kg_covid_19/query_utils/target_candidates/target_candidates.py

This file was deleted.

11 changes: 11 additions & 0 deletions queries/sparql/query-01-bl-cat-counts.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
title:
"Get counts for biolink categories"
description:
"Get counts for biolink categories"
endpoint:
"http://kg-hub-rdf.berkeleybop.io/blazegraph/sparql"
query: >
SELECT (COUNT(?v2) AS ?v1) ?v0
WHERE {
?v2 <https://w3id.org/biolink/vocab/category> ?v0
} GROUP BY ?v0
17 changes: 17 additions & 0 deletions queries/sparql/query-02-sars-cov-2-prot.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
title:
"Get SARS-CoV-2 proteins"
description:
"Get info about SARS-CoV-2 proteins in the graph"
endpoint:
"http://kg-hub-rdf.berkeleybop.io/blazegraph/sparql"
query: >
prefix owl: <http://www.w3.org/2002/07/owl#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix bl: <https://w3id.org/biolink/vocab/>
SELECT ?protein ?p ?v WHERE
{
?protein bl:category bl:Protein; <https://www.example.org/UNKNOWN/ncbi_taxid> ?ncbitaxonid;
?p ?v .
FILTER(?ncbitaxonid="2697049")
}
25 changes: 25 additions & 0 deletions queries/sparql/query-03-sars-cov-2-interactors.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
title:
"Get human proteins that interact with SARS-CoV-2 proteins"
description:
"Get human proteins that interact with SARS-CoV-2 proteins"
endpoint:
"http://kg-hub-rdf.berkeleybop.io/blazegraph/sparql"
query: >
prefix owl: <http://www.w3.org/2002/07/owl#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix bl: <https://w3id.org/biolink/vocab/>
SELECT ?covp ?covplab ?p ?plab ?humanp ?humanplab WHERE
{
?interaction bl:edge_label bl:interacts_with .
?interaction bl:object ?covp .
?interaction bl:subject ?humanp .
?interaction bl:relation ?p .
?covp bl:category bl:Protein; <https://www.example.org/UNKNOWN/ncbi_taxid> ?covtaxon .
?humanp bl:category bl:Protein; <https://www.example.org/UNKNOWN/ncbi_taxid> ?humantaxon .
OPTIONAL { ?covp rdfs:label ?covplab } .
OPTIONAL { ?humanp rdfs:label ?humanplab } .
OPTIONAL { ?p rdfs:label ?plab }
FILTER(?covtaxon="2697049")
FILTER(?humantaxon="9606")
}
Loading

0 comments on commit 86dbb34

Please sign in to comment.