-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.py
286 lines (255 loc) · 8.97 KB
/
web.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function, division
__version__ = "0.0.1"
import re
import sys
import json
import ast
import urllib
import time
from bottle import route, run, static_file
from bottle import redirect, view, post, request
from bottle import Response
import networkx as nx
from redis import Redis
from rq import Queue
import logging
log = logging.getLogger()
logging.basicConfig(level=logging.DEBUG)
from phylonets import cluster_networks
from phylonets import enewick
# importing the cached calculations
from proxy import cluster_soft, cluster_hard, tree_child_families
DEBUG = False
MAX_SUBTREES = 25*1000
MAX_LABEL = 12
MAX_QUOTED = 4400
index_examples = [
# name, clusters
('Remove edge, 2 tree-child',
urllib.quote_plus("(1, 2), (3, 4), (4, 5), (1, 2, 3), (3, 4, 5), (1, 2, 3, 4)"),),
('Remove edge, 1 tree-child',
urllib.quote_plus("(1, 2), (2, 3), (3, 4), (3, 4, 5), (1, 2, 3), (1, 2, 3, 4)"),),
('Remove edge, non tree-child',
urllib.quote_plus("(1, 2), (2, 3, 4), (4, 5), (2, 4), (1, 2, 3, 4)"),),
('Little phylo tree',
#"(((((HUMAN,PANTR),MACMU),CANFA),(MOUSE,RAT)),CHICK);"
urllib.quote_plus("'CANFA,CHICK,HUMAN,MACMU,MOUSE,PANTR,RAT', 'HUMAN,MACMU,PANTR', 'HUMAN,PANTR', 'CANFA', 'MACMU', 'CANFA,HUMAN,MACMU,MOUSE,PANTR,RAT', 'CHICK', 'PANTR', 'MOUSE,RAT', 'RAT', 'CANFA,HUMAN,MACMU,PANTR', 'HUMAN', 'MOUSE'"), ),
]
def get_dot(G_orig):
"""Change labels and colors to be presented with graphviz"""
G = G_orig.copy()
cluster_networks.relabel_graph(G)
tr = {}
for node in G.nodes_iter():
tr[node] = '"{}"'.format(node)
nx.relabel_nodes(G, tr, copy=False)
for node in G.nodes_iter():
label = str(node)
if len(label) > MAX_LABEL:
label = u'{}..."'.format(label[:MAX_LABEL])
G.node[node]['label'] = label
for node in cluster_networks.get_leaf_nodes(G):
G.node[node]['color'] = "blue"
for node in cluster_networks.hybrid_nodes(G):
G.node[node]['color'] = "#7BFF74" # light green
G.node[node]['style'] = 'filled'
for node in cluster_networks.get_root_nodes(G):
G.node[node]['color'] = "orange"
for node in cluster_networks.problematic_treechild_nodes(G):
G.node[node]['color'] = "#FF77EB" # light pink
G.node[node]['style'] = 'filled'
for u, v in cluster_networks.removable_edges(G):
G.edge[u][v]['color'] = "red"
for root in cluster_networks.get_root_nodes(G):
G.node[root]['label'] = '"R"'
dot = nx.to_pydot(G).to_string()
return dot.strip()
@route("/cluster/<cluster>")
@view('templates/network')
def process_cluster(cluster):
network = get_network_from_cluster(cluster)
hard = cluster_hard(network)
subtrees = cluster_networks.potential_number_of_calls(network)
soft_too_expensive = subtrees > MAX_SUBTREES
async = not DEBUG
q = Queue(connection=Redis(), default_timeout=60*15, async=async)
if not soft_too_expensive:
soft_job = q.enqueue(cluster_soft, network)
soft_job_id = soft_job.id
else:
soft_job_id = None
if not cluster_networks.is_treechild(network):
families_job = q.enqueue(tree_child_families, network)
families_job_id = families_job.id
else:
families_job_id = None
G = network
return {
'cluster': cluster,
'network_dot': get_dot(G),
'network_ascii': network.nodes(),
'network_hard': hard,
'subtrees': subtrees,
'queue_key': q.key,
'soft_job_id': soft_job_id,
'soft_too_expensive': 1 if soft_too_expensive else 0,
'families_job_id': families_job_id,
'is_treechild': cluster_networks.is_treechild(G),
'number_nodes': len(G.nodes()),
'number_edges': len(G.edges()),
'number_hybrids': len(cluster_networks.hybrid_nodes(G)),
'number_leafs': len(cluster_networks.get_leaf_nodes(G)),
'number_conflictive_nodes': len(cluster_networks.problematic_treechild_nodes(G)),
'number_removable_edges': len(cluster_networks.removable_edges(G)),
'hybridization_degree': cluster_networks.hybridization_degree(G),
}
###
# Inputs
@post("/input/upload/")
def process_from_upload():
file_content = request.forms.optionsRadios
data = request.files.data
if file_content and data and data.file:
raw = data.file.read() # This is dangerous for big files
if file_content == 'cluster':
return process_clusters(raw)
elif file_content == 'enewick':
return process_enewick(raw)
redirect("/?error=upload")
@post("/input/cluster/")
def process_cluster_post():
clusters = request.forms.clusters
return process_clusters(clusters)
def process_clusters(clusters):
clusters = re.sub("\s+", "", clusters)
quoted = urllib.quote_plus(clusters)
redirect("/cluster/"+quoted)
@post("/input/enewick/")
def process_eNewick_post():
phrase = request.forms.enewick
return process_enewick(phrase)
def process_enewick(phrase):
try:
g = enewick.enewick_to_phylonet(phrase)
except enewick.MalformedNewickException:
redirect("/?error=enewick")
trans = {}
for node in g.nodes():
if "#" in node:
new_node = node.replace("#", "")
trans[node] = new_node
nx.relabel_nodes(g, trans, copy=False)
clusters = re.sub("\s+", "", str(cluster_hard(g)))
quoted = urllib.quote_plus(clusters)
redirect("/cluster/"+quoted)
def get_network_from_cluster(cluster):
if len(cluster) > MAX_QUOTED:
redirect("/?error=too_long")
unquoted = urllib.unquote_plus(cluster)
try:
clusters = ast.literal_eval(unquoted)
except (SyntaxError, ValueError):
redirect("/?error=syntax")
network = cluster_networks.construct(clusters)
return network
###
# Downloads
@route("/network/<cluster>/hard/download")
def download_hard(cluster):
g = get_network_from_cluster(cluster)
hard = cluster_hard(g)
s = str(hard)
r = Response(body=s, status=200)
r.set_header('Content-Type', 'text/txt')
r.set_header('Content-Disposition', 'attachment; filename="phylonetwork_soft.txt"')
return r
@route("/network/<cluster>/soft/download")
def download_soft(cluster):
g = get_network_from_cluster(cluster)
async = not DEBUG
q = Queue(connection=Redis(), default_timeout=60*15, async=async)
job = q.enqueue(cluster_soft, g)
for i in range(10):
if not job.result:
time.sleep(5)
else:
break
else:
redirect("/?error='problem generating the file'")
s = str(job.result)
r = Response(body=s, status=200)
r.set_header('Content-Type', 'text/txt')
r.set_header('Content-Disposition', 'attachment; filename="phylonetwork_soft.txt"')
return r
###
# Queues and jobs
@route("/job/families/<queue_key>/<job_id>")
def process_job_treechild(queue_key, job_id):
redis_connection = Redis()
q = Queue.from_queue_key(queue_key, redis_connection)
job = q.safe_fetch_job(job_id)
if job.result != None:
# This construction may seem weird but the tree-child families may return
# empty so we cannot just check against «if job.result»
if not job.result:
return {'status': 'done', 'value': "", }
# make web ready and then transform to pydot
value = [get_dot(g) for g in job.result]
return {'status': 'done',
'value': value, }
else:
return {'status': 'pending'}
@route("/job/soft/<queue_key>/<job_id>")
def process_job_soft(queue_key, job_id):
redis_connection = Redis()
q = Queue.from_queue_key(queue_key, redis_connection)
job = q.safe_fetch_job(job_id)
if job.result:
value = json.dumps(list(job.result))
return {'status': 'done',
'value': value, }
else:
return {'status': 'pending'}
###
@route("/")
@view('templates/index')
def repr_network():
global index_examples
vals = { 'examples': index_examples, }
error = request.params.get('error', None)
if error:
msg = "Unkown error"
if error == 'too_long':
msg = "The input is too long"
elif error == 'upload':
msg = "Error during the upload; please check the file content."
elif error == 'syntax':
msg = "Incorrect input; please check the syntax"
elif error == 'enewick':
msg = "Incorrect eNewick; please check the ending «;»"
vals['error'] = msg
return vals
@route("/help")
@view('templates/help')
def help():
return {}
@route("/about")
@view('templates/about')
def about():
return {}
@route("/clusters/")
@route("/cluster/")
@route("/input/")
def redirect_main():
redirect("/")
@route('/static/<filepath:path>')
def server_static(filepath):
return static_file(filepath, root='./static')
if __name__ == "__main__":
if len(sys.argv) > 1:
DEBUG = True
run(reloader=True, debug=DEBUG, port=8000)
else:
run(server='gunicorn', port=8000, workers=5)