-
Notifications
You must be signed in to change notification settings - Fork 14
/
ACP_DL.py
416 lines (385 loc) · 15.5 KB
/
ACP_DL.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
from keras.models import Sequential
from keras.layers import LSTM, Dense, Dropout, Activation, initializers, GRU, SimpleRNN
from keras.callbacks import ModelCheckpoint,TensorBoard
from keras.optimizers import RMSprop
import matplotlib.pyplot as plt
from keras.utils import plot_model
from lightgbm import LGBMClassifier
from sklearn.metrics import roc_curve, auc
from sklearn.model_selection import train_test_split
import pickle
import scipy.io as sio
import numpy as np
from numpy import linalg as la
import argparse
def TransDict_from_list(groups):
transDict = dict()
tar_list = ['0', '1', '2', '3', '4', '5', '6']
result = {}
index = 0
for group in groups:
g_members = sorted(group) # Alphabetically sorted list
for c in g_members:
# print('c' + str(c))
# print('g_members[0]' + str(g_members[0]))
result[c] = str(tar_list[index]) # K:V map, use group's first letter as represent.
index = index + 1
return result
def get_3_protein_trids():
nucle_com = []
chars = ['0', '1', '2', '3', '4', '5', '6']
base = len(chars)
end = len(chars) ** 3
for i in range(0, end):
n = i
ch0 = chars[n % base]
n = n / base
ch1 = chars[int(n % base)]
n = n / base
ch2 = chars[int(n % base)]
nucle_com.append(ch0 + ch1 + ch2)
return nucle_com
def translate_sequence(seq, TranslationDict):
'''
Given (seq) - a string/sequence to translate,
Translates into a reduced alphabet, using a translation dict provided
by the TransDict_from_list() method.
Returns the string/sequence in the new, reduced alphabet.
Remember - in Python string are immutable..
'''
import string
from_list = []
to_list = []
for k, v in TranslationDict.items():
from_list.append(k)
to_list.append(v)
# TRANS_seq = seq.translate(str.maketrans(zip(from_list,to_list)))
TRANS_seq = seq.translate(str.maketrans(str(from_list), str(to_list)))
# TRANS_seq = maketrans( TranslationDict, seq)
return TRANS_seq
def get_4_nucleotide_composition(tris, seq, pythoncount=True):
seq_len = len(seq)
tri_feature = [0] * len(tris)
k = len(tris[0])
note_feature = [[0 for cols in range(len(seq) - k + 1)] for rows in range(len(tris))]
if pythoncount:
for val in tris:
num = seq.count(val)
tri_feature.append(float(num) / seq_len)
else:
# tmp_fea = [0] * len(tris)
for x in range(len(seq) + 1 - k):
kmer = seq[x:x + k]
if kmer in tris:
ind = tris.index(kmer)
# tmp_fea[ind] = tmp_fea[ind] + 1
note_feature[ind][x] = note_feature[ind][x] + 1
# tri_feature = [float(val)/seq_len for val in tmp_fea] #tri_feature type:list len:256
u, s, v = la.svd(note_feature)
for i in range(len(s)):
tri_feature = tri_feature + u[i] * s[i] / seq_len
# print tri_feature
# pdb.set_trace()
return tri_feature
# def get_4_nucleotide_composition(tris, seq, pythoncount=True):
# seq_len = len(seq)
# tri_feature = []
#
# if pythoncount:
# for val in tris:
# num = seq.count(val)
# tri_feature.append(float(num) / seq_len)
# else:
# k = len(tris[0])
# tmp_fea = [0] * len(tris)
# for x in range(len(seq) + 1 - k):
# kmer = seq[x:x + k]
# if kmer in tris:
# ind = tris.index(kmer)
# tmp_fea[ind] = tmp_fea[ind] + 1
# tri_feature = [float(val) / seq_len for val in tmp_fea]
# # pdb.set_trace()
# return tri_feature
def prepare_feature_acp740():
label = []
interaction_pair = {}
RNA_seq_dict = {}
protein_seq_dict = {}
protein_index = 0
with open('acp740.txt', 'r') as fp:
for line in fp:
if line[0] == '>':
values = line[1:].strip().split('|')
label_temp = values[1]
proteinName = values[0]
if label_temp == '1':
label.append(1)
else:
label.append(0)
else:
seq = line[:-1]
protein_seq_dict[protein_index] = seq
protein_index = protein_index + 1
# name_list = read_name_from_lncRNA_fasta('ncRNA-protein/lncRNA_RNA.fa')
groups = ['AGV', 'ILFP', 'YMTS', 'HNQW', 'RK', 'DE', 'C']
group_dict = TransDict_from_list(groups)
protein_tris = get_3_protein_trids()
# tris3 = get_3_trids()
bpf=[]
kmer=[]
# get protein feature
# pdb.set_trace()
for i in protein_seq_dict: # and protein_fea_dict.has_key(protein) and RNA_fea_dict.has_key(RNA):
protein_seq = translate_sequence(protein_seq_dict[i], group_dict)
bpf_feature = BPF(protein_seq_dict[i])
# print('bpf:',shape(bpf_feature))
# pdb.set_trace()
# RNA_tri_fea = get_4_nucleotide_composition(tris, RNA_seq, pythoncount=False)
protein_tri_fea = get_4_nucleotide_composition(protein_tris, protein_seq, pythoncount =False)
bpf.append(bpf_feature)
kmer.append(protein_tri_fea)
# protein_index = protein_index + 1
# chem_fea.append(chem_tmp_fea)
return np.array(bpf), np.array(kmer), label
def prepare_feature_acp240():
label = []
interaction_pair = {}
RNA_seq_dict = {}
protein_seq_dict = {}
protein_index = 1
with open('acp240.txt', 'r') as fp:
for line in fp:
if line[0] == '>':
values = line[1:].strip().split('|')
label_temp = values[1]
protein = values[0]
if label_temp=='1':
label.append(1)
else:
label.append(0)
else:
seq = line[:-1]
protein_seq_dict[protein_index] = seq
protein_index = protein_index + 1
# name_list = read_name_from_lncRNA_fasta('ncRNA-protein/lncRNA_RNA.fa')
groups = ['AGV', 'ILFP', 'YMTS', 'HNQW', 'RK', 'DE', 'C']
group_dict = TransDict_from_list(groups)
protein_tris = get_3_protein_trids()
# tris3 = get_3_trids()
bpf = []
kmer = []
# get protein feature
# pdb.set_trace()
for i in protein_seq_dict: # and protein_fea_dict.has_key(protein) and RNA_fea_dict.has_key(RNA):
protein_seq = translate_sequence(protein_seq_dict[i], group_dict)
bpf_feature = BPF(protein_seq_dict[i])
# print('bpf:',shape(bpf_feature))
# pdb.set_trace()
# RNA_tri_fea = get_4_nucleotide_composition(tris, RNA_seq, pythoncount=False)
protein_tri_fea = get_4_nucleotide_composition(protein_tris, protein_seq, pythoncount =False)
bpf.append(bpf_feature)
kmer.append(protein_tri_fea)
protein_index = protein_index + 1
# chem_fea.append(chem_tmp_fea)
return np.array(bpf), np.array(kmer), label
def BPF(seq_temp):
seq = seq_temp
chars = ['A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'Y']
fea = []
tem_vec =[]
k = 7
for i in range(k):
if seq[i] =='A':
tem_vec = [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
elif seq[i]=='C':
tem_vec = [0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
elif seq[i]=='D':
tem_vec = [0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
elif seq[i]=='E':
tem_vec = [0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
elif seq[i]=='F':
tem_vec = [0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
elif seq[i]=='G':
tem_vec = [0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
elif seq[i]=='H':
tem_vec = [0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0]
elif seq[i]=='I':
tem_vec = [0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0]
elif seq[i]=='K':
tem_vec = [0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0]
elif seq[i]=='L':
tem_vec = [0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0]
elif seq[i]=='M':
tem_vec = [0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0]
elif seq[i]=='N':
tem_vec = [0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0]
elif seq[i]=='P':
tem_vec = [0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0]
elif seq[i]=='Q':
tem_vec = [0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0]
elif seq[i]=='R':
tem_vec = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0]
elif seq[i]=='S':
tem_vec = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0]
elif seq[i]=='T':
tem_vec = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0]
elif seq[i]=='V':
tem_vec = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0]
elif seq[i]=='W':
tem_vec = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0]
elif seq[i]=='Y':
tem_vec = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]
fea = fea + tem_vec
return fea
def prepare_feature():
label = []
interaction_pair = {}
RNA_seq_dict = {}
protein_seq_dict = {}
protein_index = 1
with open('acp740.txt', 'r') as fp:
for line in fp:
if line[0] == '>':
values = line[1:].strip().split('|')
label_temp = values[1]
protein = values[0]
if label_temp == '1':
label.append(1)
else:
label.append(0)
else:
seq = line[:-1]
protein_seq_dict[protein_index] = seq
protein_index = protein_index + 1
# name_list = read_name_from_lncRNA_fasta('ncRNA-protein/lncRNA_RNA.fa')
groups = ['AGV', 'ILFP', 'YMTS', 'HNQW', 'RK', 'DE', 'C']
group_dict = TransDict_from_list(groups)
protein_tris = get_3_protein_trids()
# tris3 = get_3_trids()
train = []
# get protein feature
# pdb.set_trace()
for i in protein_seq_dict: # and protein_fea_dict.has_key(protein) and RNA_fea_dict.has_key(RNA):
protein_seq = translate_sequence(protein_seq_dict[i], group_dict)
# bpf_feature = BPF(protein_seq_dict[i])
# pdb.set_trace()
# RNA_tri_fea = get_4_nucleotide_composition(tris, RNA_seq, pythoncount=False)
protein_tri_fea = get_4_nucleotide_composition(protein_tris, protein_seq, pythoncount =False)
train.append(protein_tri_fea)
protein_index = protein_index + 1
# chem_fea.append(chem_tmp_fea)
return np.array(train), label
def calculate_performace(test_num, pred_y, labels):
tp = 0
fp = 0
tn = 0
fn = 0
for index in range(test_num):
if labels[index] == 1:
if labels[index] == pred_y[index]:
tp = tp + 1
else:
fn = fn + 1
else:
if labels[index] == pred_y[index]:
tn = tn + 1
else:
fp = fp + 1
acc = float(tp + tn) / test_num
precision = float(tp) / (tp + fp)
sensitivity = float(tp) / (tp + fn)
specificity = float(tn) / (tn + fp)
MCC = float(tp * tn - fp * fn) / (np.sqrt((tp + fp) * (tp + fn) * (tn + fp) * (tn + fn)))
return acc, precision, sensitivity, specificity, MCC
def transfer_label_from_prob(proba):
label = [1 if val >= 0.5 else 0 for val in proba]
return label
def plot_roc_curve(labels, probality, legend_text, auc_tag=True):
# fpr2, tpr2, thresholds = roc_curve(labels, pred_y)
fpr, tpr, thresholds = roc_curve(labels, probality) # probas_[:, 1])
roc_auc = auc(fpr, tpr)
if auc_tag:
rects1 = plt.plot(fpr, tpr, label=legend_text + ' (AUC=%6.3f) ' % roc_auc)
else:
rects1 = plt.plot(fpr, tpr, label=legend_text)
def ACP_DL():
# define parameters
data_dim = 483
timesteps = 1
batch_size = 32 # if dataset = acp240, set batch_size = 32; if dataset = acp740, set batch_size = 64
epochs = 30
# get data
#bpf, kmer, label = prepare_feature_acp740()
bpf, kmer, label = prepare_feature_acp240()
X = np.concatenate((bpf, kmer), axis=1) # 1 行拼接 0 默认,列拼接
# expected input data shape: (batch_size, timesteps, data_dim)
X = np.reshape(X, (len(X), timesteps, data_dim))
# split data
# x_train, x_test, y_train, y_test = train_test_split(X, label, test_size=0.1, random_state=1024)
num_cross_val = 5 # 5-fold
all_performance_lstm = []
all_labels = []
all_prob = {}
num_classifier = 3
all_prob[0] = []
all_average = []
for fold in range(num_cross_val):
# train = np.array([x for i, x in enumerate(bpf_fea) if i % num_cross_val != fold])
# test = np.array([x for i, x in enumerate(bpf_fea) if i % num_cross_val == fold])
# train = np.array([x for i, x in enumerate(kmer_fea) if i % num_cross_val != fold])
# test = np.array([x for i, x in enumerate(kmer_fea) if i % num_cross_val == fold])
train = np.array([x for i, x in enumerate(X) if i % num_cross_val != fold])
test = np.array([x for i, x in enumerate(X) if i % num_cross_val == fold])
train_label = np.array([x for i, x in enumerate(label) if i % num_cross_val != fold])
test_label = np.array([x for i, x in enumerate(label) if i % num_cross_val == fold])
real_labels = []
for val in test_label:
if val == 1:
real_labels.append(1)
else:
real_labels.append(0)
train_label_new = []
for val in train_label:
if val == 1:
train_label_new.append(1)
else:
train_label_new.append(0)
all_labels = all_labels + real_labels
# init = initializers.TruncatedNormal(mean=0.0, stddev=0.05, seed=64)
model = Sequential()
# model.add(Dense(200,input_shape=(200,1)))
model.add(LSTM(128, return_sequences=False,input_shape=(timesteps, data_dim), name='lstm1')) # returns a sequence of vectors of dimension 32
#model.add(LSTM(32, return_sequences=True, name='lstm2')) # returns a sequence of vectors of dimension 32
#model.add(LSTM(8, return_sequences=True,name='lstm3')) # returns a sequence of vectors of dimension 32
#model.add(LSTM(3, return_sequences=False,name='lstm4')) # return a single vector of dimension 32
model.add(Dropout(0.25, name='dropout'))
model.add(Dense(1, name='full_connect'))
model.add(Activation('sigmoid'))
model.summary()
print('Compiling the Model...')
model.compile(loss='binary_crossentropy', #
optimizer='adam',
metrics=['accuracy'])
print("Train...")
model.fit(train, train_label, batch_size=batch_size,epochs=epochs)
lstm_proba = model.predict_proba(test)
all_prob[0] = all_prob[0] + [val for val in lstm_proba]
y_pred_xgb = transfer_label_from_prob(lstm_proba)
acc, precision, sensitivity, specificity, MCC = calculate_performace(len(real_labels), y_pred_xgb, real_labels)
print(acc, precision, sensitivity, specificity, MCC)
all_performance_lstm.append([acc, precision, sensitivity, specificity, MCC])
print('---' * 50)
print('mean performance of ACP_DL')
print(np.mean(np.array(all_performance_lstm), axis=0))
print('---' * 50)
plot_roc_curve(all_labels, all_prob[0], 'proposed method')
plt.plot([0, 1], [0, 1], 'k--')
plt.xlim([-0.05, 1])
plt.ylim([0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('ROC')
plt.legend(loc="lower right")
# plt.savefig(save_fig_dir + selected + '_' + class_type + '.png')
plt.show()
ACP_DL()