-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphenotype_predict_DmpR.py
268 lines (222 loc) · 9.68 KB
/
phenotype_predict_DmpR.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
#%%
import Bio.SeqIO
from Bio.SeqRecord import SeqRecord
from Bio.Seq import Seq
import numpy as np
import pandas as pd
import tensorflow.keras.backend as K
from tensorflow.keras.layers import Conv1D, Dense, MaxPooling1D, Flatten
from tensorflow.keras.models import Sequential
from tensorflow import keras as k
from keras_preprocessing.sequence import pad_sequences
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
#%%
def mcc(label_real, label_predicted):
"""Custom Metric for Accuracy and Precision
Parameters:
----------
label_real: tensorflow.Tensor
Correct labels
label_predicted: tensorflow.Tensor
Predicted labels
Returns
-------
float
Custom metric
"""
true_pos = K.sum(K.round(K.clip(label_real * label_predicted, 0, 1)))
true_neg = K.sum(K.round(K.clip((1 - label_real) * (1 - label_predicted), 0, 1)))
false_pos = K.sum(K.round(K.clip((1 - label_real) * label_predicted, 0, 1)))
false_neg = K.sum(K.round(K.clip(label_real * (1 - label_predicted), 0, 1)))
number = true_pos * true_neg - false_pos * false_neg
denominator = K.sqrt((true_pos + false_pos) *
(true_pos + false_neg) *
(true_neg + false_pos) *
(true_neg + false_neg))
return number / (denominator + K.epsilon())
def one_hot_encoding_aa(dataset):
"""One hot encoding of Amino Acid sequences
Parameters:
----------
dataset: List[str]
List of Amino Acid sequences
Returns
-------
numpy.ndarray
One Hot Encoding of the amino acids (has dimensions dataset_size x seq_len x 21)
"""
integer_encoder = LabelEncoder()
one_hot_encoder = OneHotEncoder(categories='auto')
amino_acids = "ARNDCQEGHILKMFPSTWYV*"
input_features = []
# fix the encoded categories
ie = integer_encoder.fit_transform(list(amino_acids)) #.toarray().reshape(-1, 1)
ie = np.array(ie).reshape(-1, 1)
oe = one_hot_encoder.fit_transform(ie)
for sequence in dataset:
if type(sequence) == str:
integer_encoded = integer_encoder.transform(list(sequence)) #.toarray().reshape(-1, 1)
integer_encoded = np.array(integer_encoded).reshape(-1,1)
one_hot_encoded = one_hot_encoder.transform(integer_encoded)
input_features.append(one_hot_encoded.toarray())
input_features = pad_sequences(input_features, padding="post")
input_features = np.stack(input_features)
return input_features
def one_hot_encoding(dataset, maxlen):
"""One hot encoding of DNA sequences
Parameters:
----------
dataset: List[str]
List of DNA sequences
maxlen: int
Max length of padded sequences
Returns
-------
numpy.ndarray
One Hot Encoding of the DNA sequences (has dimensions dataset_size x maxlen x 4)
"""
integer_encoder = LabelEncoder()
one_hot_encoder = OneHotEncoder(categories='auto')
input_features = []
for sequence in dataset:
if type(sequence) == str:
integer_encoded = integer_encoder.fit_transform(list(sequence)) #.toarray().reshape(-1, 1)
integer_encoded = np.array(integer_encoded).reshape(-1,1)
one_hot_encoded = one_hot_encoder.fit_transform(integer_encoded)
input_features.append(one_hot_encoded.toarray())
input_features = pad_sequences(input_features, padding="post", maxlen=maxlen)
input_features = np.stack(input_features)
return input_features
candidates = []
description = []
for record in Bio.SeqIO.parse("Insilico/function_test_4-5_aa.fasta", "fasta"):
candidat = str(record.seq)
candidates.append(candidat)
description.append(record.description)
candidate = one_hot_encoding(candidates, 1784)
def cnn_1d(train_features):
model = Sequential()
model.add(Conv1D(filters=49, kernel_size=3, input_shape=(train_features, 4)))
model.add(MaxPooling1D(pool_size=4))
model.add(Conv1D(filters=49, kernel_size=3, input_shape=(train_features, 4)))
model.add(MaxPooling1D(pool_size=4))
# model.add(Flatten())
model.add(Conv1D(filters=49, kernel_size=3, input_shape=(train_features, 4)))
model.add(MaxPooling1D(pool_size=4))
#model.add(Conv1D(filters=400, kernel_size=3, input_shape=(train_features, 4)))
#model.add(MaxPooling1D(pool_size=4))
model.add(Flatten())
model.add(Dense(16, activation='elu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='sgd', metrics=['binary_accuracy', mcc])
# if you need to convert the model to a multi-class classification model
#model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['categorical_accuracy'])
model.summary()
return model
dependencies = {
'mcc': mcc
}
#signal_model = keras.models.load_model('CNN_aa/trained_models/mcc_BC_opt_aaprop_signal_v0.1.h5', compile=False)
#bkg_model = keras.models.load_model('CNN_aa/trained_models/mcc_BC_opt_aaprop_bkg_v0.2.h5', compile=False)
#signal_model = keras.models.load_model('CNN_aa/trained_models/mcc_BC_opt_aaprop_signal_v0.1.h5', compile=False)
background_model = keras.models.load_model('CNN_multilabel/trained_models/binary_classification_CNN1D_bkg_v2.3.h5', compile=False)
#signal_model.load_weights("CNN_multilabel/val_signal_best_weights_4.h5")
signal_model = cnn_1d(1784)
signal_model.load_weights("CNN_multilabel/opt_sig_best.h5")
# with h5py.File('CNN_multilabel/opt_sig_best.h5', 'r') as f:
# # Iterate through the layers in the model
# for layer_name in f.keys():
# layer = f[layer_name]
# print(layer.values)
# for i in layer:
# print(i)
signal_prediction = signal_model.predict(candidate)#.flatten().tolist()
bkg_prediction = background_model.predict(candidate)#.flatten().tolist()
THRESHOLD_SIGNAL = 0.52
THRESHOLD_BACKGROUND = 0.52
# pred_labels = []
# for i in signal_prediction:
# if i[0] > i[1]:
# pred = "low"
# else:
# pred = "high"
# pred_labels.append(pred)
# pred_labels = []
# for i in np.round(signal_prediction):
# pred_labels.append("high" if i > THRESHOLD else "low")
# def compare_predict(softmax_sig=False, softmax_bkg=False):
# if softmax_sig:
# sig_pred = [("low" if i[0] > i[1] else "high") for i in signal_prediction]
# else:
# sig_pred = [("low" if i < THRESHOLD_SIGNAL else "high") for i in np.round(signal_prediction)]
# if softmax_bkg:
# bkg_pred = [("low" if i[0] > i[1] else "high") for i in bkg_prediction]
# else:
# bkg_pred = [("low" if i < THRESHOLD_BACKGROUND else "high") for i in np.round(bkg_prediction)]
def multi_pheno_predict(signal_prediction, bkg_prediction, signal_first=False):
bkg_predi = [i[1] for i in bkg_prediction]
bkg_pred = pd.DataFrame(bkg_predi, columns=["Background"])#, index=description)
sig_pred = pd.DataFrame(signal_prediction, columns=["Signal"])#, index=description)
prediction = pd.concat([sig_pred, bkg_pred], axis=1)
if signal_first:
sort = prediction.sort_values(['Signal', 'Background'], ascending=[False, True])
else:
sort = prediction.sort_values(['Background', 'Signal'], ascending=[True, False])
hslb, hshb, lshb, lslb = [], [], [], []
for index, row in sort.iterrows():
if round(row['Background']) == 0:
if not "*" in description[index]:
hslb.append(index)
elif round(row['Background']) == 1:
hshb.append(index)
for index, row in sort[::-1].iterrows():
if round(row['Background']) == 0:
lslb.append(index)
elif round(row['Background']) == 1:
lshb.append(index)
hslb_f = hslb[:101]
hshb_f = hshb[:100]
lshb_f = lshb[:100]
lslb_f = lslb[:100]
# for idx, row in prediction.iterrows():
# sig_val, bkg_val = row['Signal'], row['Background']
# if round(sig_val) == 0 and round(bkg_val) == 0:
# lslb.append(idx)
# elif round(sig_val) == 0 and round(bkg_val) == 1:
# lshb.append(idx)
# elif round(sig_val) == 1 and round(bkg_val) == 0:
# if not "*" in description[index]:
# hslb.append(idx)
# elif round(sig_val) == 1 and round(bkg_val) == 1:
# hshb.append(idx)
# lslb.sort(key=lambda idx: (prediction.iloc[idx]['Background'], prediction.iloc[idx]['Signal']))
# lshb.sort(key=lambda idx: (prediction.iloc[idx]['Background'], -prediction.iloc[idx]['Signal']))
# hslb.sort(key=lambda idx: (-prediction.iloc[idx]['Background'], prediction.iloc[idx]['Signal']))
# hshb.sort(key=lambda idx: (-prediction.iloc[idx]['Background'], -prediction.iloc[idx]['Signal']))
# Rename lslb to lslb_f (also others)
prediction.iloc[hslb_f]
prediction.iloc[lshb]
prediction.iloc[lslb]
fasta_dna = []
fasta_aa = []
for i in hslb_f:
signal = signal_prediction[i][0]
bkg = bkg_predi[i]
dna_read = SeqRecord(Seq(candidates[i]), id=description[i], description=f"{signal} {bkg}")
fasta_dna.append(dna_read)
Bio.SeqIO.write(fasta_dna, "high_candidates_dna.fasta", "fasta")
fasta_dna = []
fasta_aa = []
for i in range(len(hslb_f)):
diff = dif(ref_aa, candidates_aa[i])
description=[]
for x in diff:
des = f"{ref_aa[x]}{x + 1}{candidates_aa[i][x]}"
description.append(des)
description = "|".join(description)
dna_read = SeqRecord(Seq(candidates_dna[i]), id = "DmpR_insilico_mutant", description=description)
fasta_dna.append(dna_read)
aa_read = SeqRecord(Seq(candidates_aa[i]), id = "DmpR_insilico_mutant", description=description)
fasta_aa.append(aa_read)
Bio.SeqIO.write(fasta_dna, f"{filename}_dna.fasta", "fasta")
Bio.SeqIO.write(fasta_aa, f"{filename}_aa.fasta", "fasta")