diff options
author | Munyoki Kilyungi | 2023-10-20 16:37:56 +0300 |
---|---|---|
committer | BonfaceKilz | 2023-10-27 13:45:32 +0300 |
commit | c4b77143cd763d70931871645d6eb976272d3165 (patch) | |
tree | cc2df514480134326561b6148ccd120997cc8f1b /gn3/api | |
parent | cf67b6f54a52f06a6e244c242c3e3d221e8f059a (diff) | |
download | genenetwork3-c4b77143cd763d70931871645d6eb976272d3165.tar.gz |
Implement "GET /metadata/phenotypes/:name".
* gn3/api/metadata.py: Delete gn3.db.rdf.get_phenotype_metadata.
(phenotype): Rename this to ...
(phenotypes): ... this and implement update logic.
* gn3/db/rdf.py (get_phenotype_metadata): Delete.
Signed-off-by: Munyoki Kilyungi <me@bonfacemunyoki.com>
Diffstat (limited to 'gn3/api')
-rw-r--r-- | gn3/api/metadata.py | 90 |
1 files changed, 80 insertions, 10 deletions
diff --git a/gn3/api/metadata.py b/gn3/api/metadata.py index ef6cffe..f023e17 100644 --- a/gn3/api/metadata.py +++ b/gn3/api/metadata.py @@ -12,7 +12,6 @@ from flask import current_app from pyld import jsonld from SPARQLWrapper import JSON, JSONLD, SPARQLWrapper -from gn3.db.rdf import get_phenotype_metadata from gn3.db.rdf import sparql_query from gn3.db.rdf import RDF_PREFIXES, PREFIXES @@ -309,17 +308,88 @@ CONSTRUCT { except (RemoteDisconnected, URLError): return jsonify({}) -@metadata.route("/phenotype/<name>", methods=["GET"]) -def phenotype(name): +@metadata.route("/phenotypes/<name>", methods=["GET"]) +def phenotypes(name): """Fetch a phenotype's metadata given it's name""" try: - return jsonify( - get_phenotype_metadata( - SPARQLWrapper(current_app.config.get("SPARQL_ENDPOINT")), - name, - ).data - ) - # The virtuoso server is misconfigured or it isn't running at all + args = request.args + dataset = args.get("dataset", "") + sparql = SPARQLWrapper(current_app.config.get("SPARQL_ENDPOINT")) + sparql.setQuery(Template(""" +$prefix + +CONSTRUCT { + ?phenotype ?predicate ?object ; + ?pubPredicate ?pubObject ; + ex:species ?speciesName ; + ex:inbredSet ?inbredSetName ; + ex:dataset ?datasetName . +} WHERE { + ?phenotype skos:altLabel "$name" ; + xkos:classifiedUnder ?inbredSet ; + ?predicate ?object . + ?inbredSet ^xkos:classifiedUnder ?phenotype ; + rdfs:label ?inbredSetName ; + xkos:generalizes ?species . + ?species skos:prefLabel ?speciesName . + FILTER (!regex(str(?predicate), '(classifiedUnder)', 'i')) . + OPTIONAL { + ?publication ^dct:isReferencedBy ?phenotype ; + rdf:type fabio:ResearchPaper ; + ?pubPredicate ?pubObject . + FILTER (!regex(str(?pubPredicate), '(hasPubMedId|type)', 'i')) . + } . + OPTIONAL { + ?dataset rdf:type dcat:Dataset ; + xkos:classifiedUnder ?type; + rdfs:label "$dataset" ; + skos:prefLabel ?datasetName . + ?type ^skos:member gnc:DatasetType . + FILTER(?type = gnc:Phenotype) . + } +} +""").substitute(prefix=RDF_PREFIXES, name=name, + dataset=dataset)) + results = json.loads(sparql.queryAndConvert().serialize(format="json-ld")) + if not results: + return jsonify({}) + frame = { + "@context": PREFIXES | { + "data": "@graph", + "type": "@type", + "id": "@id", + "traitName": "skos:altLabel", + "trait": "rdfs:label", + "altName": "rdfs:altLabel", + "description": "dct:description", + "abbreviation": "dct:abbreviation", + "labCode": "gnt:labCode", + "submitter": "gnt:submitter", + "contributor": "dct:contributor", + "mean": "gnt:mean", + "locus": "gnt:locus", + "LRS": "gnt:LRS", + "references": "dct:isReferencedBy", + "additive": "gnt:additive", + "sequence": "gnt:sequence", + "title": "dct:title", + "journal": "fabio:Journal", + "volume": "prism:volume", + "page": "fabio:page", + "creator": "dct:creator", + "abstract": "dct:abstract", + "year": { + "@id": "fabio:hasPublicationYear", + "@type": "xsd:gYear", + }, + "month": { + "@id": "prism:publicationDate", + "@type": "xsd:gMonth" + }, + }, + "type": "gnc:Phenotype", + } + return jsonld.compact(jsonld.frame(results, frame), frame) except (RemoteDisconnected, URLError): return jsonify({}) |