Exemplo n.º 1
0
    def test_fail_read(self):
        """
        Test the bad formating of a gro file
        """

        files = ["fail.gro", "fail_100000.gro", "missing_total_atoms.gro"]
        files_desc = [os.path.join(REFDIR, filin) for filin in files]
        print(files_desc)

        for filin in files_desc:
            with self.assertRaises(groIO.FormatError) as context:
                groIO.parse_file(filin)
                self.assertEqual("Something is wrong in the format", context.exception)
Exemplo n.º 2
0
    def test_raise_ref_objects(self):
        """
        Test if the exception is raised when reference objects (atom or residue)
        are missing in the file.
        """

        path = os.path.join(REFDIR, "regular.gro")
        title, atoms, box = groIO.parse_file(path)

        # Lipid atom
        with self.assertRaises(grw.RefObjectError) as context:
            grw.perform_bilayer_removing(atoms, "P12", "OW", verbose=False)

        # Water atom
        with self.assertRaises(grw.RefObjectError) as context:
            grw.perform_bilayer_removing(atoms, "P1", "IEW", verbose=False)

        # Sphere residue
        with self.assertRaises(grw.RefObjectError) as context:
            grw.perform_sphere_removing(atoms,
                                        "AZE",
                                        0.4,
                                        "SOL",
                                        verbose=False)

        # Water residue
        with self.assertRaises(grw.RefObjectError) as context:
            grw.perform_sphere_removing(atoms,
                                        "POPC",
                                        0.4,
                                        "SO",
                                        verbose=False)
Exemplo n.º 3
0
    def test_z_mean_values(self):
        """
        Test the mean Z values returned for each leaflet by z_mean_values.

        The result of the function is compared with the result obtained using
        the g_traj gromacs tool with the following command::

            echo 0 1 | g_traj -f regular.gro -s regular.gro -n leaflets.ndx  \
                    -ox regular_com -com -ng 2
        """
        reference = (2.14186, 5.71483)
        # For the record, values for X are (2.23333, 2.5495), values for Y are
        # (2.688, 2.50058)

        path = os.path.join(REFDIR, "regular.gro")
        title, atoms, box = groIO.parse_file(path)

        z_low, z_top = grw.z_mean_values(atoms, "P1")
        self.assertAlmostEqual(
            reference[0], z_low, PRECISION,
            ("Z mean value for the lower leaflet do not "
             "match with {0} places: {1} instead of {2}").format(
                 PRECISION, reference[0], z_low))
        self.assertAlmostEqual(
            reference[1], z_top, PRECISION,
            ("Z mean value for the upper leaflet do not "
             "match with {0} places: {1} instead of {2}").format(
                 PRECISION, reference[1], z_top))
Exemplo n.º 4
0
    def test_z_mean_values(self):
        """
        Test the mean Z values returned for each leaflet by z_mean_values.

        The result of the function is compared with the result obtained using
        the g_traj gromacs tool with the following command::

            echo 0 1 | g_traj -f regular.gro -s regular.gro -n leaflets.ndx  \
                    -ox regular_com -com -ng 2
        """
        reference = (2.14186, 5.71483)
        # For the record, values for X are (2.23333, 2.5495), values for Y are
        # (2.688, 2.50058)

        path = os.path.join(REFDIR, "regular.gro")
        title, atoms, box = groIO.parse_file(path)

        z_low, z_top = grw.z_mean_values(atoms, "P1")
        self.assertAlmostEqual(reference[0], z_low, PRECISION,
                               ("Z mean value for the lower leaflet do not "
                                "match with {0} places: {1} instead of {2}")
                               .format(PRECISION, reference[0], z_low))
        self.assertAlmostEqual(reference[1], z_top, PRECISION,
                               ("Z mean value for the upper leaflet do not "
                                "match with {0} places: {1} instead of {2}")
                               .format(PRECISION, reference[1], z_top))
Exemplo n.º 5
0
    def test_get_resids(self):
        """
        Test the mapping of resids from resnames
        """
        path = os.path.join(REFDIR, "regular.gro")
        title, atoms, box = groIO.parse_file(path)

        resname = ["POPC"]
        resids = list(range(1, 73))

        self.assertEqual(grw.get_resids(atoms, resname), resids)
Exemplo n.º 6
0
    def test_get_resids(self):
        """
        Test the mapping of resids from resnames
        """
        path = os.path.join(REFDIR, "regular.gro")
        title, atoms, box = groIO.parse_file(path)

        resname = ["POPC"]
        resids = list(range(1,73))

        self.assertEqual(grw.get_resids(atoms, resname), resids)
Exemplo n.º 7
0
    def test_renumber(self):
        """
        Test the atom renumbering with the renumber function
        """
        path = os.path.join(REFDIR, "regular.gro")
        title, atoms, box = groIO.parse_file(path)

        removed_res = (10, 50, 60)
        # Remove some residues and renumber atoms and residues
        renumbered = _create_runumbered(atoms, removed_res)
        # Check numbering
        # (number of atom per residue, number of residue)
        topology = ((52, 72 - len(removed_res)), (3, 3739))
        _test_renumber(renumbered, topology)
Exemplo n.º 8
0
def main():
    """
    Run everythin from the command line
    """

    # Command line parsing
    args = define_options(sys.argv[1:])

    try:
        title, atoms, box = groIO.parse_file(args.filin)
    except groIO.FormatError as e:
        print(e, file=sys.stderr)
        return 1

    if args.verbose:
        print("The input coordinate file is {0}".format(args.filin))
        print("The output file will be {0}".format(args.filout))
        print("The reference atom for the lipid bilayer is {0}".format(
            args.lipid_atom))
        print("The reference atom for the water is {0}".format(
            args.water_atom))

    try:
        if args.sphere is None:
            output_atoms, nb_water = perform_bilayer_removing(
                atoms, args.lipid_atom, args.water_atom, args.verbose)
        else:
            output_atoms, nb_water = perform_sphere_removing(
                atoms, args.sphere, args.radius, args.water_residue,
                args.verbose)

        if args.verbose:
            print()

        print("The old system contained {0} atoms.".format(len(atoms)))
        print("{0} water molecules have been removed.".format(nb_water))
        print("The new system contains {0} atoms.".format(len(output_atoms)))

        # Write in the output file
        with open(args.filout, "w") as fout:
            for line in groIO.write_gro(title, output_atoms, box):
                print(line, end='', file=fout)

    except RefObjectError as e:
        print(e)
        return 1

    return 0
Exemplo n.º 9
0
    def test_find_ref_atom_regular(self):
        """
        Test find_ref_atom function in the regular case where the reference
        atom is present or absent from the file body.
        """
        path = os.path.join(REFDIR, "regular.gro")
        title, atoms, box = groIO.parse_file(path)

        # P1 is present in the file
        self.assertTrue(grw.find_ref_atom(atoms, "P1"),
                        "P1 should be find in {0} body.".format(path))
        # ABS is absent from the file
        self.assertFalse(grw.find_ref_atom(atoms, "ABS"),
                         "ABS should not be find in {0} body.".format(path))
        # AA is present at the right place in the header but should be ignored
        self.assertFalse(grw.find_ref_atom(atoms, "AA"),
                         "AA should be ignored in {0} head.".format(path))
Exemplo n.º 10
0
    def test_find_ref_atom_regular(self):
        """
        Test find_ref_atom function in the regular case where the reference
        atom is present or absent from the file body.
        """
        path = os.path.join(REFDIR, "regular.gro")
        title, atoms, box = groIO.parse_file(path)

        # P1 is present in the file
        self.assertTrue(grw.find_ref_atom(atoms, "P1"),
                        "P1 should be find in {0} body.".format(path))
        # ABS is absent from the file
        self.assertFalse(grw.find_ref_atom(atoms, "ABS"),
                         "ABS should not be find in {0} body.".format(path))
        # AA is present at the right place in the header but should be ignored
        self.assertFalse(grw.find_ref_atom(atoms, "AA"),
                         "AA should be ignored in {0} head.".format(path))
Exemplo n.º 11
0
def main():
    """
    Run everythin from the command line
    """

    # Command line parsing
    args = define_options(sys.argv[1:])

    try:
        title, atoms, box = groIO.parse_file(args.filin)
    except groIO.FormatError as e:
        print(e, file=sys.stderr)
        return 1

    if args.verbose:
        print("The input coordinate file is {0}".format(args.filin))
        print("The output file will be {0}".format(args.filout))
        print("The reference atom for the lipid bilayer is {0}".format(args.lipid_atom))
        print("The reference atom for the water is {0}".format(args.water_atom))

    try:
        if args.sphere is None:
            output_atoms, nb_water = perform_bilayer_removing(atoms, args.lipid_atom,
                                                              args.water_atom, args.verbose)
        else:
            output_atoms, nb_water = perform_sphere_removing(atoms, args.sphere, args.radius,
                                                             args.water_residue, args.verbose)

        if args.verbose:
            print()

        print("The old system contained {0} atoms.".format(len(atoms)))
        print("{0} water molecules have been removed.".format(nb_water))
        print("The new system contains {0} atoms.".format(len(output_atoms)))

        # Write in the output file
        with open(args.filout, "w") as fout:
            for line in groIO.write_gro(title, output_atoms, box):
                print(line, end='', file=fout)

    except RefObjectError as e:
        print(e)
        return 1

    return 0
Exemplo n.º 12
0
    def test_geometric_center(self):
        """
        Test the geometric center calculation.

        Reference coordinates were calculated with g_traj. The output of the
        program is available in test_resources/center.xvg.
        """
        reference = {"x": 0.000216667, "y": 0.00045, "z": 5.00003e-05}
        resids = [1]
        path = os.path.join(REFDIR, "center.gro")
        title, atoms, box = groIO.parse_file(path)

        center = grw.geometric_center(atoms, resids)

        for ref, value in zip(reference.values(), center[resids[0]].values()):
            self.assertAlmostEqual(ref, value, PRECISION,
                                   ("Geometric center is wrong: "
                                    "{0} instead of {1}").format(
                                        center, reference))
Exemplo n.º 13
0
    def test_geometric_center(self):
        """
        Test the geometric center calculation.

        Reference coordinates were calculated with g_traj. The output of the
        program is available in test_resources/center.xvg.
        """
        reference = {"x": 0.000216667, "y": 0.00045, "z": 5.00003e-05}
        resids = [1]
        path = os.path.join(REFDIR, "center.gro")
        title, atoms, box = groIO.parse_file(path)

        center = grw.geometric_center(atoms, resids)

        for ref, value in zip(reference.values(), center[resids[0]].values()):
            self.assertAlmostEqual(ref, value, PRECISION,
                                   ("Geometric center is wrong: "
                                    "{0} instead of {1}")
                                   .format(center, reference))
Exemplo n.º 14
0
    def test_read(self):
        """
        Test the reading of a regular gro file
        """

        path = os.path.join(REFDIR, "regular.gro")
        title, atoms, box = groIO.parse_file(path)

        nb_atoms = len(atoms)

        self.assertEqual(title, "Regular      AA\n")
        self.assertEqual(box, "   5.15414   5.15414   7.93645\n")
        self.assertEqual(nb_atoms, 14961)

        # Pick a random atom
        # 24POPC  C220 1235   2.520   4.888   3.113
        atom = atoms[1234]
        keys_tested = ['resid', 'atomid', 'x', 'z']
        values_tested = [24, 1235, 2.520, 3.113]

        for key, value in zip(keys_tested, values_tested):
            self.assertEqual(atom[key], value)
Exemplo n.º 15
0
    def test_raise_ref_objects(self):
        """
        Test if the exception is raised when reference objects (atom or residue)
        are missing in the file.
        """

        path = os.path.join(REFDIR, "regular.gro")
        title, atoms, box = groIO.parse_file(path)

        # Lipid atom
        with self.assertRaises(grw.RefObjectError) as context:
            grw.perform_bilayer_removing(atoms, "P12", "OW", verbose=False)

        # Water atom
        with self.assertRaises(grw.RefObjectError) as context:
            grw.perform_bilayer_removing(atoms, "P1", "IEW", verbose=False)

        # Sphere residue
        with self.assertRaises(grw.RefObjectError) as context:
            grw.perform_sphere_removing(atoms, "AZE", 0.4, "SOL", verbose=False)

        # Water residue
        with self.assertRaises(grw.RefObjectError) as context:
            grw.perform_sphere_removing(atoms, "POPC", 0.4, "SO", verbose=False)