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 test_mapping_create(self): mapping = Mapping("test/data/water.map", DummyOptions) self.assertEqual(1, len(mapping)) self.assertTrue("SOL" in mapping) self.assertEqual(1, len(mapping["SOL"])) self.assertEqual(3, len(mapping["SOL"][0].atoms)) self.assertEqual("OW", mapping["SOL"][0].atoms[0])
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_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_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_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_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_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_mapping_itp_multi(self): mapping = Mapping( self.data_dir.joinpath("membrane/membrane.map"), DummyOptions, itp_filename=self.data_dir.joinpath("membrane/membrane.top"), ) self.assertAlmostEqual(-1.2, mapping["POPE"][0].charge, delta=0.0001) self.assertAlmostEqual(0, mapping["POPG"][0].charge, delta=0.0001) self.assertAlmostEqual(94.9716, mapping["POPE"][0].mass, delta=0.0001)
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_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_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_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_virtual_mapping_create(self): mapping = Mapping(self.data_dir.joinpath("martini3/naphthalene.map"), DummyOptions) self.assertEqual(1, len(mapping)) self.assertTrue("NAPH" in mapping) self.assertEqual(5, len(mapping["NAPH"])) self.assertTrue(isinstance(mapping["NAPH"][2], VirtualMap)) self.assertEqual(4, len(mapping["NAPH"][2].atoms)) self.assertEqual("R1", mapping["NAPH"][2].atoms[0]) self.assertEqual(1, [ isinstance(bead, VirtualMap) for bead in mapping["NAPH"] ].count(True))
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_mapping_create(self): """Test that a mapping can be correctly read from a file.""" mapping = Mapping(self.data_dir.joinpath("water.map"), DummyOptions) self.assertEqual(2, len(mapping)) # SOL and HOH self.assertTrue("SOL" in mapping) sol_map = mapping["SOL"] self.assertEqual(1, len(sol_map)) self.assertEqual(3, len(sol_map[0].atoms)) self.assertEqual("OW", sol_map[0].atoms[0]) self.assertEqual("HW1", sol_map[0].atoms[1]) self.assertEqual("HW2", sol_map[0].atoms[2])
def test_mapping_rename(self): """Test that an alternate mapping is created using MDTraj conventions.""" mapping = Mapping(self.data_dir.joinpath("water.map"), DummyOptions) self.assertEqual(2, len(mapping)) # SOL and HOH self.assertTrue("HOH" in mapping) sol_map = mapping["HOH"] self.assertEqual(1, len(sol_map)) self.assertEqual(3, len(sol_map[0].atoms)) self.assertEqual("O", sol_map[0].atoms[0]) self.assertEqual("H1", sol_map[0].atoms[1]) self.assertEqual("H2", sol_map[0].atoms[2])
def test_mapping_duplicate_atom_weight(self): """Test that an atom can be duplicated in a bead specification to act as a weighting.""" frame = Frame(self.data_dir.joinpath("water.gro")) mapping = Mapping(self.data_dir.joinpath("water.map"), DummyOptions) cg_frame = mapping.apply(frame) np.testing.assert_allclose( np.array([[0.716, 1.300, 1.198]]), cg_frame.residue(0).atom(0).coords, atol=0.0005, ) mapping = Mapping(self.data_dir.joinpath("water_double_weight.map"), DummyOptions) cg_frame = mapping.apply(frame) np.testing.assert_allclose( np.array([[0.720, 1.294, 1.195]]), cg_frame.residue(0).atom(0).coords, atol=0.0005, )
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_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 apply_mapping(self, in_frame: Frame) -> typing.Tuple[Mapping, Frame]: """Map input frame to output using requested mapping file.""" mapping = Mapping(self.config.mapping, self.config, itp_filename=self.config.itp) out_frame = mapping.apply(in_frame) out_frame.save(self.get_output_filepath("gro"), frame_number=0) if self.config.backmapper_resname and self.out_frame.n_frames > 1: try: self.train_backmapper(self.config.resname) except ImportError: logger.error("MDPlus must be installed to perform backmapping") return mapping, out_frame
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_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_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_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_full_itp_sugar(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() with tempfile.TemporaryDirectory() as tmpdir: tmp_path = pathlib.Path(tmpdir) measure.write_itp(tmp_path.joinpath("sugar_out.itp"), mapping) self.assertTrue( cmp_file_whitespace_float( tmp_path.joinpath("sugar_out.itp"), self.data_dir.joinpath("sugar_out.itp"), rtol=0.005, verbose=True, ))
def test_mapping_charges(self): mapping = Mapping(self.data_dir.joinpath("dppc.map"), DummyOptions) self.assertEqual(1, mapping["DPPC"][0].charge) self.assertEqual(-1, mapping["DPPC"][1].charge)