How to align structures#
Goal#
Superimpose one structure or trajectory onto a reference by minimising RMSD over a chosen atom selection.
Minimal example#
from moleculekit.molecule import Molecule
from moleculekit.util import uniformRandomRotation, molRMSD
ref = Molecule("3PTB")
mol = ref.copy()
# Apply a uniform random rotation around the centroid of mol
centroid = mol.coords[:, :, 0].mean(axis=0)
mol.rotateBy(uniformRandomRotation(), center=centroid)
ca = mol.atomselect("protein and name CA", indexes=True)
ca_ref = ref.atomselect("protein and name CA", indexes=True)
print(f"RMSD before align: {molRMSD(mol, ref, ca, ca_ref):.2f} Å")
mol.align("protein and name CA", refmol=ref)
print(f"RMSD after align: {molRMSD(mol, ref, ca, ca_ref):.2f} Å")
The RMSD drops from several Ångström (random rotation) to essentially zero because the two structures share the same coordinates; alignment can recover the original superposition exactly.
Parameters that matter#
Parameter |
Type |
Default |
What it does |
|---|---|---|---|
|
|
required |
Atoms in |
|
|
Reference molecule; if |
|
|
|
same as |
Atoms in |
|
|
all frames |
Which frames of |
|
|
|
Atom-correspondence rule. |
Common variations#
# Structural alignment via TM-align — robust to mismatched sequences,
# does not require sel and refsel to have the same atom count
mol.align("protein", refmol=ref, mode="structure")
# Sequence-based alignment — handles mismatched residue numbering
# by first aligning sequences and then calling .align on matched residues
mol.alignBySequence(ref)
Gotchas#
With
mode="index"(the default),align()requires the same number of atoms inselandrefsel; a mismatch raises an error.With
mode="structure", the correspondence is found by TM-align internally —selandrefselcan have different atom counts and the routine is robust to large conformational differences.alignBySequence()handles residue count mismatches by first finding a sequence alignment, then callingalign()(mode="index") on the matched residues.If
refmolisNone,molis aligned to its own first frame, which removes rigid-body drift across frames.