-
Notifications
You must be signed in to change notification settings - Fork 2
/
plot_telCNT.py
executable file
·358 lines (296 loc) · 12.3 KB
/
plot_telCNT.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
#!/usr/bin/env python
__version__ = '0.0.1'
import os
import sys
import csv
import time
import glob
import random
#from scipy.stats import ttest_ind as ttest
from optparse import OptionParser
from Bio import Cluster
from copy import deepcopy
import numpy as np
import matplotlib
matplotlib.use("Agg")
# or use 'PDF'
import matplotlib.pyplot as plt
prop = matplotlib.font_manager.FontProperties(size=6)
_time_init = time.time()
### GO INTO OPTIONS ###
parser= OptionParser(usage="%prog", version=__version__)
parser.add_option('-f', dest='indir',
help='/path/to/infile_directory [REQUIRED]')
parser.add_option('-d', dest='deco',
help='decorator string to locate files [%default]',
default='.tsv')
parser.add_option('--repeats', dest='repeats', type='int',
default=-8,
help='location of telomeric repeat sequences [%default]')
parser.add_option('-p', dest='plot', type='choice',
choices=('offt', 'chrm', 'offtcnt'),
default='offt',
help='plot value choices: offt,chrM,offtcnt [%default]')
parser.add_option('-i', dest='ignore',
help='comma-separated list of sample names to ignore [%default]', default='')
parser.add_option('-n', dest='normals',
help='/path/to/summary_normarls file [%default]')
(option, arg)= parser.parse_args()
# collect and summarize
outprefix = os.path.join(option.indir, "_".join([p for p in ("summary", option.plot, option.deco.lstrip("_").replace(".tsv", "", 1).replace("*", "-")) if len(p)]))
infile = outprefix + '.txt'
ignore_snames = option.ignore.split(',')
flist = glob.glob(os.path.join(option.indir, "*%s"%option.deco))
if len(flist) == 0:
print 'no files found, check deco %s' %option.deco
sys.exit(1)
outdata = dict()
for fname in flist:
with open(fname) as inf:
line = inf.readline().strip().split("\t")
sname = line[1]
for line in inf:
pass
# write the last line
outdata[sname] = line
prefix = os.path.commonprefix([p[::-1] for p in outdata.keys()])[::-1]
try:
outdata = dict((p.split(prefix)[0],q) for p,q in outdata.items())
except ValueError:
pass
scaleDB = {'offt' : 'RTPMO-w/offt',
'chrm' : 'RTPKM-w/chrM',
'offtcnt' : 'RTPMO-w/offtCNT',
'dups' : 'DUP-RATE',
'ont' : 'ONTARGET-RATE'}
main_values = scaleDB.values()
dataDB = dict()
with open(infile, 'w') as outf:
# read the file in memory
for sname, line in outdata.items():
line = line.strip().split("\t")
if sname not in ignore_snames:
dataDB[sname] = dict()
outf.write(outdata[sname])
"""
# consume the repeats
dataDB[sname]['repeats'] = dict()
for i in xrange(1, len(line)):
if ':' in line[-i]:
dataDB[sname]['repeats'].update(tuple(line[-i].split(":")))
else:
break
"""
for i in xrange(2,len(main_values)*3,3):
if line[i] in main_values:
dataDB[sname][line[i]] = [float(line[i+1]), float(line[i+2])]
else:
break
dataDB[sname]['repeats'] = dict(tuple(p.split(":")) for p in line[i+3:])
# if present; read the normals file as well
normalsDB = dict()
normal_repeatsDB = dict()
if option.normals != None and os.path.isfile(option.normals):
with open(option.normals) as inf:
csv_inf = csv.reader(inf, delimiter='\t')
for line in csv_inf:
sname = line[0].split(prefix)[0]
normalsDB[sname] = dict()
for i in xrange(2,len(main_values)*3,3):
if line[i] in main_values:
normalsDB[sname][line[i]] = [float(line[i+1]), float(line[i+2])]
else:
break
normalsDB[sname]['repeats'] = dict(tuple(p.split(":")) for p in line[i:])
for repeat in normalsDB[sname]['repeats']:
normalsDB[sname]['repeats'][repeat] = float(normalsDB[sname]['repeats'][repeat])
normal_repeatsDB.setdefault(repeat, list()).append(normalsDB[sname]['repeats'][repeat])
# define normal mean and SD
normal_data = [normalsDB[p][scaleDB[option.plot]][0] for p in normalsDB]
normal_mean = np.mean(normal_data)
normal_std = np.std(normal_data)
normal_repeatsDB = dict((p,sum(normal_repeatsDB[p])/len(normalsDB)) for p in normal_repeatsDB)
# plot 1
# bar graph for sorted scale
sort_data = list()
repeatsDB = dict()
for sname in dataDB.keys():
line = [sname] + dataDB[sname][scaleDB[option.plot]]
sort_data.append(line)
# also collect repeat info
for repeat in dataDB[sname]['repeats']:
dataDB[sname]['repeats'][repeat] = float(dataDB[sname]['repeats'][repeat])
repeatsDB.setdefault(repeat, list()).append(dataDB[sname]['repeats'][repeat])
sort_data = sorted(sort_data, key=lambda x:x[1])
len_data = len(sort_data)
ind = np.arange(len_data)
Y_axis = list()
X_axis = list()
E_bars = list() # error bars
for p in sort_data:
X_axis.append(p[0])
Y_axis.append(p[1])
E_bars.append(p[2])
PLOT = plt.bar(ind, Y_axis, yerr=E_bars, linewidth=0.1)
bw = PLOT.patches[0].get_width() / 2
plt.xlim(xmax=len_data)
ymin, ymax = plt.ylim()
plt.xticks(ind + bw, X_axis, rotation=90, fontsize=8, ha='center')
plot_title = "%s Values plot"%scaleDB[option.plot]
if len(normalsDB):
# plot normals
plt.axhspan(normal_mean - normal_std, normal_mean + normal_std, color='r', alpha=0.2)
plt.axhline(normal_mean, color='r', alpha=0.2)
"""
# run stats
p_value = ttest([p for p in Y_axis if p<normal_mean], [p for p in Y_axis if p>normal_mean])[1]#, equal_var=False)
plot_title += '\nLonger_vs_shorther than Normals p_value=%0.2e'%p_value
"""
plt.title(plot_title, fontsize=16, fontweight='bold')
plt.tight_layout()
plt.savefig(outprefix + '.pdf')
plt.close()
# plot 2
# distribution of all the hexamer counts
# collect the most prevelant repeats
bck_repeatsDB = deepcopy(repeatsDB)
with open(outprefix + '_cluster.txt', 'w') as outf:
repeatsDB = dict((p,sum(repeatsDB[p])/len_data) for p in repeatsDB)
sort_repeats = sorted(repeatsDB.items(), key=lambda x: x[1], reverse=True)
limit_bottom = len_data * [0]
if len(normalsDB):
repeatsDB = normal_repeatsDB
X_axis.append('NORMALS')
limit_bottom.append(0)
len_data += 1
ind = np.arange(len_data)
outf.write("name\t" + "\t".join(X_axis) + "\n")
legendDB = dict()
#all_colors = open(os.path.join(os.path.dirname(sys.argv[0]), 'colors_hex_values.txt')).read().split('\n')
#random.shuffle(all_colors)
all_colors = ("navy", "green", "maroon",
"blue", "lime", "red",
"teal", "olive", "purple",
"aqua", "yellow", "fuchsia",
"FireBrick", "Indigo", "DodgerBlue", "DarkGreen")
top_repeats = list()
delta_repeats = dict()
for repeat,color in zip(sort_repeats, all_colors[:15]):
repeat = repeat[0]
top_repeats.append(repeat)
temp = [float(dataDB[p]['repeats'].get(repeat, '0.')) for p in X_axis if p in dataDB]
delta_repeats[repeat] = temp
if len(normalsDB):
temp.append(np.mean(normal_repeatsDB.get(repeat, 0.)))
outf.write('%s\t' %repeat + '\t'.join([str(p-repeatsDB[repeat]) for p in temp]) + '\n')
legendDB[repeat] = plt.bar(ind, temp, bottom=limit_bottom, color=color, ec=color, linewidth=0.1)
limit_bottom = [p+q for p,q in zip(limit_bottom, temp)]
# add-up to 1.0 with summing up all others
temp = [1.0-p for p in limit_bottom]
legendDB['other'] = plt.bar(ind, temp, bottom=limit_bottom, color='gray', ec='gray', linewidth=0.1)
top_repeats.append('other')
#what are 'others'
repeatsDB['other'] = sum([repeatsDB[p] for p in repeatsDB if p not in top_repeats])/len(dataDB)
outf.write('%s\t' % 'other' + '\t'.join([str(p-repeatsDB["other"]) for p in temp]) + '\n')
try:
bw = legendDB['TTAGGG'].patches[0].get_width() / 2
except KeyError:
print >> sys.stderr, '%s ERROR: no TTAGGG for' %os.path.basename(sys.argv[0]), option.deco
# format the plot
plt.ylabel("fraction", fontsize=12, fontweight='bold')
plt.title("Distribution of Hexameric Repeats", fontsize=16, fontweight='bold')
plt.xticks(ind+bw, X_axis, rotation=90, size=8, ha='center')
plt.xlim(xmax=len_data)
ymin_scale = min([dataDB[p]['repeats'].get(top_repeats[0], 0) for p in X_axis if p in dataDB])*.9
plt.ylim(ymax=1, ymin=ymin_scale)
#top_repeats += ['other']
plt.figlegend([legendDB[p][0] for p in top_repeats], top_repeats,
ncol=2, numpoints=1, prop=prop, loc=5)
plt.tight_layout()
plt.savefig(outprefix + '_hexrepeats.pdf', format="pdf", bbox_inches='tight')
plt.close()
"""
# plot 3
# group hexamers
# define groups
with open(outprefix + '_groups.txt', 'w') as outf:
repeatsDB = dict((p,sum(repeatsDB[p])/len_data) for p in repeatsDB)
sort_repeats = sorted(repeatsDB.items(), key=lambda x: x[1], reverse=True)
limit_bottom = len_data * [0]
if len(normalsDB):
repeatsDB = normal_repeatsDB
X_axis.append('NORMALS')
limit_bottom.append(0)
len_data += 1
ind = np.arange(len_data)
outf.write("name\t" + "\t".join(X_axis) + "\n")
legendDB = dict()
all_colors = ("b", "m", "g", "r", "y", "c", 'k')
all_colors = ("navy", "green", "maroon",
"blue", "lime", "red",
"teal", "olive", "purple",
"aqua", "yellow", "fuchsia",
"gray", "black")
top_repeats = list()
delta_repeats = dict()
for repeat,color in zip(sort_repeats, all_colors):
repeat = repeat[0]
top_repeats.append(repeat)
temp = [float(dataDB[p]['repeats'].get(repeat, '0.')) for p in X_axis if p in dataDB]
delta_repeats[repeat] = temp
if len(normalsDB):
temp.append(np.mean(normal_repeatsDB.get(repeat, 0.)))
outf.write('%s\t' %repeat + '\t'.join([str(p-repeatsDB[repeat]) for p in temp]) + '\n')
legendDB[repeat] = plt.bar(ind, temp, bottom=limit_bottom, color=color, ec=color, linewidth=0.1)
limit_bottom = [p+q for p,q in zip(limit_bottom, temp)]
# add-up to 1.0 with summing up all others
#temp = [1.0-p for p in limit_bottom]
#legendDB['other'] = plt.bar(ind, temp, bottom=limit_bottom, color='silver', ec='0.75', linewidth=0.1)
# what are 'others'
#repeatsDB['other'] = sum([repeatsDB[p] for p in repeatsDB if p not in top_repeats])/len(dataDB)
#outf.write('%s\t' % 'other' + '\t'.join([str(p-repeatsDB["other"]) for p in temp]) + '\n')
# plot 4
# line-graph for change in repeat-fraction
# use an running-average of three
AVG = 2
for repeat,color in zip(delta_repeats, all_colors):
data = [np.mean(delta_repeats[repeat][i:i+AVG]) for i in xrange(0, len(delta_repeats[repeat])-AVG, AVG)]
mdata = np.mean(data)
data = [p-mdata for p in data]
plt.plot(data, color=color, linestyle='-', label=repeat)
plt.tight_layout()
#plt.figlegend()
plt.savefig(outprefix + '_delta_hexrepeats.pdf', format="pdf", bbox_inches='tight')
plt.close()
"""
# plot 3 heatmap
# can you generate the clusters and save the files for viewing
cluster_method= "m"
"""
method== s : pairwise single-linkage clustering
method== m : pairwise maximum- (or complete-) linkage clustering
method== c : pairwise centroid-linkage clustering
method== a : pairwise average-linkage clustering
"""
cluster_dist= "c"
"""
dist== c : correlation;
dist== a : absolute value of the correlation;
dist== u : uncentered correlation;
dist== x : absolute uncentered correlation;
dist== s : Spearman s rank correlation;
dist== k : Kendall s;
dist== e : Euclidean distance;
dist== b : City-block distance.
"""
"""
if len(repeatsDB) > 1:
record= Cluster.read(open(outprefix + '_cluster.txt'))
genetree= record.treecluster(transpose=0, method=cluster_method, dist=cluster_dist)
genetree.scale()
exptree= record.treecluster(transpose=1, method=cluster_method, dist=cluster_dist)
exptree.scale()
record.save(outprefix + '_cluster', geneclusters=genetree, expclusters=exptree)
else:
print 'need adv counting for a heatmap'
"""