def test_get_random(self):
        q1 = Quaternion.random()
        assert_almost_equals(1.0, q1.norm())

        rng = MTGenerator(1379)
        q2 = Quaternion.random(rng)
        expected = Quaternion(0.53224081, -0.00589472, 0.84280352, 0.07979471)

        assert expected == q2
    def test_get_random(self):
        q1 = Quaternion.random()
        assert_almost_equals(1.0, q1.norm())

        rng = MTGenerator(1379)
        q2 = Quaternion.random(rng)
        expected = Quaternion(0.53224081, -0.00589472, 0.84280352, 0.07979471)

        assert expected == q2
Exemple #3
0
def populate_poses(to_generate,
                   center,
                   radius,
                   number_generator,
                   rec_translation,
                   lig_translation,
                   rng_nm=None,
                   rec_nm=0,
                   lig_nm=0,
                   receptor_restraints=None,
                   ligand_restraints=None,
                   ligand_diameter=1.):
    """Creates new poses around a given center and a given radius"""
    new_poses = []

    # Flatten if necessary receptor_restraints
    if receptor_restraints:
        try:
            receptor_restraints = receptor_restraints[
                'active'] + receptor_restraints['passive']
        except TypeError:
            pass

    # Calculate closest residue restraints
    closest_residues = []
    if receptor_restraints:
        distances = []
        for i, residue in enumerate(receptor_restraints):
            ca = residue.get_calpha()
            if not ca:
                ca = residue.get_atom('P')
            distances.append(
                (i, cdistance(ca.x, ca.y, ca.z, center[0], center[1],
                              center[2])))
        distances.sort(key=lambda tup: tup[1])
        closest_residues = [x[0] for x in distances[:10]]

    for _ in range(to_generate):
        # First calculate a random translation within the swarm sphere
        x, y, z = get_random_point_within_sphere(number_generator, radius)
        tx = center[0] + x
        ty = center[1] + y
        tz = center[2] + z

        # Restraints in both partners
        if receptor_restraints and ligand_restraints:
            # We select one of the closest residue restraints to point the quaternion
            rec_residue = receptor_restraints[closest_residues[
                number_generator.randint(0,
                                         len(closest_residues) - 1)]]
            # Random restraint on the ligand to use for pre-orientation
            lig_residue = ligand_restraints[number_generator.randint(
                0,
                len(ligand_restraints) - 1)]
            # Calculate the quaternion which rotates the ligand to point to the given receptor restraint
            q = get_quaternion_for_restraint(rec_residue, lig_residue, tx, ty,
                                             tz, rec_translation,
                                             lig_translation)

        # Only restraints in the ligand partner
        elif ligand_restraints and not receptor_restraints:
            # The strategy is similar to previous but for the receptor side we will use a simulated point
            # over the receptor surface to point out the quaternion
            coef = norm(center) / ligand_diameter
            if coef > 1.0:
                raise LightDockWarning(
                    'Found wrong coefficient on calculating poses with restraints'
                )
            # It is important to keep the coordinates as in the original complex without
            # moving to the center of coordinates (applying translation)
            rec_residue = Residue.dummy(center[0] * coef - rec_translation[0],
                                        center[1] * coef - rec_translation[1],
                                        center[2] * coef - rec_translation[2])

            lig_residue = ligand_restraints[number_generator.randint(
                0,
                len(ligand_restraints) - 1)]
            q = get_quaternion_for_restraint(rec_residue, lig_residue, tx, ty,
                                             tz, rec_translation,
                                             lig_translation)
        # No restraints at all
        else:
            q = Quaternion.random(number_generator)

        # Glowworm's optimization vector
        op_vector = [tx, ty, tz, q.w, q.x, q.y, q.z]

        # If ANM is enabled, we need to create random components for the extents
        if rng_nm:
            if rec_nm > 0:
                op_vector.extend([rng_nm() for _ in range(rec_nm)])
            if lig_nm > 0:
                op_vector.extend([rng_nm() for _ in range(lig_nm)])

        new_poses.append(op_vector)

    return new_poses