Exemplo n.º 1
0
 def test_read_gro(self):
     """
     Test gro file reading on a very simple case.
     """
     path = os.path.join(REFDIR, "simple.gro")
     reference = [
         {"resid":1, "resname":"FOO", "atom_name":"BAR", "atomid":1,
          "x":0.0, "y":1.0, "z":2.0},
         {"resid":1, "resname":"FOO", "atom_name":"BAR2", "atomid":2,
          "x":3.0, "y":4.0, "z":5.0},
         {"resid":2, "resname":"OOF", "atom_name":"BAR3", "atomid":3,
          "x":6.0, "y":7.0, "z":-8.0},
     ]
     with open(path) as infile:
         atoms = list(splitleafs.read_gro(infile))
     for ref, atom in zip(reference, atoms):
         for key, value in ref.items():
             if not key in "xyz":
                 self.assertEqual(value, atom[key],
                                  ("Different value for key {0}: {1} "
                                   "instead of {2}")
                                  .format(key, atom[key], value))
             else:
                 self.assertAlmostEqual(value, atom[key], PRECISION,
                                        ("Different value for key {0}: {1} "
                                         "instead of {2}")
                                        .format(key, atom[key], value))
Exemplo n.º 2
0
    def test_multiple_references(self):
        """
        Test if it works with several reference atoms in the same residue.
        """
        # Split the bilayer using POPC:P1 and POPC:C12 as reference atoms
        path = os.path.join(REFDIR, "membrane.gro")
        axis = "z"
        select_code = [("POPC", "P1"), ("POPC", "C12")]
        with open(path) as infile:
            atoms = list(splitleafs.read_gro(infile))
        print(len(atoms))
        selection = list(splitleafs.select_atoms(atoms, select_code))
        print(len(selection))
        coordinates = splitleafs.axis_coordinates(selection, axis)
        average = splitleafs.mean(coordinates)
        groups = splitleafs.split_get_res(atoms, average, axis, select_code)

        # The resulting groups should be the exact same as the one from the
        # reference output built with just POPC:P1 as reference atom
        ndx_reference = os.path.join(REFDIR, "leafs.ndx")
        with open(ndx_reference) as infile:
            reference = read_ndx(infile)
        for key, value in reference.items():
            self.assertEqual(value, groups[key],
                             ("The {0} group is different between the "
                              "function output and the reference.")
                             .format(key))
Exemplo n.º 3
0
    def test_split_get_res(self):
        """
        Test if the group formed by the split_get_res function are consistent.
        """
        # Split the bilayer
        path = os.path.join(REFDIR, "membrane.gro")
        axis = "z"
        with open(path) as infile:
            atoms = list(splitleafs.read_gro(infile))
        selection = list(splitleafs.select_atoms(atoms, [("P1",)]))
        coordinates = splitleafs.axis_coordinates(selection, axis)
        average = splitleafs.mean(coordinates)
        groups = splitleafs.split_get_res(atoms, average, axis, [("P1",)])

        # Do the group have the right size?
        natoms_popc = sum((1 for atom in atoms if atom["resname"] == "POPC"))
        print("natom_popc: {0}".format(natoms_popc))
        for key, value in groups.items():
            self.assertEqual(len(value) * 2, natoms_popc,
                             ("The {0} group contains {1} atoms it should "
                              "contain {2} ones.")
                             .format(key, len(value), natoms_popc // 2))

        # Is there doubles in the groups?
        for key, value in groups.items():
            self.assertEqual(len(value), len(set(value)),
                             "There is double values in the {0} group."
                             .format(key))

        # Is the group sorted? A group can not be sorted if the input file
        # is not but the test input file is correctly sorted.
        for key, value in groups.items():
            ordered = value[:]
            ordered.sort()
            self.assertEqual(value, ordered,
                             "The {0} group is nor correctly ordered."
                             .format(key))

        # Are the groups the same as the reference?
        ndx_reference = os.path.join(REFDIR, "leafs.ndx")
        with open(ndx_reference) as infile:
            reference = read_ndx(infile)
        for key, value in reference.items():
            self.assertEqual(value, groups[key],
                             ("The {0} group is different between the "
                              "function output and the reference.")
                             .format(key))