From 9d87a928cb0cfbd8ee49d8202c6fe1cc920fbd02 Mon Sep 17 00:00:00 2001 From: Munyoki Kilyungi Date: Wed, 25 Oct 2023 12:08:16 +0300 Subject: Implement "GET /metadata/groups/:name". * gn3/api/metadata.py (fetch_group_by_species): New end-point. Signed-off-by: Munyoki Kilyungi --- gn3/api/metadata.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'gn3/api') diff --git a/gn3/api/metadata.py b/gn3/api/metadata.py index 193357a..79c0417 100644 --- a/gn3/api/metadata.py +++ b/gn3/api/metadata.py @@ -1420,3 +1420,50 @@ CONSTRUCT { return jsonify({}) +@metadata.route("/groups/", methods=["GET"]) +def fetch_group_by_species(name): + """Fetch the list of groups""" + try: + sparql = SPARQLWrapper(current_app.config.get("SPARQL_ENDPOINT")) + sparql.setQuery(Template(""" +$prefix + +CONSTRUCT { + ?group ?predicate ?object . +} WHERE { + ?species gnt:shortName "$name" ; + ^skos:member gnc:Species . + ?group ^skos:member gnc:Set ; + xkos:generalizes ?species ; + ?predicate ?object . + VALUES ?predicate { + rdfs:label skos:prefLabel + gnt:geneticType gnt:mappingMethod + gnt:code gnt:family + } + +} +""").substitute(prefix=RDF_PREFIXES, name=name)) + results = sparql.queryAndConvert() + results = json.loads( + results.serialize(format="json-ld") + ) + return jsonld.compact(results, { + "@context": { + "data": "@graph", + "type": "@type", + "id": "@id", + "skos": "http://www.w3.org/2004/02/skos/core#", + "gnt": "http://genenetwork.org/term/", + "rdfs": "http://www.w3.org/2000/01/rdf-schema#", + "name": "rdfs:label", + "family": "gnt:family", + "shortName": "gnt:shortName", + "code": "gnt:code", + "mappingMethod": "gnt:mappingMethod", + "geneticType": "gnt:geneticType", + "fullName": "skos:prefLabel", + }, + }) + except (RemoteDisconnected, URLError): + return jsonify({}) -- cgit v1.2.3