aboutsummaryrefslogtreecommitdiff
path: root/gn3/api
diff options
context:
space:
mode:
authorMunyoki Kilyungi2023-10-25 12:07:08 +0300
committerBonfaceKilz2023-10-27 13:45:32 +0300
commitee6510523d68f82f2fd387618d8e21d56ccc6732 (patch)
tree7f9b80c66a777f44d2f3e36c2e4dda1f987d9b0f /gn3/api
parenta228d4a9b08ed0903a3ca36be8fa6fe8766782a3 (diff)
downloadgenenetwork3-ee6510523d68f82f2fd387618d8e21d56ccc6732.tar.gz
Implement "GET /metadata/species".
* gn3/api/metadata.py (list_species): New end-point. Signed-off-by: Munyoki Kilyungi <me@bonfacemunyoki.com>
Diffstat (limited to 'gn3/api')
-rw-r--r--gn3/api/metadata.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/gn3/api/metadata.py b/gn3/api/metadata.py
index 3731cc1..bbf6a74 100644
--- a/gn3/api/metadata.py
+++ b/gn3/api/metadata.py
@@ -1283,3 +1283,47 @@ CONSTRUCT {
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": {
+ "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",
+ "alternateName": "skos:altLabel",
+ "taxonomicId": "skos:notation",
+ "fullName": "skos:prefLabel",
+ },
+ })
+ except (RemoteDisconnected, URLError):
+ return jsonify({})