From fa2ce410d4730b136f24555c049cef3d6dac1102 Mon Sep 17 00:00:00 2001 From: Munyoki Kilyungi Date: Thu, 8 Jun 2023 14:44:38 +0300 Subject: Fetch phenotypes from virtuoso * gn3/api/metadata.py: Import get_phenotype_metadata. (phenotype): New end-point. * gn3/db/rdf.py (get_phenotype_metadata): New function. Signed-off-by: Munyoki Kilyungi --- gn3/api/metadata.py | 16 ++++++++++++++++ gn3/db/rdf.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/gn3/api/metadata.py b/gn3/api/metadata.py index 49c6798..a2eff05 100644 --- a/gn3/api/metadata.py +++ b/gn3/api/metadata.py @@ -10,6 +10,7 @@ from SPARQLWrapper import SPARQLWrapper from gn3.db.rdf import get_dataset_metadata from gn3.db.rdf import get_publication_metadata +from gn3.db.rdf import get_phenotype_metadata from gn3.db.rdf import sparql_query from gn3.db.rdf import RDF_PREFIXES @@ -51,6 +52,21 @@ def publication(name): return jsonify({}) +@metadata.route("/phenotype/", methods=["GET"]) +def phenotype(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 + except (RemoteDisconnected, URLError): + return jsonify({}) + + @metadata.route("/genewiki/", methods=["GET"]) def get_genewiki_entries(symbol): """Fetch the GN and NCBI GeneRIF entries""" diff --git a/gn3/db/rdf.py b/gn3/db/rdf.py index e92ccdb..0e472ce 100644 --- a/gn3/db/rdf.py +++ b/gn3/db/rdf.py @@ -26,6 +26,7 @@ PREFIX ncbiTaxon: PREFIX xsd: PREFIX publication: +PREFIX phenotype: """ @@ -198,12 +199,48 @@ CONSTRUCT { return response +def get_phenotype_metadata( + sparql_conn: SPARQLWrapper, name: str ): + """Return info about a phenotype with a given NAME""" __metadata_query = """ +$prefix +CONSTRUCT { + ?phenotype ?pPredicate ?pValue . + ?phenotype ?publicationTerm ?publicationValue . + ?phenotype gn:speciesName ?speciesName . + ?phenotype gn:inbredSetName ?inbredSetBinomialName . + ?phenotype gn:datasetName ?datasetFullName . +} WHERE { + ?phenotype ?pPredicate ?pValue . + OPTIONAL { + ?phenotype gn:phenotypeOfPublication ?publication . + ?publication ?publicationTerm ?publicationValue . + } . + OPTIONAL { + ?phenotype gn:phenotypeOfDataset ?dataset . + ?dataset gn:name ?datasetFullName . + ?dataset gn:datasetOfInbredSet ?inbredSet . + ?inbredSet gn:binomialName ?inbredSetBinomialName . + ?inbredSet gn:inbredSetOfSpecies ?species . + ?species gn:displayName ?speciesName . + } . + FILTER( ?phenotype = phenotype:$name ) . + MINUS { + ?phenotype rdf:type ?pValue . + } + MINUS { + ?publication rdf:type ?publicationValue . + } } """ result: MonadicDict = MonadicDict() + for key, value in sparql_query( sparql_conn, Template(__metadata_query) + .substitute(name=name, + prefix=RDF_PREFIXES) + )[0].items(): + result[key] = value return result -- cgit v1.2.3