From d90ebbefc517e28a00f322b4bda1a721788eff36 Mon Sep 17 00:00:00 2001 From: Munyoki Kilyungi Date: Tue, 24 Oct 2023 16:49:22 +0300 Subject: Implement "GET /phenotype/:group/:name". * gn3/api/metadata.py (fetch_phenotype_by_group): New end-point. Signed-off-by: Munyoki Kilyungi --- gn3/api/metadata.py | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) (limited to 'gn3/api') diff --git a/gn3/api/metadata.py b/gn3/api/metadata.py index fd4de69..d18fff9 100644 --- a/gn3/api/metadata.py +++ b/gn3/api/metadata.py @@ -505,6 +505,101 @@ CONSTRUCT { except (RemoteDisconnected, URLError): return jsonify({}) +@metadata.route("/phenotypes//", methods=["GET"]) +def fetch_phenotype_by_group(group, name): + """Fetch a phenotype's metadata given it's name""" + try: + 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 rdfs:label "$name" ; + xkos:classifiedUnder ?inbredSet ; + ?predicate ?object . + ?inbredSet ^xkos:classifiedUnder ?phenotype ; + (rdfs:label|skos:prefLabel|gnt:code) "$group" ; + 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, + group=group, + name=name, + dataset=dataset)) + results = json.loads(sparql.queryAndConvert().serialize(format="json-ld")) + if not results: + return jsonify({}) + frame = { + "@context": { + "data": "@graph", + "type": "@type", + "id": "@id", + "skos": "http://www.w3.org/2004/02/skos/core#", + "rdfs": "http://www.w3.org/2000/01/rdf-schema#", + "dct": "http://purl.org/dc/terms/", + "gnt": "http://genenetwork.org/term/", + "fabio": "http://purl.org/spar/fabio/", + "xsd": "http://www.w3.org/2001/XMLSchema#", + "prism": "http://prismstandard.org/namespaces/basic/2.0/", + "gnc": "http://genenetwork.org/category/", + "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({}) + @metadata.route("/genotypes/", methods=["GET"]) def genotypes(name): -- cgit v1.2.3