-
Notifications
You must be signed in to change notification settings - Fork 14
/
sim_inference.py
140 lines (107 loc) · 4.56 KB
/
sim_inference.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
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--sim_ckpt', type=str, default=None, required=True)
parser.add_argument('--data_dir', type=str, default=None, required=True)
parser.add_argument('--suffix', type=str, default='')
parser.add_argument('--pdb_id', nargs='*', default=[])
parser.add_argument('--num_frames', type=int, default=1000)
parser.add_argument('--num_rollouts', type=int, default=100)
parser.add_argument('--no_frames', action='store_true')
parser.add_argument('--tps', action='store_true')
parser.add_argument('--xtc', action='store_true')
parser.add_argument('--out_dir', type=str, default=".")
parser.add_argument('--split', type=str, default='splits/4AA_test.csv')
args = parser.parse_args()
import os, torch, mdtraj, tqdm, time
import numpy as np
from mdgen.geometry import atom14_to_frames, atom14_to_atom37, atom37_to_torsions
from mdgen.residue_constants import restype_order, restype_atom37_mask
from mdgen.tensor_utils import tensor_tree_map
from mdgen.wrapper import NewMDGenWrapper
from mdgen.utils import atom14_to_pdb
import pandas as pd
os.makedirs(args.out_dir, exist_ok=True)
def get_batch(name, seqres, num_frames):
arr = np.lib.format.open_memmap(f'{args.data_dir}/{name}{args.suffix}.npy', 'r')
if not args.tps: # else keep all frames
arr = np.copy(arr[0:1]).astype(np.float32)
frames = atom14_to_frames(torch.from_numpy(arr))
seqres = torch.tensor([restype_order[c] for c in seqres])
atom37 = torch.from_numpy(atom14_to_atom37(arr, seqres[None])).float()
L = len(seqres)
mask = torch.ones(L)
if args.no_frames:
return {
'atom37': atom37,
'seqres': seqres,
'mask': restype_atom37_mask[seqres],
}
torsions, torsion_mask = atom37_to_torsions(atom37, seqres[None])
return {
'torsions': torsions,
'torsion_mask': torsion_mask[0],
'trans': frames._trans,
'rots': frames._rots._rot_mats,
'seqres': seqres,
'mask': mask, # (L,)
}
def rollout(model, batch):
#print('Start sim', batch['trans'][0,0,0])
if args.no_frames:
expanded_batch = {
'atom37': batch['atom37'].expand(-1, args.num_frames, -1, -1, -1),
'seqres': batch['seqres'],
'mask': batch['mask'],
}
else:
expanded_batch = {
'torsions': batch['torsions'].expand(-1, args.num_frames, -1, -1, -1),
'torsion_mask': batch['torsion_mask'],
'trans': batch['trans'].expand(-1, args.num_frames, -1, -1),
'rots': batch['rots'].expand(-1, args.num_frames, -1, -1, -1),
'seqres': batch['seqres'],
'mask': batch['mask'],
}
atom14, _ = model.inference(expanded_batch)
new_batch = {**batch}
if args.no_frames:
new_batch['atom37'] = torch.from_numpy(
atom14_to_atom37(atom14[:,-1].cpu(), batch['seqres'][0].cpu())
).cuda()[:,None].float()
else:
frames = atom14_to_frames(atom14[:,-1])
new_batch['trans'] = frames._trans[None]
new_batch['rots'] = frames._rots._rot_mats[None]
atom37 = atom14_to_atom37(atom14[0,-1].cpu(), batch['seqres'][0].cpu())
torsions, _ = atom37_to_torsions(atom37, batch['seqres'][0].cpu())
new_batch['torsions'] = torsions[None, None].cuda()
return atom14, new_batch
def do(model, name, seqres):
item = get_batch(name, seqres, num_frames = model.args.num_frames)
batch = next(iter(torch.utils.data.DataLoader([item])))
batch = tensor_tree_map(lambda x: x.cuda(), batch)
all_atom14 = []
start = time.time()
for _ in tqdm.trange(args.num_rollouts):
atom14, batch = rollout(model, batch)
# print(atom14[0,0,0,1], atom14[0,-1,0,1])
all_atom14.append(atom14)
print(time.time() - start)
all_atom14 = torch.cat(all_atom14, 1)
path = os.path.join(args.out_dir, f'{name}.pdb')
atom14_to_pdb(all_atom14[0].cpu().numpy(), batch['seqres'][0].cpu().numpy(), path)
if args.xtc:
traj = mdtraj.load(path)
traj.superpose(traj)
traj.save(os.path.join(args.out_dir, f'{name}.xtc'))
traj[0].save(os.path.join(args.out_dir, f'{name}.pdb'))
@torch.no_grad()
def main():
model = NewMDGenWrapper.load_from_checkpoint(args.sim_ckpt)
model.eval().to('cuda')
df = pd.read_csv(args.split, index_col='name')
for name in df.index:
if args.pdb_id and name not in args.pdb_id:
continue
do(model, name, df.seqres[name])
main()