diff options
Diffstat (limited to 'gn3')
-rw-r--r-- | gn3/api/metadata.py | 2 | ||||
-rw-r--r-- | gn3/api/metadata_api/wiki.py | 4 | ||||
-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 |
4 files changed, 44 insertions, 41 deletions
diff --git a/gn3/api/metadata.py b/gn3/api/metadata.py index 3f28f5d..6110880 100644 --- a/gn3/api/metadata.py +++ b/gn3/api/metadata.py @@ -15,7 +15,7 @@ from gn3.db.datasets import (retrieve_metadata, get_history) from gn3.db.rdf import (query_frame_and_compact, query_and_compact) -from gn3.db.constants import ( +from gn3.db.rdf import ( RDF_PREFIXES, BASE_CONTEXT, DATASET_CONTEXT, DATASET_SEARCH_CONTEXT, PUBLICATION_CONTEXT, diff --git a/gn3/api/metadata_api/wiki.py b/gn3/api/metadata_api/wiki.py index a4abef6..b9942a0 100644 --- a/gn3/api/metadata_api/wiki.py +++ b/gn3/api/metadata_api/wiki.py @@ -5,8 +5,8 @@ from typing import Any, Dict from flask import Blueprint, request, jsonify, current_app, make_response from gn3 import db_utils from gn3.db import wiki -from gn3.db.rdf import (query_frame_and_compact, - get_wiki_entries_by_symbol) +from gn3.db.rdf import query_frame_and_compact +from gn3.db.rdf.wiki import get_wiki_entries_by_symbol wiki_blueprint = Blueprint("wiki", __name__, url_prefix="wiki") 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: |