-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfunc.py
884 lines (728 loc) · 33 KB
/
func.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
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
##################################################################################
##################################################################################
#import
import sys
import csv
import pysam
import numpy as np
import math
import get_conf_int
import validate
import get_align_info
##################################################################################
##################################################################################
#ttmars.py
########################################
########################################
#define class
class struc_var:
def __init__(self, idx, ref_name, sv_type, sv_pos, sv_stop, length, gt, wrong_len):
self.idx = idx
self.ref_name = ref_name
self.sv_pos = sv_pos
self.sv_stop = sv_stop
self.sv_type = sv_type
self.length = length
self.gt = gt
self.wrong_len = wrong_len
#if the call is part of an aggregate SV
self.is_agg = False
#if second filtered out
self.is_sec_fil = False
self.is_third_fil = False
self.query_name_hap1 = "NA"
self.query_name_hap2 = "NA"
self.ref_start_best_hap1 = -1
self.ref_end_best_hap1 = -1
self.query_start_best_hap1 = -1
self.query_end_best_hap1 = -1
self.ref_start_best_hap2 = -1
self.ref_end_best_hap2 = -1
self.query_start_best_hap2 = -1
self.query_end_best_hap2 = -1
self.analyzed_hap1 = False
self.analyzed_hap2 = False
self.len_query_hap1 = -1
self.len_ref_hap1 = -1
self.len_query_hap2 = -1
self.len_ref_hap2 = -1
self.score_before_hap1 = -1
self.score_after_hap1 = -1
self.score_before_hap2 = -1
self.score_after_hap2 = -1
self.neg_strand_hap1 = False
self.neg_strand_hap2 = False
self.ins_seq = ""
self.if_seq_resolved = False
#for dup validation
self.valid_non_ins = False
def check_tp(self, rela_len, rela_score):
result = True
if self.sv_type in ['DEL', 'DUP', 'DUP:TANDEM']:
if rela_score >= 0 and rela_score <= 2.5:
if rela_len >= -0.05*rela_score + 0.8 and rela_len <= 0.05*rela_score + 1.2:
result = True
else:
result = False
elif rela_score > 2.5:
if rela_len >= 0.675 and rela_len <= 1.325:
result = True
else:
result = False
else:
result = False
elif self.sv_type == 'INS':
#not seq-resolved
#if len(self.ins_seq) == 0:
if not self.if_seq_resolved:
if rela_len < 0.675 or rela_len > 1.325:
result = False
#seq-resolved
else:
if rela_score >= 0 and rela_score <= 2.5:
if rela_len >= -0.05*rela_score + 0.8 and rela_len <= 0.05*rela_score + 1.2:
result = True
else:
result = False
elif rela_score > 2.5:
if rela_len >= 0.675 and rela_len <= 1.325:
result = True
else:
result = False
else:
result = False
elif self.sv_type == 'INV':
if rela_score <= 0:
result = False
return result
#TP when wrong length flag presents -- looser rules for TP
def check_tp_wlen(self, rela_len, rela_score):
result = True
if self.sv_type in ['DEL', 'DUP', 'DUP:TANDEM']:
if rela_score >= 0 and rela_score <= 2.5:
if rela_len >= -0.05*rela_score + 0.6 and rela_len <= 0.05*rela_score + 1.4:
result = True
else:
result = False
elif rela_score > 2.5:
if rela_len >= 0.475 and rela_len <= 1.525:
result = True
else:
result = False
else:
result = False
elif self.sv_type == 'INS':
#not seq-resolved
#if len(self.ins_seq) == 0:
if not self.if_seq_resolved:
if rela_len < 0.475 or rela_len > 1.525:
result = False
#seq-resolved
else:
if rela_score >= 0 and rela_score <= 2.5:
if rela_len >= -0.05*rela_score + 0.6 and rela_len <= 0.05*rela_score + 1.4:
result = True
else:
result = False
elif rela_score > 2.5:
if rela_len >= 0.475 and rela_len <= 1.525:
result = True
else:
result = False
else:
result = False
elif self.sv_type == 'INV':
if rela_score <= 0:
result = False
return result
def print_info(self):
print(self.idx, self.ref_name, self.sv_pos, self.sv_stop, self.sv_type, self.length, self.gt, self.is_agg, self.is_sec_fil, self.is_third_fil)
def cal_rela_score(self, score_before, score_after):
if score_before > -1 and score_before < 0:
tmp_score_before = -1
tmp_score_after = score_after + (tmp_score_before - score_before)
return round((tmp_score_after - tmp_score_before) / abs(tmp_score_before), 2)
elif score_before >= 0 and score_before < 1:
tmp_score_before = 1
tmp_score_after = score_after + (tmp_score_before - score_before)
return round((tmp_score_after - tmp_score_before) / abs(tmp_score_before), 2)
else:
return round((score_after - score_before) / abs(score_before), 2)
def cal_rela_len(self, query_len, ref_len):
return round((query_len - ref_len) / self.length, 2)
def get_vali_res(self):
if (not self.analyzed_hap1) or (not self.analyzed_hap2):
return -1
if self.analyzed_hap1 and self.analyzed_hap2:
rela_len_1 = self.cal_rela_len(self.len_query_hap1, self.len_ref_hap1)
rela_len_2 = self.cal_rela_len(self.len_query_hap2, self.len_ref_hap2)
rela_score_1 = self.cal_rela_score(self.score_before_hap1, self.score_after_hap1)
rela_score_2 = self.cal_rela_score(self.score_before_hap2, self.score_after_hap2)
if not self.wrong_len:
res_hap1 = self.check_tp(rela_len_1, rela_score_1)
res_hap2 = self.check_tp(rela_len_2, rela_score_2)
else:
res_hap1 = self.check_tp_wlen(rela_len_1, rela_score_1)
res_hap2 = self.check_tp_wlen(rela_len_2, rela_score_2)
gt_validate = False
if self.gt:
if res_hap1 and res_hap2:
if self.gt == (1,1):
gt_validate = True
elif res_hap1 or res_hap2:
if self.gt == (1,0) or self.gt == (0,1):
gt_validate = True
if res_hap1 and res_hap2:
if abs(rela_len_1 - 1) <= abs(rela_len_2 - 1):
return (res_hap1, rela_len_1, rela_score_1, gt_validate)
else:
return (res_hap2, rela_len_2, rela_score_2, gt_validate)
elif res_hap1:
return (res_hap1, rela_len_1, rela_score_1, gt_validate)
elif res_hap2:
return (res_hap2, rela_len_2, rela_score_2, gt_validate)
else:
if abs(rela_len_1 - 1) <= abs(rela_len_2 - 1):
return (res_hap1, rela_len_1, rela_score_1, gt_validate)
else:
return (res_hap2, rela_len_2, rela_score_2, gt_validate)
def get_vali_res_reg_dup(self):
if self.sv_type == 'DUP':
#if there's no valid non ins for a DUP call, won't validate it
if not self.valid_non_ins:
self.analyzed_hap1 = False
self.analyzed_hap2 = False
if (not self.analyzed_hap1) or (not self.analyzed_hap2):
return -1
if self.analyzed_hap1 and self.analyzed_hap2:
rela_len_1 = self.cal_rela_len(self.len_query_hap1, self.len_ref_hap1)
rela_len_2 = self.cal_rela_len(self.len_query_hap2, self.len_ref_hap2)
rela_score_1 = self.cal_rela_score(self.score_before_hap1, self.score_after_hap1)
rela_score_2 = self.cal_rela_score(self.score_before_hap2, self.score_after_hap2)
if not self.wrong_len:
res_hap1 = self.check_tp(rela_len_1, rela_score_1)
res_hap2 = self.check_tp(rela_len_2, rela_score_2)
else:
res_hap1 = self.check_tp_wlen(rela_len_1, rela_score_1)
res_hap2 = self.check_tp_wlen(rela_len_2, rela_score_2)
gt_validate = False
if self.gt:
if res_hap1 and res_hap2:
if self.gt == (1,1):
gt_validate = True
elif res_hap1 or res_hap2:
if self.gt == (1,0) or self.gt == (0,1):
gt_validate = True
if res_hap1 and res_hap2:
if abs(rela_len_1 - 1) <= abs(rela_len_2 - 1):
return (res_hap1, rela_len_1, rela_score_1, gt_validate)
else:
return (res_hap2, rela_len_2, rela_score_2, gt_validate)
elif res_hap1:
return (res_hap1, rela_len_1, rela_score_1, gt_validate)
elif res_hap2:
return (res_hap2, rela_len_2, rela_score_2, gt_validate)
else:
if abs(rela_len_1 - 1) <= abs(rela_len_2 - 1):
return (res_hap1, rela_len_1, rela_score_1, gt_validate)
else:
return (res_hap2, rela_len_2, rela_score_2, gt_validate)
def get_vali_res_chrx(self):
if (not self.analyzed_hap1) and (not self.analyzed_hap2):
return -1
elif self.analyzed_hap1 and self.analyzed_hap2:
rela_len_1 = self.cal_rela_len(self.len_query_hap1, self.len_ref_hap1)
rela_len_2 = self.cal_rela_len(self.len_query_hap2, self.len_ref_hap2)
rela_score_1 = self.cal_rela_score(self.score_before_hap1, self.score_after_hap1)
rela_score_2 = self.cal_rela_score(self.score_before_hap2, self.score_after_hap2)
if not self.wrong_len:
res_hap1 = self.check_tp(rela_len_1, rela_score_1)
res_hap2 = self.check_tp(rela_len_2, rela_score_2)
else:
res_hap1 = self.check_tp_wlen(rela_len_1, rela_score_1)
res_hap2 = self.check_tp_wlen(rela_len_2, rela_score_2)
gt_validate = False
if self.gt:
if res_hap1 and res_hap2:
if self.gt == (1,1):
gt_validate = True
elif res_hap1 or res_hap2:
if self.gt == (1,0) or self.gt == (0,1):
gt_validate = True
if res_hap1 and res_hap2:
if abs(rela_len_1 - 1) <= abs(rela_len_2 - 1):
return (res_hap1, rela_len_1, rela_score_1, gt_validate)
else:
return (res_hap2, rela_len_2, rela_score_2, gt_validate)
elif res_hap1:
return (res_hap1, rela_len_1, rela_score_1, gt_validate)
elif res_hap2:
return (res_hap2, rela_len_2, rela_score_2, gt_validate)
else:
if abs(rela_len_1 - 1) <= abs(rela_len_2 - 1):
return (res_hap1, rela_len_1, rela_score_1, gt_validate)
else:
return (res_hap2, rela_len_2, rela_score_2, gt_validate)
elif self.analyzed_hap1:
rela_len_1 = self.cal_rela_len(self.len_query_hap1, self.len_ref_hap1)
rela_score_1 = self.cal_rela_score(self.score_before_hap1, self.score_after_hap1)
res_hap1 = self.check_tp(rela_len_1, rela_score_1)
gt_validate = False
if self.gt:
if res_hap1:
if self.gt == (1,0) or self.gt == (0,1):
gt_validate = True
return (res_hap1, rela_len_1, rela_score_1, gt_validate)
elif self.analyzed_hap2:
rela_len_2 = self.cal_rela_len(self.len_query_hap2, self.len_ref_hap2)
rela_score_2 = self.cal_rela_score(self.score_before_hap2, self.score_after_hap2)
res_hap2 = self.check_tp(rela_len_2, rela_score_2)
gt_validate = False
if self.gt:
if res_hap2:
if self.gt == (1,0) or self.gt == (0,1):
gt_validate = True
return (res_hap2, rela_len_2, rela_score_2, gt_validate)
class alignment:
def __init__(self, idx, agt_rec, hap, query_length):
self.idx = idx
self.ref_name = 'NA'
self.ref_start = -1
self.ref_end = -1
self.contig_name = agt_rec.reference_name
self.contig_start = agt_rec.reference_start
self.contig_end = agt_rec.reference_end
self.query_name = agt_rec.query_name
#This the index of the first base in seq that is not soft-clipped
self.query_start = agt_rec.query_alignment_start
self.query_end = agt_rec.query_alignment_end
#the index of the last base in seq that is not soft-clipped - the index of the first base in seq that is not soft-clipped
self.aligned_length = agt_rec.query_alignment_length
#use query length from the fasta file instead!!!
self.query_length = query_length
self.hap = hap
def cal_aligned_portion(self):
return self.aligned_length/self.query_length
def cal_ins_portion(self):
return 1 - (self.ref_end - self.ref_start)/self.aligned_length
def set_ref_info(self, ref_name, ref_start, ref_end):
self.ref_name = ref_name
self.ref_start = ref_start
self.ref_end = ref_end
def print_info(self):
print(self.idx, self.ref_name, self.ref_start, self.ref_end, self.contig_name, self.contig_start, self.contig_end, self.query_name, self.query_start, self.query_end,\
self.aligned_length, self.query_length, self.hap)
########################################
########################################
#define functions
#return False if not filtered
#first_filter: type, PASS, chr_name
def first_filter(sv, sv_type, valid_types, if_pass_only, chr_list):
#type filter
if sv_type not in valid_types:
return True
#PASS filter
if if_pass_only:
if 'PASS' not in sv.filter.keys():
return True
chr_name = sv.chrom
#chr filter
if chr_name not in chr_list:
return True
return False
#second_filter: centromere, non-cov
def second_filter(sv, if_hg38, dict_centromere, exclude_assem1_non_cover, exclude_assem2_non_cover):
index = sv.idx
ref_name = sv.ref_name
sv_pos = sv.sv_pos
sv_stop = sv.sv_stop
if if_hg38:
centro_start = int(dict_centromere[ref_name][0])
centro_end = int(dict_centromere[ref_name][1])
else:
centro_start = int(dict_centromere['chr'+ref_name][0])
centro_end = int(dict_centromere['chr'+ref_name][1])
#centromere
if (sv_pos > centro_start and sv_pos < centro_end) or (sv_stop > centro_start and sv_stop < centro_end):
sv.is_sec_fil = True
return True
#non-cov
list_to_check = [str(ref_name), str(sv_pos), str(sv_stop)]
#if sv in high-depth regions or non-covered regions, skip
if validate.check_exclude(list_to_check, exclude_assem1_non_cover, exclude_assem2_non_cover):
sv.is_sec_fil = True
return True
#second_filter for chrX: centromere, non-cov
def second_filter_chrx(sv, if_hg38, dict_centromere, exclude_assem1_non_cover, exclude_assem2_non_cover):
index = sv.idx
ref_name = sv.ref_name
sv_pos = sv.sv_pos
sv_stop = sv.sv_stop
if if_hg38:
centro_start = int(dict_centromere[ref_name][0])
centro_end = int(dict_centromere[ref_name][1])
else:
centro_start = int(dict_centromere['chr'+ref_name][0])
centro_end = int(dict_centromere['chr'+ref_name][1])
#centromere
if (sv_pos > centro_start and sv_pos < centro_end) or (sv_stop > centro_start and sv_stop < centro_end):
sv.is_sec_fil = True
return True
#non-cov
list_to_check = [str(ref_name), str(sv_pos), str(sv_stop)]
#if sv in high-depth regions or non-covered regions, skip
if validate.check_exclude_chrx(list_to_check, exclude_assem1_non_cover, exclude_assem2_non_cover):
sv.is_sec_fil = True
return True
#third_filter: size
def third_filter(sv, memory_min, memory_limit, dup_memory_min, dup_memory_limit):
#size
if sv.sv_type not in ['DUP:TANDEM', 'DUP']:
if abs(sv.length) < memory_min or abs(sv.length) > memory_limit:
sv.is_third_fil = True
return True
else:
if abs(sv.length) < dup_memory_min or abs(sv.length) > dup_memory_limit:
sv.is_third_fil = True
return True
#get validation info
def write_vali_info(sv_list, output_dir):
g = open(output_dir + "ttmars_res.txt", "w")
for sv in sv_list:
#skip if not analyzed
if (not sv.analyzed_hap1) or (not sv.analyzed_hap2):
continue
res = sv.get_vali_res()
g.write(str(sv.ref_name) + "\t")
g.write(str(sv.sv_pos) + "\t")
g.write(str(sv.sv_stop) + "\t")
g.write(str(sv.sv_type) + "\t")
g.write(str(res[1]) + "\t")
g.write(str(res[2]) + "\t")
g.write(str(res[0]))
if sv.gt:
g.write("\t" + str(res[3]))
g.write("\n")
g.close()
##################################################################################
##################################################################################
#reg_dup.py
##############################
##############################
#define class
class mappy_alignment:
def __init__(self, ctr, agt_rec, hap, qname, qlen):
self.idx = ctr
self.ref_name = 'NA'
self.ref_start = -1
self.ref_end = -1
self.contig_name = agt_rec.ctg
self.contig_start = agt_rec.r_st
self.contig_end = agt_rec.r_en
self.query_name = qname
#This the index of the first base in seq that is not soft-clipped
self.query_start = agt_rec.q_st
self.query_end = agt_rec.q_en
#the index of the last base in seq that is not soft-clipped - the index of the first base in seq that is not soft-clipped
self.aligned_length = agt_rec.mlen
#use query length from the fasta file instead!!!
self.query_length = qlen
self.hap = hap
self.contig_int_start = max(int(agt_rec.r_st - 0.75*qlen), 1)
self.contig_int_end = min(int(agt_rec.r_en + 0.75*qlen), agt_rec.ctg_len)
self.ref_int_start = -1
self.ref_int_end = -1
def cal_aligned_portion(self):
return self.aligned_length/self.query_length
def cal_ins_portion(self):
return 1 - (self.ref_end - self.ref_start)/self.aligned_length
def set_ref_int_info(self, ref_int_start, ref_int_end):
self.ref_int_start = ref_int_start
self.ref_int_end = ref_int_end
def cal_ins_rela_len(self):
return ((self.contig_int_end - self.contig_int_start) - (self.ref_int_end - self.ref_int_start))/self.query_length
def set_ref_info(self, ref_name, ref_start, ref_end):
self.ref_name = ref_name
self.ref_start = ref_start
self.ref_end = ref_end
def print_info(self):
print(self.idx, self.ref_name, self.ref_start, self.ref_end, self.contig_name, self.contig_start, self.contig_end, self.query_name, self.query_start, self.query_end,\
self.aligned_length, self.query_length, "hap:", self.hap, self.contig_int_start, self.contig_int_end, self.ref_int_start, self.ref_int_end,
self.cal_ins_rela_len())
def build_map_asm_to_ref_compress(query_fasta_file, liftover_file, interval, if_hg38):
ref_name_list = []
ref_pos_list = []
#dict stores: {contig name: [idx, length]}
contig_idx_len = {}
ctr = 0
for contig in query_fasta_file.references:
contig_len = query_fasta_file.get_reference_length(contig)
contig_idx_len[contig] = [ctr, contig_len]
ctr += 1
ref_name_list.append(np.zeros(int((contig_len+1)//interval) + 1, dtype='int16') - 1)
ref_pos_list.append(np.zeros(int((contig_len+1)//interval) + 1, dtype='uint32'))
#print(query_fasta_file.get_reference_length(chrom))
#build a dictionary for contig names: to avoid store too many str
with open(liftover_file) as f:
contig_name_ctr = -1
pre_contig_name = ""
for line in f:
record = line.strip().split()
int_ref_name = get_align_info.get_int_chr_name(record[1], if_hg38)
contig_name = record[0]
contig_idx = contig_idx_len[contig_name][0]
lo_info = record[2].strip().split(";")
for info in lo_info[:len(lo_info)-1]:
info_list = info.strip().split(":")
ctr = int(info_list[2])
strand = info_list[3]
if strand == "+":
forward = True
elif strand == "-":
forward = False
for i in range(0, ctr):
contig_pos = int(info_list[0]) + i*interval
if forward:
ref_pos = int(info_list[1]) + i*interval
else:
ref_pos = int(info_list[1]) - i*interval
#pos in the chr list
contig_list_pos = int(contig_pos//interval)
ref_name_list[contig_idx][contig_list_pos] = int_ref_name
ref_pos_list[contig_idx][contig_list_pos] = ref_pos
f.close()
return ref_name_list, ref_pos_list, contig_idx_len
#check overlap of two ref interval
def check_ol(list1, list2):
#return False: no ol, True: does ol
#list: [ref_name, start, end]
max_valid_ol_len = 0.5 * min(abs(list1[2] - list1[1]), abs(list2[2] - list2[1]))
if list1[0] != list2[0] or list1[1] > list2[2] - max_valid_ol_len or list1[2] < list2[1] + max_valid_ol_len:
return False
return True
#check duplicated alignment
def check_duplicate(dup_list,
cur_dup_info,
contig_idx_len_1,
ref_name_list_1,
ref_pos_list_1,
contig_idx_len_2,
ref_name_list_2,
ref_pos_list_2,
interval,
valid_ins_ratio):
#cur_dup_info: hap, query_name, ref_name, start, end
#dup_list[i][1:6]
for dup in dup_list:
dup_info = dup[1:7]
first_hap = dup_info[0]
second_hap = cur_dup_info[0]
if first_hap == 1:
contig_idx_len_first = contig_idx_len_1
ref_name_list_first = ref_name_list_1
ref_pos_list_first = ref_pos_list_1
else:
contig_idx_len_first = contig_idx_len_2
ref_name_list_first = ref_name_list_2
ref_pos_list_first = ref_pos_list_2
if second_hap == 1:
contig_idx_len_second = contig_idx_len_1
ref_name_list_second = ref_name_list_1
ref_pos_list_second = ref_pos_list_1
else:
contig_idx_len_second = contig_idx_len_2
ref_name_list_second = ref_name_list_2
ref_pos_list_second = ref_pos_list_2
first_contig_idx = contig_idx_len_first[dup_info[2]][0]
second_contig_idx = contig_idx_len_second[cur_dup_info[2]][0]
first_start_ref_name = ref_name_list_first[first_contig_idx][int(dup_info[3]//interval)]
first_end_ref_name = ref_name_list_first[first_contig_idx][int(dup_info[4]//interval)]
second_start_ref_name = ref_name_list_second[second_contig_idx][int(cur_dup_info[3]//interval)]
second_end_ref_name = ref_name_list_second[second_contig_idx][int(cur_dup_info[4]//interval)]
# if first_start_ref_name != first_end_ref_name or second_start_ref_name != second_end_ref_name:
# continue
# if -1 in [first_start_ref_name, first_end_ref_name, second_start_ref_name, second_end_ref_name]:
# continue
first_start = ref_pos_list_first[first_contig_idx][int(dup_info[3]//interval)]
first_end = ref_pos_list_first[first_contig_idx][int(dup_info[4]//interval)]
second_start = ref_pos_list_second[second_contig_idx][int(cur_dup_info[3]//interval)]
second_end = ref_pos_list_second[second_contig_idx][int(cur_dup_info[4]//interval)]
if first_end < first_start:
tmp = first_start
first_start = first_end
first_end = tmp
if second_end < second_start:
tmp = second_start
second_start = second_end
second_end = tmp
first_aligned_len = dup_info[5]
second_aligned_len = cur_dup_info[5]
if (first_end - first_start)/first_aligned_len < (1 - valid_ins_ratio) or (second_end - second_start)/second_aligned_len < (1 - valid_ins_ratio):
continue
if check_ol([first_start_ref_name, first_start, first_end], [second_start_ref_name, second_start, second_end]):
return True
# print([first_start_ref_name, first_start, first_end], [second_start_ref_name, second_start, second_end])
# print(potentail_dup[0][1], potentail_dup[1][1], first_end - first_start, second_end - second_start, potentail_dup[0][5], potentail_dup[1][5], potentail_dup[0][6], potentail_dup[1][6])
return False
#get seq function
def getSeqRec(fasta_file, seq_name):
seq = fasta_file.fetch(seq_name)
return seq
def get_ref_info(alm,
contig_idx_len_1,
ref_name_list_1,
ref_pos_list_1,
contig_idx_len_2,
ref_name_list_2,
ref_pos_list_2,
interval):
if alm.hap == 1:
contig_idx_len = contig_idx_len_1
ref_name_list = ref_name_list_1
ref_pos_list = ref_pos_list_1
else:
contig_idx_len = contig_idx_len_2
ref_name_list = ref_name_list_2
ref_pos_list = ref_pos_list_2
contig_idx = contig_idx_len[alm.contig_name][0]
start_ref_name = ref_name_list[contig_idx][int(alm.contig_start//interval)]
end_ref_name = ref_name_list[contig_idx][int(alm.contig_end//interval)]
#TODO: find the best interval!
if start_ref_name != end_ref_name:
return 'NA', -1, -1
if -1 in [start_ref_name, end_ref_name]:
return 'NA', -1, -1
start = ref_pos_list[contig_idx][int(alm.contig_start//interval)]
end = ref_pos_list[contig_idx][int(alm.contig_end//interval)]
if end < start:
tmp = start
start = end
end = tmp
return start_ref_name, start, end
def find_best_ref_int(alm, contig_idx_len, ref_name_list, ref_pos_list, interval):
contig_int_start = alm.contig_int_start
contig_int_end = alm.contig_int_end
best_rela_len = 100
best_contig_int_start = contig_int_start
best_contig_int_end = contig_int_end
best_ref_int_start = -1
best_ref_int_end = -1
best_ref_name = 'NA'
for i in range(0, (contig_int_end - contig_int_start)//interval * interval, interval):
cur_contig_int_start = contig_int_start + i
cur_contig_int_end = contig_int_end - i
if cur_contig_int_end <= cur_contig_int_start:
break
contig_idx = contig_idx_len[alm.contig_name][0]
cur_start_ref_int_name = ref_name_list[contig_idx][int(cur_contig_int_start//interval)]
cur_end_ref_int_name = ref_name_list[contig_idx][int(cur_contig_int_end//interval)]
#TODO: find the best interval!
if cur_start_ref_int_name != cur_end_ref_int_name:
continue
if -1 in [cur_start_ref_int_name, cur_end_ref_int_name]:
continue
cur_ref_int_start = ref_pos_list[contig_idx][int(cur_contig_int_start//interval)]
cur_ref_int_end = ref_pos_list[contig_idx][int(cur_contig_int_end//interval)]
if cur_ref_int_end < cur_ref_int_start:
tmp = cur_ref_int_start
cur_ref_int_start = cur_ref_int_end
cur_ref_int_end = tmp
cur_rela_len = ((cur_contig_int_end - cur_contig_int_start) - (cur_ref_int_end - cur_ref_int_start))/alm.query_length
if abs(cur_rela_len-1) < abs(best_rela_len-1):
best_rela_len = cur_rela_len
best_ref_int_start = cur_ref_int_start
best_ref_int_end = cur_ref_int_end
best_ref_name = cur_start_ref_int_name
alm.contig_int_start = cur_contig_int_start
alm.contig_int_end = cur_contig_int_end
return best_ref_name, best_ref_int_start, best_ref_int_end
def get_ref_int_info(alm,
contig_idx_len_1,
ref_name_list_1,
ref_pos_list_1,
contig_idx_len_2,
ref_name_list_2,
ref_pos_list_2,
interval):
if alm.hap == 1:
contig_idx_len = contig_idx_len_1
ref_name_list = ref_name_list_1
ref_pos_list = ref_pos_list_1
else:
contig_idx_len = contig_idx_len_2
ref_name_list = ref_name_list_2
ref_pos_list = ref_pos_list_2
#test
# print(alm.hap, alm.contig_int_start, alm.contig_int_end)
start_ref_int_name, start, end = find_best_ref_int(alm, contig_idx_len, ref_name_list, ref_pos_list, interval)
# contig_idx = contig_idx_len[alm.contig_name][0]
# start_ref_int_name = ref_name_list[contig_idx][int(alm.contig_int_start//interval)]
# end_ref_int_name = ref_name_list[contig_idx][int(alm.contig_int_end//interval)]
# #TODO: find the best interval!
# if start_ref_int_name != end_ref_int_name:
# return 'NA', -1, -1
# if -1 in [start_ref_int_name, end_ref_int_name]:
# return 'NA', -1, -1
# start = ref_pos_list[contig_idx][int(alm.contig_int_start//interval)]
# end = ref_pos_list[contig_idx][int(alm.contig_int_end//interval)]
# if end < start:
# tmp = start
# start = end
# end = tmp
return start_ref_int_name, start, end
def fake_tp_sv(sv, hap):
if hap == 1:
sv.analyzed_hap1 = True
sv.len_query_hap1 = 1 + sv.length
sv.len_ref_hap1 = 1
sv.score_before_hap1 = 1
sv.score_after_hap1 = 2
elif hap == 2:
sv.analyzed_hap2 = True
sv.len_query_hap2 = 1 + sv.length
sv.len_ref_hap2 = 1
sv.score_before_hap2 = 1
sv.score_after_hap2 = 2
def get_sv_list_idx(sv_list, sv_idx):
sv_list_idx = -1
for ctr, sv in enumerate(sv_list):
if sv.idx == sv_idx:
sv_list_idx = ctr
break
return sv_list_idx
def write_vali_info_reg_dup(sv_list, output_dir):
g = open(output_dir + "ttmars_regdup_res.txt", "w")
for sv in sv_list:
res = sv.get_vali_res_reg_dup()
#skip if not analyzed
if (not sv.analyzed_hap1) or (not sv.analyzed_hap2):
continue
g.write(str(sv.ref_name) + "\t")
g.write(str(sv.sv_pos) + "\t")
g.write(str(sv.sv_stop) + "\t")
g.write(str(sv.sv_type) + "\t")
g.write(str(res[1]) + "\t")
g.write(str(res[2]) + "\t")
g.write(str(res[0]))
if sv.gt:
g.write("\t" + str(res[3]))
g.write("\n")
g.close()
def write_vali_info_chrx(sv_list, output_dir):
g = open(output_dir + "ttmars_chrx_res.txt", "w")
for sv in sv_list:
#skip if not analyzed
if (not sv.analyzed_hap1) and (not sv.analyzed_hap2):
continue
res = sv.get_vali_res_chrx()
g.write(str(sv.ref_name) + "\t")
g.write(str(sv.sv_pos) + "\t")
g.write(str(sv.sv_stop) + "\t")
g.write(str(sv.sv_type) + "\t")
g.write(str(res[1]) + "\t")
g.write(str(res[2]) + "\t")
g.write(str(res[0]))
if sv.gt:
g.write("\t" + str(res[3]))
g.write("\n")
g.close()