Skip to content

Commit b345847

Browse files
Merge pull request #476 from joaomcteixeira/i356
Add general parameters to `haddock3-cfg` client
2 parents 958543d + 83bcd11 commit b345847

File tree

6 files changed

+168
-30
lines changed

6 files changed

+168
-30
lines changed

src/haddock/clis/cli_cfg.py

Lines changed: 92 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@
66
77
Usage::
88
9-
haddock3-cfg -m MODULE
9+
haddock3-cfg # prints only the general parametes
10+
haddock3-cfg -g # same as above
11+
haddock3-cfg -m MODULE # prints only the module parameters
1012
haddock3-cfg -m MODULE -l LEVEL
13+
haddock3-cfg -m topoaa -l all
14+
haddock3-cfg -m rigidbody -l all -g # prints the module and the globals
1115
"""
1216
import argparse
1317
import importlib
18+
import os
1419
import sys
1520

1621
from haddock import config_expert_levels
17-
from haddock.gear.yaml2cfg import yaml2cfg_text
18-
from haddock.libs.libio import read_from_yaml
1922
from haddock.modules import modules_category
2023

2124

@@ -29,8 +32,8 @@
2932
"-m",
3033
dest="module",
3134
help="The module for which you want to retrieve the default configuration.",
32-
required=True,
3335
choices=sorted(modules_category.keys()),
36+
default=None,
3437
)
3538

3639
ap.add_argument(
@@ -42,6 +45,14 @@
4245
choices=config_expert_levels + ("all",),
4346
)
4447

48+
ap.add_argument(
49+
'-g',
50+
'--globals',
51+
dest='global_params',
52+
help="Add also the optional module's general parameters.",
53+
action="store_true",
54+
)
55+
4556

4657
def _ap():
4758
return ap
@@ -57,6 +68,20 @@ def load_args(ap):
5768
def cli(ap, main):
5869
"""Command-line interface entry point."""
5970
cmd = load_args(ap)
71+
72+
# I didn't want to to have the `--globals` param as a negative parameter
73+
# ('store_false'). However, `module` and `globals` can't both be false.
74+
# In order for the commands below performing the same:
75+
#
76+
# haddock3-cfg
77+
# haddock3-cfg -g
78+
#
79+
# we need this quick if statement. Which basically adjust the argparse to
80+
# the default values in the main function.
81+
# @joaomcteixeira
82+
if cmd.global_params is False and cmd.module is None:
83+
cmd.global_params = True
84+
6085
main(**vars(cmd))
6186

6287

@@ -65,22 +90,69 @@ def maincli():
6590
cli(ap, main)
6691

6792

68-
def main(module, explevel):
69-
"""Extract the default configuration file for a given module."""
70-
module_name = ".".join((
71-
'haddock',
72-
'modules',
73-
modules_category[module],
74-
module,
75-
))
76-
77-
module_lib = importlib.import_module(module_name)
78-
cfg = module_lib.DEFAULT_CONFIG
79-
80-
ycfg = read_from_yaml(cfg)
81-
82-
new_config = yaml2cfg_text(ycfg, module, explevel)
83-
print(new_config, file=sys.stdout, flush=True) # noqa: T201
93+
def main(module=None, explevel="all", global_params=True):
94+
"""
95+
Extract the defaults in the form of a run configuration file.
96+
97+
Parameters
98+
----------
99+
module : str or None
100+
The module name to extract the defaults from.
101+
If ``None`` given, we expect ``global_params`` to be ``True``,
102+
otherwise nothing will be printed.
103+
104+
explevel : str
105+
Filter the parameters according to the expert level. Output all
106+
parameters that belong to the referred level or below. Choices
107+
are: all, easy, expert, and guru.
108+
109+
global_params : bool
110+
Whether to add the module's general global parameter. If
111+
``True`` and ``module`` is ``None``, outputs only the general
112+
parameters.
113+
"""
114+
from haddock import modules_defaults_path
115+
from haddock.gear.yaml2cfg import yaml2cfg_text
116+
from haddock.libs.libio import read_from_yaml
117+
118+
new_config = ''
119+
120+
if global_params:
121+
general_cfg = read_from_yaml(modules_defaults_path)
122+
general_params_str = yaml2cfg_text(
123+
general_cfg,
124+
module=None,
125+
explevel="all",
126+
)
127+
comment = os.linesep.join((
128+
"# The parameters below are optional parameters. ",
129+
"# They can either be used as global parameters or as part ",
130+
"# of the module's parameters",
131+
))
132+
133+
new_config = os.linesep.join((
134+
comment,
135+
general_params_str,
136+
))
137+
138+
if module:
139+
140+
module_name = ".".join((
141+
'haddock',
142+
'modules',
143+
modules_category[module],
144+
module,
145+
))
146+
147+
module_lib = importlib.import_module(module_name)
148+
cfg = module_lib.DEFAULT_CONFIG
149+
150+
ycfg = read_from_yaml(cfg)
151+
module_config = yaml2cfg_text(ycfg, module, explevel)
152+
153+
new_config = os.linesep.join((new_config, module_config))
154+
155+
sys.stdout.write(new_config)
84156

85157
return 0
86158

src/haddock/gear/yaml2cfg.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ def yaml2cfg_text(ymlcfg, module, explevel):
3333
parameters will be considered.
3434
"""
3535
new_config = []
36-
new_config.append(f"[{module}]")
36+
if module is not None:
37+
new_config.append(f"[{module}]")
3738

3839
new_config.append(_yaml2cfg_text(ymlcfg, module, explevel))
3940

@@ -68,7 +69,10 @@ def _yaml2cfg_text(ycfg, module, explevel):
6869
if isinstance(param, Mapping) and "default" not in param:
6970

7071
params.append("") # give extra space
71-
curr_module = f"{module}.{param_name}"
72+
if module is not None:
73+
curr_module = f"{module}.{param_name}"
74+
else:
75+
curr_module = param_name
7276
params.append(f"[{curr_module}]")
7377
_ = _yaml2cfg_text(param, module=curr_module, explevel=explevel)
7478
params.append(_)

tests/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
emptycfg = Path(configs_data, 'empty.cfg')
1212
haddock3_yaml_cfg_examples = Path(configs_data, 'yml_example.yml')
1313
haddock3_yaml_converted = Path(configs_data, 'yaml2cfg_converted.cfg')
14+
haddock3_yaml_converted_no_header = \
15+
Path(configs_data, 'yaml2cfg_converted_no_header.cfg')
1416
clean_steps_folder = Path(tests_path, 'clean_output_data')
1517

1618
# defines which modules are already working
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
autohis = True # $title Automatic HIS protonation state / $group molecule
2+
delenph = True # $title Keep or remove non-polar hydrogen atoms / $group molecule
3+
log_level = 'verbose' # $minchars 0 / $maxchars 100 / $title Log level verbosity for CNS / $group module
4+
iniseed = 917 # $min 0 / $max 9999999999999999 / $precision 3 / $title Random seed / $group molecule
5+
ligand_param_fname = '' # $title No title yet
6+
ligand_top_fname = '' # $title No title yet
7+
limit = True # $title No title yet
8+
tolerance = 0 # $min -9999 / $max 9999 / $precision 3 / $title No title yet
9+
10+
[mol1]
11+
prot_segid = 'A' # $minchars 0 / $maxchars 100 / $title No title yet
12+
fix_origin = False # $title No title yet
13+
dna = False # $title No title yet
14+
shape = False # $title No title yet
15+
cg = False # $title No title yet
16+
cyclicpept = False # $title No title yet
17+
nhisd = 0 # $min -9999 / $max 9999 / $precision 3 / $title No title yet
18+
hisd_1 = nan # $min -9999 / $max 9999 / $precision 3 / $title No title yet
19+
nhise = 0 # $min -9999 / $max 9999 / $precision 3 / $title No title yet
20+
hise_1 = nan # $min -9999 / $max 9999 / $precision 3 / $title No title yet
21+
22+
[mol2]
23+
prot_segid = 'B' # $minchars 0 / $maxchars 100 / $title No title yet
24+
fix_origin = False # $title No title yet
25+
dna = False # $title No title yet
26+
shape = False # $title No title yet
27+
cg = False # $title No title yet
28+
cyclicpept = False # $title No title yet
29+
nhisd = 0 # $min -9999 / $max 9999 / $precision 3 / $title No title yet
30+
hisd_1 = nan # $min -9999 / $max 9999 / $precision 3 / $title No title yet
31+
nhise = 0 # $min -9999 / $max 9999 / $precision 3 / $title No title yet
32+
hise_1 = nan # $min -9999 / $max 9999 / $precision 3 / $title No title yet

tests/test_cli_cfg.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,17 @@ def config_level(request):
1212
return request.param
1313

1414

15-
@pytest.mark.parametrize(
16-
"module",
17-
list(modules_category.keys()),
18-
)
19-
def test_export_cfgs(module, config_level):
20-
"""Test export all configs work."""
21-
cli_cfg.main(module, config_level)
15+
@pytest.fixture(params=(True, False))
16+
def global_params(request):
17+
"""Haddock3 config levels."""
18+
return request.param
19+
20+
21+
@pytest.fixture(params=list(modules_category.keys()) + [None])
22+
def module(request):
23+
return request.param
24+
25+
26+
def test_export_cfgs_add_global(module, config_level, global_params):
27+
"""Test export all configs work with `add_global` parameter."""
28+
cli_cfg.main(module, explevel=config_level, global_params=global_params)

tests/test_yaml2cfg.py renamed to tests/test_gear_yaml2cfg.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
from haddock.gear.yaml2cfg import flat_yaml_cfg, yaml2cfg_text
66
from haddock.libs.libio import read_from_yaml
77

8-
from . import haddock3_yaml_cfg_examples, haddock3_yaml_converted
8+
from . import (
9+
haddock3_yaml_cfg_examples,
10+
haddock3_yaml_converted,
11+
haddock3_yaml_converted_no_header,
12+
)
913

1014

1115
complex_cfg = {
@@ -76,3 +80,20 @@ def test_yaml2cfg_test():
7680

7781
assert filecmp.cmp(p, haddock3_yaml_converted, shallow=False)
7882
p.unlink()
83+
84+
85+
def test_yaml2cfg_test_no_header():
86+
"""Test yaml dict to cfg."""
87+
ycfg = read_from_yaml(haddock3_yaml_cfg_examples)
88+
result = yaml2cfg_text(ycfg, None, "all")
89+
assert isinstance(result, str)
90+
91+
p = Path('dummy_test.cfg')
92+
p.write_text(result)
93+
94+
assert filecmp.cmp(
95+
p,
96+
haddock3_yaml_converted_no_header,
97+
shallow=False,
98+
)
99+
p.unlink()

0 commit comments

Comments
 (0)