biotite icon indicating copy to clipboard operation
biotite copied to clipboard

kabsch alignment algorithm swapped mobile and fixed when creating rotation matrix

Open jonathanking-absci opened this issue 1 year ago • 2 comments

Hello,

I'm working on some code that suggests that when creating the rotation matrix via superimpose, the kabsch algorithm implementation simply has fixed and mobile swapped as in superimpose.py:_superimpose(). Do you think this is an accurate assessment? I think the definitions of x and y in that function should be flipped. Or, in other words, I think the kabsch algorithm should have mobile.T dot reference, when this implementation is doing reference.T dot mobile.

My method:

  • create a set of points
  • create a rotation matrix R
  • rotate points by R to get points*
  • use superimpose(points*, points) to calculate R2
  • use superimpose(points, points*) to calculate R3
  • R2 != R
  • R3 == R

Just wanted to bring that to your attention! Or perhaps I'm mistaken, and that would be nice to know as well 🙂

Best, Jonathan

jonathanking-absci avatar Jan 13 '24 02:01 jonathanking-absci

Hi, I will look into it. Intuitively, I would also presume, that R2 = R should be the case.

padix-key avatar Jan 15 '24 22:01 padix-key

I took this issue to perform some refactoring on superimpose() in #526, including some new tests. One test also tests simply if the rotation matrix is as expected for a 90 degrees rotation around the z-axis:

https://github.com/biotite-dev/biotite/blob/29ecc1e0276ae806e3cf3ce612b6cc01a3242951/tests/structure/test_superimpose.py#L46-L69

However, the rotation matrix is as expected, but it might also be fixed by the refactoring. As soon as #526 is merged or released, could you check again?

If the issue persists, could it be possible the error is this step:

rotate points by R to get points*

which, I think, could easily happen as the inverse of a rotation matrix is simply its transpose.

padix-key avatar Jan 24 '24 21:01 padix-key

I went back and looked at this. You were right. I was rotating points via points @ rotation_matrix and not points @ rotation_matrix.T. Sorry for the confusion, and thanks for the help!

jonathanking-absci avatar Feb 28 '24 05:02 jonathanking-absci