diff --git a/src/haddock/modules/refinement/openmm/__init__.py b/src/haddock/modules/refinement/openmm/__init__.py index 2a55c2a46..a115eaf78 100644 --- a/src/haddock/modules/refinement/openmm/__init__.py +++ b/src/haddock/modules/refinement/openmm/__init__.py @@ -1,12 +1,12 @@ """OpenMM refinement module for HADDOCK3.""" import os import shutil + from contextlib import suppress from pathlib import Path - -from haddock.core.exceptions import ThirdPartyIntallationError +from subprocess import run as subprocrun +from haddock.core.exceptions import ThirdPartyIntallationError, HaddockModuleError from haddock.libs.libontology import PDBFile -from haddock.libs.libsubprocess import run_subprocess from haddock.modules import BaseHaddockModule, get_engine # allow general testing when OpenMM is not installed @@ -68,6 +68,16 @@ def confirm_installation(cls) -> None: ThirdPartyIntallationError When OpenMM pdbfixer is not installed """ + def run_subprocess(command_to_run: str) -> str: + """Run subprocess.""" + subprocess_output = subprocrun( + [command_to_run], + shell=True, + capture_output=True, + encoding='utf-8', + ) + return subprocess_output.stdout.strip() + checkOpenMM = run_subprocess("conda list openmm --json") checkPdbfixer = run_subprocess("conda list pdbfixer --json") @@ -102,7 +112,6 @@ def _run(self) -> None: Path("."), directory_dict, self.params, - self.log, ) # Hold it openmm_jobs.append(openmm_job_i) @@ -118,21 +127,9 @@ def _run(self) -> None: # Check if at least one output file was generated if len(output_pdbs) == 0: - raise Exception("No output models generated. Check Openmm Execution.") # noqa: E501 - # ensemble_name = "openmm_ensemble.pdb" - # ensemble = make_ensemble(output_pdbs) # ensemble is a generator - # with open(ensemble_name, "w") as wfile: - # for line in ensemble: - # wfile.write(line) - # self.log(f'Output ensemble {ensemble_name} created.') - - # Setting the output variable - self.output_models = [ - PDBFile(openmmout) - for openmmout in sorted(output_pdbs) - ] - # Generating standardized haddock3 outputs - self.export_output_models() + raise HaddockModuleError( + "No output models generated. Check Openmm Execution." + ) # deleting unnecessary directories self.log("Removing unnecessary directories...") @@ -144,3 +141,11 @@ def _run(self) -> None: " the OpenMM module, the next module should be `[topoaa]`, " "to rebuild the CNS molecular topologies." ) + + # Setting the output variable + self.output_models = [ + PDBFile(openmmout) + for openmmout in sorted(output_pdbs) + ] + # Generating standardized haddock3 outputs + self.export_io_models() diff --git a/src/haddock/modules/refinement/openmm/openmm.py b/src/haddock/modules/refinement/openmm/openmm.py index e8844739b..6b1db2daa 100644 --- a/src/haddock/modules/refinement/openmm/openmm.py +++ b/src/haddock/modules/refinement/openmm/openmm.py @@ -78,7 +78,6 @@ def __init__( path: Path, directory_dict: dict[str, str], params: ParamDict, - logger: Optional, ): """ Initialize the class. @@ -111,14 +110,14 @@ def __init__( self.path = path self.directory_dict = directory_dict self.params = params - self.log = logger if logger else log + self.log = log # other parameters self.output = Path("output_openmm.log") self.constraints = self.import_constraints() self.output_filename = self.model.file_name.replace(".pdb", "_omm.pdb") - def import_constraints(self) -> Optional[Union[AllBonds, HAngles, HBonds]]: + def import_constraints(self): # type: ignore """Cast parameter string to proper openmm constraints.""" if self.params["constraints"] == "HBonds": return HBonds @@ -365,7 +364,7 @@ def equilibrate_solvation_box( simulation, max_temperature, statereporter._dof, - tolerence=10.0, + tolerance=10.0, steps=50 ) # Print log info @@ -418,7 +417,7 @@ def _stabilize_temperature( simulation: Simulation, temperature: float, dof: int, - tolerence: float = 5.0, + tolerance: float = 5.0, steps: int = 50, ) -> None: """ @@ -432,16 +431,16 @@ def _stabilize_temperature( The temperature hoped to be reached dof : int The degree of freedom obtained from the statereporter._dof - tolerence : float - The tolerence allowed for the temperature + tolerance : float + The tolerance allowed for the temperature steps : int The number of steps to do before checking again that temperature was reached """ # Makes sure temperature of the system is reached - while not ((temperature - tolerence) + while not ((temperature - tolerance) <= self._get_simulation_temperature(simulation, dof) - <= (temperature + tolerence)): + <= (temperature + tolerance)): # Do several simulation steps simulation.step(steps) @@ -721,7 +720,7 @@ def run_openmm( simulation, qtemp, statereporter._dof, - tolerence=5.0, + tolerance=5.0, steps=50 ) log.info(