-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_breakpoint.py
130 lines (94 loc) · 4.11 KB
/
find_breakpoint.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
# This translocation breakpoint finder is part of the TranScan software presented by the paper entitled "Translocation Detection from Hi-C data via Scan Statistics" by Anthony Cheng, Disheng Mao, Yuping Zhang, Joseph Glaz, and Zhengqing Ouyang.
# License: The implementations written for this project is covered by the GNU General Public License, version 3.0 (GPL-3.0).
import matplotlib
matplotlib.use("Agg")
import sys
import argparse
import matplotlib.pyplot as plt
import numpy as np
def get_sizes(f_sizes):
sizes = {}
with open(f_sizes, "r") as f:
for line in f:
row = line.strip("\r\n").split("\t")
sizes[row[0]] = int(row[1])
return sizes
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("-i", default=None, help="RR.txt")
parser.add_argument("-k", default=None, help="KDE.txt")
parser.add_argument("-sizes", default=None, help="sizes")
parser.add_argument("-c1", default=None, help="chrom1")
parser.add_argument("-c2", default=None, help="chrom2")
parser.add_argument("-o", default=None, help="output")
args = parser.parse_args()
p_chrom1 = args.c1
p_chrom2 = args.c2
f_txt = args.i
f_kde = args.k
f_sizes = args.sizes
f_output = args.o
p_gap = 2500000
p_quantitest = 2**8 #256 # 2**8
p_half = p_gap/2
p_bw_win = 1
p_threshold = 0 # 25
if p_chrom1 == None:
p_chrom1, p_chrom2 = f_txt.split("/")[-2].split("_")[-1].split("-vs-")
data = np.loadtxt(f_txt)
kde = np.loadtxt(f_kde)
sizes = get_sizes(f_sizes)
len_chrA = sizes[p_chrom1]
len_chrB = sizes[p_chrom2]
p_eff_res = (len_chrA + len_chrB + p_gap)/float(p_quantitest)
prop_chrA = len_chrA/float(len_chrA+len_chrB)
prop_chrB = len_chrB/float(len_chrA+len_chrB)
inter_chrA = prop_chrA * (len_chrA + len_chrB + p_gap)
inter_chrB = prop_chrB * (len_chrB + len_chrB + p_gap)
#gap_chrA = prop_chrA * p_gap
#gap_chrB = prop_chrB * p_gap
bins_chrA = int(round(inter_chrA/p_eff_res))
bins_chrB = int(p_quantitest - bins_chrA)
inter = data[:bins_chrA,bins_chrA:]
rows_ignore = np.where(inter[p_bw_win:-p_bw_win,:].sum(axis=1) == 0)[0]
cols_ignore = np.where(inter[:,p_bw_win:-p_bw_win].sum(axis=0) == 0)[0]
interkde = kde[:bins_chrA,bins_chrA:][p_bw_win:-p_bw_win,p_bw_win:-p_bw_win]
X = []
Y = []
pos_X = []
pos_Y = []
XY = []
for i in range(p_bw_win, bins_chrA-1):
state_change_xb = np.count_nonzero(inter[i-p_bw_win,:] != inter[i,:])
state_change_xf = np.count_nonzero(inter[i,:] != inter[i+p_bw_win,:])
binth1 = int((i-1)*p_eff_res)
binth2 = int(i*p_eff_res)
X.append(state_change_xb+state_change_xf)
pos_X.append((p_chrom1, binth1, binth2, i))
for i in range(p_bw_win, bins_chrB-1):
state_change_yb = np.count_nonzero(inter[:,i-p_bw_win] != inter[:,i])
state_change_yf = np.count_nonzero(inter[:,i] != inter[:,i+p_bw_win])
#binth2 = i*((chrB_l+p_half)/128)
binth1 = int((i-1)*p_eff_res)
binth2 = int(i*p_eff_res)
Y.append(state_change_yb+state_change_yf)
pos_Y.append((p_chrom2, binth1, binth2, i))
XY = np.outer(X, Y)
# ignore those with contiguous gaps
XY[rows_ignore,:] = 0
XY[:,cols_ignore] = 0
# zero out non-rejected regions
#interkde[np.where(inter[p_bw_win:-p_bw_win,p_bw_win:-p_bw_win] == 0)] = 0
# alternative 1
XY[np.where(inter[p_bw_win:-p_bw_win,p_bw_win:-p_bw_win] == 0)] = 0
index = np.unravel_index(np.argmax(XY, axis=None), XY.shape)
no_of_pixels = np.max(XY)
if no_of_pixels >= p_threshold:
print("\t".join(map(str,
list(pos_X[index[0]][:3])+list(pos_Y[index[1]][:3])+[no_of_pixels])))
""" Output """
if f_output:
plt.imshow(inter)
plt.axhline(pos_X[index[0]][3], linestyle="--", linewidth=1.0)
plt.axvline(pos_Y[index[1]][3], linestyle="--", linewidth=1.0)
plt.savefig(f_output, dpi=300)