aboutsummaryrefslogtreecommitdiff
path: root/gn3
diff options
context:
space:
mode:
Diffstat (limited to 'gn3')
-rw-r--r--gn3/api/metadata.py49
-rw-r--r--gn3/db/rdf.py40
2 files changed, 38 insertions, 51 deletions
diff --git a/gn3/api/metadata.py b/gn3/api/metadata.py
index fd3166c..5ef14ab 100644
--- a/gn3/api/metadata.py
+++ b/gn3/api/metadata.py
@@ -13,7 +13,6 @@ from pyld import jsonld
from SPARQLWrapper import JSON, JSONLD, 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 get_genotype_metadata
from gn3.db.rdf import sparql_query
@@ -201,20 +200,48 @@ CONSTRUCT {
return jsonify({})
-@metadata.route("/publication/<name>", methods=["GET"])
-def publication(name):
+@metadata.route("/publications/<name>", methods=["GET"])
+def publications(name):
"""Fetch a publication's metadata given it's ACCESSION_ID"""
try:
if "unpublished" in name:
- name = f"gn:{name}"
+ name = f"gn:unpublished{name}"
else:
- name = f"publication:{name}"
- return jsonify(
- get_publication_metadata(
- SPARQLWrapper(current_app.config.get("SPARQL_ENDPOINT")),
- name,
- ).data
- )
+ name = f"pubmed:{name}"
+ sparql = SPARQLWrapper(current_app.config.get("SPARQL_ENDPOINT"))
+ sparql.setQuery(Template("""
+$prefix
+
+CONSTRUCT {
+ $name ?predicate ?object .
+} WHERE {
+ $name rdf:type fabio:ResearchPaper ;
+ ?predicate ?object .
+ FILTER (!regex(str(?predicate), '(hasPubMedId)', 'i')) .
+}
+""").substitute(name=name, prefix=RDF_PREFIXES))
+ return jsonld.compact(
+ json.loads(sparql.queryAndConvert().serialize(format="json-ld")),
+ {
+ "@context": PREFIXES | {
+ "type": "@type",
+ "id": "@id",
+ "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"
+ },
+ },
+ })
# The virtuoso server is misconfigured or it isn't running at all
except (RemoteDisconnected, URLError):
return jsonify({})
diff --git a/gn3/db/rdf.py b/gn3/db/rdf.py
index 1d714c1..2f80aa2 100644
--- a/gn3/db/rdf.py
+++ b/gn3/db/rdf.py
@@ -130,46 +130,6 @@ CONSTRUCT {
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
-
-
def get_phenotype_metadata(
sparql_conn: SPARQLWrapper, name: str
):