diff options
Diffstat (limited to 'gn3/db')
-rw-r--r-- | gn3/db/rdf/__init__.py (renamed from gn3/db/constants.py) | 40 | ||||
-rw-r--r-- | gn3/db/rdf/wiki.py (renamed from gn3/db/rdf.py) | 39 |
2 files changed, 41 insertions, 38 deletions
diff --git a/gn3/db/constants.py b/gn3/db/rdf/__init__.py index 45e3bfc..c763810 100644 --- a/gn3/db/constants.py +++ b/gn3/db/rdf/__init__.py @@ -1,6 +1,15 @@ +"""RDF + +Constants for prefixes and contexts; and wrapper functions around +creating contexts to be used by jsonld when framing and/or compacting. + """ -This module contains some constants used in other modules. -""" +import json + +from SPARQLWrapper import SPARQLWrapper +from pyld import jsonld # type: ignore + + PREFIXES = { "dcat": "http://www.w3.org/ns/dcat#", "dct": "http://purl.org/dc/terms/", @@ -150,3 +159,30 @@ PHENOTYPE_CONTEXT = BASE_CONTEXT | PUBLICATION_CONTEXT | { "species": "gnt:belongsToSpecies", "group": "gnt:belongsToGroup", } + + +def sparql_construct_query(query: str, endpoint: str) -> dict: + """Query virtuoso using a CONSTRUCT query and return a json-ld + dictionary""" + sparql = SPARQLWrapper(endpoint) + sparql.setQuery(query) + results = sparql.queryAndConvert() + return json.loads(results.serialize(format="json-ld")) # type: ignore + + +def query_frame_and_compact(query: str, context: dict, endpoint: str) -> dict: + """Frame and then compact the results given a context""" + results = sparql_construct_query(query, endpoint) + return jsonld.compact(jsonld.frame(results, context), context) + + +def query_and_compact(query: str, context: dict, endpoint: str) -> dict: + """Compact the results given a context""" + results = sparql_construct_query(query, endpoint) + return jsonld.compact(results, context) + + +def query_and_frame(query: str, context: dict, endpoint: str) -> dict: + """Frame the results given a context""" + results = sparql_construct_query(query, endpoint) + return jsonld.frame(results, context) diff --git a/gn3/db/rdf.py b/gn3/db/rdf/wiki.py index 5a95683..1fc3130 100644 --- a/gn3/db/rdf.py +++ b/gn3/db/rdf/wiki.py @@ -1,42 +1,9 @@ -"""RDF utilities - -This module is a collection of functions that handle SPARQL queries. +"""Sparql queries to get metadata about WIKI and RIF metadata. """ -import json from string import Template -from SPARQLWrapper import SPARQLWrapper -from pyld import jsonld # type: ignore -from gn3.db.constants import ( - RDF_PREFIXES, BASE_CONTEXT -) - - -def sparql_construct_query(query: str, endpoint: str) -> dict: - """Query virtuoso using a CONSTRUCT query and return a json-ld - dictionary""" - sparql = SPARQLWrapper(endpoint) - sparql.setQuery(query) - results = sparql.queryAndConvert() - return json.loads(results.serialize(format="json-ld")) # type: ignore - - -def query_frame_and_compact(query: str, context: dict, endpoint: str) -> dict: - """Frame and then compact the results given a context""" - results = sparql_construct_query(query, endpoint) - return jsonld.compact(jsonld.frame(results, context), context) - - -def query_and_compact(query: str, context: dict, endpoint: str) -> dict: - """Compact the results given a context""" - results = sparql_construct_query(query, endpoint) - return jsonld.compact(results, context) - - -def query_and_frame(query: str, context: dict, endpoint: str) -> dict: - """Frame the results given a context""" - results = sparql_construct_query(query, endpoint) - return jsonld.frame(results, context) +from gn3.db.rdf import (BASE_CONTEXT, RDF_PREFIXES, + query_frame_and_compact) def get_wiki_entries_by_symbol(symbol: str, sparql_uri: str) -> dict: |