forked from hqyone/BCR_Evaluator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBCReval.py
341 lines (306 loc) · 12.6 KB
/
BCReval.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
#!/usr/bin/python
# BCR evaluator is used to evaluate bisulfite conversion ratio in WGSBS experiment
# Author : Quanyuan He Ph.D
# Email : [email protected]
# Insititution: School of Medicine, Hunan Normal University
# Licensed under the MIT License
# Last update: 2019/02/11
# version: 1.3
import sys, getopt
import re , os
import gzip
def reverse_complement(dna):
complement = {'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A'}
return ''.join([complement[base] for base in dna[::-1]])
# All possible telomeric block
tel_aseq_ls0 = ['TTTTAA','CTTTAA','TCTTAA','TTCTAA','CCTTAA','TCCTAA','CTCTAA','CCCTAA']
tel_aseq_ls1 = ['TTAAAA','TTAAAG','TTAAGA','TTAGAA','TTAAGG','TTAGGA','TTAGAG','TTAGGG']
# For test only
testTeloSeq = "CCCTAAGGTCCTAACTCTAACCTTAATTCTAAGGTTTTAATCTTAATTTTAA"
class ResultReport:
def __init__(self, file):
self.file= file
self.type = "" #+/-
self.min_unit_count= 0 # The mininal broke number
self.total_reads = 0 # The total number of reads in the fastq file
self.total_telo_reads = 0 # The total number of telomeric reads
self.total_n3_reads = 0 # The number of c strand orignal N3 reads, which have at least one N3 block with three potential methylated sites
self.total_unit_num = 0 # The total number of telomeric brock/units
self.c_strand_unit_num = 0 # The number of brocks from C strands
self.g_len_series = "" # The histogram of brock number in g strand original sequences
self.c_len_series = "" # The histogram of brock number in c strand original sequences
self.a_count_series = "" # The statistics of all possible telomeric blocks from FASTQ_1
self.b_count_series = "" # The statistics of all possible telomeric blocks from FASTQ_2
self.rc1 = float(0) # The methylation ratio of site1
self.rc2 = float(0) # The methylation ratio of site2
self.rc3 = float(0) # The methylation ratio of site3
def getTitle(self):
return "file\t"+"type\t"\
+"min_unit_count\ttotal_reads\t"\
+"total_telo_reads\ttotal_n3_reads\ttotal_unit_num\t"\
+"c_strand_unit_num\t"\
+"g_len_series\t"\
+"c_len_series\t"+",".join(tel_aseq_ls0)\
+"\t"+",".join(tel_aseq_ls1)+"\t"+"rc1\t"+"rc2\t"+"rc3"
def getValues(self):
return self.file+"\t"\
+ str(self.type)+"\t"\
+ str(self.min_unit_count)+"\t"\
+ str(self.total_reads)+"\t"\
+ str(self.total_telo_reads)+"\t" \
+ str(self.total_n3_reads) + "\t" \
+ str(self.total_unit_num) + "\t" \
+ str(self.c_strand_unit_num) + "\t" \
+ self.g_len_series+"\t"\
+ self.c_len_series+"\t"\
+ self.a_count_series+"\t"\
+ self.b_count_series+"\t"\
+ str(round(self.rc1, 4))+"\t"\
+ str(round(self.rc2, 4))+"\t"\
+ str(round(self.rc3, 4))
# Patterns of C strand
p0 = '(([C,T]{3}TAA)+)'
# Patterns of G strand
p1 = '((TTA[G,A]{3})+)'
def findLongestTelomereMatch(seq, pattern):
best_len = 0
best_match_str = ""
matchs = re.findall(pattern, seq)
if matchs:
for m in matchs:
if len(m[0])>best_len:
best_len = len(m[0])
best_match_str = m[0]
return best_match_str
def findBestTelomereMatch(seq):
bs1= findLongestTelomereMatch(seq, p0)
bs2 = findLongestTelomereMatch(seq, p1)
if len(bs1)>=len(bs2):
return [0,bs1] # C strand pattern If the reads can't match both of strands, just count as 0.
else:
return [1,bs2] # G strand pattern
#a = findLongestTelomereMatch(testTeloSeq, p0)
def getTeloRepCount(min_unit_count, fastq_file):
len_dic0 = [0]*26
len_dic1 = [0]*26
report = ResultReport(fastq_file)
python_version = sys.version_info[0]
report.min_unit_count = min_unit_count
result_str =""
# tel_aseq_ls0 = ['TTTTAA','CTTTAA','TCTTAA','TTCTAA','CCTTAA','TCCTAA','CTCTAA','CCCTAA']
tel_seq_dic0 = {
'CCCTAA': 0,
'TCCTAA': 0,
'CTCTAA': 0,
'CCTTAA': 0,
'TTCTAA': 0,
'CTTTAA': 0,
'TCTTAA': 0,
'TTTTAA': 0
}
site1_ls0=[tel_aseq_ls0[1], tel_aseq_ls0[4], tel_aseq_ls0[6], tel_aseq_ls0[7]]
site2_ls0=[tel_aseq_ls0[2], tel_aseq_ls0[4], tel_aseq_ls0[5], tel_aseq_ls0[7]]
site3_ls0=[tel_aseq_ls0[3], tel_aseq_ls0[5], tel_aseq_ls0[6], tel_aseq_ls0[7]]
# tel_aseq_ls1 = ['TTAAAA','TTAAAG','TTAAGA','TTAGAA','TTAAGG','TTAGGA','TTAGAG','TTAGGG']
tel_seq_dic1 = {
'TTAGGG': 0,
'TTAGGA': 0,
'TTAGAG': 0,
'TTAAGG': 0,
'TTAGAA': 0,
'TTAAAG': 0,
'TTAAGA': 0,
'TTAAAA': 0
}
site1_ls1=[tel_aseq_ls1[1], tel_aseq_ls1[4], tel_aseq_ls1[6], tel_aseq_ls1[7]]
site2_ls1=[tel_aseq_ls1[2], tel_aseq_ls1[4], tel_aseq_ls1[5], tel_aseq_ls1[7]]
site3_ls1=[tel_aseq_ls1[3], tel_aseq_ls1[5], tel_aseq_ls1[6], tel_aseq_ls1[7]]
unit_len = 6 # The lengtho of "TTAGGG"
total_read_number = 0
total_telo_number = 0
c_n3_read_number = 0
g_n3_read_number = 0
#FASTQ = open(fastq_file,"r")
# Go through fastq_file to classify sequences
FASTQ=gzip.open(fastq_file, "rb")
for line in FASTQ:
if line.startswith("@".encode('utf-8')):
total_read_number+=1
n = 1
if python_version>=3:
line = FASTQ.readline().strip()
else:
line = FASTQ.next().strip()
[type, bs] = findBestTelomereMatch(line)
if type == 0: # Seqeuence containing more (CCCTAA) blocks
len_dic0[int(len(bs)/unit_len)]+=1
if len(bs)/unit_len >= min_unit_count:
total_telo_number+=1
if "CCCTAA" in line:
c_n3_read_number+=1
for i in range(0, len(bs),unit_len):
if bs[i:i+unit_len] in tel_seq_dic0:
tel_seq_dic0[bs[i:i+unit_len]]+=1
else:
print (bs[i:i+unit_len])
elif type == 1: # Seqeuence containing more (TTAGGG) blocks
len_dic1[int(len(bs)/unit_len)]+=1
if len(bs)/unit_len >= min_unit_count:
total_telo_number+=1
if "TTAGGG" in line:
g_n3_read_number += 1
for i in range(0, len(bs), unit_len):
if bs[i:i+unit_len] in tel_seq_dic1:
tel_seq_dic1[bs[i:i+unit_len]]+=1
else:
print (bs[i:i+unit_len])
report.c_len_series = ",".join(str(x) for x in len_dic0)
report.g_len_series = ",".join(str(x) for x in len_dic1)
report.total_reads = total_read_number
report.total_telo_reads = total_telo_number
#result_str += "len_dic0:\t"+"\t".join(str(x) for x in len_dic0)+"\n"
#result_str += "len_dic1:\t"+"\t".join(str(x) for x in len_dic1)+"\n"
temp_ls = []
total_unit_num = 0
for i in tel_aseq_ls0:
total_unit_num += tel_seq_dic0[i]
temp_ls.append(str(tel_seq_dic0[i]))
report.a_count_series = ",".join(temp_ls)
#result_str += "\t".join(tel_aseq_ls0)+"\n"
#result_str += "\t".join(temp_ls)+"\n"
temp_ls = []
for i in tel_aseq_ls1:
total_unit_num+=tel_seq_dic1[i]
temp_ls.append(str(tel_seq_dic1[i]))
report.b_count_series = ",".join(temp_ls)
#result_str += "\t".join(tel_aseq_ls1)+"\n"
#result_str += "\t".join(temp_ls)+"\n"
#Summary for each sites
#transformed_unit_num = total_unit_num-tel_seq_dic0['CCCTAA']-tel_seq_dic1['TTAGGG']
c_strand_unit_num = 0
c_strand_completed_trans_unit_num = 0 #All C are unmethylated
site1=0
site2=0
site3=0
#Decide what the strand represented dy the fastq file
if (tel_seq_dic0[tel_aseq_ls0[7]]<tel_seq_dic1[tel_aseq_ls1[7]]):
# if the number of 'CCCTAA' blocks < number of 'TTAGGG' blocks
# The file is from FASTQ_1
# So only count transformation in TTTTAA related reads
report.type = "+"
for key in tel_seq_dic0:
c_strand_unit_num += tel_seq_dic0[key]
c_strand_completed_trans_unit_num = tel_seq_dic0['TTTTAA']
for i in site1_ls0:
if i in tel_seq_dic0:
site1 += tel_seq_dic0[i]
for i in site2_ls0:
if i in tel_seq_dic0:
site2 += tel_seq_dic0[i]
for i in site3_ls0:
if i in tel_seq_dic0:
site3 += tel_seq_dic0[i]
else:
# The file is from FASTQ_2
# So only count transformation in TTAAAA related reads
report.type = "-"
for key in tel_seq_dic1:
c_strand_unit_num+=tel_seq_dic1[key]
c_strand_completed_trans_unit_num = tel_seq_dic1['TTAAAA']
for i in site1_ls1:
if i in tel_seq_dic1:
site1 += tel_seq_dic1[i]
for i in site2_ls1:
if i in tel_seq_dic1:
site2 += tel_seq_dic1[i]
for i in site3_ls1:
if i in tel_seq_dic1:
site3 += tel_seq_dic1[i]
if report.type=="+":
report.total_n3_reads = c_n3_read_number
else:
report.total_n3_reads = g_n3_read_number
report.total_unit_num = total_unit_num
report.c_strand_unit_num = c_strand_unit_num
report.rc1 = round(float(site1) / c_strand_unit_num, 4)
report.rc2 = round(float(site2) / c_strand_unit_num, 4)
report.rc3 = round(float(site3) / c_strand_unit_num, 4)
"""
complete_transform_unit_num = tel_seq_dic0['TTTTAA']+tel_seq_dic1['TTAAAA']
result_str += "Total_Unit_Number:\t"+str(total_unit_num)+"\n"
result_str += "C_Stand_Unit_Number:\t"+str(c_strand_unit_num)+"\n"
result_str += "C_Stand_Complete_Transform_Unit_num:\t"+str(c_strand_completed_trans_unit_num)+"\n"
result_str += str(site1)+"\t"+str(site2)+"\t"+str(site3)+"\n"
result_str += str(round(float(site1)/c_strand_unit_num,4))+"\t"+str(round(float(site2)/c_strand_unit_num,4))+"\t"+str(round(float(site3)/c_strand_unit_num,4))+"\n"
"""
#print(report.getTitle())
#print(report.getValues())
return report
"""
# Inline testing part
findLongestTelomereMatch(testTeloSeq, p1)
n = 6
#rep_unit = "TTGCAA"
#fastq_file = "/home/server3/test.fastq.gz"
fastq_file="/home/server3/data2/zmq/ENCFF567DAI.fastq.gz"
result = getTeloRepCount(n,fastq_file)
print(result.getValues())
# python BCReval.py -n 8 -i /home/server3/data/zmq/ENCFF156UKB.fastq.gz -o ./FF156UKB_8.txt
"""
help_text="BCR evaluator is used to evaluate bisulfite conversion ratio in WGSBS experiment\n"\
+"Author : Quanyuan He Ph.D\n"\
+"Email : [email protected]\n"\
+"Last update: 2018/12/16\n"\
+"version: 1.2\n"\
+"\n"\
+"Usage: BCReval.py -n <rep_number> -i <inputfiles> -o <outputfile>\n"\
+"-n\t\tmin_rep_number\tmininal number of repeat units (default:6)','\n"\
+"-i\t--ifile\tinnputfiles\tfastq files seperate by ','\n"\
+"-o\t--ofile\toutputfile\toutput will be printed in the file or at console if it is null\n"
def main(argv):
min_rep_number = 6
inputfiles = ''
outputfile = ''
try:
opts, args = getopt.getopt(argv,"hn:i:o:",["ifile=","ofile="])
except getopt.GetoptError:
print ('BCReval.py -n <rep_number> -i <inputfiles> -o <outputfile>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print (help_text)
sys.exit()
elif opt in ("-n"):
try:
min_rep_number=int(arg)
except:
print (arg)
print("min_rep_number should be a integer!")
sys.exit()
elif opt in ("-i", "--ifile"):
inputfiles = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
fastq_files = inputfiles.split(",")
OUT = open(outputfile, "w")
if OUT:
if outputfile!="" and os.path.isfile(outputfile):
a = ResultReport("")
OUT.write(a.getTitle())
for f in fastq_files:
if os.path.isfile(f):
rep = getTeloRepCount(min_rep_number, f)
out_str = rep.getValues()
if OUT:
OUT.write(out_str+"\n")
else :
print (out_str+"\n")
else :
if f=="":
print ("Where are FASTQ files?")
else:
print (f+" is not a file, omit.\n" )
if OUT:
OUT.close()
if __name__ == "__main__":
main(sys.argv[1:])