-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate_heatmap.py
executable file
·291 lines (247 loc) · 9.38 KB
/
generate_heatmap.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
#!/usr/bin/env python
import pylab as pl
import numpy as np
import os
import csv
import sys
sys.path.append("/home/szh41/oxford-svn/utility")
sys.path.append("/Users/joezhu/oxford-svn/utility")
sys.path.append("/home/joezhu/oxford-svn/utility")
import ms2something as ms
shifting = 0
## @ingroup group_compare_psmc
def psmc_XYZ( prefix ):
print "./grep_prob.sh " + prefix
os.system("./grep_prob.sh " + prefix)
spacing = 50
site_file = open( prefix + "Site", 'r')
site = []
line_index = 0
for line in site_file:
if line_index % spacing == 0 : site.append(int(line.strip()))
line_index += 1;
site_file.close()
time_file = open( prefix + "Time", 'r')
time = []
for line in time_file:
time.append(float(line.strip()))
time_file.close()
X, Y = np.meshgrid( site, time )
Z = []
probability_file = open( prefix + "probability", 'r')
line_index = 0
for line in probability_file:
if line_index % spacing == 0 : Z.append([float(x) for x in line.split()]); #xticks.append(line_index)
line_index += 1
probability_file.close()
myarray = np.asarray(Z).transpose()
return X, Y, myarray, site, time
## @ingroup group_compare_psmc
def psmc_figure(cum_change, tmrca, position, prefix):
X, Y, Z, site, time = psmc_XYZ( prefix )
fig = pl.figure(figsize=(24,7))
pl.pcolor(X, Y, Z, vmin=0, vmax=0.15)
my_axes = fig.gca()
#ylabels = ["%.4g" % (float(y)*2 * default_pop_size) for y in my_axes.get_yticks()]
ylabels = ["%.4g" % (float(y)) for y in my_axes.get_yticks()]
my_axes.set_yticklabels(ylabels)
#pl.colorbar()
pl.step(cum_change, [x*2 for x in tmrca] , color = "red", linewidth=15.0) # becasue x is generated by ms, which is scaled to 4N0, psmc scale to 2N0. Thereofore, needs to multiply by 2
#pl.step(cum_change, [x for x in tmrca] , color = "red", linewidth=5.0)
pl.plot(position, [0.9*max(time)]*len(position), "wo", markersize=15)
pl.axis([0, max(site) , 0, max(time)])
pl.title("PSMC heat map", fontsize=25)
pl.xlabel("Sequence base", fontsize=20)
#pl.ylabel("TMRCA (generation)")
pl.ylabel("TMRCA (2N0)", fontsize=20)
#my_axes.set_yticklabels([])
#my_axes.set_xticklabels([])
pl.savefig(prefix + "psmc_heat" + ".png", transparent=True)
print "done"
pl.close()
## @ingroup group_compare_pfarg
def read_smcsmc_data( file_name ):
smcsmc_data = []
base = []
#smcsmc_data_file = open(file_name, "r")
#for line in smcsmc_data_file:
#dummy = [float(x) for x in line.split("\t")]
#print dummy
#base.append(dummy.pop(0))
#smcsmc_data.append(dummy)
#smcsmc_data_file.close()
with open(file_name, 'rb') as csvfile:
spamreader = csv.reader(csvfile, delimiter='\t')
for row in spamreader:
dummy = [float(x) for x in row]
base.append(dummy.pop(0))
smcsmc_data.append(dummy)
return smcsmc_data, base
## @ingroup group_compare_pfarg
def makeHeatmatrix(x, y, WEIGHT, TMRCA):
xlength = len(x)
ylength = len(y)
WEIGHT = np.array(WEIGHT)
TMRCA = np.array(TMRCA)
#print TMRCA
Z = []
for xi in range(xlength):
weight = []
for wi in range(len(WEIGHT[0])):
weight.append(WEIGHT[xi][wi])
tmrca = []
for ti in range(len(TMRCA[0])):
tmrca.append(TMRCA[xi][ti])
z = []
zindx = ylength - 2
upper = y[ zindx+1 ];
while zindx >= 0 :
lower = y[zindx]
zcell_sum = 0
for it in range(len(tmrca)):
if lower < tmrca[it] <= upper:
zcell_sum += weight[it]
z.append(float(zcell_sum))
zindx=zindx-1;
upper=lower;
z.reverse()
zcolsum = 0
#print z
for zi in z: zcolsum += zi
#z = [ (zi / zcolsum)**0.45 for zi in z] # For better presentation
#print zcolsum
if zcolsum > 0: # DEBUG, check how could zcolsum = 0
z = [ (zi / zcolsum) for zi in z]
Z.append(z)
return np.asarray(Z).transpose()
## @ingroup group_compare_pfarg
def smcsmc_XYZ( prefix ):
WEIGHT, x = read_smcsmc_data( prefix + "WEIGHT" )
TMRCA, x = read_smcsmc_data( prefix + "TMRCA" )
#default_pop_size = 10000
#TMRCA_max = 10 * default_pop_size
TMRCA_max = 2 + shifting
numof_y = 50; # number of grids on the TMRCA (y-axis)
y = np.linspace(0, TMRCA_max, numof_y)
Z = makeHeatmatrix(x, y, WEIGHT, TMRCA)
X, Y = np.meshgrid( x, y )
return X, Y, Z, x, y
## @ingroup group_compare_pfarg
def smcsmc_figure(cum_change, tmrca, position, prefix):
X, Y, Z, site, time = smcsmc_XYZ( prefix )
fig = pl.figure(figsize=(24,7))
pl.pcolor(X, Y, Z, vmin=0, vmax=0.15)
my_axes = fig.gca()
ylabels = ["%.4g" % (float(y)+shifting) for y in my_axes.get_yticks()]
my_axes.set_yticklabels(ylabels)
#pl.colorbar()
#pl.step(cum_change, [x*4*default_pop_size for x in tmrca] , color = "red", linewidth=5.0)
pl.step(cum_change, tmrca , color = "red", linewidth=15.0)
pl.plot(position, [0.9*max(time)]*len(position), "wo", markersize=15)
pl.axis([min(site), max(site) , 0, max(time)])
pl.axis([min(site), max(site) , shifting, max(time)]) # for the case of split
#my_axes.set_yticklabels([])
#my_axes.set_xticklabels([])
pl.title("SMCSMC heat map", fontsize=25)
pl.xlabel("Sequence base", fontsize=20)
#pl.ylabel("TMRCA (generation)")
pl.ylabel("TMRCA (4Ne)", fontsize=20)
pl.savefig( prefix + "smcsmc_heat" + ".png", transparent=True)
pl.close()
def get_cum_change( file_name ):
change_file = open( file_name, "r" )
cum_change = [0]
for line in change_file:
cum_change.append( cum_change[len(cum_change)-1] + int(line.strip()))
change_file.close()
return cum_change
def get_tmrca( file_name ):
tmrca_file = open( file_name, "r")
tmrca = [0]
for line in tmrca_file:
tmrca.append(float(line.strip()))
tmrca_file.close()
return tmrca
## @ingroup group_compare_dical
def extract_diCal_time ( prefix ):
file_name = prefix + "diCalout"
dc_file = open( file_name, "r")
for line in dc_file:
if len( line.split() ) == 0: continue # Skipping the empty lines
if ( line.split()[0] == "decoding" ) & (line.split()[1] == "hap" ):
break
site = []
absorptionTime = []
for line in dc_file:
if ( line.split()[0] == "finished" ) & (line.split()[1] == "hap" ):
break
site.append ( float(line.split()[2]) )
absorptionTime.append ( float(line.split()[3]) )
dc_file.close()
return site, absorptionTime
## @ingroup group_compare_dical
def diCal_figure (cum_change, tmrca, position, prefix):
tmrca = [x*2 for x in tmrca] # convert tmrca from unit of 4Ne to 2Ne
site, absorptionTime = extract_diCal_time ( prefix )
maxtime = max( max(absorptionTime), max(tmrca) )
fig = pl.figure(figsize=(24,7))
pl.plot(site, absorptionTime, color = "green", linewidth=3.0)
pl.step(cum_change, tmrca, color = "red", linewidth=5.0)
pl.plot(position, [maxtime*1.05]*len(position), "bo")
pl.axis([min(site), max(site) , 0, maxtime*1.1])
pl.title("Dical Absorption time (Green)")
pl.xlabel("Sequence base")
pl.ylabel("TMRCA (2N0)")
pl.savefig( prefix + "diCal_absorption_time" + ".png")
pl.close()
## @ingroup group_compare_dical
def diCal_lines (arg1, arg2):
prefix = arg1
seqlen = int(arg2)
cum_change = get_cum_change ( prefix + "mschange" )
tmrca = get_tmrca ( prefix + "tmrca")
position = ms.get_position ( seqlen, prefix + "position")
diCal_figure(cum_change, tmrca, position, prefix)
## @ingroup group_compare_pfarg
def smcsmc_heat (arg1, arg2, subbool = False):
sub = "_NA1" if subbool else ""
prefix = arg1
seqlen = int(arg2)
cum_change = get_cum_change ( prefix + "mschange" )
tmrca = get_tmrca ( prefix + "tmrca")
position = ms.get_position ( seqlen, prefix + "position")
smcsmc_figure(cum_change, tmrca, position, prefix+sub )
def smcsmc_survivor_XYZ( prefix ):
Z, x = read_smcsmc_data( prefix + "SURVIVOR" )
numof_y = len(Z[0]); # number of grids on the (y-axis)
y = range(numof_y)
Z= np.array(np.transpose(Z))
X, Y = np.meshgrid( x, y )
return X, Y, Z, x, y
def smcsmc_survivor ( prefix ):
X, Y, Z, site, particles = smcsmc_survivor_XYZ( prefix )
fig = pl.figure(figsize=(24,7))
pl.pcolor(X, Y, Z)
my_axes = fig.gca()
pl.axis([min(site), max(site) , 0, max(particles)])
pl.title("PFPSMC Survivors")
pl.xlabel("Sequence base")
#pl.ylabel("TMRCA (generation)")
pl.ylabel("Particles")
pl.savefig( prefix + "smcsmc_survivor" + ".png")
pl.close()
## @ingroup group_compare_psmc
def psmc_heat (arg1, arg2):
prefix = arg1
seqlen = int(arg2)
cum_change = get_cum_change ( prefix + "mschange" )
tmrca = get_tmrca ( prefix + "tmrca")
position = ms.get_position ( seqlen, prefix + "position")
psmc_figure(cum_change, tmrca, position, prefix)
if __name__ == "__main__":
#try:
smcsmc_heat (sys.argv[1], sys.argv[2])
#psmc_heat (sys.argv[1], sys.argv[2])
#except:
#print "something wrong"
#sys.exit(1)