-
Notifications
You must be signed in to change notification settings - Fork 11
/
visualize.py
233 lines (199 loc) · 8.75 KB
/
visualize.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
#!/usr/bin/env python
"""
Script for visualizing results from a trained model. Kernels from the first convolutional layer are converted to histogram plots and a .meme file.
Use `visualize.py -h` to see an auto-generated description of advanced options.
"""
import numpy as np
import utils
# Standard library imports
import sys
import os
import errno
import argparse
def output_results(bigwig_names, datagen_bed, model, output_dir):
from keras import backend as K
from keras.models import Model
print 'Visualizing first convolutional layer'
layer_names = [l.name for l in model.layers]
conv_layer_index = layer_names.index('convolution1d_1')
conv_layer = model.layers[conv_layer_index]
num_motifs = conv_layer.nb_filter
w = conv_layer.filter_length
fwd_conv, rev_conv = conv_layer.get_output_at(0), conv_layer.get_output_at(1)
f = K.function(model.input,
[fwd_conv.argmax(axis=1), fwd_conv.max(axis=1),
rev_conv.argmax(axis=1), rev_conv.max(axis=1)])
print 'Getting activations'
num_seqs = len(datagen_bed)
print 'Processing', str(num_seqs), 'sequences'
motifs = np.zeros((num_motifs, w, 4 + len(bigwig_names)))
nsites = np.zeros(num_motifs)
num_seqs_processed = 0
while num_seqs_processed < num_seqs:
x = datagen_bed.next()
num_seqs_processed += len(x[0])
z = f(x)
max_inds = z[0] # N x M matrix, where M is the number of motifs
max_acts = z[1]
max_inds_rc = z[2]
max_acts_rc = z[3]
for m in xrange(num_motifs):
for n in xrange(len(x[0])):
# Forward strand
if max_acts[n, m] > 0:
nsites[m] += 1
motifs[m] += x[0][n, max_inds[n, m]:max_inds[n, m] + w, :]
# Reverse strand
if max_acts_rc[n, m] > 0:
nsites[m] += 1
motifs[m] += x[1][n, max_inds_rc[n, m]:max_inds_rc[n, m] + w, :]
print 'Making motifs'
f = open(output_dir + '/motifs.txt', 'w')
f.write('MEME version 4.9.0\n\n'
'ALPHABET= ACGT\n\n'
'strands: + -\n\n'
'Background letter frequencies (from uniform background):\n'
'A 0.25000 C 0.25000 G 0.25000 T 0.25000\n\n')
for m in xrange(num_motifs):
if nsites[m] == 0:
continue
f.write('MOTIF M%i O%i\n' % (m, m))
f.write("letter-probability matrix: alength= 4 w= %i nsites= %i E= 1337.0e-6\n" % (w, nsites[m]))
for j in xrange(w):
f.write("%f %f %f %f\n" % tuple(1.0 * motifs[m, j, 0:4] / np.sum(motifs[m, j, 0:4])))
f.write('\n')
f.close()
print 'Saving mean bigwig signals'
motifs_bws = motifs[:, :, 4:]
for m in xrange(num_motifs):
motifs_bws[m] = motifs_bws[m] / nsites[m]
for i, bigwig_name in enumerate(bigwig_names):
np.save(output_dir + '/' + bigwig_name + '_meansignal.npy', motifs_bws[:, :, i])
print 'Visualizing time distributed layer'
datagen_bed.reset()
timedistributed_layer_index = layer_names.index('timedistributed_1')
timedistributed_layer = model.layers[timedistributed_layer_index]
fwd_timedistributed, rev_timedistributed = timedistributed_layer.get_output_at(0), timedistributed_layer.get_output_at(1)
intermediate_layer_model = Model(input=model.input,output=[fwd_timedistributed, rev_timedistributed])
print 'Getting activations'
motifs = np.zeros((num_motifs, w, 4 + len(bigwig_names)))
nsites = np.zeros(num_motifs)
num_seqs_processed = 0
while num_seqs_processed < num_seqs:
x = datagen_bed.next()
num_seqs_processed += len(x[0])
z = intermediate_layer_model.predict(x)
max_inds = z[0].argmax(axis=1) # N x M matrix, where M is the number of motifs
max_acts = z[0].max(axis=1)
max_inds_rc = z[1].argmax(axis=1)
max_acts_rc = z[1].max(axis=1)
for m in xrange(num_motifs):
for n in xrange(len(x[0])):
# Forward strand
if max_acts[n, m] > 0:
nsites[m] += 1
motifs[m] += x[0][n, max_inds[n, m]:max_inds[n, m] + w, :]
# Reverse strand
if max_acts_rc[n, m] > 0:
nsites[m] += 1
motifs[m] += x[1][n, max_inds_rc[n, m]:max_inds_rc[n, m] + w, :]
print 'Making motifs'
f = open(output_dir + '/motifs2.txt', 'w')
f.write('MEME version 4.9.0\n\n'
'ALPHABET= ACGT\n\n'
'strands: + -\n\n'
'Background letter frequencies (from uniform background):\n'
'A 0.25000 C 0.25000 G 0.25000 T 0.25000\n\n')
for m in xrange(num_motifs):
if nsites[m] == 0:
continue
f.write('MOTIF M%i O%i\n' % (m, m))
f.write("letter-probability matrix: alength= 4 w= %i nsites= %i E= 1337.0e-6\n" % (w, nsites[m]))
for j in xrange(w):
f.write("%f %f %f %f\n" % tuple(1.0 * motifs[m, j, 0:4] / np.sum(motifs[m, j, 0:4])))
f.write('\n')
f.close()
print 'Saving mean bigwig signals'
motifs_bws = motifs[:, :, 4:]
for m in xrange(num_motifs):
motifs_bws[m] = motifs_bws[m] / nsites[m]
for i, bigwig_name in enumerate(bigwig_names):
np.save(output_dir + '/' + bigwig_name + '_meansignal2.npy', motifs_bws[:, :, i])
def make_argument_parser():
"""
Creates an ArgumentParser to read the options for this script from
sys.argv
"""
parser = argparse.ArgumentParser(
description="Visualize results of a trained model.",
epilog='\n'.join(__doc__.strip().split('\n')[1:]).strip(),
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('--inputdir', '-i', type=str, required=True,
help='Folder containing data.')
parser.add_argument('--modeldir', '-m', type=str, required=True,
help='Folder containing trained model generated by train.py.')
parser.add_argument('--bed', '-b', type=str, required=True,
help='BED file containing intervals to generate predictions from. Typically a ChIP or DNase peak file.')
parser.add_argument('--chrom', '-c', type=str, required=False,
default='chr11',
help='Chromosome to use for visualization. Only sequences on this chromosome will be used (default: chr11).')
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-o', '--outputdir', type=str,
help='The output directory. Causes error if the directory already exists.')
group.add_argument('-oc', '--outputdirc', type=str,
help='The output directory. Will overwrite if directory already exists.')
return parser
def main():
"""
The main executable function
"""
parser = make_argument_parser()
args = parser.parse_args()
input_dir = args.inputdir
model_dir = args.modeldir
bed_file = args.bed
chrom = args.chrom
if args.outputdir is None:
clobber = True
output_dir = args.outputdirc
else:
clobber = False
output_dir = args.outputdir
try: # adapted from dreme.py by T. Bailey
os.makedirs(output_dir)
except OSError as exc:
if exc.errno == errno.EEXIST:
if not clobber:
print >> sys.stderr, ('output directory (%s) already exists '
'but you specified not to clobber it') % output_dir
sys.exit(1)
else:
print >> sys.stderr, ('output directory (%s) already exists '
'so it will be clobbered') % output_dir
print 'Loading genome'
genome = utils.load_genome()
print 'Loading model'
model_tfs, model_bigwig_names, features, model = utils.load_model(model_dir)
L = model.input_shape[0][1]
utils.L = L
use_meta = 'meta' in features
use_gencode = 'gencode' in features
print 'Loading BED data'
is_sorted = False
bigwig_names, meta_names, datagen_bed, nonblacklist_bools = utils.load_beddata(genome, bed_file, use_meta, use_gencode, input_dir, is_sorted, chrom)
assert bigwig_names == model_bigwig_names
if use_meta:
model_meta_file = model_dir + '/meta.txt'
assert os.path.isfile(model_meta_file)
model_meta_names = np.loadtxt(model_meta_file, dtype=str)
if len(model_meta_names.shape) == 0:
model_meta_names = [str(model_meta_names)]
else:
model_meta_names = list(model_meta_names)
assert meta_names == model_meta_names
output_results(bigwig_names, datagen_bed, model, output_dir)
if __name__ == '__main__':
"""
See module-level docstring for a description of the script.
"""
main()