AlphaFold2 from scratch (or not)

In progressSlopbiology
Last reviewed22 min read

This article is my attempt to reconstruct AlphaFold2 from the ground up - or at least from the lowest level I currently understand. I want to know what information enters the system, what each learned representation can express, how those representations communicate, and how the network finally commits to three-dimensional coordinates.

A short story about AlphaFold2

AlphaFold2 was entered in the CASP14 blind assessment in 2020 CASP (Critical Assessment of Structure Prediction) is a community wide experiment to determine and advance the state of the art in modeling protein structure from amino acid sequence. Every two years, participants are invited to submit models for a set of proteins for which the experimental structures are not yet public. Independent assessors then compare the models with experiment. CASP14 means this assessment was held for 14th time.. Its authors described it as the first computational method, to their knowledge, capable of predicting protein structures to near-experimental accuracy in a majority of cases. Across the CASP14 domains, the median root-mean-square deviation of Cα\ce{C}\alpha positions over the best-aligned 95% of residues was 0.96A˚0.96\,\text{\AA}, compared with 2.8A˚2.8\,\text{\AA} for the next-best method [Jumper et al., 2021]. Those numbers do not mean that every AlphaFold2 prediction has atomic accuracy. They locate the claim in a particular assessment, on particular targets, under a particular metric.

In 2024, Demis Hassabis and John Jumper jointly received half of the Nobel Prize in Chemistry “for protein structure prediction”; David Baker received the other half for computational protein design [Nobel Prize, 2024]. AlphaFold2 was central to that recognition, but an AI system did not literally receive the prize—the people did. I still find it remarkable that understanding a neural architecture leads us into work recognized by the Nobel Committee in chemistry.

That success does not mean AlphaFold2 simulates the physical trajectory by which a protein folds, determines protein function from structure alone, or removes the need for experiments. It predicts a structural model from sequence-derived evidence. In this article, AlphaFold2 means the original monomer system described in the 2021 paper and its supplementary methods; later additions such as the pTM variants will be identified when they matter.

The biological prerequisite develops the protein chemistry and geometry needed here: amino-acid residues, peptide bonds, torsion angles, long-range contacts, chirality, and residue-local coordinate frames. I will use those ideas rather than derive them again. Our question is now specific to AlphaFold2:

How does AlphaFold2 turn sequence-derived evidence into one geometrically explicit structural hypothesis?

Write the target sequence as A=(a1,,aN)A=(a_1,\ldots,a_N), where aia_i is the residue type at position ii. The end product is a set of predicted heavy-atom coordinates together with estimates of confidence. If residue type aia_i has heavy-atom set H(ai)\mathcal{H}(a_i), we can denote the coordinate output by

X^={x^i,aR3  |  i{1,,N}, aH(ai)}.\widehat{X}=\left\{\widehat{x}_{i,a}\in\mathbb{R}^3 \;\middle|\; i\in\{1,\ldots,N\},\ a\in\mathcal{H}(a_i)\right\}.

This notation suppresses an implementation detail: the network packs atoms into fixed-size arrays and uses existence masks for atoms absent from a residue. It tells us what eventually comes out, but not how the system gets there. The difficult part is constructing useful intermediate states between a one-dimensional sequence and a three-dimensional structure.

The model sees more than one sequence

At the user boundary, inference starts with a target amino-acid sequence, usually supplied in FASTA format. Before the neural network runs, an external data pipeline searches sequence and structure databases. It produces three sources of evidence:

  • the target sequence whose structure we want;
  • a multiple sequence alignment (MSA) of evolutionarily related sequences;
  • zero or more structural templates aligned to the target.
Target sequence, MSA, and templates entering the AlphaFold2 data pipeline

The target sequence, MSA, and templates provide different kinds of evidence to the model. Source: [Mandon, 2024]

These database searches are part of the AlphaFold2 prediction system, but they are not learned layers of the neural network. This boundary matters. “AlphaFold2 takes a sequence” is correct from the user’s point of view; “the AlphaFold2 network consumes only a sequence” is not.

Training begins from different sources. Experimentally determined structures and their sequences, principally obtained from PDB mmCIF files, provide supervised examples; the final model also trains on a larger self-distillation set of structures predicted by an earlier model. I will keep inference preprocessing, network computation, and training supervision separate throughout the article.

Multiple sequence alignment: evolutionary evidence

A multiple sequence alignment places evolutionarily related protein sequences into a common coordinate system. Each row is one sequence; each column is intended to collect residues that correspond across those sequences.

CODE
Target: M G S A K E
Homolog 1: M G T A K E
Homolog 2: M - S V K E

Without alignment, an insertion or deletion can shift every later array index. Comparing raw position 4 in two sequences would then compare residues that do not play corresponding roles. The alignment makes the intended correspondence explicit.

After the alignment has been projected onto the target’s columns, if the selected MSA contains SS sequences and the target contains NN residues, its model-facing residue identities can be pictured as

MAgapS×N,M\in\mathcal{A}_{\mathrm{gap}}^{S\times N},

where Agap\mathcal{A}_{\mathrm{gap}} contains amino-acid symbols and a gap symbol. The network-facing residue axis remains tied to the NN target positions. Gap state and deletion counts associated with those positions are stored in feature channels; insertions do not create a longer residue axis for the model.

Why should other sequences help predict the target’s structure? Evolution supplies repeated natural perturbations of a related protein family. Two simple patterns illustrate the evidence available in an alignment.

Conservation

Conservation

Conservation signal in the MSA. Source: [Mandon, 2024]

The highlighted column barely changes across the sampled sequences. This suggests that substitutions at this position were often unfavorable in the evolutionary contexts represented by the alignment.

Conservation does not tell us why the position is constrained. The residue may contribute to core packing, catalysis, binding, a disulfide bond, or another structural or functional requirement. It is evidence that the position matters, not a complete explanation.

Correlated variation

Co-evolution

Co-evolution signal in the MSA. Source: [Mandon, 2024]

Let the two highlighted columns be target positions ii and jj. The important observation is not one particular substitution. It is that the residue appearing at ii is statistically associated with the residue appearing at jj across the alignment.

One possible explanation is a structural constraint: if the two residues interact in the folded protein, a change at one position may favor a compensating change at the other. But correlated columns do not prove a direct contact. Correlations can also arise through indirect couplings, shared ancestry, and sampling bias.

AlphaFold2 does not receive a hand-computed table that declares which correlations are contacts. During training, its masked-MSA objective encourages the network to learn dependencies within the alignment while the structural objectives decide which learned patterns are useful for prediction [Jumper et al., 2021].

Important

An MSA contains evidence about constraints among sequence positions. It does not contain a finished three-dimensional structure, and no individual correlation should be read as proof of a spatial contact.

The data pipeline may also find experimentally determined structures whose sequences can be aligned to the target. These templates provide direct but incomplete geometric evidence: aligned residue identities, backbone torsions, pairwise distances, and masks indicating which coordinates are available.

A template is not simply copied into the output. It may cover only part of the target, differ at important residues, or represent a relative whose structure does not transfer exactly. AlphaFold2 embeds template features into the same learned states that will also receive sequence and MSA evidence.

From biological records to numerical features

AlphaFold2 feature

Feature extraction pipeline. Source: [Jumper et al., 2021]

The neural network does not operate directly on FASTA text, alignment files, or PDB coordinate records. The original supplementary methods aggregate them into six main numerical inputs. Here, NresN_{\text{res}} is the number of target residues, NclustN_{\text{clust}} the number of MSA cluster centres, Nextra_seqN_{\text{extra\_seq}} the number of additional unclustered sequences, and NtemplN_{\text{templ}} the number of templates:

Model inputShapeWhat the model needs from it
target_featNres×21N_{\text{res}}\times 21Amino-acid identity of each target residue
residue_indexNresN_{\text{res}}Position of each residue in the original chain
msa_featNclust×Nres×49N_{\text{clust}}\times N_{\text{res}}\times 49Sampled MSA cluster centres, deletion information and cluster profiles
extra_msa_featNextra_seq×Nres×25N_{\text{extra\_seq}}\times N_{\text{res}}\times 25Additional MSA residue tokens, a deletion flag, and a transformed deletion count
template_pair_featNtempl×Nres×Nres×88N_{\text{templ}}\times N_{\text{res}}\times N_{\text{res}}\times 88Pairwise template geometry and residue information
template_angle_featNtempl×Nres×51N_{\text{templ}}\times N_{\text{res}}\times 51Template residue types and torsion-angle information

These arrays are input features, not the internal representations processed by the main Evoformer stack. Their dimensions also carry semantics. The MSA features need a sequence axis and a target-residue axis. Template pair features need two target-residue axes because they describe a relationship between positions ii and jj. This table is a view of the six aggregated inputs in the supplementary description; masks and other bookkeeping features also accompany them wherever observations are missing or padded [Jumper et al., 2021].

Two learned views of the same protein

AlphaFold2 begins its main computation with two learned representations [Jumper et al., 2021]:

RepresentationShape in the original modelState attached to
Clustered-MSA representation msim_{si} before template rows are addedNclust×Nres×256N_{\text{clust}}\times N_{\text{res}}\times 256sequence row ss at target position ii
Pair representation zijz_{ij}Nres×Nres×128N_{\text{res}}\times N_{\text{res}}\times 128ordered target-residue pair (i,j)(i,j)

The word representation matters. An element msim_{si} is no longer an amino-acid symbol, and zijz_{ij} is not a predicted distance. Each is a learned feature vector whose meaning is distributed across its channels and refined by later layers.

The two arrays organize different questions:

  • msim_{si} asks what the network currently needs to retain about sequence ss at target-aligned position ii.
  • zijz_{ij} asks what the network currently needs to retain about the relationship from target residue ii to target residue jj.

The pair entries are indexed by ordered pairs. We should therefore not assume that zij=zjiz_{ij}=z_{ji}, even when a physical quantity such as distance is symmetric. Directional pair features can carry orientation-dependent information.

Initializing the MSA representation

Let fsimsaR49f^{\text{msa}}_{si}\in\mathbb{R}^{49} be the clustered-MSA feature vector and fitargetR21f^{\text{target}}_i\in\mathbb{R}^{21} the target amino-acid feature. AlphaFold2 projects both into the common MSA channel dimension and adds them:

msi(0)=Wmsafsimsa+WtargetfitargetR256.m^{(0)}_{si} =W_{\text{msa}}f^{\text{msa}}_{si} +W_{\text{target}}f^{\text{target}}_i \in\mathbb{R}^{256}.

The target projection is shared across the sequence rows. Every MSA cell therefore begins with both row-specific alignment information and the identity of the target residue to which that column corresponds.

Initializing the pair representation

The target features are also projected in two different roles and combined across each ordered pair:

zij(0)=Wleftfitarget+Wrightfjtarget+Wrelonehot ⁣(clip(rirj,32,32))R128.z^{(0)}_{ij} =W_{\text{left}}f^{\text{target}}_i +W_{\text{right}}f^{\text{target}}_j +W_{\text{rel}}\operatorname{onehot}\!\left( \operatorname{clip}(r_i-r_j,-32,32) \right) \in\mathbb{R}^{128}.

Here rir_i is the supplied residue_index for target position ii. The first two terms identify the residues at the two ends of the pair. The final term records their signed residue-index separation, clipped to the range [32,32][-32,32]. In a contiguous single chain, this is simply iji-j. Positions separated by more than 32 residues therefore share the same initial long-range bin. The model knows that they are far apart in sequence without assigning a distinct embedding to every possible separation.

At this point, zij(0)z^{(0)}_{ij} has not yet extracted a coevolutionary relationship between MSA columns ii and jj. That communication happens later: the extra-MSA stack and the main Evoformer use outer-product updates to move information from MSA states into pair states.

Adding templates and the extra MSA

The two initial arrays are then enriched through specialized paths [Jumper et al., 2021]:

  • Template angle features are embedded and concatenated to the MSA representation as additional rows.
  • Template pair features pass through a template pair stack and pointwise attention before their update is added to zijz_{ij}.
  • The much larger set of unclustered MSA sequences is processed by a smaller four-block extra-MSA stack. Its final MSA activations are discarded, but the information it transfers into zijz_{ij} is retained.

After template-angle rows are concatenated, the MSA sequence axis entering the main Evoformer has size Nseq=Nclust+NtemplN_{\text{seq}}=N_{\text{clust}}+N_{\text{templ}} when templates are present. This division is computational as well as conceptual. The model preserves a manageable set of rich MSA rows for the 48-block main Evoformer while still allowing many additional homologous sequences to inform residue-pair relationships.

Intuition

The MSA representation keeps evolutionary evidence arranged by sequence and position. The pair representation keeps the model’s evolving account of how target positions relate. AlphaFold2’s central trunk is built around letting these two views repeatedly correct one another.

We now have the state that enters the main Evoformer: an MSA representation msim_{si} and a pair representation zijz_{ij}. The next question is no longer “what are the inputs?” It is what must one Evoformer block do so that evolutionary evidence becomes geometrically useful?

How Evoformer updates the representations

From learned representations to three-dimensional structure

After the final Evoformer block, AlphaFold2 projects the first, target-sequence row of the MSA representation into a per-residue single representation siR384s_i\in\mathbb{R}^{384}. The structure module receives this single representation together with the final pair representation zijz_{ij}. It must turn these abstract learned states into explicit geometry.

The structure module’s first representation choice is how to describe the pose of an individual residue.

A local backbone frame

A coordinate frame gives the backbone atoms N\ce{N}, Cα\ce{C\alpha}, and C\ce{C} a local coordinate system. For residue ii, AlphaFold2 represents the backbone frame as a rigid transformation

Ti=(Ri,ti)SE(3),T_i=(R_i,t_i)\in \text{SE}(3),

where RiSO(3)R_i\in\text{SO}(3) is a rotation and tiR3t_i\in\mathbb{R}^3 is a translation. The transform maps a local point into the global coordinate system:

xglobal=Tixlocal=Rixlocal+ti.\mathbf{x}_{\mathrm{global}}=T_i\circ\mathbf{x}_{\mathrm{local}}=R_i\mathbf{x}_{\mathrm{local}}+t_i.

The notation SE(3)\text{SE}(3) records that TiT_i preserves distances and orientation.

AlphaFold2 uses the Cα\ce{C\alpha} position as tit_i. Its ground-truth frame orientation is constructed from the positions of N\ce{N}, Cα\ce{C\alpha}, and C\ce{C} by Gram–Schmidt orthonormalization [Jumper et al., 2021]. If

v1=xCxCα,v2=xNxCα,\begin{aligned} v_1 &= x_{\ce{C}}-x_{\ce{C\alpha}},\\ v_2 &= x_{\ce{N}}-x_{\ce{C\alpha}}, \end{aligned}

then the basis is

e1=v1v1,u2=v2e1(e1v2),e2=u2u2,e3=e1×e2,Ri=[e1 e2 e3],ti=xCα.\begin{aligned} e_1 &= \frac{v_1}{\lVert v_1\rVert},\\ u_2 &= v_2-e_1(e_1^\top v_2),\\ e_2 &= \frac{u_2}{\lVert u_2\rVert},\\ e_3 &= e_1\times e_2, \end{aligned} \qquad R_i=[e_1\ e_2\ e_3], \qquad t_i=x_{\ce{C\alpha}}.

The construction fixes a reproducible, right-handed local orientation for every non-degenerate backbone geometry. A global reflection of the finished structure cannot be obtained from the original by one proper rigid motion in SE(3)\text{SE}(3); this distinction later matters to frame-aligned supervision.

Why AlphaFold2 predicts frames and torsions together

AlphaFold2 does not simulate the physical process by which a chain folds. Its structure module iteratively refines a geometric hypothesis. The concrete part of that hypothesis is one independently movable backbone frame per residue—the paper calls this collection a residue gas [Jumper et al., 2021].

At the beginning of the structure module, every frame is initialized to the identity transform, so the computational state places all residues at the same origin with the same orientation. This is an initialization of the model’s geometric hypothesis, not a claim that a physical protein passes through such a configuration. Each of the module’s eight weight-shared layers then performs four relevant operations:

  1. Invariant point attention updates the per-residue single representation using the single, pair, and current geometric representations.
  2. A transition network further updates the single representation.
  3. A learned local rigid update is composed with each backbone frame.
  4. A small residual torsion head predicts seven two-component torsion vectors for (pre-ω,ϕ,ψ,χ1,χ2,χ3,χ4)(\operatorname{pre}\text{-}\omega,\phi,\psi,\chi_1,\chi_2,\chi_3,\chi_4) from the current and initial single representations.

In compact notation, one layer looks roughly like

sisi+IPAi({sj},{zij},{Tj}),sisi+Transition(si),TiTiΔT(si),α~iTorsionHead(si,siinitial).\begin{aligned} s_i &\leftarrow s_i+\operatorname{IPA}_i(\{s_j\},\{z_{ij}\},\{T_j\}),\\ s_i &\leftarrow s_i+\operatorname{Transition}(s_i),\\ T_i &\leftarrow T_i\circ\Delta T(s_i),\\ \widetilde{\alpha}_i &\leftarrow \operatorname{TorsionHead}(s_i,s_i^{\mathrm{initial}}). \end{aligned}

Here sis_i is the current single representation for residue ii, zijz_{ij} is the pair representation from residue ii to residue jj, and ΔT(si)\Delta T(s_i) is a rotation-and-translation update expressed in residue ii‘s local frame. The equations suppress normalization, residual details, dropout, masking, and intermediate losses so that the information flow remains visible.

The frame update predicts a translation and three components of a quaternion whose scalar component is fixed to one before normalization. This produces a valid rotation while biasing each composed update toward a small rotation. Each torsion is predicted as a two-dimensional vector that is normalized onto the unit circle, rather than as a scalar in [0,2π)[0,2\pi). Interpreting that vector as (cosθ,sinθ)(\cos\theta,\sin\theta) avoids an artificial discontinuity where 00 and 2π2\pi denote the same angle. Valid torsion slots are masked as needed; χ\chi angles have residue-specific masks, and periodic or symmetric cases require alternative targets. Residue-specific rigid-group tables ensure that nonexistent side-chain groups contribute no atoms, while backbone geometry is also supervised through frame-aligned losses [Jumper et al., 2021].

After the eighth layer, AlphaFold2 constructs atom coordinates from the final backbone frames, predicted torsions, residue identities, and tables of idealized bond lengths and angles. Residue-type-specific ideal transforms generate the backbone rigid group containing N\ce{N}, Cα\ce{C\alpha}, C\ce{C}, and—except for glycine—Cβ\ce{C\beta}. The ψ\psi rigid group places the carbonyl oxygen, while the χ\chi groups successively place side-chain atoms. In AlphaFold2’s particular construction, no predicted heavy-atom position depends on its pre-ω\operatorname{pre}\text{-}\omega or ϕ\phi rigid groups, although those frames still participate in supervision through the frame-aligned loss [Jumper et al., 2021].

The backbone frames and torsions are predicted states, not independently learned model parameters. Each residue frame has six geometric degrees of freedom, and the torsion head predicts additional angle vectors, but the same structure-module weights produce those values for every residue. Frames give the network a local geometric language in which it can reason about relative positions while deterministically reconstructing chemically standardized groups of atoms.

What happens under a global rotation or translation?

Suppose we apply the same rigid motion GSE(3)G\in\text{SE}(3) to the entire predicted structure. Every residue frame becomes

Ti=GTi.T_i' = G\circ T_i.

The absolute coordinates change, but the relationship between two frames does not:

(Ti)1Tj=(GTi)1(GTj)=Ti1Tj.(T_i')^{-1}\circ T_j'=(G\circ T_i)^{-1}\circ(G\circ T_j)=T_i^{-1}\circ T_j.

AlphaFold2 uses this structure deliberately. Distances between IPA query and key points are unchanged by a shared rigid motion, so the attention update is invariant to the arbitrary global coordinate frame. The predicted frame updates are applied locally, making the resulting structure update equivariant: rotate and translate the input frames, and the updated frames rotate and translate in the same way. The frame-aligned point error follows the same principle when comparing predicted and experimental structures [Jumper et al., 2021].

Important

The frames are a representation of AlphaFold2’s current structural hypothesis, not snapshots of a physical folding trajectory. Idealized local geometry fixes atoms within rigid groups, but independently predicted residue frames can still violate peptide-bond geometry or create clashes. As a preview of later system stages, a structural-violation loss is introduced during fine-tuning, and optional Amber relaxation can reduce remaining violations after the network has produced its coordinates.

We now know what geometric state the structure module updates: one frame per residue, followed by torsion-controlled rigid groups. To understand how the learned single and pair representations move those frames, we next need to derive invariant point attention.

References

  1. [Jumper et al., 2021]
    Highly Accurate Protein Structure Prediction with AlphaFold[DOI]
    Jumper, John, Evans, Richard, Pritzel, Alexander, et al., 2021. Nature, vol. 596, pp. 583--589
  2. [Nobel Prize, 2024]
    They cracked the code for proteins' amazing structures[HTML]
    The Royal Swedish Academy of Sciences, 2024. Nobel Prize Press Release.
  3. [Mandon, 2024]
    AlphaFold Decoded: Feature Extraction (Lesson 4)[HTML]
    Mandon, Kilian, 2024. YouTube.