Dear Akshay,
Welcome to MDAnalysis!
Unfortunately dynamic selections are not supported by AlignTraj or DistanceMatrix in that way; when you pass a selection string, only a static selection is made. Therefore, the number of atoms is not different in each frame.This is because both AlignTraj and DistanceMatrix use an atom-wise metric (RMSD), where atom i in group1 is compared to atom i in group 2. So for AlignTraj, the number of atoms used to align the trajectory in each frame must be the same as the number in the static reference. When calculating the RMSD of frame 1 vs. frame 2 in distance matrix, the number of atoms must again be the same.
In your code, you are only choosing the atom group once in the first frame — the backbone atoms near chain A or B. Then, without updating, the trajectory is aligned to the first frame using those atoms, and the RMSD is calculated between each pair of frames.
Perhaps you could find out all backbone atoms near either chain in any frame of the trajectory, and run the analysis on that instead?
ag = u.select_atoms(interface)
nearby_at_any_point = u.atoms[[]]
for _ in u.trajectory:
nearby_at_any_point |= ag
aligner = align.AlignTraj(nearby_at_any_point, nearby_at_any_point, in_memory=True).run()
select_by_index = f"index {' '.join(list(map(str, nearby_at_any_point.ix)))}"
matrix = diffusionmap.DistanceMatrix(u, select=select_by_index)
Cheers,
Lily