-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathrun_rfdiffusion.py
More file actions
355 lines (292 loc) · 10.4 KB
/
run_rfdiffusion.py
File metadata and controls
355 lines (292 loc) · 10.4 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import os
import glob
import pandas as pd
import subprocess
from difflib import SequenceMatcher
from Bio import SeqIO
from Bio.PDB import PDBParser, PDBIO, Chain
from Bio.PDB.Polypeptide import PPBuilder
from seq_models.util.numbering import mask_regions
from seq_models.sample import make_tags
def parse_pdb_chains(pdb_file):
parser = PDBParser()
structure = parser.get_structure("protein", pdb_file)
pp_builder = PPBuilder()
sequences = {}
for model in structure:
for chain in model:
chain_id = chain.get_id()
sequence = "".join([str(pp.get_sequence()) for pp in pp_builder.build_peptides(chain)])
sequences[chain_id] = sequence
return sequences
def parse_fasta(file):
sequences = {}
with open(file, "r") as fasta_file:
for i, record in enumerate(SeqIO.parse(fasta_file, "fasta")):
sequences[i] = str(record.seq).split("/")
return sequences
def get_strs(mask_info, chain_id, fixed_length):
ranges = mask_info["mask_ranges"]
chain_len = len(mask_info["seed"])
if not fixed_length:
masked_seed = mask_info['masked_seed']
masked_ranges = mask_info['masked_ranges']
len_ranges = []
for r in masked_ranges:
vals = masked_seed[r[0]:r[1]]
max_len = len(vals)
min_len = max_len - vals.count("-")
len_ranges += [(min_len, max_len)]
contigs = []
inpaint = []
start_idx = 0
for i, r in enumerate(ranges):
contigs += [f"{chain_id}{start_idx+1}-{r[0]}"]
if fixed_length:
contigs += [f"{r[1]-r[0]}-{r[1]-r[0]}"]
else:
min_len, max_len = len_ranges[i]
contigs += [f"{min_len}-{max_len}"]
inpaint += [f"{chain_id}{r[0]}-{r[1]}"]
start_idx = r[1]
contigs += [f"{chain_id}{start_idx+1}-{chain_len}"]
return "/".join(contigs), "/".join(inpaint)
def get_rfdiffusion_range_str(mask_info, fixed_length):
h_contigs, h_inpaint = get_strs(mask_info["vh"], "H", fixed_length)
l_contigs, l_inpaint = get_strs(mask_info["vl"], "L", fixed_length)
contigs = h_contigs + "/0 " + l_contigs + "/0"
if len(h_inpaint) > 0 and len(l_inpaint) > 0:
inpaint_seq = h_inpaint + "/" + l_inpaint
elif len(h_inpaint) > 0:
inpaint_seq = h_inpaint
elif len(l_inpaint) > 0:
inpaint_seq = l_inpaint
return contigs, inpaint_seq
def renumber_pdb(input_pdb, output_pdb):
parser = PDBParser()
structure = parser.get_structure("protein", input_pdb)
for model in structure:
old_chains = []
new_chains = []
for chain in model:
new_chain_id = chain.id + "_renum"
new_chain = Chain.Chain(new_chain_id)
for i, residue in enumerate(chain):
new_residue = residue.copy()
new_residue_id = (residue.id[0], i + 1, residue.id[2])
new_residue.id = new_residue_id
new_chain.add(new_residue)
old_chains.append(chain)
new_chains.append(new_chain)
for chain, new_chain in zip(old_chains, new_chains):
model.detach_child(chain.id)
new_chain.id = chain.id
model.add(new_chain)
io = PDBIO()
io.set_structure(structure)
io.save(output_pdb)
def run_inference(pdb_file, output_prefix, contigs, inpaint_seq, num_samples=10):
h_infill = len(contigs.split(" ")[0].split("/")) > 2
l_infill = len(contigs.split(" ")[1].split("/")) > 2
if h_infill and l_infill:
command = [
"RFdiffusion/scripts/run_inference.py",
f"inference.output_prefix={output_prefix}_vh",
f"inference.input_pdb={pdb_file}",
f"contigmap.contigs=[{contigs}]",
f"inference.num_designs={num_samples}",
f"contigmap.inpaint_seq=[{inpaint_seq}]",
]
# hydra.output_subdir
subprocess.run(command, check=True)
else:
command = [
"RFdiffusion/scripts/run_inference.py",
f"inference.output_prefix={output_prefix}",
f"inference.input_pdb={pdb_file}",
f"contigmap.contigs=[{contigs}]",
f"inference.num_designs={num_samples}",
f"contigmap.inpaint_seq=[{inpaint_seq}]",
]
subprocess.run(command, check=True)
def run_inverse_folding(
output_prefix,
seq_per_sample=5,
):
pdb_dir = os.path.dirname(output_prefix)
# Run ProteinMPNN
output_path = os.path.join(pdb_dir, "parsed_pdbs.jsonl")
command = [
'python',
f'ProteinMPNN/helper_scripts/parse_multiple_chains.py',
f'--input_path={pdb_dir}',
f'--output_path={output_path}',
]
subprocess.run(command, check=True)
command = [
'python',
f'ProteinMPNN/protein_mpnn_run.py',
'--out_folder',
pdb_dir,
'--jsonl_path',
output_path,
'--num_seq_per_target',
str(seq_per_sample),
'--sampling_temp',
'0.1',
'--seed',
'38',
'--batch_size',
'1',
]
subprocess.run(command, check=True)
def match_dicts(vh_seed, vl_seed, chains):
def get_match(seed, chains):
keys = list(chains.keys())
sims = [
SequenceMatcher(None, seed, chains[k]).ratio() for k in keys
]
match = keys[sims.index(max(sims))]
return chains[match]
return {
"vh": get_match(vh_seed, chains),
"vl": get_match(vl_seed, chains),
}
def parse_to_df(output_prefix, info, model_tag, fixed_length):
pdb_dir = os.path.dirname(output_prefix)
fastas = glob.glob(os.path.join(pdb_dir,'seqs','*.fa'))
samples = []
for f in fastas:
pdb_path = os.path.join(
pdb_dir,
os.path.basename(f).split(".")[0] + ".pdb"
)
pdb_chains = parse_pdb_chains(pdb_path)
rfdiffusion_samples = match_dicts(
info["vh"]["seed"],
info["vl"]["seed"],
pdb_chains
)
mpnn_samples = parse_fasta(f)
og_seqs = mpnn_samples[0]
mpnn_samples = [v for k,v in mpnn_samples.items() if k != 0]
chain_to_idx = {
"vh": og_seqs.index(rfdiffusion_samples["vh"]),
"vl": og_seqs.index(rfdiffusion_samples["vl"]),
}
og_seqs = {k: og_seqs[v] for k,v in chain_to_idx.items()}
for s in mpnn_samples:
sample = {}
for chain_id in chain_to_idx:
sampled = s[chain_to_idx[chain_id]]
og = og_seqs[chain_id]
# print(len(info[chain_id]["seed"]))
# print(info[chain_id])
# print(len(sampled))
# print(len(og))
# print(1/0)
mask = info[chain_id]["mask_arr"]
sample[chain_id] = "".join([
sampled[i] if mask[i] else og[i] for i in range(len(mask))
])
samples.append(sample)
df = []
for i, sample in enumerate(samples):
df.append({
"vh_seed": info["vh"]["seed"],
"vl_seed": info["vl"]["seed"],
"sample_num": i,
"vh_sample": sample["vh"],
"vl_sample": sample["vl"],
"vh_mask": info["vh"]["mask_str"],
"vl_mask": info["vl"]["mask_str"],
"sample_tag": info["tag"],
"model_tag": model_tag,
"fixed_length": fixed_length,
})
df = pd.DataFrame(df)
return df
def _tag_to_df(i, pdb_file, results_dir, sample_tag, model_tag, fixed_length, extra_tag=None):
def rename_chain_id(chain_id):
return "v" + chain_id.replace("_renum","").lower()
chains = {
rename_chain_id(k): v for k,v in parse_pdb_chains(pdb_file).items()
}
mask_info = mask_regions(
chains,
sample_tag,
fixed_length=fixed_length,
)
sub_dir = f"{model_tag}_{sample_tag.replace('/','_')}_seed_{i}"
if extra_tag is not None:
sub_dir += f"_{extra_tag}"
output_prefix = os.path.join(
results_dir, sub_dir, "sample",
)
contigs, inpaint_seq = get_rfdiffusion_range_str(
mask_info, fixed_length,
)
print(contigs, inpaint_seq)
run_inference(
pdb_file, output_prefix, contigs, inpaint_seq,
)
run_inverse_folding(
output_prefix,
)
df = parse_to_df(
output_prefix, mask_info, model_tag, fixed_length
)
return df
def tag_to_df(i, pdb_file, results_dir, sample_tag, model_tag, fixed_length):
scheme, regions = sample_tag.split(":")
if ("h" in regions) and ("l" in regions):
dfs = {}
for c in ["h", "l"]:
rs = [r for r in regions.split("/") if c in r]
sub_tag = f"{scheme}:{'/'.join(rs)}"
dfs[c] = _tag_to_df(
i, pdb_file, results_dir, sub_tag, model_tag, fixed_length,
extra_tag=c
)
df = dfs["h"]
df["vl_sample"] = dfs["l"]["vl_sample"]
df["vl_mask"] = dfs["l"]["vl_mask"]
df["sample_tag"] = scheme + "_" + regions.replace("/", "_")
else:
df = _tag_to_df(
i, pdb_file, results_dir, sample_tag, model_tag, fixed_length
)
return df
if __name__ == "__main__":
numbering_schemes = ["chothia", "aho"]
cdr_combos = [
["hcdr1"],
["hcdr2"],
["hcdr3"],
["hcdr1", "hcdr2", "hcdr3"],
["lcdr1"],
["lcdr2"],
["lcdr3"],
]
model_tag = 'rfdiffusion'
tags = make_tags(numbering_schemes, cdr_combos)
pdb_dir = "/home/nvg7279/src/seq-struct/poas_seed_pdbs"
results_dir = "/scratch/nvg7279/rfdiffusion_results"
pdb_files = []
for i in range(0, 10):
pdb_file = os.path.join(pdb_dir, f"{i}.pdb")
new_pdb_file = os.path.join(pdb_dir, f"{i}_renum.pdb")
# renumber_pdb(pdb_file, new_pdb_file)
pdb_files.append(new_pdb_file)
dfs = []
for fixed_length in [True]:#[True, False]:
full_tag = f"{model_tag}_{'fixed' if fixed_length else 'variable'}"
for tag in tags:
for i, pdb_file in enumerate(pdb_files):
df = tag_to_df(
i, pdb_file, results_dir, tag, full_tag, fixed_length
)
print(df)
dfs.append(df)
df = pd.concat(dfs)
df.to_csv(os.path.join(results_dir, f"{model_tag}_results.csv"))