def align_models(CA): n_models = CA.shape[0] working_CA = np.copy(CA) sup=SVDSuperimposer() ref_model = working_CA[0, :, :] rms_total = 0 for i_model in range(1, n_models): sup.set(ref_model, working_CA[i_model]) sup.run() rms_total += sup.get_rms()**2 working_CA[i_model] = sup.get_transformed() rms_best = float("inf") epsilon = 0.001 while rms_best - rms_total > epsilon: rms_best = rms_total mean_model = np.mean(working_CA,0) rms_total = 0 for i_model in range(n_models): sup.set(mean_model, working_CA[i_model]) sup.run() rms_total += sup.get_rms()**2 working_CA[i_model] = sup.get_transformed() transformations = [] for start_model, result_model in zip(CA, working_CA): sup.set(result_model, start_model) sup.run() transformations.append(sup.get_rotran()) return transformations,np.sqrt(rms_total/n_models)
def superimpose(X, Xtag): try: sup = SVDSuperimposer() sup.set(X, Xtag) sup.run() Xtag_super = sup.get_transformed() return Xtag_super except: return np.zeros((X.shape[0],X.shape[1]))
def Superimpose(atoms1, atoms2): assert len(atoms1) == len(atoms2) #aligner = QCPSuperimposer() aligner = SVDSuperimposer() aligner.set(atoms1, atoms2) aligner.run() RMSD = aligner.get_rms() ## calculate the distance deviation at each position atoms2_transformed = aligner.get_transformed() diff = atoms1 - atoms2_transformed diff2 = np.power(diff, 2) deviations = np.sqrt(np.sum(diff2, axis=1)) return RMSD, deviations
def run_sup3d(coord1, coord2): sup = SVDSuperimposer() sup.set( np.array(coord1), np.array(coord2) ) #set is setting the group of coordinates because i have initialized SVD, it is empty sup.run( ) #superimpose the coordinates, run does all the work. Then we compute the RMSD between vc1 and vc2 after transformation rmsd = sup.get_rms() rot, tran = sup.get_rotran( ) #shows the matrix of rotation and vector for translation tcoord = sup.get_transformed() print rmsd print rot print tran print tcoord #you obtain the set of coordinates to be superimposable to the se 1, so the set of coordinates after transformation. return
def align(predicted, gt): """ # Grid search through scales for affine alignment. scale_range = np.arange(0.9, 1.1, 0.05) best_drmsd = float("inf") best_sx = 1 best_sy = 1 best_sz = 1 for sx in scale_range: for sy in scale_range: for sz in scale_range: sup = SVDSuperimposer() scaling = np.diag([sx, sy, sz]) scaled_predicted = np.dot(np.array(predicted), scaling) sup.set(np.array(gt), scaled_predicted) sup.run() rms = sup.get_rms() rot, tran = sup.get_rotran() b = sup.get_transformed() a = np.array(gt) drmsd = compute_drmsd(a, b) if drmsd < best_drmsd: best_drmsd = drmsd best_sx = sx best_sy = sy best_sz = sz """ best_sx = 1 best_sy = 1 best_sz = 1 # Use best sx, sy, sz to perform final alignment. sup = SVDSuperimposer() scaling = np.diag([best_sx, best_sy, best_sz]) scaled_predicted = np.dot(np.array(predicted), scaling) sup.set(np.array(gt), scaled_predicted) sup.run() predicted = sup.get_transformed() return predicted
def align(coordinate_file): ''' 1. Input: File contains lines, where each line contains the coordinates of a model, e.g., if model 1 has 70 atoms, each with 3 coordinates (3*70 = 210 coordinates), then the line corresponding model 1 is like this: 210 x1 y1 z1 x2 y2 z2 ... x70 y70 z70 2. Alignes all the model with the first model in the cordinate_file. 3. Returns: a dictionary of aligned models. Each model, i.e., each entry (value) in the dictionary is a flattened numpy array. ''' modelDict = {} ind = 0 ref = [] sup = SVDSuperimposer() with open(coordinate_file) as f: for line in f: if ind == 0: l = [float(t) for t in line.split()] l = l[1:] samples = [l[i:i + 3] for i in range(0, len(l), 3)] ref = array(samples, 'f') modelDict[ind] = np.ravel(ref) ind += 1 else: l = [float(t) for t in line.split()] l = l[1:] samples = [l[i:i + 3] for i in range(0, len(l), 3)] seq = array(samples, 'f') s = sup.set(ref, seq) sup.run() z = sup.get_transformed() modelDict[ind] = np.ravel(z) ind += 1 return modelDict, ref
sup.set(x, y) # do the lsq fit sup.run() # get the rmsd rms = sup.get_rms() # get rotation (right multiplying!) and the translation rot, tran = sup.get_rotran() # rotate y on x manually y_on_x1 = dot(y, rot) + tran # same thing y_on_x2 = sup.get_transformed() def simple_matrix_print(matrix): """Simple string to display a floating point matrix This should give the same output on multiple systems. This is needed because a simple "print matrix" uses scientific notation which varies between platforms. Only 4 decimal places are used to avoid false test failures due to slight differences in the calculation (e.g. due to different versions of the underlying libraries or the compilation options they used). """ return "[%s]" % "\n ".join("[%s]" % " ".join("% 1.4f" % v for v in row)
class SVDSuperimposerTest(unittest.TestCase): def setUp(self): self.x = array([[51.65, -1.90, 50.07], [50.40, -1.23, 50.65], [50.68, -0.04, 51.54], [50.22, -0.02, 52.85]]) self.y = array([[51.30, -2.99, 46.54], [51.09, -1.88, 47.58], [52.36, -1.20, 48.03], [52.71, -1.18, 49.38]]) self.sup = SVDSuperimposer() self.sup.set(self.x, self.y) def test_get_init_rms(self): x = array([[1.19, 1.28, 1.37], [1.46, 1.55, 1.64], [1.73, 1.82, 1.91]]) y = array([[1.91, 1.82, 1.73], [1.64, 1.55, 1.46], [1.37, 1.28, 1.19]]) self.sup.set(x, y) self.assertIsNone(self.sup.init_rms) init_rms = 0.8049844719 self.assertTrue(float("%.3f" % self.sup.get_init_rms()), float("%.3f" % init_rms)) def test_oldTest(self): self.assertTrue( array_equal(around(self.sup.reference_coords, decimals=3), around(self.x, decimals=3))) self.assertTrue( array_equal(around(self.sup.coords, decimals=3), around(self.y, decimals=3))) self.assertIsNone(self.sup.rot) self.assertIsNone(self.sup.tran) self.assertIsNone(self.sup.rms) self.assertIsNone(self.sup.init_rms) self.sup.run() self.assertTrue( array_equal(around(self.sup.reference_coords, decimals=3), around(self.x, decimals=3))) self.assertTrue( array_equal(around(self.sup.coords, decimals=3), around(self.y, decimals=3))) rot = array([[0.68304983, 0.53664371, 0.49543563], [-0.52277295, 0.83293229, -0.18147242], [-0.51005037, -0.13504564, 0.84947707]]) tran = array([38.78608157, -20.65451334, -15.42227366]) self.assertTrue( array_equal(around(self.sup.rot, decimals=3), around(rot, decimals=3))) self.assertTrue( array_equal(around(self.sup.tran, decimals=3), around(tran, decimals=3))) self.assertIsNone(self.sup.rms) self.assertIsNone(self.sup.init_rms) rms = 0.00304266526014 self.assertEqual(float("%.3f" % self.sup.get_rms()), float("%.3f" % rms)) rot_get, tran_get = self.sup.get_rotran() self.assertTrue( array_equal(around(rot_get, decimals=3), around(rot, decimals=3))) self.assertTrue( array_equal(around(tran_get, decimals=3), around(tran, decimals=3))) y_on_x1 = dot(self.y, rot) + tran y_x_solution = array( [[5.16518846e+01, -1.90018270e+00, 5.00708397e+01], [5.03977138e+01, -1.22877050e+00, 5.06488200e+01], [5.06801788e+01, -4.16095666e-02, 5.15368866e+01], [5.02202228e+01, -1.94372374e-02, 5.28534537e+01]]) self.assertTrue( array_equal(around(y_on_x1, decimals=3), around(y_x_solution, decimals=3))) y_on_x2 = self.sup.get_transformed() self.assertTrue( array_equal(around(y_on_x2, decimals=3), around(y_x_solution, decimals=3)))
best_drmsd = float("inf") best_sx = 1 best_sy = 1 best_sz = 1 for sx in scale_range: for sy in scale_range: for sz in scale_range: sup = SVDSuperimposer() scaling = np.diag([sx, sy, sz]) scaled_predicted = np.dot(np.array(predicted[i]), scaling) print(predicted[i]) sup.set(np.array(gt[i]), scaled_predicted) sup.run() rms = sup.get_rms() rot, tran = sup.get_rotran() b = sup.get_transformed() a = np.array(gt[i]) drmsd = (1.0 / len(a) * np.sum((a - b)**2))**0.5 if drmsd < best_drmsd: best_drmsd = drmsd best_sx = sx best_sy = sy best_sz = sz scales.append((best_sx, best_sy, best_sz)) drmsds.append(best_drmsd) print(drmsds) for i in range(len(proteins)): sup = SVDSuperimposer() scaling = np.diag([scales[i][0], scales[i][1], scales[i][2]]) scaled_predicted = np.dot(np.array(predicted[i]), scaling)
class SVDSuperimposerTest(unittest.TestCase): def setUp(self): self.x = array([[51.65, -1.90, 50.07], [50.40, -1.23, 50.65], [50.68, -0.04, 51.54], [50.22, -0.02, 52.85]]) self.y = array([[51.30, -2.99, 46.54], [51.09, -1.88, 47.58], [52.36, -1.20, 48.03], [52.71, -1.18, 49.38]]) self.sup = SVDSuperimposer() self.sup.set(self.x, self.y) def test_get_init_rms(self): x = array([[1.19, 1.28, 1.37], [1.46, 1.55, 1.64], [1.73, 1.82, 1.91]]) y = array([[1.91, 1.82, 1.73], [1.64, 1.55, 1.46], [1.37, 1.28, 1.19]]) self.sup.set(x, y) self.assertIsNone(self.sup.init_rms) init_rms = 0.8049844719 self.assertTrue( float('%.3f' % self.sup.get_init_rms()), float('%.3f' % init_rms)) def test_oldTest(self): self.assertTrue( array_equal(around(self.sup.reference_coords, decimals=3), around(self.x, decimals=3))) self.assertTrue( array_equal(around(self.sup.coords, decimals=3), around(self.y, decimals=3))) self.assertIsNone(self.sup.rot) self.assertIsNone(self.sup.tran) self.assertIsNone(self.sup.rms) self.assertIsNone(self.sup.init_rms) self.sup.run() self.assertTrue( array_equal(around(self.sup.reference_coords, decimals=3), around(self.x, decimals=3))) self.assertTrue( array_equal(around(self.sup.coords, decimals=3), around(self.y, decimals=3))) rot = array([[0.68304983, 0.53664371, 0.49543563], [-0.52277295, 0.83293229, -0.18147242], [-0.51005037, -0.13504564, 0.84947707]]) tran = array([38.78608157, -20.65451334, -15.42227366]) self.assertTrue( array_equal(around(self.sup.rot, decimals=3), around(rot, decimals=3))) self.assertTrue( array_equal(around(self.sup.tran, decimals=3), around(tran, decimals=3))) self.assertIsNone(self.sup.rms) self.assertIsNone(self.sup.init_rms) rms = 0.00304266526014 self.assertEqual( float('%.3f' % self.sup.get_rms()), float('%.3f' % rms)) rot_get, tran_get = self.sup.get_rotran() self.assertTrue( array_equal(around(rot_get, decimals=3), around(rot, decimals=3))) self.assertTrue( array_equal(around(tran_get, decimals=3), around(tran, decimals=3))) y_on_x1 = dot(self.y, rot) + tran y_x_solution = array( [[5.16518846e+01, -1.90018270e+00, 5.00708397e+01], [5.03977138e+01, -1.22877050e+00, 5.06488200e+01], [5.06801788e+01, -4.16095666e-02, 5.15368866e+01], [5.02202228e+01, -1.94372374e-02, 5.28534537e+01]]) self.assertTrue( array_equal(around(y_on_x1, decimals=3), around(y_x_solution, decimals=3))) y_on_x2 = self.sup.get_transformed() self.assertTrue( array_equal(around(y_on_x2, decimals=3), around(y_x_solution, decimals=3)))