File size: 14,159 Bytes
9e6e56b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) 2025 Max Planck Institute for Polymer Research
# Licensed under the MIT license.

import os
import time
import numpy as np
import hydra
import torch
from torch.utils.data import DataLoader
import GPUtil
import subprocess
from typing import Optional
from pathlib import Path
import pandas as pd
from pytorch_lightning import Trainer
import warnings
import logging
from typing import Union
from pathlib import Path
from tqdm.auto import tqdm

from omegaconf import DictConfig, OmegaConf
from omegaconf.base import ContainerMetadata
# depending on the torch version (higher than 2.6), we need to add the DictConfig and ContainerMetadata to the safe globals:
if hasattr(torch.serialization, 'add_safe_globals'):
    torch.serialization.add_safe_globals([DictConfig, ContainerMetadata]) # needed for loading the checkpoint with weights_only=True

from gafl import experiment_utils as eu
from gafl.data import utils as du
from bbflow.analysis import utils as au
from bbflow.deployment.utils import estimate_max_batchsize, recursive_update, ckpt_path_from_tag

from bbflow.models.bbflow_module import BBFlowModule
from bbflow.data.utils import frames_from_pdb



log = eu.get_pylogger(__name__)
torch.set_float32_matmul_precision('high')

class BBFlow:

    def __init__(self, ckpt_path: Union[Path,str], cfg:dict={}, timesteps:int=20, gamma_trans:float=None, gamma_rots:float=None, progress_bar:bool=True, _pbar_kwargs={}, device:str='cuda'):
        """
        Arguments:
        ckpt_path: Path or str. Path to checkpoint file. Must contain a 'config.yaml' file in the same directory.
        cfg: dict. default:{}. Configuration dictionary. Will overwrite the configuration from the checkpoint for the entries given.
        timesteps: int or None. default:20. Number of timesteps to sample. Overwrites the configuration from the checkpoint or in cfg.
        gamma_trans: float or None. default:None. Conditional prior parameter controlling how close the prior is to the equilibrium structure. Overwrites the configuration from the checkpoint or in cfg.
        gamma_rots: float or None. default:None. Conditional prior parameter controlling how close the prior is to the equilibrium structure. Overwrites the configuration from the checkpoint or in cfg.
        progress_bar: bool. default:True. Whether to show a progress bar during sampling.
        _pbar_kwargs: dict. default:{} Additional arguments to pass to the progress bar.
        device: str. default:'cuda'. Device to use.
        """

        self.device = device

        ckpt_dir = os.path.dirname(ckpt_path)
        config_path = os.path.join(ckpt_dir, 'config.yaml')
        if not os.path.exists(ckpt_path):
            raise FileNotFoundError(f"Checkpoint file {ckpt_path} does not exist.")
        if not os.path.exists(config_path):
            raise FileNotFoundError(f"Config file {os.path.join(ckpt_dir, 'config.yaml')} does not exist.")
        
        ckpt_cfg = OmegaConf.load(config_path)

        self._pbar_kwargs = _pbar_kwargs


        # Set-up config.
        if timesteps is not None:
            assert isinstance(timesteps, int), "timesteps must be an integer."
            if not 'inference' in cfg:
                cfg['inference'] = {}
            if not 'interpolant' in cfg['inference']:
                cfg['inference']['interpolant'] = {}
            if not 'sampling' in cfg['inference']['interpolant']:
                cfg['inference']['interpolant']['sampling'] = {}
            cfg['inference']['interpolant']['sampling']['num_timesteps'] = timesteps

        default_inference_config = Path(__file__).parent.parent.parent / 'configs/inference.yaml'
        cfg_ = OmegaConf.load(default_inference_config)
        OmegaConf.set_struct(cfg_, True)

        recursive_update(cfg, cfg_)

        OmegaConf.set_struct(cfg_, False)

        OmegaConf.set_struct(ckpt_cfg, False)
        cfg = OmegaConf.merge(cfg_, ckpt_cfg)
        cfg.experiment.checkpointer.dirpath = './'

        cfg.inference.interpolant.batch_ot = ckpt_cfg.interpolant.batch_ot
        
        if gamma_rots is None:
            gamma_rots = ckpt_cfg.interpolant.prior_conditional.gamma_rots
        if gamma_trans is None:
            gamma_trans = ckpt_cfg.interpolant.prior_conditional.gamma_trans
            
        cfg.inference.interpolant.prior_conditional.gamma_rots = gamma_rots
        cfg.inference.interpolant.prior_conditional.gamma_trans = gamma_trans
        cfg.model = ckpt_cfg.model

        self._cfg = cfg
        self._cfg.interpolant = cfg.inference.interpolant
        self._cfg.interpolant.use_tqdm = False
        self._ckpt_cfg = ckpt_cfg
        self._infer_cfg = cfg.inference
        self._samples_cfg = self._infer_cfg.samples

        # set the number of devices to 1:
        self._cfg.experiment.num_devices = 1

        self._progress_bar = progress_bar

        # Read checkpoint and initialize module.
        self._flow_module = self.load_module(ckpt_path)
        self._flow_module.eval()
        self._flow_module._infer_cfg = self._infer_cfg
        self._flow_module._samples_cfg = self._samples_cfg

    @classmethod
    def from_tag(cls, tag:str='latest', cfg:dict={}, timesteps:int=20, gamma_trans:float=None, gamma_rots:float=None, progress_bar:bool=True, _pbar_kwargs={}, force_download:bool=False, device:str='cuda'):
        """
        Arguments:
        tag: str. Tag of the checkpoint to load. Searches the checkpoint at models/{tag}/*.ckpt. If not present, tries to download it.
        cfg: dict. default:{}. Configuration dictionary. Will overwrite the configuration from the checkpoint for the entries given.
        timesteps: int or None. default:None. Number of timesteps to sample. Overwrites the configuration from the checkpoint or in cfg.
        gamma_trans: float or None. default:None. Conditional prior parameter controlling how close the prior is to the equilibrium structure. Overwrites the configuration from the checkpoint or in cfg.
        gamma_rots: float or None. default:None. Conditional prior parameter controlling how close the prior is to the equilibrium structure. Overwrites the configuration from the checkpoint or in cfg.
        progress_bar: bool. default:True. Whether to show a progress bar during sampling.
        _pbar_kwargs: dict. default:{} Additional arguments to pass to the progress bar.
        force_download: bool. default:False. If True, forces the download of the checkpoint even if it is already present.
        device: str. default:'cuda'. Device to use.
        """
        ckpt_path = ckpt_path_from_tag(tag, force_download=force_download)
        return cls(ckpt_path, cfg=cfg, timesteps=timesteps, gamma_trans=gamma_trans, gamma_rots=gamma_rots, progress_bar=progress_bar, _pbar_kwargs=_pbar_kwargs, device=device)

    def load_module(self, ckpt_path):
        ckpt = torch.load(ckpt_path, map_location=torch.device(self.device), weights_only=True)
        model_ckpt = ckpt["state_dict"]
        model_ckpt = {k.replace('model.', ''): v for k, v in model_ckpt.items()}

        module = BBFlowModule(self._cfg)
        current_state = module.model.state_dict()
        compatible_state = {
            k: v for k, v in model_ckpt.items()
            if k in current_state and current_state[k].shape == v.shape
        }
        module.model.load_state_dict(compatible_state, strict=False)
        module.to(self.device)
        return module
    
    def to(self, device: str):
        self._flow_module.to(device)
        self.device = device

    def _sample_states(self, trans_eq:torch.Tensor, rotmats_eq:torch.Tensor, seq:torch.Tensor, n_samples:int=10, batch_size:int=None, device:str=None, cuda_memory_GB:int=40):
        """
        trans_equilibrium: torch.Tensor of shape (n_residues, 3)
        rotmats_equilibrium: torch.Tensor of shape (n_residues, 3, 3)
        seq: torch.Tensor of shape (n_residues, 21)
        n_samples: int
        batch_size: int or None. If not None, overrides the batch size estimation. Otherwise, the batch size is estimated based on the number of residues in the protein.
        device: str or None. If None, uses self.device.
        cuda_memory_GB: int. Maximum amount of memory to use on the GPU. Used for estimating the batch size if batch_size is None.

        Returns:
        atom37_traj: np.array of shape (n_samples, n_residues, 37, 3)
        """
        num_res = trans_eq.shape[0]
        if batch_size is None:
            batch_size = estimate_max_batchsize(n_res=num_res, memory_GB=cuda_memory_GB)
        B = batch_size

        if device is None:
            device = self.device

        if device != 'cpu':
            assert torch.cuda.is_available(), "CUDA is not available."

        self._flow_module.to(device)
        self._flow_module.interpolant.set_device(device)
        trans_eq = trans_eq.to(device)
        rotmats_eq = rotmats_eq.to(device)
        seq = seq.to(device)

        interpolant = self._flow_module.interpolant
        batches = []
        for i in range(n_samples // B):
            batches.append(B)
        if n_samples % B != 0:
            batches.append(n_samples % B)

        if self._progress_bar:
            progress_bar = tqdm(total=n_samples, desc='Sampling states', dynamic_ncols=True, **self._pbar_kwargs)

        # sample states:
        sampled_conformations = []

        with torch.no_grad():
            for b in batches:
                atom37_traj, pred_traj, _ = interpolant.sample(
                    b,
                    num_res,
                    self._flow_module.model,
                    trans_eq.repeat(b, 1, 1),
                    rotmats_eq.repeat(b, 1, 1, 1),
                    seq.repeat(b, 1, 1),
                )
                atom37_traj = du.to_numpy(torch.stack(atom37_traj, dim=1))

                # remove the c betas. they are not predicted by the network but rule based and have no real meaning
                # Masking CB atoms
                # atom37 bb order = ['N', 'CA', 'C', 'CB', 'O']
                atom37_traj[..., 3, :] = 0

                for i in range(b):
                    sampled_conformations.append(atom37_traj[i][-1])

                if self._progress_bar:
                    progress_bar.update(b)
        
        # (num_samples, num_residues, 37, 3)
        sampled_conformations = np.stack(sampled_conformations, axis=0)
        
        return sampled_conformations



    def sample(
            self,
            input_path:Union[Path,str],
            num_samples:int=10,
            output_path:Union[Path,str]=None,
            device:str=None,
            cuda_memory_GB:int=40,
            batch_size:int=None,
            output_dir:Optional[Union[Path,str]]=None,
            output_fmt:str='pdb',
            overwrite:bool=True
        ):
        """
        Loads a PDB file describing the equilibrium structure of a protein and samples num_samples conformations. Stores the sampled backbone conformations in a PDB file and returns them as array of shape (num_samples, n_residues, 37, 3).

        Arguments:
        input_path: Path or str. Path to PDB file.
        num_samples: int. Number of conformations to sample.
        output_path: Path or str. Path to output PDB file. If None, the sampled conformations are not stored.
        device: str. If None, uses self.device from the init call.
        cuda_memory_GB: int. Maximum amount of memory to use on the GPU. Used for estimating the batch size if batch_size is None.
        batch_size: int. Batch size for sampling. If None, the batch size is estimated based on the number of residues in the protein.
        output_dir: Path or str. Path to output directory, in which the pdb file with the sampled conformations will be stored as 'sampled_conformations.pdb' if no output_path is given.
        output_fmt: str. 'pdb' or 'xtc'. Format of the output file if output_dir is given.
        overwrite: bool. If True, overwrites the output file if it already exists.
        """

        # some checks:
        assert input_path is not None, "Input path must be given."
        if not str(input_path).endswith('.pdb'):
            raise ValueError(f"Input path must be a PDB file but got {input_path}.")
        
        if output_path is None and output_dir is None:
            raise ValueError("Either output_path or output_dir must be given.")
        
        if output_path is not None and output_dir is not None:
            raise ValueError("Only one of output_path or output_dir must be given.")

        # infer output path:
        if output_dir is not None:
            output_path = Path(output_dir) / f'sampled_conformations.{output_fmt}'

        output_path = Path(output_path)
        
        # print warning if the suffix of the given output path does not match the output format:
        if output_fmt != Path(output_path).suffix[1:]:
            log.warning(f"Output format {output_fmt} does not match the suffix of the given output path {output_path}.")

        if output_path.exists():
            if overwrite:
                warnings.warn(f"An output path already exists and will be overwritten.")
            else:    
                raise ValueError(f"Output path {output_path} already exists.")

        # Load trans/rotmats from PDB
        trans_eq, rotmats_eq, seq, chain_ids = frames_from_pdb(input_path)

        # Sample states
        sampled_conformations = self._sample_states(
            trans_eq=trans_eq,
            rotmats_eq=rotmats_eq,
            seq=seq,
            n_samples=num_samples,
            batch_size=batch_size,
            device=device,
            cuda_memory_GB=cuda_memory_GB
        )

        if str(output_path).endswith('.xtc'):
            raise NotImplementedError("XTC output format is not implemented yet.")
        else:

            logging.info(f"Writing sampled conformations to {output_path}")

            au.write_prot_to_pdb(
                sampled_conformations,
                str(output_path),
                aatype=seq.argmax(-1).numpy(),
                no_indexing=True,
                chain_index=chain_ids
            )