-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseq_gc_plot.py
279 lines (250 loc) · 10.4 KB
/
seq_gc_plot.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
#!/usr/bin/env python
import warnings
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from Bio.Alphabet import IUPAC
import sys, argparse, os
from collections import defaultdict
from natsort import natsorted
import backports.functools_lru_cache
import numpy as np
from scipy import stats
import random
import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
from PyPDF2 import PdfFileMerger, PdfFileReader
parser = argparse.ArgumentParser(description="")
parser.add_argument("dir", help="Input dir")
parser.add_argument("--window", nargs="+", default=["1000"], help="")
parser.add_argument("--events", action="store_true", default=False, help="")
parser.add_argument("--events_range", default = "10", help="")
parser.add_argument("--local_z", action = "store_true", default = False, help="")
parser.add_argument("--min_contig", default = "1000", help="")
parser.add_argument("--bins", default="100", help="")
parser.add_argument("--out", default="GC_plot.pdf", help="")
parser.add_argument("--out_stats", default="GC_plot.txt", help="")
parser.add_argument("--plot_len", action="store_true", default=False, help="")
parser.add_argument("--plot_no_title", action="store_true", default=False, help="")
parser.add_argument("--plot_sd", action="store_true", default=False, help="")
parser.add_argument("--plot_count_pdf", default="20", help="")
args = parser.parse_args()
pp_out = PdfPages(args.out)
pp_out.close()
pp = PdfPages(args.out+"_tmp")
pp.close()
plots = []
gc_stats = defaultdict(list)
out_stats_file = open(args.out.replace("pdf","txt"),"w")
out_stats_file.write("Input\t"+"\t".join(natsorted([str(x+"\t"+x+"_SD") for x in args.window]))+"\n")
files = sorted(os.listdir(args.dir))
for fi,f1 in enumerate(files):
with open(args.dir+"/"+f1) as f:
first = True
for l in f:
if len(l.split()) > 0:
print l
fname = l.split()[0]
win = l.split()[1]
#slide = win
#min_contig = l.split()[2]
#n50 = l.split()[3]
#gc_data_raw = l.split()[4:]
slide = l.split()[2]
min_contig = l.split()[3]
n50 = l.split()[4]
gc_data_raw = l.split()[5:]
gc_data = []
if win in natsorted(args.window) and int(min_contig) >= int(args.min_contig):
if first == True:
out_stats_file.write(f1)
first = False
if args.plot_len == False:
if len(gc_data_raw) >= 1:
if gc_data_raw[0].find("_") == -1:
gc_data = [float(x) for x in gc_data_raw]
else:
for w in gc_data_raw:
for gc in w.split("_"):
gc_data.append(float(gc))
#print gc_data
#print gc_data_raw
bins = np.linspace(0,1,int(args.bins)+1)
fig = plt.figure()
ax = plt.gca()
ax.xaxis.set_ticklabels(['{:2.0f}%'.format(x) for x in np.linspace(0,100,11)])
ax.set_xlabel('GC content')
ax.xaxis.set_ticks_position("bottom")
ax.xaxis.set_tick_params(width=2)
plt.xlim([0,1])
plt.xticks(np.linspace(0,1,11))
if args.plot_no_title == False:
plt.title(f1+"_w"+win+"_s"+slide+"_m"+min_contig+"_n"+n50,fontsize=10)
print str(fi+1)+"/"+str(len(files)),f1,win,"plotting ("+str(len(gc_data))+" points)"
sys.stdout.flush()
mean_gc = np.mean(gc_data)
std_gc = np.std(gc_data)
plt.hist(gc_data, bins=bins, label="GC"+"_"+"{:.2f}".format(mean_gc*100)+"_"+"{:.2f}".format(std_gc*100), alpha=0.5, color="red")
out_stats_file.write("\t"+str(mean_gc)+"\t"+str(std_gc))
out_stats_file.flush()
else:
contig_breaks = []
if gc_data_raw[0].find("_") == -1:
gc_data = [float(x) for x in gc_data_raw]
print str(fi+1)+"/"+str(len(files)),f1,win,"plotting ("+str(len(gc_data))+" points)"
sys.stdout.flush()
ticks = 0
mod = 1
mod = 1000000/int(win)
mod = 1000000/int(slide)
ticks = ((len(gc_data)/mod)+1)
fig = plt.figure()
plt.xlim([0,ticks*mod])
plt.xticks(np.linspace(0,ticks*mod,ticks+1))
plt.ylim([0,1])
ax = plt.gca()
ax.xaxis.set_ticks_position("bottom")
ax.xaxis.set_tick_params(width=2)
ax.xaxis.set_ticklabels(['{:2.0f}'.format(x) for x in np.linspace(0,ticks,ticks+1)])
ax.set_xlabel('Genome position (Mb)')
plt.yticks(np.linspace(0,1,11))
ax.yaxis.set_ticklabels(['{:3.0f}%'.format(x) for x in np.linspace(0,100,11)])
ax.set_ylabel('GC content')
if args.plot_no_title == False:
plt.title(f1+"_w"+win+"_s"+slide+"_m"+min_contig+"_n"+n50,fontsize=10)
for y in np.linspace(0,1,11):
plt.axhline(y, color="lightgrey",linewidth=1)
mean_gc = np.mean(gc_data)
std_gc = np.std(gc_data)
plt.plot(gc_data, label="GC"+"_"+"{:.2f}".format(mean_gc*100)+"_"+"{:.2f}".format(std_gc*100), alpha=0.5, color="red")
plt.axhline(mean_gc, color="blue")
out_stats_file.write("\t"+str(mean_gc)+"\t"+str(std_gc))
out_stats_file.flush()
else:
all_gc = []
for w in gc_data_raw:
for gc in w.split("_"):
all_gc.append(float(gc))
print str(fi+1)+"/"+str(len(files)),f1,win,"plotting ("+str(len(all_gc))+" points)"
sys.stdout.flush()
ticks = 0
mod = 1
mod = 1000000/int(win)
mod = 1000000/int(slide)
ticks = ((len(all_gc)/mod)+1)
fig = plt.figure()
plt.xlim([0,ticks*mod])
plt.xticks(np.linspace(0,ticks*mod,ticks+1))
plt.ylim([0,1])
ax = plt.gca()
ax.xaxis.set_ticks_position("bottom")
ax.xaxis.set_tick_params(width=1)
ax.xaxis.set_ticklabels(['{:2.0f}'.format(x) for x in np.linspace(0,ticks,ticks+1)])
ax.set_xlabel('Genome position (Mb)')
plt.yticks(np.linspace(0,1,11))
ax.yaxis.set_ticklabels(['{:3.0f}%'.format(x) for x in np.linspace(0,100,11)])
ax.set_ylabel('GC content')
if args.plot_no_title == False:
plt.title(f1+"_w"+win+"_s"+slide+"_m"+min_contig+"_n"+n50,fontsize=10)
for y in np.linspace(0,1,11):
plt.axhline(y, color="lightgrey", alpha=0.5, linewidth=1)
mean_gc = np.mean(all_gc)
std_gc = np.std(all_gc)
plt.plot(0, label="GC"+"_"+"{:.2f}".format(mean_gc*100)+"_"+"{:.2f}".format(std_gc*100), alpha=1, color="red")
shift = 0
#out_stats_file.write(f1+"\t"+str(mean_gc)+"\t"+str(std_gc)+"\n")
out_stats_file.write("\t"+str(mean_gc)+"\t"+str(std_gc))
out_stats_file.flush()
for i,w in enumerate(gc_data_raw):
plt.axvline(shift+len(w.split("_")), color="lightblue",alpha=1,linewidth=1)
gc_data_win = []
for gc in w.split("_"):
gc_data_win.append(float(gc))
#ANALYZE WINDOW
if args.events == True:
w_gc_min = min(gc_data_win)
w_gc_max = max(gc_data_win)
w_gc_mean = np.mean(gc_data_win)
w_gc_std = np.std(gc_data_win)
w_gc_median = np.median(gc_data_win)
w_gc_mode = stats.mode(gc_data_win)
#w_gc_zscore = stats.zscore(gc_data_win)
#for gc,z in zip(gc_data_win,w_gc_zscore):
# print gc_mean,gc,z
t_down = -2
t_up = 2
for j,gc in enumerate(gc_data_win):
#if gc == w_gc_min:
#plt.axvline(shift+j, ymax=gc,color="green", alpha=0.5, linewidth=1)
z = (gc-mean_gc)/std_gc
if z > t_up or z < t_down:
#print str(i+1),(shift+j)*int(win),"{:.3f}".format(gc-mean_gc),"{:.3f}".format(mean_gc),"{:.3f}".format(gc),z
if z > t_up:
plt.axvline(shift+j, ymin=gc, color="orange", alpha=0.5, linewidth=1)
if z < t_down:
plt.axvline(shift+j, ymax=gc,color="violet", alpha=1, linewidth=1)
if args.local_z == True:
loc_mean = 0
loc_size = 10
for j,gc in enumerate(gc_data_win):
if j > loc_size and j < len(gc_data_win)-loc_size:
loc_mean = np.mean(gc_data_win[j-loc_size:j+loc_size])
loc_std = np.std(gc_data_win[j-loc_size:j+loc_size])
loc_z = (gc-loc_mean)/loc_std
if loc_z > t_up or loc_z < t_down:
#print str(i+1),(shift+j)*int(win),"{:.3f}".format(gc-loc_mean),"{:.3f}".format(loc_mean),"{:.3f}".format(gc),loc_z
if loc_z > t_up:
plt.axvline(shift+j, ymin=gc, color="orange", alpha=0.5, linewidth=1)
if loc_z < t_down:
plt.axvline(shift+j, ymax=gc,color="violet", alpha=0.5, linewidth=1)
#plt.plot(range(shift,shift+len(gc_data_win)), gc_data_win, alpha=0.5, color="red")
shift += len(w.split("_"))
plt.plot(all_gc, alpha=0.5, color="red", linewidth=1)
plt.axhline(mean_gc, color="blue")
if args.plot_sd == True:
ax.fill_between(range(1,len(all_gc)),mean_gc-2*std_gc,mean_gc+2*std_gc,color="silver")
ax.fill_between(range(1,len(all_gc)),mean_gc-std_gc,mean_gc+std_gc,color="lightgray")
#plt.axhline(mean_gc+std_gc, color="blue")
#plt.axhline(mean_gc-std_gc, color="blue")
#plt.axhline(mean_gc+2*std_gc, color="lightblue")
#plt.axhline(mean_gc-2*std_gc, color="lightblue")
for i,w in enumerate(gc_data_raw):
gc_data_win = []
for gc in w.split("_"):
gc_data_win.append(float(gc))
plt.legend(loc="upper right")
plots.append(fig)
if (len(plots)%int(args.plot_count_pdf)) == 0:
pp = PdfPages(args.out+"_tmp")
for p in plots:
p.savefig(pp,format="pdf")
plt.close(p)
pp.close()
merger = PdfFileMerger()
merger.append(PdfFileReader(file(args.out, 'rb')))
merger.append(PdfFileReader(file(args.out+"_tmp", 'rb')))
merger.write(args.out)
plots = []
#plt.savefig(pp,format="pdf")
#pp.savefig(format="pdf")
#plt.close()
#pp.close()
#merger = PdfFileMerger()
#merger.append(PdfFileReader(file(args.out, 'rw')))
#merger.append(PdfFileReader(file(args.out+"_tmp", 'rb')))
#merger.write(args.out)
out_stats_file.write("\n")
pp = PdfPages(args.out+"_tmp")
for p in plots:
p.savefig(pp,format="pdf")
plt.close(p)
pp.close()
merger = PdfFileMerger()
merger.append(PdfFileReader(file(args.out, 'rb')))
merger.append(PdfFileReader(file(args.out+"_tmp", 'rb')))
merger.write(args.out)
if os.path.exists(args.out+"_tmp"):
os.remove(args.out+"_tmp")
out_stats_file.close()