-
Notifications
You must be signed in to change notification settings - Fork 0
/
combineRegions3.py
309 lines (283 loc) · 9.36 KB
/
combineRegions3.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
#!/usr/bin/python
# JMG 4/22/16
# Combining regions for a set of bismark cov files.
import sys
import os.path
import gzip
import math
def usage():
print '''Usage: python combineRegions3.py [options] -o <outfile> <infile(s)>
<outfile> Output file listing combined regions (for each: genomic
position [chrom, start, end], number of CpGs, and
methylation fraction for each sample, tab-delimited)
<infile(s)> One or more files listing methylation counts at each
genomic position (produced by SAMtoCOV.py, or
coverage2cytosine from Bismark)
Options:
To consider a particular CpG:
-r <int> Minimum number of reads at a position in a sample (def. 1)
-s <int> Minimum number of samples with the minimum number of reads
to consider a position (def. 1)
To analyze a region of CpGs:
-d <int> Maximum distance between CpGs to combine into the same
region (def. 100)
-c <int> Minimum number of CpGs in a region to report (def. 1)
-x <int> Maximum length of a region of CpGs -- regions longer than
this will be split into smaller regions (def. 1e9)
To report a particular result:
-m <int> Minimum total reads in a region for a sample (def. 1)
Other:
-v Run in verbose mode'''
sys.exit(-1)
def openRead(filename):
'''
Open filename for reading. '-' indicates stdin.
'.gz' suffix indicates gzip compression.
'''
if filename == '-':
return sys.stdin
try:
if filename[-3:] == '.gz':
f = gzip.open(filename, 'rb')
else:
f = open(filename, 'rU')
except IOError:
sys.stderr.write('Error! Cannot open %s for reading\n' % filename)
sys.exit(-1)
return f
def openWrite(filename):
'''
Open filename for writing. '-' indicates stdout.
'''
if filename == '-':
return sys.stdout
try:
f = open(filename, 'w')
except IOError:
sys.stderr.write('Error! Cannot open %s for writing\n' % filename)
sys.exit(-1)
return f
def getInt(arg):
'''
Convert given argument to int.
'''
try:
val = int(arg)
except ValueError:
sys.stderr.write('Error! Cannot convert %s to int\n' % arg)
sys.exit(-1)
return val
def splitRegion(chrom, reg, count, minCpG, minReg, \
maxLen, samples, fOut):
'''
Split a CpG region that is too large and process
each subregion via processRegion(), skipping
<minCpG> requirement but keeping <minReg>.
'''
# determine number of subregions and length
subReg = int(math.ceil((reg[-1] - reg[0]) / float(maxLen)))
lengthReg = (reg[-1] - reg[0]) / subReg
# create and process subregions
total = 0 # number of regions printed
start = 0 # index of beginning of subregion
prev = reg[0] # genomic position of beginning of subregion
for i in range(subReg - 1):
# determine subset of reg list to process
for j in range(start, len(reg)):
if reg[j] > prev + lengthReg:
break
# pass to processRegion(), with minCpG of 1
total += processRegion(chrom, reg[start:j], count, 1,
minReg, maxLen, samples, fOut)
start = j
prev += lengthReg
# process final subregion
total += processRegion(chrom, reg[start:], count, 1,
minReg, maxLen, samples, fOut)
return total
def processRegion(chrom, reg, count, minCpG, minReg, \
maxLen, samples, fOut):
'''
Produce output for a given region of CpGs: a line
containing chromosome name, start and end
coordinates, number of CpGs, and methylation
fraction for each sample, all tab-delimited.
To print a line, the region must have at least
<minCpG> sites, and at least one sample must have
at least <minReg> methylation counts.
Any region longer than <maxLen> will be split via
splitRegion().
Any sample that does not have <minReg> methylation
counts gets an 'NA' designation.
'''
if len(reg) < minCpG:
return 0
# split region larger than maxLen
if reg[-1] - reg[0] > maxLen:
return splitRegion(chrom, reg, count, minCpG, minReg, \
maxLen, samples, fOut)
flag = 0 # boolean for printing line
res = '%s\t%d\t%d\t%d' % (chrom, reg[0], reg[-1], len(reg))
for sample in samples:
meth = unmeth = 0
# sum methylated/unmeth bases at each position in region
for r in reg:
pos = str(r)
if sample in count[chrom][pos]:
meth += count[chrom][pos][sample][0]
unmeth += count[chrom][pos][sample][1]
if meth + unmeth < minReg:
# less than minimum number of counts
res += '\tNA'
else:
# compute methylated fraction
res += '\t%f' % (meth / float(meth + unmeth))
#res += '\t%d-%d' % (meth, unmeth) # to save actual counts
flag = 1
if flag:
fOut.write(res + '\n')
return 1
return 0
def combineRegions(count, total, order, minSamples, maxDist, \
minCpG, minReg, maxLen, samples, fOut):
'''
Combine data from CpG positions that are close to each
other. Process combined regions on the fly (via
processRegion() function).
'''
printed = 0 # count of printed regions
for chrom in order:
reg = [] # for saving connected positions
pos3 = 0
for pos in sorted(total[chrom], key=int):
# require a min. number of samples
if total[chrom][pos] >= minSamples:
loc = int(pos)
# if next position is more than maxDist away,
# process previous genomic region
if pos3 and loc - pos3 > maxDist:
printed += processRegion(chrom, reg, count, \
minCpG, minReg, maxLen, samples, fOut)
reg = [] # reset list
reg.append(loc)
pos3 = loc
# process last genomic region for this chromosome
printed += processRegion(chrom, reg, count, minCpG, \
minReg, maxLen, samples, fOut)
return printed
def processFile(fname, minReads, count, total, order, \
samples):
'''
Load the methylation/unmethylation counts for a file.
'''
f = openRead(fname)
# save sample name
sample = fname.split('/')[-1].split('.')[0]
while sample in samples:
sample += '-'
samples.append(sample)
# load counts from file
for line in f:
try:
chrom, pos, end, strand, meth, unmeth \
= line.rstrip().split('\t')
except ValueError:
sys.stderr.write('Error! Poorly formatted record' \
+ ' in %s:\n%s' % (fname, line))
sys.exit(-1)
meth = getInt(meth)
unmeth = getInt(unmeth)
# save counts and total
if not chrom in count:
count[chrom] = {}
total[chrom] = {}
order.append(chrom)
if not pos in count[chrom]:
count[chrom][pos] = {}
count[chrom][pos][sample] = [meth, unmeth]
# save to 'total' dict. only if sufficient coverage
if meth + unmeth >= minReads:
total[chrom][pos] = total[chrom].get(pos, 0) + 1
f.close()
def main():
'''
Main.
'''
# Default parameters
minReads = 1 # min. reads in a sample at a position
minSamples = 1 # min. samples with min. reads at a position
maxDist = 100 # max. distance between CpGs
minCpG = 1 # min. CpGs in a region
minReg = 1 # min. reads in a sample for a region
maxLen = 1000000000 # max. length of a combined region
outfile = '' # output file
fIn = [] # list of input files
verbose = 0 # verbose option
# Get command-line args
args = sys.argv[1:]
if len(args) < 2: usage()
i = 0
while i < len(args):
if args[i][0] == '-':
if args[i] == '-r':
minReads = getInt(args[i+1])
elif args[i] == '-s':
minSamples = getInt(args[i+1])
elif args[i] == '-d':
maxDist = getInt(args[i+1])
elif args[i] == '-c':
minCpG = getInt(args[i+1])
elif args[i] == '-m':
minReg = getInt(args[i+1])
elif args[i] == '-x':
maxLen = getInt(args[i+1])
elif args[i] == '-o':
outfile = args[i+1]
elif args[i] == '-v':
verbose = 1
i -= 1
elif args[i] == '-h':
usage()
else:
sys.stderr.write('Error! Unknown argument: %s\n' % args[i])
usage()
i += 1
else:
fIn.append(args[i])
i += 1
# check for I/O errors
if outfile == '':
sys.stderr.write('Error! Must supply an output file\n')
usage()
if len(fIn) == 0:
sys.stderr.write('Error! Must supply one or more input files\n')
usage()
for fname in fIn:
if not os.path.isfile(fname):
sys.stderr.write('Error! Cannot open input file %s\n' % fname)
usage()
fOut = openWrite(outfile)
# load methylation information for each sample
if verbose:
sys.stderr.write('Loading methylation information\n')
count = {} # for methylated, unmethylated counts
total = {} # for number of samples with min. coverage
order = [] # for ordered chromosome names
samples = [] # list of sample names
for fname in fIn:
if verbose:
sys.stderr.write(' file: %s\n' % fname)
processFile(fname, minReads, count, total, order, samples)
# produce output
if verbose:
sys.stderr.write('Combining regions and producing output\n')
fOut.write('\t'.join(['chr', 'start', 'end', 'CpG'] \
+ samples) + '\n')
printed = combineRegions(count, total, order, minSamples, \
maxDist, minCpG, minReg, maxLen, samples, fOut)
if verbose:
sys.stderr.write('Regions printed: %d\n' % printed)
if fOut != sys.stdout:
fOut.close()
if __name__ == '__main__':
main()