diff options
author | Munyoki Kilyungi | 2023-10-25 12:05:37 +0300 |
---|---|---|
committer | BonfaceKilz | 2023-10-27 13:45:32 +0300 |
commit | 19298caab3da5960cceb6a3609a5a8a608c7a0b5 (patch) | |
tree | 68876869af98df827ef424ab07f306aac561795f | |
parent | 4fed1c7937604ae9d9f316104847237770f0598a (diff) | |
download | genenetwork3-19298caab3da5960cceb6a3609a5a8a608c7a0b5.tar.gz |
Implement "GET /metadata/genotypes/:name".
* gn3/api/metadata.py (fetch_group_by_species): New end-point.
Signed-off-by: Munyoki Kilyungi <me@bonfacemunyoki.com>
-rw-r--r-- | gn3/api/metadata.py | 57 |
1 files changed, 57 insertions, 0 deletions
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/<name>", 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({}) + |