-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdifferences_interdomain.py
More file actions
197 lines (157 loc) · 7.91 KB
/
differences_interdomain.py
File metadata and controls
197 lines (157 loc) · 7.91 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
from sys import argv, exit
from collections import defaultdict
import optparse, os, math
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
from util import clip_and_blur
from matplotlib.backends import backend_pdf
from matplotlib import rcParams, cm
def lines(path,header=True):
with open(path,'r') as handle:
if header:
handle.next()
else: pass
for line in handle:
yield line.split('\n')[0].split(' ')
def parse_domains(gen):
domains = defaultdict(list)
end0 = 0.0
for l in gen:
#print l
if l[1] == opts.Level and l[2] == opts.Chrom:
start = float(l[3])/150000.0
end = (float(l[4].split("\n")[0])/150000.0) - 1
lev = l[1]
if end > end0: end0 = end
domains[("lev"+lev, l[0])].append((start,end))
return domains, end0, lev
def plot_all(mtx, mt_i, xy, out):
#with backend_pdf.PdfPages("%s_roznicowa_Rafala.pdf" % ( os.path.basename(out))) as pdf:
#plt.figure(dpi=1200)
#mtx = np.load(mat)
#fig = plt.figure(dpi=2200)
#fig = plt.figure()
#colormap = plt.cm.gist_ncar
#plt.imshow(mtx,interpolation='nearest',origin='lower',norm=LogNorm(),cmap=cm.jet)
#plt.imshow(np.tril(mtx),origin='lower',norm=LogNorm(),cmap="Blues", interpolation='nearest')
x,y = zip(*xy)
plt.plot(x,y,linestyle='-',linewidth=0.3, color = 'black')
plt.imshow(mt_i, norm=LogNorm(), cmap="Reds",interpolation='none', origin = 'upper')
plt.colorbar()
plt.imshow(np.tril(mtx),norm=LogNorm(), cmap="Blues", interpolation='nearest', origin = 'upper')
#plt.show()
#ax=pylab.gca()
#ax.set_ylim(ax.get_ylim()[::-1])
#plt.axis([0,mtx.shape[0],0,mtx.shape[0]])
plt.legend(loc='best')
plt.title("Plot",fontsize=7)
out = "%s_diff_Rafala.png" % ( os.path.basename(out).split('.')[0])
plt.savefig(out, dpi = 1500, bbox_inches='tight')
plt.show()
plt.close()
print "finished plotting"
def prepar_interac_matr(inte, si, dom):
i_m = np.zeros((si, si))
ZERO = True
#print dom
header = True
if header: inte.next()
else: pass
int_dict = {}
for l in inte:
#print l
if l[2] != '0.0' and float(l[2]) < 0.05:
print "NIE 0.0"
dom1 = [dom[i] for i in dom.keys() if i[1] == l[0]]
dom2 = [dom[i] for i in dom.keys() if i[1] == l[1]]
dom1_2 = sum(dom1 + dom2,[])
#print dom1_2
if dom1_2[0]==dom1_2[1]:
#print dom1_2[0], dom1_2[1]
i_m[int(dom1_2[0][0]):int(dom1_2[0][1])+1, int(dom1_2[1][0]):int(dom1_2[1][1])+1] = 0.0
elif len(dom1_2) == 2:
#print dom1_2[0][0], int(dom1_2[0][1])+1, dom1_2[1][0], int(dom1_2[1][1])+1, i_m.shape
#i_m[int(dom1_2[0][0]):int(dom1_2[0][1])+1, int(dom1_2[1][0]):int(dom1_2[1][1])+1] = round(-math.log10(float(l[2])), 5)
i_m[int(dom1_2[0][0]):int(dom1_2[0][1])+1, int(dom1_2[1][0]):int(dom1_2[1][1])+1] = 1.0
domeny = "%s %s" %(l[0], l[1])
int_dict[domeny]=l[2]
elif len(dom1_2) == 1:
#print dom1_2, dom1, dom2
i_m[int(dom1_2[0][0]):int(dom1_2[0][1])+1, int(dom1_2[1][0]):int(dom1_2[1][1])+1] = 1.0
domeny = "%s %s" %(l[0], l[1])
int_dict[domeny]=l[2]
else: print "More domains!!!!", dom1_2
elif l[2] == '0.0' and ZERO:
dom1 = [dom[i] for i in dom.keys() if i[1] == l[0]]
dom2 = [dom[i] for i in dom.keys() if i[1] == l[1]]
dom1_2 = sum(dom1 + dom2,[])
if dom1_2[0]==dom1_2[1]:
#print dom1_2[0], dom1_2[1]
i_m[int(dom1_2[0][0]):int(dom1_2[0][1])+1, int(dom1_2[1][0]):int(dom1_2[1][1])+1] = 0.0
elif len(dom1_2) == 2:
#print dom1_2, type(dom1)
#i_m[int(dom1_2[0][0]):int(dom1_2[0][1])+1, int(dom1_2[1][0]):int(dom1_2[1][1])+1] = 500
i_m[int(dom1_2[0][0]):int(dom1_2[0][1])+1, int(dom1_2[1][0]):int(dom1_2[1][1])+1] = 1.0
domeny = "%s %s" %(l[0], l[1])
int_dict[domeny]=l[2]
elif len(dom1_2) == 1:
#print dom1_2, dom1, dom2
i_m[int(dom1_2[0][0]):int(dom1_2[0][1])+1, int(dom1_2[1][0]):int(dom1_2[1][1])+1] = 1.0
domeny = "%s %s" %(l[0], l[1])
int_dict[domeny]=l[2]
else: print "More domains!!!!", dom1_2
else:
pass
return np.triu(i_m), int_dict
if __name__=="__main__":
optparser = optparse.OptionParser(usage = "%prog [<options>]")
optparser.add_option('-m', type = "string", default = "", dest="Matrix", help = "Numpy matrix in npy format")
optparser.add_option('-d', type = "string", default = "", dest="Domains", help ="Txt file with domain information")
optparser.add_option('-i', type = "string", default = "", dest="Interaction", help ="Txt file with domain-domain interactions")
optparser.add_option('-f', type = "string", default = "", dest="Files", help ="Txt file with information abouth the whole path of domin and interactions files in the collumns in the format domain_path interaction_path ")
optparser.add_option('-l', type = "string", default = "", dest="Level", help ="The level of sherpa")
optparser.add_option('-c', type = "string", default = "", dest="Chrom", help ="The level of sherpa")
(opts,args) = optparser.parse_args()
if len(argv) ==1:
print optparser.format_help()
exit(1)
domeny, dl, level = parse_domains(lines(opts.Domains, header=False))
#print domeny
inter_mtx, interac_d = prepar_interac_matr(lines(opts.Interaction, header=False), dl, domeny)
matr = np.load(opts.Matrix)
matr = clip_and_blur(matr)
other_mtx=lines(opts.Files, header=True)
int_mtres = []
for lin in other_mtx:
dom, d, level = parse_domains(lines(lin[0], header=False))
int_m, dict = prepar_interac_matr(lines(lin[1], header=False), d, dom)
int_mtres.append(int_m)
#plt.imshow(inter_mtx,origin='lower',norm=LogNorm(),cmap="Reds")
#plt.show()
#plt.imshow(int_mtres[0],origin='lower',norm=LogNorm(),cmap="Reds")
#plt.show()
for ma in int_mtres:
inter_mtx[np.where(inter_mtx==ma)] = 0.0
xyki = []
for d_key in domeny.keys():
xyki = xyki + sum([[(start,start), (end,start), (end,end)] for start, end in domeny[d_key]],[])
new = np.zeros_like(inter_mtx)
#print np.sum(new)
for key in interac_d.keys():
#print key, domeny
ke = ('lev'+level, key.split(' ')[0])
ka = ('lev'+level, key.split(' ')[1])
if np.sum(inter_mtx[int(domeny[ke][0][0]):int(domeny[ke][0][1])+1, int(domeny[ka][0][0]):int(domeny[ka][0][1])+1]) != 0.0:
if float(interac_d[key]) != 0.0:
new[int(domeny[ke][0][0]):int(domeny[ke][0][1])+1, int(domeny[ka][0][0]):int(domeny[ka][0][1])+1][np.where(inter_mtx[int(domeny[ke][0][0]):int(domeny[ke][0][1])+1, int(domeny[ka][0][0]):int(domeny[ka][0][1])+1] == 1.0)] = round(-math.log10(float(interac_d[key])), 5)
else:
print "p-val = 0!!!"
new[int(domeny[ke][0][0]):int(domeny[ke][0][1])+1, int(domeny[ka][0][0]):int(domeny[ka][0][1])+1][np.where(inter_mtx[int(domeny[ke][0][0]):int(domeny[ke][0][1])+1, int(domeny[ka][0][0]):int(domeny[ka][0][1])+1] == 1.0)] = 500.0
print ke[1], ka[1], interac_d[key]
plot_all(matr, new, xyki, opts.Interaction)
#Sprawdzenie symetrycznosci
#inter_mtx[np.isnan(inter_mtx)] = 0
#print np.allclose(np.transpose(inter_mtx), inter_mtx), inter_mtx.size - np.isnan(inter_mtx).sum()