def test_frame_mdtraj_read_xtc(self): logging.disable(logging.WARNING) frame = Frame(gro="test/data/water.gro", xtc="test/data/water.xtc", xtc_reader="mdtraj") logging.disable(logging.NOTSET) self.assertEqual(663, frame.natoms) # These are the coordinates from the gro file np.testing.assert_allclose(np.array([0.696, 1.33, 1.211]), frame.residues[0].atoms[0].coords) np.testing.assert_allclose(np.array([1.89868, 1.89868, 1.89868]), frame.box) frame.next_frame() # These coordinates are from the xtc file np.testing.assert_allclose(np.array([1.176, 1.152, 1.586]), frame.residues[0].atoms[0].coords) np.testing.assert_allclose(np.array([1.9052, 1.9052, 1.9052]), frame.box) frame.next_frame() np.testing.assert_allclose(np.array([1.122, 1.130, 1.534]), frame.residues[0].atoms[0].coords) np.testing.assert_allclose(np.array([1.90325272, 1.90325272, 1.90325272]), frame.box)
def test_mapping_pbc(self): frame = Frame(self.data_dir.joinpath("pbcwater.gro")) mapping = Mapping(self.data_dir.joinpath("water.map"), DummyOptions) cg_frame = mapping.apply(frame) np.testing.assert_allclose( frame.atom(0).coords, cg_frame.atom(0).coords)
def test_bondset_boltzmann_invert(self): measure = BondSet("test/data/sugar.bnd", DummyOptions) frame = Frame("test/data/sugar.gro", xtc="test/data/sugar.xtc") mapping = Mapping("test/data/sugar.map", DummyOptions) cgframe = mapping.apply(frame) while frame.next_frame(): cgframe = mapping.apply(frame, cgframe=cgframe) measure.apply(cgframe) measure.boltzmann_invert() self.support_check_mean_fc(measure["ALLA"], 1)
def test_frame_output_gro(self): frame = Frame(self.data_dir.joinpath("water.gro")) with tempfile.TemporaryDirectory() as tmpdir: tmp_path = pathlib.Path(tmpdir) frame.save(tmp_path.joinpath("water-out.gro")) self.assertTrue( filecmp.cmp( self.data_dir.joinpath("water.gro"), tmp_path.joinpath("water-out.gro"), ))
def test_frame_write_xtc_mdtraj(self): try: os.remove("water_test2.xtc") except IOError: pass logging.disable(logging.WARNING) frame = Frame(gro="test/data/water.gro", xtc="test/data/water.xtc", xtc_reader="mdtraj") logging.disable(logging.NOTSET) while frame.next_frame(): frame.write_xtc("water_test2.xtc")
def test_frame_write_xtc(self): """Test that :class:`Frame` can save a trajectory file.""" frame = Frame(self.data_dir.joinpath("water.gro"), self.data_dir.joinpath("water.xtc")) with tempfile.TemporaryDirectory() as tmpdir: tmp_path = pathlib.Path(tmpdir) frame.save(tmp_path.joinpath("water.xtc")) self.assertTrue( util.compare_trajectories( self.data_dir.joinpath("water.xtc"), tmp_path.joinpath("water.xtc"), topology_file=self.data_dir.joinpath("water.gro"), ))
def test_frame_read_xtc_mdtraj_numframes(self): logging.disable(logging.WARNING) frame = Frame(gro="test/data/water.gro", xtc="test/data/water.xtc", xtc_reader="mdtraj") logging.disable(logging.NOTSET) self.assertEqual(11, frame.numframes)
def test_full_itp_vsites(self): """Test full operation to output of .itp file for molecule with vsites.""" options = DummyOptions() options.generate_angles = False measure = BondSet(self.data_dir.joinpath("martini3/naphthalene.bnd"), options) frame = Frame(self.data_dir.joinpath("martini3/naphthalene.gro")) mapping = Mapping(self.data_dir.joinpath("martini3/naphthalene.map"), options) cg_frame = mapping.apply(frame) measure.apply(cg_frame) measure.boltzmann_invert() with tempfile.TemporaryDirectory() as tmpdir: tmp_path = pathlib.Path(tmpdir) measure.write_itp(tmp_path.joinpath("naphthalene_out.itp"), mapping) self.assertTrue( cmp_file_whitespace_float( tmp_path.joinpath("naphthalene_out.itp"), self.data_dir.joinpath("martini3/naphthalene_out.itp"), rtol=0.005, verbose=True, ))
def __init__(self, config): self.config = config self.in_frame = Frame( topology_file=self.config.topology, trajectory_file=self.config.trajectory, # May be None frame_start=self.config.begin, frame_end=self.config.end, ) self.mapping = None self.out_frame = self.in_frame if self.config.mapping: self.mapping, self.out_frame = self.apply_mapping(self.in_frame) if self.out_frame.natoms == 0: # The following steps won't work with frames containing no atoms logger.warning( "Mapped trajectory contains no atoms - check your mapping file is correct!" ) return self.bondset = None if self.config.bondset: self.bondset = BondSet(self.config.bondset, self.config) self.measure_bonds() if self.config.output_xtc: self.out_frame.save(self.get_output_filepath("xtc"))
def test_bondset_boltzmann_invert_mdtraj(self): logging.disable(logging.WARNING) frame = Frame("test/data/sugar.gro", xtc="test/data/sugar.xtc", xtc_reader="mdtraj") logging.disable(logging.NOTSET) measure = BondSet("test/data/sugar.bnd", DummyOptions) mapping = Mapping("test/data/sugar.map", DummyOptions) cgframe = mapping.apply(frame) while frame.next_frame(): cgframe = mapping.apply(frame, cgframe=cgframe) measure.apply(cgframe) measure.boltzmann_invert() self.support_check_mean_fc(measure["ALLA"], 1)
def test_bondset_pbc(self): bondset = BondSet("test/data/polyethene.bnd", DummyOptions) frame = Frame("test/data/pbcpolyethene.gro") bondset.apply(frame) bondset.boltzmann_invert() for bond in bondset.get_bond_lengths("ETH", True): self.assertAlmostEqual(1., bond.eqm) self.assertEqual(float("inf"), bond.fconst)
def test_frame_mdtraj_read_xtc(self): logging.disable(logging.WARNING) frame = Frame(gro="test/data/water.gro", xtc="test/data/water.xtc", xtc_reader="mdtraj") logging.disable(logging.NOTSET) self.helper_read_xtc(frame, skip_names=True)
def test_mapping_apply(self): mapping = Mapping("test/data/water.map", DummyOptions) frame = Frame("test/data/water.gro") cgframe = mapping.apply(frame) self.assertEqual(len(frame), len(cgframe)) cgframe.output("water-cg.gro", format="gro") self.assertTrue(filecmp.cmp("test/data/water-cg.gro", "water-cg.gro")) os.remove("water-cg.gro")
def test_bondset_boltzmann_invert_func_forms(self): class FuncFormOptions(DummyOptions): length_form = "CosHarmonic" angle_form = "Harmonic" dihedral_form = "MartiniDefaultLength" measure = BondSet("test/data/sugar.bnd", FuncFormOptions) frame = Frame("test/data/sugar.gro", xtc="test/data/sugar.xtc") mapping = Mapping("test/data/sugar.map", DummyOptions) cgframe = mapping.apply(frame) while frame.next_frame(): cgframe = mapping.apply(frame, cgframe=cgframe) measure.apply(cgframe) measure.boltzmann_invert() self.support_check_mean_fc(measure["ALLA"], 3)
def test_full_itp_sugar(self): measure = BondSet("test/data/sugar.bnd", DummyOptions) frame = Frame("test/data/sugar.gro", xtc="test/data/sugar.xtc") mapping = Mapping("test/data/sugar.map", DummyOptions) cgframe = mapping.apply(frame) while frame.next_frame(): cgframe = mapping.apply(frame, cgframe=cgframe) measure.apply(cgframe) measure.boltzmann_invert() logging.disable(logging.WARNING) measure.write_itp("sugar_out.itp", mapping) logging.disable(logging.NOTSET) self.assertTrue(cmp_whitespace_float("sugar_out.itp", "test/data/sugar_out.itp", float_rel_error=0.001))
def test_mapping_weights_geom(self): frame = Frame(self.data_dir.joinpath("two.gro")) mapping = Mapping(self.data_dir.joinpath("two.map"), DummyOptions) cg_frame = mapping.apply(frame) np.testing.assert_allclose(np.array([[1.5, 1.5, 1.5]]), cg_frame.residue(0).atom(0).coords)
def test_mapping_charges(self): mapping = Mapping("test/data/dppc.map", DummyOptions) self.assertEqual(1, mapping["DPPC"][0].charge) self.assertEqual(-1, mapping["DPPC"][1].charge) frame = Frame("test/data/dppc.gro") cgframe = mapping.apply(frame) self.assertEqual(1, cgframe[0][0].charge) self.assertEqual(-1, cgframe[0][1].charge)
def test_bondset_boltzmann_invert_manual_default_fc(self): class FuncFormOptions(DummyOptions): length_form = "MartiniDefaultLength" angle_form = "MartiniDefaultAngle" dihedral_form = "MartiniDefaultDihedral" measure = BondSet("test/data/sugar.bnd", FuncFormOptions) frame = Frame("test/data/sugar.gro", xtc="test/data/sugar.xtc") mapping = Mapping("test/data/sugar.map", DummyOptions) cgframe = mapping.apply(frame) while frame.next_frame(): cgframe = mapping.apply(frame, cgframe=cgframe) measure.apply(cgframe) measure.boltzmann_invert() self.support_check_mean_fc(measure["ALLA"], 2)
def test_virtual_mapping_weights_geom(self): frame = Frame(self.data_dir.joinpath("martini3/four.gro")) mapping = Mapping(self.data_dir.joinpath("martini3/four.map"), DummyOptions) cg_frame = mapping.apply(frame) np.testing.assert_allclose(np.array([[2.5, 2.5, 2.5]]), cg_frame.residue(0).atom(2).coords)
def test_mapping_weights_first(self): frame = Frame("test/data/two.gro") options = DummyOptions() options.map_center = "first" mapping = Mapping("test/data/two.map", options, itp="test/data/two.itp") cg = mapping.apply(frame) np.testing.assert_allclose(np.array([1., 1., 1.]), cg[0][0].coords)
def test_full_itp_sugar(self): measure = BondSet("test/data/sugar.bnd", DummyOptions) frame = Frame("test/data/sugar.gro", xtc="test/data/sugar.xtc") mapping = Mapping("test/data/sugar.map", DummyOptions) cgframe = mapping.apply(frame) while frame.next_frame(): cgframe = mapping.apply(frame, cgframe=cgframe) measure.apply(cgframe) measure.boltzmann_invert() logging.disable(logging.WARNING) measure.write_itp("sugar_out.itp", mapping) logging.disable(logging.NOTSET) self.assertTrue( cmp_whitespace_float("sugar_out.itp", "test/data/sugar_out.itp", float_rel_error=0.001))
def test_bondset_pbc(self): bondset = BondSet(self.data_dir.joinpath("polyethene.bnd"), DummyOptions) frame = Frame(self.data_dir.joinpath("pbcpolyethene.gro")) bondset.apply(frame) bondset.boltzmann_invert() for bond in bondset.get_bond_lengths("ETH", True): self.assertAlmostEqual(1.0, bond.eqm) self.assertEqual(math.inf, bond.fconst)
def test_bondset_polymer(self): bondset = BondSet("test/data/polyethene.bnd", DummyOptions) frame = Frame("test/data/polyethene.gro") bondset.apply(frame) self.assertEqual(5, len(bondset["ETH"][0].values)) self.assertEqual(4, len(bondset["ETH"][1].values)) self.assertEqual(4, len(bondset["ETH"][2].values)) self.assertEqual(4, len(bondset["ETH"][3].values)) bondset.boltzmann_invert() self.assertAlmostEqual(0.107, bondset["ETH"][0].eqm, delta=0.107 / 500) self.assertAlmostEqual(0.107, bondset["ETH"][1].eqm, delta=0.107 / 500)
def test_mapping_weights_guess_mass(self): frame = Frame("test/data/two.gro") options = DummyOptions() options.map_center = "mass" mapping = Mapping("test/data/two.map", options) cg = mapping.apply(frame) np.testing.assert_allclose(np.array([1.922575, 1.922575, 1.922575], dtype=np.float32), cg[0][0].coords, rtol=0.01)
def test_bondset_boltzmann_invert(self): measure = BondSet(self.data_dir.joinpath("sugar.bnd"), DummyOptions) frame = Frame(self.data_dir.joinpath("sugar.gro"), self.data_dir.joinpath("sugar.xtc")) mapping = Mapping(self.data_dir.joinpath("sugar.map"), DummyOptions) cg_frame = mapping.apply(frame) measure.apply(cg_frame) measure.boltzmann_invert() self.support_check_mean_fc(measure["ALLA"], 1)
def test_empty_result_frame(self): """Test that no error is raised when an empty output frame is encountered. This is instead checked and logged as a warning in the outer script. """ frame = Frame(self.data_dir.joinpath("sugar.gro")) mapping = Mapping(self.data_dir.joinpath("water.map"), DummyOptions) cg_frame = mapping.apply(frame) self.assertEqual(0, cg_frame.natoms) self.assertEqual((1, 0, 3), cg_frame._trajectory.xyz.shape)
def test_mapping_weights_guess_mass(self): frame = Frame(self.data_dir.joinpath("two.gro")) options = DummyOptions() options.map_center = "mass" mapping = Mapping(self.data_dir.joinpath("two.map"), options) cg_frame = mapping.apply(frame) np.testing.assert_allclose( np.array([[1.922575, 1.922575, 1.922575]], dtype=np.float32), cg_frame.residue(0).atom(0).coords, rtol=0.01, )
def test_virtual_mapping_weights_guess_mass(self): frame = Frame(self.data_dir.joinpath("martini3/four.gro")) options = DummyOptions() options.virtual_map_center = "mass" mapping = Mapping(self.data_dir.joinpath("martini3/four.map"), options) cg_frame = mapping.apply(frame) np.testing.assert_allclose( np.array([[2.83337, 2.83337, 2.83337]], dtype=np.float32), cg_frame.residue(0).atom(2).coords, rtol=0.01, )
def test_mapping_apply(self): mapping = Mapping(self.data_dir.joinpath("water.map"), DummyOptions) frame = Frame(self.data_dir.joinpath("water.gro")) cg_frame = mapping.apply(frame) self.assertEqual(frame.natoms / 3, cg_frame.natoms) cg_frame.save("water-cg.gro") self.assertTrue( filecmp.cmp(self.data_dir.joinpath("water-cg.gro"), "water-cg.gro")) os.remove("water-cg.gro")
def test_virtual_mapping_weights_mass(self): frame = Frame(self.data_dir.joinpath("martini3/four.gro")) options = DummyOptions() options.virtual_map_center = "mass" mapping = Mapping( self.data_dir.joinpath("martini3/four.map"), options, itp_filename=self.data_dir.joinpath("martini3/four.itp"), ) cg_frame = mapping.apply(frame) np.testing.assert_allclose(np.array([[3.0, 3.0, 3.0]]), cg_frame.residue(0).atom(2).coords)
def test_mapping_weights_first(self): frame = Frame(self.data_dir.joinpath("two.gro")) options = DummyOptions() options.map_center = "first" mapping = Mapping( self.data_dir.joinpath("two.map"), options, itp_filename=self.data_dir.joinpath("two.itp"), ) cg_frame = mapping.apply(frame) np.testing.assert_allclose(np.array([[1.0, 1.0, 1.0]]), cg_frame.residue(0).atom(0).coords)
def test_bondset_boltzmann_invert_manual_default_fc(self): class FuncFormOptions(DummyOptions): length_form = "MartiniDefaultLength" angle_form = "MartiniDefaultAngle" dihedral_form = "MartiniDefaultDihedral" measure = BondSet(self.data_dir.joinpath("sugar.bnd"), FuncFormOptions) frame = Frame(self.data_dir.joinpath("sugar.gro"), self.data_dir.joinpath("sugar.xtc")) mapping = Mapping(self.data_dir.joinpath("sugar.map"), DummyOptions) cg_frame = mapping.apply(frame) measure.apply(cg_frame) measure.boltzmann_invert() self.support_check_mean_fc(measure["ALLA"], 2)
def test_dump_bonds(self): measure = BondSet(self.data_dir.joinpath("sugar.bnd"), DummyOptions) frame = Frame(self.data_dir.joinpath("sugar.gro"), self.data_dir.joinpath("sugar.xtc")) mapping = Mapping(self.data_dir.joinpath("sugar.map"), DummyOptions) cg_frame = mapping.apply(frame) measure.apply(cg_frame) measure.boltzmann_invert() measure.dump_values() filenames = ("ALLA_length.dat", "ALLA_angle.dat", "ALLA_dihedral.dat") for filename in filenames: self.assertTrue( cmp_file_whitespace_float(self.data_dir.joinpath(filename), filename, rtol=0.008, verbose=True)) os.remove(filename)
def test_frame_add_residue(self): residue = Residue() frame = Frame() frame.add_residue(residue) self.assertEqual(residue, frame.residues[0]) self.assertTrue(residue is frame.residues[0])
def test_frame_output_gro(self): frame = Frame("test/data/water.gro") frame.output("water-out.gro", format="gro") self.assertTrue(filecmp.cmp("test/data/water.gro", "water-out.gro")) os.remove("water-out.gro")