From 281497bfe22e88680d2334a0b8fcb05602810957 Mon Sep 17 00:00:00 2001 From: mgiulini Date: Thu, 11 Jan 2024 11:56:08 +0100 Subject: [PATCH 1/3] fixed problems in cli_score --- src/haddock/clis/cli_score.py | 44 +++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/src/haddock/clis/cli_score.py b/src/haddock/clis/cli_score.py index 353c65e6b..3b39094ce 100644 --- a/src/haddock/clis/cli_score.py +++ b/src/haddock/clis/cli_score.py @@ -18,6 +18,7 @@ """ import argparse import sys +import tempfile from haddock.core.typing import ( Any, @@ -74,7 +75,7 @@ "--other-params", dest="other_params", help=( - "Any other parameter of the `emscoring` module." + "Any other parameter of the `emscoring` module. " "For example: -p nemsteps 1000. You can give any number of " "parameters." ), @@ -174,6 +175,10 @@ def main( print( f"* ATTENTION * Value ({value}) of parameter {param} different from default ({default_emscoring[param]})" ) # noqa:E501 + # get the type of default value + default_type = type(default_emscoring[param]) + # convert the value to the same type + value = default_type(value) ems_dict[param] = value n_warnings += 1 @@ -182,27 +187,36 @@ def main( "* ATTENTION * Non-default parameter values were used. They should be properly reported if the output data are used for publication." ) # noqa:E501 - params = { - "topoaa": {"molecules": [input_pdb]}, - "emscoring": ems_dict, - } - - print("> starting calculations...") - + # create run directory run_dir = Path(run_dir) with suppress(FileNotFoundError): shutil.rmtree(run_dir) run_dir.mkdir() zero_fill.set_zerofill_number(2) - with working_directory(run_dir): - workflow = WorkflowManager( - workflow_params=params, - start=0, - run_dir=run_dir, - ) + # create temporary file + with tempfile.NamedTemporaryFile(suffix=".pdb") as tmp: + + # create a copy of the input pdb + input_pdb_copy = Path(tmp.name) + shutil.copy(input_pdb, input_pdb_copy) + + params = { + "topoaa": {"molecules": [input_pdb_copy]}, + "emscoring": ems_dict, + } + + print("> starting calculations...") + + # run workflow + with working_directory(run_dir): + workflow = WorkflowManager( + workflow_params=params, + start=0, + run_dir=run_dir, + ) - workflow.run() + workflow.run() minimized_mol = Path(run_dir, "1_emscoring", "emscoring_1.pdb") haddock_score_component_dic = HaddockModel(minimized_mol).energies From 6659417c739f383cd99cb96ae3a4a7f3b7b5ef3b Mon Sep 17 00:00:00 2001 From: mgiulini Date: Thu, 11 Jan 2024 11:56:31 +0100 Subject: [PATCH 2/3] changed test to integration --- integration_tests/test_cli_score.py | 31 ++++++++++++++++++++++++++++ tests/test_cli_score.py | 32 ----------------------------- 2 files changed, 31 insertions(+), 32 deletions(-) create mode 100644 integration_tests/test_cli_score.py delete mode 100644 tests/test_cli_score.py diff --git a/integration_tests/test_cli_score.py b/integration_tests/test_cli_score.py new file mode 100644 index 000000000..1c8e7484f --- /dev/null +++ b/integration_tests/test_cli_score.py @@ -0,0 +1,31 @@ +import tempfile +from pathlib import Path + +from tests import golden_data +from haddock.clis import cli_score + +import io +from contextlib import redirect_stdout +import os +from . import has_cns + +@has_cns +def test_cli_score(): + """Test the haddock3-score CLI.""" + pdb_f = Path(golden_data, "protprot_complex_1.pdb") + # tempdir + with tempfile.TemporaryDirectory(dir=".") as tmpdir: + # parsing + f = io.StringIO() + with redirect_stdout(f): + cli_score.main(pdb_f, tmpdir, full=True, keep_all=False) + out = f.getvalue().split(os.linesep) + assert out[-3].startswith("> HADDOCK-score (emscoring) = ") + assert out[-2].startswith("> vdw") + + # now changing some weitght + f = io.StringIO() + with redirect_stdout(f): + cli_score.main(pdb_f, tmpdir, full=True, keep_all=False, w_vdw=0.5) + out = f.getvalue().split(os.linesep) + assert out[3].startswith("> HADDOCK-score = (0.5 * vdw) + ") diff --git a/tests/test_cli_score.py b/tests/test_cli_score.py deleted file mode 100644 index b56bde466..000000000 --- a/tests/test_cli_score.py +++ /dev/null @@ -1,32 +0,0 @@ -from haddock.clis import cli_score -from tests import golden_data -from pathlib import Path -import tempfile -import pytest_mock -import os -import shutil -import io -from contextlib import redirect_stdout - - -def test_cli_score_main(mocker): - """Test the main function of the cli_score module.""" - # setup - pdb_f = Path(golden_data, "protprot_complex_1.pdb") - tmpdir = "tmpdir" - os.mkdir(tmpdir) - os.mkdir(Path(tmpdir, "1_emscoring")) - shutil.copy(pdb_f, Path(tmpdir, "1_emscoring", "emscoring_1.pdb")) - # mocking - mocker.patch("haddock.libs.libworkflow.WorkflowManager.run", return_value=None) - mocker.patch("shutil.rmtree", return_value=None) - mocker.patch("pathlib.Path.mkdir", return_value=None) - # parsing - f = io.StringIO() - with redirect_stdout(f): - cli_score.main(pdb_f, tmpdir, full=True, keep_all=True) - out = f.getvalue().split(os.linesep) - # clean up - os.unlink(Path(tmpdir, "1_emscoring", "emscoring_1.pdb")) - os.rmdir(Path(tmpdir, "1_emscoring")) - os.rmdir(tmpdir) From c304ba43301be147d913427c39b87e147a4ee71b Mon Sep 17 00:00:00 2001 From: mgiulini Date: Thu, 11 Jan 2024 12:57:08 +0100 Subject: [PATCH 3/3] added prefix for tempfile --- src/haddock/clis/cli_score.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/haddock/clis/cli_score.py b/src/haddock/clis/cli_score.py index 3b39094ce..89372f3bf 100644 --- a/src/haddock/clis/cli_score.py +++ b/src/haddock/clis/cli_score.py @@ -195,7 +195,7 @@ def main( zero_fill.set_zerofill_number(2) # create temporary file - with tempfile.NamedTemporaryFile(suffix=".pdb") as tmp: + with tempfile.NamedTemporaryFile(prefix=input_pdb.stem, suffix=".pdb") as tmp: # create a copy of the input pdb input_pdb_copy = Path(tmp.name)