def test_drms(self): """ Test drms measure """ m = IMP.Model() sel = atom.CAlphaPDBSelector() prot1 = atom.read_pdb(self.open_input_file("mini.pdb"), m, sel) prot2 = atom.read_pdb(self.open_input_file("mini.pdb"), m, sel) xyzs1 = core.XYZs(atom.get_leaves(prot1)) xyzs2 = core.XYZs(atom.get_leaves(prot2)) drms = atom.get_drms(xyzs1, xyzs2) # Molecule with itself self.assertAlmostEqual(drms, 0) R = IMP.algebra.get_random_rotation_3d() v = IMP.algebra.get_random_vector_in( IMP.algebra.get_unit_bounding_box_3d()) T = IMP.algebra.Transformation3D(R, v) for x in xyzs2: core.transform(x, T) drms = atom.get_drms(xyzs1, xyzs2) # Same thing after transformation self.assertAlmostEqual(drms, 0) # for x in xyzs2: R = IMP.algebra.get_random_rotation_3d() T = IMP.algebra.Transformation3D(R, v) core.transform(x, T) drms = atom.get_drms(xyzs1, xyzs2) self.assertTrue(drms > 0)
def test_component_placement_score(self): """Testing that component placement score returns the same transformation if called twice""" m = IMP.Model() # read PDB mp1_ref = atom.read_pdb(self.open_input_file("1z5s_A.pdb"), m, atom.NonWaterPDBSelector()) mp1_mdl = atom.read_pdb(self.open_input_file("1z5s_A.pdb"), m, atom.NonWaterPDBSelector()) mp2_ref = atom.read_pdb(self.open_input_file("1z5s_C.pdb"), m, atom.NonWaterPDBSelector()) mp2_mdl = atom.read_pdb(self.open_input_file("1z5s_C.pdb"), m, atom.NonWaterPDBSelector()) xyz1_ref = core.XYZs(atom.get_leaves(mp1_ref)) xyz1_mdl = core.XYZs(atom.get_leaves(mp1_mdl)) xyz2_ref = core.XYZs(atom.get_leaves(mp2_ref)) xyz2_mdl = core.XYZs(atom.get_leaves(mp2_mdl)) # create a random transformation t = IMP.algebra.Transformation3D(IMP.algebra.get_random_rotation_3d(), IMP.algebra.get_random_vector_in(IMP.algebra.get_unit_bounding_box_3d())) for d in xyz1_mdl: core.transform(d, t) t = IMP.algebra.Transformation3D(IMP.algebra.get_random_rotation_3d(), IMP.algebra.get_random_vector_in(IMP.algebra.get_unit_bounding_box_3d())) # core.get_transformed(xyz2_mdl,t) for d in xyz2_mdl: core.transform(d, t) da1 = atom.get_component_placement_score( xyz1_ref, xyz2_ref, xyz1_mdl, xyz2_mdl) da2 = atom.get_component_placement_score( xyz1_ref, xyz2_ref, xyz1_mdl, xyz2_mdl) self.assertAlmostEqual(da1[1], da2[1])
def get_ccc(native_assembly, assembly, resolution, voxel_size, threshold, write_maps=False): """ Threshold - threshold used for the map of the native assembly. Pixels with values above this threshold in the native map are used for the calculation of the cross_correlation_coefficient """ import IMP.em as em particles_native = atom.get_leaves(native_assembly) particles_solution = atom.get_leaves(assembly) bb_native = core.get_bounding_box(core.XYZs(particles_native)) bb_solution = core.get_bounding_box(core.XYZs(particles_solution)) # bounding box enclosing both the particles of the native assembly # and the particles of the model bb_union = alg.get_union(bb_native, bb_solution) # add border of 4 voxels border = 4 * voxel_size bottom = bb_union.get_corner(0) bottom += alg.Vector3D(-border, -border, -border) top = bb_union.get_corner(1) top += alg.Vector3D(border, border, border) bb_union = alg.BoundingBox3D(bottom, top) mrw = em.MRCReaderWriter() header = em.create_density_header(bb_union, voxel_size) header.set_resolution(resolution) map_native = em.SampledDensityMap(header) map_native.set_particles(particles_native) map_native.resample() map_solution = em.SampledDensityMap(header) map_solution.set_particles(particles_solution) map_solution.resample() if (write_maps): em.write_map(map_solution, "map_solution.mrc", mrw) em.write_map(map_native, "map_native.mrc", mrw) map_native.calcRMS() map_solution.calcRMS() coarse_cc = em.CoarseCC() # base the calculation of the cross_correlation coefficient on the threshold] # for the native map, because the threshold for the map of the model changes # with each model threshold = 0.25 # threshold AFTER normalization using calcRMS() ccc = coarse_cc.cross_correlation_coefficient(map_solution, map_native, threshold) log.debug( "cross_correlation_coefficient (based on native_map " "treshold %s) %s", threshold, ccc) return ccc
def test__rigid_bodies_drmsd_Q(self): """ Test drmsd_Q measure""" m = IMP.Model() sel = atom.CAlphaPDBSelector() prot1 = atom.read_pdb(self.open_input_file("mini.pdb"), m, sel) prot2 = atom.read_pdb(self.open_input_file("mini.pdb"), m, sel) xyzs1 = core.XYZs(atom.get_leaves(prot1)) xyzs2 = core.XYZs(atom.get_leaves(prot2)) R = IMP.algebra.get_random_rotation_3d() v = IMP.algebra.get_random_vector_in( IMP.algebra.get_unit_bounding_box_3d()) T = IMP.algebra.Transformation3D(R, v) for x in xyzs2: core.transform(x, T) thresholds = [10, 20, 30, 40, 60] for threshold in thresholds: # for x in xyzs2: R = IMP.algebra.get_random_rotation_3d() T = IMP.algebra.Transformation3D(R, v) core.transform(x, T) # test that the function is correctly implemented drmsd = 0. npairs = 0. for i in range(0, len(xyzs1) - 1): for j in range(i + 1, len(xyzs2)): dist0 = IMP.core.get_distance(xyzs1[i], xyzs1[j]) dist1 = IMP.core.get_distance(xyzs2[i], xyzs2[j]) if dist0 <= threshold or dist1 <= threshold: drmsd += (dist0 - dist1)**2 npairs += 1. drmsd = math.sqrt(drmsd / npairs) drmsd_target = atom.get_drmsd_Q(xyzs1, xyzs2, threshold) self.assertAlmostEqual(drmsd, drmsd_target) drmsd_Q = atom.get_drmsd_Q(xyzs1, xyzs2, 1000000.0) drmsd = atom.get_drmsd(xyzs1, xyzs2) self.assertAlmostEqual(drmsd, drmsd_Q)
def write_particles_as_text(leaves, fn_output): """ Writes a set of particles with coordinates to a file """ xyzs = core.XYZs(leaves) f_output = open(fn_output, "w") f_output.write(io.imp_info([IMP, em2d])) f_output.write(get_common_title()) for xyz in xyzs: x, y, z = xyz.get_coordinates() f_output.write("%8.3f %8.3f %8.3f\n" % (x, y, z)) f_output.close()
def test_placement_score(self): """Test placement score""" m = IMP.kernel.Model() # read PDB mp = atom.read_pdb(self.open_input_file("mini.pdb"), m, atom.NonWaterPDBSelector()) mp1 = atom.read_pdb(self.open_input_file("mini.pdb"), m, atom.NonWaterPDBSelector()) xyz = core.XYZs(atom.get_leaves(mp)) xyz1 = core.XYZs(atom.get_leaves(mp1)) # create a random transformation t = IMP.algebra.Transformation3D(IMP.algebra.get_random_rotation_3d(), IMP.algebra.get_random_vector_in(IMP.algebra.get_unit_bounding_box_3d())) for d in xyz1: core.transform(d, t) da = atom.get_placement_score(xyz1, xyz) d = t.get_translation().get_magnitude() a = IMP.algebra.get_axis_and_angle(t.get_rotation()).second self.assertAlmostEqual(da[0], d, 2) self.assertAlmostEqual(da[1], a, 2)
def test__rigid_bodies_drmsd(self): """ Test drmsd measure""" m = IMP.Model() sel = atom.CAlphaPDBSelector() prot1 = atom.read_pdb(self.open_input_file("mini.pdb"), m, sel) prot2 = atom.read_pdb(self.open_input_file("mini.pdb"), m, sel) xyzs1 = core.XYZs(atom.get_leaves(prot1)) xyzs2 = core.XYZs(atom.get_leaves(prot2)) drmsd = atom.get_drmsd(xyzs1, xyzs2) # Molecule with itself self.assertAlmostEqual(drmsd, 0) R = IMP.algebra.get_random_rotation_3d() v = IMP.algebra.get_random_vector_in( IMP.algebra.get_unit_bounding_box_3d()) T = IMP.algebra.Transformation3D(R, v) for x in xyzs2: core.transform(x, T) drmsd = atom.get_drmsd(xyzs1, xyzs2) # Same thing after transformation self.assertAlmostEqual(drmsd, 0) # for x in xyzs2: R = IMP.algebra.get_random_rotation_3d() T = IMP.algebra.Transformation3D(R, v) core.transform(x, T) # test that the function is correctly implemented drmsd = 0. npairs = 0. for i in range(0, len(xyzs1) - 1): for j in range(i + 1, len(xyzs2)): dist0 = IMP.core.get_distance(xyzs1[i], xyzs1[j]) dist1 = IMP.core.get_distance(xyzs2[i], xyzs2[j]) drmsd += (dist0 - dist1)**2 npairs += 1. drmsd1 = math.sqrt(drmsd / npairs) drmsd2 = atom.get_drmsd(xyzs1, xyzs2) self.assertAlmostEqual(drmsd1, drmsd2)
def test__rigid_bodies_drms(self): """ Test drms measure taking into account rigid bodies""" m = IMP.kernel.Model() sel = atom.CAlphaPDBSelector() prot1 = atom.read_pdb(self.open_input_file("mini.pdb"), m, sel) prot2 = atom.read_pdb(self.open_input_file("mini.pdb"), m, sel) hchains1 = atom.get_by_type(prot1, atom.CHAIN_TYPE) hchains2 = atom.get_by_type(prot2, atom.CHAIN_TYPE) xyzs1 = core.XYZs(atom.get_leaves(prot1)) xyzs2 = core.XYZs(atom.get_leaves(prot2)) x = 0 ranges = [] for h in hchains1: ls1 = (atom.get_leaves(h)) y = x + len(ls1) ranges.append((x, y)) x = y drms = atom.get_drms(xyzs1, xyzs2) rb_drms = atom.get_rigid_bodies_drms(xyzs1, xyzs2, ranges) self.assertAlmostEqual(rb_drms, 0) self.assertAlmostEqual( drms, rb_drms, delta=1e-3, msg="rb_drms != drms") # Same thing after transformation of each of the chains for h in hchains2: R = alg.get_random_rotation_3d() v = alg.get_random_vector_in(alg.get_unit_bounding_box_3d()) T = alg.Transformation3D(R, v) ls = atom.get_leaves(h) for l in ls: core.transform(l.get_as_xyz(), T) drms = atom.get_drms(xyzs1, xyzs2) rb_drms = atom.get_rigid_bodies_drms(xyzs1, xyzs2, ranges) self.assertAlmostEqual(drms, rb_drms, delta=0.3, msg="rb_drms != drms")
def test_rigid_body_image_fit_restraint(self): """Test scoring with RigidBodiesImageFitRestraint""" m = IMP.kernel.Model() # read full complex fn = self.get_input_file_name("1z5s.pdb") prot = atom.read_pdb(fn, m, IMP.atom.ATOMPDBSelector()) # read components names = ["1z5sA", "1z5sB", "1z5sC", "1z5sD"] fn_pdbs = [self.get_input_file_name(name + ".pdb") for name in names] components = [ atom.read_pdb(fn, m, IMP.atom.ATOMPDBSelector()) for fn in fn_pdbs ] components_rbs = [atom.create_rigid_body(c) for c in components] # img R = alg.get_identity_rotation_3d() reg = em2d.RegistrationResult(R) img = em2d.Image() img.set_size(80, 80) srw = em2d.SpiderImageReaderWriter() resolution = 5 pixel_size = 1.5 options = em2d.ProjectingOptions(pixel_size, resolution) ls = core.get_leaves(prot) em2d.get_projection(img, ls, reg, options) # img.write("rbfit_test_image.spi",srw) # set restraint score_function = em2d.EM2DScore() rb_fit = em2d.RigidBodiesImageFitRestraint(score_function, components_rbs, img) pp = em2d.ProjectingParameters(pixel_size, resolution) rb_fit.set_projecting_parameters(pp) # set the trivial case: n_masks = 1 for rb in components_rbs: # set as the only possible orientation the one that the rigid # body already has rb_fit.set_orientations(rb, [ rb.get_reference_frame().get_transformation_to().get_rotation( ) ]) self.assertEqual(rb_fit.get_number_of_masks(rb), n_masks, "Incorrect number rigid body masks") # Calculate the positions of the rigid bodies respect to the centroid # of the entire molecule ls = core.get_leaves(prot) xyzs = core.XYZs(ls) centroid = core.get_centroid(xyzs) coords = [rb.get_coordinates() - centroid for rb in components_rbs] for rb, coord in zip(components_rbs, coords): rb.set_coordinates(coord) # Check that the value is a perfect registration m.add_restraint(rb_fit) score = rb_fit.evaluate(False) # print "score ...", score # It seems that projecting with the masks is slightly less accurate # I have to establish a tolerance of 0.03 self.assertAlmostEqual(score, 0, delta=0.03, msg="Wrong value for the score %f " % (score))