diff options
author | Munyoki Kilyungi | 2023-10-17 16:06:39 +0300 |
---|---|---|
committer | BonfaceKilz | 2023-10-27 13:45:32 +0300 |
commit | 6cf155a878f592a4d8f93a8aaf00667e081174fe (patch) | |
tree | d349766cc7c6aaa5c49332d4d3c3a9932df91b8b | |
parent | c82819fabb0564726677f0b74d8a55c853b2b309 (diff) | |
download | genenetwork3-6cf155a878f592a4d8f93a8aaf00667e081174fe.tar.gz |
Implement "GET /metadata/publications/search/:term".
* gn3/api/metadata.py (search_publications): New function.
Signed-off-by: Munyoki Kilyungi <me@bonfacemunyoki.com>
-rw-r--r-- | gn3/api/metadata.py | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/gn3/api/metadata.py b/gn3/api/metadata.py index 5ef14ab..08a9418 100644 --- a/gn3/api/metadata.py +++ b/gn3/api/metadata.py @@ -247,6 +247,70 @@ CONSTRUCT { return jsonify({}) +@metadata.route("/publications/search/<term>", methods=["GET"]) +def search_publications(term): + """Search publications""" + try: + args = request.args + page = args.get("page", 0) + page_size = args.get("limit", 10) + sparql = SPARQLWrapper(current_app.config.get("SPARQL_ENDPOINT")) + sparql.setQuery(Template(""" +$prefix + +CONSTRUCT { + ex:result rdf:type ex:resultType ; + ex:totalCount ?totalCount ; + ex:currentPage $offset ; + ex:items [ + rdfs:label ?publication ; + dct:title ?title ; + ] +} WHERE { +{ + SELECT ?publication ?title ?pmid WHERE { + ?pub rdf:type fabio:ResearchPaper ; + ?predicate ?object ; + dct:title ?title . + ?object bif:contains "'$term'" . + BIND( STR(?pub) AS ?publication ) . + } ORDER BY ?title LIMIT $limit OFFSET $offset + } +{ + SELECT (COUNT(*)/$limit+1 AS ?totalCount) WHERE { + ?publication rdf:type fabio:ResearchPaper ; + ?predicate ?object . + ?object bif:contains "'$term'" . + } +} +} +""").substitute(prefix=RDF_PREFIXES, term=term, limit=page_size, offset=page)) + results = sparql.queryAndConvert() + results = json.loads(results.serialize(format="json-ld")) + frame = { + "@context": PREFIXES | { + "data": "@graph", + "type": "@type", + "id": "@id", + "title": "dct:title", + "pubmed": "fabio:hasPubMedId", + "currentPage": "ex:currentPage", + "result": "ex:result", + "results": "ex:items", + "resultItem": "ex:resultType", + "pages": "ex:totalCount", + "url": "rdfs:label", + }, + "type": "resultItem", + "paper": { + "@type": "fabio:ResearchPaper", + "@container": "@index" + } + } + return jsonld.frame(results, frame) + except (RemoteDisconnected, URLError): + return jsonify({}) + @metadata.route("/phenotype/<name>", methods=["GET"]) def phenotype(name): """Fetch a phenotype's metadata given it's name""" |