diff options
-rw-r--r-- | gn3/api/metadata.py | 95 |
1 files changed, 95 insertions, 0 deletions
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/<group>/<name>", 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/<name>", methods=["GET"]) def genotypes(name): |