-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path30_virtual_sky.py
More file actions
1483 lines (1248 loc) · 52.9 KB
/
Copy path30_virtual_sky.py
File metadata and controls
1483 lines (1248 loc) · 52.9 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
import os
import sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from skype_utils import *
import numpy as np
import pandas as pd
from virtual_sky_plotting import render_karyotype_diagram as render_virtual_sky
import pickle as pkl
import csv
import re
import ast
import glob
import logging
import argparse
from collections import defaultdict
from collections import Counter
# logging 설정(레벨/포맷)은 skype_utils 에서 중앙 관리한다 (LOG_LEVEL).
logging.info("30_virtual_sky start")
CTG_NAM = 0
CTG_LEN = 1
CTG_STR = 2
CTG_END = 3
CTG_DIR = 4
CHR_NAM = 5
CHR_LEN = 6
CHR_STR = 7
CHR_END = 8
CTG_MAPQ = 9
CTG_TYP = 10
CTG_STRND = 11
CTG_ENDND = 12
CTG_TELCHR = 13
CTG_TELDIR = 14
CTG_TELCON = 15
CTG_RPTCHR = 16
CTG_RPTCASE = 17
CTG_MAINFLOWDIR = 18
CTG_MAINFLOWCHR = 19
DIR_FOR = 1
DIR_BAK = 1
ABS_MAX_COVERAGE_RATIO = 3
MAX_PATH_CNT = 100
INF = 1000000000
DIR_FOR = 1
TELOMERE_EXPANSION = 5 * K
BND_TYPE = 0
CTG_IN_TYPE = 1
TEL_TYPE = 2
MAJOR_BASELINE = 0.6
TARGET_DEPTH = 0.2
MEANDEPTH_FLANKING_LENGTH = 5*M
TYPE34_BREAK_CHUKJI_LIMIT = 1*M
TYPE4_CLUSTER_SIZE = 10 * M
TYPE4_MEANDEPTH_FLANKING_LENGTH = 500 * K
NCLOSE_SIM_COMPARE_RATIO = 1.2
NCLOSE_SIM_DIFF_THRESHOLD = 5
RAW_TRANSLOCATION_RESULT_PKL = 'raw_translocation_result.pkl'
RAW_TRANSLOCATION_REPORT_TSV = 'raw_translocation_read_counts.tsv'
JOIN_BASELINE = 0.8
KARYOTYPE_SECTION_MINIMUM_LENGTH = 100 * K
KARYOTYPE_MIN_SEGMENT_LENGTH = 1 * M # karyotype 텍스트 표기 시 이보다 짧은 segment/indel 은 무시 (1Mb)
KARYOTYPE_NORMAL_RATIO = 0.9 # 단일 염색체 path 가 reference 길이의 이 비율 미만이면 normal('1') 대신 del 로 취급
CTG_INTYPE_CHECK_MIN_LENGTH = 100 * K
CTG_INTYPE_INSERT_MIN_SEGMENT_LENGTH = 10 * K
CTG_INTYPE_INSERT_MIN_RATIO = 0.2
NODE_NAME = 1
CHR_CHANGE_IDX = 2
DIR_CHANGE_IDX = 3
def similar_check(v1, v2, ratio):
try:
assert(v1 >= 0 and v2 >= 0)
except:
logging.error(f"Invalid values for similarity check: v1={v1}, v2={v2}")
assert(False)
mi, ma = sorted([v1, v2])
return False if mi == 0 else (ma / mi <= ratio) or ma-mi < NCLOSE_SIM_DIFF_THRESHOLD
def exist_near_bnd(chrom, inside_st, inside_nd, ratio=NCLOSE_SIM_COMPARE_RATIO):
# subset of df for the given chromosome
df_chr = df[df['chr'] == chrom]
def mean_depth(start, end):
"""Return mean meandepth over windows overlapping [start, end)."""
mask = (df_chr['nd'] > start) & (df_chr['st'] < end)
return df_chr.loc[mask, 'meandepth'].mean()
# for inside_st
st_depth = mean_depth(inside_st - MEANDEPTH_FLANKING_LENGTH, inside_st)
nd_depth = mean_depth(inside_nd, inside_nd + MEANDEPTH_FLANKING_LENGTH)
if np.isnan(st_depth) or np.isnan(nd_depth):
return True
# print(chrom, inside_st, inside_nd, not similar_check(st_depth, nd_depth))
return not similar_check(st_depth, nd_depth, ratio)
def check_near_type4(chrom, inside_st, inside_nd):
# subset of df for the given chromosome
df_chr = df[df['chr'] == chrom]
def mean_depth(start, end):
"""Return mean meandepth over windows overlapping [start, end)."""
mask = (df_chr['nd'] > start) & (df_chr['st'] < end)
return df_chr.loc[mask, 'meandepth'].mean()
# for inside_st
st_depth = mean_depth(inside_st - TYPE4_MEANDEPTH_FLANKING_LENGTH, inside_st)
nd_depth = mean_depth(inside_nd, inside_nd + TYPE4_MEANDEPTH_FLANKING_LENGTH)
if np.isnan(st_depth) or np.isnan(nd_depth):
return True
# print(chrom, inside_st, inside_nd, not similar_check(st_depth, nd_depth))
return not similar_check(st_depth, nd_depth, NCLOSE_SIM_COMPARE_RATIO)
def chr2int(x):
if x.startswith('chr'):
chrXY2int = {'chrX' : 24, 'chrY' : 25}
if x in chrXY2int:
return chrXY2int[x]
else:
return int(x[3:])
else:
return INF
def find_chr_len(file_path : str) -> dict:
chr_data_file = open(file_path, "r")
chr_len = {}
for curr_data in chr_data_file:
curr_data = curr_data.split("\t")
chr_len[curr_data[0]] = int(curr_data[1])
chr_data_file.close()
return chr_len
def import_ppc_data(file_path : str) -> list :
paf_file = open(file_path, "r")
contig_data = []
for curr_contig in paf_file:
curr_contig = curr_contig.rstrip()
temp_list = curr_contig.split("\t")
int_induce_idx = [CTG_LEN, CTG_STR, CTG_END, \
CHR_LEN, CHR_STR, CHR_END, \
CTG_MAPQ, CTG_TYP, CTG_STRND, CTG_ENDND,]
for i in int_induce_idx:
temp_list[i] = int(temp_list[i])
contig_data.append(tuple(temp_list))
paf_file.close()
return contig_data
def import_paf_data(file_path : str) -> list :
contig_data = []
int_induce_idx = [1, 2, 3, 6, 7, 8, 9]
idx = 0
with open(file_path, "r") as paf_file:
for curr_contig in paf_file:
curr_contig = curr_contig.rstrip()
a = curr_contig.split("\t")
temp_list = a[:9]
temp_list.append(a[11])
for i in int_induce_idx:
temp_list[i] = int(temp_list[i])
temp_list.append(idx)
contig_data.append(temp_list)
idx+=1
return contig_data
def import_index_path(file_path : str) -> list:
file_path_list = file_path.split('/')
key = file_path_list[-2]
cnt = int(file_path_list[-1].split('.')[0]) - 1
return path_list_dict[key][cnt][0]
def import_path_paf_rows(file_path : str) -> list:
rows = []
with open(file_path, "r") as paf_file:
for curr_contig in paf_file:
curr_contig = curr_contig.rstrip()
if not curr_contig:
continue
temp_list = curr_contig.split("\t")
int_induce_idx = [
CTG_LEN, CTG_STR, CTG_END,
CHR_LEN, CHR_STR, CHR_END,
CTG_MAPQ,
]
for i in int_induce_idx:
temp_list[i] = int(temp_list[i])
rows.append(tuple(temp_list))
return rows
def import_telo_data(file_path : str, chr_len : dict) -> dict :
fai_file = open(file_path, "r")
telo_data = []
for curr_data in fai_file:
temp_list = curr_data.split("\t")
int_induce_idx = [1, 2]
for i in int_induce_idx:
temp_list[i] = int(temp_list[i])
if temp_list[1]>chr_len[temp_list[0]]/2:
temp_list[1]-=TELOMERE_EXPANSION
temp_list.append('b')
else:
temp_list.append('f')
temp_list[2]+=TELOMERE_EXPANSION
telo_data.append(tuple(temp_list))
fai_file.close()
return telo_data
def extract_telomere_connect_contig(telo_info_path : str) -> list:
telomere_connect_contig = []
with open(telo_info_path) as f:
for curr_data in f:
curr_data = curr_data.rstrip()
temp_list = curr_data.split("\t")
chr_info = temp_list[0]
contig_id = ast.literal_eval(temp_list[1])
telomere_connect_contig.append((chr_info, contig_id[1]))
return telomere_connect_contig
def distance_checker(node_a : tuple, node_b : tuple) -> int :
if max(int(node_a[0]), int(node_b[0])) < min(int(node_a[1]), int(node_b[1])):
return 0
else:
return min(abs(int(node_b[0]) - int(node_a[1])), abs(int(node_b[1]) - int(node_a[0])))
def telo_condition(node : list, need_label_index : dict) -> bool:
return node in need_label_index
def virtual_event_label(event_type : str) -> str:
return {'d': 'del', 'i': 'ins', 'v': 'inv'}[event_type]
def parse_chromosome_labels(s):
"""
Parse '...<f|b>_...<f|b>' into a canonical tuple:
(left_label, left_is_f, right_label, right_is_f)
Rules:
- The two ends must end with 'f' or 'b' (assert if not).
- Keep each '...' label string intact (e.g., 'chr12', 'scaf_007', etc.).
- Canonicalize by sorting so the lexicographically smaller label comes first.
If labels are equal, put 'f' (True) before 'b' (False).
- When swapping due to sorting, directions stay attached to their original labels.
(So 'chr12f_chr1b' becomes ('chr1', True, 'chr12', False).)
"""
m = re.fullmatch(r'(.+?)([fb])_(.+?)([fb])', s)
assert m is not None, "Input must match ...(f|b)_...(f|b) pattern"
a_label, a_dir_ch, b_label, b_dir_ch = m.groups()
a_is_f = (a_dir_ch == 'f')
b_is_f = (b_dir_ch == 'f')
# Canonical order by label; if same label, 'f' (True) first.
if (a_label > b_label) or (a_label == b_label and not a_is_f and b_is_f):
# Swap ends to enforce canonical order; keep directions with their labels.
a_label, b_label = b_label, a_label
a_is_f, b_is_f = b_is_f, a_is_f
return (a_label, a_is_f, b_label, b_is_f)
def max_aligned_match_length(
seq_a: list[tuple[tuple[str, str], int]],
seq_b: list[tuple[tuple[str, str], int]],
) -> int:
"""
Return the maximum total matched length after sliding two piecewise-constant
label sequences along one axis. A and B are lists of ((chrom, strand), length).
Only regions with exactly the same (chrom, strand) contribute to the score.
Algorithm:
1) Convert each sequence into absolute intervals [(start, end, label)].
2) Consider candidate shifts = {a_ep - b_ep | a_ep in endpoints(A), b_ep in endpoints(B)}.
(The overlap configuration only changes when an endpoint meets another.)
3) For each shift, line-sweep over the two interval lists and accumulate
overlap length where labels are equal.
4) Return the maximum accumulated length across all shifts.
Time complexity:
Let n, m be #segments. Endpoints ~ (n+1), (m+1).
Candidates O((n+1)*(m+1)); each evaluation O(n+m). Works well for tens~hundreds of segments.
"""
# --- build absolute intervals: [(start, end, label)] and endpoint lists ---
def build_intervals(seq):
intervals = []
endpoints = []
pos = 0
endpoints.append(pos)
for (label, length) in seq:
start = pos
end = pos + length
intervals.append((start, end, label))
pos = end
endpoints.append(pos)
return intervals, endpoints
A, A_ep = build_intervals(seq_a)
B, B_ep = build_intervals(seq_b)
if not A or not B:
return 0
# --- generate candidate shifts (all endpoint differences) ---
# shift d means: compare A intervals with B intervals shifted by +d
candidates = set()
for a_e in A_ep:
for b_e in B_ep:
candidates.add(a_e - b_e)
# --- overlap length for a given shift ---
def match_length_for_shift(d: int) -> int:
i, j = 0, 0
total = 0
# Two-pointer sweep over A and shifted-B
while i < len(A) and j < len(B):
a_s, a_e, a_lab = A[i]
b_s, b_e, b_lab = B[j]
b_s += d
b_e += d
# If no overlap, advance the one that ends earlier / starts later
if a_e <= b_s:
i += 1
continue
if b_e <= a_s:
j += 1
continue
# Overlapping segment
ov_s = a_s if a_s > b_s else b_s
ov_e = a_e if a_e < b_e else b_e
if ov_e > ov_s and a_lab == b_lab:
total += (ov_e - ov_s)
# Advance the interval that ends first
if a_e <= b_e:
i += 1
else:
j += 1
return total
best = 0
# (Optional) small heuristic: iterate over sorted candidates for deterministic behavior
for d in sorted(candidates):
val = match_length_for_shift(d)
if val > best:
best = val
return best
def should_join_by_baseline(
seq_a: list[tuple[tuple[str, str], int]],
seq_b: list[tuple[tuple[str, str], int]]
) -> bool:
"""
Decide if two sequences should be joined based on:
max_aligned_match_length(seq_a, seq_b) / max(total_len_a, total_len_b) >= JOIN_BASELINE
Notes:
- Returns False if both sequences have total length 0 (to avoid 0-division).
- Assumes non-negative lengths.
- Threshold is inclusive (>=).
"""
total_a = sum(length for (_, length) in seq_a)
total_b = sum(length for (_, length) in seq_b)
denom = total_a if total_a >= total_b else total_b
if denom == 0:
return False
score = max_aligned_match_length(seq_a, seq_b)
return (score / denom) >= JOIN_BASELINE
def append_karyotype_piece(pieces : list, chrom : str, strand : str, length : int, merge : bool = True):
if length <= 0:
return
key = (chrom, strand)
if merge and pieces and pieces[-1][0] == key:
pieces[-1] = (key, pieces[-1][1] + length)
else:
pieces.append((key, length))
def append_ctg_intype_interrupt_piece(pieces : list, chrom : str, strand : str, length : int):
if length <= 0:
return
if pieces and pieces[-1][0][0] == chrom:
prev_chrom, prev_strand = pieces[-1][0]
pieces[-1] = ((prev_chrom, prev_strand), pieces[-1][1] + length)
else:
pieces.append(((chrom, strand), length))
def infer_path_strand_from_paf_rows(rows : list, idx : int) -> str:
chrom = rows[idx][CHR_NAM]
curr_mid = (rows[idx][CHR_STR] + rows[idx][CHR_END]) // 2
if idx + 1 < len(rows) and rows[idx + 1][CHR_NAM] == chrom:
next_mid = (rows[idx + 1][CHR_STR] + rows[idx + 1][CHR_END]) // 2
if next_mid != curr_mid:
return '+' if next_mid > curr_mid else '-'
if idx > 0 and rows[idx - 1][CHR_NAM] == chrom:
prev_mid = (rows[idx - 1][CHR_STR] + rows[idx - 1][CHR_END]) // 2
if curr_mid != prev_mid:
return '+' if curr_mid > prev_mid else '-'
return rows[idx][CTG_DIR] if rows[idx][CTG_DIR] in {'+', '-'} else '+'
def get_ctg_intype_interrupt_pieces(key_int : int, endpoint_chroms : set) -> list:
paf_loc = f"{output_folder}/{key_int}.paf"
if not os.path.isfile(paf_loc):
return []
rows = import_path_paf_rows(paf_loc)
if not rows:
return []
row_lengths = [abs(row[CHR_END] - row[CHR_STR]) for row in rows]
total_length = sum(row_lengths)
if total_length <= CTG_INTYPE_CHECK_MIN_LENGTH:
return []
foreign_chrom_lengths = Counter()
for row, length in zip(rows, row_lengths):
chrom = row[CHR_NAM]
if chrom not in endpoint_chroms:
foreign_chrom_lengths[chrom] += length
interrupt_chroms = {
chrom for chrom, length in foreign_chrom_lengths.items()
if length / total_length >= CTG_INTYPE_INSERT_MIN_RATIO
}
if not interrupt_chroms:
return []
pieces = []
for i, (row, length) in enumerate(zip(rows, row_lengths)):
chrom = row[CHR_NAM]
if chrom not in interrupt_chroms:
continue
if length < CTG_INTYPE_INSERT_MIN_SEGMENT_LENGTH:
continue
strand = infer_path_strand_from_paf_rows(rows, i)
append_ctg_intype_interrupt_piece(pieces, chrom, strand, length)
return pieces
def get_karyotype_summary_from_index(path_path : str, type4_edge_to_event_key=None,
type4_event_by_key=None) -> list:
"""
Fallback summary from the compact index path. This keeps the old behavior
for prefixes that do not have the 21_pat_depth PAF fragments available.
"""
pieces = []
path = import_index_path(path_path)
ctg_intype_key_by_edge = {}
for key_int in path2key_int_list.get(path_path, []):
key_type, key_value = int2key[key_int]
if key_type == CTG_IN_TYPE:
ctg_intype_key_by_edge[key_value] = key_int
# Padding for easier calculation
if len(path[0]) < 4:
path[0] = tuple([0] + list(path[0]))
if len(path[-1]) < 4:
path[-1] = tuple([0] + list(path[-1]))
# Initialize direction and chromosome from the first dummy node
curr_incr = '+' if path[0][NODE_NAME][-1] == 'f' else '-'
# Set the starting reference using the first real node (index 1)
# instead of assuming the absolute ends of the chromosome (0 or chr_len)
first_real_node = ppc_data[path[1][NODE_NAME]]
# Take chr from the real node's CHR_NAM, not the telomere endpoint name:
# the shared chrX/chrY telomere node is always labeled chrXf/chrXb, so a
# pure-chrY path with no chr/dir-change transition would be mislabeled chrX.
curr_chr = [first_real_node[CHR_NAM], curr_incr]
curr_ref = first_real_node[CHR_STR] if curr_incr == '+' else first_real_node[CHR_END]
for i in range(1, len(path)-1):
prev_node_name = path[i-1][NODE_NAME]
curr_node_name = path[i][NODE_NAME]
if not isinstance(prev_node_name, int) or not isinstance(curr_node_name, int):
continue
last_node = ppc_data[prev_node_name]
curr_node = ppc_data[curr_node_name]
edge_key = (
(path[i-1][0], prev_node_name),
(path[i][0], curr_node_name),
)
type4_event_key = None
type4_event = None
if type4_edge_to_event_key is not None:
type4_event_key = type4_edge_to_event_key.get(edge_key)
if type4_event_key is not None and type4_event_by_key is not None:
type4_event = type4_event_by_key.get(type4_event_key)
type4_deletion_edge = (
type4_event is not None and type4_event.get("event_type") == "d"
)
ctg_intype_key_int = ctg_intype_key_by_edge.get(edge_key)
interrupt_pieces = []
if ctg_intype_key_int is not None:
endpoint_chroms = {last_node[CHR_NAM], curr_node[CHR_NAM]}
interrupt_pieces = get_ctg_intype_interrupt_pieces(ctg_intype_key_int, endpoint_chroms)
if path[i][CHR_CHANGE_IDX] > path[i-1][CHR_CHANGE_IDX] \
or path[i][DIR_CHANGE_IDX] > path[i-1][DIR_CHANGE_IDX] \
or interrupt_pieces \
or type4_deletion_edge:
# Add last piece
if curr_incr == '+':
append_karyotype_piece(pieces, curr_chr[0], curr_chr[1], abs(last_node[CHR_END] - curr_ref), merge=False)
else:
append_karyotype_piece(pieces, curr_chr[0], curr_chr[1], abs(curr_ref - last_node[CHR_STR]), merge=False)
for piece_chr, piece_length in interrupt_pieces:
append_karyotype_piece(pieces, piece_chr[0], piece_chr[1], piece_length, merge=True)
# Update info of new piece (starting ref, chromosome type, increment ..)
if path[i][NODE_NAME] > path[i-1][NODE_NAME]:
curr_incr = curr_node[CTG_DIR]
curr_chr = [curr_node[CHR_NAM], curr_incr]
curr_ref = curr_node[CHR_STR] if curr_incr == '+' else curr_node[CHR_END]
else:
curr_incr = '-' if curr_node[CTG_DIR] == '+' else '+'
curr_chr = [curr_node[CHR_NAM], curr_incr]
curr_ref = curr_node[CHR_STR] if curr_incr == '+' else curr_node[CHR_END]
# Process the final piece using the last real node (index -2)
# instead of extending it to absolute end
last_real_node = ppc_data[path[-2][NODE_NAME]]
if curr_incr == '+':
final_length = last_real_node[CHR_END] - curr_ref
else:
final_length = curr_ref - last_real_node[CHR_STR]
append_karyotype_piece(pieces, curr_chr[0], curr_chr[1], abs(final_length), merge=False)
return pieces
def get_karyotype_summary(non_type4_path_list: list, type4_edge_to_event_key=None,
type4_event_by_key=None):
"""
Summarizes karyotype data from the compact index path. Only CTG_IN_TYPE
edges are inspected in the expanded PAF fragments to reveal long inserted
sequence from chromosomes outside the edge endpoints.
"""
karyotypes_data_direction_include = {}
for path_path in non_type4_path_list:
pieces = get_karyotype_summary_from_index(
path_path, type4_edge_to_event_key, type4_event_by_key
)
karyotypes_data_direction_include[path_path] = pieces
return karyotypes_data_direction_include
def ecdna_format(x:int) -> str:
if x >= 1_000_000_000:
return f"{x / 1_000_000_000:.2f}G"
elif x >= 1_000_000:
return f"{x / 1_000_000:.2f}M"
elif x >= 1_000:
return f"{x / 1_000:.2f}K"
else:
return str(x)
def chrom_to_iscn(chrom : str) -> str:
"""'chr1' -> '1', 'chrX' -> 'X' (plot_virtual_chromosome 라벨과 동일 규칙)."""
return chrom[3:] if chrom.startswith('chr') else chrom
def parse_optional_float(value):
if value is None or value == '*':
return None
try:
parsed = float(value)
except (TypeError, ValueError):
return None
return parsed if np.isfinite(parsed) else None
def load_raw_translocation_report(prefix):
report_path = f'{prefix}/{RAW_TRANSLOCATION_REPORT_TSV}'
if not os.path.isfile(report_path):
return {}
rows_by_pair = {}
with open(report_path, 'r') as f:
for row in csv.DictReader(f, delimiter='\t'):
try:
pair_id = int(row['pair_id'])
except (KeyError, TypeError, ValueError):
continue
rows_by_pair[pair_id] = row
return rows_by_pair
def finite_mean(values):
finite_values = [
float(value) for value in values
if value is not None and np.isfinite(value)
]
if not finite_values:
return None
return sum(finite_values) / len(finite_values)
def estimate_raw_virtual_inv_depth(record, report_row=None):
estimate = record.get('depth_weighted_nclose_estimate', {})
expected_depth = parse_optional_float(estimate.get('weighted_expected_nclose_depth'))
if expected_depth is not None:
return expected_depth
if report_row is not None:
expected_depth = parse_optional_float(report_row.get('weighted_expected_nclose_depth'))
if expected_depth is not None:
return expected_depth
counts = record.get('read_counts', {})
point_depth = record.get('point_500k_depth', {})
side_inputs = [
(point_depth.get('point_a', {}), counts.get('d1', 0), counts.get('d2', 0)),
(point_depth.get('point_b', {}), counts.get('d4', 0), counts.get('d3', 0)),
]
weighted_sum = 0.0
weight_sum = 0
for depth_pair, nclose_count, point_count in side_inputs:
point_mean = finite_mean([depth_pair.get('front'), depth_pair.get('back')])
if point_mean is None:
continue
try:
weight = int(nclose_count) + int(point_count)
nclose_count = int(nclose_count)
except (TypeError, ValueError):
continue
if weight <= 0:
continue
weighted_sum += point_mean * (nclose_count / weight) * weight
weight_sum += weight
if weight_sum == 0:
return None
return weighted_sum / weight_sum
def record_depth_is_balanced(record, report_row=None):
if 'depth_balanced_translocation' in record:
return bool(record.get('depth_balanced_translocation'))
# The TSV is emitted after the depth-balance filter, so an old-schema pkl can
# be treated as balanced only when its pair_id still exists in the TSV.
return report_row is not None
def raw_false_value(value):
return value is False or value == 0 or value == 'False' or value == 'false'
def record_has_both_point_spans(record):
raw_no_span = record.get('raw_point_no_spanning')
if isinstance(raw_no_span, dict):
return raw_false_value(raw_no_span.get('point_a')) and raw_false_value(raw_no_span.get('point_b'))
side_records = record.get('side_records', [])
if len(side_records) < 2:
return False
side_flags = [
side.get('raw_point_no_spanning', side.get('no_spanning_rawread'))
for side in side_records[:2]
]
return raw_false_value(side_flags[0]) and raw_false_value(side_flags[1])
def raw_virtual_inv_vafs(record, report_row=None):
estimate = record.get('depth_weighted_nclose_estimate', {})
point_a_vaf = parse_optional_float(estimate.get('point_a_nclose_vaf'))
point_b_vaf = parse_optional_float(estimate.get('point_b_nclose_vaf'))
if point_a_vaf is not None and point_b_vaf is not None:
return point_a_vaf, point_b_vaf
if report_row is not None:
if point_a_vaf is None:
point_a_vaf = parse_optional_float(report_row.get('point_a_nclose_vaf'))
if point_b_vaf is None:
point_b_vaf = parse_optional_float(report_row.get('point_b_nclose_vaf'))
if point_a_vaf is not None and point_b_vaf is not None:
return point_a_vaf, point_b_vaf
counts = record.get('read_counts', {})
try:
d1 = int(counts.get('d1', 0))
d2 = int(counts.get('d2', 0))
d3 = int(counts.get('d3', 0))
d4 = int(counts.get('d4', 0))
except (TypeError, ValueError):
return point_a_vaf, point_b_vaf
if point_a_vaf is None and d1 + d2 > 0:
point_a_vaf = d1 / (d1 + d2)
if point_b_vaf is None and d4 + d3 > 0:
point_b_vaf = d4 / (d4 + d3)
return point_a_vaf, point_b_vaf
def record_passes_virtual_inv_vaf(record, report_row=None, min_vaf=RAW_VIRTUAL_INV_MIN_VAF):
point_a_vaf, point_b_vaf = raw_virtual_inv_vafs(record, report_row)
if point_a_vaf is None or point_b_vaf is None:
return False
return point_a_vaf > min_vaf and point_b_vaf > min_vaf
def directed_entry_coord(endpoint):
return int(endpoint['ref_st']) if endpoint['dir'] == '+' else int(endpoint['ref_nd'])
def record_display_points(record, report_row=None):
if report_row is not None:
chrom_a = report_row.get('chrom_a')
chrom_b = report_row.get('chrom_b')
try:
point_a = int(report_row.get('point_a'))
point_b = int(report_row.get('point_b'))
except (TypeError, ValueError):
chrom_a = chrom_b = None
else:
if chrom_a and chrom_b:
return chrom_a, point_a, chrom_b, point_b
chrom_pair = record.get('chrom_pair', ('*', '*'))
layout_a = record.get('layout_a', {})
layout_b = record.get('layout_b', {})
endpoints_a = list(layout_a.get('endpoints', ()))
endpoints_b = list(layout_b.get('endpoints', ()))
side_records = record.get('side_records', [])
chrom_a = chrom_pair[0] if len(chrom_pair) > 0 else '*'
chrom_b = chrom_pair[1] if len(chrom_pair) > 1 else '*'
coord_a = None
coord_b = None
if len(endpoints_b) > 0:
coord_a = directed_entry_coord(endpoints_b[0])
elif len(side_records) > 0:
coord_a = int(side_records[0]['inner_st'])
if len(endpoints_a) > 1:
coord_b = directed_entry_coord(endpoints_a[1])
elif len(side_records) > 1:
coord_b = int(side_records[1]['inner_nd'])
return chrom_a, coord_a, chrom_b, coord_b
def point_to_chrom_end_interval(chrom, point, side, chrom_lengths):
chrom_len = int(chrom_lengths[chrom])
point = max(0, min(int(point), chrom_len))
if side == 'left':
st, nd = 0, point
else:
st, nd = point, chrom_len
if nd <= st:
return None
return st, nd
def layout_side(record, layout_name, side_idx, default='right'):
sides = record.get(layout_name, {}).get('sides', ())
if side_idx < len(sides):
return sides[side_idx]
return default
def read_component_ref_intervals(prefix, key_int, cache):
if key_int in cache:
return cache[key_int]
by_chrom = defaultdict(list)
paf_path = f'{prefix}/21_pat_depth/{key_int}.paf'
if not os.path.isfile(paf_path):
cache[key_int] = by_chrom
return by_chrom
with open(paf_path, 'r') as f:
for line in f:
if not line.strip():
continue
fields = line.rstrip('\n').split('\t')
if len(fields) <= CHR_END:
continue
try:
st = int(fields[CHR_STR])
nd = int(fields[CHR_END])
except ValueError:
continue
if nd < st:
continue
by_chrom[fields[CHR_NAM]].append((st, nd))
cache[key_int] = by_chrom
return by_chrom
def merge_ref_intervals(intervals):
if not intervals:
return []
intervals = sorted(intervals)
merged = [list(intervals[0])]
for st, nd in intervals[1:]:
if st <= merged[-1][1]:
if nd > merged[-1][1]:
merged[-1][1] = nd
else:
merged.append([st, nd])
return [tuple(x) for x in merged]
def interval_strictly_contains_any(merged_intervals, st, nd):
return any(intv_st < st and nd < intv_nd for intv_st, intv_nd in merged_intervals)
def raw_true_value(value):
return value is True or value == 1 or value == 'True' or value == 'true'
def record_has_any_point_no_span(record):
raw_no_span = record.get('raw_point_no_spanning')
if isinstance(raw_no_span, dict):
return raw_true_value(raw_no_span.get('point_a')) or raw_true_value(raw_no_span.get('point_b'))
side_records = record.get('side_records', [])
return any(
raw_true_value(side.get('raw_point_no_spanning', side.get('no_spanning_rawread')))
for side in side_records
)
def side_inner_interval(side):
return (
int(side.get('inner_st', side.get('path_drop_st', 0))),
int(side.get('inner_nd', side.get('path_drop_nd', 0))),
)
def record_has_same_chrom_contiguous_span_path(record, prefix, weights, meandepth, min_depth_N):
if weights is None:
return False
chrom_pair = record.get('chrom_pair', ())
side_records = record.get('side_records', [])
if len(side_records) < 2:
return False
chrom_a = chrom_pair[0] if len(chrom_pair) > 0 else side_records[0].get('chrom')
chrom_b = chrom_pair[1] if len(chrom_pair) > 1 else side_records[1].get('chrom')
if chrom_a != chrom_b:
return False
if not record_has_any_point_no_span(record):
return False
a_st, a_nd = side_inner_interval(side_records[0])
b_st, b_nd = side_inner_interval(side_records[1])
span_st = min(a_st, b_st)
span_nd = max(a_nd, b_nd)
if span_nd <= span_st:
return False
path_records = globals().get('paf_ans_list', [])
if not path_records:
return False
min_weight = float(min_depth_N) * float(meandepth) / 2.0
component_cache = {}
for col_idx, (_, key_int_list) in enumerate(path_records):
if col_idx >= len(weights) or float(weights[col_idx]) <= min_weight:
continue
intervals = []
for key_int in key_int_list:
intervals.extend(read_component_ref_intervals(prefix, key_int, component_cache).get(chrom_a, []))
if interval_strictly_contains_any(merge_ref_intervals(intervals), span_st, span_nd):
return True
return False
def build_virtual_inv_events(prefix, meandepth, chrom_lengths, min_depth_N=0.0, weights=None):
result_path = f'{prefix}/{RAW_TRANSLOCATION_RESULT_PKL}'
if not os.path.isfile(result_path) or meandepth <= 0:
return []
report_by_pair = load_raw_translocation_report(prefix)
with open(result_path, 'rb') as f:
records = pkl.load(f)
display_inv = []
for record in records:
try:
pair_id = int(record.get('pair_id'))
except (TypeError, ValueError):
continue
report_row = report_by_pair.get(pair_id)
if not record_depth_is_balanced(record, report_row):
continue
if not record_passes_virtual_inv_vaf(record, report_row):
continue
if not (
record_has_both_point_spans(record) or
record_has_same_chrom_contiguous_span_path(record, prefix, weights, meandepth, min_depth_N)
):
continue
expected_depth = estimate_raw_virtual_inv_depth(record, report_row)
if expected_depth is None:
continue
depth_N = expected_depth / meandepth * 2
if depth_N < min_depth_N:
continue
chrom_a, point_a, chrom_b, point_b = record_display_points(record, report_row)
if chrom_a not in chrom_lengths or chrom_b not in chrom_lengths:
continue
if point_a is None or point_b is None:
continue
if chrom_a == chrom_b:
st, nd = sorted([int(point_a), int(point_b)])
st = max(0, min(st, int(chrom_lengths[chrom_a])))
nd = max(0, min(nd, int(chrom_lengths[chrom_a])))
if nd > st:
display_inv.append(('v', st, nd, depth_N, chrom_a, f'RAW_TRANSLOCATION_PAIR_{pair_id}'))
continue
side_a = layout_side(record, 'layout_b', 0)
side_b = layout_side(record, 'layout_a', 1)
interval_a = point_to_chrom_end_interval(chrom_a, point_a, side_a, chrom_lengths)
interval_b = point_to_chrom_end_interval(chrom_b, point_b, side_b, chrom_lengths)
if interval_a is not None:
display_inv.append(('v', interval_a[0], interval_a[1], depth_N, chrom_a, f'RAW_TRANSLOCATION_PAIR_{pair_id}_A'))
if interval_b is not None:
display_inv.append(('v', interval_b[0], interval_b[1], depth_N, chrom_b, f'RAW_TRANSLOCATION_PAIR_{pair_id}_B'))
return display_inv
def type4_indel_graph_source_label(event, event_key):
contig_name = event.get("contig_name")
if contig_name:
return f"TYPE4_INDEL_GRAPH_{contig_name}"
type4_tuple = event.get("type4_tuple")
if type4_tuple:
return "TYPE4_INDEL_GRAPH_" + "_".join(map(str, type4_tuple))
if isinstance(event_key, tuple):
return "TYPE4_INDEL_GRAPH_" + "_".join(map(str, event_key))
return f"TYPE4_INDEL_GRAPH_{event_key}"
def get_path_type4_indel_events(path, type4_event_by_key, type4_path_event_usage):
return [
type4_event_by_key[event_key]
for event_key in type4_path_event_usage.get(path, {})
if event_key in type4_event_by_key
]
def type4_indel_karyotype_labels(type4_indel_events):
labels = []
seen = set()
for event in type4_indel_events or []:
if event.get("span_len", 0) < KARYOTYPE_MIN_SEGMENT_LENGTH:
continue
key = (
event.get("event_type"),
event.get("chrom"),
event.get("st"),
event.get("nd"),
)
if key in seen:
continue
seen.add(key)
labels.append(
f"{virtual_event_label(event['event_type'])}({chrom_to_iscn(event['chrom'])})"
)
return labels
def append_extra_karyotype_labels(base_iscn, extra_labels):
remaining = list(extra_labels)
if base_iscn in remaining:
remaining.remove(base_iscn)
return base_iscn + ''.join(remaining)
def get_type4_indel_boundary_labels(path_path, type4_edge_to_event_key,
type4_event_by_key, maxh):
if not type4_edge_to_event_key or not type4_event_by_key:
return []
path = import_index_path(path_path)
ctg_intype_key_by_edge = {}
for key_int in path2key_int_list.get(path_path, []):
key_type, key_value = int2key[key_int]
if key_type == CTG_IN_TYPE:
ctg_intype_key_by_edge[key_value] = key_int