-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPbiofilm_inhibitory.py
109 lines (102 loc) · 4.48 KB
/
Pbiofilm_inhibitory.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
import torch
from transformers import T5EncoderModel, T5Tokenizer
import re
import gc
from sklearn.ensemble import ExtraTreesClassifier
from Extract_feature import *
import pickle
import sys
def Load_data():
print('Data Loading...')
Sequence = []
with open('Biofilm_inhibitory_peptides/Biofilm_In_train.fasta', 'r') as myfile:
for line in myfile:
if line[0] != '>':
Sequence.append(line.strip('\n'))
with open('Biofilm_inhibitory_peptides/Biofilm_In_test.fasta', 'r') as myfile:
for line in myfile:
if line[0] != '>':
Sequence.append(line.strip('\n'))
Mysequence = []
for i in range(len(Sequence)):
zj = ''
for j in range(len(Sequence[i])-1):
zj += Sequence[i][j] + ' '
zj += Sequence[i][-1]
Mysequence.append(zj)
return Sequence, Mysequence
def ALL_features(Sequence, sequences_Example):
# Crafted features
features_crafted = Get_features(Sequence, 5)
# Automatic extracted features
tokenizer = T5Tokenizer.from_pretrained("prot_t5_xl_uniref50", do_lower_case=False)
model = T5EncoderModel.from_pretrained("prot_t5_xl_uniref50")
gc.collect()
print(torch.cuda.is_available())
# 'cuda:0' if torch.cuda.is_available() else
device = torch.device('cuda:1' if torch.cuda.is_available() else 'cpu')
model = model.to(device)
model = model.eval()
features = []
for i in range(len(sequences_Example)):
print('For sequence ', str(i+1))
sequences_Example_i = sequences_Example[i]
sequences_Example_i = [re.sub(r"[UZOB]", "X", sequences_Example_i)]
ids = tokenizer.batch_encode_plus(sequences_Example_i, add_special_tokens=True, padding=True)
input_ids = torch.tensor(ids['input_ids']).to(device)
attention_mask = torch.tensor(ids['attention_mask']).to(device)
with torch.no_grad():
embedding = model(input_ids=input_ids, attention_mask=attention_mask)
embedding = embedding.last_hidden_state.cpu().numpy()
for seq_num in range(len(embedding)):
seq_len = (attention_mask[seq_num] == 1).sum()
seq_emd = embedding[seq_num][:seq_len - 1]
features.append(seq_emd)
features_normalize = np.zeros([len(features), len(features[0][0])], dtype=float)
for i in range(len(features)):
for k in range(len(features[0][0])):
for j in range(len(features[i])):
features_normalize[i][k] += features[i][j][k]
features_normalize[i][k] /= len(features[i])
print(len(features_normalize), len(features_normalize[0]))
# features_normalize = []
return features_crafted, features_normalize
def Peptide_bih(features_crafted, features_normalize):
features_ensemble = np.concatenate((features_normalize, features_crafted), axis=1)
Label = np.concatenate((np.ones([201], dtype=int), np.zeros([88], dtype=int),
np.ones([10], dtype=int), np.zeros([10], dtype=int)), axis=0)
model = ExtraTreesClassifier()
model.fit(features_ensemble, Label)
with open('Peptide_bih.pkl', 'wb') as f:
pickle.dump(model, f)
if __name__ == '__main__':
Tag = sys.argv[1]
Dir = sys.argv[2]
if Tag == 'Train':
Sequence, Mysequence = Load_data()
features_crafted, features_normalize = ALL_features(Sequence, Mysequence)
Peptide_bih(features_crafted, features_normalize)
elif Tag == 'Predict':
Sequence = []
with open(Dir, 'r') as myfile:
for line in myfile:
if line[0] != '>':
Sequence.append(line.strip('\n'))
Mysequence = []
for i in range(len(Sequence)):
zj = ''
for j in range(len(Sequence[i])-1):
zj += Sequence[i][j] + ' '
zj += Sequence[i][-1]
Mysequence.append(zj)
features_crafted, features_normalize = ALL_features(Sequence, Mysequence)
features_ensemble = np.concatenate((features_normalize, features_crafted), axis=1)
with open('Peptide_bih.pkl', 'rb') as myfile:
model = pickle.load(myfile)
Pre_label = model.predict(features_ensemble)
print(Pre_label)
Pre_label = np.array(Pre_label).T
res = pd.DataFrame({'Pre_label': Pre_label})
res.to_excel('Pre_label.xlsx')
else:
print('Please input Train/Test/Predict !')