-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·207 lines (184 loc) · 10.1 KB
/
Copy pathmain.py
File metadata and controls
executable file
·207 lines (184 loc) · 10.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
from loguru import logger
import os
import argparse
import importlib
import random
import numpy as np
from utils import MutGenerator, GetSummaryStatisticsCallback, write_splice_site_file_header, write_splice_sites, get_top1_statistics, get_correlation, collect_predictions, density_scatter
import torch
from torch.utils.data import DataLoader
import copy
def main():
# load config file
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--config", help="the path to the config file")
args = parser.parse_args()
config_path = args.config.replace("/", ".")
config = importlib.import_module(config_path)
config = config.config
logger.add(config.log_path)
# set seeds
os.environ["PYTHONHASHSEED"] = str(1+config.seed)
os.environ["TF_DETERMINISTIC_OPS"] = "1"
random.seed(1+config.seed)
np.random.seed(1+config.seed)
torch.random.manual_seed(1+config.seed)
print("finish set env")
from utils import DataGenerator
if config.is_train:
# prepare model
train_data = DataLoader(DataGenerator(EL=config.EL, jsonfile=config.trainjsonfile),
shuffle=True, batch_size=config.batch_size, drop_last=True)
validation_data = DataLoader(DataGenerator(EL=config.EL, jsonfile=config.validjsonfile, ), shuffle=False,
batch_size=config.batch_size, drop_last=True) # only test on human data
if hasattr(config, "mut_data") and config.mut_data is not None:
mut_data = DataLoader(MutGenerator(
jsonfile=config.mut_data, EL=config.EL,
), batch_size=1, shuffle=False)
else:
mut_data = None
test_data = [] # in train mode, do not consider test data
summary = GetSummaryStatisticsCallback(
config.model,
train_data, validation_data, test_data=test_data, mut_data=mut_data,
summary_fout_path=os.path.join(config.save_path, "summary_log"),
model_save_path=os.path.join(config.save_path, "models")
)
logger.info("Finish loading data ...")
logger.info(
"Train data size {} validation data size {} test data size {}".format(len(train_data), len(validation_data),
len(test_data)))
summary.fit(config.epoch_num)
logger.info("Finish training")
return
else:
# load model
assert config.model_path is not None
assert config.testjsonfile is not None or config.mut_data is not None
if config.testjsonfile is None and config.mut_data is not None:
config.testjsonfile = config.mut_data
DataGenerator = MutGenerator
else:
from utils import DataGenerator
if not isinstance(config.testjsonfile, list):
config.testjsonfile = [config.testjsonfile]
if not isinstance(config.model_path, list):
config.model_path = [config.model_path]
save_dir = os.path.join(config.save_path, "test_results/")
if not os.path.exists(save_dir):
os.mkdir(save_dir)
Models = [copy.deepcopy(config.model) for _ in config.model_path]
[m.load_state_dict(torch.load(b))
for m, b in zip(Models, config.model_path)]
for path in config.testjsonfile:
save_path_ = "{}+{}".format(path.replace("/", "_"),
"eval"+config.model_path[0].replace("/", "_"))
save_path_ = os.path.join(save_dir, save_path_)
save_path = []
test_data = DataLoader(DataGenerator(
EL=config.EL, jsonfile=path), batch_size=config.batch_size, shuffle=False, num_workers=5)
if config.save_files:
Y_true_1 = []
Y_true_2 = []
Y_pred_1 = []
Y_pred_2 = []
save_path.append(open(save_path_+"_acceptor", "w"))
save_path.append(open(save_path_+"_donor", "w"))
[write_splice_site_file_header(_) for _ in save_path]
for index, d in enumerate(test_data):
if index % 100 == 0:
print(index)
X, Y = d["X"], d["single_pred_psi"].numpy()
if "mutX" in d:
d.pop("mutX")
pred = sum([m.predict(d)["single_pred_psi"]
for m in Models])/len(Models)
if len(Y.shape) == 4:
Y = Y[:, :, 0]
assert len(Y.shape) == len(pred.shape)
assert len(pred.shape) == 3
ly = Y.shape[1]
bias = pred.shape[1]-ly
assert bias % 2 == 0
pred = pred[:, bias//2:bias//2+ly]
assert Y.shape == pred.shape
# note that tx_start/tx_end refer to the start/end of input sequence instead of gene start/end
CHROM = d["chrom"]
NAME = d["name"]
STRAND = d["strand"]
TX_START = d["txstart"].numpy()
TX_END = d["txend"].numpy()
SPECIES = d["species"]
write_splice_sites(save_path, CHROM, NAME, STRAND,
TX_START, TX_END, SPECIES, pred, Y)
pred_sites = np.nonzero(Y[:, :, 1:].sum(-1))
Y_true_1.extend(Y[pred_sites][:, 1].flatten())
Y_true_2.extend(Y[pred_sites][:, 2].flatten())
Y_pred_1.extend(pred[pred_sites][:, 1].flatten())
Y_pred_2.extend(pred[pred_sites][:, 2].flatten())
[_.close() for _ in save_path]
topk1_acc_a, auprc_a, threshold_a, num_idx_true_a = get_top1_statistics(
np.asarray(Y_true_1), np.asarray(Y_pred_1)
)
logger.info("File {} Model {} Acceptor: topk1_acc {} auprc {} threshold {} num_idx_true {}".format(
path, config.model_path[0], topk1_acc_a, auprc_a, threshold_a, num_idx_true_a
))
rho_a, spa, num_idx_true_a2 = get_correlation(
np.asarray(Y_true_1), np.asarray(Y_pred_1)
)
logger.info("Acceptor correlation: rho {} pearson {} num_idx_true {}".format(
rho_a, spa, num_idx_true_a2
))
topk1_acc_d, auprc_d, threshold_d, num_idx_true_d = get_top1_statistics(
np.asarray(Y_true_2), np.asarray(Y_pred_2)
)
logger.info("File {} Model{} Donor: topk1_acc {} auprc {} threshold {} num_idx_true {}".format(
path, config.model_path[0], topk1_acc_d, auprc_d, threshold_d, num_idx_true_d
))
rho_d, spd, num_idx_true_d2 = get_correlation(
np.asarray(Y_true_2), np.asarray(Y_pred_2)
)
logger.info("Donor correlation: rho {} pearson {} num_idx_true {}".format(
rho_d, spd, num_idx_true_d2
))
y_true1, y_true2 = np.copy(Y_true_1), np.copy(Y_true_2)
y_true1[np.isnan(y_true1)] = -1
y_true2[np.isnan(y_true2)] = -1
idx_true1, idx_true2 = np.nonzero(
y_true1 > 0+1e-10), np.nonzero(y_true2 > 0+1e-10)
print(len(idx_true1[0]), len(idx_true2[0]))
density_scatter(np.asarray(Y_true_1)[idx_true1], np.asarray(Y_pred_1)[idx_true1], "GT", "Pred", os.path.join(
save_dir, "Acceptor_{}.png".format("{}+{}".format(path.replace("/", "_"), config.model_path[0].replace("/", "_")))))
density_scatter(np.asarray(Y_true_2)[idx_true2], np.asarray(Y_pred_2)[idx_true2], "GT", "Pred", os.path.join(
save_dir, "Donor_{}.png".format("{}+{}".format(path.replace("/", "_"), config.model_path[0].replace("/", "_")))))
logger.info("finish {}".format(path))
elif config.print_summary:
Pred = [collect_predictions(m, test_data) for m in Models]
Y_true_1, Y_true_2, Y_pred_1, Y_pred_2 = sum([p[0] for p in Pred])/len(Pred), sum([p[1] for p in Pred])/len(Pred), sum([p[2]
for p in Pred])/len(Pred), sum([p[3] for p in Pred])/len(Pred)
topk1_acc_a, auprc_a, threshold_a, num_idx_true_a = get_top1_statistics(
np.asarray(Y_true_1), np.asarray(Y_pred_1)
)
logger.info("File {} Model {} Acceptor: topk1_acc {} auprc {} threshold {} num_idx_true {}".format(
path, config.model_path[0], topk1_acc_a, auprc_a, threshold_a, num_idx_true_a
))
rho_a, spa, num_idx_true_a2 = get_correlation(
np.asarray(Y_true_1), np.asarray(Y_pred_1)
)
logger.info("Acceptor correlation: rho {} pearson {} num_idx_true {}".format(
rho_a, spa, num_idx_true_a2
))
topk1_acc_d, auprc_d, threshold_d, num_idx_true_d = get_top1_statistics(
np.asarray(Y_true_2), np.asarray(Y_pred_2)
)
logger.info("File {} Model{} Donor: topk1_acc {} auprc {} threshold {} num_idx_true {}".format(
path, config.model_path[0], topk1_acc_d, auprc_d, threshold_d, num_idx_true_d
))
rho_d, spd, num_idx_true_d2 = get_correlation(
np.asarray(Y_true_2), np.asarray(Y_pred_2)
)
logger.info("Donor correlation: rho {} pearson {} num_idx_true {}".format(
rho_d, spd, num_idx_true_d2
))
if __name__ == "__main__":
main()