From 59c8bc53f8aa386a1052478c8553c523bfaaeb3d Mon Sep 17 00:00:00 2001 From: Munyoki Kilyungi Date: Mon, 20 Nov 2023 17:42:59 +0300 Subject: Attach phenotype endpoints to one function. * gn3/api/metadata.py (phenotypes): Add "/phenotypes//" to this endpoint. (fetch_phenotype_by_group): Delete. Signed-off-by: Munyoki Kilyungi --- gn3/api/metadata.py | 64 +++++------------------------------------------------ 1 file changed, 5 insertions(+), 59 deletions(-) (limited to 'gn3/api') diff --git a/gn3/api/metadata.py b/gn3/api/metadata.py index b97a4c4..45bef41 100644 --- a/gn3/api/metadata.py +++ b/gn3/api/metadata.py @@ -416,13 +416,14 @@ CONSTRUCT { except (RemoteDisconnected, URLError): return jsonify({}) - -@metadata.route("/phenotypes/", methods=["GET"]) -def phenotypes(name): +@metadata.route("/phenotypes/",methods=["GET"]) +@metadata.route("/phenotypes//", methods=["GET"]) +def phenotypes(name, group=None): """Fetch a phenotype's metadata given it's name""" try: args = request.args - dataset = args.get("dataset", "") + if group: + name = f"{group}_{name}" _query = Template(""" $prefix @@ -476,61 +477,6 @@ CONSTRUCT { 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", "") - _query = 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) - _context = { - "@context": PHENOTYPE_CONTEXT, - "type": "gnc:Phenotype", - } - return query_frame_and_compact( - _query, _context, - current_app.config.get("SPARQL_ENDPOINT") - ) - except (RemoteDisconnected, URLError): - return jsonify({}) - - @metadata.route("/genotypes/", methods=["GET"]) def genotypes(name): """Fetch a genotype's metadata given it's name""" -- cgit v1.2.3