forked from wengong-jin/RefineGNN
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprint_cdr_plus.py
executable file
·146 lines (110 loc) · 3.33 KB
/
print_cdr_plus.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
import torch
import torch.nn as nn
import torch.optim as optim
import torch.optim.lr_scheduler as lr_scheduler
from torch.utils.data import DataLoader
import json
import csv
import math, random, sys
import numpy as np
import argparse
import os
from fold_train_plus import *
from tqdm import tqdm
restype_1to3 = {
"A": "ALA",
"R": "ARG",
"N": "ASN",
"D": "ASP",
"C": "CYS",
"Q": "GLN",
"E": "GLU",
"G": "GLY",
"H": "HIS",
"I": "ILE",
"L": "LEU",
"K": "LYS",
"M": "MET",
"F": "PHE",
"P": "PRO",
"S": "SER",
"T": "THR",
"W": "TRP",
"Y": "TYR",
"V": "VAL",
}
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('--data_path', default='data/sabdab_2022_01/test_data.jsonl')
parser.add_argument('--save_dir', default='pred_pdb/')
parser.add_argument('--load_model', required=True)
parser.add_argument('--seed', type=int, default=7)
args = parser.parse_args()
return args
def set_SEED(args):
torch.manual_seed(args.seed)
np.random.seed(args.seed)
random.seed(args.seed)
return
def load_model(args):
model_ckpt, opt_ckpt, model_args = torch.load(args.load_model)
model = RefineFolder_plus(model_args).cuda()
model.load_state_dict(model_ckpt)
model.eval()
return model, model_args
def load_data(
args,
model_args
):
data = AntibodyComplexDataset(
args.data_path,
cdr_type=model_args.cdr,
L_binder=model_args.L_binder,
L_target=model_args.L_target,
language_model=False
)
loader = ComplexLoader(data, batch_tokens=0)
return loader
def predict_cdrs(
args,
model,
loader
):
with torch.no_grad():
for data in tqdm(loader):
binder, scaffold, target, surface = make_batch(data)[:4]
binder_surface = surface[0][0].tolist()
out = model(binder, scaffold, surface)
bind_X, _, bind_A = binder
bind_mask = bind_A.clamp(max=1).float()
bb_rmsd = compute_rmsd(
out.bind_X[:, :, 1], bind_X[:, :, 1], bind_mask[:, :, 1]
).item()
pdb = data[0]['pdb']
X = out.bind_X + 200
X = X.cpu().numpy()
path = os.path.join(args.save_dir, f'{pdb}_RefineGNNplus_cdrs.pdb')
with open(path, 'w') as f:
print(f'REMARK RMSD={bb_rmsd:.4f}', file=f)
for i in range(bind_X.size(1)):
idx = binder_surface[i]
aaname = data[0]['antibody_seq'][idx]
aaname = restype_1to3[aaname]
print(f'ATOM 924 CA {aaname} H ' + str(binder_surface[i]) + ' ' + ' '.join(niceprint(X[0, i, 1, :])), file=f)
return
if __name__ == '__main__':
args = get_args() # get variables
set_SEED(args) # set seed for reproducibility
os.makedirs(args.save_dir, exist_ok=True) # make directory
model, model_args = load_model(args=args) # load model
loader = load_data(
args=args,
model_args=model_args
) # load data
niceprint = np.vectorize(lambda x : "%.3f" % (x,))
# predict cdrs and save pdbs
predict_cdrs(
args = args,
model = model,
loader = loader
)