def cal_transformation_kabsch(src_points, dst_points): ''' Calculates the optimal rigid transformation from src_points to dst_points. (Regarding the least squares error) Args: ------ src_points: array, (N, 3) matrix. dst_points: array, (N, 3) matrix. Returns: ------ R: rotaion matrix, array. T: translation vector, array. rmsd_value: float. ''' assert src_points.shape == dst_points.shape if src_points.shape[1] != 3: raise Exception( "The input data matrix had to be transposed in order to compute transformation." ) #src_points = src_points.transpose() #dst_points = dst_points.transpose() src_points_centered = src_points - rmsd.centroid(src_points) dst_points_centered = dst_points - rmsd.centroid(dst_points) R = rmsd.kabsch(src_points_centered, dst_points_centered) rmsd_value = rmsd.kabsch_rmsd(src_points_centered, dst_points_centered) T = rmsd.centroid(dst_points) - np.matmul(rmsd.centroid(src_points), R) return R.transpose(), T.transpose(), rmsd_value
def test_pdb(threshold=0.001): """ A simple test for the PDB functionality :return: True if all test passed """ p_atoms, P = calculate_rmsd.get_coordinates('examples/ci2_1.pdb', 'pdb') q_atoms, Q = calculate_rmsd.get_coordinates('examples/ci2_2.pdb', 'pdb') n_rmsd = calculate_rmsd.rmsd(P, Q) Pc = calculate_rmsd.centroid(P) Qc = calculate_rmsd.centroid(Q) P -= Pc Q -= Qc k_rmsd = calculate_rmsd.kabsch_rmsd(P, Q) q_rmsd = calculate_rmsd.quaternion_rmsd(P, Q) if abs(n_rmsd - 26.975) > threshold: print('Failed to calculate normal RMSD, result: {0}'.format(n_rmsd)) return False if abs(k_rmsd - 11.777) > threshold: print('Failed to calculate Kabsch RMSD, result: {0}'.format(k_rmsd)) return False if abs(q_rmsd - 11.777) > threshold: print('Failed to calculate quaternion RMSD, result: {0}'.format(q_rmsd)) return False if abs(q_rmsd - k_rmsd) > threshold ** 2: print('Failed to yield similar Kabsch and quaternion RMSD, result: {0} vs {1}'.format(k_rmsd, q_rmsd)) return False return True
def rmsd_dist(A, B): #REMOVE CENTER OF MASS A -= rmsd.centroid(A) B -= rmsd.centroid(B) #FIND ROTATION MATRIX U = rmsd.kabsch(A, B) # ROTATE TOWARDS B A = np.dot(A, U) #RETURN ROOT MEAN SQUARE DEVIATION return rmsd.rmsd(A, B)
def rmsd_VEC(A, B): #REMOVE CENTER OF MASS A -= rmsd.centroid(A) B -= rmsd.centroid(B) #FIND ROTATION MATRIX U = rmsd.kabsch(A, B) # ROTATE TOWARDS B A = np.dot(A, U) #RETURN ROTATED ARRAY return A
def write_rmsd(fp, fp2, A, B): A -= rmsd.centroid(A) B -= rmsd.centroid(B) fp2.write('%d\n' % (len(A))) fp2.write('\n') for i in range(len(A)): fp2.write('A %f %f %f\n' % (A[i, 0], A[i, 1], A[i, 2])) #FIND ROTATION MATRIX U = rmsd.kabsch(A, B) # ROTATE TOWARDS B A = np.dot(A, U) fp.write('%d\n' % (len(A))) fp.write('\n') for i in range(len(A)): fp.write('A %f %f %f\n' % (A[i, 0], A[i, 1], A[i, 2])) return fp
def arrange(ref, to): ref -= rmsd.centroid(ref) to -= rmsd.centroid(to) U = rmsd.kabsch(to[:3, :], ref[:3, :]) to = np.dot(to, U) dist = [] ref_CT_taken = [] to_CT_taken = [] unordered_results = [] for i in range(3, len(ref)): for j in range(3, len(to)): ref_CT = ref[i, :] to_CT = to[j, :] dist.append([i - 3, j - 3, abs(np.linalg.norm(ref_CT - to_CT))]) while len(ref_CT_taken) != len(ref) - 3: min_index = min_dist_index(dist) unordered_results.append([dist[min_index][0], dist[min_index][1]]) ref_lmo = dist[min_index][0] to_lmo = dist[min_index][1] ref_CT_taken.append(ref_lmo) to_CT_taken.append(to_lmo) to_be_deleted = [] for i in range(len(dist)): if dist[i][0] == ref_lmo: if i not in to_be_deleted: to_be_deleted.append(dist[i]) continue if dist[i][1] == to_lmo: if i not in to_be_deleted: to_be_deleted.append(dist[i]) for i in range(len(to_be_deleted)): dist.remove(to_be_deleted[i]) return order(unordered_results, len(ref) - 3)
from __future__ import print_function import calculate_rmsd as rmsd import os import sys all_rmsd = [] q_atoms, Q = rmsd.get_coordinates(sys.argv[1], 'xyz') Q -= rmsd.centroid(Q) for filename in os.listdir('../../ML/samples3-1-1000'): if not filename.endswith('.xyz'): continue p_atoms, P = rmsd.get_coordinates('../../ML/samples3-1-1000/' + filename, 'xyz') P -= rmsd.centroid(P) all_rmsd.append(rmsd.kabsch_rmsd(P, Q)) file = open('./rmsd_results/min_rmsd.csv', 'w') for rmsd in all_rmsd: print(rmsd, file=file) file.close()