# Public API for PubSeq
import os
import requests
import sys
from flask import Flask, request, redirect, send_file, send_from_directory, render_template, jsonify
from bh20simplewebuploader.main import app, baseURL
# Helper functions
def fetch_sample_metadata(id):
query = """
PREFIX pubseq:
PREFIX sio:
PREFIX edam:
PREFIX efo:
select distinct ?id ?seq ?info ?sequencer
{
?sample sio:SIO_000115 "%s" .
?sample sio:SIO_000115 ?id .
?seq pubseq:technology ?tech .
?seq pubseq:sample ?sample .
?sample edam:data_2091 ?info .
?tech efo:EFO_0002699 ?sequencer .
} limit 5
""" % id
payload = {'query': query, 'format': 'json'}
r = requests.get(baseURL, params=payload)
return r.json()['results']['bindings']
# Main API routes
@app.route('/api/version')
def version():
return jsonify({ 'service': 'PubSeq', 'version': 0.10 })
@app.route('/api/sample/.json')
def sample(id):
# metadata = file.name(seq)+"/metadata.yaml"
meta = fetch_sample_metadata(id)
return jsonify([{
'id': x['id']['value'],
'fasta': x['seq']['value'],
'collection': os.path.dirname(x['seq']['value']),
'info': x['info']['value'],
'sequencer': x['sequencer']['value'],
} for x in meta])
@app.route('/api/ebi/sample-.xml', methods=['GET'])
def ebi_sample(id):
meta = fetch_sample_metadata(id)[0]
print("HERE",meta,file=sys.stderr)
page = render_template('ebi-sample.xml',sampleid=id,sequencer=meta['sequencer']['value'])
return page
@app.route('/api/search', methods=['GET'])
def search():
"""
Execute a 'global search'
"""
s = request.args.get('s')
if s == "":
s = "MT326090.1"
query = """
PREFIX pubseq:
PREFIX sio:
PREFIX edam:
select distinct ?id ?seq ?info
{
?sample sio:SIO_000115 "%s" .
?sample sio:SIO_000115 ?id .
?seq pubseq:sample ?sample .
?sample edam:data_2091 ?info .
} limit 100
""" % s
payload = {'query': query, 'format': 'json'}
r = requests.get(baseURL, params=payload)
result = r.json()['results']['bindings']
# metadata = file.name(seq)+"/metadata.yaml"
print(result)
return jsonify([{
'id': x['id']['value'],
'fasta': x['seq']['value'],
'collection': os.path.dirname(x['seq']['value']),
'info': x['info']['value'],
} for x in result])