From a228d4a9b08ed0903a3ca36be8fa6fe8766782a3 Mon Sep 17 00:00:00 2001 From: Munyoki Kilyungi Date: Wed, 25 Oct 2023 12:06:41 +0300 Subject: Implement "GET /metadata/genewikis/ncbi/:symbol". * gn3/api/metadata.py (get_ncbi_genewiki_entries): New end-point. Signed-off-by: Munyoki Kilyungi --- gn3/api/metadata.py | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) (limited to 'gn3/api') diff --git a/gn3/api/metadata.py b/gn3/api/metadata.py index c501331..3731cc1 100644 --- a/gn3/api/metadata.py +++ b/gn3/api/metadata.py @@ -1187,3 +1187,99 @@ CONSTRUCT { except (RemoteDisconnected, URLError): return jsonify({}) +@metadata.route("/genewikis/ncbi/", methods=["GET"]) +def get_ncbi_genewiki_entries(symbol): + """Fetch the NCBI GeneRIF entries""" + try: + args = request.args + page = args.get("page", 0) + page_size = args.get("per-page", 10) + sparql = SPARQLWrapper(current_app.config.get("SPARQL_ENDPOINT")) + sparql.setQuery(Template(""" +$prefix + +CONSTRUCT { + ?symbol ex:entries [ + rdfs:comment ?comment ; + gnt:hasGeneId ?geneId ; + ex:species ?species_ ; + dct:created ?createTime ; + dct:references ?pmids ; + dct:creator ?creator ; + ] . + ?symbol rdf:type gnc:GNWikiEntry ; + ex:totalCount ?totalCount ; + ex:currentPage $offset . +} WHERE { +{ + SELECT ?symbol ?comment ?geneId (GROUP_CONCAT(DISTINCT ?speciesName; SEPARATOR='; ') AS ?species_) + ?createTime ?creator + (GROUP_CONCAT(DISTINCT ?pubmed; SEPARATOR='; ') AS ?pmids) + WHERE { + ?symbol rdfs:label ?label ; + rdfs:comment _:entry . + ?label bif:contains "'$symbol'" . + _:entry rdf:type gnc:NCBIWikiEntry ; + rdfs:comment ?comment . + OPTIONAL { + ?species ^xkos:classifiedUnder _:entry ; + ^skos:member gnc:Species ; + skos:prefLabel ?speciesName . + } . + OPTIONAL { _:entry gnt:hasGeneId ?geneId . } . + OPTIONAL { _:entry dct:created ?createTime . } . + OPTIONAL { _:entry dct:references ?pubmed . } . + OPTIONAL { + ?investigator foaf:name ?creator ; + ^dct:creator _:entry . + } . + } GROUP BY ?comment ?symbol ?createTime ?creator ?geneId ORDER BY ?createTime LIMIT $limit OFFSET $offset +} + +{ + SELECT (COUNT(DISTINCT ?comment)/$limit+1 AS ?totalCount) WHERE { + ?symbol rdfs:comment _:entry ; + rdfs:label ?label . + _:entry rdfs:comment ?comment ; + rdf:type gnc:NCBIWikiEntry . + ?label bif:contains "'$symbol'" . + } +} +} +""").substitute(prefix=RDF_PREFIXES, symbol=symbol, + limit=page_size, offset=page)) + results = sparql.queryAndConvert() + results = json.loads(results.serialize(format="json-ld")) + context = { + "@context": { + "data": "@graph", + "type": "@type", + "id": "@id", + "ex": "http://example.org/stuff/1.0/", + "rdfs": "http://www.w3.org/2000/01/rdf-schema#", + "gnt": "http://genenetwork.org/term/", + "gnc": "http://genenetwork.org/category/", + "dct": "http://purl.org/dc/terms/", + "xsd": "http://www.w3.org/2001/XMLSchema#", + "entries": "ex:entries", + "comment": "rdfs:comment", + "category": 'gnt:belongsToCategory', + "author": "dct:creator", + "species": "ex:species", + "geneId": "gnt:hasGeneId", + "pubmed": "dct:references", + "currentPage": "ex:currentPage", + "pages": "ex:totalCount", + "created": { + "@id": "dct:created", + "@type": "xsd:datetime" + }, + }, + "type": "gnc:GNWikiEntry" + } + return jsonld.compact( + jsonld.frame(results, context), + context) + except (RemoteDisconnected, URLError): + return jsonify({}) + -- cgit v1.2.3