def _amber_to_gromacs(self): # Load prmtop and inpcrd as a Structure parmstruct = load_file(self.outprefix + ".prmtop", xyz=self.outprefix + ".inpcrd", structure=True) # Save .gro coordinate file GromacsGroFile.write(struct=parmstruct, dest=self.outprefix + ".gro") # Save .top topology and parameter file grotop = GromacsTopologyFile.from_structure(parmstruct, copy=False) grotop.write(dest=self.outprefix + ".top", parameters="inline")
def test_gro_velocities(self): """ Test parsing and writing GROMACS GRO files with velocities """ gro = load_file(get_fn('1aki.ff99sbildn.gro')) self.assertIsInstance(gro, Structure) vel = np.random.rand(len(gro.atoms), 3) gro.velocities = vel fn = get_fn('test.gro', written=True) gro.save(fn) self.assertTrue(GromacsGroFile.id_format(fn)) gro.save(fn, overwrite=True, precision=8) self.assertTrue(GromacsGroFile.id_format(fn)) with open(fn) as f: tmp = GromacsGroFile.parse(f) np.testing.assert_allclose(vel, tmp.velocities, atol=1e-8)
def testWriteGroFile(self): """ Tests writing GRO file """ gro = GromacsGroFile.parse(get_fn('1aki.ff99sbildn.gro')) GromacsGroFile.write(gro, get_fn('1aki.ff99sbildn.gro', written=True)) gro = load_file(get_fn('1aki.ff99sbildn.gro', written=True)) self.assertIsInstance(gro, Structure) self.assertEqual(len(gro.atoms), 1960) self.assertEqual(len(gro.residues), 129) self.assertAlmostEqual(gro.atoms[0].xx, 44.6) self.assertAlmostEqual(gro.atoms[0].xy, 49.86) self.assertAlmostEqual(gro.atoms[0].xz, 18.10) self.assertAlmostEqual(gro.atoms[1959].xx, 50.97) self.assertAlmostEqual(gro.atoms[1959].xy, 39.80) self.assertAlmostEqual(gro.atoms[1959].xz, 38.64) self.assertAlmostEqual(gro.box[0], 74.1008) self.assertAlmostEqual(gro.box[1], 74.10080712) self.assertAlmostEqual(gro.box[2], 74.10074585) self.assertAlmostEqual(gro.box[3], 70.52882666) self.assertAlmostEqual(gro.box[4], 109.47126278) self.assertAlmostEqual(gro.box[5], 70.52875398)
def testReadWriteHighPrecisionGroFile(self): """ Tests reading/writing high-precision GRO files """ gro = GromacsGroFile.parse(get_fn('1aki.ff99sbildn.gro')) GromacsGroFile.write(gro, get_fn('1aki.ff99sbildn_highprec.gro', written=True), precision=6) self.assertTrue(diff_files(get_saved_fn('1aki.ff99sbildn_highprec.gro'), get_fn('1aki.ff99sbildn_highprec.gro', written=True) ) ) gro2 = GromacsGroFile.parse(get_fn('1aki.ff99sbildn_highprec.gro', written=True)) gro3 = load_file(get_fn('1aki.ff99sbildn_highprec.gro', written=True)) self.assertIsInstance(gro3, Structure) for a1, a2, a3 in zip(gro.atoms, gro2.atoms, gro3.atoms): self.assertEqual(a1.name, a2.name) self.assertEqual(a3.name, a2.name) self.assertEqual(a1.xx, a2.xx) self.assertEqual(a3.xx, a2.xx) self.assertEqual(a1.xy, a2.xy) self.assertEqual(a3.xy, a2.xy) self.assertEqual(a1.xz, a2.xz) self.assertEqual(a3.xz, a2.xz)
async def _run_grompp(self, projname: str) -> str: # Writing files for grompp gro_path = os.path.join(self.working_dir, f"{projname}.gro") top_path = os.path.join(self.working_dir, f"{projname}.top") mdp_path = os.path.join(self.working_dir, f"{projname}.mdp") tpr_path = os.path.join(self.working_dir, f"{projname}.tpr") GromacsGroFile.write(self.gro_struct, gro_path) with open(top_path, "w") as file: file.write(self.topology) self.mdp.write_mdp(mdp_path) command_list = [*self.grompp_cmd, "-f", mdp_path, "-c", gro_path, "-p", top_path, "-o", tpr_path] self.logger.debug("grompp-ing trajectory %s with command %s", projname, command_list) grompp_proc = subprocess.Popen(command_list, cwd=self.working_dir, stderr=subprocess.PIPE, stdout=subprocess.PIPE) # Wait for it to finish while grompp_proc.poll() is None: # Non-blocking sleep await asyncio.sleep(0.1) if grompp_proc.returncode != 0: stdout, stderr = grompp_proc.communicate() stdout_msg = stdout.decode('ascii') stderror_msg = stderr.decode('ascii') self.logger.error("Trajectory %s exited fatally when grompp-ing:\n" "stdout: %s\n stderr: %s", projname, stdout_msg, stderror_msg) raise RuntimeError(f"grompp of {projname} failed") return tpr_path
def testReadGroFile(self): """ Tests reading GRO file """ gro = GromacsGroFile.parse(get_fn('1aki.ff99sbildn.gro')) self.assertIsInstance(gro, Structure) self.assertEqual(len(gro.atoms), 1960) self.assertEqual(len(gro.residues), 129) self.assertAlmostEqual(gro.atoms[0].xx, 44.6) self.assertAlmostEqual(gro.atoms[0].xy, 49.86) self.assertAlmostEqual(gro.atoms[0].xz, 18.10) self.assertAlmostEqual(gro.atoms[1959].xx, 50.97) self.assertAlmostEqual(gro.atoms[1959].xy, 39.80) self.assertAlmostEqual(gro.atoms[1959].xz, 38.64) self.assertAlmostEqual(gro.box[0], 74.1008) self.assertAlmostEqual(gro.box[1], 74.10080712) self.assertAlmostEqual(gro.box[2], 74.10074585) self.assertAlmostEqual(gro.box[3], 70.52882666) self.assertAlmostEqual(gro.box[4], 109.47126278) self.assertAlmostEqual(gro.box[5], 70.52875398) # Check atomic number and mass assignment self.assertEqual(gro.atoms[0].atomic_number, 7) self.assertEqual(gro.atoms[0].mass, 14.0067)
def __init__(self, inputs: dict, working_dir: str = None): super().__init__(inputs, working_dir) self.grompp_cmd = inputs["grompp_cmd"].split() self.mdp = MDPHandler(inputs["mdp_file"]) self.gro_struct = GromacsGroFile.parse(inputs["gro_file"], skip_bonds=True) # This is a hacky way of getting around parmed's Structure. Structure # implements a correct deep copy in __copy__, but does not implement # __deepcopy__, and the default behavior is incorrect. Since # GromacsEngine gets deep copied, we need the correct version to be called. # See https://github.com/ParmEd/ParmEd/issues/1205 for if this can be # safely removed self.gro_struct.__deepcopy__ = lambda memo_dict: self.gro_struct.__copy__() with open(inputs["top_file"], "r") as file: self.topology = file.read() self.set_delta_t(inputs["delta_t"]) self.should_pin = inputs["should_pin"]
def test_gro_detection(self): """ Tests automatic detection of GROMACS GRO files """ fn = get_fn('candidate.gro', written=True) with open(fn, 'w') as f: f.write('Some title\n 1000\n aISNot a valid format\n') self.assertFalse(GromacsGroFile.id_format(fn))