From 4da80ca9c17bdd28e2689895ab6acb1ec0c70d33 Mon Sep 17 00:00:00 2001 From: Munyoki Kilyungi Date: Wed, 7 Jun 2023 09:39:58 +0300 Subject: Add publication end-point * gn3/api/metadata.py: Import get_publication_metadata (publication): New endpoint. * gn3/db/rdf.py (get_dataset_metadata): New function. Signed-off-by: Munyoki Kilyungi --- gn3/api/metadata.py | 10 ++++++++++ gn3/db/rdf.py | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) (limited to 'gn3') diff --git a/gn3/api/metadata.py b/gn3/api/metadata.py index b54f032..1330b6f 100644 --- a/gn3/api/metadata.py +++ b/gn3/api/metadata.py @@ -9,6 +9,7 @@ from flask import current_app 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 sparql_query from gn3.db.rdf import RDF_PREFIXES @@ -31,9 +32,18 @@ def dataset(name): return jsonify({}) +@metadata.route("/publication/", methods=["GET"]) +def publication(name): + """Fetch a dataset's metadata given it's ACCESSION_ID""" try: + if "unpublished" in name: + name = f"gn:{name}" + else: + name = f"publication:{name}" return jsonify( + get_publication_metadata( SPARQLWrapper(current_app.config.get("SPARQL_ENDPOINT")), + name, ).data ) # The virtuoso server is misconfigured or it isn't running at all diff --git a/gn3/db/rdf.py b/gn3/db/rdf.py index 5b62b46..c559774 100644 --- a/gn3/db/rdf.py +++ b/gn3/db/rdf.py @@ -156,6 +156,46 @@ CONSTRUCT { ): response[key] = value.map(get_url_local_name) # type: ignore return response + + +def get_publication_metadata( + sparql_conn: SPARQLWrapper, name: str +): + """Return info about a publication with a given NAME""" + __metadata_query = """ +$prefix + +CONSTRUCT { + gn:publication ?publicationTerm ?publicationValue . + gn:publication ?predicate ?subject . +} WHERE { + $name ?publicationTerm ?publicationValue . + ?publication ?publicationTerm ?publicationValue . + OPTIONAL { + ?subject ?predicate ?publication . + } . + VALUES ?publicationTerm { + gn:pubMedId gn:title gn:volume + gn:abstract gn:pages gn:month gn:year gn:author + } + VALUES ?predicate { + gn:phenotypeOfPublication + } +} +""" + response: MonadicDict = MonadicDict() + + for key, value in sparql_query( + sparql_conn, + Template(__metadata_query) + .substitute( + prefix=RDF_PREFIXES, + name=name + ) + )[0].items(): + response[key] = value + if isinstance(value, str) and not key.endswith("pubMedId"): + response[key] = value.map(get_url_local_name) # type: ignore return response -- cgit v1.2.3