From 19298caab3da5960cceb6a3609a5a8a608c7a0b5 Mon Sep 17 00:00:00 2001 From: Munyoki Kilyungi Date: Wed, 25 Oct 2023 12:05:37 +0300 Subject: Implement "GET /metadata/genotypes/:name". * gn3/api/metadata.py (fetch_group_by_species): New end-point. Signed-off-by: Munyoki Kilyungi --- gn3/api/metadata.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/gn3/api/metadata.py b/gn3/api/metadata.py index dfedd27..72337a9 100644 --- a/gn3/api/metadata.py +++ b/gn3/api/metadata.py @@ -1033,3 +1033,60 @@ CONSTRUCT { }) except (RemoteDisconnected, URLError): return jsonify({}) + + +@metadata.route("/genotypes/", methods=["GET"]) +def genotypes(name): + """Fetch a genotype's metadata given it's name""" + try: + sparql = SPARQLWrapper(current_app.config.get("SPARQL_ENDPOINT")) + sparql.setQuery(Template(""" +$prefix + +CONSTRUCT { + ?genotype ?predicate ?object . + ?species rdfs:label ?speciesName . +} WHERE { + ?genotype rdf:type gnc:Genotype ; + rdfs:label "$name" ; + ?predicate ?object . + OPTIONAL { + ?species ^xkos:classifiedUnder ?genotype ; + rdfs:label ?speciesName . + } +} +""").substitute(prefix=RDF_PREFIXES, name=name)) + results = json.loads(sparql.queryAndConvert().serialize(format="json-ld")) + if not results: + return jsonify({}) + frame = { + "@context": { + "data": "@graph", + "type": "@type", + "id": "@id", + "rdfs": "http://www.w3.org/2000/01/rdf-schema#", + "gnt": "http://genenetwork.org/term/", + "xkos": "http://rdf-vocabulary.ddialliance.org/xkos#", + "gnc": "http://genenetwork.org/category/", + "xsd": "http://www.w3.org/2001/XMLSchema#", + "name": "rdfs:label", + "chr": "gnt:chr", + "mb": "gnt:mb", + "mbMm8": "gnt:mbMm8", + "mb2016": "gnt:mb2016", + "sequence": "gnt:hasSequence", + "source": "gnt:hasSource", + "species": "xkos:classifiedUnder", + "alternateSource": "gnt:hasAltSourceName", + "comments": "rdfs:comments", + "chrNum": { + "@id": "gnt:chrNum", + "@type": "xsd:int", + } + }, + "type": "gnc:Genotype", + } + return jsonld.compact(jsonld.frame(results, frame), frame) + except (RemoteDisconnected, URLError): + return jsonify({}) + -- cgit v1.2.3