aboutsummaryrefslogtreecommitdiff
path: root/gn3/db
diff options
context:
space:
mode:
authorMunyoki Kilyungi2024-08-22 12:58:29 +0300
committerBonfaceKilz2024-08-26 15:35:33 +0300
commitc60d7c9599d8a1d1514f16e7ba7c333270840baa (patch)
treee633871ef5e1f63829b3a63cd20c5a38f78b9c11 /gn3/db
parentf0e8c7e680bf6d62b2ca5f0100e846d78ed97745 (diff)
downloadgenenetwork3-c60d7c9599d8a1d1514f16e7ba7c333270840baa.tar.gz
Fetch all the wiki entries by symbol.
* gn3/api/metadata.py: Import "get_wiki_entries_by_symbol". (get_gn_genewiki_entries): Rename this to... (get_wiki_entries): this; and update the URL endpoint. * gn3/db/rdf.py: Import constants.BASE_CONTEXT. (get_wiki_entries_by_symbol): New function. Signed-off-by: Munyoki Kilyungi <me@bonfacemunyoki.com>
Diffstat (limited to 'gn3/db')
-rw-r--r--gn3/db/rdf.py69
1 files changed, 67 insertions, 2 deletions
diff --git a/gn3/db/rdf.py b/gn3/db/rdf.py
index c5a8d7f..9d6346b 100644
--- a/gn3/db/rdf.py
+++ b/gn3/db/rdf.py
@@ -4,11 +4,11 @@ This module is a collection of functions that handle SPARQL queries.
"""
import json
-
+from string import Template
from SPARQLWrapper import SPARQLWrapper
from pyld import jsonld # type: ignore
from gn3.db.constants import (
- RDF_PREFIXES
+ RDF_PREFIXES, BASE_CONTEXT
)
@@ -43,3 +43,68 @@ def query_and_frame(query: str, context: dict, endpoint: str) -> dict:
if not results:
return {}
return jsonld.frame(results, context)
+
+
+def get_wiki_entries_by_symbol(symbol: str, sparql_uri: str) -> dict:
+ """Fetch all the Wiki entries using the symbol"""
+ # FIXME: Get the latest VersionId of a comment.
+ query = Template("""
+$prefix
+
+CONSTRUCT {
+ _:node rdfs:label '$symbol';
+ gnt:reason ?reason ;
+ gnt:species ?species ;
+ dct:references ?pmid ;
+ foaf:homepage ?weburl ;
+ rdfs:comment ?wikientry ;
+ foaf:mbox ?email ;
+ gnt:initial ?usercode ;
+ gnt:belongsToCategory ?category ;
+ gnt:hasVersion ?versionId
+} WHERE {
+ ?symbolId rdfs:comment _:node ;
+ rdfs:label '$symbol' .
+ _:node rdf:type gnc:GNWikiEntry ;
+ dct:hasVersion "0"^^xsd:int ;
+ dct:hasVersion ?version ;
+ rdfs:comment ?wikientry .
+ OPTIONAL { _:node gnt:reason ?reason } .
+ OPTIONAL {
+ _:node gnt:belongsToSpecies ?speciesId .
+ ?speciesId gnt:shortName ?species .
+ } .
+ OPTIONAL { _:node dct:references ?pubmedId . } .
+ OPTIONAL { _:node foaf:homepage ?weburl . } .
+ OPTIONAL { _:node gnt:initial ?usercode . } .
+ OPTIONAL { _:node gnt:mbox ?email . } .
+ OPTIONAL { _:node gnt:belongsToCategory ?category . }
+ BIND (str(?version) AS ?versionId) .
+ BIND (str(?pubmedId) AS ?pmid)
+}
+""").substitute(prefix=RDF_PREFIXES, symbol=symbol,)
+ context = BASE_CONTEXT | {
+ "foaf": "http://xmlns.com/foaf/0.1/",
+ "dct": "http://purl.org/dc/terms/",
+ "categories": "gnt:belongsToCategory",
+ "web_url": "foaf:homepage",
+ "version": "gnt:hasVersion",
+ "symbol": "rdfs:label",
+ "reason": "gnt:reason",
+ "species": "gnt:species",
+ "pubmed_id": "dct:references",
+ "web_url": "foaf:homepage",
+ "email": "foaf:mbox",
+ "initial": "gnt:initial",
+ "comment": "rdfs:comment"
+ }
+ results = query_frame_and_compact(
+ query, context,
+ sparql_uri
+ )
+ if data := results.get("data"):
+ for result in data:
+ if result.get("categories"):
+ result["categories"] = list(map(str.strip,
+ result.get("categories").split(";")))
+ return results