forked from intermine/intermine-python-ws-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pathways-app.py
73 lines (59 loc) · 2.39 KB
/
pathways-app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from flask import Flask, request, redirect, session, url_for, render_template, flash
from pathways import PathwayDemo
from collections import defaultdict
import org_util
import os
DEBUG = True
app = Flask(__name__)
app.config.from_object(__name__)
app.secret_key = os.urandom(24)
pd = PathwayDemo()
@app.route('/')
def index():
return redirect(url_for('search'))
@app.route('/search', methods=['GET', 'POST'])
def search():
if request.method == 'POST':
symbol = request.form['symbol']
org = request.form['organism']
if symbol:
genes = pd.find_gene(symbol, org_util.get_name(org))
if len(genes) == 1:
symbol = genes[0][0]
return redirect(url_for('view', symbol=symbol, organism=org))
flash('Could not find gene: ' + symbol + ' in organism ' + org)
return redirect(url_for('search'))
else:
orgs = org_util.get_abbrevs()
return render_template('search.html', organisms=orgs)
@app.route('/view/<organism>/<symbol>')
def view(organism, symbol):
# if we came from the search page then we know the symbol has been
# found already, othwerise was entered in the URL so do search
if not request.referrer or not request.referrer.endswith('search'):
genes = pd.find_gene(symbol, org_util.get_name(organism))
if len(genes) == 1:
symbol = genes[0][0]
else:
flash('Could not find gene: ' + symbol + ' in organism ' + organism)
return redirect(url_for('search'))
org_full_name = org_util.get_name(organism)
homologs = pd.get_homologs_for_gene(symbol, org_full_name)
# include starting gene in search
all_orgs = dict(homologs)
if not org_full_name in all_orgs:
all_orgs[org_full_name] = {}
all_orgs[org_full_name][symbol] = None
# fetch pathways
pathways = []
for org in all_orgs:
for gene in all_orgs[org]:
for pws in pd.get_pathways(gene, org):
pathways.append(pws)
# re-organise for display: {pathway: {organisms: [genes]}}
table = defaultdict(lambda: defaultdict(list))
for row in pathways:
table[row[0]][row[1]].append(row[2])
return render_template('view.html', symbol=symbol, organism=org_full_name, org_abbrev=organism, orgs=all_orgs, homologs=homologs, table=table)
if __name__ == '__main__':
app.run('0.0.0.0', debug=False)