-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathbismark_methylation_extractor_split.py
executable file
·239 lines (218 loc) · 8.75 KB
/
bismark_methylation_extractor_split.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from __future__ import division, with_statement
'''
Copyright 2015, 陈同 ([email protected]).
===========================================================
'''
__author__ = 'chentong & ct586[9]'
__author_email__ = '[email protected]'
#=========================================================
desc = '''
Program description:
This is designed to split bismark_methylation_extractor output file to be suitable for following analysis.
Two types of input file are allowed.
The first type is the output of bismark_methylation_extractor or other format specified below (without the header line):
#<chromosome> <position> <strand> <count methylated> <count non-methylated> <C-context> <trinucleotide context>
The second type would be in format specified below (with header line)
#Chr Pos Ref Chain Total Met UnMet MetRate Ref_context Type
chr1 14485 C + 1 1 0 1 CCG CHG
chr1 14486 C + 1 1 0 1 CGT CpG
chr1 14489 C + 1 1 0 1 CCC CHH
'''
import sys
import os
from json import dumps as json_dumps
from time import localtime, strftime
timeformat = "%Y-%m-%d %H:%M:%S"
from optparse import OptionParser as OP
import gzip
#from multiprocessing.dummy import Pool as ThreadPool
#from bs4 import BeautifulSoup
#reload(sys)
#sys.setdefaultencoding('utf8')
debug = 0
def fprint(content):
"""
This is a Google style docs.
Args:
param1(str): this is the first param
param2(int, optional): this is a second param
Returns:
bool: This is a description of what is returned
Raises:
KeyError: raises an exception))
"""
print json_dumps(content,indent=1)
def cmdparameter(argv):
if len(argv) == 1:
global desc
print >>sys.stderr, desc
cmd = 'python ' + argv[0] + ' -h'
os.system(cmd)
sys.exit(1)
usages = "\n\t%prog -i file\n\tzcat gzippedfile | %prog -i -"
parser = OP(usage=usages)
parser.add_option("-i", "--input-file", dest="filein",
metavar="FILEIN",
help="One of the two types of files in format as described above. Gzipped file should be input as STDIN (-).")
parser.add_option("-l", "--label", dest="label",
help="One simple string normally sample name as label.")
parser.add_option("-t", "--type", dest="type",
default='first', help="<first> or <second>. Default <first>.")
parser.add_option("-s", "--statistics-only", dest="statistics",
default=False, help="Compute statistics, like distribution of reads coverage, methylation rate. Default <False>.")
parser.add_option("-o", "--output-prefix", dest="op_prefix",
help="Prefix of output files.")
parser.add_option("-v", "--verbose", dest="verbose",
action="store_true", help="Show process information")
parser.add_option("-D", "--debug", dest="debug",
default=False, action="store_true", help="Debug the program")
(options, args) = parser.parse_args(argv[1:])
assert options.filein != None, "A filename needed for -i"
return (options, args)
#--------------------------------------------------------------------
def bismark_split(fh, strandD, fhD, countD, methylRateD, statistics_only):
#<chromosome> <position> <strand> <count methylated> <count non-methylated> <C-context> <trinucleotide context>
for line in fh:
lineL = line.strip().split()
methyl = int(lineL[3])
unmethyl = int(lineL[4])
total = methyl+unmethyl
if total == 0:
continue
type = lineL[5]
if type == "CG":
type = "CpG"
countD[type] += 1
freqC = "{:.2f}".format(100.0*methyl/total)
if debug:
print >>sys.stderr, "\t".join([str(methyl), str(total), freqC, type])
methylRateD[type][freqC] = methylRateD[type].get(freqC, 0)+1
if not statistics_only:
chr = lineL[0]
base = lineL[1]
strand = strandD[lineL[2]]
freqT = "{:.2f}".format(100.0*unmethyl/total)
chrBase = '.'.join([chr, base, lineL[6]])
print >>fhD[type], '\t'.join([chrBase, chr, base, strand, str(total), freqC, freqT])
#-------------------------------------
#----------------------------------------------
def second_split(fh, strandD, fhD, countD, methylRateD, statistics_only):
#Chr Pos Ref Chain Total Met UnMet MetRate Ref_context Type
header = 1
for line in fh:
if header:
header -= 1
continue
lineL = line.strip().split()
total = lineL[4]
if total == '0':
continue
type = lineL[-1]
if type == "CG":
type = "CpG"
countD[type] += 1
MetRate = float(lineL[7])*100
freqC = "{:.2f}".format(MetRate)
methylRateD[type][freqC] = methylRateD[type].get(freqC, 0)+1
if not statistics_only:
strand = strandD[lineL[3]]
chr = lineL[0]
base = lineL[1]
freqT = "{:.2f}".format(100-MetRate)
chrBase = '.'.join([chr, base, lineL[8]])
print >>fhD[type], '\t'.join([chrBase, chr, base, strand, str(total), freqC, freqT])
#-------------------------------------
#----------------------------------------------
def output(countD, methylRateD, label, op_prefix):
'''
countD = {"CpG":0, "CHH":0, "CHG":0}
methylRateD = {"CpG":{}, "CHH":{}, "CHG":{}}
'''
count_fh = open(op_prefix+'.c_count.xls', 'w')
methyl_fh = open(op_prefix+'.methylRate.xls', 'w')
print >>count_fh, "value\tvariable\tsample"
for key, value in countD.items():
print >>count_fh, "\t".join([str(value), key, label])
count_fh.close()
print >>methyl_fh, "value\tvariable\tsample\ttype"
for type, valueD in methylRateD.items():
for variable, value in valueD.items():
print >>methyl_fh, '\t'.join([str(value), variable, label, type])
methyl_fh.close()
#---------------------------------------------------
def main():
options, args = cmdparameter(sys.argv)
#-----------------------------------
file = options.filein
label = options.label
type = options.type
statistics_only = options.statistics
op_prefix = options.op_prefix
verbose = options.verbose
global debug
debug = options.debug
#-----------------------------------
if file == '-':
fh = sys.stdin
else:
fh = open(file)
#--------------------------------
strandD = {"+":"F", "-":"R"}
fhD = {}
if not statistics_only:
CpG = op_prefix+'.CpG.txt.gz'
CHG = op_prefix+'.CHG.txt.gz'
CHH = op_prefix+'.CHH.txt.gz'
fhD = {'CpG': gzip.open(CpG, 'wb'),
'CHH': gzip.open(CHH, 'wb'),
'CHG': gzip.open(CHG, 'wb')}
for tmp_fh in fhD.values():
print >>tmp_fh, "chrBase chr base strand coverage freqC freqT"
countD = {"CpG":0, "CHH":0, "CHG":0}
methylRateD = {"CpG":{}, "CHH":{}, "CHG":{}}
if type == 'second':
second_split(fh, strandD, fhD, countD, methylRateD, statistics_only)
elif type == 'first':
#<chromosome> <position> <strand> <count methylated> <count non-methylated> <C-context> <trinucleotide context>
bismark_split(fh, strandD, fhD, countD, methylRateD, statistics_only)
else:
print >>sys.stderr, "Unsupport type"
sys.exit(1)
if not statistics_only:
for tmp_fh in fhD.values():
tmp_fh.close()
#---------------------------------------------
#-------------END reading file----------
#----close file handle for files-----
if file != '-':
fh.close()
#-----------end close fh-----------
output(countD, methylRateD, label, op_prefix)
###--------multi-process------------------
#pool = ThreadPool(5) # 5 represents thread_num
#result = pool.map(func, iterable_object)
#pool.close()
#pool.join()
###--------multi-process------------------
if verbose:
print >>sys.stderr,\
"--Successful %s" % strftime(timeformat, localtime())
if __name__ == '__main__':
startTime = strftime(timeformat, localtime())
main()
endTime = strftime(timeformat, localtime())
fh = open('python.log', 'a')
print >>fh, "%s\n\tRun time : %s - %s " % \
(' '.join(sys.argv), startTime, endTime)
fh.close()
###---------profile the program---------
#import profile
#profile_output = sys.argv[0]+".prof.txt")
#profile.run("main()", profile_output)
#import pstats
#p = pstats.Stats(profile_output)
#p.sort_stats("time").print_stats()
###---------profile the program---------