def test_compare_coordinates(self):
        coordinates1 = model.Coordinates(11.01, 12.02, 13.03)
        coordinates2 = model.Coordinates(11.01, 12.02, 13.03)
        coordinates3 = model.Coordinates(15.01, 14.02, 13.03)

        self.assertEqual(coordinates1, coordinates2)
        self.assertNotEqual(coordinates1, coordinates3)
    def test_norm(self):
        coordinates1 = model.Coordinates(1.0, 0.0, 0.0)
        coordinates2 = model.Coordinates(3.0, 4.0, 0.0)
        coordinates3 = model.Coordinates(-1.0, 0.0, 1.0)

        self.assertEqual(1.0, coordinates1.norm())
        self.assertEqual(5.0, coordinates2.norm())
        self.assertEqual(math.sqrt(2.0), coordinates3.norm())
    def __make_example_molecule(self):
        coordinatesH1 = model.Coordinates(0.0, 0.0, 0.95)
        coordinatesH2 = model.Coordinates(0.89567, 0.00, -0.316663)
        coordinatesO1 = model.Coordinates(0.0, 0.0, 0.0)
        coordinatesO2 = model.Coordinates(4.0, 0.0, 0.0)

        atomH1 = model.Atom("H", coordinatesH1)
        atomH2 = model.Atom("H", coordinatesH2)
        atomO1 = model.Atom("O", coordinatesO1)
        atomO2 = model.Atom("O", coordinatesO2)

        return model.Molecule([atomH1, atomH2, atomO1, atomO2], "MOL")
    def test_compare_molecules(self):
        coordinates1 = model.Coordinates()
        coordinates2 = model.Coordinates(1.0, 0.0, 0.0)
        coordinates3 = model.Coordinates(0.5, 1.0, 0.0)

        atom1 = model.Atom("H", coordinates1)
        atom2 = model.Atom("H", coordinates2)
        atom3 = model.Atom("H", coordinates3)

        molecule1 = model.Molecule([atom1, atom2], "MOL")
        molecule2 = model.Molecule([atom2, atom1], "MOL")
        molecule3 = model.Molecule([atom1, atom2, atom3], "MOL")

        self.assertEqual(molecule1, molecule2)
        self.assertNotEqual(molecule2, molecule3)
    def test_distance(self):
        coordinates1 = model.Coordinates(1.0, 0.0, 0.0)
        coordinates2 = model.Coordinates(1.0, 0.0, 0.0)
        distance12 = coordinates1.calculate_distance(coordinates2)

        self.assertEqual(0.0, distance12)

        coordinates3 = model.Coordinates(4.0, 4.0, 0.0)
        distance13 = coordinates1.calculate_distance(coordinates3)
        self.assertEqual(5.0, distance13)

        coordinates4 = model.Coordinates(-3.0, 0.0, 4.0)
        distance14 = coordinates1.calculate_distance(coordinates4)
        distance43 = coordinates4.calculate_distance(coordinates3)
        self.assertGreaterEqual(distance14 + distance43, distance13)
Example #6
0
    def test_xyz_tinker_reader(self):
        reader = readingutils.InputReader(PACKMOL_TINKER_FILE_NAME,
                                          tinker_format=True)
        reader.parse_packmol_input()

        system = reader.read_xyz_data()
        f_atom = model.Atom("F",
                            model.Coordinates(1.714650, -1.415751, 0.421888),
                            -0.13, 18.9984, "Ff")

        molecule = system.molecules[0][0]
        atoms = molecule.atoms
        self.assertIn(f_atom, atoms)
        self.assertEqual(system.xyz_file_name, "fsi-tinker/fsi-res.xyz")

        bonds = molecule.bonds
        self.assertIn((0, 3), bonds)
        self.assertIn((5, 8), bonds)

        angles = molecule.angles
        self.assertIn((4, 5, 6), angles)
        self.assertIn((1, 0, 4), angles)

        dihedrals = molecule.dihedrals
        self.assertIn((3, 0, 4, 5), dihedrals)
        self.assertIn((1, 0, 4, 5), dihedrals)
    def test_compare_atoms(self):
        coordinates = model.Coordinates(12.03, 11.01, 10.02)
        atom1 = model.Atom("C", coordinates)
        atom2 = model.Atom("C", coordinates)
        atom3 = model.Atom("Ce", coordinates)

        self.assertEqual(atom1, atom2)
        self.assertNotEqual(atom1, atom3)
    def test_determine_dihedrals(self):
        coordinatesO1 = model.Coordinates(0.0, 0.0, 0.0)
        coordinatesO2 = model.Coordinates(0.0, 0.0, 1.48)
        coordinatesH1 = model.Coordinates(0.895669, 0.0, -0.316667)
        coordinatesH2 = model.Coordinates(-0.895669, 0.0, 1.796667)

        atomO1 = model.Atom("O", coordinatesO1)
        atomO2 = model.Atom("O", coordinatesO2)
        atomH1 = model.Atom("H", coordinatesH1)
        atomH2 = model.Atom("H", coordinatesH2)

        molecule = model.Molecule([atomO1, atomO2, atomH1, atomH2], "MOL")
        molecule.determine_bonds(1.50)
        molecule.determine_angles()
        molecule.determine_dihedrals()

        dihedrals = molecule.dihedrals
        expected_dihedrals = ((2, 0, 1, 3),)
        self.assertEqual(dihedrals, expected_dihedrals)
Example #9
0
    def test_xyz_default_reader(self):
        reader = readingutils.InputReader(PACKMOL_FILE_NAME)
        reader.parse_packmol_input()

        system = reader.read_xyz_data()
        li_atom = model.Atom("Li", model.Coordinates(0.0, 0.0, 0.0), 1.00,
                             6.997, "kLi")
        molecule = model.Molecule([li_atom], "LI")

        molecules = system.molecules
        self.assertEqual(molecules[0][0], molecule)
        self.assertEqual(system.xyz_file_name, "li-ec/li-ec-01.xyz")
    def __read_xyz_data_tinker(self):
        molecules = []

        for (xyz_file_name, molecule_count) in self._xyz_data:
            xyz_file = open(xyz_file_name, 'r')

            atoms_number = int(xyz_file.readline().split()[0])
            atoms = []

            for xyz_line in xyz_file:
                xyz_line = xyz_line.split()
                if len(xyz_line) >= self.COORDINATE_LINE_COLUMNS_TINKER:
                    atom_symbol = xyz_line[1]
                    atom_coordinates = model.Coordinates(
                        float(xyz_line[2]), float(xyz_line[3]),
                        float(xyz_line[4]))

                    atoms.append(model.Atom(atom_symbol, atom_coordinates))

            xyz_file.close()

            if len(atoms) != atoms_number:
                print(
                    '[WARNING] Difference between declared and read atoms number in file {}, declared: {}, read: {}'
                    .format(xyz_file_name, atoms_number, len(atoms)))

            molecule = model.Molecule(atoms)
            self.__read_dat_data(xyz_file_name.replace('.xyz', '.dat'),
                                 molecule)

            if molecule.atoms_number > 1:
                self.__read_conn_data(xyz_file_name.replace('.xyz', '.conn'),
                                      molecule)

            molecules.append((molecule, molecule_count))

        system = model.System(molecules, self._packmol_output_name)
        return system
    def __read_xyz_data_default(self):
        molecules = []

        for (xyz_file_name, molecule_count) in self._xyz_data:
            xyz_file = open(xyz_file_name, 'r')

            atoms_number = int(xyz_file.readline())
            xyz_file.readline()  # omit commentary line in XYZ file
            atoms = []

            for xyz_line in xyz_file:
                xyz_line = xyz_line.split()
                if len(xyz_line) >= self.COORDINATE_LINE_COLUMNS:
                    atom_symbol = xyz_line[0]
                    atom_coordinates = model.Coordinates(
                        float(xyz_line[1]), float(xyz_line[2]),
                        float(xyz_line[3]))

                    atoms.append(model.Atom(atom_symbol, atom_coordinates))

            xyz_file.close()

            if len(atoms) != atoms_number:
                print(
                    '[WARNING] Difference between declared and read atoms number in file {}, declared: {}, read: {}'
                    .format(xyz_file_name, atoms_number, len(atoms)))

            molecule = model.Molecule(atoms)
            self.__read_dat_data(xyz_file_name.replace('.xyz', '.dat'),
                                 molecule)

            molecule.determine_angles()
            molecule.determine_dihedrals()
            molecules.append((molecule, molecule_count))

        system = model.System(molecules, self._packmol_output_name)
        return system
    def save_to_file(self):
        xyz_file = open(self._system.xyz_file_name, 'r')

        occupancy = 0.00
        temperature_factor = 0.00
        atom_number = 1

        # check if number of atoms in .xyz matches number given in system
        atoms_number = int(xyz_file.readline())
        if atoms_number != self._system.atoms_number:
            print(
                '[ERROR] Number of atoms calculated from Packmol input does not match number of atoms in .xyz file'
            )
            xyz_file.close()
            return

        # write header line in PDB
        self._output_file.write(self.FIRST_LINE)

        # omit commentary line in .xyz
        xyz_file.readline()

        for (molecule, counter) in self._system.molecules:
            residue_id = 1

            for i in range(0, counter):
                for atom in molecule.atoms:
                    xyz_line = xyz_file.readline().split()

                    if xyz_line[0] != atom.symbol:
                        print(
                            '[ERROR] No match between atoms in line {}, symbol is {}, expected {}'
                            .format(xyz_line, xyz_line[0], atom.symbol))
                        xyz_file.close()
                        self._output_file.close()
                        return

                    coordinates = model.Coordinates(float(xyz_line[1]),
                                                    float(xyz_line[2]),
                                                    float(xyz_line[3]))
                    self._output_file.write(
                        self.LINE_FORMAT.format(atom_number, atom.namd_symbol,
                                                molecule.residue_name,
                                                residue_id, coordinates.x,
                                                coordinates.y, coordinates.z,
                                                occupancy, temperature_factor,
                                                self._system.segment_id))

                    drude_atom = atom.drude_atom
                    if drude_atom is not None:
                        atom_number += 1
                        self._output_file.write(
                            self.LINE_FORMAT.format(
                                atom_number, drude_atom.namd_symbol,
                                molecule.residue_name, residue_id,
                                coordinates.x, coordinates.y, coordinates.z,
                                occupancy, temperature_factor,
                                self._system.segment_id))

                    atom_number += 1
                residue_id += 1

        self._output_file.write('END\n')

        xyz_file.close()
        self._output_file.close()
        print('PDB file successfully written')