-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #216 from Knowledge-Graph-Hub/add_example_sparql_q…
…ueries Refactor query command to execute SPARQL queries described in YAML files
- Loading branch information
Showing
12 changed files
with
176 additions
and
404 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
285
kg_covid_19/query_utils/target_candidates/target_candidates.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
Oops, something went wrong.