Beispiel #1
0
    def fitness_function(pdb_dic, individual):
        """Calculate the fitness of an individual."""
        # use the chromossome and create the structure!

        # fun fact: if you do not use deepcopy here,
        #  the fitness will depend on the number of processors
        #  since pdb_dic is a shared data structure
        individual_dic = copy.deepcopy(pdb_dic)
        individual_str = ' '.join([f'{j:.2f}' for j in individual])
        c = np.array(individual_dic['B']['coord'])

        translation_center = individual[3:]
        rotation_angles = individual[:3]

        translated_coords = Geometry.translate(c, translation_center)
        rotated_coords = Geometry.rotate(translated_coords, rotation_angles)

        individual_dic['B']['coord'] = list(rotated_coords)

        # use a temporary file to keep the execution simple with
        #  some I/O trade-off
        pdb = NamedTemporaryFile(delete=False, suffix='.pdb')
        for chain in individual_dic:
            coord_l = individual_dic[chain]['coord']
            raw_l = individual_dic[chain]['raw']
            for coord, line in zip(coord_l, raw_l):
                new_x, new_y, new_z = format_coords(coord)
                new_line = (f'{line[:30]} {new_x} {new_y} {new_z} '
                            f'{line[55:]}' + os.linesep)
                pdb.write(str.encode(new_line))
        pdb.close()

        # Calculate fitnesses!
        # ================================#
        # energy = run_dcomplex(pdb.name)
        satisfaction = calc_satisfaction(pdb.name,
                                         individual_dic['A']['restraints'],
                                         individual_dic['B']['restraints'])
        # ================================#

        # unlink the pdb so that it disappears
        os.unlink(pdb.name)

        # return [satisfaction, energy]
        ga_log.debug(f'{individual_str} {satisfaction:.2f} {pdb.name}')
        # this must (?) be a list: github.com/DEAP/deap/issues/256
        return [satisfaction]
Beispiel #2
0
    def _recreate(input_structure_dic, individual, pdb_name):
        """Use the chromossome information and create the structure."""
        input_dic = copy.deepcopy(input_structure_dic)
        c = np.array(input_dic['B']['coord'])

        translation_center = individual[3:]
        rotation_angles = individual[:3]

        translated_coords = Geometry.translate(c, translation_center)
        rotated_coords = Geometry.rotate(translated_coords, rotation_angles)

        input_dic['B']['coord'] = list(rotated_coords)

        with open(pdb_name, 'w') as fh:
            for chain in input_dic:
                coord_l = input_dic[chain]['coord']
                raw_l = input_dic[chain]['raw']
                for coord, line in zip(coord_l, raw_l):
                    new_x, new_y, new_z = format_coords(coord)
                    new_line = (f'{line[:30]} {new_x} {new_y} {new_z}'
                                f' {line[55:]}' + os.linesep)
                    fh.write(new_line)
        fh.close()
Beispiel #3
0
class TestGeometry(unittest.TestCase):
    def setUp(self):
        test_input_data = PDB()
        test_input_data.load(f'{data_folder}/molA.pdb')
        test_input_data.load(f'{data_folder}/molB.pdb')

        test_restraints = Restraint(test_input_data.raw_pdb)
        test_restraints.load([1, 2, 3], 'A')
        test_restraints.load([2, 3, 4], 'B')

        self.Geometry = Geometry(test_input_data, test_restraints)

    def test_calc_initial_position(self):
        self.Geometry.calc_initial_position()
        expected_ligand_coord = np.array(
            [[4.41488596, 6.18960229, 2.7898511],
             [5.38935079, 4.86187319, 6.26942201],
             [3.13271966, 7.23145808, 8.22975065],
             [-0.11367046, 5.48686873, 9.2551331],
             [-3.37010188, 7.31580583, 9.73693127]])
        expected_receptor_coord = np.array([[-5.0052, 0.9464, 0.611],
                                            [-1.9152, 2.3144, 2.455],
                                            [0.3288, -0.6806, 1.717],
                                            [2.6488, 0.0484, -1.236],
                                            [3.9428, -2.6286, -3.547]])

        observed_ligand_coord = self.Geometry.ligand_coord
        observed_receptor_coord = self.Geometry.receptor_coord

        self.assertTrue(
            np.allclose(observed_ligand_coord, expected_ligand_coord))
        self.assertTrue(
            np.allclose(observed_receptor_coord, expected_receptor_coord))

    def test_apply_transformation(self):
        observed_tidy_complex = self.Geometry.apply_transformation()
        observed_tidy_complex = observed_tidy_complex.split(os.linesep)

        with open(f'{data_folder}/tidy_transformed.pdb', 'r') as test_tidy_fh:
            expected_tidy_complex = ''.join(test_tidy_fh.readlines())
        test_tidy_fh.close()

        expected_tidy_complex = expected_tidy_complex.split(os.linesep)
        self.assertEqual(len(observed_tidy_complex),
                         len(expected_tidy_complex))
        for observed_line, expected_line in zip(observed_tidy_complex,
                                                expected_tidy_complex):
            self.assertEqual(observed_line, expected_line)

    def test_translate(self):
        coords = np.array([[34.082, -15.413, 35.895],
                           [34.912, -15.074, 34.747]])
        point = [-1, +2, -3]
        observed_trans_coords = self.Geometry.translate(coords, point)
        expected_trans_coords = np.array([[33.082, -13.413, 32.895],
                                          [33.912, -13.074, 31.747]])
        self.assertTrue(
            np.allclose(observed_trans_coords, expected_trans_coords))

    def test_rotate(self):
        coords = np.array([[34.082, -15.413, 35.895],
                           [34.912, -15.074, 34.747]])
        rotation = [327, 57, 12]
        observed_rot_coords = self.Geometry.rotate(coords, rotation)
        expected_rot_coords = np.array(
            [[34.42920652, -15.11616826, 36.03487809],
             [34.56479348, -15.37083174, 34.60712191]])

        self.assertTrue(np.allclose(observed_rot_coords, expected_rot_coords))