-
Notifications
You must be signed in to change notification settings - Fork 2
/
chromhmm_signalize.py
executable file
·280 lines (227 loc) · 9.13 KB
/
chromhmm_signalize.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
#!/usr/bin/env python
"""
`chromhmm_signalize.py`
-----------------------
Starting from bigWig or bedGraph files, this program helps construct input
files for ChromHMM's BinarizeSignal program.
ChromHMM's BinarizeSignal expects input data of this form::
{celltype} {chromosome}
{mark1} {mark2} {mark3}
0 4 0
1 3 0
2 1 9
Values are read counts in each consecutive bin of a chromosome, for each mark
(e.g., histone modifications).
If you have mapped reads, then ChromHMM's BinarizeBed is best for generating
this kind of file. However, lots of publicly availiable data are available as
bigWig files, which do not have regularly spaced bins.
The "rebin" program interpolates bigWig or bedGraph signal into regularly
spaced bins, and separates this interpolated signal by chromosome.
The "combine" program merges interpolated files into a single file suitable for
use by ChromHMM's BinarizeSignal program (and the rest of the ChromHMM
workflow).
REBIN
-----
The config file is a tab-delimited file with 3 required and 1 optional
field::
{celltype} {mark} {path} [{control}]
Each file specified by `path` will be interpolated into consecutive bins of
size `binsize` and will be split into mulitple files based on the
chromosomes present in the original file. These new files will be named
according to the scheme::
{binned_dir}/{celltype}_{mark}_{binsize}_{chrom}.binned
In the config file, `path` can refer to either a bigWig file or a bedGraph
file (format is detected automatically). If bigWig, then it will be
converted into a bedGraph with UCSC's bigWigToBedGraph program, which must
be on the path. This converted file will be saved with a '.bedgraph'
extension next to the original bigWig. If this file already exists, it
will be used without doing the conversion again.
COMBINE
-------
After running `rebin`, run `combine` to combine all files of the same
celltype and chromosome into a single "_signal" file, which can be directly
used with ChromHMM's BinarizeSignal program. Best practice is to re-use
the same config file used for `rebin`.
GENERAL WORKFLOW
----------------
* Write a config file
(see above for format; here called `config.txt`)
* `chromhmm-signalize.py config.txt hg19`
(see `binned` dir for output by default)
* `chromhmm-signalize.py config.txt binned`
(see `signal` dir for output by default)
* `java -jar -xm4000M ChromHMM.jar BinarizeSignal signal binarized`
(see `binarized` for output)
* `java -jar -xm4000M ChromHMM.jar LearnModel binarized output 9 hg19`
(see `output` for 9-state model results)
"""
import argh
from argh import arg
import os
import logging
import pybedtools
import glob
import numpy as np
import itertools
logging.basicConfig(
level=logging.INFO, format="[%(asctime)s] %(levelname)s: %(msg)s")
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
def is_bigwig(fn):
"""
checks magic number to see if we're working with a bigWig file
"""
fh = open(fn, 'rb')
magic = fh.read(4)
if (magic == '&\xfc\x8f\x88') or (magic == '\x88\x8f\xfc&'):
return True
class Config(object):
"""
Lightweight object to parse and store config info
"""
def __init__(self, filename):
self.filename = filename
self.config = []
for line in open(filename):
if line.startswith('#'):
continue
toks = line.strip().split('\t')
if len(toks) == 1:
continue
celltype, mark, path = toks[:3]
if len(toks) == 4:
control = toks[-1]
else:
control = None
self.config.append(
(celltype, mark, path, control)
)
@arg('configfile', help='input config file')
@arg('genome', help='assembly name')
@arg('--binsize', help='Bin size for _signal file, default is "%(default)s"')
@arg('--binned_dir', help='Output dir, default "%(default)s"')
@arg('--quiet', help='disable logging')
def rebin(configfile, genome, binsize=200, quiet=False, binned_dir='binned'):
"""
Split bigwig/bedGraph files by chromosome, and interpolate signal into
`binsize` bins.
"""
if quiet:
logger.disabled = True
config = Config(configfile)
genome = pybedtools.chromsizes(genome)
if not os.path.exists(binned_dir):
os.makedirs(binned_dir)
for celltype, mark, path, control in config.config:
chrom = None
fout = None
output_pattern = (
'{binned_dir}/{celltype}-{mark}-{binsize}-'
'{{chrom}}.binned'.format(**locals()))
logger.info('{path} -> {output_pattern}'.format(**locals()))
# convert to bedGraph if needed
if is_bigwig(path):
bg = path + '.bedgraph'
if not os.path.exists(bg):
logger.info('converting to bedgraph')
os.system('bigWigToBedGraph %s %s' % (path, bg))
else:
logger.info('%s already exists, using it' % bg)
else:
bg = path
bt = pybedtools.BedTool(bg)
x = []
y = []
def write_interpolated_results(x, y, chrom):
"""
interpolation and file-creation happens here
"""
logger.info(chrom)
filename = output_pattern.format(chrom=chrom)
max_pos = genome[chrom][-1]
x = np.array(x)
y = np.array(y)
xi = np.arange(0, max_pos, binsize)
yi = np.interp(xi, x, y, left=-1, right=-1)
fout = open(filename, 'w')
fout.write('%s\t%s\n' % (celltype, chrom))
fout.write('%s\n' % mark)
for xii, yii in itertools.izip(xi, yi):
fout.write('%s\n' % yii)
fout.close()
# try to save a little memory
del x, y, xi, yi
for i in bt:
if (i.chrom != chrom) and (chrom is not None):
write_interpolated_results(x, y, chrom)
x = []
y = []
# use the midpoint of each bedgraph feature
x.append(i.start + (i.stop - i.start) / 2)
y.append(float(i[-1]))
chrom = i.chrom
# last one
write_interpolated_results(x, y, chrom)
if quiet:
logger.disabled = False
@arg('configfile', help='input config file')
@arg('--binned_dir', help='Dir containing binned data, default "%(default)s"')
@arg('--binsize', help='Bin size for _signal file, default is "%(default)s"')
@arg('--combined_dir',
help='Output dir for combined files, default is "%(default)s"')
def combine(configfile, binned_dir='binned', binsize=200,
combined_dir='signal'):
"""
Combine interpolated files from the "rebin" command into "_signal" files
ready for ChromHMM's BinarizeSignal.
"""
config = Config(configfile)
if not os.path.exists(combined_dir):
os.makedirs(combined_dir)
# accumluate all possible files based on the config
files = []
for celltype, mark, path, control in config.config:
pattern = os.path.join(
binned_dir,
'{celltype}-{mark}-{binsize}-*.binned'.format(**locals()))
files.extend(glob.glob(pattern))
# One file for each (celltype, chrom) combo which aggregates all marks.
def sortkey(x):
(celltype, mark, binsize, chrom) = \
os.path.basename(x).replace('.binned', '').split('-')
return (celltype, chrom)
files.sort(key=sortkey)
for item, group in itertools.groupby(files, sortkey):
# this group should now have all the same chromosome and celltype
group = sorted(list(group))
celltype, chrom = item
logger.info('%s, %s, combining:' % (celltype, chrom))
for g in group:
logger.info(' ' + g)
open_files = [open(f) for f in group]
header_1 = [f.readline() for f in open_files]
cells, chroms = zip(*[i.strip().split('\t') for i in header_1])
if len(set(cells)) != 1 or list(set(cells))[0] != celltype:
raise ValueError("expecting a single celltype, %s found" % cells)
if len(set(chroms)) != 1 or list(set(chroms))[0] != chrom:
raise ValueError(
"filename chrom (%s) does not match header (%s)"
% (chrom, chroms))
header_2 = [f.readline() for f in open_files]
marks = [i.strip() for i in header_2]
filename = os.path.join(
combined_dir,
'{celltype}_{chrom}_{binsize}_signal.txt'.format(**locals()))
logger.info('writing %s' % filename)
final = open(filename, 'w')
final.write('%s\t%s\n' % (celltype, chrom))
final.write('\t'.join(marks) + '\n')
for values in itertools.izip(*open_files):
values = [v.strip() for v in values]
final.write('\t'.join(values) + '\n')
final.close()
if __name__ == "__main__":
parser = argh.ArghParser()
#parser.usage = __doc__
parser.add_commands([rebin, combine])
parser.dispatch()