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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# Copyright Generate Biomedicines, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Models for generating protein backbone structure via diffusion.
"""

from types import SimpleNamespace
from typing import Optional, Tuple, Union

import torch
import torch.nn as nn

from chroma.data.xcs import validate_XC
from chroma.layers import basic, graph
from chroma.layers.structure import backbone, diffusion, transforms
from chroma.models.graph_design import BackboneEncoderGNN
from chroma.utility.model import load_model as utility_load_model


class GraphBackbone(nn.Module):
    """Graph-based backbone generation for protein complexes.

    GraphBackbone parameterizes a generative model of the backbone coordinates
    of protein complexes.

    Args:
        See documention of `layers.structure.protein_graph.ProteinFeatureGraph`,
        `graph.GraphNN`, `layers.structure.backbone.GraphBackboneUpdate` and
        `layers.structure.diffusion.DiffusionChainCov` for more details on
        hyperparameters.

    Inputs:
        X (Tensor): Backbone coordinates with shape
                `(num_batch, num_residues, num_atoms, 3)`.
        C (LongTensor): Chain map with shape `(num_batch, num_residues)`.

    Outputs:
        neglogp (Tensor): Sum of `neglogp_S` and `neglogp_chi`.
    """

    def __init__(
        self,
        dim_nodes: int = 128,
        dim_edges: int = 128,
        num_neighbors: int = 30,
        node_features: Tuple = (("internal_coords", {"log_lengths": True}),),
        edge_features: Tuple = (
            "distances_2mer",
            "orientations_2mer",
            "distances_chain",
        ),
        num_layers: int = 3,
        dropout: float = 0.1,
        node_mlp_layers: int = 1,
        node_mlp_dim: Optional[int] = None,
        edge_update: bool = True,
        edge_mlp_layers: int = 1,
        edge_mlp_dim: Optional[int] = None,
        skip_connect_input: bool = False,
        mlp_activation: str = "softplus",
        decoder_num_hidden: int = 512,
        graph_criterion: str = "knn",
        graph_random_min_local: int = 20,
        backbone_update_method: str = "neighbor",
        backbone_update_iterations: int = 1,
        backbone_update_num_weights: int = 1,
        backbone_update_unconstrained: bool = True,
        use_time_features: bool = True,
        time_feature_type: str = "t",
        time_log_feature_scaling: float = 0.05,
        noise_schedule: str = "log_snr",
        noise_covariance_model: str = "brownian",
        noise_beta_min: float = 0.2,
        noise_beta_max: float = 70.0,
        noise_log_snr_range: Tuple[float] = (-7.0, 13.5),
        noise_complex_scaling: bool = False,
        loss_scale: float = 10.0,
        loss_scale_ssnr_cutoff: float = 0.99,
        loss_function: str = "squared_fape",
        checkpoint_gradients: bool = False,
        prediction_type: str = "X0",
        num_graph_cycles: int = 1,
        **kwargs,
    ):
        """Initialize GraphBackbone network."""
        super(GraphBackbone, self).__init__()

        # Save configuration in kwargs
        self.kwargs = locals()
        self.kwargs.pop("self")
        for key in list(self.kwargs.keys()):
            if key.startswith("__") and key.endswith("__"):
                self.kwargs.pop(key)
        args = SimpleNamespace(**self.kwargs)

        # Important global options
        self.dim_nodes = args.dim_nodes
        self.dim_edges = args.dim_edges

        # Encoder GNN process backbone
        self.num_graph_cycles = args.num_graph_cycles
        self.encoders = nn.ModuleList(
            [
                BackboneEncoderGNN(
                    dim_nodes=args.dim_nodes,
                    dim_edges=args.dim_edges,
                    num_neighbors=args.num_neighbors,
                    node_features=args.node_features,
                    edge_features=args.edge_features,
                    num_layers=args.num_layers,
                    node_mlp_layers=args.node_mlp_layers,
                    node_mlp_dim=args.node_mlp_dim,
                    edge_update=args.edge_update,
                    edge_mlp_layers=args.edge_mlp_layers,
                    edge_mlp_dim=args.edge_mlp_dim,
                    mlp_activation=args.mlp_activation,
                    dropout=args.dropout,
                    skip_connect_input=args.skip_connect_input,
                    graph_criterion=args.graph_criterion,
                    graph_random_min_local=args.graph_random_min_local,
                    checkpoint_gradients=checkpoint_gradients,
                )
                for i in range(self.num_graph_cycles)
            ]
        )

        self.backbone_updates = nn.ModuleList(
            [
                backbone.GraphBackboneUpdate(
                    dim_nodes=args.dim_nodes,
                    dim_edges=args.dim_edges,
                    method=args.backbone_update_method,
                    iterations=args.backbone_update_iterations,
                    num_transform_weights=args.backbone_update_num_weights,
                    unconstrained=args.backbone_update_unconstrained,
                )
                for i in range(self.num_graph_cycles)
            ]
        )

        self.use_time_features = args.use_time_features
        self.time_feature_type = args.time_feature_type
        self.time_log_feature_scaling = time_log_feature_scaling
        if self.use_time_features:
            self.time_features = basic.FourierFeaturization(
                d_input=1, d_model=dim_nodes, trainable=False, scale=16.0
            )

        self.noise_perturb = diffusion.DiffusionChainCov(
            noise_schedule=args.noise_schedule,
            beta_min=args.noise_beta_min,
            beta_max=args.noise_beta_max,
            log_snr_range=args.noise_log_snr_range,
            covariance_model=args.noise_covariance_model,
            complex_scaling=args.noise_complex_scaling,
        )
        self.noise_schedule = self.noise_perturb.noise_schedule
        method = "symeig"
        self.loss_scale = args.loss_scale
        self.loss_scale_ssnr_cutoff = loss_scale_ssnr_cutoff
        self.loss_function = args.loss_function
        self.prediction_type = args.prediction_type
        self._loss_eps = 1e-5

        self.loss_diffusion = diffusion.ReconstructionLosses(
            diffusion=self.noise_perturb, rmsd_method=method, loss_scale=args.loss_scale
        )

        if self.prediction_type.startswith("scale"):
            self.mlp_W = graph.MLP(
                dim_in=args.dim_nodes, num_layers_hidden=args.node_mlp_layers, dim_out=1
            )

        # Wrap sampling functions
        _X0_func = lambda X, C, t: self.denoise(X, C, t)
        self.sample_sde = lambda C, **kwargs: self.noise_perturb.sample_sde(
            _X0_func, C, **kwargs
        )
        self.sample_baoab = lambda C, **kwargs: self.noise_perturb.sample_baoab(
            _X0_func, C, **kwargs
        )
        self.sample_ode = lambda C, **kwargs: self.noise_perturb.sample_ode(
            _X0_func, C, **kwargs
        )
        self.estimate_metrics = lambda X, C, **kwargs: self.loss_diffusion.estimate_metrics(
            _X0_func, X, C, **kwargs
        )
        self.estimate_elbo = lambda X, C, **kwargs: self.noise_perturb.estimate_elbo(
            _X0_func, X, C, **kwargs
        )
        self.estimate_pseudoelbo_X = lambda X, C, **kwargs: self.noise_perturb.estimate_pseudoelbo_X(
            _X0_func, X, C, **kwargs
        )

    def _time_features(self, t):
        h = {"t": lambda: t, "log_snr": lambda: self.noise_schedule.log_SNR(t)}[
            self.time_feature_type
        ]()

        if "log" in self.time_feature_type:
            h = self.time_log_feature_scaling * h

        time_h = self.time_features(h[:, None, None])
        return time_h

    @validate_XC()
    def denoise(
        self,
        X: torch.Tensor,
        C: torch.Tensor,
        t: Optional[Union[float, torch.Tensor]] = None,
        return_geometry: bool = False,
    ):
        if not isinstance(t, torch.Tensor):
            t = torch.Tensor([t]).float().to(X.device)
        if t.shape == torch.Size([]):
            t = t.unsqueeze(-1)

        time_h = self._time_features(t) if self.use_time_features else None
        node_h = time_h
        edge_h, edge_idx, mask_ij = [None] * 3

        # Normalize minimum average C-alpha distances
        X_update = X

        for i in range(self.num_graph_cycles):
            # Encode as graph
            node_h, edge_h, edge_idx, mask_i, mask_ij = self.encoders[i](
                X_update,
                C,
                node_h_aux=node_h,
                edge_h_aux=edge_h,
                edge_idx=edge_idx,
                mask_ij=mask_ij,
            )
            # Update backbone
            X_update, R_ji, t_ji, logit_ji = self.backbone_updates[i](
                X_update, C, node_h, edge_h, edge_idx, mask_i, mask_ij
            )

        # Shrink towards the input
        if time_h is None:
            time_h = torch.zeros(
                [node_h.shape[0], 1, node_h.shape[2]], device=node_h.device
            )
        if self.prediction_type == "scale":
            scale_shift = self.mlp_W(time_h)
            ssnr = self.noise_perturb.noise_schedule.SSNR(t)
            logit_bias = torch.logit(torch.sqrt(1 - ssnr))
            scale = torch.sigmoid(scale_shift + logit_bias[:, None, None])[..., None]
            X_update = scale * X_update + (1 - scale) * X
        elif self.prediction_type == "scale_cutoff":
            # Scale below a given hard-coded noise floor cutoff
            scale_shift = self.mlp_W(time_h)
            ssnr = self.noise_perturb.noise_schedule.SSNR(t)
            logit_bias = torch.logit(torch.sqrt(1 - ssnr))
            scale = torch.sigmoid(scale_shift + logit_bias[:, None, None])[..., None]

            # Skip connect for values of alpha close to 1
            skip = (1 - scale) * (ssnr > self.loss_scale_ssnr_cutoff).float().reshape(
                scale.shape
            )
            X_update = skip * X + (1 - skip) * X_update

        if not return_geometry:
            return X_update
        else:
            return X_update, R_ji, t_ji, logit_ji, edge_idx, mask_ij

    @validate_XC(all_atom=False)
    def _debug_plot_denoising_geometry(self, X, C, t=None):
        """Debug plots for analyzing denoising geometry"""
        if t is None:
            X_noise, t = self.noise_perturb(X, C)
        else:
            X_noise = self.noise_perturb(X, C, t=t)

        # Compute denoised geometry
        (
            X_denoise,
            R_ji_pred,
            t_ji_pred,
            logit_ji_pred,
            edge_idx,
            mask_ij,
        ) = self.denoise(X_noise, C, t, return_geometry=True)

        # Featurize other inputs and outpus
        R_ji_native, t_ji_native = self.backbone_updates[0]._inner_transforms(
            X, C, edge_idx
        )
        R_ji_noise, t_ji_noise = self.backbone_updates[0]._inner_transforms(
            X_noise, C, edge_idx
        )
        R_ji_denoise, t_ji_denoise = self.backbone_updates[0]._inner_transforms(
            X_denoise, C, edge_idx
        )

        R_ji = torch.cat([R_ji_native, R_ji_noise, R_ji_pred, R_ji_denoise], 0)
        t_ji = torch.cat([t_ji_native, t_ji_noise, t_ji_pred, t_ji_denoise], 0)
        logit_ji = torch.cat([mask_ij, mask_ij, logit_ji_pred[:, :, :, 0], mask_ij], 0)
        edge_idx = edge_idx.expand([4, -1, -1])
        from matplotlib import pyplot as plt

        transforms._debug_plot_transforms(R_ji, t_ji, logit_ji, edge_idx, mask_ij)
        plt.show()
        return X_denoise, X_noise

    @validate_XC(all_atom=False)
    def forward(
        self,
        X: torch.Tensor,
        C: torch.Tensor,
        t: Optional[Union[torch.Tensor, float]] = None,
        **kwargs,
    ):
        # If all atom structure is passed, discard side chains
        X = X[:, :, :4, :] if X.size(2) == 14 else X

        # Sample perturbed structure
        if t is None:
            X_t, t = self.noise_perturb(X, C)
        else:
            X_t = self.noise_perturb(X, C, t=t)

        X0_pred, R_ji_pred, t_ji_pred, logit_ji_pred, edge_idx, mask_ij = self.denoise(
            X_t, C, t, return_geometry=True
        )

        losses = self.loss_diffusion(X0_pred, X, C, t)

        # Per complex weights
        weights = (C > 0).float().sum(-1)

        ssnr = self.noise_perturb.noise_schedule.SSNR(t)
        prob_ssnr = self.noise_perturb.noise_schedule.prob_SSNR(ssnr)
        importance_weights = 1 / prob_ssnr

        _importance_weight = lambda h: h * importance_weights.reshape(
            [-1] + [1] * (len(h.shape) - 1)
        )
        _weighted_avg = lambda h: (weights * _importance_weight(h)).sum() / (
            weights.sum() + self._loss_eps
        )
        # Interresidue geometry predictions agreement
        if self.backbone_updates[0].method != "local":
            R_ij_mse, t_ij_mse = self.backbone_updates[0]._transform_loss(
                R_ji_pred, t_ji_pred, X, C, edge_idx, mask_ij
            )
            losses["batch_translate_mse"] = _weighted_avg(
                t_ij_mse / (self.loss_scale ** 2)
            )
            losses["batch_rotate_mse"] = _weighted_avg(R_ij_mse)
            losses["batch_transform_mse"] = (
                losses["batch_translate_mse"] + losses["batch_rotate_mse"]
            )

        losses_extend = {}
        for k, v in losses.items():
            if "elbo" in k:
                losses_extend[k.replace("elbo", "neg_elbo")] = -v
        losses.update(losses_extend)
        return losses


def load_model(
    weight_file: str,
    device: str = "cpu",
    strict: bool = False,
    strict_unexpected: bool = False,
    verbose: bool = True,
) -> GraphBackbone:
    """Load model `GraphBackbone`

    Args:
        weight_file (str): The destination path of the model weights to load.
            Compatible with files saved by `save_model`.
        device (str, optional): Pytorch device specification, e.g. `'cuda'` for
        GPU. Default is `'cpu'`.
        strict (bool): Whether to require that the keys match between the
            input file weights and the model created from the parameters stored
            in the model kwargs.
        strict_unexpected (bool): Whether to require that there are no
            unexpected keys when loading model weights, as distinct from the
            strict option which doesn't allow for missing keys either. By
            default, we use this option rather than strict for ease of
            development when adding model features.
        verbose (bool, optional): Show outputs from download and loading.
            Default True.

    Returns:
        model (GraphBackbone): Instance of `GraphBackbone` with loaded weights.
    """
    return utility_load_model(
        weight_file,
        GraphBackbone,
        device=device,
        strict=strict,
        strict_unexpected=strict_unexpected,
        verbose=verbose,
    )