From a1a0e99acaccc95fafeffed489642af28d744d76 Mon Sep 17 00:00:00 2001 From: lltommy Date: Thu, 23 Apr 2020 22:50:58 +0200 Subject: Adding API endpoints to drive the homepage. Dirty but functional --- bh20simplewebuploader/main.py | 83 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) diff --git a/bh20simplewebuploader/main.py b/bh20simplewebuploader/main.py index ceab8a6..e88eb4c 100644 --- a/bh20simplewebuploader/main.py +++ b/bh20simplewebuploader/main.py @@ -9,8 +9,9 @@ import string import yaml import pkg_resources from flask import Flask, request, redirect, send_file, send_from_directory, render_template - import os.path +import requests + if not os.path.isfile('bh20sequploader/mainx.py'): print("WARNING: run FLASK from the root of the source repository!", file=sys.stderr) @@ -343,3 +344,83 @@ def receive_files(): return render_template('success.html', log=result.stdout.decode('utf-8', errors='replace')) finally: shutil.rmtree(dest_dir) + + +## Dynamic API functions starting here +## This is quick and dirty for now, just to get something out and demonstrate the queries +## Feel free to rename the functions/endpoints, feel free to process result so we get nicer JSON +## but most likley you don't want to touch the queries, Cheers. +baseURL='http://sparql.genenetwork.org/sparql/' + +@app.route('/api/getAllaccessions', methods=['GET']) +def getAllaccessions(): + query="""SELECT DISTINCT ?fasta ?value WHERE {?fasta ?x[ ?value ]}""" + payload = {'query': query, 'format': 'json'} + r = requests.get(baseURL, params=payload) + result = r.json()['results']['bindings'] + return str(result) + + +# parameter must be encoded e.g. http://arvados.org/keep:6e6276698ed8b0e6cd21f523e4f91179+123/sequence.fasta must become +# http%3A%2F%2Farvados.org%2Fkeep%3A6e6276698ed8b0e6cd21f523e4f91179%2B123%2Fsequence.fasta +@app.route('/api/getDetailsForSeq', methods=['GET']) +def getDetailsForSeq(): + seq_id = request.args.get('seq') + query="""SELECT DISTINCT ?key ?value WHERE { ?x [?key ?value]}""" + query=query.replace("placeholder", seq_id) + print(query) + payload = {'query': query, 'format': 'json'} + r = requests.get(baseURL, params=payload) + result = r.json()['results']['bindings'] + return str(result) + +@app.route('/api/getSEQbytech', methods=['GET']) +def getSEQbytech(): + query="""SELECT ?specimen_source ?specimen_source_label (count(?fasta) as ?fastaCount) WHERE + {?fasta ?x [ ?specimen_source] + BIND (concat(?specimen_source,"_label") as ?specimen_source_label)} + GROUP BY ?specimen_source ?specimen_source_label ORDER BY DESC (?fastaCount) + """ + payload = {'query': query, 'format': 'json'} + r = requests.get(baseURL, params=payload) + result = r.json()['results']['bindings'] + return str(result) + +@app.route('/api/getSEQbyLocation', methods=['GET']) +def getSEQbyLocation(): + query="""SELECT ?geoLocation ?geoLocation_label (count(?fasta) as ?fastaCount) WHERE + {?fasta ?x [ ?geoLocation] + BIND (concat(?geoLocation,"_label") as ?geoLocation_label)} + GROUP BY ?geoLocation ?geoLocation_label ORDER BY DESC (?fastaCount) + """ + payload = {'query': query, 'format': 'json'} + r = requests.get(baseURL, params=payload) + result = r.json()['results']['bindings'] + return str(result) + +@app.route('/api/getSEQbySpecimenSource', methods=['GET']) +def getSEQbySpecimenSource(): + query="""SELECT ?specimen_source ?specimen_source_label (count(?fasta) as ?fastaCount) WHERE + {?fasta ?x [ ?specimen_source] + BIND (concat(?specimen_source,"_label") as ?specimen_source_label)} + GROUP BY ?specimen_source ?specimen_source_label + ORDER BY DESC (?fastaCount) + """ + payload = {'query': query, 'format': 'json'} + r = requests.get(baseURL, params=payload) + result = r.json()['results']['bindings'] + return str(result) + +#No data for this atm +@app.route('/api/getSEQbyHostHealthStatus', methods=['GET']) +def getSEQbyHostHealthStatus(): + query="""SELECT ?health_status ?health_status_label (count(?fasta) as ?fastaCount) WHERE + {?fasta ?x [ ?health_status] + BIND (concat(?health_status,"_label") as ?health_status_label)} + GROUP BY ?health_status ?health_status_label + ORDER BY DESC (?fastaCount) + """ + payload = {'query': query, 'format': 'json'} + r = requests.get(baseURL, params=payload) + result = r.json()['results']['bindings'] + return str(result) \ No newline at end of file -- cgit v1.2.3