forked from microsoft/bioemu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.py
More file actions
273 lines (234 loc) · 10.7 KB
/
sample.py
File metadata and controls
273 lines (234 loc) · 10.7 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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
"""Script for sampling from a trained model."""
import logging
import typing
from collections.abc import Callable
from pathlib import Path
from typing import Literal
import hydra
import numpy as np
import torch
import yaml
from torch_geometric.data.batch import Batch
from tqdm import tqdm
from .chemgraph import ChemGraph
from .convert_chemgraph import save_pdb_and_xtc
from .get_embeds import get_colabfold_embeds
from .model_utils import load_model, load_sdes, maybe_download_checkpoint
from .sde_lib import SDE
from .seq_io import check_protein_valid, parse_sequence, write_fasta
from .utils import (
count_samples_in_output_dir,
format_npz_samples_filename,
print_traceback_on_exception,
)
logger = logging.getLogger(__name__)
DEFAULT_DENOISER_CONFIG_DIR = Path(__file__).parent / "config/denoiser/"
SupportedDenoisersLiteral = Literal["dpm", "heun"]
SUPPORTED_DENOISERS = list(typing.get_args(SupportedDenoisersLiteral))
@print_traceback_on_exception
@torch.no_grad()
def main(
sequence: str | Path,
num_samples: int,
output_dir: str | Path,
batch_size_100: int = 10,
model_name: str | None = "bioemu-v1.0",
ckpt_path: str | Path | None = None,
model_config_path: str | Path | None = None,
denoiser_type: SupportedDenoisersLiteral | None = "dpm",
denoiser_config_path: str | Path | None = None,
cache_embeds_dir: str | Path | None = None,
cache_so3_dir: str | Path | None = None,
msa_host_url: str | None = None,
filter_samples: bool = True,
) -> None:
"""
Generate samples for a specified sequence, using a trained model.
Args:
sequence: Amino acid sequence for which to generate samples, or a path to a .fasta file, or a path to an .a3m file with MSAs.
If it is not an a3m file, then colabfold will be used to generate an MSA and embedding.
num_samples: Number of samples to generate. If `output_dir` already contains samples, this function will only generate additional samples necessary to reach the specified `num_samples`.
output_dir: Directory to save the samples. Each batch of samples will initially be dumped as .npz files. Once all batches are sampled, they will be converted to .xtc and .pdb.
batch_size_100: Batch size you'd use for a sequence of length 100. The batch size will be calculated from this, assuming
that the memory requirement to compute each sample scales quadratically with the sequence length.
model_name: Name of pretrained model to use. The model will be retrieved from huggingface. If not set,
this defaults to `bioemu-v1.0`. If this is set, you do not need to provide `ckpt_path` or `model_config_path`.
ckpt_path: Path to the model checkpoint. If this is set, `model_name` will be ignored.
model_config_path: Path to the model config, defining score model architecture and the corruption process the model was trained with.
Only required if `ckpt_path` is set.
denoiser_type: Denoiser to use for sampling, if `denoiser_config_path` not specified. Comes in with default parameter configuration. Must be one of ['dpm', 'heun']
denoiser_config_path: Path to the denoiser config, defining the denoising process.
cache_embeds_dir: Directory to store MSA embeddings. If not set, this defaults to `COLABFOLD_DIR/embeds_cache`.
cache_so3_dir: Directory to store SO3 precomputations. If not set, this defaults to `~/sampling_so3_cache`.
msa_host_url: MSA server URL. If not set, this defaults to colabfold's remote server. If sequence is an a3m file, this is ignored.
filter_samples: Filter out unphysical samples with e.g. long bond distances or steric clashes.
"""
output_dir = Path(output_dir).expanduser().resolve()
output_dir.mkdir(parents=True, exist_ok=True) # Fail fast if output_dir is non-writeable
ckpt_path, model_config_path = maybe_download_checkpoint(
model_name=model_name, ckpt_path=ckpt_path, model_config_path=model_config_path
)
score_model = load_model(ckpt_path, model_config_path)
sdes = load_sdes(model_config_path=model_config_path, cache_so3_dir=cache_so3_dir)
# User may have provided an MSA file instead of a sequence. This will be used for embeddings.
msa_file = sequence if str(sequence).endswith(".a3m") else None
if msa_file is not None and msa_host_url is not None:
logger.warning(f"msa_host_url is ignored because MSA file {msa_file} is provided.")
# Parse FASTA or A3M file if sequence is a file path. Extract the actual sequence.
sequence = parse_sequence(sequence)
# Check input sequence is valid
check_protein_valid(sequence)
fasta_path = output_dir / "sequence.fasta"
if fasta_path.is_file():
if parse_sequence(fasta_path) != sequence:
raise ValueError(
f"{fasta_path} already exists, but contains a sequence different from {sequence}!"
)
else:
# Save FASTA file in output_dir
write_fasta([sequence], fasta_path)
if denoiser_config_path is None:
assert (
denoiser_type in SUPPORTED_DENOISERS
), f"denoiser_type must be one of {SUPPORTED_DENOISERS}"
denoiser_config_path = DEFAULT_DENOISER_CONFIG_DIR / f"{denoiser_type}.yaml"
with open(denoiser_config_path) as f:
denoiser_config = yaml.safe_load(f)
denoiser = hydra.utils.instantiate(denoiser_config)
logger.info(
f"Sampling {num_samples} structures for sequence of length {len(sequence)} residues..."
)
batch_size = int(batch_size_100 * (100 / len(sequence)) ** 2)
if batch_size == 0:
logger.warning(f"Sequence {sequence} may be too long. Attempting with batch_size = 1.")
batch_size = 1
logger.info(f"Using batch size {min(batch_size, num_samples)}")
existing_num_samples = count_samples_in_output_dir(output_dir)
logger.info(f"Found {existing_num_samples} previous samples in {output_dir}.")
for seed in tqdm(
range(existing_num_samples, num_samples, batch_size), desc="Sampling batches..."
):
n = min(batch_size, num_samples - seed)
npz_path = output_dir / format_npz_samples_filename(seed, n)
if npz_path.exists():
raise ValueError(
f"Not sure why {npz_path} already exists when so far only {existing_num_samples} samples have been generated."
)
logger.info(f"Sampling {seed=}")
batch = generate_batch(
score_model=score_model,
sequence=sequence,
sdes=sdes,
batch_size=min(batch_size, n),
seed=seed,
denoiser=denoiser,
cache_embeds_dir=cache_embeds_dir,
msa_file=msa_file,
msa_host_url=msa_host_url,
)
batch = {k: v.cpu().numpy() for k, v in batch.items()}
np.savez(npz_path, **batch, sequence=sequence)
logger.info("Converting samples to .pdb and .xtc...")
samples_files = sorted(list(output_dir.glob("batch_*.npz")))
sequences = [np.load(f)["sequence"].item() for f in samples_files]
if set(sequences) != {sequence}:
raise ValueError(f"Expected all sequences to be {sequence}, but got {set(sequences)}")
positions = torch.tensor(np.concatenate([np.load(f)["pos"] for f in samples_files]))
node_orientations = torch.tensor(
np.concatenate([np.load(f)["node_orientations"] for f in samples_files])
)
save_pdb_and_xtc(
pos_nm=positions,
node_orientations=node_orientations,
topology_path=output_dir / "topology.pdb",
xtc_path=output_dir / "samples.xtc",
sequence=sequence,
filter_samples=filter_samples,
)
logger.info(f"Completed. Your samples are in {output_dir}.")
def get_context_chemgraph(
sequence: str,
cache_embeds_dir: str | Path | None = None,
msa_file: str | Path | None = None,
msa_host_url: str | None = None,
) -> ChemGraph:
n = len(sequence)
single_embeds_file, pair_embeds_file = get_colabfold_embeds(
seq=sequence,
cache_embeds_dir=cache_embeds_dir,
msa_file=msa_file,
msa_host_url=msa_host_url,
)
single_embeds = torch.from_numpy(np.load(single_embeds_file))
pair_embeds = torch.from_numpy(np.load(pair_embeds_file))
assert pair_embeds.shape[0] == pair_embeds.shape[1] == n
assert single_embeds.shape[0] == n
assert len(single_embeds.shape) == 2
_, _, n_pair_feats = pair_embeds.shape # [seq_len, seq_len, n_pair_feats]
pair_embeds = pair_embeds.view(n**2, n_pair_feats)
edge_index = torch.cat(
[
torch.arange(n).repeat_interleave(n).view(1, n**2),
torch.arange(n).repeat(n).view(1, n**2),
],
dim=0,
)
pos = torch.full((n, 3), float("nan"))
node_orientations = torch.full((n, 3, 3), float("nan"))
return ChemGraph(
edge_index=edge_index,
pos=pos,
node_orientations=node_orientations,
single_embeds=single_embeds,
pair_embeds=pair_embeds,
sequence=sequence,
)
def generate_batch(
score_model: torch.nn.Module,
sequence: str,
sdes: dict[str, SDE],
batch_size: int,
seed: int,
denoiser: Callable,
cache_embeds_dir: str | Path | None,
msa_file: str | Path | None = None,
msa_host_url: str | None = None,
) -> dict[str, torch.Tensor]:
"""Generate one batch of samples, using GPU if available.
Args:
score_model: Score model.
sequence: Amino acid sequence.
sdes: SDEs defining corruption process. Keys should be 'node_orientations' and 'pos'.
embeddings_file: Path to embeddings file.
batch_size: Batch size.
seed: Random seed.
msa_file: Optional path to an MSA A3M file.
msa_host_url: MSA server URL for colabfold.
"""
torch.manual_seed(seed)
context_chemgraph = get_context_chemgraph(
sequence=sequence,
cache_embeds_dir=cache_embeds_dir,
msa_file=msa_file,
msa_host_url=msa_host_url,
)
context_batch = Batch.from_data_list([context_chemgraph] * batch_size)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
sampled_chemgraph_batch = denoiser(
sdes=sdes,
device=device,
batch=context_batch,
score_model=score_model,
)
assert isinstance(sampled_chemgraph_batch, Batch)
sampled_chemgraphs = sampled_chemgraph_batch.to_data_list()
pos = torch.stack([x.pos for x in sampled_chemgraphs]).to("cpu")
node_orientations = torch.stack([x.node_orientations for x in sampled_chemgraphs]).to("cpu")
return {"pos": pos, "node_orientations": node_orientations}
if __name__ == "__main__":
import logging
import fire
logging.basicConfig(level=logging.DEBUG)
fire.Fire(main)