[6d0c6b]: / temporal_output_values.lua

Download this file

193 lines (151 with data), 6.1 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
require 'torch'
require 'nn'
require 'optim'
require 'model'
include('util/auRoc.lua')
require 'lfs'
local cmd = torch.CmdLine()
-- GPU
cmd:option('-gpu', 1) -- set to 0 if no GPU
-- Dataset options
cmd:option('-data_root', 'data') -- data root directory
cmd:option('-dataset', 'deepbind') -- dataset
cmd:option('-seq_length', 101) --length of DNA sequences
cmd:option('-TF', 'ATF1_K562_ATF1_-06-325-_Harvard') -- change for different TF
cmd:option('-alphabet', 'ACGT')
cmd:option('-size', 0) -- how much of each dataset to load. 0 = full
cmd:option('-batch_size', 1)
cmd:option('class_labels','1,0') --specify positive label first
local opt = cmd:parse(arg)
opt.class_labels_table = opt.class_labels:split(',')
opt.num_classes = #opt.class_labels_table
opt.alphabet_size = #opt.alphabet
local data_dir = opt.data_root..'/'..opt.dataset..'/'
-- Set up GPU stuff
local dtype = 'torch.FloatTensor'
if opt.gpu > 0 then
collectgarbage()
require 'cutorch'
require 'cunn'
cutorch.setDevice(opt.gpu )
dtype = 'torch.CudaTensor'
print(string.format('Running with CUDA on GPU %d', opt.gpu))
else
print 'Running in CPU mode'
end
local data_dir = opt.data_root..'/'..opt.dataset..'/'
opt.TF = TF or opt.TF
opt.data_dir = data_dir..opt.TF
-- specify directories
model_root = 'models'
data_root = 'data/deepbind/'
viz_dir = 'visualization_results/'
-- ****************************************************************** --
-- ****************** CHANGE THESE FIELDS *************************** --
TFs = {'ATF1_K562_ATF1_-06-325-_Harvard'}
rnn_model = 'model=RNN,rnn_size=32,rnn_layers=1,dropout=0.5,learning_rate=0.01,batch_size=256'
cnnrnn_model = 'model=CNN-RNN,cnn_size=128,cnn_filter=9,rnn_size=32,rnn_layers=1,dropout=0.5,learning_rate=0.01,batch_size=256'
model_names = {rnn_model,cnnrnn_model} --add or remove to this
-- which sequences in the test set to show temporal outputs for
start_seq = 1
end_seq = start_seq + 0
-- ****************************************************************** --
-- ****************************************************************** --
alphabet = opt.alphabet
rev_dictionary = {}
dictionary = {}
for i = 1,#alphabet do
rev_dictionary[i] = alphabet:sub(i,i)
dictionary[alphabet:sub(i,i)] = i
end
OneHot = OneHot(#alphabet):type(dtype)
crit = nn.ClassNLLCriterion():type(dtype)
for _,TF in pairs(TFs) do
print(TF)
save_path = viz_dir..TF..'/'
os.execute('mkdir '..save_path..' > /dev/null 2>&1')
-- os.execute('rm '..save_path..'/*.csv > /dev/null 2>&1')
-- os.execute('rm '..save_path..'*.png > /dev/null 2>&1')
data_dir = data_root..TF
opt.data_dir = data_dir
require('data')
data = {}
test_seqs = createDatasetOneHot("test", opt)
-- Load Models into models table
models = {}
for _,model_name in pairs(model_names) do
print()
load_path = model_root..'/'..model_name..'/'..TF..'/'
model = torch.load(load_path..'best_model.t7')
model.model:remove(1)
model:evaluate()
model.model:type(dtype)
models[model_name] = model
end
for t = start_seq,end_seq do
x = test_seqs.inputs[t]:type(dtype)
X = OneHot:forward(x)
y = test_seqs.labels[t]:type(dtype)
--####################### CREATE SEQ LOGO ###############################--
s2l_filename = save_path..'sequence_'..t..'.txt'
f = io.open(s2l_filename, 'w')
print(s2l_filename)
f:write('PO ')
alphabet:gsub(".",function(c) f:write(tostring(c)..' ') end)
f:write('\n')
for i=1,X[1]:size(1) do
f:write(tostring(i)..' ')
for j=1,X[1]:size(2) do
f:write(tostring(X[1][i][j])..' ')
end
f:write('\n')
end
f:close()
cmd = "weblogo -D transfac -F png -o "..save_path.."sequence_"..t..".png --errorbars NO --show-xaxis NO --show-yaxis NO -A dna --composition none -n 101 --color '#00CC00' 'A' 'A' --color '#0000CC' 'C' 'C' --color '#FFB300' 'G' 'G' --color '#CC0000' 'T' 'T' < "..s2l_filename
os.execute(cmd)
--####################### TEMPORAL OUTPUT ###############################--
for model_name, model in pairs(models) do
print('***** '..model_name..' *****')
out_file_fwd = io.open(save_path..model_name..'_output_values_fwd_'..t..'.csv', 'w')
out_file_bwd = io.open(save_path..model_name..'_output_values_bwd_'..t..'.csv', 'w')
-- need to get CNN output column vectors to be fed into RNN output
if string.match(model.model:__tostring__(),'Convolution') then
CNN = model.model:get(1)
model.model:remove(1)
X_in = CNN:forward(X)
else
X_in = X
end
-- FORWARD
for i = 1,X_in:size(2) do
model:resetStates()
model:zeroGradParameters()
output = model:forward(X_in[{{1,1},{1,i}}])
pos_sent_value = torch.exp(output[1])[1]
out_file_fwd:write(rev_dictionary[x[1][i]]..',')
out_file_fwd:write(pos_sent_value..',\n')
end
-- REVERSE
for i = 1,X_in:size(2) do
model:resetStates()
model:zeroGradParameters()
output = model:forward(X_in[{{1,1},{i,X_in:size(2)}}])
pos_sent_value = torch.exp(output[1])[1]
out_file_bwd:write(rev_dictionary[x[1][i]]..',')
out_file_bwd:write(pos_sent_value..',\n')
end
out_file_fwd:write('\n')
out_file_bwd:write('\n')
out_file_fwd:close()
out_file_bwd:close()
cmd = 'Rscript ./heatmap_scripts/heatmap_temporal.R '..save_path..model_name..'_output_values_fwd_'..t..'.csv '..save_path..model_name..'_output_values_fwd_'..t..'.png -20'
os.execute(cmd..' > /dev/null 2>&1')
cmd = 'Rscript ./heatmap_scripts/heatmap_temporal.R '..save_path..model_name..'_output_values_bwd_'..t..'.csv '..save_path..model_name..'_output_values_bwd_'..t..'.png -20'
os.execute(cmd..' > /dev/null 2>&1')
end -- model in models
end -- test sequences
print('')
print(lfs.currentdir()..'/'..save_path)
os.execute('rm '..save_path..'/*.csv > /dev/null 2>&1')
os.execute('rm '..save_path..'/*.txt > /dev/null 2>&1')
end -- TFs