-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict.py
137 lines (118 loc) · 4.97 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
import os
import argparse
import pandas as pd
import numpy as np
import torch
from torch import nn
from torch.nn import functional as F
from torch import optim
import torch.utils.data as Data
from sklearn.model_selection import KFold
import pickle as pkl
from model import Transformer
from sklearn.metrics import classification_report
from sklearn.metrics import accuracy_score
from scipy.special import softmax
from sklearn.metrics import f1_score
from sklearn.metrics import precision_score
parser = argparse.ArgumentParser(description="""Main script of PhaSUIT.""")
parser.add_argument('--file', help='input patches', default = 'converted_protein')
parser.add_argument('--task', help='binary task or multi-class task', default = 'binary')
parser.add_argument('--midfolder', help='pth to the midfolder foder', default = 'midfolder/')
parser.add_argument('--out', help='pth to the output foder', default = 'out/')
parser.add_argument('--toolpth', help='pth to the PhaVP foder', default = 'PhaVP/')
parser.add_argument('--outfile', help='name of the output file', default = 'final_prediction.csv')
inputs = parser.parse_args()
file_fn = inputs.file
mid_fn = inputs.midfolder
task = inputs.task
out_fn = inputs.out
tool_fn = inputs.toolpth
outfile = inputs.outfile
converted_test = pkl.load(open(f"{mid_fn}/{file_fn}", 'rb'))
test_labels = np.array(np.zeros(converted_test.shape[0]))
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if task == 'binary':
out_dim = 2
else:
out_dim = 8
def reset_model():
model = Transformer(
src_vocab_size = converted_test.shape[2],
src_pad_idx = 0,
device=device,
max_length=converted_test.shape[1],
dropout=0.1,
out_dim = out_dim
).to(device)
optimizer = optim.Adam(model.parameters(), lr=0.001)
loss_func = nn.CrossEntropyLoss()
return model, optimizer, loss_func
def return_softmax(all_score):
result = []
for item in all_score:
result.append(softmax(item))
return np.array(result)
def return_batch(train_sentence, label, flag, drop):
X_train = torch.from_numpy(train_sentence).float()
y_train = torch.from_numpy(label).long()
train_dataset = Data.TensorDataset(X_train, y_train)
training_loader = Data.DataLoader(
dataset=train_dataset,
batch_size=256,
shuffle=flag,
num_workers=0,
drop_last=drop
)
return training_loader
def return_tensor(var, device):
return torch.from_numpy(var).to(device)
model, optimizer, loss_func = reset_model()
try:
if task == 'binary':
pretrained_dict=torch.load(f'{tool_fn}/model/transformer_binary.pth', map_location=device)
else:
pretrained_dict=torch.load(f'{tool_fn}/model/transformer_multi.pth', map_location=device)
model.load_state_dict(pretrained_dict)
except:
print('cannot find pre-trained model')
exit(1)
int2label = {0:'minor_capsid',1:'tail_fiber', 2:'major_tail', 3:'portal', 4:'minor_tail', 5:'baseplate', 6:'major_capsid', 7:'other'}
if task == 'binary':
test_loader = return_batch(converted_test, test_labels, flag = False, drop=False)
model = model.eval()
with torch.no_grad():
all_pred = []
all_score = []
for step, (batch_x, batch_y) in enumerate(test_loader):
logit = model(batch_x.to(device))
pred = np.argmax(logit.squeeze(1).cpu().detach().numpy(), axis=1).tolist()
all_pred += pred
pred = logit.squeeze(1).cpu().detach().numpy()
all_score.append(pred)
all_score = np.concatenate(all_score)
all_score = return_softmax(all_score)
name = file_fn.split('converted_')[1]
df = pd.read_csv(f'{mid_fn}/{name}.txt')
all_pred = ['PVP' if item > 0.5 else 'non-PVP' for item in all_pred]
pred_df = pd.DataFrame({"accession":df['accession'].values, "pred":all_pred, "score":all_score[:, 1]})
pred_df.to_csv(f'{out_fn}/{outfile}', index=False)
else:
test_loader = return_batch(converted_test, test_labels, flag = False, drop=False)
model = model.eval()
with torch.no_grad():
all_pred = []
all_score = []
for step, (batch_x, batch_y) in enumerate(test_loader):
logit = model(batch_x.to(device))
pred = np.argmax(logit.squeeze(1).cpu().detach().numpy(), axis=1).tolist()
all_pred += pred
pred = logit.squeeze(1).cpu().detach().numpy()
all_score.append(pred)
all_score = np.concatenate(all_score)
all_score = return_softmax(all_score)
name = file_fn.split('converted_')[1]
df = pd.read_csv(f'{mid_fn}/{name}.txt')
all_pred = [int2label[item] for item in np.argmax(all_score, axis=1)]
pred_df = pd.DataFrame({"accession":df['accession'].values, "pred":all_pred, "score":np.max(all_score, axis=1)})
pred_df.to_csv(f'{out_fn}/{outfile}', index=False)