-
Notifications
You must be signed in to change notification settings - Fork 2
/
contiguator.py
616 lines (548 loc) · 23.8 KB
/
contiguator.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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
#!/usr/bin/env python
import os
import sys
import subprocess
import json
import time
from flask import Flask, request, session, g, redirect, url_for, abort, \
render_template, flash, escape, Response, send_from_directory, send_file
from werkzeug.utils import secure_filename
from Bio import SeqIO
from utils import 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
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, 'CONTIGuator-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')
@app.route('/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
req_id = generate_time_hash(request.remote_addr)
# 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)
# Sanity check for form entries
float_entries = ['maxtemp', 'mingc', 'mintemp',
'maxgc', 'opttemp', 'multitreshold',
'evalue', 'optgc']
int_entries = ['contigcoverage',
'minsize', 'threads',
'exclude', 'contiglength',
'optsize',
'flanksize', 'maxsize', 'minprod', 'maxprod',
'numN', 'hitlength', 'gcclamp']
bool_entries = ['pcr', 'blastn', 'non']
str_entries = ['email', 'jobname']
for f, entries in ((float, float_entries),
(str, str_entries),
(bool, bool_entries),
(int, int_entries)):
for e in entries:
try:
if e in request.form:
f(request.form[e])
except Exception as e:
flash(u'Something went wrong while processing your options %s'%e,
'danger')
return redirect(url_for('index'))
# Save input files
draft = request.files['contigs']
if draft:
filename = secure_filename(draft.filename)
draft.save(os.path.join(wdir, filename))
dname = filename
else:
flash(u'Forgot to provide your draft genome?',
'danger')
return redirect(url_for('index'))
# Save the genomes files
genomes = set()
try:
for genome in request.files.getlist('reference'):
filename = secure_filename(genome.filename)
genome.save(os.path.join(wdir, filename))
genomes.add(filename)
except:
flash(u'Forgot to provide your reference genome?',
'danger')
return redirect(url_for('index'))
# Save the genomes files
ptts = request.files['pttfile']
if ptts:
try:
for ptt in request.files.getlist('pttfile'):
filename = secure_filename(ptt.filename)
ptt.save(os.path.join(wdir, filename))
except:
flash(u'Something went wrong with your ptt files',
'danger')
return redirect(url_for('index'))
# Check email, hash it
email = request.form['email']
if email:
hemail = generate_hash(email)
else:
flash(u'Something went wrong with your email', 'danger')
return redirect(url_for('index'))
# Submit the job
# Then redirect to the waiting page
try:
# Handle the bool flags
if 'non' in request.form:non = True
else:non = False
if 'pcr' in request.form:pcr = True
else:pcr = False
if 'inner' in request.form:inner = True
else:inner = False
if 'blastn' in request.form:blastn = True
else:blastn = False
evalue=request.form.get('evalue', 1e-20)
contiglength=request.form.get('contiglength', 1000)
contigcoverage=request.form.get('contigcoverage', 20)
hitlength=request.form.get('hitlength', 1100)
multitreshold=request.form.get('multitreshold', 1.5)
non=non
numN=request.form.get('numN', 100)
pcr=pcr
inner=inner
blastn=blastn
threads=request.form.get('threads', 1)
optsize=request.form.get('optsize', 20)
minsize=request.form.get('minsize', 18)
maxsize=request.form.get('maxsize', 27)
opttemp=request.form.get('opttemp', 60)
mintemp=request.form.get('mintemp', 57)
maxtemp=request.form.get('maxtemp', 63)
flanksize=request.form.get('flanksize', 1000)
minprod=request.form.get('minprod', 1000)
maxprod=request.form.get('maxprod', 7000)
optgc=request.form.get('optgc', 50)
mingc=request.form.get('mingc', 20)
maxgc=request.form.get('maxgc', 80)
gcclamp=request.form.get('gcclamp', 1)
exclude=request.form.get('exclude', 100)
jobname=request.form.get('jobname', 'CONTIGuator job')
cmd = 'python tasks.py %s %s %s ' % (req_id,
wdir,
dname)
cmd += '%s %s %s %s ' % (evalue, contiglength, contigcoverage, hitlength)
cmd += '%s %s %s %s %s %s ' % (multitreshold, non, numN, pcr, inner, blastn)
cmd += '%s %s %s %s %s %s ' % (threads, optsize, minsize, maxsize, opttemp, mintemp)
cmd += '%s %s %s %s %s %s %s ' % (maxtemp, flanksize, minprod, maxprod, optgc, mingc, maxgc)
cmd += '%s %s "%s" ' % (gcclamp, exclude, jobname)
cmd += ' '.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)
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)
# 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 'CONTIGuator.log' 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, 'CONTIGuator.log')
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)
# 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('/archive/<req_id>')
def archive(req_id):
# Get details from redis
j = retrieve_job(req_id)
# Return the archive, 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 archive: is your job older than one week?', 'danger')
return render_template('index.html')
if 'CONTIGuator_results.tar.gz' not in os.listdir(os.path.join(
app.config['UPLOAD_FOLDER'],
h2c, req_id)):
flash('Could not retrieve the archive', '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,
'CONTIGuator_results.tar.gz',
as_attachment=True)
@app.route('/unmapped/<req_id>')
def unmapped(req_id):
# Get details from redis
j = retrieve_job(req_id)
# Return the file, 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 file: is your job older than one week?', 'danger')
return render_template('index.html')
if not os.path.exists(os.path.join(
app.config['UPLOAD_FOLDER'],
h2c, req_id, 'UnMappedContigs')):
flash('No contig has been mapped to yor genome (or error occurred)); you'+
'may want to relaunch a new job with different parameters', 'danger')
return render_template('index.html')
if 'UnMappedContigs.txt' not in os.listdir(os.path.join(
app.config['UPLOAD_FOLDER'],
h2c, req_id, 'UnMappedContigs')):
flash('Could not retrieve the UnMappedContigs.txt file',
'danger')
return render_template('error.html', req_id=req_id)
path = os.path.join(app.config['UPLOAD_FOLDER'],
h2c, req_id, 'UnMappedContigs',
'UnMappedContigs.txt')
return Response(''.join(open(path).readlines()),
mimetype='text/plain')
@app.route('/mapped/<req_id>/<mdir>')
def mapped(req_id, mdir):
# Get details from redis
j = retrieve_job(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 file: is your job older than one week?', 'danger')
return render_template('index.html')
if 'MappedContigs.txt' not in os.listdir(os.path.join(
app.config['UPLOAD_FOLDER'],
h2c, req_id, mdir)):
flash('Could not retrieve the MappedContigs.txt file',
'danger')
return render_template('error.html', req_id=req_id)
path = os.path.join(app.config['UPLOAD_FOLDER'],
h2c, req_id, mdir, 'MappedContigs.txt')
return Response(''.join(open(path).readlines()),
mimetype='text/plain')
@app.route('/pdf/<req_id>/<mdir>/<fname>')
def pdf(req_id, mdir, fname):
# Get details from redis
j = retrieve_job(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 document: is your job older than one week?', 'danger')
return render_template('index.html')
if fname not in os.listdir(os.path.join(
app.config['UPLOAD_FOLDER'],
h2c, req_id, mdir)):
flash('Could not retrieve the %s file'%fname,
'danger')
return render_template('error.html', req_id=req_id)
path = os.path.join(app.config['UPLOAD_FOLDER'],
h2c, req_id, mdir, fname)
return send_file(path, mimetype='application/pdf')
@app.route('/png/<req_id>/<mdir>/<fname>')
def png(req_id, mdir, fname):
# Get details from redis
j = retrieve_job(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 picture: is your job older than one week?', 'danger')
return render_template('index.html')
if fname not in os.listdir(os.path.join(
app.config['UPLOAD_FOLDER'],
h2c, req_id, mdir)):
flash('Could not retrieve the %s file'%fname,
'danger')
return render_template('error.html', req_id=req_id)
path = os.path.join(app.config['UPLOAD_FOLDER'],
h2c, req_id, mdir, fname)
return send_file(path, mimetype='image/png')
@app.route('/scaffold/<req_id>/<mdir>')
def scaffold(req_id, mdir):
# Get details from redis
j = retrieve_job(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 'PseudoContig.fsa' not in os.listdir(os.path.join(
app.config['UPLOAD_FOLDER'],
h2c, req_id, mdir)):
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, mdir, 'PseudoContig.fsa')
return Response(''.join(open(path).readlines()),
mimetype='text/plain')
@app.route('/pcr/<req_id>/<mdir>')
def pcr(req_id, mdir):
# Get details from redis
j = retrieve_job(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 file: is your job older than one week?', 'danger')
return render_template('index.html')
if 'PCRPrimers.tsv' not in os.listdir(os.path.join(
app.config['UPLOAD_FOLDER'],
h2c, req_id, mdir)):
flash('Could not retrieve the PCR summary file',
'danger')
return render_template('error.html', req_id=req_id)
path = os.path.join(app.config['UPLOAD_FOLDER'],
h2c, req_id, mdir, 'PCRPrimers.tsv')
return Response(''.join(open(path).readlines()),
mimetype='text/plain')
@app.route('/legend')
def legend():
return render_template('legend.html')
@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
# Get details from redis
j = retrieve_job(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)
# Handle the results and prepare the data for page rendering
wdir = os.path.join(app.config['UPLOAD_FOLDER'],
req_id[:2], req_id)
wdir = os.path.abspath(wdir)
# Summary data
genparams = []
if 'genparams.tsv' in os.listdir(wdir):
for l in open(os.path.join(wdir, 'genparams.tsv')):
s = l.strip().split('\t')
if len(s) == 1:
genparams.append((s[0], ''))
else:
genparams.append((s[0], s[1]))
# Run parameters
runparams = []
if 'runparams.tsv' in os.listdir(wdir):
for l in open(os.path.join(wdir, 'runparams.tsv')):
s = l.strip().split('\t')
if len(s) == 1:
runparams.append((s[0], ''))
else:
runparams.append((s[0], s[1]))
# PCR parameters
pcrparams = []
if 'pcrparams.tsv' in os.listdir(wdir):
for l in open(os.path.join(wdir, 'pcrparams.tsv')):
s = l.strip().split('\t')
if len(s) == 1:
pcrparams.append((s[0], ''))
else:
pcrparams.append((s[0], s[1]))
# Summary stats
summary = []
if 'summary.tsv' in os.listdir(wdir):
b = True
for l in open(os.path.join(wdir, 'summary.tsv')):
if b:
b = False
continue
summary.append(l.strip().split('\t'))
# No summary stats = errors? (or unmapped)
if len(summary) == 0:
return render_template('nomapped.html', req_id=req_id)
# Are there any Map_ directories there?
maps = filter(lambda x: os.path.isdir(
os.path.join(wdir, x)) and
x.startswith('Map_'),
os.listdir(wdir))
# Collect data for each mapped reference replicon
mapdata = []
for m in maps:
d = {}
d['dir'] = m
# Reference length
d['reflen'] = len(SeqIO.read(open(os.path.join(wdir,
m, 'Reference.embl')),'embl'))
# Mapped contigs
mcontigs = [int(x.strip().split('\t')[1]) for x in open(os.path.join(wdir,
m, 'MappedContigs.txt'))]
d['mapped'] = len(mcontigs)
d['mappedbp'] = sum(mcontigs)
# PDF/PNG map
d['png'] = list(filter(lambda x: x.endswith('.png') and
not x.endswith('_small.png'),
os.listdir(os.path.join(wdir, m))))[0]
d['pdf'] = d['png'].rstrip('.png')
# PCR?
if 'PCRPrimers.tsv' in os.listdir(os.path.join(wdir, m)):
d['pcr'] = len(filter(lambda x: len(x) >= 5 and
'Left Contig' not in x[0],
[x.rstrip().split('\t')
for x in open(os.path.join(wdir, m,
'PCRPrimers.tsv'))]))
# Wrap it all together, keep the same order
mapdata.append(d)
return render_template('result.html', req_id=req_id,
mapdata=mapdata,
genparams=genparams,
runparams=runparams,
pcrparams=pcrparams,
summary=summary,
maps=maps)
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('/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()