diff options
author | Munyoki Kilyungi | 2023-10-18 14:33:13 +0300 |
---|---|---|
committer | BonfaceKilz | 2023-10-27 13:45:32 +0300 |
commit | f8e5a04d5e7c340657ae69fb95884f00d68ac7c1 (patch) | |
tree | 7655755ceec6204a1a5650c9a16b0681cdd4cdf1 | |
parent | 6cf155a878f592a4d8f93a8aaf00667e081174fe (diff) | |
download | genenetwork3-f8e5a04d5e7c340657ae69fb95884f00d68ac7c1.tar.gz |
Implement "GET /metadata/genotypes/:name".
* gn3/api/metadata.py: Delete gn3.db.rdf.get_genotype_metadata.
(genotype): Rename this to ...
(genotypes): ... this. Construct a query for fetching genotypes and
return a response as json-ld.
-rw-r--r-- | gn3/api/metadata.py | 55 |
1 files changed, 45 insertions, 10 deletions
diff --git a/gn3/api/metadata.py b/gn3/api/metadata.py index 08a9418..126b45e 100644 --- a/gn3/api/metadata.py +++ b/gn3/api/metadata.py @@ -14,7 +14,6 @@ from SPARQLWrapper import JSON, JSONLD, SPARQLWrapper from gn3.db.rdf import get_dataset_metadata from gn3.db.rdf import get_phenotype_metadata -from gn3.db.rdf import get_genotype_metadata from gn3.db.rdf import sparql_query from gn3.db.rdf import RDF_PREFIXES, PREFIXES @@ -326,17 +325,53 @@ def phenotype(name): return jsonify({}) -@metadata.route("/genotype/<name>", methods=["GET"]) -def genotype(name): +@metadata.route("/genotypes/<name>", methods=["GET"]) +def genotypes(name): """Fetch a genotype's metadata given it's name""" try: - return jsonify( - get_genotype_metadata( - SPARQLWrapper(current_app.config.get("SPARQL_ENDPOINT")), - name, - ).data - ) - # The virtuoso server is misconfigured or it isn't running at all + 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": PREFIXES | { + "data": "@graph", + "type": "@type", + "id": "@id", + "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({}) |