Structure module, templates and recycling
AlphaFold 2 Template Pair Features
AlphaFold2OpenFold
Free account with email, Google or GitHub. Your code runs in an isolated PyTorch sandbox.
Your task
Describe each template residue pair by its representative-atom distance, ordered residue identities, and the direction from i to j in i’s backbone frame. Build the AlphaFold2 feature vector in the stated channel order and project it into an embedding.
Function signature
af2_template_pair_features(
activations: dict,
parameters: dict,
masks: dict,
structure: dict,
eps: float = 1e-08,
type_count: int = 22,
glycine_index: int = 7,
) -> dict[str, torch.Tensor]Worked example
An off-diagonal direction in a rotated frame
Consider the AF2 raw-feature stage for one template and two residues. Atom order is N, CA, C, CB. Residue 0 has atoms (-1,0,0),(0,0,0),(0,1,0),(0,0,1); residue 1 has (1,0,0),(2,0,0),(2,1,0),(2,0,1). All atoms and pairs are valid. Set residue_type=[0,1], type_count=2, glycine_index=1, distance_edges=[2], and eps=0.01. The representative atoms are (0,0,1) and glycine CA (2,0,0), with squared distance 5, so the distance one-hot is [0,1]. Residue 0 has local x along global y and local y along negative global x; therefore CA displacement (2,0,0) becomes local (0,-2,0). For pair (0,1), concatenate [0,1], [1], right type [0,1], left type [1,0], local direction [0,-2/sqrt(4.01),0], and [1]. The local y channel is approximately -0.998752; affine projection is the next stage.
Parameters
| Parameter | Shape and meaning |
|---|---|
activationsdict | Supplied intermediate tensors. |
parametersdict | Supplied model parameters, keyed as shown below. |
masksdict | Supplied masks. Each field below defines what True includes or permits. |
structuredict | Fields supplied together in this dictionary. |
epsfloatDefault 1e-08 | Positive geometric squared-norm floor for frame/angle construction and an additive squared-distance stabilizer for local unit directions; use the distinct placements in the selected feature rule. |
type_countintDefault 22 | Size of the residue-type vocabulary. |
glycine_indexintDefault 7 | Vocabulary index for glycine. |
Return
dict[str, torch.Tensor]
Return exactly the named fields applicable to this operation.
Required behavior
- atom_positions is
[B,T,N,4,3], with atom slots N, CA, C, CB. atom_mask marks available atoms. residue_type uses type_count categories (default 22); one-hot feature order follows these supplied integer IDs. - Pseudo-beta uses CA for glycine(residue_type == glycine_index, default 7) and supplied CB otherwise, with that selected atom’s mask.
- A candidate pair requires both complete N/CA/C backbones, both valid pseudo-beta atoms, and pair_mask. Squared-distance bin index counts distance_edges² strictly below squared pseudo-beta distance.
- Build the frame at residue i with x along C−CA, y along the component of N−CA orthogonal to x, and z=x×y. Unit-vector normalization divides by sqrt(max(norm²,eps)); both endpoint frames require squared x/y norms > eps (default 1e-8).
- Express CA_j−CA_i in i’s frame and divide by sqrt(local_squared_norm + eps). Concatenate: distance one-hot, pair-valid indicator, right residue one-hot, left residue one-hot, local unit vector(3), pair-valid indicator again.
- Zero invalid feature rows, then project with w_features and b_features. Return pseudo_beta, the final pair_mask, raw concatenated features, and learned embedding; zero invalid embeddings after every bias.
- Use CPU float32/float64 tensors with one shared floating dtype/device. Preserve strided layouts and do not mutate supplied tensors.
- Use raw tensor operations, not a ready-made implementation of the requested primitive. Supplied parameters and random variates must not be reinitialized.
Why this matters
AlphaFold2 converts each template structure into pairwise geometric features. The features describe distance, residue types and the direction to the second residue in the first residue’s local frame.
Starter code
You write the body of this function in the BioTorch workspace. Your code runs in an isolated PyTorch sandbox; Run checks the public examples and Submit grades the full test suite, including gradients.
import torch
def af2_template_pair_features(
activations: dict,
parameters: dict,
masks: dict,
structure: dict,
eps: float = 1e-08,
type_count: int = 22,
glycine_index: int = 7,
) -> dict[str, torch.Tensor]:
"""Return dict[str, torch.Tensor]:
pseudo_beta: (B, T, N, 3).
pair_mask: (B, T, N, N).
features: (B,T,N,N,K+2V+6).
embedding: (B, T, N, N, Ct)."""
# Implement the operation. Inputs and test setup are supplied by BioTorch.
raise NotImplementedError("Implement af2_template_pair_features")
Ready to implement it?
3 progressive hints, a solution walkthrough, and grading that checks values, shapes and gradients are waiting in the workspace.
Free account with email, Google or GitHub. Your code runs in an isolated PyTorch sandbox.
Related exercises
Sources and further reading
A primitive exercise, not an end-to-end reproduction of the named models. No affiliation or endorsement is implied.