Repository navigation
Expand file tree
/
Copy pathminisv.py
More file actions
1187 lines (1063 loc) · 45.1 KB
/
Copy pathminisv.py
File metadata and controls
1187 lines (1063 loc) · 45.1 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
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
"""A module for parsing GAF/PAF format from minigraph alignment and minimap2 alignment
"""
import functools
import gzip
import math
import os
import re
from datetime import datetime
from multiprocessing import Pool
from typing import Any, Optional
import intervaltree # type: ignore
import mappy as mp
from intervaltree import Interval # type: ignore
from .identify_breaks_v5 import call_breakpoints
from .io import GroupedResults, load_gaf_to_grouped_reads, merge_breakpoint_indel_vcf
from .merge_break_pts_v3 import merge_breaks
cigar_pattern = re.compile(r"(\d+)([=XIDM])")
path_seg_pattern = re.compile(r"([><])([^><:\s]+):(\d+)-(\d+)")
# ds:Z: tag example
# *ag: SNV
# :234: reference bp
# -[gcgccgcgccggcgcaggcgcagagag]: TSD deletion, can be left or right end
# +tggagggactgcccagt: insertion
ds_pattern = re.compile(r"([\+\-\*:])([A-Za-z\[\]0-9]+)")
path_seg_pattern = re.compile(r"([><])([^><:\s]+):(\d+)-(\d+)")
re_tsd = re.compile(r"(\[([A-Za-z]+)\])?([A-Za-z]+)(\[([A-Za-z]+)\])?")
class StartPosException(Exception):
pass
class EndPosException(Exception):
pass
def parse_one_line(line, min_mapq=30, min_len=100, verbose=False, lineno=0, ds=True):
read = line.strip().split()
parsed_read = AlignedRead(read)
large_indels_one_read = parsed_read.get_indels(
min_mapq=min_mapq, min_len=min_len, dbg=verbose, lineno=lineno, ds=ds
)
return large_indels_one_read
def parse_grouped_reads(
grouped_reads: list[str],
min_mapq: int = 30,
min_indel_len: int = 100,
verbose: bool = False,
lineno: int = 0,
ds: bool = True,
) -> GroupedResults:
all_indels = []
for read in grouped_reads:
parsed_read = AlignedRead(read)
large_indels_one_read = parsed_read.get_indels(
min_mapq=min_mapq, min_len=min_indel_len, dbg=verbose, lineno=lineno, ds=ds
)
all_indels += large_indels_one_read
if len(grouped_reads) > 1:
all_breaks = call_breakpoints(grouped_reads)
else:
all_breaks = []
return GroupedResults(all_indels, all_breaks)
class GafParser(object):
"""Gaf file parser"""
def __init__(
self,
gaf_paths: list[str],
output: str,
assembly: str,
vntr: Optional[str] = None,
cent: Optional[str] = None,
l1: Optional[str] = None,
) -> None:
"""
parameters
------------
gaf_paths: a list of gaf files, restricted to tumor[0] and normal[1] pair
"""
self.gaf_paths = gaf_paths
assert len(self.gaf_paths) >= 1
self.output = output
self.vntr = vntr
self.cent = cent
self.l1 = l1
self.assembly = assembly
def parse_sv_on_group_reads(
self,
min_mapq: int = 5,
min_indel_len: int = 50,
min_map_len: int = 2000,
verbose: bool = False,
n_cpus: int = 4,
ds: bool = True,
) -> None:
self.ds = ds
if os.path.exists(f"{self.output}.bed.gz"):
return
if len(self.gaf_paths) == 1:
read_tags = [""]
elif len(self.gaf_paths) == 2:
read_tags = ["tumor", "normal"]
lineno = 0
indel_output = gzip.open(f"{self.output}_indel.bed.gz", "wt")
breakpoint_output = gzip.open(f"{self.output}_brk.bed.gz", "wt")
all_breaks = []
for gaf_path, read_tag in zip(self.gaf_paths, read_tags):
print(gaf_path, read_tag)
if n_cpus == 1:
for grouped_reads in load_gaf_to_grouped_reads(
gaf_path, min_mapq, min_map_len
):
if len(grouped_reads) > 1:
brks = call_breakpoints(grouped_reads)
for brk in brks:
brk[6] = (
f"{read_tag}_{brk[6]}" if read_tag != "" else brk[6]
)
all_breaks.append(brk)
brk_row_str = "\t".join(map(str, brk))
breakpoint_output.write(f"{brk_row_str}\n")
for read in grouped_reads:
lineno += 1
parsed_read = AlignedRead(read)
large_indels_one_read = parsed_read.get_indels(
min_mapq=min_mapq,
min_len=min_indel_len,
dbg=verbose,
lineno=lineno,
ds=ds,
)
for indel in large_indels_one_read:
indel[3] = (
f"{read_tag}_{indel[3]}" if read_tag != "" else indel[3]
)
indel_row_str = "\t".join(map(str, indel))
indel_output.write(f"{indel_row_str}\n")
else:
fin = load_gaf_to_grouped_reads(gaf_path, min_mapq, min_map_len)
with Pool(n_cpus) as pool:
parser = functools.partial(
parse_grouped_reads,
ds=ds,
min_mapq=min_mapq,
min_indel_len=min_indel_len,
verbose=verbose,
)
for group_results in pool.imap(parser, fin, chunksize=300000):
indel_results = group_results.indels
for indel in indel_results:
indel[3] = (
f"{read_tag}_{indel[3]}" if read_tag != "" else indel[3]
)
indel_row_str = "\t".join(map(str, indel))
indel_output.write(f"{indel_row_str}\n")
brk_results = group_results.breakpoints
for brk in brk_results:
brk[6] = (
f"{read_tag}_{brk[6]}" if read_tag != "" else brk[6]
)
all_breaks.append(brk)
brk_row_str = "\t".join(map(str, brk))
breakpoint_output.write(f"{brk_row_str}\n")
indel_output.close()
breakpoint_output.close()
return all_breaks
def merge_breakpts(self, all_breaks):
"""
Merge the similar breakpoints
all_breaks: list of breakpoints, each element is a string
We append the centromere and VNTR to the end of the merged breakpoints file
"""
self.breakpt_file = f"{self.output}_mergedbreaks.bed.gz"
break_merged_file = gzip.open(f"{self.output}_mergedbreaks.bed.gz", "wt")
merged_breakpt_str_list, merged_breakpt_vcf_list = merge_breaks(all_breaks)
merged_breakpt_str_list_with_annotation = []
for breakpt in merged_breakpt_str_list:
breakpt = breakpt.split("\t")
ctg = breakpt[0]
start = int(breakpt[1])
ctg2 = breakpt[3]
start2 = int(breakpt[4])
cent_hit = False
vntr_hit = False
cent_hit2 = False
vntr_hit2 = False
cent_dist = ""
if self.vntr is not None:
if ctg in self.vntr_sites_dict:
vntr_hit = (
len(self.vntr_sites_dict[ctg].overlap(start, start + 1)) > 0
)
else:
vntr_hit = "N/A"
if ctg2 in self.vntr_sites_dict:
vntr_hit2 = (
len(self.vntr_sites_dict[ctg2].overlap(start2, start2 + 1)) > 0
)
else:
vntr_hit2 = "N/A"
else:
vntr_hit, vntr_hit2 = False, False
if self.cent is not None:
if ctg in self.cent_sites_dict:
cent_hit = (
len(self.cent_sites_dict[ctg].overlap(start, start + 1)) > 0
)
else:
cent_hit = "N/A"
if ctg2 in self.cent_sites_dict:
cent_hit2 = (
len(self.cent_sites_dict[ctg2].overlap(start2, start2 + 1)) > 0
)
else:
cent_hit2 = "N/A"
# measure the distance between indel middle point and centromere
cent_dist_start = self.get_dist_to_cen(ctg, start)
cent_dist_end = self.get_dist_to_cen(ctg2, start2)
cent_dist = f"{cent_dist_start}:{cent_dist_end}"
else:
cent_dist = "N/A"
breakpt.append(f"{cent_hit},{cent_hit2},{vntr_hit},{vntr_hit2},{cent_dist}")
merged_breakpt_str_list_with_annotation.append("\t".join(breakpt))
break_merged_file.write("\n".join(merged_breakpt_str_list_with_annotation))
break_merged_file.close()
return merged_breakpt_vcf_list
def parse_indel(
self,
min_mapq: int = 5,
min_len: int = 100,
verbose: bool = False,
n_cpus: int = 4,
ds: bool = True,
) -> None:
self.ds = ds
lineno = 0
# if os.path.exists(f"{self.output}.bed.gz"):
# return
output = gzip.open(f"{self.output}_indel.bed.gz", "wt")
if len(self.gaf_paths) == 1:
read_tags = [""]
elif len(self.gaf_paths) == 2:
read_tags = ["tumor", "normal"]
for gaf_path, read_tag in zip(self.gaf_paths, read_tags):
print(gaf_path, read_tag)
with open(gaf_path) as fin:
if n_cpus == 1:
for line in fin:
lineno += 1
read = line.strip().split()
parsed_read = AlignedRead(read)
large_indels_one_read = parsed_read.get_indels(
min_mapq=min_mapq,
min_len=min_len,
dbg=verbose,
lineno=lineno,
ds=ds,
)
for indel in large_indels_one_read:
indel[3] = (
f"{read_tag}_{indel[3]}" if read_tag != "" else indel[3]
)
indel_row_str = "\t".join(map(str, indel))
output.write(f"{indel_row_str}\n")
else:
with Pool(n_cpus) as pool:
parser = functools.partial(
parse_one_line, ds=ds, min_mapq=min_mapq, min_len=min_len
)
for result in pool.imap(parser, fin, chunksize=10000):
for indel in result:
output.write(
f"{indel[0]}\t{indel[1]}\t{indel[2]}\t{read_tag}_{indel[3]}\t{indel[4]}\t{indel[5]}\t{indel[6]}\t{indel[7]}\t{indel[8]}\t{indel[9]}\n"
)
output.close()
def bed2vcf(self, command: str = "", merged_breakpt_vcf_list=[]):
"""Convert merged indel bed file to a vcf file and index by bgzip and tabix"""
current_time = datetime.now()
formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
vcf_output = open(f"{self.output}_mergedindel.vcf", "w")
# refer to severus and sniffle2
hdr = ["##fileformat=VCFv4.2"]
hdr.append("##source=gafcall1.0")
hdr.append(f'##command="{command}"')
hdr.append(f'##fileDate="{formatted_time}"')
# chm13graph,chm13linear,grch37graph,grch37linear,grch38graph,grch38linear
if self.assembly in [
"chm13graph",
"chm13linear",
"grch38graph",
"grch38linear",
"chm13",
"grch38",
]:
hdr.append(
"""##contig=<ID=chr1>
##contig=<ID=chr2>
##contig=<ID=chr3>
##contig=<ID=chr4>
##contig=<ID=chr5>
##contig=<ID=chr6>
##contig=<ID=chr7>
##contig=<ID=chr8>
##contig=<ID=chr9>
##contig=<ID=chr10>
##contig=<ID=chr11>
##contig=<ID=chr12>
##contig=<ID=chr13>
##contig=<ID=chr14>
##contig=<ID=chr15>
##contig=<ID=chr16>
##contig=<ID=chr17>
##contig=<ID=chr18>
##contig=<ID=chr19>
##contig=<ID=chr20>
##contig=<ID=chr21>
##contig=<ID=chr22>
##contig=<ID=chrX>
##contig=<ID=chrY>
##contig=<ID=chrM>"""
)
elif self.assembly in ["grch37graph", "grch37linear", "grch37"]:
hdr.append(
"""##contig=<ID=1>
##contig=<ID=2>
##contig=<ID=3>
##contig=<ID=4>
##contig=<ID=5>
##contig=<ID=6>
##contig=<ID=7>
##contig=<ID=8>
##contig=<ID=9>
##contig=<ID=10>
##contig=<ID=11>
##contig=<ID=12>
##contig=<ID=13>
##contig=<ID=14>
##contig=<ID=15>
##contig=<ID=16>
##contig=<ID=17>
##contig=<ID=18>
##contig=<ID=19>
##contig=<ID=20>
##contig=<ID=21>
##contig=<ID=22>
##contig=<ID=X>
##contig=<ID=Y>
##contig=<ID=MT>"""
)
hdr.append(
"""##ALT=<ID=INS,Description="Insertion">
##ALT=<ID=DEL,Description="Deletion">
##ALT=<ID=BND,Description="Breakend; Translocation">"""
)
hdr.append('##FILTER=<ID=PASS,Description="All filters passed">')
hdr.append(
"""##INFO=<ID=PRECISE,Number=0,Type=Flag,Description="SV with precise breakpoints coordinates and length">
##INFO=<ID=IMPRECISE,Number=0,Type=Flag,Description="SV with imprecise breakpoints coordinates and length">
##INFO=<ID=SVTYPE,Number=1,Type=String,Description="Type of structural variant">
##INFO=<ID=SVLEN,Number=1,Type=Integer,Description="Length of the SV">
##INFO=<ID=TSDLEN,Number=1,Type=Integer,Description="Length of the SV">
##INFO=<ID=POLYALEN,Number=1,Type=Integer,Description="Length of the SV">
##INFO=<ID=STRANDS,Number=1,Type=String,Description="Breakpoint strandedness">
##INFO=<ID=DETAILED_TYPE,Number=1,Type=Integer,Description="Detailed type of the SV">
##INFO=<ID=MAPQ,Number=1,Type=Integer,Description="Median mapping quality of supporting reads">
##INFO=<ID=VNTR,Number=1,Type=Integer,Description="VNTR support">
##INFO=<ID=CENTROMERE,Number=1,Type=Integer,Description="Overlap with Centromere">
##INFO=<ID=SUPPREAD,Number=1,Type=Integer,Description="Number of supporting reads">
##INFO=<ID=END,Number=1,Type=Integer,Description="End position of the SV">
##INFO=<ID=RNAMES,Number=.,Type=String,Description="Names of supporting reads">
##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">
##FORMAT=<ID=GQ,Number=1,Type=Integer,Description="Genotyping quality">
##FORMAT=<ID=DR,Number=1,Type=Integer,Description="Number of reference reads">
##FORMAT=<ID=DV,Number=1,Type=Integer,Description="Number of variant reads">
##FORMAT=<ID=VAF,Number=1,Type=Float,Description="Variant allele frequency">"""
)
for h in hdr:
vcf_output.write(f"{h}\n")
samples_header = ["SAMPLE"]
columns = [
"#CHROM",
"POS",
"ID",
"REF",
"ALT",
"QUAL",
"FILTER",
"INFO",
"FORMAT",
] + samples_header
columns_str = "\t".join(columns)
vcf_output.write(f"{columns_str}\n")
# merged indel bed column names
# [ctg, start, end, len_str, # average indel length
# tsd_len_str, # average tsd length
# polyA_len_str, # average polyA length
# indel_seq, # randomly pick one
# n, ".", f"mq:i:{mapq}", f"cf:i:{nf}", f"cr:i:{nr}", vntr, centromere, f"rd:Z:+/-readname"]
with gzip.open(f"{self.output}_mergedindel.bed.gz", "rt") as merge_indel_file:
indel_num = 0
for line in merge_indel_file:
line = line.strip().split("\t")
if not (
re.match(r"^[><HNC]", line[0]) or "#" in line[0] or "_" in line[0]
): # remove non-linear genome
type = "INS" if int(line[3]) > 0 else "DEL"
start = int(line[1]) + 1
end = int(line[2])
# NOTE: shall we uppercase this base pair
# QUAL is assigned to . now, not sure how to compute it without PHRED score in bam since we input GAF/PAF, maybe not necessary for filter
# FILTER is set to PASS since we output only INDEL with supported read >= 3
# SV ID
ALT = line[6].upper() if type == "INS" else "<DEL>"
# Sniffle2 INFO: PRECISE;SVTYPE=INS;SVLEN=76;END=10872;SUPPORT=9;COVERAGE=8,9,9,11,10;STRAND=+-;AF=1.000;STDEV_LEN=8.319;STDEV_POS=4.930;SUPPORT_LONG=0
# Severus INFO: IMPRECISE;SVTYPE=INS;SVLEN=246;CHR2=chr1;END=34802253;DETAILED_TYPE=None;INSLEN=0;MAPQ=60;SUPPREAD=29;HVAF=0.00|0.00|0.50;CLUSTERID=severus_3
# How to tell if detailed type is BFB_foldback
# No need for CHR2 now since no breakpoint added yet
# Not sure how other tool define PRECISE/IMPRECISE
mapq = line[9].replace("mq:i:", "")
reads = line[-2].replace("rd:Z:", "")
VNTR = line[-5] == "True"
CENT = line[-4] == "True"
# GT:GQ:VAF:DR:DV 0|1:279:0.45:36:29
# Not sure how to decide GT, GQ and VAF, DR yet
vcf_output.write(
f"{line[0]}\t{start}\tgafcall1.{type}.{indel_num}\tN\t{ALT}\t.\tPASS\tEND={end};SVTYPE={type};SVLEN={line[3]};TSDLEN={line[4]};POLYALEN={line[5]};DETAILED_TYPE=None;MAPQ={mapq};SUPPREAD={line[7]};VNTR={VNTR};CENTROMERE={CENT};RNAMES={reads}\tGT:GQ:VAF:DR:DV\t.:.:.:.:{line[7]}\n"
)
indel_num += 1
vcf_output.close()
breakpoint_vcf_output = open(f"{self.output}_mergedbreakpoint.vcf", "w")
for line in merged_breakpt_vcf_list:
breakpoint_vcf_output.write(f"{line}\tGT:GQ:VAF:DR:DV\t.:.:.:.:.\n")
breakpoint_vcf_output.close()
merge_breakpoint_indel_vcf(
self.output,
f"{self.output}_mergedindel.vcf",
f"{self.output}_mergedbreakpoint.vcf",
)
def parse_tree_bed(self, bed, window_size=10) -> dict:
bed_dict: dict[Any, Any] = {} # type: ignore
extension = window_size // 2
with open(bed) as bed_file:
for line in bed_file:
line = line.strip().split()
if line[0] not in bed_dict:
bed_dict[line[0]] = intervaltree.IntervalTree()
bed_dict[line[0]].add(
Interval(int(line[1]) - extension, int(line[2]) + extension)
)
return bed_dict
def parse_linear_bed(self):
centromeres = {}
if "grch37" in self.assembly:
# NOTE: no centromere annotation available
return centromeres
if self.cent is not None:
with open(self.cent, "r") as f:
for line in f:
s = line.strip().split("\t")
if s[0] in centromeres:
centromeres[s[0]][0].append(int(s[1]))
centromeres[s[0]][1].append(int(s[2]))
else:
centromeres[s[0]] = [[int(s[1])], [int(s[2])]]
for key in centromeres:
# start pos of centromere
centromeres[key][0] = min(centromeres[key][0])
# end pos of centromere
centromeres[key][1] = max(centromeres[key][1])
return centromeres
def get_dist_to_cen(self, chrom, loc):
"""chrom: breakpoints chromomosome, loc: breakpoints"""
if chrom not in self.cent_sites_linear_dict.keys():
return "N/A"
cen_locs = self.cent_sites_linear_dict[chrom]
dist = 0
if loc < min(cen_locs):
dist = min(cen_locs) - loc
elif loc > max(cen_locs):
dist = loc - max(cen_locs)
return str(dist)
def merge_indel(
self,
min_mapq: int = 5,
min_cnt: int = 1,
win_size: int = 100,
max_diff: float = 0.05,
):
"""Merge the similar indels
If the two indel overlap and have the same type and similar length, then merge together
"""
if self.l1 is not None:
minimap2 = mp.Aligner(self.l1)
if not minimap2:
raise Exception("Error: failed to load/build index")
if self.vntr is not None:
self.vntr_sites_dict = self.parse_tree_bed(self.vntr)
if self.cent is not None:
self.cent_sites_dict = self.parse_tree_bed(self.cent)
self.cent_sites_linear_dict = self.parse_linear_bed()
self.indel_file = f"{self.output}_mergedindel.bed.gz"
merged_output = gzip.open(f"{self.output}_mergedindel.bed.gz", "wt")
def print_bed(ctg, t):
# t is bj below: [start, end, ., mapq, ., indel length, [indel length], [mapq], [tsd length], [polyA length], [indel seq], [strandreadname]]
n = len(t[-1]) # number of reads
if n < min_cnt:
return
length, mapq, tsd_length, polyA_length = 0, 0, 0, 0
# forward and reverse strand reads
nf, nr = 0, 0
for i in range(n):
length += t[6][i]
mapq += t[7][i]
if self.ds: # gaf file
tsd_length += t[8][i]
polyA_length += t[9][i]
# 11th element
if t[-1][i][0] == "+":
nf += 1
else:
nr += 1
indel_seq = "."
# TODO: convert multiple sequences into concensus sequence in the next version
if self.ds: # gaf file, pick a arbitrary sequence
indel_seq = t[10][0]
mapq = math.floor(mapq / n + 0.499)
if mapq < min_mapq:
return
length = math.floor(length / n + 0.499)
len_str = f"+{length}" if length > 0 else str(length)
if self.ds:
tsd_length = math.floor(tsd_length / n + 0.499)
# Check if TSD can be deletion
tsd_len_str = f"+{tsd_length}" if tsd_length > 0 else str(tsd_length)
polyA_length = math.floor(polyA_length / n + 0.499)
# Check if polyA can be deletion
polyA_len_str = (
f"+{polyA_length}" if polyA_length > 0 else str(polyA_length)
)
else:
tsd_len_str = "0"
polyA_len_str = "0"
cent_hit = False
vntr_hit = False
if self.vntr is not None:
if ctg in self.vntr_sites_dict:
vntr_hit = len(self.vntr_sites_dict[ctg].overlap(t[0], t[1])) > 0
else:
vntr_hit = "N/A"
cent_dist = ""
if self.cent is not None:
if ctg in self.cent_sites_dict:
cent_hit = len(self.cent_sites_dict[ctg].overlap(t[0], t[1])) > 0
else:
cent_hit = "N/A"
# measure the distance between indel middle point and centromere
cent_dist_start = self.get_dist_to_cen(ctg, t[0])
cent_dist_end = self.get_dist_to_cen(ctg, t[1])
cent_dist = f"{cent_dist_start},{cent_dist_end}"
else:
cent_dist = "N/A"
if self.l1 is not None:
if length > 0: # only apply to insertion for L1
hits = minimap2.map(indel_seq)
hits_list = []
for hit in hits:
hits_list.append((hit.ctg, hit.mlen / hit.ctg_len))
hits_list.sort(key=lambda x: x[-1])
if len(hits_list) == 0:
best_hit = [".", 0]
else:
best_hit = hits_list[-1]
else:
best_hit = [".", 0]
else:
best_hit = [".", 0]
output_str = "\t".join(
map(
str,
[
ctg,
t[0],
t[1],
len_str, # average indel length
tsd_len_str, # average tsd length
polyA_len_str, # average polyA length
indel_seq, # randomly pick one
n,
".", # 8
f"mq:i:{mapq}",
f"cf:i:{nf}",
f"cr:i:{nr}",
vntr_hit,
cent_hit,
f"{best_hit[0]}:{best_hit[1]}",
f"rd:Z:{','.join(t[-1])}", # +/-readname
cent_dist,
],
)
)
merged_output.write(f"{output_str}\n")
# [path, start, end, read_name, mapq, strand, indel length, tsd length, polyA length, indel seq]
merged_indel_dict = {}
with gzip.open(f"{self.output}_indel.bed.gz", "rt") as indel_read_output:
for line in indel_read_output:
t = line.strip().split("\t")
t[1] = int(t[1])
t[2] = int(t[2])
t[4] = int(t[4])
t[6] = int(t[6])
t[7] = int(t[7])
t[8] = int(t[8])
ctg = t.pop(0)
if ctg not in merged_indel_dict:
merged_indel_dict[ctg] = []
merged_indel_dict[ctg].append(t)
# iterate through contig
for ctg in merged_indel_dict:
# key: contig, value:[start, end, read_name, mapq, strand, indel length, tsd length, polyA length, indel seq]
# sort indels by start, end, indel length, and strand, read name
# this will fix the merge indel issue
merged_indel_dict[ctg].sort(key=lambda x: (x[0], x[1], x[5], x[4], x[2]))
# sanity check reads before merging
# merged_indel_dict[ctg].sort(key=lambda x: x[0])
# for key in ctg:
# for indel in merged_indel_dict[ctg]:
# test='\t'.join(map(str, indel))
# print(f"{test}")
# all indels
a = merged_indel_dict[ctg]
# candidate merged indel
b = []
# iterate through indel
for i in range(len(a)):
# ith indel or L1 element
ai = a[i]
# compare ai with existing merged indels
while len(b) > 0:
# indel ai start position larger than indel end position
if ai[0] - b[0][1] > win_size:
t = b.pop(0)
print_bed(ctg, t)
else:
break
merge_j = -1
# where merge starts
for j in range(len(b) - 1, -1, -1):
# start from the closest indel to ai
# ai: [start, end, read_name, mapq, strand, indel length, tsd length, polyA length, indel seq]
# bj: [start, end, ., mapq, ., indel length, [indel length], [mapq], [tsd length], [polyA length], [indel seq], [strandreadname]]
bj = b[j]
if bj[5] * ai[5] <= 0:
# bj and ai are not the same SV type,
# deletion cannot merge with insertion
# it is not break here...
# continue to compare with next candidate merged indel
continue
# ai and bj indel length
la = ai[5] if ai[5] > 0 else -ai[5]
# BUG!
# lb = bj[5] if bj[5] > 0 else -bj[5]
# insertion
if all([bjj > 0 for bjj in bj[6]]):
# conservative minimum distance between ai and a list of bj indels
diff = min([abs(la - bjj) for bjj in bj[6]])
# ai and bj indel maximum length
max_indel = max(bj[6] + [la])
# deletion
elif all([bjj < 0 for bjj in bj[6]]):
# minimum distance between ai and a list of bj indels
diff = min([abs(la - (-bjj)) for bjj in bj[6]])
# ai and bj indel maximum length
max_indel = max(-min(bj[6]), la)
else:
raise Exception("Bug!")
# two indels are different skip merge
# sorted order may change this result
if diff > max_indel * max_diff:
# continue to compare with next candidate merged indel
continue
# indel length
bj[6].append(ai[5])
# mapq
bj[7].append(ai[3])
if self.ds: # gaf file
# tsd length
bj[8].append(ai[6])
# polyA length
bj[9].append(ai[7])
# indel seq
bj[10].append(ai[6])
# read name
bj[11].append(f"{ai[4]}{ai[2]}")
else: # paf file
raise Exception("Not implemented yet")
# reassign the end point
# if bj contains the ai indel
# use bj end else ai end
bj[1] = bj[1] if bj[1] > ai[1] else ai[1]
merge_j = j
# ai indel only merge one time successfully
# with the last bj indel in the candidate merged list
# then break
break
# empty indel b list, initialize with first ai indel
# or ai is different from bj based on max_diff and sv_type
# put in the ai into b
if merge_j < 0:
# ai: [start, end, read_name, mapq, strand, indel length, tsd length, polyA length, indel seq]
# bj: [start, end, ., mapq, ., indel length, [indel length], [mapq], [tsd length], [polyA length], [indel seq], [strandreadname]]
if self.ds: # gaf file with indel seq
b.append(
[
ai[0],
ai[
1
], # always the most recent/largest coordinate indel
".",
ai[3],
".",
ai[
5
], # NOTED issue: only a constant first indel length is used for max diff compute
[ai[5]],
[ai[3]],
[ai[6]],
[ai[7]],
[ai[8]],
[f"{ai[4]}{ai[2]}"],
]
)
else: # paf file
# paf output from minimap2 do not have ai[6-8]: tsd, polyA, indel seq yet
# ai [start, end, readname, mapq, strand, indel length]
# bj [start, end, ., mapq, ., indel length, [indel length], [mapq], [strandreadname]]
b.append(
[
ai[0],
ai[1],
".",
ai[3],
".",
ai[5],
[ai[5]],
[ai[3]],
[f"{ai[4]}{ai[2]}"],
]
)
# output indel that are skipped in the merging for loop
while len(b) > 0:
t = b.pop(0)
print_bed(ctg, t)
merged_output.close()
def parse_tsd(
self, min_mapq: int = 5, min_len: int = 100, verbose: bool = False
) -> None:
output = open(f"{self.output}_tsd.bed", "w")
with open(self.gaf_path) as fin:
for line in fin:
read = line.strip().split()
parsed_read = AlignedRead(read)
large_tsds = parsed_read.get_tsd(
min_mapq=min_mapq, min_len=min_len, dbg=verbose
)
if large_tsds:
for tsd in large_tsds:
tsd_row_str = "\t".join(map(str, tsd))
output.write(f"{tsd_row_str}\n")
output.close()
class AlignedRead(object):
"""Parse one read from the gaf file"""
def __init__(self, read: list):
self.read = read
@property
def querylen(self):
return int(self.read[1])
@property
def strand(self):
return self.read[4]
@property
def path(self):
return self.read[5]
@property
def mapq(self):
return int(self.read[11])
@property
def cigar(self):
# to be compatible with latest minigraph
# https://github.com/lh3/minigraph/commit/54c74fc76b8b946e74aa6f44b559503b5821f12d
if "ds:Z" in self.read[-1]:
return self.read[-2]
return self.read[-1]
@property
def path_start(self):
"""https://github.com/lh3/gfatools/blob/master/doc/rGFA.md"""
return int(self.read[7])
@property
def path_end(self):
return int(self.read[8])
@property
def ds_Z(self):
"""Parse the ds:Z: tag"""
# to be compatible with latest minigraph
# https://github.com/lh3/minigraph/commit/54c74fc76b8b946e74aa6f44b559503b5821f12d
if "ds:Z" in self.read[-1]:
return self.read[-1][5:]
raise Exception("Upgrade your minigraph to 0.20-r574-dirty or later")
def convert_type(self):
"""Convert coordinate column into integers"""
# convert element types
for i in range(1, 4):
self.read[i] = int(self.read[i])
for i in range(6, 12):
self.read[i] = int(self.read[i])
def get_indels(
self,
lineno: int,
min_mapq: int = 5,
min_len: int = 100,
max_cnt: int = 5,
min_frac: float = 0.7,
ds: bool = True,
polyA_pen: int = 5,
polyA_drop: int = 100,
dbg: bool = False,
) -> list:
"""Get indels from one aligned long read in GAF or PAF file
Update script from https://github.com/lh3/minigraph/blob/master/misc/mgutils-es6.js#L232-L401
"""
# at least 12 columns for one read
# read should be minigraph or minimap2 output
if len(self.read) < 12:
return []
self.convert_type()
# low mapping quality
if self.mapq < min_mapq:
return []
# mapped fraction is low
if self.read[3] - self.read[2] < self.read[1] * min_frac:
return []
cg_segs = cigar_pattern.findall(self.cigar)
# record cigar indels
a = []
x = self.path_start
for length, op in cg_segs:
length = int(length)
if dbg:
print("X0", x, length, op)
if length >= min_len:
if op == "I":
# [start, end, indel length, tsd length, polyA length, tsd seq, indel seq]
a.append([x - 1, x + 1, length, 0, 0, "", ""])
elif op == "D":
a.append([x, x + length, -length, 0, 0, "", ""])
if op in ["M", "D", "=", "X"]:
x += length
if dbg:
print("X0", a)
print("X0", self.path_start)
print("X0", self.cigar, self.read[0])
# No indel cigar or too many
if len(a) == 0 or len(a) > max_cnt:
return []
if ds:
i = 0 # indel index for one read
x = self.path_start
ds_Z = self.ds_Z
ds_segs_iter = ds_pattern.findall(ds_Z)
for op, ds_str in ds_segs_iter:
seq = re.sub(r"[\]\[]", "", ds_str) if op in ["+", "-"] else ""
if op == ":":
length = int(ds_str)
elif op == "*":
length = 1
elif op in ["+", "-"]:
length = len(seq)
else:
length = -1
assert length > 0, "not found ds:Z supported tag"
if length >= min_len:
if op == "+": # insertion
# a: [start, end, indel length, tsd length, polyA length, tsd seq, indel seq]
if a[i][0] != x - 1 or a[i][1] != x + 1 or a[i][2] != length:
raise Exception(
f"CIGAR and ds insertion not consistent line number {lineno}"
)
a[i][5] = ds_str
i += 1
elif op == "-": # deletion
if a[i][0] != x or a[i][1] != x + length or a[i][2] != -length:
raise Exception(
f"CIGAR and ds deletion not consistent {lineno}"
)
a[i][5] = ds_str
i += 1