A Julia implementation of the Kabsch algorithm for finding the optimal rotation between paired sets of points.
using Kabsch, Manifolds
# Generate realistic test data
P = randn(3, 10)
R_true = rand(Rotations(3)) # Random rotation matrix
Q = R_true * centered(P) .+ randn(3) # Rotate and translate P
# Find optimal alignment
R, P_centroid, Q_centroid = kabsch(P, Q)
Q_aligned = superimposed(Q, P)
# Verify perfect alignment
@assert P ≈ Q_aligned
@assert rmsd(superimposed, Q, P) ≈ 0
Process multiple alignments simultaneously:
Ps = randn(3, 50, 8) # 8 reference point sets
Qs = randn(3, 50, 8) # 8 point sets to align, uncorrelated for ease of showcase
Rs, P_centroids, Q_centroids = kabsch(Ps, Qs)
Q_aligned_all = superimposed(Qs, Ps)
StaticArrays: Optimal performance for small point sets
using StaticArrays
P = @SMatrix randn(3, 5)
Q = @SMatrix randn(3, 5) # uncorrelated for ease of showcase
R, Pt, Qt = kabsch(P, Q)
CUDA: GPU acceleration for 3D case
using CUDA
P_gpu = CUDA.randn(3, 100, 1000) # 100 batches on GPU
Q_gpu = CUDA.randn(3, 100, 1000) # uncorrelated for ease of showcase
R_gpu, _, _ = kabsch(P_gpu, Q_gpu)
- BioStructures.jl for
rmsd
andsuperimpose!
on molecular structures (including residue alignment)