-
Notifications
You must be signed in to change notification settings - Fork 0
/
d4_predict.py
executable file
·352 lines (329 loc) · 11.5 KB
/
d4_predict.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tensorflow import keras
from scipy.stats import pearsonr, spearmanr
from d4_generation import data_generator_vals, DataGenerator
from d4_interactions import (
atom_interaction_matrix_d,
check_structure,
model_interactions,
)
from d4_utils import (
protein_settings,
hydrophobicity,
h_bonding,
sasa,
charge,
side_chain_length,
aa_dict,
read_blosum,
aa_dict_pos,
)
from d4_split import read_split_file
from d4_argpars import predict_dict
def predict_score(
protein_pdb: str,
protein_seq: list[str],
variant_s: list[str],
model_filepath: str,
dist_th: int | float,
algn_path: str | None = None,
algn_base: str | None = None,
batch_size: int = 32,
first_ind: int = 0,
) -> np.ndarray[tuple[int], np.dtype[float]]:
"""predicts scores of variants with provided trained model
:parameter
- protein_pdb:
filepath to the proteins pdb file containing its structure
- protein_seq:
amino acid sequence of the protein ['A', 'V', 'L', 'I']
- variant_s:
variants for which the score should be predicted e.g.
['A1S', 'K3F,I9L']
- model_filepath:
file path to the trained model that should be loaded
to be used for the predictions
- dist_th:
distance threshold used when training the model
- algn_path:
path to the multiple sequence alignment in clustalw format
- algn_base:
name of the wild type sequence in the alignment file
- first_ind:
index of the start of the protein sequence
- batch_size:
how many variants get predicted at once
- first_ind:
offset of the start of the sequence
(when sequence doesn't start with residue 0)
:return
- pred:
predicted scores for the variants"""
# values needed as input for the DataGenerator
(
hm_pos_vals,
hp_norm,
ia_norm,
hm_converted,
hp_converted,
cm_converted,
ia_converted,
mat_index,
cl_converted,
cl_norm,
co_converted,
co_table,
co_rows,
) = data_generator_vals(protein_seq, algn_path, algn_base)
# set number of channels based on presence of the alignment file
channel_num = 7
if algn_path is None:
channel_num = 6
dist_m, factor, comb_bool = atom_interaction_matrix_d(protein_pdb, dist_th)
# checks whether sequence in structure as wt_seq match
check_structure(protein_pdb, comb_bool, protein_seq, silent=True)
# DataGenerator parameters
params = {
"interaction_matrix": comb_bool,
"dim": comb_bool.shape,
"n_channels": channel_num,
"batch_size": batch_size,
"first_ind": first_ind,
"hm_converted": hm_converted,
"hm_pos_vals": hm_pos_vals,
"factor": factor,
"hp_converted": hp_converted,
"hp_norm": hp_norm,
"cm_converted": cm_converted,
"ia_converted": ia_converted,
"ia_norm": ia_norm,
"mat_index": mat_index,
"cl_converted": cl_converted,
"cl_norm": cl_norm,
"dist_mat": dist_m,
"dist_th": dist_th,
"co_converted": co_converted,
"co_table": co_table,
"co_rows": co_rows,
"shuffle": False,
"train": False,
}
# loading the model
model = keras.models.load_model(model_filepath)
# use DataGenerator and model.predict only when more than 64 variants should
# be predicted
if len(variant_s) <= 64:
pred = []
for i in variant_s:
pred.append(
float(
model(
np.asarray(
[
model_interactions(
feature_to_encode=i,
interaction_matrix=comb_bool,
index_matrix=mat_index,
factor_matrix=factor,
distance_matrix=dist_m,
dist_thrh=dist_th,
first_ind=first_ind,
hmc=hm_converted,
hb=h_bonding,
hm_pv=hm_pos_vals,
hpc=hp_converted,
hp=hydrophobicity,
hpn=hp_norm,
cmc=cm_converted,
c=charge,
iac=ia_converted,
sa=sasa,
ian=ia_norm,
clc=cl_converted,
scl=side_chain_length,
cln=cl_norm,
coc=co_converted,
cp=aa_dict_pos,
cot=co_table,
cor=co_rows,
)
]
),
training=False,
)
)
)
pred = np.asarray(pred)
else:
generator = DataGenerator(variant_s, np.zeros(len(variant_s)), **params)
pred = np.asarray(model.predict(generator).flatten())
if len(pred) == 1:
pred = float(pred[0])
# predicted score(s)
return pred
def assess_performance(
ground_truth: np.ndarray[tuple[int], np.dtype[int | float]],
predicted_score: np.ndarray[tuple[int], np.dtype[int | float]],
scatter_plot: bool = False,
):
"""calculates the error and correlation of predictions made by a trained model
:parameter
- ground_truth: ndarray or ints or floats
true experiamentaly determined fitness scores
- predicted_score: ndarray of ints or floats
scores predicted by the neural network
:return
- None
"""
sr, sp = spearmanr(ground_truth, predicted_score)
pr, pp = pearsonr(ground_truth, predicted_score)
mse = np.mean((ground_truth - predicted_score) ** 2)
mae = np.mean(np.abs(ground_truth - predicted_score))
print(
"Pearson's R: {:0.4f} ({:0.4f})\n"
"Spearman R: {:0.4f} ({:0.4f})\n"
"MeanAbsoluteError: {:0.4f}\n"
"MeanSquarredError: {:0.4f}".format(pr, pp, sr, sp, mae, mse)
)
if scatter_plot:
plt.scatter(ground_truth, predict_score, color="forestgreen")
plt.xlabel("ground truth")
plt.ylabel("predicted score")
plt.show()
def recall_calc(
protein: str,
test_var_inds_file: str,
model_filepath: str,
steps: int = 100,
test_size: int | None = None,
N: int = 100,
):
"""calculates the recall percentage for a given network and protein dataset
:parameter
- protein:
name of the protein
- test_var_inds_file:
file path to the test.txt file that contains the indices of samples
for the nononsense_PROTEIN.tsv
- model_filepath:
file path to the trained model_filepath
- steps:
number of steps in range function that should test the
recall percentage
- test_size:
max number of samples in one prediction
- N:
number of top samples (budget)
:return
- num
list with sample sizes per data point
- recall_perc
percentage of samples in the top N predictions
"""
ps = protein_settings(protein)
dms_data = pd.read_csv(f"./nononsense/nononsense_{protein}.tsv", delimiter="\t")
tvi = read_split_file(test_var_inds_file)
dms_variants = np.asarray(dms_data[ps["variants"]])[tvi]
dms_scores = np.asarray(dms_data[ps["score"]])[tvi]
# predict the scores of the test data
score = predict_score(
f"./datasets/{protein}.pdb",
list(ps["sequence"]),
dms_variants,
model_filepath,
20,
f"./datasets/alignment_files/{protein}_1000_experimental.clustal",
protein,
first_ind=int(ps["offset"]),
)
# sort the scores of the true data for the best variants
top_n_ground_trouth = dms_variants[np.argsort(dms_scores)[::-1]][:N]
# variants sorted by prediction
pred_sort = dms_variants[np.argsort(score)[::-1]]
# variants randomly 'sorted'
random_ind = np.arange(len(dms_scores))
np.random.shuffle(random_ind)
random_sort = dms_variants[random_ind]
# calculates for different budgets how many of the predictions are truly in the
# top N
recall_perc = []
num = []
best_case = []
random_case = []
if test_size is None:
test_size = len(tvi) + 1
for i in range(10, test_size, steps):
predicted_top_n = pred_sort[:i]
random_top_n = random_sort[:i]
# percentage of correctly recalls in the top N
recall_perc.append(np.sum(np.isin(predicted_top_n, top_n_ground_trouth)) / N)
# best case percentage
best_calc = i / N
if best_calc > 1.0:
best_calc = 1.0
best_case.append(best_calc)
# random choices percentage
random_case.append(np.sum(np.isin(random_top_n, top_n_ground_trouth)) / N)
num.append(i)
return num, recall_perc, random_case, best_case
if __name__ == "__main__":
protein = "pab1"
ps = protein_settings(protein)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# calculate the models performance
"""
dms_data = pd.read_csv(
f"nononsense/nononsense_{protein}.tsv", delimiter="\t"
)
dms_variants = np.asarray(dms_data[ps["variants"]])[:2000]
dms_scores = np.asarray(dms_data[ps["score"]])[:2000]
score = predict_score(
f"./datasets/{protein}.pdb",
list(ps["sequence"]),
dms_variants,
"./pub_result_files/saved_models/recall_whole_ds/"
"nononsense_pab1_04_11_2022_094109/",
20,
f"./datasets/alignment_files/{protein}_1000_experimental.clustal",
protein,
first_ind=int(ps["offset"]),
)
assess_performance(dms_scores, score)
"""
# ------------------------------------------------------------------------
# accessing predicted scores
"""
voi = ["A128K", "R145L,K160T"]
score = predict_score(
protein_pdb=f"./datasets/{protein}.pdb",
protein_seq=list(ps["sequence"]),
variant_s=voi,
model_filepath="./result_files/saved_models/pab1_fr_50_27_08_2022_100124/",
dist_th=20,
algn_path=f"./datasets/alignment_files/{protein}_1000_experimental.clustal",
algn_base=protein,
first_ind=int(ps["offset"]),
)
for i, j in zip(voi, score):
print(f"{i}: {j}")
"""
# ------------------------------------------------------------------------
# calculate recall percentage
"""
print(recall_calc(
"gb1",
"result_files/rr5/recall/recall_fract_splits/dense_net2/"
"nononsense_gb1_28_09_2022_142206_splits0/test.txt",
"result_files/saved_models/recall_fract_ds/dense_net2/"
"nononsense_gb1_28_09_2022_142206/",
))
"""
param_dict = predict_dict()
predictions = predict_score(**param_dict)
print("variant_prediction")
for i, j in zip(param_dict["variant_s"], predictions):
print(f"{i}_{j}")