用Python计算RMSD
时间: 2023-11-19 19:50:18 浏览: 45
要计算两个分子之间的RMSD,需要先将它们的坐标对齐。常见的方法是使用Kabsch算法进行最小二乘拟合,将两个分子的坐标旋转、平移至最佳匹配。然后对齐后的坐标计算RMSD即可。
以下是用Python实现的示例代码:
```python
import numpy as np
def kabsch_rmsd(P, Q):
"""Calculate RMSD between two sets of points after Kabsch alignment"""
# center points
P -= np.mean(P, axis=0)
Q -= np.mean(Q, axis=0)
# calculate covariance matrix
C = np.dot(P.T, Q)
# singular value decomposition
V, S, W = np.linalg.svd(C)
# check if we need to correct for reflection
if np.linalg.det(V) * np.linalg.det(W) < 0:
S[-1] = -S[-1]
V[:, -1] = -V[:, -1]
# calculate rotation matrix and transformed coordinates
U = np.dot(V, W)
P = np.dot(P, U)
# return RMSD
return np.sqrt(np.mean(np.sum((P - Q)**2, axis=1)))
# example usage
P = np.array([[0, 0, 0], [0, 1, 0], [1, 1, 0], [1, 0, 0]])
Q = np.array([[0.2, 0.1, 0.1], [0.2, 1.1, 0.1], [1.2, 1.1, 0.1], [1.2, 0.1, 0.1]])
rmsd = kabsch_rmsd(P, Q)
print(rmsd)
```