-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpredict.py
194 lines (159 loc) · 6.09 KB
/
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
import sys
import pandas as pd
import os
import torch as torch
import numpy as np
import argparse
from utils_tools.utils import *
data_dir = 'data_processed'
embedding_feature_dim_msa = 768
# Set up argument parser
parser = argparse.ArgumentParser(description='Predict')
parser.add_argument('--data_dir', nargs='?', default='data_processed', help='data directory')
parser.add_argument('--group_info', nargs='?', default='default', help='group information provided or not')
# Parse arguments
args = parser.parse_args()
dic = {'NO_SP': 0, 'SP': 1, 'LIPO': 2, 'TAT': 3, 'TATLIPO' : 4, 'PILIN' : 5}
dic2 = {0: 'NO_SP', 1: 'SP', 2: 'LIPO', 3: 'TAT', 4: 'TATLIPO', 5: 'PILIN'}
kingdom_dic = {'EUKARYA':0, 'ARCHAEA':1, 'POSITIVE':2, 'NEGATIVE': 3}
def trans_data(str1, padding_length):
# Translates amino acids into numbers
a = []
trans_dic = {'A':1,'C':2,'D':3,'E':4,'F':5,'G':6,'H':7,'I':8,'K':9,'L':10,'M':11,'N':12,'P':13,'Q':14,'R':15,'S':16,'T':17,'V':18,'W':19,'Y':20,'X':0}
for i in range(len(str1)):
if (str1[i] in trans_dic.keys()):
a.append(trans_dic.get(str1[i]))
else:
print("Unknown letter:" + str(str1[i]))
a.append(trans_dic.get('X'))
while(len(a)<padding_length):
a.append(0)
return a
def trans_label(str1):
# Translates labels into numbers
if((str1) in dic.keys()):
a = dic.get(str1)
else:
print(str1)
raise Exception('Unknown category!')
return a
def createTestData(data_path='./test_data/data_list.txt',
kingdom_path='./test_data/kingdom_list.txt',
maxlen=70, test_path="./embedding/test_feature.npy"
):
# Initialize
data_list = []
kingdom_list=[]
raw_data=[]
# Load data
with open(data_path, 'r') as data_file:
for line in data_file:
str = np.array(trans_data(line.strip('\n')[0:70], maxlen))
data_list.append(str)
with open(data_path, 'r') as data_file:
for line in data_file:
str = line.strip('\n\t')[0:70]
raw_data.append(("protein", str))
features = np.load(test_path)
with open(kingdom_path, 'r') as kingdom_file:
for line in kingdom_file:
if args.group_info == 'no_group_info':
kingdom_list.append([0, 0, 0, 0])
else:
kingdom_list.append(np.eye(len(kingdom_dic.keys()))[kingdom_dic[line.strip('\n\t')]])
data_file.close()
kingdom_file.close()
X = np.array(data_list)
kingdoms= np.array(kingdom_list)
X = np.concatenate((X,kingdoms, features), axis=1)
return X
def trans_output(str1):
# Translates numbers into labels
if((str1) in dic2.keys()):
a = dic2.get(str1)
else:
print(str1)
raise Exception('Unknown category!')
return a
if __name__ == '__main__':
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# read file names if provided
if args.data_dir != 'data_processed':
data_dir = args.data_dir
if args.group_info == 'no_group_info':
model = torch.load("../data/mdl/USPNet_no_group_info.pth", map_location=device)
else:
model = torch.load("../data/mdl/USPNet_model.pth", map_location=device)
if isinstance(model, torch.nn.DataParallel):
# access the model inside the DataParallel wrapper
model = model.module
model = model.to(device)
model.eval()
filename_list = ["data_list.txt",
"kingdom_list.txt",
"test_feature.npy",
]
for i in range(len(filename_list)):
filename_list[i] = os.path.join(data_dir, filename_list[i])
X_test = createTestData(data_path=filename_list[0],
kingdom_path=filename_list[1],
test_path=filename_list[2])
output = []
output_aa = []
aux_test = []
X_test = torch.tensor(X_test).to(device)
test_loader = torch.utils.data.DataLoader(X_test, batch_size=256)
for i, input in enumerate(test_loader):
input = input.to(device)
aux = input[:, 70:74]
aux = aux.cpu().detach().numpy()
aux_test.extend(aux)
o1, o_aa = model(input)
output.extend(o1.cpu().detach().numpy())
output_aa.extend(o_aa.cpu().detach().numpy())
output = torch.tensor(np.array(output))
results = pred(output).cpu().detach().numpy()
# For Eurkaryota:
for i in range(len(aux_test)):
if (aux_test[i][0] == 1 and results[i] != 1 and results[i] != 0):
results[i] = 0
output_aa = torch.argmax(torch.tensor(np.array(output_aa)), dim=2).reshape(-1, 1)
results_aa = output_aa.cpu().detach().numpy()
output_aa_ = results_aa.reshape(-1, 70).copy()
indexes_ = np.where(output_aa_ == 1)
output_aa_[indexes_] = 100
indexes_1 = np.where(output_aa_ == 3)
indexes_2 = np.where(output_aa_ == 0)
output_aa_[indexes_1] = 1
output_aa_[indexes_2] = 1
indexes_0 = np.where(output_aa_ != 1)
output_aa_[indexes_0] = 0
indexes_pos = np.where(output_aa_ == 1)
predicted_type = []
for result in results:
predicted_type.append(trans_output(result))
indexes1= indexes_pos[0].copy().tolist()
indexes2 = indexes_pos[1].copy().tolist()
predicted_cleavage = []
count = 0
data_list = []
with open(filename_list[0], 'r') as data_file:
for line in data_file:
data_list.append(line.strip('\n'))
data_file.close()
for result in results:
if result==0:
predicted_cleavage.append('')
else:
try:
index = indexes1.index(count)
except:
predicted_cleavage.append(data_list[count])
else:
index = indexes1.index(count)
index2 = indexes2[index]
sq=data_list[count]
predicted_cleavage.append(sq[:index2+1])
count = count + 1
df = pd.DataFrame({'sequence': data_list, 'predicted_type': predicted_type, 'predicted_cleavage': predicted_cleavage})
df.to_csv(data_dir + '/results.csv', index=False)