-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcommand.py
More file actions
651 lines (601 loc) · 21.6 KB
/
command.py
File metadata and controls
651 lines (601 loc) · 21.6 KB
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
"""
main workflow
"""
# BUILT-INS
import sys
import os
import datetime
import argparse
# varVAMP
from varvamp.scripts import alignment
from varvamp.scripts import config
from varvamp.scripts import consensus
from varvamp.scripts import regions
from varvamp.scripts import logging
from varvamp.scripts import param_estimation
from varvamp.scripts import primers
from varvamp.scripts import qpcr
from varvamp.scripts import reporting
from varvamp.scripts import scheme
from varvamp.scripts import blast
from varvamp import __version__
def get_args(sysargs):
"""
arg parsing for varvamp
"""
parser = argparse.ArgumentParser(
usage='''\tvarvamp <mode> --help\n\tvarvamp <mode> [mode optional arguments] <alignment> <output dir>''')
mode_parser = parser.add_subparsers(
title="varvamp mode",
dest="mode",
)
SINGLE_parser = mode_parser.add_parser(
"single",
help="design primers for single amplicons",
usage="varvamp single [optional arguments] <alignment> <output dir>"
)
TILED_parser = mode_parser.add_parser(
"tiled",
help="design primers for whole genome sequencing",
usage="varvamp tiled [optional arguments] <alignment> <output dir>"
)
QPCR_parser = mode_parser.add_parser(
"qpcr",
help="design qPCR primers",
usage="varvamp qpcr [optional arguments] <alignment> <output dir>"
)
parser.add_argument(
"input",
nargs=2,
help="alignment file and dir to write results"
)
for par in (SINGLE_parser, TILED_parser, QPCR_parser):
par.add_argument(
"-a",
"--n-ambig",
metavar="2",
type=int,
default=2,
help="max number of ambiguous characters in a primer"
)
par.add_argument(
"-db",
"--database",
help="location of the BLAST db",
metavar="None",
type=str,
default=None
)
par.add_argument(
"-th",
"--threads",
help="number of threads for BLAST search and deltaG calculations",
metavar="1",
type=int,
default=1
)
par.add_argument(
"--name",
help="name of the scheme",
metavar="varVAMP",
type=str,
default="varVAMP"
)
par.add_argument(
"--compatible-primers",
metavar="None",
type=str,
default=None,
help="FASTA primer file with which new primers should not form dimers. Sequences >40 nt are ignored. Can significantly increase runtime."
)
for par in (SINGLE_parser, TILED_parser):
par.add_argument(
"-t",
"--threshold",
metavar="",
type=float,
default=None,
help="consensus threshold (0-1) - if not set it will be estimated (higher values result in higher specificity at the expense of found primers)"
)
par.add_argument(
"-ol",
"--opt-length",
help="optimal length of the amplicons",
metavar="1000",
type=int,
default=1000
)
par.add_argument(
"-ml",
"--max-length",
help="max length of the amplicons",
metavar="1500",
type=int,
default=1500
)
TILED_parser.add_argument(
"-o",
"--overlap",
type=int,
metavar="25",
default=25,
help="min overlap of the amplicon inserts"
)
SINGLE_parser.add_argument(
"-n",
"--report-n",
type=int,
metavar="inf",
default=float("inf"),
help="report the top n best hits"
)
QPCR_parser.add_argument(
"-pa",
"--pn-ambig",
metavar="1",
type=int,
default=None,
help="max number of ambiguous characters in a probe"
)
QPCR_parser.add_argument(
"-t",
"--threshold",
metavar="0.9",
type=float,
default=0.9,
help="consensus threshold (0-1) - higher values result in higher specificity at the expense of found primers"
)
QPCR_parser.add_argument(
"-n",
"--test-n",
type=int,
metavar="50",
default=50,
help="test the top n qPCR amplicons for secondary structures at the minimal primer temperature"
)
QPCR_parser.add_argument(
"-d",
"--deltaG",
type=int,
metavar="-3",
default=-3,
help="minimum free energy (kcal/mol/K) cutoff at the lowest primer melting temperature"
)
parser.add_argument(
"--verbose",
action=argparse.BooleanOptionalAction,
default=True,
help="show varvamp console output"
)
parser.add_argument(
"-v",
"--version",
action='version',
version=f"varvamp {__version__}"
)
if len(sysargs) < 1:
parser.print_help()
sys.exit(-1)
else:
return parser.parse_args(sysargs)
def shared_workflow(args, log_file):
"""
part of the workflow that is shared by all modes
"""
# start varvamp
logging.varvamp_progress(log_file, mode=args.mode)
# read in alignment and preprocess
preprocessed_alignment = alignment.preprocess(args.input[0])
# read in external primer sequences with which new primers should not form dimers
if args.compatible_primers is not None:
compatible_primers = primers.parse_primer_fasta(args.compatible_primers)
else:
compatible_primers = None
# check alignment length and number of gaps and report if its significantly more/less than expected
logging.check_alignment_length(preprocessed_alignment, log_file)
logging.check_gaped_sequences(preprocessed_alignment, log_file)
# estimate threshold or number of ambiguous bases if args were not supplied
if args.threshold is None and not args.mode == 'qpcr':
args.threshold = param_estimation.get_parameters(preprocessed_alignment, args, log_file)
# set the number of ambiguous chars for qPCR probes to one less than for primers if not given
if args.mode == "qpcr" and args.pn_ambig is None:
if args.n_ambig == 0:
args.pn_ambig = 0
else:
args.pn_ambig = args.n_ambig - 1
with open(log_file, "a") as f:
print(f"Automatic parameter selection set -pa {args.pn_ambig}.", file=f)
# check arguments
logging.raise_arg_errors(args, log_file)
# config check
logging.confirm_config(args, log_file)
logging.varvamp_progress(
log_file,
progress=0.1,
job="Checking config.",
progress_text="config file passed"
)
# clean alignment of gaps
alignment_cleaned, gaps_to_mask = alignment.process_alignment(
preprocessed_alignment,
args.threshold,
)
logging.varvamp_progress(
log_file,
progress=0.2,
job="Preprocessing alignment and cleaning gaps.",
progress_text=f"{len(gaps_to_mask)} gaps with {alignment.calculate_total_masked_gaps(gaps_to_mask)} nucleotides"
)
# create consensus sequences
majority_consensus, ambiguous_consensus = consensus.create_consensus(
alignment_cleaned,
args.threshold
)
logging.varvamp_progress(
log_file,
progress=0.3,
job="Creating consensus sequences.",
progress_text=f"length of the consensus is {len(majority_consensus)} nt"
)
# generate primer region list
primer_regions = regions.find_regions(
ambiguous_consensus,
args.n_ambig
)
if not primer_regions:
logging.raise_error(
"no primer regions found. Lower the threshold!",
log_file,
exit=True
)
logging.varvamp_progress(
log_file,
progress=0.4,
job="Finding primer regions.",
progress_text=f"{regions.mean(primer_regions, majority_consensus)} % of the consensus sequence will be evaluated for primers"
)
# produce kmers for all primer regions
kmers = regions.produce_kmers(
primer_regions,
majority_consensus
)
logging.varvamp_progress(
log_file,
progress=0.5,
job="Digesting into kmers.",
progress_text=f"{len(kmers)} kmers"
)
# find potential primers
left_primer_candidates, right_primer_candidates = primers.find_primers(
kmers,
ambiguous_consensus,
alignment_cleaned
)
for primer_type, primer_candidates in [("+", left_primer_candidates), ("-", right_primer_candidates)]:
if not primer_candidates:
logging.raise_error(
f"no {primer_type} primers found.\n",
log_file,
exit=True
)
logging.varvamp_progress(
log_file,
progress=0.6,
job="Filtering for primers.",
progress_text=f"{len(left_primer_candidates)} fw and {len(right_primer_candidates)} rv potential primers"
)
# filter primers against non-dimer sequences if provided, can use multi-processing
if compatible_primers is not None:
left_primer_candidates = primers.filter_non_dimer_candidates(
left_primer_candidates, compatible_primers, args.threads
)
right_primer_candidates = primers.filter_non_dimer_candidates(
right_primer_candidates, compatible_primers, args.threads
)
logging.varvamp_progress(
log_file,
progress=0.65,
job="Filtering primers against provided primers.",
progress_text=f"{len(left_primer_candidates)} fw and {len(right_primer_candidates)} rv primers after filtering"
)
return alignment_cleaned, majority_consensus, ambiguous_consensus, primer_regions, left_primer_candidates, right_primer_candidates, compatible_primers
def single_and_tiled_shared_workflow(args, left_primer_candidates, right_primer_candidates, data_dir, log_file):
"""
part of the workflow shared by the single and tiled mode
"""
# find best primers and create primer dict
all_primers = primers.find_best_primers(left_primer_candidates, right_primer_candidates)
logging.varvamp_progress(
log_file,
progress=0.7,
job="Considering primers with low penalties.",
progress_text=f"{len(all_primers['+'])} fw and {len(all_primers['-'])} rv primers"
)
# find all possible amplicons
amplicons = scheme.find_amplicons(
all_primers,
args.opt_length,
args.max_length
)
if not amplicons:
logging.raise_error(
"no amplicons found. Increase the max amplicon length or number of ambiguous bases or lower threshold!\n",
log_file,
exit=True
)
logging.varvamp_progress(
log_file,
progress=0.8,
job="Finding potential amplicons.",
progress_text=f"{len(amplicons)} potential amplicons"
)
if args.database is not None:
# create blast query
query_path = blast.create_BLAST_query(amplicons, data_dir)
# perform primer blast
amplicons = blast.primer_blast(
data_dir,
args.database,
query_path,
amplicons,
args.max_length,
args.threads,
log_file,
mode="single_tiled"
)
return all_primers, amplicons
def single_workflow(args, amplicons, log_file):
"""
workflow part specific for single mode
"""
amplicon_scheme = scheme.find_single_amplicons(amplicons, args.report_n)
logging.varvamp_progress(
log_file,
progress=0.9,
job="Finding amplicons with low penalties.",
progress_text=f"{len(amplicon_scheme)} amplicons."
)
return amplicon_scheme
def tiled_workflow(args, amplicons, left_primer_candidates, right_primer_candidates, all_primers, ambiguous_consensus, log_file, results_dir):
"""
part of the workflow specific for the tiled mode
"""
# create graph
amplicon_graph = scheme.create_amplicon_graph(amplicons, args.overlap)
# search for amplicon scheme
coverage, amplicon_scheme = scheme.find_best_covering_scheme(
amplicons,
amplicon_graph
)
# check for dimers
dimers_not_solved = scheme.check_and_solve_heterodimers(
amplicon_scheme,
left_primer_candidates,
right_primer_candidates,
all_primers)
if dimers_not_solved:
logging.raise_error(
f"varVAMP found {len(dimers_not_solved)} primer dimers without replacements. Check the dimer file and perform the PCR for incomaptible amplicons in a sperate reaction.",
log_file
)
reporting.write_dimers(results_dir, dimers_not_solved)
# evaluate coverage
# ATTENTION: Genome coverage of the scheme might still change slightly through resolution of primer dimers, but this potential, minor inaccuracy is currently accepted.
percent_coverage = round(coverage/len(ambiguous_consensus)*100, 2)
logging.varvamp_progress(
log_file,
progress=0.9,
job="Creating amplicon scheme.",
progress_text=f"{percent_coverage} % total coverage with {len(amplicon_scheme)} amplicons"
)
if percent_coverage < 70:
logging.raise_error(
"coverage < 70 %. Possible solutions:\n"
"\t - lower threshold\n"
"\t - increase amplicons lengths\n"
"\t - increase number of ambiguous nucleotides\n"
"\t - relax primer settings (not recommended)\n",
log_file
)
return amplicon_scheme
def qpcr_workflow(args, data_dir, alignment_cleaned, ambiguous_consensus, majority_consensus, left_primer_candidates, right_primer_candidates, compatible_primers, log_file):
"""
part of the workflow specific for the tiled mode
"""
# find regions for qPCR probes
probe_regions = regions.find_regions(
ambiguous_consensus,
args.pn_ambig
)
if not probe_regions:
logging.raise_error(
"no regions that fullfill probe criteria! lower threshold or increase number of ambiguous chars in probe\n",
log_file,
exit=True
)
# digest probe regions
probe_kmers = regions.produce_kmers(
probe_regions,
majority_consensus,
config.QPROBE_SIZES
)
# find potential probes
qpcr_probes = qpcr.get_qpcr_probes(probe_kmers, ambiguous_consensus, alignment_cleaned)
if not qpcr_probes:
logging.raise_error(
"no qpcr probes found\n",
log_file,
exit=True
)
logging.varvamp_progress(
log_file,
progress=0.7,
job="Finding qPCR probes.",
progress_text=f"{len(qpcr_probes)} potential qPCR probes"
)
# filter primers against non-dimer sequences if provided
if compatible_primers is not None:
qpcr_probes = primers.filter_non_dimer_candidates(
qpcr_probes, compatible_primers, args.threads)
logging.varvamp_progress(
log_file,
progress=0.75,
job="Filtering probes against provided primer sequences.",
progress_text=f"{len(qpcr_probes)} potential qPCR probes after filtering"
)
# find unique amplicons with a low penalty and an internal probe
qpcr_scheme_candidates = qpcr.find_qcr_schemes(qpcr_probes, left_primer_candidates, right_primer_candidates, majority_consensus, ambiguous_consensus)
if not qpcr_scheme_candidates:
logging.raise_error(
"no qPCR scheme candidates found. lower threshold or increase number of ambiguous chars in primer and/or probe\n",
log_file,
exit=True
)
logging.varvamp_progress(
log_file,
progress=0.8,
job="Finding unique amplicons with probe.",
progress_text=f"{len(qpcr_scheme_candidates)} unique amplicons with internal probe"
)
# run blast if db is given
if args.database is not None:
# create blast query
query_path = blast.create_BLAST_query(qpcr_scheme_candidates, data_dir, mode="qpcr")
# perform primer blast
qpcr_scheme_candidates = blast.primer_blast(
data_dir,
args.database,
query_path,
qpcr_scheme_candidates,
config.QAMPLICON_LENGTH[1],
args.threads,
log_file,
mode="qpcr"
)
# test amplicons for deltaG
final_schemes = qpcr.test_amplicon_deltaG_parallel(qpcr_scheme_candidates, majority_consensus, args.test_n, args.deltaG, args.threads)
if not final_schemes:
logging.raise_error(
"no qPCR amplicon passed the deltaG threshold\n",
log_file,
exit=True
)
logging.varvamp_progress(
log_file,
progress=0.9,
job="Filtering amplicons for deltaG.",
progress_text=f"{len(final_schemes)} non-overlapping qPCR schemes that passed deltaG cutoff"
)
return probe_regions, final_schemes
def main():
"""
main varvamp workflow
"""
# start varVAMP
args = get_args(sys.argv[1:])
if not args.verbose:
sys.stdout = open(os.devnull, 'w')
start_time = datetime.datetime.now()
results_dir, data_dir, log_file = logging.create_dir_structure(args.input[1])
# check if blast is installed
if args.database is not None:
blast.check_BLAST_installation(log_file)
# mode unspecific part of the workflow
alignment_cleaned, majority_consensus, ambiguous_consensus, primer_regions, left_primer_candidates, right_primer_candidates, compatible_primers = shared_workflow(args, log_file)
# write files that are shared in all modes
reporting.write_regions_to_bed(primer_regions, args.name, data_dir)
reporting.write_alignment(data_dir, alignment_cleaned)
reporting.write_fasta(data_dir, f"majority_consensus", f"{args.name}_majority_consensus",majority_consensus)
reporting.write_fasta(results_dir, f"ambiguous_consensus", f"{args.name}_ambiguous_consensus", ambiguous_consensus)
# Functions called from here on return lists of amplicons that are refined step-wise into final schemes.
# These lists that are passed between functions and later used for reporting consist of dictionary elemnts,
# which represent individual amplicons. A minimal amplicon dict could take the form:
# {
# "id": amplicon_name,
# "penalty": amplicon_cost,
# "length": amplicon_length,
# "LEFT": [left primer data],
# "RIGHT": [right primer data]
# }
# to which different functions may add additional information.
# SINGLE/TILED mode
if args.mode == "tiled" or args.mode == "single":
all_primers, amplicons = single_and_tiled_shared_workflow(
args,
left_primer_candidates,
right_primer_candidates,
data_dir,
log_file
)
if args.mode == "single":
amplicon_scheme = single_workflow(
args,
amplicons,
log_file
)
elif args.mode == "tiled":
amplicon_scheme = tiled_workflow(
args,
amplicons,
left_primer_candidates,
right_primer_candidates,
all_primers,
ambiguous_consensus,
log_file,
results_dir
)
# write files
if args.mode == "tiled":
# assign amplicon numbers from 5' to 3' along the genome
amplicon_scheme.sort(key=lambda x: x["LEFT"][1])
else:
# make sure amplicons with no off-target products and with low penalties get the lowest numbers
amplicon_scheme.sort(key=lambda x: (x.get("off_targets", False), x["penalty"]))
reporting.write_all_primers(data_dir, args.name, all_primers)
reporting.write_scheme_to_files(
results_dir,
amplicon_scheme,
ambiguous_consensus,
args.name,
args.mode,
log_file
)
reporting.varvamp_plot(
results_dir,
alignment_cleaned,
primer_regions,
args.name,
all_primers=all_primers,
amplicon_scheme=amplicon_scheme,
)
reporting.per_base_mismatch_plot(results_dir, amplicon_scheme, args.threshold, args.name)
# QPCR mode
if args.mode == "qpcr":
probe_regions, final_schemes = qpcr_workflow(
args,
data_dir,
alignment_cleaned,
ambiguous_consensus,
majority_consensus,
left_primer_candidates,
right_primer_candidates,
compatible_primers,
log_file
)
# write files
# make sure amplicons with no off-target products and with low penalties get the lowest numbers
final_schemes.sort(key=lambda x: (x.get("off_targets", False), x["penalty"]))
reporting.write_regions_to_bed(probe_regions, args.name, data_dir, "probe")
reporting.write_qpcr_to_files(results_dir, final_schemes, ambiguous_consensus, args.name, log_file)
reporting.varvamp_plot(
results_dir,
alignment_cleaned,
primer_regions,
args.name,
probe_regions=probe_regions,
amplicon_scheme=final_schemes
)
reporting.per_base_mismatch_plot(results_dir, final_schemes, args.threshold, args.name, mode="QPCR")
# varVAMP finished
logging.varvamp_progress(log_file, progress=1, start_time=start_time)
logging.goodbye_message()