Skip to content

Commit

Permalink
Implement "GET /species".
Browse files Browse the repository at this point in the history
* gn3/api/metadata.py (list_species): New end-point.

Signed-off-by: Munyoki Kilyungi <me@bonfacemunyoki.com>
  • Loading branch information
BonfaceKilz committed Oct 24, 2023
1 parent ad979ed commit 671c7c7
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions gn3/api/metadata.py
Expand Up @@ -620,3 +620,45 @@ def get_ncbi_genewiki_entries(symbol):
context)
except (RemoteDisconnected, URLError):
return jsonify({})


@metadata.route("/species", methods=["GET"])
def list_species():
"""List all species"""
try:
sparql = SPARQLWrapper(current_app.config.get("SPARQL_ENDPOINT"))
sparql.setQuery(Template("""
$prefix
CONSTRUCT {
?species ?predicate ?object .
} WHERE {
?species ^skos:member gnc:Species ;
?predicate ?object .
VALUES ?predicate {
rdfs:label skos:prefLabel
skos:altLabel gnt:shortName
gnt:family skos:notation
}
}
""").substitute(prefix=RDF_PREFIXES))
results = sparql.queryAndConvert()
results = json.loads(
results.serialize(format="json-ld")
)
return jsonld.compact(results, {
"@context": PREFIXES | {
"data": "@graph",
"type": "@type",
"id": "@id",
"name": "rdfs:label",
"family": "gnt:family",
"shortName": "gnt:shortName",
"alternateName": "skos:altLabel",
"taxonomicId": "skos:notation",
"preferredName": "skos:prefLabel",
},
})
except (RemoteDisconnected, URLError):
return jsonify({})

0 comments on commit 671c7c7

Please sign in to comment.