-
Notifications
You must be signed in to change notification settings - Fork 3
/
run_clairs_to
executable file
·2311 lines (2054 loc) · 127 KB
/
run_clairs_to
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# BSD 3-Clause License
#
# Copyright 2023 The University of Hong Kong, Department of Computer Science
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import os
import sys
import argparse
import shlex
import subprocess
from collections import defaultdict, namedtuple
from argparse import SUPPRESS
try:
from packaging.version import parse as version_parse
except ModuleNotFoundError:
from distutils.version import LooseVersion as version_parse
from time import time
import shared.param as param
from shared.interval_tree import bed_tree_from
from shared.utils import file_path_from, folder_path_from, subprocess_popen, str2bool, str_none, \
legal_range_from, log_error, log_warning
major_contigs = {"chr" + str(a) for a in list(range(1, 23)) + ["X", "Y"]}.union(
{str(a) for a in list(range(1, 23)) + ["X", "Y"]})
major_contigs_order = ["chr" + str(a) for a in list(range(1, 23)) + ["X", "Y"]] + [str(a) for a in
list(range(1, 23)) + ["X", "Y"]]
file_directory = os.path.dirname(os.path.realpath(__file__))
main_entry = os.path.join(file_directory, "clairs_to.py")
MAX_STEP = 20
OutputPath = namedtuple('OutputPath', [
'log_path',
'tmp_file_path',
'split_bed_path',
'split_indel_bed_path',
'candidates_path',
'pileup_tensor_can_affirmative_path',
'pileup_tensor_can_negational_path',
'vcf_output_path',
])
class Tee(object):
def __init__(self, name, mode):
self.file = open(name, mode)
self.stdout = sys.stdout
sys.stdout = self
def __del__(self):
sys.stdout = self.stdout
self.file.close()
def write(self, data):
self.file.write(data)
self.stdout.write(data)
def flush(self):
self.file.flush()
def logging(str):
if args.tee is None:
print(str)
else:
args.tee.stdin.write(bytes(str + '\n', encoding='utf8'))
def create_output_folder(args):
# create temp file folder
args.output_dir = folder_path_from(args.output_dir, create_not_found=True)
log_path = folder_path_from(os.path.join(args.output_dir, 'logs'), create_not_found=True)
tmp_file_path = folder_path_from(os.path.join(args.output_dir, 'tmp'), create_not_found=True)
split_bed_path = folder_path_from(os.path.join(tmp_file_path, 'split_beds'), create_not_found=True)
split_indel_bed_path = folder_path_from(os.path.join(tmp_file_path, 'split_indel_beds'), create_not_found=True) if not args.disable_indel_calling else None
candidates_path = folder_path_from(os.path.join(tmp_file_path, 'candidates'), create_not_found=True)
pileup_tensor_can_affirmative_path = folder_path_from(os.path.join(tmp_file_path, 'pileup_tensor_can_affirmative'),
create_not_found=True)
pileup_tensor_can_negational_path = folder_path_from(os.path.join(tmp_file_path, 'pileup_tensor_can_negational'),
create_not_found=True)
vcf_output_path = folder_path_from(os.path.join(tmp_file_path, 'vcf_output'), create_not_found=True)
if args.platform != 'ilmn':
phasing_log_path = folder_path_from(os.path.join(args.output_dir, 'logs', 'phasing_log'), create_not_found=True)
phasing_phased_vcf_output_path = folder_path_from(os.path.join(tmp_file_path, 'phasing_output/phased_vcf_output'), create_not_found=True)
phasing_phased_bam_output_path = folder_path_from(os.path.join(tmp_file_path, 'phasing_output/phased_bam_output'), create_not_found=True)
output_path = OutputPath(log_path=log_path,
tmp_file_path=tmp_file_path,
split_bed_path=split_bed_path,
split_indel_bed_path=split_indel_bed_path,
candidates_path=candidates_path,
pileup_tensor_can_affirmative_path=pileup_tensor_can_affirmative_path,
pileup_tensor_can_negational_path=pileup_tensor_can_negational_path,
vcf_output_path=vcf_output_path)
return output_path
def check_version(tool, pos=None, is_pypy=False):
try:
if is_pypy:
proc = subprocess.run("{} -c 'import sys; print (sys.version)'".format(tool), stdout=subprocess.PIPE,
shell=True)
else:
proc = subprocess.run([tool, "--version"], stdout=subprocess.PIPE)
if proc.returncode != 0:
return None
first_line = proc.stdout.decode().split("\n", 1)[0]
version = first_line.split()[pos]
version = version_parse(version)
except Exception:
return None
return version
def check_skip_steps_legal(args):
skip_steps = args.skip_steps
skip_steps_list = skip_steps.rstrip().split(",")
if len(skip_steps_list) == 0:
sys.exit(log_error("[ERROR] --skip_steps option provided but no skip steps index found"))
for step in skip_steps_list:
if int(step) < 1 or int(step) > MAX_STEP:
sys.exit(log_error(
"[ERROR] --skip_steps option provided but contains invalid skip steps index, should be 1-index"))
def check_python_path():
python_path = subprocess.run("which python", stdout=subprocess.PIPE, shell=True).stdout.decode().rstrip()
sys.exit(log_error("[ERROR] Current python execution path: {}".format(python_path)))
def check_python_version(python):
python_path = subprocess.run("{} --version".format(python), stdout=subprocess.PIPE,
shell=True).stdout.decode().rstrip()
return python_path.split(' ')[1]
def check_tools_version(args):
required_tool_version = {
'python': version_parse('3.9.0'),
'pypy': version_parse('3.6'),
'samtools': version_parse('1.10'),
'whatshap': version_parse('1.0'),
'parallel': version_parse('20191122'),
}
tool_version = {
'python': version_parse(check_python_version(args.python)),
'pypy': check_version(tool=args.pypy, pos=0, is_pypy=True),
'samtools': check_version(tool=args.samtools, pos=1),
'parallel': check_version(tool=args.parallel, pos=2),
}
for tool, version in tool_version.items():
required_version = required_tool_version[tool]
if version is None:
logging(log_error(
"[ERROR] {} not found, please check if you are in the conda virtual environment".format(tool)))
check_python_path()
elif version < required_version:
logging(
log_error("[ERROR] Tool version not match, please check if you are in the conda virtual environment"))
logging(' '.join([str(item).ljust(10) for item in ["Tool", "Version", "Required"]]))
error_info = ' '.join([str(item).ljust(10) for item in [tool, version, '>=' + str(required_version)]])
logging(error_info)
check_python_path()
return
def check_contig_in_bam(bam_fn, sorted_contig_list, samtools, allow_none=False, is_tumor=False):
flag = 'tumor' if is_tumor else None
if allow_none and bam_fn is None:
return sorted_contig_list, True
bai_process = subprocess_popen(shlex.split("{} idxstats {}".format(samtools, bam_fn)))
contig_with_read_support_set = set()
for row_id, row in enumerate(bai_process.stdout):
row = row.split('\t')
if len(row) != 4:
continue
contig_name, contig_length, mapped_reads, unmapped_reads = row
if contig_name not in sorted_contig_list:
continue
if int(mapped_reads) > 0:
contig_with_read_support_set.add(contig_name)
for contig_name in sorted_contig_list:
if contig_name not in contig_with_read_support_set:
logging(log_warning(
"[WARNING] Contig name {} provided but no mapped reads found in {} BAM, skip!".format(contig_name, flag)))
filtered_sorted_contig_list = [item for item in sorted_contig_list if item in contig_with_read_support_set]
found_contig = True
if len(filtered_sorted_contig_list) == 0:
found_contig = False
logging(log_warning(
"[WARNING] No mapped reads found in {} BAM for provided contigs set {}".format(
flag, ' '.join(sorted_contig_list))))
return filtered_sorted_contig_list, found_contig
def check_threads(args):
threads = args.threads
# sched_getaffinity is not exist in pypy
try:
sched_getaffinity_list = list(os.sched_getaffinity(0))
num_cpus = len(sched_getaffinity_list)
except:
num_cpus = int(subprocess.run(args.python + " -c \"import os; print(len(os.sched_getaffinity(0)))\"", \
stdout=subprocess.PIPE, shell=True).stdout.decode().rstrip())
if threads > num_cpus:
logging(log_warning(
'[WARNING] Threads setting {} is larger than the number of available threads {} in the system,'.format(
threads, num_cpus)))
logging(log_warning('Set --threads={} for better parallelism.'.format(num_cpus)))
args.threads = num_cpus
return args
def split_extend_vcf(genotyping_mode_vcf_fn, output_fn):
expand_region_size = param.no_of_positions
output_ctg_dict = defaultdict(list)
unzip_process = subprocess_popen(shlex.split("gzip -fdc %s" % (genotyping_mode_vcf_fn)))
for row_id, row in enumerate(unzip_process.stdout):
if row[0] == '#':
continue
columns = row.strip().split(maxsplit=3)
ctg_name = columns[0]
center_pos = int(columns[1])
ctg_start, ctg_end = center_pos - 1, center_pos
if ctg_start < 0:
sys.exit(
log_error(
"[ERROR] Invalid VCF input at the {}-th row {} {}".format(row_id + 1, ctg_name, center_pos)))
if ctg_start - expand_region_size < 0:
continue
expand_ctg_start = ctg_start - expand_region_size
expand_ctg_end = ctg_end + expand_region_size
output_ctg_dict[ctg_name].append(
' '.join([ctg_name, str(expand_ctg_start), str(expand_ctg_end)]))
for key, value in output_ctg_dict.items():
ctg_output_fn = os.path.join(output_fn, key)
with open(ctg_output_fn, 'w') as output_file:
output_file.write('\n'.join(value))
unzip_process.stdout.close()
unzip_process.wait()
know_vcf_contig_set = set(list(output_ctg_dict.keys()))
return know_vcf_contig_set
def split_extend_bed(bed_fn, output_fn, contig_set=None, expand_region=True):
expand_region_size = param.no_of_positions
if not expand_region:
expand_region_size = 0
output_ctg_dict = defaultdict(list)
unzip_process = subprocess_popen(shlex.split("gzip -fdc %s" % (bed_fn)))
for row_id, row in enumerate(unzip_process.stdout):
if row[0] == '#':
continue
columns = row.strip().split()
ctg_name = columns[0]
if contig_set and ctg_name not in contig_set:
continue
ctg_start, ctg_end = int(columns[1]), int(columns[2])
if ctg_end < ctg_start or ctg_start < 0 or ctg_end < 0:
sys.exit(log_error(
"[ERROR] Invalid BED input at the {}-th row {} {} {}".format(row_id + 1, ctg_name, ctg_start, ctg_end)))
expand_ctg_start = max(0, ctg_start - expand_region_size)
expand_ctg_end = max(0, ctg_end + expand_region_size)
output_ctg_dict[ctg_name].append(
' '.join([ctg_name, str(expand_ctg_start), str(expand_ctg_end)]))
for key, value in output_ctg_dict.items():
ctg_output_fn = os.path.join(output_fn, key)
with open(ctg_output_fn, 'w') as output_file:
output_file.write('\n'.join(value))
unzip_process.stdout.close()
unzip_process.wait()
def write_region_bed(region):
try:
ctg_name, start_end = region.split(':')
ctg_start, ctg_end = int(start_end.split('-')[0]) - 1, int(start_end.split('-')[1]) - 1 # bed format
except:
sys.exit("[ERROR] Please use the correct format for --region: ctg_name:start-end, your input is {}".format(
region))
if ctg_end < ctg_start or ctg_start < 0 or ctg_end < 0:
sys.exit("[ERROR] Invalid region input: {}".format(region))
output_bed_path = os.path.join(args.output_dir, 'tmp', 'region.bed')
with open(output_bed_path, 'w') as f:
f.write('\t'.join([ctg_name, str(ctg_start), str(ctg_end)]) + '\n')
return output_bed_path
def check_contigs_intersection(args, fai_fn):
MIN_CHUNK_LENGTH = 200000
MAX_CHUNK_LENGTH = 20000000
is_include_all_contigs = args.include_all_ctgs
is_bed_file_provided = args.bed_fn is not None or args.region is not None
is_indel_bed_file_provided = args.call_indels_only_in_these_regions is not None
is_known_vcf_file_provided = args.genotyping_mode_vcf_fn is not None
is_ctg_name_list_provided = args.ctg_name is not None
if args.region is not None:
args.bed_fn = write_region_bed(args.region)
split_bed_path = os.path.join(args.output_dir, 'tmp', 'split_beds')
split_indel_bed_path = os.path.join(args.output_dir, 'tmp', 'split_indel_beds') if not args.disable_indel_calling else None
tree = bed_tree_from(bed_file_path=args.bed_fn, region=args.region)
know_vcf_contig_set = split_extend_vcf(genotyping_mode_vcf_fn=args.genotyping_mode_vcf_fn,
output_fn=split_bed_path) if is_known_vcf_file_provided else set()
contig_set = set(args.ctg_name.split(',')) if is_ctg_name_list_provided else set()
if not args.include_all_ctgs:
logging("[INFO] --include_all_ctgs not enabled, use chr{1..22,X,Y} and {1..22,X,Y} by default")
else:
logging("[INFO] --include_all_ctgs enabled")
if is_ctg_name_list_provided and is_bed_file_provided:
logging(log_warning(
"[WARNING] both --ctg_name and --bed_fn provided, will only proceed with the contigs appeared in both"))
if is_ctg_name_list_provided and is_known_vcf_file_provided:
logging(log_warning(
"[WARNING] both --ctg_name and --genotyping_mode_vcf_fn provided, will only proceed with the contigs appeared in both"))
if is_ctg_name_list_provided:
contig_set = contig_set.intersection(
set(tree.keys())) if is_bed_file_provided else contig_set
contig_set = contig_set.intersection(
know_vcf_contig_set) if is_known_vcf_file_provided else contig_set
else:
contig_set = contig_set.union(
set(tree.keys())) if is_bed_file_provided else contig_set
contig_set = contig_set.union(
know_vcf_contig_set) if is_known_vcf_file_provided else contig_set
# if each split region is too small(long) for given default chunk num, will increase(decrease) the total chunk num
default_chunk_num = 0
DEFAULT_CHUNK_SIZE = args.chunk_size
contig_length_list = []
contig_chunk_num = {}
with open(fai_fn, 'r') as fai_fp:
for row in fai_fp:
columns = row.strip().split("\t")
contig_name, contig_length = columns[0], int(columns[1])
if not is_include_all_contigs and (
not (is_bed_file_provided or is_ctg_name_list_provided or is_known_vcf_file_provided)) and str(
contig_name) not in major_contigs:
continue
if is_bed_file_provided and contig_name not in tree:
continue
if is_ctg_name_list_provided and contig_name not in contig_set:
continue
if is_known_vcf_file_provided and contig_name not in contig_set:
continue
contig_set.add(contig_name)
contig_length_list.append(contig_length)
chunk_num = int(
contig_length / float(DEFAULT_CHUNK_SIZE)) + 1 if contig_length % DEFAULT_CHUNK_SIZE else int(
contig_length / float(DEFAULT_CHUNK_SIZE))
contig_chunk_num[contig_name] = max(chunk_num, 1)
if default_chunk_num > 0:
min_chunk_length = min(contig_length_list) / float(default_chunk_num)
max_chunk_length = max(contig_length_list) / float(default_chunk_num)
contigs_order = major_contigs_order + list(contig_set)
sorted_contig_list = sorted(list(contig_set), key=lambda x: contigs_order.index(x))
if not len(contig_set):
if is_bed_file_provided:
all_contig_in_bed = ' '.join(list(tree.keys()))
logging(log_warning(
"[WARNING] No contig in --bed_fn was found in the reference, contigs in BED {}: {}".format(args.bed_fn,
all_contig_in_bed)))
if is_known_vcf_file_provided:
all_contig_in_vcf = ' '.join(list(know_vcf_contig_set))
logging(log_warning(
"[WARNING] No contig in --genotyping_mode_vcf_fn was found in the reference, contigs in VCF {}: {}".format(
args.genotyping_mode_vcf_fn, all_contig_in_vcf)))
if is_ctg_name_list_provided:
all_contig_in_ctg_name = ' '.join(args.ctg_name.split(','))
logging(log_warning(
"[WARNING] No contig in --ctg_name was found in the reference, contigs in contigs list: {}".format(
all_contig_in_ctg_name)))
found_contig = False
else:
for c in sorted_contig_list:
if c not in contig_chunk_num:
logging(log_warning(("[WARNING] Contig {} given but not found in the reference".format(c))))
# check contig in bam have support reads
sorted_contig_list, tumor_found_contig = check_contig_in_bam(bam_fn=args.tumor_bam_fn,
sorted_contig_list=sorted_contig_list,
samtools=args.samtools, is_tumor=True)
found_contig = tumor_found_contig
if not found_contig:
log_warning("[WARNING] Exit calling because no contig was found in BAM!")
sys.exit(0)
logging('[INFO] Call variants in contigs: {}'.format(' '.join(sorted_contig_list)))
logging('[INFO] Number of chunks for each contig: {}'.format(
' '.join([str(contig_chunk_num[c]) for c in sorted_contig_list])))
if default_chunk_num > 0 and max_chunk_length > MAX_CHUNK_LENGTH:
logging(log_warning(
'[WARNING] The maximum chunk size set {} is larger than the suggested maximum chunk size {}, consider setting a larger --chunk_num= instead for better parallelism.'.format(
min_chunk_length, MAX_CHUNK_LENGTH)))
elif default_chunk_num > 0 and min_chunk_length < MIN_CHUNK_LENGTH:
logging(log_warning(
'[WARNING] The minimum chunk size set {} is smaller than the suggested minimum chunk size {}, consider setting a smaller --chunk_num= instead.'.format(
min_chunk_length, MIN_CHUNK_LENGTH)))
if default_chunk_num == 0 and max(contig_length_list) < DEFAULT_CHUNK_SIZE / 5:
logging(log_warning(
'[WARNING] The length of the longest contig {} is more than five times smaller than the default chunk size {}, consider setting a smaller --chunk_size= instead for better parallelism.'.format(
max(contig_length_list), DEFAULT_CHUNK_SIZE)))
if is_bed_file_provided:
split_extend_bed(bed_fn=args.bed_fn, output_fn=split_bed_path, contig_set=contig_set)
if not args.disable_indel_calling and is_indel_bed_file_provided:
split_extend_bed(bed_fn=args.call_indels_only_in_these_regions, output_fn=split_indel_bed_path, contig_set=contig_set, expand_region=False)
contig_path = os.path.join(args.output_dir, 'tmp', 'CONTIGS')
with open(contig_path, 'w') as output_file:
output_file.write('\n'.join(sorted_contig_list))
if not args.disable_verdict:
contigs_order = ["chr" + str(a) for a in list(range(1, 23)) + ["X"]]
verdict_flag = len(set(contigs_order).intersection(set(sorted_contig_list))) > 0
if not verdict_flag:
args.disable_verdict = True
logging(log_warning(
"[WARNING] Verdict currently only works for GRCh38 reference genome, apply the --disable_verdict option!"))
chunk_list = []
chunk_list_path = os.path.join(args.output_dir, 'tmp', 'CHUNK_LIST')
with open(chunk_list_path, 'w') as output_file:
for contig_name in sorted_contig_list:
chunk_num = contig_chunk_num[contig_name] if args.chunk_num is None else args.chunk_num
for chunk_id in range(1, chunk_num + 1):
output_file.write(contig_name + ' ' + str(chunk_id) + ' ' + str(chunk_num) + '\n')
chunk_list.append((contig_name, chunk_id, chunk_num))
args.chunk_list = chunk_list
return args
def check_args(args):
if args.conda_prefix is None:
if 'CONDA_PREFIX' in os.environ:
args.conda_prefix = os.environ['CONDA_PREFIX']
else:
try:
python_path = subprocess.run('which python', stdout=subprocess.PIPE,
shell=True).stdout.decode().rstrip()
args.conda_prefix = os.path.dirname(os.path.dirname(python_path))
except:
sys.exit(log_error("[ERROR] Conda prefix not found, please activate a correct conda environment."))
args.tumor_bam_fn = file_path_from(file_name=args.tumor_bam_fn, exit_on_not_found=True)
tumor_bai_fn = file_path_from(file_name=args.tumor_bam_fn, suffix=".bai", exit_on_not_found=False, sep='.')
tumor_crai_fn = file_path_from(file_name=args.tumor_bam_fn, suffix=".crai", exit_on_not_found=False, sep='.')
tumor_csi_fn = file_path_from(file_name=args.tumor_bam_fn, suffix=".csi", exit_on_not_found=False, sep='.')
args.ref_fn = file_path_from(file_name=args.ref_fn, exit_on_not_found=True)
fai_fn = file_path_from(file_name=args.ref_fn, suffix=".fai", exit_on_not_found=True, sep='.')
args.bed_fn = file_path_from(file_name=args.bed_fn, exit_on_not_found=True, allow_none=True)
args.genotyping_mode_vcf_fn = file_path_from(file_name=args.genotyping_mode_vcf_fn, exit_on_not_found=True,
allow_none=True)
args.hybrid_mode_vcf_fn = file_path_from(file_name=args.hybrid_mode_vcf_fn, exit_on_not_found=True, allow_none=True)
if args.platform in param.model_name_platform_dict:
updated_platform = param.model_name_platform_dict[args.platform]
logging(
"[INFO] Platform parameter is using ONT model name format, change --platform {} to --platform {}".format(
args.platform, updated_platform))
args.platform = updated_platform
if tumor_bai_fn is None and tumor_crai_fn is None and tumor_csi_fn is None:
sys.exit(log_error(
"[ERROR] Tumor BAM index file {} or {} not found. Please run `samtools index $BAM` first.".format(
args.tumor_bam_fn + '.bai',
args.tumor_bam_fn + '.crai')))
if not args.disable_indel_calling and args.platform not in {'ont_r10_dorado_sup_4khz', 'ont_r10_dorado_hac_4khz', 'ont_r10_dorado_sup_5khz', 'ont_r10_dorado_sup_5khz_ss', 'ont_r10_dorado_sup_5khz_ssrs',
'ont_r10_guppy_sup_4khz', 'ont_r10_guppy_hac_5khz', 'ont_r10_dorado_4khz',
'ont_r10_dorado_5khz', 'ont_r10_guppy', 'ont_r10_guppy_4khz', 'ont_r10_guppy_5khz', 'ilmn', 'ilmn_ss', 'ilmn_ssrs',
'hifi_revio', 'hifi_revio_ss', 'hifi_revio_ssrs'}:
sys.exit(log_error("[ERROR] Indel calling only support 'ont_r10_dorado_sup_4khz', 'ont_r10_dorado_hac_4khz', 'ont_r10_dorado_sup_5khz', 'ont_r10_dorado_sup_5khz_ss', 'ont_r10_dorado_sup_5khz_ssrs', 'ont_r10_guppy_sup_4khz', 'ont_r10_guppy_hac_5khz', 'ilmn', ilmn_ss', 'ilmn_ssrs', 'hifi_revio', 'hifi_revio_ss', and 'hifi_revio_ssrs' platform"))
if args.genotyping_mode_vcf_fn is not None and args.hybrid_mode_vcf_fn is not None:
sys.exit(log_error("[ERROR] Please provide either --genotyping_mode_vcf_fn or --hybrid_mode_vcf_fn only"))
if args.snv_pileup_affirmative_model_path is None:
if args.platform == 'ont_r10_guppy_sup_4khz' or args.platform == 'ont_r10_guppy_4khz' or args.platform == 'ont_r10_guppy':
args.snv_pileup_affirmative_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_guppy_sup_4khz', 'pileup_affirmative.pkl')
elif args.platform == 'ont_r10_dorado_sup_5khz' or args.platform == 'ont_r10_dorado_5khz' or args.platform == 'ont_r10_dorado_sup_5khz_ss':
args.snv_pileup_affirmative_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_dorado_sup_5khz', 'pileup_affirmative.pkl')
elif args.platform == 'ont_r10_dorado_sup_5khz_ssrs':
args.snv_pileup_affirmative_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_dorado_sup_5khz_ssrs', 'pileup_affirmative.pkl')
elif args.platform == 'ont_r10_dorado_sup_4khz' or args.platform == 'ont_r10_dorado_4khz':
args.snv_pileup_affirmative_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_dorado_sup_4khz', 'pileup_affirmative.pkl')
elif args.platform == 'ont_r10_dorado_hac_4khz':
args.snv_pileup_affirmative_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_dorado_hac_4khz', 'pileup_affirmative.pkl')
elif args.platform == 'ont_r10_guppy_hac_5khz' or args.platform == 'ont_r10_guppy_5khz':
args.snv_pileup_affirmative_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_guppy_hac_5khz', 'pileup_affirmative.pkl')
elif args.platform == 'ilmn' or args.platform == 'ilmn_ss':
args.snv_pileup_affirmative_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ilmn', 'pileup_affirmative.pkl')
elif args.platform == 'ilmn_ssrs':
args.snv_pileup_affirmative_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ilmn_ssrs', 'pileup_affirmative.pkl')
elif args.platform == 'hifi_revio' or args.platform == 'hifi_revio_ss':
args.snv_pileup_affirmative_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'hifi_revio', 'pileup_affirmative.pkl')
elif args.platform == 'hifi_revio_ssrs':
args.snv_pileup_affirmative_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'hifi_revio_ssrs', 'pileup_affirmative.pkl')
else:
args.snv_pileup_affirmative_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
args.platform, 'pileup_affirmative.pkl')
if args.snv_pileup_negational_model_path is None:
if args.platform == 'ont_r10_guppy_sup_4khz' or args.platform == 'ont_r10_guppy_4khz' or args.platform == 'ont_r10_guppy':
args.snv_pileup_negational_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_guppy_sup_4khz', 'pileup_negational.pkl')
elif args.platform == 'ont_r10_dorado_sup_5khz' or args.platform == 'ont_r10_dorado_5khz' or args.platform == 'ont_r10_dorado_sup_5khz_ss':
args.snv_pileup_negational_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_dorado_sup_5khz', 'pileup_negational.pkl')
elif args.platform == 'ont_r10_dorado_sup_5khz_ssrs':
args.snv_pileup_negational_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_dorado_sup_5khz_ssrs', 'pileup_negational.pkl')
elif args.platform == 'ont_r10_dorado_sup_4khz' or args.platform == 'ont_r10_dorado_4khz':
args.snv_pileup_negational_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_dorado_sup_4khz', 'pileup_negational.pkl')
elif args.platform == 'ont_r10_dorado_hac_4khz':
args.snv_pileup_negational_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_dorado_hac_4khz', 'pileup_negational.pkl')
elif args.platform == 'ont_r10_guppy_hac_5khz' or args.platform == 'ont_r10_guppy_5khz':
args.snv_pileup_negational_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_guppy_hac_5khz', 'pileup_negational.pkl')
elif args.platform == 'ilmn' or args.platform == 'ilmn_ss':
args.snv_pileup_negational_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ilmn', 'pileup_negational.pkl')
elif args.platform == 'ilmn_ssrs':
args.snv_pileup_negational_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ilmn_ssrs', 'pileup_negational.pkl')
elif args.platform == 'hifi_revio' or args.platform == 'hifi_revio_ss':
args.snv_pileup_negational_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'hifi_revio', 'pileup_negational.pkl')
elif args.platform == 'hifi_revio_ssrs':
args.snv_pileup_negational_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'hifi_revio_ssrs', 'pileup_negational.pkl')
else:
args.snv_pileup_negational_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
args.platform, 'pileup_negational.pkl')
args.snv_pileup_affirmative_model_path = file_path_from(file_name=args.snv_pileup_affirmative_model_path,
exit_on_not_found=True, is_directory=False, allow_none=False)
args.snv_pileup_negational_model_path = file_path_from(file_name=args.snv_pileup_negational_model_path,
exit_on_not_found=True, is_directory=False, allow_none=False)
if args.snv_likelihood_matrix_data is None:
if args.platform == 'ont_r10_guppy_sup_4khz' or args.platform == 'ont_r10_guppy_4khz' or args.platform == 'ont_r10_guppy':
args.snv_likelihood_matrix_data = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_guppy_sup_4khz', 'likelihood_matrix.txt')
elif args.platform == 'ont_r10_dorado_sup_5khz' or args.platform == 'ont_r10_dorado_5khz' or args.platform == 'ont_r10_dorado_sup_5khz_ss' or args.platform == 'ont_r10_dorado_sup_5khz_ssrs':
args.snv_likelihood_matrix_data = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_dorado_sup_5khz', 'likelihood_matrix.txt')
elif args.platform == 'ont_r10_dorado_sup_4khz' or args.platform == 'ont_r10_dorado_4khz':
args.snv_likelihood_matrix_data = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_dorado_sup_4khz', 'likelihood_matrix.txt')
elif args.platform == 'ont_r10_dorado_hac_4khz':
args.snv_likelihood_matrix_data = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_dorado_hac_4khz', 'likelihood_matrix.txt')
elif args.platform == 'ont_r10_guppy_hac_5khz' or args.platform == 'ont_r10_guppy_5khz':
args.snv_likelihood_matrix_data = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_guppy_hac_5khz', 'likelihood_matrix.txt')
elif args.platform == 'ilmn' or args.platform == 'ilmn_ss' or args.platform == 'ilmn_ssrs':
args.snv_likelihood_matrix_data = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ilmn', 'likelihood_matrix.txt')
elif args.platform == 'hifi_revio' or args.platform == 'hifi_revio_ss' or args.platform == 'hifi_revio_ssrs':
args.snv_likelihood_matrix_data = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'hifi_revio', 'likelihood_matrix.txt')
else:
args.snv_likelihood_matrix_data = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models', args.platform,
'likelihood_matrix.txt')
if not args.disable_indel_calling:
if args.indel_pileup_affirmative_model_path is None:
if args.platform == 'ont_r10_guppy_sup_4khz' or args.platform == 'ont_r10_guppy_4khz' or args.platform == 'ont_r10_guppy':
args.indel_pileup_affirmative_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_guppy_sup_4khz', 'indel', 'pileup_affirmative.pkl')
elif args.platform == 'ont_r10_dorado_sup_5khz' or args.platform == 'ont_r10_dorado_5khz' or args.platform == 'ont_r10_dorado_sup_5khz_ss':
args.indel_pileup_affirmative_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_dorado_sup_5khz', 'indel', 'pileup_affirmative.pkl')
elif args.platform == 'ont_r10_dorado_sup_5khz_ssrs':
args.indel_pileup_affirmative_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_dorado_sup_5khz_ssrs', 'indel',
'pileup_affirmative.pkl')
elif args.platform == 'ont_r10_dorado_sup_4khz' or args.platform == 'ont_r10_dorado_4khz':
args.indel_pileup_affirmative_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_dorado_sup_4khz', 'indel', 'pileup_affirmative.pkl')
elif args.platform == 'ont_r10_dorado_hac_4khz':
args.indel_pileup_affirmative_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_dorado_hac_4khz', 'indel', 'pileup_affirmative.pkl')
elif args.platform == 'ont_r10_guppy_hac_5khz' or args.platform == 'ont_r10_guppy_5khz':
args.indel_pileup_affirmative_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_guppy_hac_5khz', 'indel', 'pileup_affirmative.pkl')
elif args.platform == 'ilmn' or args.platform == 'ilmn_ss':
args.indel_pileup_affirmative_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ilmn', 'indel', 'pileup_affirmative.pkl')
elif args.platform == 'ilmn_ssrs':
args.indel_pileup_affirmative_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ilmn_ssrs', 'indel', 'pileup_affirmative.pkl')
elif args.platform == 'hifi_revio' or args.platform == 'hifi_revio_ss':
args.indel_pileup_affirmative_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'hifi_revio', 'indel', 'pileup_affirmative.pkl')
elif args.platform == 'hifi_revio_ssrs':
args.indel_pileup_affirmative_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'hifi_revio_ssrs', 'indel', 'pileup_affirmative.pkl')
else:
args.indel_pileup_affirmative_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
args.platform, 'indel', 'pileup_affirmative.pkl')
if args.indel_pileup_negational_model_path is None:
if args.platform == 'ont_r10_guppy_sup_4khz' or args.platform == 'ont_r10_guppy_4khz' or args.platform == 'ont_r10_guppy':
args.indel_pileup_negational_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_guppy_sup_4khz', 'indel', 'pileup_negational.pkl')
elif args.platform == 'ont_r10_dorado_sup_5khz' or args.platform == 'ont_r10_dorado_5khz' or args.platform == 'ont_r10_dorado_sup_5khz_ss':
args.indel_pileup_negational_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_dorado_sup_5khz', 'indel', 'pileup_negational.pkl')
elif args.platform == 'ont_r10_dorado_sup_5khz_ssrs':
args.indel_pileup_negational_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_dorado_sup_5khz_ssrs', 'indel',
'pileup_negational.pkl')
elif args.platform == 'ont_r10_dorado_sup_4khz' or args.platform == 'ont_r10_dorado_4khz':
args.indel_pileup_negational_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_dorado_sup_4khz', 'indel', 'pileup_negational.pkl')
elif args.platform == 'ont_r10_dorado_hac_4khz':
args.indel_pileup_negational_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_dorado_hac_4khz', 'indel', 'pileup_negational.pkl')
elif args.platform == 'ont_r10_guppy_hac_5khz' or args.platform == 'ont_r10_guppy_5khz':
args.indel_pileup_negational_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_guppy_hac_5khz', 'indel', 'pileup_negational.pkl')
elif args.platform == 'ilmn' or args.platform == 'ilmn_ss':
args.indel_pileup_negational_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ilmn', 'indel', 'pileup_negational.pkl')
elif args.platform == 'ilmn_ssrs':
args.indel_pileup_negational_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ilmn_ssrs', 'indel', 'pileup_negational.pkl')
elif args.platform == 'hifi_revio' or args.platform == 'hifi_revio_ss':
args.indel_pileup_negational_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'hifi_revio', 'indel', 'pileup_negational.pkl')
elif args.platform == 'hifi_revio_ssrs':
args.indel_pileup_negational_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'hifi_revio_ssrs', 'indel',
'pileup_negational.pkl')
else:
args.indel_pileup_negational_model_path = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
args.platform, 'indel', 'pileup_negational.pkl')
args.indel_pileup_affirmative_model_path = file_path_from(file_name=args.indel_pileup_affirmative_model_path, exit_on_not_found=True,
is_directory=False, allow_none=False)
args.indel_pileup_negational_model_path = file_path_from(file_name=args.indel_pileup_negational_model_path,
exit_on_not_found=True,
is_directory=False, allow_none=False)
if args.indel_likelihood_matrix_data is None:
if args.platform == 'ont_r10_guppy_sup_4khz' or args.platform == 'ont_r10_guppy_4khz' or args.platform == 'ont_r10_guppy':
args.indel_likelihood_matrix_data = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_guppy_sup_4khz', 'indel', 'likelihood_matrix.txt')
elif args.platform == 'ont_r10_dorado_sup_5khz' or args.platform == 'ont_r10_dorado_5khz' or args.platform == 'ont_r10_dorado_sup_5khz_ss' or args.platform == 'ont_r10_dorado_sup_5khz_ssrs':
args.indel_likelihood_matrix_data = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_dorado_sup_5khz', 'indel', 'likelihood_matrix.txt')
elif args.platform == 'ont_r10_dorado_sup_4khz' or args.platform == 'ont_r10_dorado_4khz':
args.indel_likelihood_matrix_data = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_dorado_sup_4khz', 'indel', 'likelihood_matrix.txt')
elif args.platform == 'ont_r10_dorado_hac_4khz':
args.indel_likelihood_matrix_data = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_dorado_hac_4khz', 'indel', 'likelihood_matrix.txt')
elif args.platform == 'ont_r10_guppy_hac_5khz' or args.platform == 'ont_r10_guppy_5khz':
args.indel_likelihood_matrix_data = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ont_r10_guppy_hac_5khz', 'indel', 'likelihood_matrix.txt')
elif args.platform == 'ilmn' or args.platform == 'ilmn_ss' or args.platform == 'ilmn_ssrs':
args.indel_likelihood_matrix_data = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'ilmn', 'indel', 'likelihood_matrix.txt')
elif args.platform == 'hifi_revio' or args.platform == 'hifi_revio_ss' or args.platform == 'hifi_revio_ssrs':
args.indel_likelihood_matrix_data = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models',
'hifi_revio', 'indel', 'likelihood_matrix.txt')
else:
args.indel_likelihood_matrix_data = os.path.join(args.conda_prefix, 'bin', 'clairs-to_models', args.platform,
'indel', 'likelihood_matrix.txt')
default_gnomad_resource = os.path.join(args.conda_prefix, 'bin', 'clairs-to_databases',
'gnomad.r2.1.af-ge-0.001.sites.vcf.gz')
default_dbsnp_resource = os.path.join(args.conda_prefix, 'bin', 'clairs-to_databases',
'dbsnp.b138.non-somatic.sites.vcf.gz')
default_1kgpon_resource = os.path.join(args.conda_prefix, 'bin', 'clairs-to_databases', '1000g-pon.sites.vcf.gz')
default_colorsdb_resource = os.path.join(args.conda_prefix, 'bin', 'clairs-to_databases', 'CoLoRSdb.GRCh38.v1.1.0.deepvariant.glnexus.af-ge-0.001.vcf.gz')
default_gnomad_resource = file_path_from(file_name=default_gnomad_resource, exit_on_not_found=True,
is_directory=False, allow_none=True)
default_dbsnp_resource = file_path_from(file_name=default_dbsnp_resource, exit_on_not_found=True,
is_directory=False, allow_none=True)
default_1kgpon_resource = file_path_from(file_name=default_1kgpon_resource, exit_on_not_found=True,
is_directory=False, allow_none=True)
default_colorsdb_resource = file_path_from(file_name=default_colorsdb_resource, exit_on_not_found=True,
is_directory=False, allow_none=True)
# default_indel_bed_resource = os.path.join(args.conda_prefix, 'bin', 'clairs-to_databases', 'GRCh38Chr1-22XY_excludedGIABStratifV3.3AllDifficultRegions_includedCMRGv1.0.bed')
if args.bed_fn is not None and args.call_indels_only_in_these_regions is not None:
logging(log_warning(
"[WARNING] `--bed_fn` will supersede `--call_indels_only_in_these_regions`."))
# if args.call_indels_only_in_these_regions is None:
# args.call_indels_only_in_these_regions = file_path_from(file_name=default_indel_bed_resource, exit_on_not_found=True,
# is_directory=False, allow_none=True)
if args.panel_of_normals is not None:
if args.panel_of_normals == 'None' or args.panel_of_normals == 'Null' or args.panel_of_normals == ' ':
args.disable_nonsomatic_tagging = True
else:
panel_of_normals_list = list(args.panel_of_normals.split(','))
for pon in panel_of_normals_list:
pon_path = file_path_from(file_name=str(pon), exit_on_not_found=True, is_directory=False,
allow_none=True)
if str(default_gnomad_resource) not in panel_of_normals_list:
logging(log_warning(
"[INFO] Default {} PoN is not included!".format(str(default_gnomad_resource))))
if str(default_dbsnp_resource) not in panel_of_normals_list:
logging(log_warning(
"[INFO] Default {} PoN is not included!".format(str(default_dbsnp_resource))))
if str(default_1kgpon_resource) not in panel_of_normals_list:
logging(log_warning(
"[INFO] Default {} PoN is not included!".format(str(default_1kgpon_resource))))
if str(default_colorsdb_resource) not in panel_of_normals_list:
logging(log_warning(
"[INFO] Default {} PoN is not included!".format(str(default_colorsdb_resource))))
args.panel_of_normals = args.panel_of_normals
if args.panel_of_normals_require_allele_matching is not None:
ori_panel_of_normals_require_allele_matching_list = list(args.panel_of_normals_require_allele_matching.split(','))
if len(panel_of_normals_list) != len(ori_panel_of_normals_require_allele_matching_list):
logging(log_warning(
"[WARNING] Please use `--panel_of_normals_require_allele_matching` together with `--panel_of_normals`."))
if args.panel_of_normals_require_allele_matching == 'None' or args.panel_of_normals_require_allele_matching == 'Null' or args.panel_of_normals_require_allele_matching == ' ':
panel_of_normals_require_allele_matching_list = ['True'] * len(panel_of_normals_list)
args.panel_of_normals_require_allele_matching = ','.join(panel_of_normals_require_allele_matching_list)
else:
args.panel_of_normals_require_allele_matching = args.panel_of_normals_require_allele_matching
else:
panel_of_normals_require_allele_matching_list = ['True'] * len(panel_of_normals_list)
args.panel_of_normals_require_allele_matching = ','.join(panel_of_normals_require_allele_matching_list)
else:
args.panel_of_normals = str(default_gnomad_resource) + ',' + str(default_dbsnp_resource) + ',' + str(
default_1kgpon_resource) + ',' + str(default_colorsdb_resource)
args.panel_of_normals_require_allele_matching = 'True' + ',' + 'True' + ',' + 'False' + ',' + 'False'
if args.whatshap is None:
args.whatshap = os.path.join(args.conda_prefix, 'bin', 'whatshap')
if args.longphase is None:
args.longphase = os.path.join(args.conda_prefix, 'bin', 'longphase')
args.use_longphase_for_intermediate_phasing = False if args.use_whatshap_for_intermediate_phasing is True else True
if args.use_longphase_for_intermediate_phasing and not os.path.exists(args.longphase):
sys.exit(log_error("[ERROR] Cannot find longphase at {}".format(args.longphase)))
if args.use_whatshap_for_intermediate_phasing and not os.path.exists(args.whatshap):
sys.exit(log_error("[ERROR] Cannot find whatshap at {}".format(args.whatshap)))
if args.use_longphase_for_intermediate_haplotagging is None:
args.use_longphase_for_intermediate_haplotagging = True
if args.use_longphase_for_intermediate_haplotagging and not os.path.exists(args.longphase):
sys.exit(log_error("[ERROR] Cannot find longphase at {}".format(args.longphase)))
if args.snv_min_af is None:
args.snv_min_af = param.snv_min_af
if args.indel_min_af is None:
if not args.disable_indel_calling:
if 'ont' in args.platform:
args.indel_min_af = 0.1
else:
args.indel_min_af = 0.05
else:
args.indel_min_af = 1.0
if args.min_coverage is None:
args.min_coverage = param.min_coverage
if args.chunk_size is None:
args.chunk_size = 5000000
if args.min_bq is None:
args.min_bq = param.min_bq_dict[args.platform]
if args.platform not in {'ont_r10_dorado_sup_4khz', 'ont_r10_dorado_hac_4khz', 'ont_r10_dorado_sup_5khz', 'ont_r10_dorado_sup_5khz_ss', 'ont_r10_dorado_sup_5khz_ssrs',
'ont_r10_guppy_sup_4khz', 'ont_r10_guppy_hac_5khz', 'ont_r10_dorado_4khz',
'ont_r10_dorado_5khz', 'ont_r10_guppy', 'ont_r10_guppy_4khz', 'ont_r10_guppy_5khz', 'ilmn', 'ilmn_ss', 'ilmn_ssrs',
'hifi_revio', 'hifi_revio_ss', 'hifi_revio_ssrs'}:
logging(log_error(
'[ERROR] Invalid platform input, optional: {ont_r10_dorado_sup_4khz, ont_r10_dorado_hac_4khz, ont_r10_dorado_sup_5khz, ont_r10_dorado_sup_5khz_ss, ont_r10_dorado_sup_5khz_ssrs, ont_r10_guppy_sup_4khz, ont_r10_guppy_hac_5khz, ilmn, ilmn_ss, ilmn_ssrs, hifi_revio, hifi_revio_ss, hifi_revio_ssrs}'))
if args.qual is None:
if args.platform not in ["ont_r10_dorado_sup_5khz_ssrs", "ilmn_ssrs", "hifi_revio_ssrs"]:
args.qual = param.min_thred_qual[args.platform] if args.platform in param.min_thred_qual else param.min_thred_qual['ont']
if args.qual_cutoff_phaseable_region is None:
args.qual_cutoff_phaseable_region = param.min_phaseable_thred_qual[
args.platform] if args.platform in param.min_phaseable_thred_qual else \
param.min_phaseable_thred_qual['ont']
if args.qual_cutoff_unphaseable_region is None:
args.qual_cutoff_unphaseable_region = param.min_unphaseable_thred_qual[
args.platform] if args.platform in param.min_unphaseable_thred_qual else \
param.min_unphaseable_thred_qual['ont']
args.qual_indel = param.min_thred_qual_indel[args.platform] if args.platform in param.min_thred_qual_indel else \
param.min_thred_qual_indel['ont']
if args.qual_indel_cutoff_phaseable_region is None:
args.qual_indel_cutoff_phaseable_region = param.min_phaseable_thred_qual_indel[
args.platform] if args.platform in param.min_phaseable_thred_qual_indel else \
param.min_phaseable_thred_qual_indel['ont']
if args.qual_indel_cutoff_unphaseable_region is None:
args.qual_indel_cutoff_unphaseable_region = param.min_unphaseable_thred_qual_indel[
args.platform] if args.platform in param.min_unphaseable_thred_qual_indel else \
param.min_unphaseable_thred_qual_indel['ont']
else:
args.qual = 4
args.qual_cutoff_phaseable_region = args.qual
args.qual_cutoff_unphaseable_region = args.qual
args.qual_indel = 2
args.qual_indel_cutoff_phaseable_region = args.qual_indel
args.qual_indel_cutoff_unphaseable_region = args.qual_indel
else:
if args.qual_cutoff_phaseable_region is not None or args.qual_cutoff_unphaseable_region is not None:
logging(log_warning(
"[WARNING] `--qual` will supersede `--qual_cutoff_phaseable_region` and `--qual_cutoff_unphaseable_region`."))
args.qual_cutoff_phaseable_region = args.qual
args.qual_cutoff_unphaseable_region = args.qual
args.qual_indel = args.qual
args.qual_indel_cutoff_phaseable_region = args.qual
args.qual_indel_cutoff_unphaseable_region = args.qual
if args.skip_steps is not None:
check_skip_steps_legal(args)
if args.disable_intermediate_phasing:
args.phase_tumor = False
if args.phase_tumor is None:
if args.genotyping_mode_vcf_fn is not None:
logging(log_warning(
"[WARNING] HET SNPs based phasing is disabled if `--genotyping_mode_vcf_fn` is provided, add `--phase_tumor True` if phasing the tumor is still needed. Please ensure you have sufficient heterozygous variant candidates given in the --genotyping_mode_vcf_fn file, otherwise the phasing step might lead to worse performance."))
else:
args.phase_tumor = True if args.platform != 'ilmn' else False
if not args.disable_verdict:
ref_contigs_set = set()
with open(fai_fn, 'r') as fai_fp:
for row in fai_fp:
columns = row.strip().split("\t")
contig_name = columns[0]
ref_contigs_set.add(contig_name)
contigs_order = ["chr" + str(a) for a in list(range(1, 23)) + ["X"]]
verdict_flag = len(set(contigs_order).intersection(ref_contigs_set)) > 0
if not verdict_flag:
args.disable_verdict = True
logging(log_warning(
"[WARNING] Verdict currently only works for GRCh38 reference genome, apply the --disable_verdict option!"))
if args.cna_resource_dir is None:
args.cna_resource_dir = os.path.join(args.conda_prefix, 'bin', 'clairs-to_cna_data', 'reference_files')
if args.allele_counter_dir is None:
args.allele_counter_dir = os.path.join(file_directory, 'src', 'verdict', 'allele_counter')
if not os.path.exists(args.allele_counter_dir):
args.disable_verdict = True
logging(log_warning(
"[WARNING] The allele counter {} is not found, apply the --disable_verdict option!".format(
args.allele_counter_dir)))
if not os.path.exists(args.cna_resource_dir):
args.disable_verdict = True
logging(log_warning(
"[WARNING] The CNA resource directory {} is not found, apply the --disable_verdict option!".format(
args.cna_resource_dir)))
if args.genotyping_mode_vcf_fn is not None or args.hybrid_mode_vcf_fn is not None:
logging(log_warning(
"[INFO] Enable --print_ref_calls option and disable --do_not_print_nonsomatic_calls in genotyping mode!"))
args.print_ref_calls = True
args.do_not_print_nonsomatic_calls = False
args.disable_indel_calling = True
legal_range_from(param_name="threads", x=args.threads, min_num=1, exit_out_of_range=True)
legal_range_from(param_name="qual", x=args.qual, min_num=0, exit_out_of_range=True)
legal_range_from(param_name="qual_cutoff_phaseable_region", x=args.qual_cutoff_phaseable_region, min_num=0, exit_out_of_range=True)
legal_range_from(param_name="qual_cutoff_unphaseable_region", x=args.qual_cutoff_unphaseable_region, min_num=0, exit_out_of_range=True)
legal_range_from(param_name="min_coverage", x=args.min_coverage, min_num=0, exit_out_of_range=True)
legal_range_from(param_name="min_bq", x=args.min_bq, min_num=0, exit_out_of_range=True)
legal_range_from(param_name="snv_min_af", x=args.snv_min_af, min_num=0, max_num=1, exit_out_of_range=True)
legal_range_from(param_name="indel_min_af", x=args.indel_min_af, min_num=0, max_num=1, exit_out_of_range=True)
legal_range_from(param_name="chunk_size", x=args.chunk_size, min_num=0, exit_out_of_range=True)
args.output_path = create_output_folder(args)
check_tools_version(args=args)
args = check_threads(args=args)
args = check_contigs_intersection(args=args, fai_fn=fai_fn)
return args
def print_args(args):
logging("")
logging("[INFO] CALLER VERSION: {}".format(param.version))
logging("[INFO] TUMOR BAM FILE PATH: {}".format(args.tumor_bam_fn))
logging("[INFO] REFERENCE FILE PATH: {}".format(args.ref_fn))
logging("[INFO] PLATFORM: {}".format(args.platform))
logging("[INFO] THREADS: {}".format(args.threads))
logging("[INFO] OUTPUT FOLDER: {}".format(args.output_dir))
logging("[INFO] SNV OUTPUT VCF PATH: {}".format(os.path.join(args.output_dir, args.snv_output_prefix + '.vcf.gz')))
logging("[INFO] INDEL OUTPUT VCF PATH: {}".format(os.path.join(args.output_dir, args.indel_output_prefix + '.vcf.gz')))
logging("[INFO] SNV MINIMUM AF: {}".format(args.snv_min_af))
logging("[INFO] INDEL MINIMUM AF: {}".format(args.indel_min_af))
logging("[INFO] SNV PILEUP AFFIRMATIVE MODEL PATH: {}".format(args.snv_pileup_affirmative_model_path))
logging("[INFO] SNV PILEUP NEGATIONAL MODEL PATH: {}".format(args.snv_pileup_negational_model_path))
logging("[INFO] INDEL PILEUP AFFIRMATIVE MODEL PATH: {}".format(args.indel_pileup_affirmative_model_path))
logging("[INFO] INDEL PILEUP NEGATIONAL MODEL PATH: {}".format(args.indel_pileup_negational_model_path))
logging("[INFO] BED FILE PATH: {}".format(args.bed_fn))
logging("[INFO] SPECIFIED REGIONS FOR CALLING: {}".format(args.region))
# logging("[INFO] REGIONS FOR INDEL CALLING: {}".format(args.call_indels_only_in_these_regions))
logging("[INFO] CONTIGS FOR CALLING: {}".format(args.ctg_name))
logging("[INFO] ENABLE INCLUDING ALL CTGS FOR CALLING: {}".format(args.include_all_ctgs))
logging("[INFO] GENOTYPING MODE VCF FILE PATH: {}".format(args.genotyping_mode_vcf_fn))
logging("[INFO] HYBRID MODE VCF FILE PATH: {}".format(args.hybrid_mode_vcf_fn))
logging("[INFO] PANEL OF NORMALS: {}".format(args.panel_of_normals))
logging("[INFO] PANEL OF NORMALS REQUIRE ALLELE MATCHING: {}".format(args.panel_of_normals_require_allele_matching))
logging("[INFO] CHUNK SIZE: {}".format(args.chunk_size))
logging("[INFO] CONDA BINARY PREFIX: {}".format(args.conda_prefix))
logging("[INFO] SAMTOOLS BINARY PATH: {}".format(args.samtools))
logging("[INFO] PYTHON BINARY PATH: {}".format(args.python))
logging("[INFO] PYPY BINARY PATH: {}".format(args.pypy))
logging("[INFO] PARALLEL BINARY PATH: {}".format(args.parallel))
logging("[INFO] LONGPHASE BINARY PATH: {}".format(args.longphase))
logging("[INFO] WHATSHAP BINARY PATH: {}".format(args.whatshap))
logging("[INFO] ENABLE DRY RUN: {}".format(args.dry_run))
logging("[INFO] ENABLE REMOVING INTERMEDIATE FILES: {}".format(args.remove_intermediate_dir))
logging("[INFO] DISABLE INTERMEDIATE PHASING: {}".format(args.disable_intermediate_phasing))
logging("[INFO] DISABLE INDEL CALLING: {}".format(args.disable_indel_calling))
logging("[INFO] ENABLE PRINTING REFERENCE CALLS: {}".format(args.print_ref_calls))