model.py 12 KB
Newer Older
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
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
# Copyright 2021 AlQuraishi Laboratory
# Copyright 2021 DeepMind Technologies Limited
# 
# 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.

import torch
import torch.nn as nn

from alphafold.utils.feats import (
    pseudo_beta_fn,
    atom37_to_torsion_angles,
    build_extra_msa_feat,
    build_template_angle_feat,
    build_template_pair_feat,
    atom14_to_atom37,
)
from alphafold.model.embedders import (
    InputEmbedder, 
    RecyclingEmbedder,
    TemplateAngleEmbedder,
    TemplatePairEmbedder,
    ExtraMSAEmbedder,
)
from alphafold.model.evoformer import EvoformerStack, ExtraMSAStack
from alphafold.model.heads import AuxiliaryHeads
import alphafold.np.residue_constants as residue_constants
from alphafold.model.structure_module import StructureModule
from alphafold.model.template import (
    TemplatePairStack, 
    TemplatePointwiseAttention,
)
from alphafold.utils.loss import (
    compute_plddt,
)

from alphafold.utils.tensor_utils import (
    tensor_tree_map,
)
        

class AlphaFold(nn.Module):
    """ 
        Alphafold 2.

        Implements Algorithm 2 (but with training).
    """
    def __init__(self, config):
        """
            Args:
                config:
                    A dict-like config object (like the one in config.py)
        """
        super(AlphaFold, self).__init__()

        template_config = config.template
        extra_msa_config = config.extra_msa

        # Main trunk + structure module
        self.input_embedder = InputEmbedder(
            **config["input_embedder"],
        )
        self.recycling_embedder = RecyclingEmbedder(
            **config["recycling_embedder"],
        )
        self.template_angle_embedder = TemplateAngleEmbedder(
            **template_config["template_angle_embedder"],
        )
        self.template_pair_embedder = TemplatePairEmbedder(
            **template_config["template_pair_embedder"],
        )
        self.template_pair_stack = TemplatePairStack(
            **template_config["template_pair_stack"],
        )
        self.template_pointwise_att = TemplatePointwiseAttention(
            **template_config["template_pointwise_attention"],
        )
        self.extra_msa_embedder = ExtraMSAEmbedder(
            **extra_msa_config["extra_msa_embedder"],
        )
        self.extra_msa_stack = ExtraMSAStack(
            **extra_msa_config["extra_msa_stack"],
        )
        self.evoformer = EvoformerStack(
            **config["evoformer_stack"],
        )
        self.structure_module = StructureModule(
            **config["structure_module"],
        )

        self.aux_heads = AuxiliaryHeads(
            config["heads"],
        )

        self.config = config

    def embed_templates(self, batch, z, pair_mask):
        # Build template angle feats
        angle_feats = atom37_to_torsion_angles(
            batch["template_aatype"], 
            batch["template_all_atom_positions"], 
            batch["template_all_atom_masks"], 
            eps=1e-8
        )

        # Stow this away for later
        batch["torsion_angles_mask"] = angle_feats["torsion_angles_mask"]

        template_angle_feat = build_template_angle_feat(
            angle_feats,
            batch["template_aatype"],
        )
 
        # [*, S_t, N, C_m]
        a = self.template_angle_embedder(template_angle_feat)

        # [*, S_t, N, N, C_t]
        t = build_template_pair_feat(
            batch,
            eps=self.config.template.eps,
            **self.config.template.distogram
        )
        t = self.template_pair_embedder(t)
        t = self.template_pair_stack(
            t, 
            pair_mask.unsqueeze(-3),
            _mask_trans=self.config._mask_trans
        )

        # [*, N, N, C_z]
        t = self.template_pointwise_att(
            t, 
            z, 
            template_mask=batch["template_mask"]
        )
        t *= torch.sum(batch["template_mask"]) > 0
    
        return a, t

    def forward(self, batch):
        """
            Args:
                batch:
                    Dictionary of arguments outlined in Algorithm 2. Keys must
                    include the official names of the features in the
                    supplement subsection 1.2.9.

                    The final dimension of each input must have length equal to
                    the number of recycling iterations.

                    Features (without the recycling dimension):

                        "aatype" ([*, N_res]): 
                            Contrary to the supplement, this tensor of residue
                            indices is not one-hot.
                        "target_feat" ([*, N_res, C_tf])
                            One-hot encoding of the target sequence. C_tf is
                            config.model.input_embedder.tf_dim.
                        "residue_index" ([*, N_res])
                            Tensor whose final dimension consists of
                            consecutive indices from 0 to N_res.
                        "msa_feat" ([*, N_seq, N_res, C_msa])
                            MSA features, constructed as in the supplement.
                            C_msa is config.model.input_embedder.msa_dim.
                        "seq_mask" ([*, N_res])
                            1-D sequence mask
                        "msa_mask" ([*, N_seq, N_res])
                            MSA mask
                        "pair_mask" ([*, N_res, N_res])
                            2-D pair mask
                        "extra_msa_mask" ([*, N_extra, N_res])
                            Extra MSA mask
                        "template_mask" ([*, N_templ])
                            Template mask (on the level of templates, not 
                            residues)
                        "template_aatype" ([*, N_templ, N_res])
                            Tensor of template residue indices (indices greater
                            than 19 are clamped to 20 (Unknown))
                        "template_all_atom_pos" ([*, N_templ, N_res, 37, 3])
                            Template atom coordinates in atom37 format
                        "template_all_atom_mask" ([*, N_templ, N_res, 37])
                            Template atom coordinate mask
                        "template_pseudo_beta" ([*, N_templ, N_res, 3])
                            Positions of template carbon "pseudo-beta" atoms
                            (i.e. C_beta for all residues but glycine, for
                            for which C_alpha is used instead)
                        "template_pseudo_beta_mask" ([*, N_templ, N_res])
                            Pseudo-beta mask 
        """        
        # Recycling embeddings
        m_1_prev, z_prev, x_prev = None, None, None

        # Primary output dictionary
        outputs = {}

        # Main recycling loop
        for cycle_no in range(self.config.no_cycles):
            # Select the features for the current recycling cycle
            fetch_cur_batch = lambda t: t[..., cycle_no] 
            feats = tensor_tree_map(fetch_cur_batch, batch)

            # Grab some data about the input
            batch_dims = feats["target_feat"].shape[:-2]
            n = feats["target_feat"].shape[-2]
            n_seq = feats["msa_feat"].shape[-3]
            device = feats["target_feat"].device

            # Prep some features
            seq_mask = feats["seq_mask"]
            pair_mask = seq_mask[..., None] * seq_mask[..., None, :]
            msa_mask = feats["msa_mask"]

            # Initialize the MSA and pair representations

            # m: [*, S_c, N, C_m]
            # z: [*, N, N, C_z]
            m, z = self.input_embedder(
                feats["target_feat"], 
                feats["residue_index"], 
                feats["msa_feat"],
            )

            # Inject information from previous recycling iterations
            if(self.config.no_cycles > 1):
                # Initialize the recycling embeddings, if needs be
                if(None in [m_1_prev, z_prev, x_prev]):
                    # [*, N, C_m]
                    m_1_prev = torch.zeros(
                        (*batch_dims, n, self.config.c_m), 
                        requires_grad=False, 
                        device=device,
                    )

                    # [*, N, N, C_z]
                    z_prev = torch.zeros(
                        (*batch_dims, n, n, self.config.c_z),
                        requires_grad=False,
                        device=device,
                    )

                    # [*, N, 3]
                    x_prev = torch.zeros(
                        (*batch_dims, n, residue_constants.atom_type_num, 3),
                        requires_grad=False,
                        device=device,
                    )

                x_prev = pseudo_beta_fn(
                    feats["aatype"],
                    x_prev,
                    None  # TODO: figure this part out
                )

                # m_1_prev_emb: [*, N, C_m]
                # z_prev_emb: [*, N, N, C_z]
                m_1_prev_emb, z_prev_emb = self.recycling_embedder(
                    m_1_prev, 
                    z_prev, 
                    x_prev,
                )

                # [*, S_c, N, C_m]
                m[..., 0, :, :] += m_1_prev_emb

                # [*, N, N, C_z]
                z += z_prev_emb

            # Embed the templates + merge with MSA/pair embeddings
            if(self.config.template.enabled):
                a, t = self.embed_templates(feats, z, pair_mask)

                # [*, N, N, C_z]
                z += t
 
                if(self.config.template.embed_angles):
                    # [*, S = S_c + S_t, N, C_m]
                    m = torch.cat([m, a], dim=-3)

                    # [*, S, N]
                    torsion_angles_mask = feats["torsion_angles_mask"]
                    msa_mask = torch.cat(
                        [feats["msa_mask"], torsion_angles_mask[..., 2]], axis=-2
                    )

            # Embed extra MSA features + merge with pairwise embeddings 
            if(self.config.extra_msa.enabled):
                # [*, S_e, N, C_e]
                a = self.extra_msa_embedder(build_extra_msa_feat(feats))
           
                # [*, N, N, C_z]
                z = self.extra_msa_stack(
                    a, 
                    z, 
                    msa_mask=feats["extra_msa_mask"],
                    pair_mask=pair_mask,
                    _mask_trans=self.config._mask_trans,
                )

            # Run MSA + pair embeddings through the trunk of the network
            # m: [*, S, N, C_m]
            # z: [*, N, N, C_z]
            # s: [*, N, C_s]
            m, z, s = self.evoformer(
                m, 
                z, 
                msa_mask=msa_mask, 
                pair_mask=pair_mask,
                _mask_trans=self.config._mask_trans
            )

            outputs["msa"] = m[..., :n_seq, :, :]
            outputs["pair"] = z
            outputs["single"] = s

            # Predict 3D structure
            outputs["sm"] = self.structure_module(
                s, z, feats["aatype"], mask=feats["seq_mask"],
            )        
            outputs["final_atom_positions"] = atom14_to_atom37(
                outputs["sm"]["positions"][-1], feats
            )
            outputs["final_atom_mask"] = feats["atom37_atom_exists"]

            # Save embeddings for use during the next recycling iteration 

            # [*, N, C_m]
            m_1_prev = m[..., 0, :, :]

            # [* N, N, C_z]
            z_prev = z

            # [*, N, 3]
            x_prev = outputs["final_atom_positions"]

        outputs.update(self.aux_heads(outputs))

        return outputs