forked from buhrmann/elegans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
206 lines (164 loc) · 6.1 KB
/
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
from flask import Flask, render_template, request, flash, url_for, session, redirect, jsonify, Response, make_response
import json
import neo
import pubmed
import netx
# Configuration
DEBUG = True
SECRET_KEY = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
USER_NAME = 'syngnz'
PASSWORD = 'inform'
APP = Flask(__name__)
APP.config.from_object(__name__)
# Cache the results on server
NODE_CACHE = []
REL_CACHE = []
@APP.route('/about')
def about():
return render_template('about.html')
@APP.route('/discuss')
def discuss():
return render_template('discuss.html')
@APP.route('/build')
def build():
neo.db_add_neurons(clear=True)
neo.db_add_synapses(clear=False)
return redirect(url_for('index'))
@APP.route('/')
@APP.route('/<canvas>')
def index(canvas=None):
global NODE_CACHE, REL_CACHE
NODE_CACHE = neo.neurons()
REL_CACHE = neo.synapses_d3(NODE_CACHE, min_weight=0)
jsn = json.dumps({'neurons':NODE_CACHE, 'synapses':REL_CACHE})
use_canvas = canvas != "svg"
return render_template('graph.html', data=jsn, use_canvas=json.dumps(use_canvas))
@APP.route('/_subgraph')
def subgraph():
global NODE_CACHE, REL_CACHE
gr1 = request.args.get('group1', "no group1", type=str)
gr1 = [s.strip() for s in gr1.split(",") if not s.isspace()]
gr2 = request.args.get('group2', "no group2", type=str)
gr2 = [s.strip() for s in gr2.split(",") if not s.isspace()]
rec = request.args.get('receptors', "", type=str)
rec = [s.strip() for s in rec.split(",") if s and not s.isspace()] # empty list (falsy) when none
mus = request.args.get('muscles', "", type=str)
mus = [s.strip() for s in mus.split(",") if s and not s.isspace() ] # empty list (falsy) when none
min_ws = request.args.get('minWeightS', 1, type=int)
min_wj = request.args.get('minWeightJ', 1, type=int)
max_l = request.args.get('maxLength', 2, type=int)
path_dir = request.args.get('dir', 'uni', type=str)
res = neo.subgraph(gr1, gr2, max_l, min_ws, min_wj, path_dir, rec, mus)
NODE_CACHE = res['neurons']
REL_CACHE = res['synapses']
return jsonify(result=res)
@APP.route('/_group_names')
def group_names():
groups = neo.group_names()
print groups
return jsonify(result=groups)
@APP.route('/_neuron_names')
def neuron_names():
groups = request.args.getlist('groups[]')
neurons = neo.neuron_names(groups)
print neurons
return jsonify(result=neurons)
@APP.route('/_pubmed_single')
def pubmed_single():
query = request.args.get('query', 'chemotaxis', type=str)
neuron = request.args.get('name', 'ADA', type=str)
num_citations = pubmed.num_articles_for_neuron(neuron, query)
return jsonify(result={'name':neuron, 'count':num_citations})
@APP.route('/_pubmed_search')
def pubmed_search():
global NODE_CACHE, REL_CACHE
query = request.args.get('query', 'chemotaxis', type=str)
threshold = request.args.get('threshold', 1, type=int)
all_groups = neo.group_names()
filtered_groups = pubmed.neurons_for_query(all_groups, query, threshold)
neurons = neo.neuron_names(filtered_groups)
res = neo.all_cons_for_set(neurons)
NODE_CACHE = res['neurons']
REL_CACHE = res['synapses']
return jsonify(result=res)
@APP.route('/_groups_graph')
def groups_graph():
global NODE_CACHE, REL_CACHE
groups = request.args.getlist('groups[]')
neurons = neo.neuron_names(groups)
res = neo.all_cons_for_set(neurons)
NODE_CACHE = res['neurons']
REL_CACHE = res['synapses']
return jsonify(result=res)
@APP.route('/_expand')
def expand():
global NODE_CACHE, REL_CACHE
names = request.args.getlist('names[]')
mus = request.args.get('muscles', "", type=str)
mus = [s.strip() for s in mus.split(",") if s and not s.isspace() ] # empty list (falsy) when none
res = neo.all_cons_for_set(names, mus)
NODE_CACHE = res['neurons']
REL_CACHE = res['synapses']
return jsonify(result=res)
@APP.route('/_reset')
def reset():
global NODE_CACHE, REL_CACHE
NODE_CACHE = neo.neurons()
REL_CACHE = neo.synapses_d3(NODE_CACHE, min_weight=0)
res = {'neurons':NODE_CACHE, 'synapses':REL_CACHE}
return jsonify(result=res)
@APP.route('/export', methods=["GET"])
def export():
print request.args
exp_format = request.args.get('format', "json", type=str)
nxg = netx.to_netx(NODE_CACHE, REL_CACHE)
if "json" in exp_format:
jsg = netx.to_json(nxg, exp_format)
resp = Response(jsg, mimetype="application/json")
elif "graphml" in exp_format:
text = netx.to_graphml(nxg)
resp = Response(text, mimetype="text/plain")
elif "gml" in exp_format:
text = netx.to_gml(nxg)
resp = Response(text, mimetype="text/plain")
elif "adj" in exp_format:
text = netx.to_adj(nxg)
resp = Response(text, mimetype="text/plain")
return resp
@APP.route('/downloadSvg', methods=["GET"])
def downloadSvg():
print request.args
svg_source = request.args.get('svg-source', "", type=str)
#resp = Response(svg_source, mimetype="svg")
resp = make_response(svg_source)
resp.headers["Content-Disposition"] = "attachment; filename=graph.svg"
return resp
@APP.route('/sigma')
def sigma():
n_sig = neo.neurons_sigma()
s_sig = neo.synapses_sigma(n_sig, 2)
jsn = json.dumps({'nodes':n_sig, 'edges':s_sig})
return render_template('sigma.html', data=jsn)
@APP.route('/login', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
if request.form['username'] != APP.config['USER_NAME']:
error = 'Invalid username'
elif request.form['password'] != APP.config['PASSWORD']:
error = 'Invalid password'
else:
session['logged_in'] = True
flash('You were logged in')
return redirect(url_for('show_runs'))
flash(error)
return redirect(url_for('show_runs'))
@APP.route('/logout')
def logout():
session.pop('logged_in', None)
flash('You were logged out')
return redirect(url_for('show_runs'))
# Autostart
# ------------------------------------------------------------------------------
if __name__ == '__main__':
APP.run(debug=True)