def compute_matrix(self): """ Compute rotation matrix to align Z axis to this vector. """ if abs(self.vector[0]) < 0.00001 and abs(self.vector[1]) < 0.00001: rot = math.radians(0.00) tilt = math.radians(0.00) else: rot = math.atan2(self.vector[1], self.vector[0]) tilt = math.acos(self.vector[2]) psi = 0 self.matrix = euler_matrix(-rot, -tilt, -psi)
def matrixFromGeometry(shifts, angles): """ Create the transformation matrix from a given 2D shifts in X and Y...and the 3 euler angles. """ inverseTransform = True radAngles = -np.deg2rad(angles) M = transformations.euler_matrix(radAngles[0], radAngles[1], radAngles[2], 'szyz') if inverseTransform: M[:3, 3] = -shifts[:3] M = np.linalg.inv(M) else: M[:3, 3] = shifts[:3] return M
def testTransformTranslation(self): aSHSource = AtomicStructHandler(self.PDBFileName) structure = aSHSource.getStructure() structure_copy = deepcopy(aSHSource.getStructure()) shift = [100., 50., 25.] # rotation_matrix = euler_matrix(deg2rad(45.), 0., 0., 'szyz') rotation_matrix = euler_matrix(0., 0., 0., 'szyz') translation = translation_matrix(shift) M = concatenate_matrices(rotation_matrix, translation) aSHSource.transform(M) for atom1, atom2 in zip(structure.get_atoms(), structure_copy.get_atoms()): coord1 = atom1.get_coord() coord2 = [sum(x) for x in zip(atom2.get_coord(), shift)] for i in range(3): self.assertAlmostEqual(coord1[i], coord2[i], 2)
def matrixFromGeometry(shifts, angles, inverseTransform): """ Create the transformation matrix from a given 2D shifts in X and Y...and the 3 euler angles. """ from pyworkflow.em.convert.transformations import euler_matrix from numpy import deg2rad radAngles = -deg2rad(angles) M = euler_matrix(radAngles[0], radAngles[1], radAngles[2], 'szyz') if inverseTransform: from numpy.linalg import inv M[:3, 3] = -shifts[:3] M = inv(M) else: M[:3, 3] = shifts[:3] return M
def testTransformRotation(self): aSHSource = AtomicStructHandler(self.PDBFileName) structure = aSHSource.getStructure() structure_copy = deepcopy(structure) rot = numpy.deg2rad(10) theta = numpy.deg2rad(20.) psi = numpy.deg2rad(30.) rotation_matrix = euler_matrix(rot, theta, psi, 'szyz') translation = translation_matrix([0., 0., 0.]) M = concatenate_matrices(rotation_matrix, translation) aSHSource.transform(M) m = M[:3, :3] for atom1, atom2 in zip(structure.get_atoms(), structure_copy.get_atoms()): coord1 = atom1.get_coord() coord2 = m.dot(atom2.get_coord()) for i in range(3): self.assertAlmostEqual(coord1[i], coord2[i], 2)
def __applyTransform(suffix, pdbFileName, shift, angles, sampling): """ auxiliary function, transform PDB and 3dmap files""" # create a Scipion transformation matrix from numpy import deg2rad rotation_matrix = euler_matrix(deg2rad(angles[0]), deg2rad(angles[1]), deg2rad(angles[2]), 'szyz') translation = translation_matrix(shift) M = concatenate_matrices(rotation_matrix, translation) # apply it to the pdb file # if rotation move to center aSH = AtomicStructHandler(pdbFileName) if (angles[0] != 0. or angles[1] != 0. or angles[2] != 0.): from pyworkflow.em.convert import ImageHandler ih = ImageHandler() x, y, z, n = ih.getDimensions("emd_%s.map" % EMDBID) x /= 2. y /= 2. z /= 2. localShift = [-x, -y, -z] rotation_matrix = euler_matrix(0., 0., 0., 'szyz') translation = translation_matrix(localShift) localM = concatenate_matrices(rotation_matrix, translation) aSH.transform(localM, sampling=sampling) aSH.transform(M, sampling=sampling) if (angles[0] != 0. or angles[1] != 0. or angles[2] != 0.): localShift = [x, y, z] rotation_matrix = euler_matrix(0., 0., 0., 'szyz') translation = translation_matrix(localShift) localM = concatenate_matrices(rotation_matrix, translation) aSH.transform(localM, sampling=sampling) aSH.write("%s_%s_transformed.ent" % (suffix, PDBID.lower())) # get equivalent xmipp transformation shift, angles = __getXmippEulerAngles(M) # shift 3map and set sampling __runXmippProgram("xmipp_transform_geometry", '-i emd_%s.map ' '-o %s_emd_%s_transform.map ' '--interp linear ' '--shift %f %f %f ' '--rotate_volume euler %f %f %f ' % ( EMDBID, suffix, EMDBID, shift[0], shift[1], shift[2], angles[0], angles[1], angles[2] ) ) header = Ccp4Header("%s_emd_%s_transform.map" % (suffix, EMDBID), readHeader=True) header.setSampling(sampling) # put the sampling back, xmipp_transform_geometry erased it header.writeHeader() # view the results with chimera from pyworkflow.em.viewers import Chimera args = "%s %s %s %s" % ( pdbFileName, "emd_%s.map" % EMDBID, "%s_%s_transformed.ent" % (suffix, PDBID.lower()), "%s_emd_%s_transform.map" % (suffix, EMDBID) ) Chimera.runProgram(args)