-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedusa.py
573 lines (498 loc) · 22.2 KB
/
medusa.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#!/usr/bin/env python
import os
import sys
import json
import subprocess
import time
import gzip
from flask import Flask, request, session, g, redirect, url_for, abort, \
render_template, flash, escape, Response, send_from_directory
from werkzeug.utils import secure_filename
from utils import check_sequence, checkId, generate_hash
from utils import generate_time_hash
from store import add_job
from store import retrieve_job
from store import cumulative_jobs
from store import unique_ips
from store import unique_emails
import settings
app = Flask(__name__)
# App config from settings.py
app.config.from_object(settings)
# Production settings that override the testing ones
list_id=[]
try:
import production
app.config.from_object(production)
except ImportError:
pass
# Mail log setup
try:
import mail_log as ml
if not app.debug:
import logging
mail_handler = ml.TlsSMTPHandler(ml.MAIL_HOST,
ml.MAIL_FROM,
ml.ADMINS, 'Medusa-webapp Failed!',
credentials=(ml.MAIL_USER,
ml.MAIL_PWD))
mail_handler.setLevel(logging.ERROR)
app.logger.addHandler(mail_handler)
except ImportError:
pass
@app.route('/')
def index():
return render_template('index.html')
# DESCOMMENTARE mettere solo /run
@app.route('/medusa/run', methods=['GET', 'POST'])
def run():
# Here handle submissions and run the analysis
# Send emails on failures, success
# Use redis to store user stats (hashed for privacy)
if request.method == 'POST':
# First things first, compute user hash
# DESCOMMENTARE req_id = generate_time_hash(request.remote_addr)
req_id = "2"
# To avoid slow-downs in the running directory
# create subdirs w/ the first 2 chars of the hash
h2c = req_id[:2]
try:
os.mkdir(os.path.join(app.config['UPLOAD_FOLDER'],
h2c))
except:
pass
# Prepare the working directory
# Our hash scheme ensures that it should be unique
wdir = os.path.join(app.config['UPLOAD_FOLDER'],
h2c, req_id)
wdir = os.path.abspath(wdir)
os.mkdir(wdir)
#MIOCOD
if request.form['aligning_algorithm'] == 'option1':
aligning_algorithm = ''
else:
aligning_algorithm = '-a' #use Minimap align
random = request.form.get('graph_path_rd')
weight = request.form.get('graph_path_wh')
draft = request.files['draft']
#input files checks --SCRIVI LA FUNZIONE IN UTILS.PY
#print ("qui ",draft.filename)
#print ("qui2 ",draft.read())
#print(request.files)
# Save input files
#if draft and allowed_file(draft.filename):
if draft:
filename = secure_filename(draft.filename)
draftFile= os.path.join(wdir, filename)
draft.save(draftFile)
dname = filename
sequence=0
found_new_line = 0
list_id.clear()
if(filename.find('.gz')>-1):
with gzip.open(draftFile,"rt") as file:
for line in file.readlines():
print(line)
if found_new_line ==1:
if line[0]== ">":
flash(u'Something went wrong with your draft genome: empty sequence found',
'danger')
return redirect(url_for('index'))
if line[0]== ">":
found_new_line =1
id = checkId(line)
try:
if(list_id.index(id)>=0):
flash(u'Something went wrong with your draft genome: duplicate id found',
'danger')
return redirect(url_for('index'))
else:
list_id.append(id)
except Exception as e:
list_id.append(id)
else:
found_new_line =0
sequence = check_sequence(line)
if sequence == 1:
flash(u'Something went wrong with your draft genome',
'danger')
return redirect(url_for('index'))
else:
try:
with open(draftFile,"r") as file:
for line in file.readlines():
print(line)
if found_new_line ==1:
if line[0]== ">":
flash(u'Something went wrong with your draft genome: empty sequence found',
'danger')
return redirect(url_for('index'))
if line[0]== ">":
found_new_line =1
id = checkId(line)
try:
if(list_id.index(id)>=0):
flash(u'Something went wrong with your draft genome: duplicate id found',
'danger')
return redirect(url_for('index'))
else:
list_id.append(id)
except Exception as e:
list_id.append(id)
else:
found_new_line =0
sequence = check_sequence(line)
if sequence == 1:
flash(u'Something went wrong with your draft genome',
'danger')
return redirect(url_for('index'))
except Exception as e:
print(e)
flash(u'unrecognized compression type, please use GZIP for deflating your files',
'danger')
return redirect(url_for('index'))
else:
flash(u'Something went wrong with your draft genome',
'danger ')
return redirect(url_for('index'))
# Save the genomes files
genomes = set()
try:
for genome in request.files.getlist('genomes'):
filename = secure_filename(genome.filename)
genomeFile = os.path.join(wdir, filename)
genome.save(genomeFile)
sequence=0
found_new_line = 0
list_id.clear()
if(filename.find('.gz')>-1):
with gzip.open(draftFile,"rt") as file:
for line in file.readlines():
print(line)
if found_new_line ==1:
if line[0]== ">":
flash(u'Something went wrong with your target genome: empty sequence found',
'danger')
return redirect(url_for('index'))
if line[0]== ">":
found_new_line =1
id = checkId(line)
try:
if(list_id.index(id)>=0):
flash(u'Something went wrong with your target genome: duplicate id found',
'danger')
return redirect(url_for('index'))
else:
list_id.append(id)
except Exception as e:
list_id.append(id)
else:
found_new_line =0
sequence = check_sequence(line)
if sequence == 1:
flash(u'Something went wrong with your target genome: one or more sequences contain non DNA characters',
'danger')
return redirect(url_for('index'))
genomes.add(filename)
else:
try:
with open(genomeFile,"r") as file:
for line in file.readlines():
if found_new_line ==1:
if line[0]== ">" :
flash(u'Something went wrong with your target genome: empty sequence found',
'danger')
return redirect(url_for('index'))
if line[0]== ">":
found_new_line =1
id = checkId(line)
try:
if(list_id.index(id)>=0):
flash(u'Something went wrong with your target genome: duplicate id found',
'danger')
return redirect(url_for('index'))
else:
list_id.append(id)
except Exception as e:
list_id.append(id)
else:
found_new_line =0
sequence = check_sequence(line)
if sequence == 1:
print(line)
flash(u'Something went wrong with your target genome: one or more sequences contain non DNA characters',
'danger')
return redirect(url_for('index'))
genomes.add(filename)
except Exception as e:
print(e)
flash(u'unrecognized compression type, please use GZIP for deflating your files',
'danger')
return redirect(url_for('index'))
except Exception as e:
print(e)
flash(u'Something went wrong with your target genomes',
'danger')
return redirect(url_for('index'))
#MIOCOD
genomes.add(aligning_algorithm)
if random:
genomes.add(random)
if weight:
genomes.add(weight)
# Check email, hash it
email = request.form['email']
# DESCOMMENTARE if email:
# DESCOMMENTARE hemail = generate_hash(email)
# DESCOMMENTARE else:
# DESCOMMENTARE flash(u'Something went wrong with your email', 'danger')
# DESCOMMENTARE return redirect(url_for('index'))
# Secure my results?
passphrase = request.form['passphrase']
# DESCOMMENTARE if passphrase:
# DESCOMMENTARE hpass = generate_hash(passphrase)
# DESCOMMENTARE else:
# DESCOMMENTARE hpass = None
# In case of a passphrase, don't bother the current submitter
session['req_id'] = req_id
# Submit the job
# Then redirect to the waiting page
try:
cmd = 'python tasks.py %s %s %s %s' % (req_id,
wdir,
dname,
' '.join(genomes))
f = open(os.path.join(wdir, 'cmd.sh'), 'w')
f.write(cmd + '\n')
f.close()
cmd = 'at -q b -M now -f %s' % os.path.join(wdir, 'cmd.sh')
proc = subprocess.Popen(cmd,
shell=(sys.platform != "win32"),
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out = proc.communicate()
return_code = proc.returncode
if return_code != 0:
raise Exception('%s' % str(out[1]))
except Exception as e:
flash(u'Could not submit your job "%s"' % e,
'danger')
return redirect(url_for('index'))
try:
# Send details to redis
add_job(req_id, request.remote_addr, hemail, hpass)
except Exception as e:
flash(u'Could not save your job details (%s)' % e, 'danger')
return redirect(url_for('index'))
return redirect(url_for('results',
req_id=req_id))
# No POST, return to start
flash(u'No job details given, would you like to start a new one?',
'warning')
return redirect(url_for('index'))
@app.route('/log/<req_id>')
def log(req_id):
# Get details from redis
j = retrieve_job(req_id)
# TODO: avoid access to redirect to results
# Check passphrase
if 'req_id' not in session:
# bother the user
return redirect(url_for('access',
req_id=req_id))
if 'req_id' in session and req_id != escape(session['req_id']):
# clean the session, then bother the user
session.pop('req_id', None)
return redirect(url_for('access',
req_id=req_id))
# Return the log, if present
h2c = req_id[:2]
if not os.path.exists(os.path.join(
app.config['UPLOAD_FOLDER'],
h2c, req_id)):
flash('Could not retrieve the log: is your job older than one week?', 'danger')
return render_template('index.html')
if 'log.txt' not in os.listdir(os.path.join(
app.config['UPLOAD_FOLDER'],
h2c, req_id)):
flash('Could not retrieve the log.txt file', 'danger')
return render_template('error.html', req_id=req_id)
path = os.path.join(app.config['UPLOAD_FOLDER'],
h2c, req_id, 'log.txt')
return Response(''.join(open(path).readlines()),
mimetype='text/plain')
@app.route('/err/<req_id>')
def err(req_id):
# Get details from redis
j = retrieve_job(req_id)
# TODO: avoid access to redirect to results
# Check passphrase
if 'req_id' not in session:
# bother the user
return redirect(url_for('access',
req_id=req_id))
if 'req_id' in session and req_id != escape(session['req_id']):
# clean the session, then bother the user
session.pop('req_id', None)
return redirect(url_for('access',
req_id=req_id))
# Return the log, if present
h2c = req_id[:2]
if not os.path.exists(os.path.join(
app.config['UPLOAD_FOLDER'],
h2c, req_id)):
flash('Could not retrieve the log: is your job older than one week?', 'danger')
return render_template('index.html')
if 'log.err' not in os.listdir(os.path.join(
app.config['UPLOAD_FOLDER'],
h2c, req_id)):
flash('Could not retrieve the log.err file', 'danger')
return render_template('error.html', req_id=req_id)
path = os.path.join(app.config['UPLOAD_FOLDER'],
h2c, req_id, 'log.err')
return Response(''.join(open(path).readlines()),
mimetype='text/plain')
@app.route('/scaffold/<req_id>')
def scaffold(req_id):
# Get details from redis
j = retrieve_job(req_id)
# TODO: avoid access to redirect to results
# Check passphrase
if 'req_id' not in session:
# bother the user
return redirect(url_for('access',
req_id=req_id))
if 'req_id' in session and req_id != escape(session['req_id']):
# clean the session, then bother the user
session.pop('req_id', None)
return redirect(url_for('access',
req_id=req_id))
# Return the log, if present
h2c = req_id[:2]
if not os.path.exists(os.path.join(
app.config['UPLOAD_FOLDER'],
h2c, req_id)):
flash('Could not retrieve the scaffold: is your job older than one week?', 'danger')
return render_template('index.html')
if 'scaffold.fasta' not in os.listdir(os.path.join(
app.config['UPLOAD_FOLDER'],
h2c, req_id)):
flash('Could not retrieve the scaffold file', 'danger')
return render_template('error.html', req_id=req_id)
path = os.path.join(app.config['UPLOAD_FOLDER'],
h2c, req_id)
return send_from_directory(path,
'scaffold.fasta',
as_attachment=True)
@app.route('/results/<req_id>')
def results(req_id):
# Here show the results or the wait page
# Get the right job using the session or the hash key, a la contiguator
# Get details from redis
j = retrieve_job(req_id)
# Check passphrase
if 'req_id' not in session:
# bother the user
return redirect(url_for('access',
req_id=req_id))
if 'req_id' in session and req_id != escape(session['req_id']):
# clean the session, then bother the user
session.pop('req_id', None)
return redirect(url_for('access',
req_id=req_id))
h2c = req_id[:2]
status = j['status']
if status == 'Job done':
# run results logics
try:
result = json.load(open(os.path.join(app.config['UPLOAD_FOLDER'],
h2c, req_id, 'result.json')))
except Exception as e:
app.logger.error(
'Internal server error: %s\nRequest ID: %s' % (e, req_id))
flash(u'Internal server error: %s' % e, 'danger')
return render_template('error.html', req_id=req_id)
return render_template('result.html', req_id=req_id,
data=result)
elif status == 'Job failed':
error_msg = j.get('error', '')
app.logger.error('Internal server error: %s\nRequest ID: %s' % (error_msg,
req_id))
flash(u'Internal server error: %s' % error_msg,
'danger')
return render_template('error.html', req_id=req_id)
else:
# If too much time has passed, it means that the job has either failed
# or anything like that
cur_time = time.time()
start_time = float(j['time'])
delta_time = cur_time - start_time
if (60 * 15) < delta_time < (60 * 30):
flash(u'Your job exceeded 15 minutes, something might have gone wrong!' +
u'Will try 15 more minutes before giving up',
'danger')
elif delta_time > (60 * 30):
flash(u'Your job exceeded 30 minutes, something must have gone wrong!' +
u'If your genomes are many (and big) you might want to run Medusa locally',
'danger')
return render_template('error.html', req_id=req_id)
return render_template('waiting.html', status=status)
@app.route('/access/<req_id>', methods=['GET', 'POST'])
def access(req_id):
# Here ask for a passphrase
# If this is a POST request
# Compare it and redirect accordingly
# Get details from redis
j = retrieve_job(req_id)
# If no passphrase, no need to bother, just redirect
if 'passphrase' not in j:
session['req_id'] = req_id
return redirect(url_for('results',
req_id=req_id))
if request.method == 'POST':
# compare passphrases, after hashing
passphrase = request.form['passphrase']
if passphrase:
hpass = generate_hash(passphrase)
else:
flash(u'Error handling your passphrase', 'danger')
return render_template('access.html', req_id=req_id)
# Compare
if hpass == j['passphrase']:
# Correct!
session['req_id'] = req_id
return redirect(url_for('results',
req_id=req_id))
else:
flash(u'Passphrase does not match', 'danger')
session.pop('req_id', None)
return render_template('access.html', req_id=req_id)
# Redirect to password form
flash('This job is protected by a passphrase', 'info')
return render_template('access.html', req_id=req_id)
@app.route('/stats')
def stats():
return render_template('stats.html')
@app.route('/stats/jobs')
def jobs():
return Response(json.dumps([{'date': x[1],
'jobs':x[0]} for x in cumulative_jobs()]),
mimetype='text/plain')
@app.route('/stats/ips')
def ips():
return Response(json.dumps([{'date': x[1],
'ips':x[0]} for x in unique_ips()]),
mimetype='text/plain')
@app.route('/stats/emails')
def emails():
return Response(json.dumps([{'date': x[1],
'emails':x[0]} for x in unique_emails()]),
mimetype='text/plain')
@app.route('/admin')
def admin():
# Here admin section: upload a new medusa
# Clean manually the jobs flash('Not implemented yet', 'warning')
return render_template('index.html')
if __name__ == '__main__':
app.run()