-
Notifications
You must be signed in to change notification settings - Fork 1
/
RunBPNetArchs.py
171 lines (139 loc) · 7.5 KB
/
RunBPNetArchs.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
import seqdataloader
from seqdataloader.batchproducers import coordbased
from seqdataloader.batchproducers.coordbased import coordbatchproducers
from seqdataloader.batchproducers.coordbased import coordstovals
from seqdataloader.batchproducers.coordbased import coordbatchproducers
from seqdataloader.batchproducers.coordbased import coordbatchtransformers
from seqdataloader.batchproducers.coordbased.coordbatchproducers import SimpleCoordsBatchProducer
from seqdataloader.batchproducers.coordbased.coordstovals.bigwig import AbstractCountAndProfileTransformer
from seqdataloader.batchproducers.coordbased.coordstovals.bigwig import LogCountsPlusOne
from seqdataloader.batchproducers.coordbased.coordstovals.bigwig import SmoothProfiles
from seqdataloader.batchproducers.coordbased.coordstovals.bigwig import BigWigReader
from seqdataloader.batchproducers.coordbased.coordstovals.bigwig import smooth_profiles
from seqdataloader.batchproducers.coordbased.coordstovals.bigwig import rolling_window
from seqdataloader.batchproducers.coordbased.coordbatchtransformers import AbstractCoordBatchTransformer
from seqdataloader.batchproducers.coordbased.coordbatchtransformers import get_revcomp
from seqdataloader.batchproducers.coordbased.core import Coordinates, KerasBatchGenerator, apply_mask
import keras
from keras import backend as K
import keras.layers as kl
from keras.engine import Layer
from keras.engine.base_layer import InputSpec
from keras.callbacks import History
import keras_genomics
from keras_genomics.layers.convolutional import RevCompConv1D
import tensorflow as tf
import numpy as np
import os
def get_inputs_and_targets(dataset, seq_len, out_pred_len):
inputs_coordstovals = coordstovals.core.CoordsToValsJoiner(
coordstovals_list=[
coordbased.coordstovals.fasta.PyfaidxCoordsToVals(
genome_fasta_path="mm10_no_alt_analysis_set_ENCODE.fasta",
mode_name="sequence",
center_size_to_use=seq_len),
coordstovals.bigwig.PosAndNegSmoothWindowCollapsedLogCounts(
pos_strand_bigwig_path="counts.pos.bw",
neg_strand_bigwig_path="counts.neg.bw",
counts_mode_name="patchcap.logcount",
profile_mode_name="patchcap.profile",
center_size_to_use=out_pred_len,
smoothing_windows=[1,50])])
targets_coordstovals = coordstovals.bigwig.PosAndNegSeparateLogCounts(
counts_mode_name="CHIPNexus.%s.logcount" % dataset,
profile_mode_name="CHIPNexus.%s.profile" % dataset,
pos_strand_bigwig_path="counts.pos.bw",
neg_strand_bigwig_path="counts.neg.bw",
center_size_to_use=out_pred_len)
return inputs_coordstovals, targets_coordstovals
def get_train_generator(PARAMETERS, inputs_coordstovals, targets_coordstovals, model_arch):
train_file = "%s_train_1k_around_summits.bed.gz" % PARAMETERS['dataset']
chromsizes_file="mm10.chrom.sizes"
if model_arch == "Standard-RCAug":
print("Aug")
train_batch_generator = KerasBatchGenerator(
coordsbatch_producer=coordbatchproducers.SimpleCoordsBatchProducer(
bed_file=train_file,
batch_size=64,
shuffle_before_epoch=True,
seed=PARAMETERS['seed']),
inputs_coordstovals=inputs_coordstovals,
targets_coordstovals=targets_coordstovals,
coordsbatch_transformer=coordbatchtransformers.ReverseComplementAugmenter().chain(
coordbatchtransformers.UniformJitter(
maxshift=200, chromsizes_file=chromsizes_file)))
else:
train_batch_generator = KerasBatchGenerator(
coordsbatch_producer=coordbatchproducers.SimpleCoordsBatchProducer(
bed_file = train_file,
batch_size=64,
shuffle_before_epoch=True,
seed=PARAMETERS['seed']),
inputs_coordstovals=inputs_coordstovals,
targets_coordstovals=targets_coordstovals,
coordsbatch_transformer=coordbatchtransformers.UniformJitter(
maxshift=200, chromsizes_file=chromsizes_file))
return train_batch_generator
def get_val_generator(PARAMETERS, inputs_coordstovals, targets_coordstovals):
valid_file = "%s_valid_1k_around_summits.bed.gz" % PARAMETERS['dataset']
val_batch_generator = KerasBatchGenerator(
coordsbatch_producer=coordbatchproducers.SimpleCoordsBatchProducer(
bed_file = valid_file,
batch_size=64,
shuffle_before_epoch=False,
seed=PARAMETERS['seed']),
inputs_coordstovals=inputs_coordstovals,
targets_coordstovals=targets_coordstovals)
return val_batch_generator
class GeneralReverseComplement(AbstractCoordBatchTransformer):
def __call__(self, coords):
return [get_revcomp(x) for x in coords]
def get_test_generator(PARAMETERS, inputs_coordstovals, targets_coordstovals):
test_file = "%s_test_1k_around_summits.bed.gz" % PARAMETERS['dataset']
chromsizes_file="mm10.chrom.sizes"
keras_test_batch_generator = KerasBatchGenerator(
coordsbatch_producer=coordbatchproducers.SimpleCoordsBatchProducer(
bed_file = test_file,
batch_size=64,
shuffle_before_epoch=False,
seed=PARAMETERS['seed']),
inputs_coordstovals=inputs_coordstovals,
targets_coordstovals=targets_coordstovals)
keras_rc_test_batch_generator = KerasBatchGenerator(
coordsbatch_producer=coordbatchproducers.SimpleCoordsBatchProducer(
bed_file=test_file,
batch_size=64,
shuffle_before_epoch=False,
seed=PARAMETERS['seed']),
inputs_coordstovals=inputs_coordstovals,
targets_coordstovals=targets_coordstovals,
coordsbatch_transformer=GeneralReverseComplement())
return keras_test_batch_generator, keras_rc_test_batch_generator
def save_results(PARAMETERS, model, model_arch, model_history):
txt_file_name = ("%s.txt" % (model_arch))
loss_file = open(txt_file_name, "w")
loss_file.write("model parameters" + "\n")
for x in PARAMETERS:
loss_file.write(str(x) + ": " + str(PARAMETERS[x]) + "\n")
loss_file.write("val_loss\n")
for row in model_history.history["val_loss"]:
loss_file.write(str(row) + "\n")
loss_file.write("min val loss: " + str(np.min(model_history.history["val_loss"])))
loss_file.close()
if PARAMETERS['filters'] == 32:
model_save_name = ("%s-half.h5" % (model_arch))
else:
model_save_name = ("%s.h5" % (model_arch))
model.save(model_save_name)
def train_model(PARAMETERS, inputs_coordstovals, targets_coordstovals, epochs_to_train_for, model, model_arch):
train_batch_generator = get_train_generator(PARAMETERS, inputs_coordstovals, targets_coordstovals, model_arch)
val_batch_generator = get_val_generator(PARAMETERS, inputs_coordstovals, targets_coordstovals)
early_stopping_callback = keras.callbacks.EarlyStopping(
patience=10, restore_best_weights=True)
model_history = History()
model.fit_generator(train_batch_generator,
epochs = epochs_to_train_for,
validation_data=val_batch_generator,
callbacks=[early_stopping_callback, model_history])
model.set_weights(early_stopping_callback.best_weights)
save_results(PARAMETERS, model, model_arch, model_history)