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))
Exemplo n.º 2
0
    def read_from_database(self, fn_database, fields=["reference_frames"],
                                    max_number=False, orderby=False ):
        """
            Read orientations and positions from a database file.
            self.anchored and self.fixed overwrite
            the positions and orientations read from the database
        """
        db = solutions_io.ResultsDB()
        db.connect(fn_database)
        data = db.get_solutions(fields, max_number, orderby)
        db.close()
        self.transformations = [ [] for T in range(self.nc)]
        # Each record contains a reference frame for each of the
        # components. But here the states considered make sense as columns.
        # Each rigidbody is considered to have all the states in a column.
        for d in data:
            texts = d[0].split("/")
            for i, t in zip(range(self.nc), texts):
                T = io.TextToTransformation3D(t).get_transformation()
                self.transformations[i].append(T)

        # Set the anchored components
        for i in range(self.nc):
            if self.anchored[i]:
                origin = alg.Transformation3D(alg.get_identity_rotation_3d(),
                                        alg.Vector3D(0.0, 0.0, 0.))
                self.transformations[i] = [origin]

        # If fixed, only the first state is kept
        for i in range(self.nc):
            if self.fixed[i]:
                if len(self.transformations[i]) == 0:
                    raise ValueError("There are positions to keep fixed")
                self.transformations[i] = [self.transformations[i][0]]
Exemplo n.º 3
0
    def __init__(self, model, rigid_bodies, anchored):
        log.info("Setting MonteCarloRelativeMoves")
        self.model = model
        self.rbs = rigid_bodies
        self.components = []
        self.best_models = []
        self.anchored = anchored
        self.parent_rbs = []
        # triplets with the information to build a relative mover using the
        # results from docking with HEX
        self.dock_transforms = None
        self.non_relative_move_prob = 0.1

        log.debug("Anchored components %s", self.anchored)
        T = alg.Transformation3D(alg.get_identity_rotation_3d(),
                                 alg.Vector3D(0., 0., 0.))
        origin = alg.ReferenceFrame3D(T)
        for rb in self.rbs:
            rb.set_reference_frame(origin)
Exemplo n.º 4
0
    def __init__(self, model, rigid_bodies, anchored):
        log.info("Setting MonteCarloRelativeMoves")
        self.model = model
        self.rbs = rigid_bodies
        self.components = []
        self.best_models = []
        self.anchored = anchored
        self.parent_rbs = []
        # triplets with the information to build a relative mover using the
        # results from docking with HEX
        self.dock_transforms = None
        self.non_relative_move_prob = 0.1

        log.debug("Anchored components %s", self.anchored)
        T = alg.Transformation3D(alg.get_identity_rotation_3d(),
                                 alg.Vector3D(0., 0., 0.))
        origin = alg.ReferenceFrame3D(T)
        for rb in self.rbs:
            rb.set_reference_frame(origin)
Exemplo n.º 5
0
def get_orientations_nearby(rotation, n, f):
    """
        Rotations nearby a given one. They are got intepolating with
        the rotations of the uniform coverage. The parameter f
        (0 <= f <= 1) must be close to 0 to get orientations
        that are close to the given orientation
        - Values from 0.1 (tight cluster) to 0.4 (loose)
          seem to be ok
        n - number of rotations requested
    """
    log.debug("Computing nearby rotations around %s", rotation)
    unif = alg.get_uniform_cover_rotations_3d(n)
    id_rot = alg.get_identity_rotation_3d()
    oris = []
    for rot in unif:
        r = alg.get_interpolated(rot, id_rot, f)
        r = alg.compose(r, rotation )
        oris.append(r)
    return oris
Exemplo n.º 6
0
def get_orientations_nearby(rotation, n, f):
    """
        Rotations nearby a given one. They are got intepolating with
        the rotations of the uniform coverage. The parameter f
        (0 <= f <= 1) must be close to 0 to get orientations
        that are close to the given orientation
        - Values from 0.1 (tight cluster) to 0.4 (loose)
          seem to be ok
        n - number of rotations requested
    """
    log.debug("Computing nearby rotations around %s", rotation)
    unif = alg.get_uniform_cover_rotations_3d(n)
    id_rot = alg.get_identity_rotation_3d()
    oris = []
    for rot in unif:
        r = alg.get_interpolated(rot, id_rot, f)
        r = alg.compose(r, rotation)
        oris.append(r)
    return oris
Exemplo n.º 7
0
    def read_from_database(self,
                           fn_database,
                           fields=["reference_frames"],
                           max_number=False,
                           orderby=False):
        """
            Read orientations and positions from a database file.
            self.anchored and self.fixed overwrite
            the positions and orientations read from the database
        """
        if not os.path.exists(fn_database):
            raise IOError(
                "read_from_database: Database file not found. "
                "Are you perhaps trying to run the DOMINO optimization without "
                "having the database yet?")

        db = solutions_io.ResultsDB()
        db.connect(fn_database)
        data = db.get_solutions(fields, max_number, orderby)
        db.close()
        self.transformations = [[] for T in range(self.n_components)]
        # Each record contains a reference frame for each of the
        # components. But here the states considered make sense as columns.
        # Each rigidbody is considered to have all the states in a column.
        for d in data:
            texts = d[0].split("/")
            for i, t in zip(range(self.n_components), texts):
                T = io.TextToTransformation3D(t).get_transformation()
                self.transformations[i].append(T)

        # Set the anchored components
        for i in range(self.n_components):
            if self.anchored[i]:
                origin = alg.Transformation3D(alg.get_identity_rotation_3d(),
                                              alg.Vector3D(0.0, 0.0, 0.))
                self.transformations[i] = [origin]

        # If fixed, only the first state is kept
        for i in range(self.n_components):
            if self.fixed[i]:
                if len(self.transformations[i]) == 0:
                    raise ValueError("There are positions to keep fixed")
                self.transformations[i] = [self.transformations[i][0]]
Exemplo n.º 8
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))