示例#1
0
    def test__rigid_bodies_drms(self):
        """ Test drms measure taking into account rigid bodies"""
        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)

        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")
示例#2
0
    def test_docking_multiple_crosslinks(self):
        """
        Test the initial docking that is done based on minimizing the
        distances of the cross-linking restraints
        """
        mydock = bx.InitialDockingFromXlinks()
        xl = self.xlinks.get_xlinks_for_pair(("3sfdC", "3sfdD"))
        mydock.set_xlinks(xl)
        model = IMP.kernel.Model()
        fn_receptor = self.get_input_file_name("3sfdC.pdb")
        h_receptor = atom.read_pdb(fn_receptor, model,
                                   atom.NonWaterNonHydrogenPDBSelector())
        fn_ligand = self.get_input_file_name("3sfdD.pdb")
        h_ligand = atom.read_pdb(fn_ligand, model,
                                 atom.NonWaterNonHydrogenPDBSelector())
        mydock.set_hierarchies(h_receptor, h_ligand)
        p = IMP.kernel.Particle(model)
        core.RigidBody.setup_particle(p, atom.get_leaves(h_receptor))
        rb_receptor = core.RigidBody(p)
        p = IMP.kernel.Particle(model)
        core.RigidBody.setup_particle(p, atom.get_leaves(h_ligand))
        rb_ligand = core.RigidBody(p)
        mydock.set_rigid_bodies(rb_receptor, rb_ligand)
        mydock.move_ligand()

        for res1, res2 in zip([9, 78], [37, 128]):
            c1 = mydock.get_residue_coordinates(h_receptor, "C", res1)
            c2 = mydock.get_residue_coordinates(h_ligand, "D", res2)
            dist = alg.get_distance(c1, c2)
            self.assertLessEqual(dist, self.max_distance + 4.0)
示例#3
0
def get_native_model_info(fn_pdb1):
    """
        Fills the information for the native structure
    """
    model_info = ModelInfo()
    sel = atom.ATOMPDBSelector()
    m1 = IMP.Model()
    hierarchy1 =  atom.read_pdb(fn_pdb1, m1, sel)
    sel = atom.CAlphaPDBSelector()
    malphas1 = IMP.Model()
    alphas1 =  atom.read_pdb(fn_pdb1, malphas1, sel)
    axyzs1 = [core.XYZ(l) for l in atom.get_leaves(alphas1)]
    model_info.rmsd_calpha = 0.0
    model_info.drms_alpha = 0.0
    model_info.rmsd = 0.0
    model_info.nat_over = 100.
    xyzs1 = [core.XYZ(l) for l in atom.get_leaves(hierarchy1)]
    model_info.radius_g = atom.get_radius_of_gyration(xyzs1)
    h_chains1 = atom.get_by_type(hierarchy1, atom.CHAIN_TYPE)
    for hc1 in h_chains1:
        ch1 = atom.Chain(hc1)
        chain_info = ChainInfo()
        chain_info.chain_id = ch1.get_id()
        chain_info.placement_distance = 0.0
        chain_info.placement_angle = 0.0
        chain_info.rmsd =  0.0
        native_overlap_threshold = 10
        chain_info.nat_over = 0.0
        chain_info.model_id = -1
        model_info.chains_infos.append(chain_info)
    model_info.model_id = -1
    return model_info
示例#4
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])
示例#5
0
 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_rigid_body_image_fit_restraint(self):
        """Test scoring with RigidBodiesImageFitRestraint"""
        m = IMP.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
        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))
示例#7
0
    def test_relative_position_mover(self, ):
        """ Test the RelativePositionMover """
        log.info("test RelativePositionMover")
        fn_rec1 = self.get_input_file_name("1suvA_xlinked.pdb")
        fn_rec2 = self.get_input_file_name("1suvC_xlinked.pdb")
        fn_lig = self.get_input_file_name("1suvE_xlinked.pdb")
        fn_tr1  = \
            self.get_input_file_name("transforms-1suvA-1suvE_reduced.txt")
        fn_tr2  = \
            self.get_input_file_name("transforms-1suvC-1suvE_filtered.txt")
        m = IMP.kernel.Model()
        sel = atom.ATOMPDBSelector()
        h_rec1 = atom.read_pdb(fn_rec1, m, sel)
        rb_rec1 = atom.create_rigid_body(h_rec1)
        rec1_coords = [core.XYZ(l).get_coordinates()
                       for l in atom.get_leaves(h_rec1)]
        h_rec2 = atom.read_pdb(fn_rec2, m, sel)
        rb_rec2 = atom.create_rigid_body(h_rec2)
        rec2_coords = [core.XYZ(l).get_coordinates()
                       for l in atom.get_leaves(h_rec2)]
        h_ligand = atom.read_pdb(fn_lig, m, sel)
        rb_lig = atom.create_rigid_body(h_ligand)

        Ts = get_relative_transforms(fn_tr1)
        Tis1 = []
        for i, T in enumerate(Ts):
            V = get_internal_transform3(T, rb_rec1, rb_lig)
            Tis1.append(V)
        docked_refs1 = get_docked_reference_frames(Ts, rb_lig)

        Ts = get_relative_transforms(fn_tr2)
        Tis2 = []
        for i, T in enumerate(Ts):
            V = get_internal_transform3(T, rb_rec2, rb_lig)
            Tis2.append(V)
        docked_refs2 = get_docked_reference_frames(Ts, rb_lig)

        mv = em2d.RelativePositionMover(rb_lig, 10, 20)
        mv.add_internal_transformations(rb_rec1, Tis1)
        mv.add_internal_transformations(rb_rec2, Tis2)

        for i in range(2):
#            prob_random = 0
            ref_before = rb_lig.get_reference_frame()
            ps = mv.propose()  # _move(prob_random)
            ref_after = rb_lig.get_reference_frame()
            found = False
            current_coords = [core.XYZ(l).get_coordinates()
                              for l in atom.get_leaves(h_ligand)]
            # check all possible reference frames where the ligand could be
            for r in itertools.chain(docked_refs1, docked_refs2):
                rb_lig.set_reference_frame(r)
                docked_coords = [core.XYZ(l).get_coordinates()
                                 for l in atom.get_leaves(h_ligand)]
                rmsd = alg.get_rmsd(current_coords, docked_coords)
                if rmsd < 0.1:
                    found = True
            self.assertTrue(found, msg="the proposed move is not "
                            "in the relative solutions")
            mv.accept()
示例#8
0
 def test_drms(self):
     """ Test drms measure """
     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)
     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)
示例#9
0
    def test_docking_one_crosslink(self):
        """
        Test the initial docking that is done based on minimizing the
        distances of the cross-linking restraints
        """
        mydock = bx.InitialDockingFromXlinks()
        xl = self.xlinks.get_xlinks_for_pair(("3sfdB", "3sfdA"))
        mydock.set_xlinks(xl)
        self.assertEqual(len(mydock.xlinks_list), 1)
        mydock.clear_xlinks()
        self.assertEqual(len(mydock.xlinks_list), 0)
        model = IMP.kernel.Model()
        fn_receptor = self.get_input_file_name("3sfdB.pdb")
        h_receptor = atom.read_pdb(fn_receptor, model,
                                   atom.NonWaterNonHydrogenPDBSelector())
        fn_ligand = self.get_input_file_name("3sfdA.pdb")
        h_ligand = atom.read_pdb(fn_ligand, model,
                                 atom.NonWaterNonHydrogenPDBSelector())
        mydock.set_xlinks(xl)
        mydock.set_hierarchies(h_receptor, h_ligand)
        p = IMP.kernel.Particle(model)
        core.RigidBody.setup_particle(p, atom.get_leaves(h_receptor))
        rb_receptor = core.RigidBody(p)
        p = IMP.kernel.Particle(model)
        core.RigidBody.setup_particle(p, atom.get_leaves(h_ligand))
        rb_ligand = core.RigidBody(p)
        mydock.set_rigid_bodies(rb_receptor, rb_ligand)
        mydock.move_ligand()

        c1 = mydock.get_residue_coordinates(h_ligand, "A", 456)
        c2 = mydock.get_residue_coordinates(h_receptor, "B", 23)
        dist = alg.get_distance(c1, c2)
        self.assertLessEqual(dist, self.max_distance)
示例#10
0
    def test_docking_multiple_crosslinks(self):
        """
        Test the initial docking that is done based on minimizing the
        distances of the cross-linking restraints
        """
        mydock = bx.InitialDockingFromXlinks()
        xl = self.xlinks.get_xlinks_for_pair(("3sfdC", "3sfdD"))
        mydock.set_xlinks(xl)
        model = IMP.kernel.Model()
        fn_receptor = self.get_input_file_name("3sfdC.pdb")
        h_receptor = atom.read_pdb(fn_receptor, model,
                                   atom.NonWaterNonHydrogenPDBSelector())
        fn_ligand = self.get_input_file_name("3sfdD.pdb")
        h_ligand = atom.read_pdb(fn_ligand, model,
                                 atom.NonWaterNonHydrogenPDBSelector())
        mydock.set_hierarchies(h_receptor, h_ligand)
        p = IMP.kernel.Particle(model)
        core.RigidBody.setup_particle(p, atom.get_leaves(h_receptor))
        rb_receptor = core.RigidBody(p)
        p = IMP.kernel.Particle(model)
        core.RigidBody.setup_particle(p, atom.get_leaves(h_ligand))
        rb_ligand = core.RigidBody(p)
        mydock.set_rigid_bodies(rb_receptor, rb_ligand)
        mydock.move_ligand()

        for res1, res2 in zip([9, 78], [37, 128]):
            c1 = mydock.get_residue_coordinates(h_receptor, "C", res1)
            c2 = mydock.get_residue_coordinates(h_ligand, "D", res2)
            dist = alg.get_distance(c1, c2)
            self.assertLessEqual(dist, self.max_distance + 4.0)
示例#11
0
    def test_docking_one_crosslink(self):
        """
        Test the initial docking that is done based on minimizing the
        distances of the cross-linking restraints
        """
        mydock = bx.InitialDockingFromXlinks()
        xl = self.xlinks.get_xlinks_for_pair(("3sfdB", "3sfdA"))
        mydock.set_xlinks(xl)
        self.assertEqual(len(mydock.xlinks_list), 1)
        mydock.clear_xlinks()
        self.assertEqual(len(mydock.xlinks_list), 0)
        model = IMP.kernel.Model()
        fn_receptor = self.get_input_file_name("3sfdB.pdb")
        h_receptor = atom.read_pdb(fn_receptor, model,
                                   atom.NonWaterNonHydrogenPDBSelector())
        fn_ligand = self.get_input_file_name("3sfdA.pdb")
        h_ligand = atom.read_pdb(fn_ligand, model,
                                 atom.NonWaterNonHydrogenPDBSelector())
        mydock.set_xlinks(xl)
        mydock.set_hierarchies(h_receptor, h_ligand)
        p = IMP.kernel.Particle(model)
        core.RigidBody.setup_particle(p, atom.get_leaves(h_receptor))
        rb_receptor = core.RigidBody(p)
        p = IMP.kernel.Particle(model)
        core.RigidBody.setup_particle(p, atom.get_leaves(h_ligand))
        rb_ligand = core.RigidBody(p)
        mydock.set_rigid_bodies(rb_receptor, rb_ligand)
        mydock.move_ligand()

        c1 = mydock.get_residue_coordinates(h_ligand, "A", 456)
        c2 = mydock.get_residue_coordinates(h_receptor, "B", 23)
        dist = alg.get_distance(c1, c2)
        self.assertLessEqual(dist, self.max_distance)
示例#12
0
 def set_pdbs(self, fn_receptor, fn_ligand):
     """
         Set the name of the PDB files of the receptor and the ligand
         @param fn_receptor
         @param fn_ligand
     """
     sel = atom.ATOMPDBSelector()
     self.m_receptor = IMP.Model()
     self.h_receptor = atom.read_pdb(fn_receptor, self.m_receptor, sel)
     self.m_ligand = IMP.Model()
     self.h_ligand = atom.read_pdb(fn_ligand, self.m_ligand, sel)
示例#13
0
文件: buildxlinks.py 项目: sirusb/imp
 def set_pdbs(self, fn_receptor, fn_ligand):
     """
         Set the name of the PDB files of the receptor and the ligand
         @param fn_receptor
         @param fn_ligand
     """
     sel = atom.ATOMPDBSelector()
     self.m_receptor = IMP.kernel.Model()
     self.h_receptor = atom.read_pdb(fn_receptor, self.m_receptor, sel)
     self.m_ligand = IMP.kernel.Model()
     self.h_ligand = atom.read_pdb(fn_ligand, self.m_ligand, sel)
示例#14
0
    def test_filter_transformations(self):
        """
            Check if the filtered conformation are the conformations that I
            computed before
        """
        try:
            import subprocess
            import IMP.em2d.buildxlinks as bx
        except ImportError as e:
            self.skipTest(str(e))

        dock = self.import_python_application('emagefit_dock')
        sel = atom.NonWaterNonHydrogenPDBSelector()
        ligand = IMP.kernel.Model()
        fn_ligand = self.get_input_file_name("3sfdB-3sfdA_initial_docking.pdb")
        h_ligand = atom.read_pdb(fn_ligand, ligand, sel)
        rb_ligand = atom.create_rigid_body(h_ligand)
        receptor = IMP.kernel.Model()
        fn_receptor = self.get_input_file_name("3sfdB.pdb")
        h_receptor = atom.read_pdb(fn_receptor, receptor, sel)
        # read_hex_transformations
        fn = self.get_input_file_name("hex_solutions_3sfdB-3sfdA.txt")
        residue_receptor = 23
        residue_ligand = 456
        distance = 30
        xl = bx.Xlink("3sfdB", "B", residue_receptor, "3sfdA", "A",
                      residue_ligand, distance)

        xlinks_list = [xl]
        fn_filtered = "filtered_transforms.txt"
        dock.filter_docking_results(h_receptor, h_ligand, xlinks_list, fn,
                                    fn_filtered)
        fn_stored = self.get_input_file_name(
            "hex_solutions_3sfdB-3sfdA_filtered.txt")
        filtered = dock.read_hex_transforms(fn_filtered)
        stored = dock.read_hex_transforms(fn_stored)
        # check that the filtered transforms match the stored ones
        self.assertEqual(len(filtered), len(stored))
        for Tf, Ts in zip(filtered, stored):
            tf = Tf.get_translation()
            ts = Ts.get_translation()
            qf = Tf.get_rotation().get_quaternion()
            qs = Ts.get_rotation().get_quaternion()
            for k in range(3):
                self.assertAlmostEqual(tf[k], ts[k])
            for k in range(4):
                self.assertAlmostEqual(qf[k], qs[k])
        os.remove(fn_filtered)
示例#15
0
    def test_filter_transformations(self):
        """
            Check if the filtered conformation are the conformations that I
            computed before
        """
        try:
            import subprocess
            import IMP.EMageFit.buildxlinks as bx
        except ImportError as e:
            self.skipTest(str(e))

        dock = self.import_python_application('emagefit_dock')
        sel = atom.NonWaterNonHydrogenPDBSelector()
        ligand = IMP.Model()
        fn_ligand = self.get_input_file_name("3sfdB-3sfdA_initial_docking.pdb")
        h_ligand = atom.read_pdb(fn_ligand, ligand, sel)
        rb_ligand = atom.create_rigid_body(h_ligand)
        receptor = IMP.Model()
        fn_receptor = self.get_input_file_name("3sfdB.pdb")
        h_receptor = atom.read_pdb(fn_receptor, receptor, sel)
        # read_hex_transformations
        fn = self.get_input_file_name("hex_solutions_3sfdB-3sfdA.txt")
        residue_receptor = 23
        residue_ligand = 456
        distance = 30
        xl = bx.Xlink("3sfdB", "B", residue_receptor,
                      "3sfdA", "A", residue_ligand, distance)

        xlinks_list = [xl]
        fn_filtered = "filtered_transforms.txt"
        dock.filter_docking_results(h_receptor, h_ligand, xlinks_list,
                                    fn, fn_filtered)
        fn_stored = self.get_input_file_name(
            "hex_solutions_3sfdB-3sfdA_filtered.txt")
        filtered = dock.read_hex_transforms(fn_filtered)
        stored = dock.read_hex_transforms(fn_stored)
        # check that the filtered transforms match the stored ones
        self.assertEqual(len(filtered), len(stored))
        for Tf, Ts in zip(filtered, stored):
            tf = Tf.get_translation()
            ts = Ts.get_translation()
            qf = Tf.get_rotation().get_quaternion()
            qs = Ts.get_rotation().get_quaternion()
            for k in range(3):
                self.assertAlmostEqual(tf[k], ts[k])
            for k in range(4):
                self.assertAlmostEqual(qf[k], qs[k])
        os.remove(fn_filtered)
    def test_ccs_value(self):
        """Test the calculation of the collision cross section of a complex"""
        IMP.base.set_log_level(IMP.base.TERSE)
        m = IMP.kernel.Model()

        fn = self.get_input_file_name("1z5s.pdb")
        prot = atom.read_pdb(fn, m, atom.ATOMPDBSelector())
        atom.add_radii(prot)
        projections = 20
        resolution = 1.0
        pixel_size = 1.5
        img_size = 80
        ccs = em2d.CollisionCrossSection(
            projections,
            resolution,
            pixel_size,
            img_size)
        ccs.set_model_particles(IMP.atom.get_leaves(prot))
        ccs_calculated = ccs.get_ccs()
        ccs_value = 3838  # A**2
        # good within 2%
        self.assertAlmostEqual(
            ccs_calculated,
            ccs_value,
            delta=ccs_value * 0.02)
示例#17
0
 def test_placement_score(self):
     """Test placement score"""
     m = IMP.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)
示例#18
0
def read_component(model,fn_pdb, name=False):
    """ Read a PDB molecule, add atoms, and set a name
    """
    log.debug("reading component %s from %s", name, fn_pdb)
    hierarchy =  atom.read_pdb(fn_pdb, model,
                                  atom.NonWaterNonHydrogenPDBSelector())
    if(name):
        hierarchy.set_name(name)
    atom.add_radii(hierarchy)
    return hierarchy
示例#19
0
    def test__rigid_bodies_drmsd_Q(self):
        """ Test drmsd_Q measure"""
        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)

        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)
示例#20
0
    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)
示例#21
0
 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)
示例#22
0
    def test__rigid_bodies_drmsd(self):
        """ Test drmsd measure"""
        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)

        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)
示例#23
0
    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)
示例#24
0
def read_component(model, fn_pdb, name=False):
    """ Read a PDB molecule, add atoms, and set a name
    """
    if name:
        log.debug("reading component %s from %s", name, fn_pdb)
    else:
        log.debug("reading component from %s", fn_pdb)

    hierarchy = atom.read_pdb(fn_pdb, model,
                              atom.NonWaterNonHydrogenPDBSelector())
    if name:
        hierarchy.set_name(name)
    atom.add_radii(hierarchy)
    return hierarchy
示例#25
0
    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")
示例#26
0
    def test_ccs_value(self):
        """Test the calculation of the collision cross section of a complex"""
        IMP.set_log_level(IMP.TERSE)
        m = IMP.Model()

        fn = self.get_input_file_name("1z5s.pdb")
        prot = atom.read_pdb(fn, m, atom.ATOMPDBSelector())
        atom.add_radii(prot)
        projections = 20
        resolution = 7.0
        pixel_size = 1.5
        img_size = 80
        ccs = em2d.CollisionCrossSection(projections, resolution, pixel_size,
                                         img_size)
        ccs.set_model_particles(IMP.atom.get_leaves(prot))
        ccs_calculated = ccs.get_ccs()
        ccs_value = 3838  # A**2
        # good within 2%
        self.assertAlmostEqual(ccs_calculated,
                               ccs_value,
                               delta=ccs_value * 0.02)
示例#27
0
                     help="File for logging ")

    args = parser.parse_args()
    if(not args.fn_receptor):
        parser.print_help()
        sys.exit()

    if(args.log):
        logging.basicConfig(filename=args.log, filemode="w")
    else:
        logging.basicConfig(stream=sys.stdout)
    logging.root.setLevel(logging.DEBUG)

    sel = atom.ATOMPDBSelector()
    m = IMP.Model()
    h_receptor =  atom.read_pdb(args.fn_receptor, m, sel)
    rb_receptor = atom.create_rigid_body(h_receptor)
    h_ligand =  atom.read_pdb(args.fn_ligand, m, sel)
    rb_ligand = atom.create_rigid_body(h_ligand)
    if args.dock:
        check_for_hexdock()
        if not args.fn_transforms or not args.fn_internal_transforms:
            raise IOError("Docking requires the --int and --hex arguments")
        hex_docking = HexDocking()
        hex_docking.dock(args.fn_receptor, args.fn_ligand, args.fn_transforms)
        # read the HEX file of solutions and get the internal transformations
        # giving the relative orientation of the ligand respect to the receptor
        Ts = read_hex_transforms(args.fn_transforms)
        rb_receptor = atom.create_rigid_body(h_receptor)
        Tis = [get_internal_transform(T, rb_receptor, rb_ligand) for T in Ts]
        io.write_transforms(Tis, args.fn_internal_transforms)
示例#28
0
## \example em2d/collision_cross_section.py
# Example of how to compute the collision cross section of a molecule.
#

import IMP
import IMP.em2d as em2d
import IMP.atom as atom
"""

Example of how to compute the collision cross section of a molecule

"""

IMP.base.set_log_level(IMP.base.TERSE)
m = IMP.kernel.Model()

fn = em2d.get_example_path("1z5s.pdb")
prot = atom.read_pdb(fn, m, atom.ATOMPDBSelector())
atom.add_radii(prot)

projections = 20
resolution = 1.0
pixel_size = 1.5
img_size = 80
ccs = em2d.CollisionCrossSection(projections, resolution, pixel_size, img_size)
ccs.set_model_particles(IMP.atom.get_leaves(prot))
print "CCS", ccs.get_ccs(), "A**2"
示例#29
0
    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))
示例#30
0
def score_model(complete_fn_model,
                images_sel_file,
                pixel_size,
                n_projections=20,
                resolution=1,
                images_per_batch=250):
    """ Score a model

    Scores a model against the images in the selection file.
    Reads the images in batchs to avoid memory problems
    resolution and pixel_size are used for generating projections of the model
    The finder is an em2d.ProjectionFinder used for projection matching and optimizations
    """
    print "SCORING MODEL:",complete_fn_model
    cwd = os.getcwd()

    images_dir, nil  =  os.path.split(images_sel_file)
    images_names = em2d.read_selection_file(images_sel_file)
    n_images = len(images_names)
    if(n_images == 0):
        raise ValueError(" Scoring with a empty set of images")

    # TYPICAL OPTIMIZER PARAMETERS
    params = em2d.Em2DRestraintParameters(pixel_size, resolution)
    params.coarse_registration_method = em2d.ALIGN2D_PREPROCESSING
    params.optimization_steps = 4
    params.simplex_initial_length = 0.1
    params.simplex_minimum_size=0.02
    params.save_match_images = True
    score_function = em2d.EM2DScore()
    finder = em2d.ProjectionFinder()
    finder.setup(score_function, params)

    # Get the number of rows and cols from the 1st image
    srw = em2d.SpiderImageReaderWriter()
    test_imgs = em2d.read_images([os.path.join(images_dir, images_names[0])], srw)
    rows = test_imgs[0].get_header().get_number_of_columns()
    cols = test_imgs[0].get_header().get_number_of_rows()

    model = IMP.Model()
    ssel = atom.ATOMPDBSelector()
    prot =  atom.read_pdb(complete_fn_model, model, ssel)
    particles = IMP.core.get_leaves(prot)
    # generate projections
    proj_params = em2d.get_evenly_distributed_registration_results(n_projections)
    opts = em2d.ProjectingOptions(pixel_size, resolution)
    projections = em2d.get_projections(particles, proj_params, rows, cols, opts)

    finder.set_model_particles(particles)
    finder.set_projections(projections)
    optimized_solutions= 2
    finder.set_fast_mode(optimized_solutions)
    # read the images in blocks to avoid memory problems
    all_registration_results = []
    init_set = 0
    init_time = time.time()
    while(init_set < n_images):
        end_set = min( init_set + images_per_batch, n_images )
        if(images_dir != ""):
            os.chdir(images_dir)
        subjects = em2d.read_images(images_names[init_set:end_set], srw)
        # register
        finder.set_subjects(subjects)
        os.chdir(cwd)
        finder.get_complete_registration()
        # Recover the registration results:
        registration_results = finder.get_registration_results()
        for reg in registration_results:
            all_registration_results.append(reg)
        init_set += images_per_batch
    os.chdir(cwd)
    em2d.write_registration_results("registration.params", all_registration_results)
    print "score_model: time complete registration",time.time()-init_time
    print "coarse registration time",finder.get_coarse_registration_time()
    print "fine registration time",finder.get_fine_registration_time()
    return all_registration_results
示例#31
0
#***************************


fn_selection = em2d.get_example_path("all-models-1z5s.sel")
fn_em2d_scores = em2d.get_example_path("em2d_scores_for_clustering.data")
# Load models
print "Reading models ..."
model = IMP.Model()
ssel = atom.ATOMPDBSelector()
coords =[]
fn_models = em2d.read_selection_file(fn_selection)
n_models = len(fn_models)
hierarchies=[]
for fn in fn_models:
    fn_model=em2d.get_example_path(fn)
    h=atom.read_pdb(fn_model,model,ssel,True)
    hierarchies.append(h)
    xyz=core.XYZs(atom.get_leaves(h))
    coords.append( [x.get_coordinates() for x in xyz])

print "Computing matrix of RMSD ..."
rmsds=[[0.0 for i in range(0,n_models)] for n in range(0,n_models)]
transformations=[[[] for i in range(0,n_models)]  for j in range(0,n_models)]
# fill rmsd and transformations
for i in xrange(0,n_models):
    for j in xrange(i+1,n_models):
        if(i!=j):
            t=IMP.algebra.get_transformation_aligning_first_to_second(
                                              coords[i],
                                              coords[j])
            transformations[i][j]=t
示例#32
0
## Example of how to compute the collision cross section of a molecule.
##


import IMP
import IMP.em2d as em2d
import IMP.atom as atom

"""

Example of how to compute the collision cross section of a molecule

"""


IMP.base.set_log_level(IMP.base.TERSE)
m = IMP.Model()

fn = em2d.get_example_path("1z5s.pdb")
prot =  atom.read_pdb(fn, m ,atom.ATOMPDBSelector())
atom.add_radii(prot)


projections = 20
resolution = 1.0
pixel_size = 1.5
img_size = 80
ccs = em2d.CollisionCrossSection(projections, resolution, pixel_size, img_size)
ccs.set_model_particles(IMP.atom.get_leaves(prot))
print "CCS",ccs.get_ccs(),"A**2"
            self.count=0
        o=self.get_optimizer()
        m=o.get_model()
        m.show_restraint_score_statistics()
        m.show_all_statistics()
        #for i in range(0,m.get_number_of_restraints()):
        #    r=m.get_restraint(i)
        #    print "restraint",r.get_name(),"value",r.evaluate(False)
    def do_show(self, stream):
        print >> stream, ps


# Get model from PDB file
IMP.base.set_log_level(IMP.base.TERSE)
m = IMP.Model()
prot =  atom.read_pdb(em2d.get_example_path("1z5s.pdb"),m,atom.ATOMPDBSelector())
atom.add_radii(prot)

# get the chains
chains = atom.get_by_type(prot,atom.CHAIN_TYPE)
print "there are",len(chains),"chains in 1z5s.pdb"

# set the chains as rigid bodies
native_chain_centers = []
rigid_bodies= []
for c in chains:
    atoms=core.get_leaves(c)
    rbd=core.RigidBody.setup_particle(c,atoms)
    rigid_bodies.append(rbd)
    print "chain has",rbd.get_number_of_members(), \
                          "atoms","coordinates: ",rbd.get_coordinates()
def create_dockings_from_xlinks(exp):
    """
        Perform dockings that satisfy the cross-linking restraints.
        1) Based on the number of restraints, creates an order for the
           docking between pairs of subunits, favouring the subunits with
           more crosslinks to be the "receptors"
        2) Moves the subunits that are going to be docked to a position that
           satisfies the x-linking restraints. There is no guarantee that this
           position is correct. Its purpose is to help the docking
           algorithm with a clue of the proximity/orientation between subunits
        3) Performs docking between the subunits
        4) Filters the results of the docking that are not consistent with
           the cross-linking restraints
        5) Computes the relative transformations between the rigid bodies
           of the subunits that have been docked
        @param exp Class with the parameters for the experiment
    """
    log.info("Creating initial assembly from xlinks and docking")
    import docking_related as dock
    import buildxlinks as bx
    m = DominoModel.DominoModel()
    m.set_assembly_components(exp.fn_pdbs, exp.names)
    set_xlink_restraints(exp, m)
    order = bx.DockOrder()
    order.set_xlinks(m.xlinks)
    docking_pairs = order.get_docking_order()

    if hasattr(exp, "have_hexdock"):
        if not exp.have_hexdock:
            return

    for rec, lig in docking_pairs:
        pair_xlinks = m.xlinks.get_xlinks_for_pair((rec,lig))
        log.debug("Xlinks for the pair %s %s %s",rec, lig, pair_xlinks)
        h_receptor = representation.get_component(m.assembly, rec)
        h_ligand = representation.get_component(m.assembly, lig)
        rb_receptor = representation.get_rigid_body(m.components_rbs,
                                         representation.get_rb_name(rec))
        rb_ligand = representation.get_rigid_body(m.components_rbs,
                                         representation.get_rb_name(lig))
        initial_ref = rb_ligand.get_reference_frame()
        # move to the initial docking position
        mv = bx.InitialDockingFromXlinks()
        mv.set_xlinks(pair_xlinks)
        mv.set_hierarchies(h_receptor, h_ligand)
        mv.set_rigid_bodies(rb_receptor, rb_ligand)
        mv.move_ligand()
        fn_initial_docking = "%s-%s_initial_docking.pdb" % (rec,lig)
        mv.write_ligand(fn_initial_docking)
        # dock
        hex_docking = dock.HexDocking()
        receptor_index = exp.names.index(rec)
        fn_transforms = "hex_solutions_%s-%s.txt" % (rec, lig)
        fn_docked = "%s-%s_hexdock.pdb" % (rec, lig)
        hex_docking.dock(exp.fn_pdbs[receptor_index],
                         fn_initial_docking, fn_transforms, fn_docked, False)

        sel = atom.ATOMPDBSelector()
        new_m = IMP.Model()
        # After reading the file with the initial solution, the reference frame
        # for the rigid body of the ligand is not necessarily the  same one
        # that it had when saved.
        # Thus reading the file again ensures  consisten results when
        # using the HEXDOCK transforms
        new_h_ligand =  atom.read_pdb(fn_initial_docking, new_m, sel)
        new_rb_ligand = atom.create_rigid_body(new_h_ligand)
        Tlig = new_rb_ligand.get_reference_frame().get_transformation_to()
        fn_filtered = "hex_solutions_%s-%s_filtered.txt" % (rec, lig)
        # h_ligand contains the coordinates of the ligand after moving it
        # to the initial position for the docking
        dock.filter_docking_results(h_receptor, new_h_ligand, pair_xlinks,
                                            fn_transforms, fn_filtered)
        # transforms to apply to the ligand as it is in the file
        # fn_initial_docking
        Thex = dock.read_hex_transforms(fn_filtered)
        Trec = rb_receptor.get_reference_frame().get_transformation_to()
        Tinternal = []
        for i,T in enumerate(Thex):
            Tdock = alg.compose(T, Tlig)
            ref = alg.ReferenceFrame3D(Tdock)
            new_rb_ligand.set_reference_frame(ref)
            # internal transformation. The relationship is Tdock = Trec * Ti
            Ti = alg.compose(Trec.get_inverse(), Tdock)
            Tinternal.append(Ti)
        fn_relative = "relative_positions_%s-%s.txt" % (rec, lig)
        io.write_transforms(Tinternal, fn_relative)
        rb_ligand.set_reference_frame(initial_ref)