|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +from typing import Optional |
| 4 | + |
| 5 | +from langchain.tools import BaseTool |
| 6 | +from pydantic import BaseModel, Field |
| 7 | + |
| 8 | +from mdcrow.utils import FileType, PathRegistry |
| 9 | + |
| 10 | + |
| 11 | +class RunGromacsMDInput(BaseModel): |
| 12 | + topology_id: str |
| 13 | + coordinates_id: str |
| 14 | + ensemble: str = Field(description="NVT or NPT") |
| 15 | + temperature: float = Field(default=300.0) |
| 16 | + nsteps: int = Field(default=500_000) |
| 17 | + |
| 18 | + |
| 19 | +class RunGromacsMDTool(BaseTool): |
| 20 | + name = "RunGromacsMD" |
| 21 | + description = ( |
| 22 | + "Run a GROMACS MD simulation using NVT or NPT." |
| 23 | + "Requires topology (.top) and coordinate (.gro) files." |
| 24 | + ) |
| 25 | + args_schema = RunGromacsMDInput |
| 26 | + |
| 27 | + path_registry: Optional[PathRegistry] |
| 28 | + |
| 29 | + def __init__(self, path_registry: PathRegistry): |
| 30 | + super().__init__() |
| 31 | + self.path_registry = path_registry |
| 32 | + |
| 33 | + def _run(self, topology_id, coordinates_id, ensemble, temperature, nsteps): |
| 34 | + |
| 35 | + top_path = self.path_registry.get_mapped_path(topology_id) |
| 36 | + gro_path = self.path_registry.get_mapped_path(coordinates_id) |
| 37 | + |
| 38 | + if not os.path.exists(top_path): |
| 39 | + return f"Failed. Topology file not found: {top_path}" |
| 40 | + if not os.path.exists(gro_path): |
| 41 | + return f"Failed. Coordinate file not found: {gro_path}" |
| 42 | + |
| 43 | + workdir = self.path_registry.ckpt_simulations |
| 44 | + os.chdir(workdir) |
| 45 | + |
| 46 | + for stale in ["md.xtc", "md.gro"]: |
| 47 | + if os.path.exists(stale): |
| 48 | + os.remove(stale) |
| 49 | + |
| 50 | + with open("minim.mdp", "w") as f: |
| 51 | + f.write("integrator = steep\nnsteps = 5000\n") |
| 52 | + |
| 53 | + try: |
| 54 | + subprocess.run( |
| 55 | + [ |
| 56 | + "gmx", |
| 57 | + "grompp", |
| 58 | + "-f", |
| 59 | + "minim.mdp", |
| 60 | + "-c", |
| 61 | + gro_path, |
| 62 | + "-p", |
| 63 | + top_path, |
| 64 | + "-o", |
| 65 | + "em.tpr", |
| 66 | + ], |
| 67 | + check=True, |
| 68 | + capture_output=True, |
| 69 | + text=True, |
| 70 | + ) |
| 71 | + subprocess.run( |
| 72 | + ["gmx", "mdrun", "-deffnm", "em"], |
| 73 | + check=True, |
| 74 | + capture_output=True, |
| 75 | + text=True, |
| 76 | + ) |
| 77 | + except subprocess.CalledProcessError as e: |
| 78 | + return f"Failed during energy minimization: {e.stderr}" |
| 79 | + |
| 80 | + # just explicitly setting many of these. we could make this more flexible later |
| 81 | + if ensemble.upper() == "NVT": |
| 82 | + mdp = f""" |
| 83 | +integrator = md |
| 84 | +nsteps = {nsteps} |
| 85 | +dt = 0.002 |
| 86 | +tcoupl = V-rescale |
| 87 | +tc-grps = Protein Non-Protein |
| 88 | +tau_t = 0.1 0.1 |
| 89 | +ref_t = {temperature} {temperature} |
| 90 | +constraints = h-bonds |
| 91 | +cutoff-scheme = Verlet |
| 92 | +nstxout-compressed = 500 |
| 93 | +compressed-x-grps = System |
| 94 | +""" |
| 95 | + elif ensemble.upper() == "NPT": |
| 96 | + mdp = f""" |
| 97 | +integrator = md |
| 98 | +nsteps = {nsteps} |
| 99 | +dt = 0.002 |
| 100 | +tcoupl = V-rescale |
| 101 | +tc-grps = Protein Non-Protein |
| 102 | +tau_t = 0.1 0.1 |
| 103 | +ref_t = {temperature} {temperature} |
| 104 | +pcoupl = Parrinello-Rahman |
| 105 | +pcoupltype = isotropic |
| 106 | +tau_p = 2.0 |
| 107 | +ref_p = 1.0 |
| 108 | +compressibility = 4.5e-5 |
| 109 | +constraints = h-bonds |
| 110 | +cutoff-scheme = Verlet |
| 111 | +nstxout-compressed = 500 |
| 112 | +compressed-x-grps = System |
| 113 | +""" |
| 114 | + else: |
| 115 | + return f"Failed. Unsupported ensemble: {ensemble}" |
| 116 | + |
| 117 | + with open("md.mdp", "w") as f: |
| 118 | + f.write(mdp) |
| 119 | + |
| 120 | + try: |
| 121 | + subprocess.run( |
| 122 | + [ |
| 123 | + "gmx", |
| 124 | + "grompp", |
| 125 | + "-f", |
| 126 | + "md.mdp", |
| 127 | + "-c", |
| 128 | + "em.gro", |
| 129 | + "-p", |
| 130 | + top_path, |
| 131 | + "-o", |
| 132 | + "md.tpr", |
| 133 | + ], |
| 134 | + check=True, |
| 135 | + capture_output=True, |
| 136 | + text=True, |
| 137 | + ) |
| 138 | + subprocess.run( |
| 139 | + ["gmx", "mdrun", "-deffnm", "md", "-x", "md.xtc"], |
| 140 | + check=True, |
| 141 | + capture_output=True, |
| 142 | + text=True, |
| 143 | + ) |
| 144 | + except subprocess.CalledProcessError as e: |
| 145 | + return f"Failed during MD simulation: {e.stderr}" |
| 146 | + |
| 147 | + traj_name = self.path_registry.write_file_name( |
| 148 | + type=FileType.RECORD, |
| 149 | + record_type="TRAJ", |
| 150 | + file_format="xtc", |
| 151 | + ) |
| 152 | + gro_name = self.path_registry.write_file_name( |
| 153 | + type=FileType.RECORD, |
| 154 | + record_type="FINAL", |
| 155 | + file_format="gro", |
| 156 | + ) |
| 157 | + |
| 158 | + if not os.path.exists("md.xtc"): |
| 159 | + return "Failed. GROMACS did not produce md.xtc. Check md.log for errors." |
| 160 | + if not os.path.exists("md.gro"): |
| 161 | + return "Failed. GROMACS did not produce md.gro. Check md.log for errors." |
| 162 | + |
| 163 | + os.rename("md.xtc", traj_name) |
| 164 | + os.rename("md.gro", gro_name) |
| 165 | + |
| 166 | + traj_id = self.path_registry.get_fileid(traj_name, FileType.RECORD) |
| 167 | + self.path_registry.map_path(traj_id, f"{workdir}/{traj_name}", "MD trajectory") |
| 168 | + |
| 169 | + gro_id = self.path_registry.get_fileid(gro_name, FileType.RECORD) |
| 170 | + self.path_registry.map_path(gro_id, f"{workdir}/{gro_name}", "Final structure") |
| 171 | + |
| 172 | + return ( |
| 173 | + f"Succeeded. MD completed using {ensemble} ensemble.\n" |
| 174 | + f"Trajectory ID: {traj_id}\nFinal structure ID: {gro_id}" |
| 175 | + ) |
0 commit comments