-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_running_mean.py
351 lines (266 loc) · 10.7 KB
/
generate_running_mean.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
# coding: utf-8
import csv
import numpy as np
import os
import matplotlib.pyplot as plt
import matplotlib
import operator
from scipy.stats import linregress
def plot_original_fc_over_length():
gene_fc = []
gene_length = []
gene_id = []
#are we reading the first line from the file
first_row = True
with open('Test_IBA.txt', 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter='\t')
for row in reader:
#lets skip the first row, it is just the headers
if first_row:
first_row = False
continue
id, fc, length = row
if 'N/A' not in length:
try:
#cast the numbers from strings to floats
fc_float = float(fc)
length_float = float(length)
gene_id.append(id)
gene_fc.append(fc_float)
gene_length.append(length_float)
except Exception as e:
pass
#convert the lists to arrays, lots of mathematical operations
#much easier to apply to arrays
gene_length = np.array(gene_length)
gene_fc = np.array(gene_fc)
log_gene_length = np.log10(gene_length)
plt.scatter(log_gene_length, gene_fc.clip(-10, 10))
plt.show()
import IPython
IPython.embed()
assert False
def runningMean(fc, length, step_size, window_size, use_50_percentile=False):
out = np.array([(y, x) for (y, x) in sorted(zip(length, fc))])
fc_sorted_by_length = out[:, 1]
sorted_length = out[:, 0]
x = []
y = []
for ctr in range(0, len(fc_sorted_by_length)-window_size, step_size):
fcs = fc_sorted_by_length[ctr:(ctr+window_size)]
lengths = sorted_length[ctr:ctr+window_size]
#sort by fc and remove the bottom25% and top 25%
#in order to smooth the plots
if use_50_percentile:
fcs_lengths = zip(fcs, lengths)
fcs_lengths.sort(key=operator.itemgetter(0))
fcs_lengths = np.array(fcs_lengths)
fcs = fcs_lengths[window_size/4:(window_size-window_size/4), 0] * 2.0
lengths = fcs_lengths[window_size/4:(window_size-window_size/4), 1] * 2.0
y.append(np.sum(fcs))
x.append(np.sum(lengths))
x = np.array(x)
y = np.array(y)
return x/float(window_size), y/float(window_size)
def get_fc_and_length_from_file(filename):
gene_fc = []
gene_length = []
gene_id = []
#are we reading the first line from the file
first_row = True
#na_count = 0
with open(filename, 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter='\t')
for row in reader:
#lets skip the first row, it is just the headers
if first_row:
first_row = False
continue
id, fc, length = row
if fc == '' and length == '' and id == '':
continue
if "N/A" in length:
continue
gene_id.append(id)
if float(fc) < 0:
fc = -1/float(fc)
gene_fc.append(float(fc))
gene_length.append(float(length))
gene_length = np.array(gene_length)
gene_fc = np.array(gene_fc)
return gene_id, gene_fc, gene_length
def calculate_fc_from_file(filename):
value_1 = []
value_2 = []
gene_length = []
gene_id = []
#are we reading the first line from the file
first_row = True
#na_count = 0
with open(filename, 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter='\t')
for row in reader:
#lets skip the first row, it is just the headers
if first_row:
first_row = False
continue
id, v1, v2, length = row
if v1 == '' and length == '' and id == '':
continue
if "N/A" in length:
continue
# if float(v1) < 0.0000001:
# #print v1, v2
# continue
#
# if float(v2) < 0.0000001:
# #print v1, v2
# continue
gene_id.append(id)
value_1.append(float(v1))
value_2.append(float(v2))
gene_length.append(float(length))
gene_length = np.array(gene_length)
value_1 = np.array(value_1)
value_2 = np.array(value_2)
gene_fc = (value_1 + .00001) / (value_2 + .00001)
#gene_fc = (value_1 ) / (value_2 )
return gene_id, gene_fc, gene_length
def run_file(filename="data/12hr_test.txt",
output_directory="plots/",
step_size=40,
window_size=200,
save=True,
use_50_percentile=False):
#if the file does not have an fc column, then we need to calculate it
try:
gene_id, gene_fc, gene_length = calculate_fc_from_file(filename)
#if the file has the fc column, we can just load it
except ValueError as e:
gene_id, gene_fc, gene_length = get_fc_and_length_from_file(filename)
log_gene_length = np.log10(gene_length)
log_gene_fc = np.log2(gene_fc)
running_mean_log_lengths, running_mean_log_fc = runningMean(log_gene_fc,
log_gene_length,
step_size,
window_size,
use_50_percentile=use_50_percentile)
running_mean_lengths_in_kb = np.power(10, running_mean_log_lengths)/1000.0
running_mean_log_lengths_in_kb = running_mean_log_lengths - 3.0
plot_regression = step_size is 1 and win_size is 1
#
if plot_regression:
slope, intercept, r_value, p_value, std_err = linregress(running_mean_lengths_in_kb, running_mean_log_fc)
print r_value
print p_value
print "r_value: %f\np_value: %f\nstd_err: %f\n" % (r_value, p_value, std_err)
#clear the old figure before adding a new plot
plt.clf()
##########################################
#Plot parameters
##########################################
# width of the entire figure in inches
WIDTH_INCHES = 2*4
# height of the entire figure in inches.
HEIGHT_INCHES = 1.5*4
TITLE_FONTSIZE = '28'
# font size for 0,10,20, etc on each axis
TICK_LABEL_FONT_SIZE = 14
# Gene Length font size
AXES_LABEL_FONT_SIZE = '28'
# dots per inch.
DPI = 4*72
# how big are the little circles that are plotted on the graph.
PLOT_MARKER_SIZE = 20
# if the ylabel is getting cut off, make this larger.
LEFT_ADJUST = 0.18
# if the x label is getting cut off, make this larger.
BOTTOM_ADJUST = 0.18
############################################
fig = plt.figure(figsize=(WIDTH_INCHES, HEIGHT_INCHES))
#set the plot to be log scale
if not plot_regression:
plt.xscale('log', basex=10)
scatter = plt.scatter(running_mean_lengths_in_kb, running_mean_log_fc, s=PLOT_MARKER_SIZE)
#scatter = plt.scatter(running_mean_log_lengths_in_kb, running_mean_log_fc)
#add a horizontal line at y = 0, 'k' is the variable for black colored line
plt.axhline(y=0, color='k')
#
if plot_regression:
plt.plot(running_mean_lengths_in_kb, slope*running_mean_lengths_in_kb + intercept, color="r")
#
#generate the title for the figure from the name of the file, step_size, and window_size
fig_title = filename.split('/')[-1].replace(".txt", " ").replace("_", " ") + str(step_size) + " " + str(window_size)
if use_50_percentile:
fig_title += " 50 Percentile"
if plot_regression:
fig_title += " Regression"
from matplotlib import font_manager as fm
title_font = {'size': TITLE_FONTSIZE, 'color': 'black', 'weight': 'normal', 'verticalalignment': 'bottom'}
prop = fm.FontProperties(fname='/usr/share/fonts/truetype/msttcorefonts/Arial.ttf')
for tick in scatter.axes.get_xmajorticklabels():
tick.set_fontsize(TICK_LABEL_FONT_SIZE)
for tick in scatter.axes.get_ymajorticklabels():
tick.set_fontsize(TICK_LABEL_FONT_SIZE)
scatter.axes.set_xlabel("Gene Length (kb)", fontsize=AXES_LABEL_FONT_SIZE)
scatter.axes.set_ylabel('Mean Log$_2$ Fold Change', fontsize=AXES_LABEL_FONT_SIZE)
scatter.axes.tick_params(axis='both', which='major', pad=15)
scatter.axes.tick_params(axis='y', direction='out')
scatter.axes.tick_params(axis='x', direction='out')
scatter.axes.get_xaxis().tick_bottom()#removes ticks from top
scatter.axes.get_yaxis().tick_left()#removes ticks on rhs
scatter.axes.spines["right"].set_visible(False)
scatter.axes.spines["top"].set_visible(False)
scatter.axes.set_title(fig_title, fontproperties=prop, **title_font)
plt.gcf().subplots_adjust(bottom=BOTTOM_ADJUST, left=LEFT_ADJUST)
#want the x label to be 1,10,100, ... not 10^1, 10^2,...
scatter.axes.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())
if save:
plt.savefig(output_directory + fig_title.replace(" ", "_") + ".pdf", dpi=DPI)
else:
plt.show()
######
#THIS IS THE START OF OUR PROGRAM
######
if __name__ == "__main__":
#
# # where we are getting data from
# DATA_DIRECTORY = "work/"
# #where we are saving the plots
# OUTPUT_DIRECTORY = "work_plots/"
# #whether to save the plots, if false, they are displayed instead.
# SAVE = False
# #we have the fold change in the .txt file, don't need to calculate it
# CALCULATE_FC = False
#
# SLIDING_WINDOW_STEP_SIZE = 1
# SLIDING_WINDOW_SIZE = 80
# where we are getting data from
DATA_DIRECTORY = "data/"
#where we are saving the plots
OUTPUT_DIRECTORY = "plots/"
#whether to save the plots, if false, they are displayed instead.
SAVE = True
SLIDING_WINDOW_STEP_SIZE = 10
SLIDING_WINDOW_SIZE = 800
#for each sliding window, average the
#whole window, or only use middle 50 percentile
USE_50_PERCENTILE = False
if not os.path.exists(OUTPUT_DIRECTORY):
os.mkdir(OUTPUT_DIRECTORY)
# #USE THIS FOR REGRESSION
win_sizes = [1]
step_sizes = [1]
for filename in os.listdir(DATA_DIRECTORY):
for win_size in win_sizes:
for step_size in step_sizes:
print
print "working on: " + str(filename)
print "win size: " + str(win_size)
print "step size: " + str(step_size)
run_file(filename=DATA_DIRECTORY + filename,
output_directory=OUTPUT_DIRECTORY,
step_size=step_size,
window_size=win_size,
save=SAVE,
use_50_percentile=USE_50_PERCENTILE)