forked from Tong-Chen/NGS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbismark_saturation.py
More file actions
executable file
·283 lines (265 loc) · 10.4 KB
/
bismark_saturation.py
File metadata and controls
executable file
·283 lines (265 loc) · 10.4 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from __future__ import division, with_statement
'''
Copyright 2015, 陈同 (chentong_biology@163.com).
===========================================================
'''
__author__ = 'chentong & ct586[9]'
__author_email__ = 'chentong_biology@163.com'
#=========================================================
desc = '''
Program description:
'''
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 = "%prog -i file"
parser = OP(usage=usages)
parser.add_option("-i", "--input-file", dest="filein",
metavar="FILEIN", help="Bismark aligned BAM file. REQUIRED.")
parser.add_option("-L", "--file-label", dest="label",
metavar="LABEL", help="File label")
parser.add_option("-S", "--summary-only", dest="summary",
default=0, type='int', metavar="Upper S", help="Specify 1 to only do summary part.")
parser.add_option("-o", "--output-prefix", dest="op",
metavar="FILEIN", help="Prefix of output files. REQUIRED.")
parser.add_option("-g", "--genome-fa", dest="genome",
metavar="GENOME FASTA", help="Genome sequence file. REQUIRED.")
parser.add_option("-f", "--methylC-count-for-all-reads", dest="full",
default="", metavar="FILEIN",
help="Called methylated C sites using all reads. Normally <SingleCmet.gz>.")
parser.add_option("-l", "--percentile-floor", dest="per_lb",
metavar="PERCENTILE_LOW_BOUND", default=5, type="int",
help="Sampling starts from this percentile. An integer between 0 and 100. default=5")
parser.add_option("-u", "--percentile-ceiling", dest="per_ub",
metavar="PERCENTILE_UP_BOUND", default=95, type="int",
help="Sampling ends this percentile. An integer between 0 and 100. default=95. ")
parser.add_option("-s", "--percentile-step", dest="per_step",
metavar="PERCENTILE_STEP", default=5, type="int",
help="Sampling frequency. Smaller value means more sampling times. An integer between 0 and 100. default=5.")
parser.add_option("-m", "--mapped-reads", dest="map_reads",
help="Bismark output statistics or a number")
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 methylC(output, genome):
prefix = output[:-4]
pileup = ['samtools mpileup -f', genome, output, '>', prefix+'.pileup']
pileup = ' '.join(pileup)
if debug:
print >>sys.stderr, pileup
else:
os.system(pileup)
metlevel = ['SingleC_MetLevel.pl', genome, prefix+'.pileup | gzip -c', '>', prefix+'.SingleCmet.gz']
metlevel = ' '.join(metlevel)
if debug:
print >>sys.stderr, metlevel
else:
os.system(metlevel)
#--------------------END methylC-------------------
def downSample2(file, per_lb, per_ub, per_step, op, genome=''):
for per in range(per_lb, per_ub+per_step, per_step):
per = str(per / 100)
output = op+'_'+per+'.bam'
downsample = ["samtools view -b -s", per, file, '>', output]
downsample = ' '.join(downsample)
if debug:
print >>sys.stderr, downsample
else:
os.system(downsample)
methylC(output, genome)
def downSample1(file, per_lb, per_ub, per_step, op, genome=''):
for per in range(per_lb, per_ub+per_step, per_step):
per = str(per / 100)
prefix = op+'_'+per
if os.path.exists(prefix+'.SingleCmet.gz'):
print >>sys.stderr, "Exists gz "+prefix
continue
elif os.path.exists(prefix+'.SingleCmet'):
print >>sys.stderr, "Compressing "+prefix
os.system("gzip "+prefix+'.SingleCmet')
continue
downsample = ["samtools view -u -s", per, file, '|',
'samtools mpileup -f', genome, '- >', prefix+'.pileup']
downsample = ' '.join(downsample)
if debug:
print >>sys.stderr, downsample
else:
os.system(downsample)
#methylC(output, genome)
metlevel = ['SingleC_MetLevel.pl', genome, prefix+'.pileup | gzip -c', '>', prefix+'.SingleCmet.gz']
metlevel = ' '.join(metlevel)
if debug:
print >>sys.stderr, metlevel
else:
os.system(metlevel)
rm = "/bin/rm -f "+prefix+'.pileup &'
os.system(rm)
#----------------downSample----------------------------
def downSample(file, per_lb, per_ub, per_step, op, genome=''):
for per in range(per_lb, per_ub+per_step, per_step):
per = str(per / 100)
prefix = op+'_'+per
if os.path.exists(prefix+'.SingleCmet.gz'):
print >>sys.stderr, "Exists gz "+prefix
continue
elif os.path.exists(prefix+'.SingleCmet'):
print >>sys.stderr, "Compressing "+prefix
os.system("gzip "+prefix+'.SingleCmet')
continue
downsample = ["samtools view -u -s", per, file, '|',
'samtools mpileup -f', genome, '- |',
'SingleC_MetLevel.pl', genome, '- | gzip -c', '>', prefix+'.SingleCmet.gz']
downsample = ' '.join(downsample)
if debug:
print >>sys.stderr, downsample
else:
os.system(downsample)
#methylC(output, genome)
#metlevel = ['SingleC_MetLevel.pl', genome, prefix+'.pileup | gzip -c', '>', prefix+'.SingleCmet.gz']
#metlevel = ' '.join(metlevel)
#if debug:
# print >>sys.stderr, metlevel
#else:
# os.system(metlevel)
# rm = "/bin/rm -f "+prefix+'.pileup &'
# os.system(rm)
#----------------downSample----------------------------
def countLines(file, typeL):
countD = {}
for type in typeL:
countD[type] = 0
with gzip.open(file, 'rb') as f:
for line in f:
line = line.rstrip()
for type in typeL:
if line.endswith(type):
countD[type] += 1
break
#-------------------------
#-------------------------------
#----------------------------------
return countD
#-----------------------------------------
def countLines2(file, typeL):
countD = {}
for type in typeL:
cmd = "zcat "+file+' | grep "'+type+'$" | wc -l'
countD[type] = int(list(os.popen(cmd))[0])
#with gzip.open(file, 'rb') as f:
# for line in f:
# line = line.rstrip()
# for type in typeL:
# if line.endswith(type):
# countD[type] += 1
# break
#-------------------------
#-------------------------------
#----------------------------------
return countD
def main():
options, args = cmdparameter(sys.argv)
#-----------------------------------
file = options.filein
label = options.label
summary_only = options.summary
op = options.op
genome = options.genome
full = options.full
per_lb = options.per_lb
per_ub = options.per_ub
per_step = options.per_step
map_reads = options.map_reads
if map_reads.isdigit():
map_reads = int(map_reads)
else:
for line in open(map_reads):
if line.startswith("Number of paired-end alignments with a unique best hit"):
map_reads = int(line.split(':')[1])
break
assert isinstance(map_reads, int), map_reads
verbose = options.verbose
global debug
debug = options.debug
typeL = ['CpG', 'CHH', 'CHG']
#-----------------------------------
summary = op + '.summary.xls'
summary_fh = open(summary, 'w')
if not summary_only:
downSample(file, per_lb, per_ub, per_step, op, genome)
print >>summary_fh, "Per\tvariable\tvalue\tsample"
print "Per\tvariable\tvalue\tsample"
for per in range(per_lb, per_ub+per_step, per_step):
per = per / 100
prefix = op+'_'+str(per)
file = prefix+'.SingleCmet.gz'
print >>sys.stderr, file
reads = int(per*map_reads)
countD = countLines(file, typeL)
for type in typeL:
print >>summary_fh, "{}\t{}\t{}\t{}".format(reads, type, countD[type], label)
print "{}\t{}\t{}\t{}".format(reads, type, countD[type], label)
countD = countLines(full, typeL)
for type in typeL:
print >>summary_fh, "{}\t{}\t{}\t{}".format(map_reads, type, countD[type], label)
print "{}\t{}\t{}\t{}".format(map_reads, type, countD[type], label)
summary_fh.close()
#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---------