-
Notifications
You must be signed in to change notification settings - Fork 5
/
TSS_cpg_bins.py
executable file
·328 lines (265 loc) · 9.04 KB
/
TSS_cpg_bins.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
#!/usr/bin/env python3
import sys
import re
import argparse
import gzip
import matplotlib.pyplot as plt
def argparser():
parser = argparse.ArgumentParser()
parser.add_argument("--methylkit", required = True, help = "gzipped .methylKit file")
parser.add_argument("--fasta", required = False, help = "Reference fasta file")
parser.add_argument("--binary_fasta", required = False, help = "'Binary' fasta file (CpGs as 1, rest as 0)")
parser.add_argument("--binary_methylkit", required = False, help = "'Binary' fasta with methylation positions")
parser.add_argument("--all_bins", required = False, help = "File with ")
parser.add_argument("--prefix", required = True, help = "Prefix for the output file")
parser.add_argument("--annotation", required = True, help = "Annotation GTF file")
parser.add_argument("--plot", required = False, help = "Whether you want to plot the results or not")
args = parser.parse_args()
return args
def write_fasta(fasta_dict, file_handle, string):
''' Writes a dict of sequences into a fasta file'''
print("Writing {}".format(file_handle))
out = open(file_handle, "w")
for chrom in fasta_dict:
if string == True:
out.write(">{}\n{}\n".format(chrom, fasta_dict[chrom]))
else:
out.write(">{}\n{}\n".format(chrom, "".join(map(str, fasta_dict[chrom]))))
def parse_fasta(fasta):
''' Parses a fasta file and stores it in a dictionary'''
print("Parsing the fasta file...")
fasta_dict = {}
if fasta.endswith(".gz"):
fasta = gzip.open(fasta, "rb")
for line in fasta:
line = str(line, "utf-8")
if line.startswith(">"):
chrom = line.strip().split(" ")[0][1:]
print(chrom)
fasta_dict[chrom] = []
else:
fasta_dict[chrom].append(line.strip())
fasta.close()
else:
fasta = open(fasta, "r")
for line in fasta:
if line.startswith(">"):
chrom = line.strip().split(" ")[0][1:]
print(chrom)
fasta_dict[chrom] = []
else:
fasta_dict[chrom].append(line.strip())
fasta.close()
return fasta_dict
def convert_to_binary(fasta_dict):
'''
Reads a fasta dictionary and converts it to 'binary', in which
every CG position is a 1, and all other bases are 0s
'''
print("Converting the fasta to 'binary'")
bin_dict = {}
cpg_bin_dict = {}
for chrom in fasta_dict:
print(chrom)
seq = "".join(fasta_dict[chrom])
bin_dict[chrom] = [0] * len(seq)
cpg_bin_dict[chrom] = [0] * len(seq)
cgs = [cg.start() for cg in re.finditer("CG", seq)]
for pos in cgs:
bin_dict[chrom][pos] = 1
bin_dict[chrom][pos + 1] = 1
return bin_dict, cpg_bin_dict
def parse_binary_fasta(binary_fasta):
''' Parses a pre-made 'binary' fasta, assuming the sequences are on one line each'''
print("Parsing the 'binary' fasta file...")
bin_dict = {}
cpg_bin_dict = {}
if binary_fasta.endswith(".gz"):
binary_fasta = gzip.open(binary_fasta, "rb")
for line in binary_fasta:
line = str(line, "utf-8")
if line.startswith(">"):
chrom = line.strip().split(" ")[0][1:]
bin_dict[chrom] = ""
else:
bin_dict[chrom] = line.strip()
binary_fasta.close()
else:
binary_fasta = open(binary_fasta, "r")
for line in binary_fasta:
if line.startswith(">"):
chrom = line.strip().split(" ")[0][1:]
bin_dict[chrom] = ""
else:
bin_dict[chrom] = line.strip()
binary_fasta.close()
return bin_dict, cpg_bin_dict
def make_cpg_dict(bin_dict):
'''
'''
print("Making the CpG dict")
cpg_bin_dict = {}
for chrom in bin_dict:
print(chrom)
cpg_bin_dict[chrom] = [0] * len(bin_dict[chrom])
return cpg_bin_dict
def parse_all_bins(all_bins_file):
print("Parsing the bins file")
file = open(all_bins_file, "r")
for line in file:
all_bins = line.strip().split("\t")
all_bins = [int(x) for x in all_bins]
return(all_bins)
def parse_annotation(annotation):
''' Parses annotation gtf file to find coordinates of TSSs +/- 2kb'''
print("Parsing the annotation file...")
tss_dict = {}
annotation = open(annotation, "r")
for line in annotation:
if not line.startswith("##"):
line = line.split("\t")
if line[2] == "transcript":
chrom = line[0]
if chrom not in tss_dict:
print(chrom)
tss_dict[chrom] = []
if line[6] == "+":
start = int(line[3]) - 1
start -= 2000
end = start + 4000
tss_dict[chrom].append(((start, end), line[6]))
else:
start = int(line[4])
start -= 2000
end = start + 4000
tss_dict[chrom].append(((start, end), line[6]))
annotation.close()
return tss_dict
def parse_methylkit(methylkit, cpg_bin_dict):
''' Turns any 0s in cpg_bin_dict to 1s if present in methylkit file'''
print("Parsing the methylkit file...")
if methylkit.endswith(".gz"):
methylkit_file = gzip.open(methylkit, "rb")
else:
methylkit_file = open(methylkit, "r")
for line in methylkit_file:
if methylkit.endswith(".gz"):
line = str(line, "utf-8")
if not line.startswith("chrBase"):
line = line.split("\t")
pos = int(line[2]) - 1
chrom = line[1]
# cpg_bin_dict[chrom][pos] = 1
if chrom == "chr13":
cpg_bin_dict[chrom][pos] = 1
# for chrom in cpg_bin_dict:
for chrom in ["chr13"]:
temp = "".join(map(str, cpg_bin_dict[chrom]))
cpg_bin_dict[chrom] = temp
return cpg_bin_dict
def calculate_meth_bin(tss_dict, cpg_bin_dict):
'''
Checks the bins around TSSs, if there is coverage in the bin then
adds that bin to the methylation_bins dictionary, keeping track of the
total number of bins with coverage
'''
methylation_bins = [0 for x in range(400)]
for chrom in tss_dict:
# for chrom in ["chr13"]:
print(chrom)
for pos in tss_dict[chrom]:
meth_slice = cpg_bin_dict[chrom][pos[0][0]:pos[0][1]]
my_bin = 0
for x in range(0,len(meth_slice), 10):
# if sum(meth_slice[x:x+10]) != 0:
if "1" in meth_slice[x:x+10]:
if pos[1] == "+":
methylation_bins[my_bin] += 1
else:
methylation_bins[399 - my_bin] += 1
my_bin += 1
return methylation_bins
def calculate_both_bins(tss_dict, bin_dict, cpg_bin_dict):
''' Will calculate both all_bins and methylation_bins if neither is specified'''
print("Calculating the bins...")
all_bins = [0] * 400
methylation_bins = [0] * 400
# for chrom in tss_dict:
for chrom in ["chr13"]:
print(chrom)
for pos in tss_dict[chrom]:
my_slice = bin_dict[chrom][pos[0][0]:pos[0][1]]
meth_slice = cpg_bin_dict[chrom][pos[0][0]:pos[0][1]]
my_bin = 0
for x in range(0,len(my_slice), 10):
if "1" in my_slice[x:x+10]:
if pos[1] == "+":
all_bins[my_bin] += 1
else:
all_bins[399 - my_bin] += 1
my_bin += 1
my_bin = 0
for x in range(0,len(meth_slice), 10):
if "1" in meth_slice[x:x+10]:
if pos[1] == "+":
methylation_bins[my_bin] += 1
else:
methylation_bins[399 - my_bin] += 1
my_bin += 1
return all_bins, methylation_bins
def normalize_bins(all_bins, methylation_bins):
''' Normalizes the methylation bin to percent of bins covered that have CpGs'''
print("Normalizing the bins")
normal_all_bins = [(all_bins[x] / all_bins[x]) * 100 for x in range(len(all_bins))]
normal_methylation_bins = [(methylation_bins[x] / all_bins[x]) * 100 for x in range(len(all_bins))]
return(normal_all_bins, normal_methylation_bins)
def plot_bins(all_bins, methylation_bins):
''' Plots the bins simply'''
print("Plotting!")
plt.plot(all_bins)
plt.plot(methylation_bins)
plt.show()
def write_files(all_bins, methylation_bins, normal_methylation_bins, prefix):
''' Writes the bins to tab delimited files'''
print("Writing files...")
out = open("{}_allbins_raw.tab".format(prefix), "w")
out.write("\t".join(map(str, all_bins)))
out.close()
out = open("{}_methylation_raw.tab".format(prefix), "w")
out.write("\t".join(map(str, methylation_bins)))
out.close()
out = open("{}_methylation_normalized.tab".format(prefix), "w")
out.write("\t".join(map(str, normal_methylation_bins)))
out.close()
def run_script():
print("Running script...")
args = argparser()
if args.fasta == None and args.binary_fasta == None:
print("Need either a fasta or the 'binary' fasta file!")
sys.exit()
if args.binary_fasta:
bin_dict = parse_fasta(args.binary_fasta)
if args.binary_methylkit:
cpg_bin_dict = parse_fasta(args.binary_methylkit)
else:
cpg_bin_dict = make_cpg_dict(bin_dict)
cpg_bin_dict = parse_methylkit(args.methylkit, cpg_bin_dict)
elif args.fasta:
fasta_dict = parse_fasta(args.fasta)
bin_dict, cpg_bin_dict = convert_to_binary(fasta_dict)
write_fasta(bin_dict, "{}_cpgs.fa".format(args.prefix), False)
tss_dict = parse_annotation(args.annotation)
write_fasta(cpg_bin_dict, "{}_covered_cpgs.fa".format(args.prefix), False)
if args.all_bins:
all_bins = parse_all_bins(args.all_bins)
methylation_bins = calculate_meth_bin(tss_dict, cpg_bin_dict)
else:
all_bins, methylation_bins = calculate_both_bins(tss_dict, bin_dict, cpg_bin_dict)
normal_all_bins, normal_methylation_bins = normalize_bins(all_bins, methylation_bins)
write_files(all_bins, methylation_bins, normal_methylation_bins, args.prefix)
if args.plot:
plot_bins(all_bins, methylation_bins)
plot_bins(normal_all_bins, normal_methylation_bins)
if __name__ == "__main__":
run_script()
print("All done!")