Skip to content

Commit

Permalink
Merge branch 'refactor/remove_system_option_settings'
Browse files Browse the repository at this point in the history
  • Loading branch information
PauAndrio committed Sep 12, 2024
2 parents 2e6ec25 + 215b059 commit 2b4f1b9
Show file tree
Hide file tree
Showing 18 changed files with 310 additions and 250 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ mdout.mdp
traj_comp.xtc

# Jupyter notebook
.ipynb
.ipynb_checkpoints/

# Pycharm
Expand Down
379 changes: 131 additions & 248 deletions biobb_common/configuration/settings.py

Large diffs are not rendered by default.

69 changes: 67 additions & 2 deletions biobb_common/tools/test_fixtures.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Boiler plate functions for testsys
"""
import os
import pickle
import typing
from typing import Optional
from typing import Optional, Union, List, Dict
from pathlib import Path
import sys
import shutil
Expand Down Expand Up @@ -34,7 +35,7 @@ def test_setup(test_object, dict_key: Optional[str] = None, config: Optional[str
test_object.conf_file_path = str(Path(test_object.test_dir).joinpath('conf.yml'))

test_object.system = os.getenv('testsys')
conf = settings.ConfReader(test_object.conf_file_path, test_object.system)
conf = settings.ConfReader(test_object.conf_file_path)

if dict_key:
test_object.properties = conf.get_prop_dic()[dict_key]
Expand Down Expand Up @@ -291,3 +292,67 @@ def compare_images(file_a: str, file_b: str, percent_tolerance: float = 1.0) ->
if difference > tolerance:
return False
return True


def compare_object_pickle(python_object: typing.Any, pickle_file_path: Union[str, Path], **kwargs) -> bool:
""" Compare a python object with a pickle file """
print(f"Loading pickle file: {pickle_file_path}")
with open(pickle_file_path, 'rb') as f:
pickle_object = pickle.load(f)

# Special case for dictionaries
if isinstance(python_object, dict) and isinstance(pickle_object, dict):
differences = compare_dictionaries(python_object, pickle_object, ignore_keys=kwargs.get('ignore_keys', []), compare_values=kwargs.get('compare_values', True), ignore_substring=kwargs.get('ignore_substring', ""))
if differences:
print(50*'*')
print("OBJECT:")
print(python_object)
print(50*'*')
print()
print(50*'*')
print("EXPECTED OBJECT:")
print(pickle_object)
print(50*'*')

print("Differences found:")
for difference in differences:
print(f" {difference}")
return False
return True

return python_object == pickle_object


def compare_dictionaries(dict1: Dict, dict2: Dict, path: str = "", ignore_keys: Optional[List[str]] = None, compare_values: bool = True, ignore_substring: str = "") -> List[str]:
"""Compare two dictionaries and print only the differences, ignoring specified keys."""
if ignore_keys is None:
ignore_keys = []

differences = []

# Get all keys from both dictionaries
all_keys = set(dict1.keys()).union(set(dict2.keys()))

for key in all_keys:
if key in ignore_keys:
continue
if key not in dict1:
differences.append(f"Key '{path + key}' found in dict2 but not in dict1")
elif key not in dict2:
differences.append(f"Key '{path + key}' found in dict1 but not in dict2")
else:
value1 = dict1[key]
value2 = dict2[key]
if isinstance(value1, dict) and isinstance(value2, dict):
# Recursively compare nested dictionaries
nested_differences = compare_dictionaries(value1, value2, path + key + ".", ignore_keys, compare_values, ignore_substring)
differences.extend(nested_differences)
elif (value1 != value2) and compare_values:
if ignore_substring:
if (not str(value1).endswith(str(value2).replace(ignore_substring, ""))) and (not str(value2).endswith(str(value1).replace(ignore_substring, ""))):
differences.append(f"Difference at '{path + key}': dict1 has {value1}, dict2 has {value2}")

else:
differences.append(f"Difference at '{path + key}': dict1 has {value1}, dict2 has {value2}")

return differences
Empty file added test/__init__.py
Empty file.
25 changes: 25 additions & 0 deletions test/conf.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
global_properties:
can_write_console_log: False
remove_tmp: False
working_dir_path: /tmp/biobb/unitests

confreader:
paths:
config_complete: file:test_data_dir/configuration/config_complete.yml
config_empty: file:test_data_dir/configuration/config_empty.yml
config_nostep_globals: file:test_data_dir/configuration/config_nostep_globals.yml
config_nostep: file:test_data_dir/configuration/config_nostep.yml

ref_config_complete_pkl: file:test_reference_dir/configuration/config_complete.pkl
ref_config_empty_pkl: file:test_reference_dir/configuration/config_empty.pkl
ref_config_nostep_globals_pkl: file:test_reference_dir/configuration/config_nostep_globals.pkl
ref_config_nostep_pkl: file:test_reference_dir/configuration/config_nostep.pkl

ref_config_complete_paths_pkl: file:test_reference_dir/configuration/config_complete_paths.pkl
ref_config_empty_paths_pkl: file:test_reference_dir/configuration/config_empty_paths.pkl
ref_config_nostep_globals_paths_pkl: file:test_reference_dir/configuration/config_nostep_globals_paths.pkl
ref_config_nostep_paths_pkl: file:test_reference_dir/configuration/config_nostep_paths.pkl

properties:
remove_tmp: False

40 changes: 40 additions & 0 deletions test/data/configuration/config_complete.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
global_properties:
working_dir_path: biobb_wf_protein-complex_md_setup
can_write_console_log: False
run_md: false

step00_cat_pdb:
tool: cat_pdb
paths:
input_structure1: superinput.pdb
input_structure2: /path/to/inputs/ions.pdb
output_structure_path: protein.ions.pdb

step4_fix_side_chain:
tool: fix_side_chain
paths:
input_pdb_path: dependency/step00_cat_pdb/output_structure_path
output_pdb_path: fixsidechain.pdb

step5_pdb2gmx:
tool: pdb2gmx
paths:
input_pdb_path: dependency/step4_fix_side_chain/output_pdb_path
output_gro_path: pdb2gmx.gro
output_top_zip_path: pdb2gmx_top.zip
properties:
force_field: amber99sb
water_type: tip3p

step9_make_ndx:
paths:
input_structure_path: file:/path/to/inputs/ligand.gro
output_ndx_path: ligand.index.ndx
properties:
selection: 0 & ! a H*

step9_bis_make_ndx:
paths:
input_structure_path: /path/to/inputs/ligand.gro
output_ndx_path: ligand.index.ndx
properties:
Empty file.
8 changes: 8 additions & 0 deletions test/data/configuration/config_nostep.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
tool: pdb2gmx
paths:
input_pdb_path: path/to/output_pdb_path
output_gro_path: pdb2gmx.gro
output_top_zip_path: pdb2gmx_top.zip
properties:
force_field: amber99sb
water_type: tip3p
13 changes: 13 additions & 0 deletions test/data/configuration/config_nostep_globals.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
global_properties:
working_dir_path: biobb_wf_protein-complex_md_setup
can_write_console_log: False
run_md: false

tool: pdb2gmx
paths:
input_pdb_path: path/to/output_pdb_path
output_gro_path: pdb2gmx.gro
output_top_zip_path: pdb2gmx_top.zip
properties:
force_field: amber99sb
water_type: tip3p
Binary file added test/reference/configuration/config_complete.pkl
Binary file not shown.
Binary file not shown.
Binary file added test/reference/configuration/config_empty.pkl
Binary file not shown.
1 change: 1 addition & 0 deletions test/reference/configuration/config_empty_paths.pkl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�}�.
Binary file added test/reference/configuration/config_nostep.pkl
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added test/reference/configuration/config_nostep_paths.pkl
Binary file not shown.
24 changes: 24 additions & 0 deletions test/unitests/test_configuration/test_confreader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# type: ignore
from biobb_common.tools import test_fixtures as fx
from biobb_common.configuration.settings import ConfReader


class TestConfReader():
def setup_class(self):
fx.test_setup(self, 'confreader')

def teardown_class(self):
# pass
fx.test_teardown(self)

def test_confreader_properties(self):
print() # Add a new line for better readability in the console
for file_name in ['config_complete', 'config_empty', 'config_nostep', 'config_nostep_globals']:
conf = ConfReader(self.paths[file_name])
assert fx.compare_object_pickle(conf.get_prop_dic(), self.paths[f'ref_{file_name}_pkl'], ignore_keys=['path', 'working_dir_path', 'sandbox_path'])

def test_confreader_paths(self):
print() # Add a new line for better readability in the console
for file_name in ['config_complete', 'config_empty', 'config_nostep', 'config_nostep_globals']:
conf = ConfReader(self.paths[file_name])
assert fx.compare_object_pickle(conf.get_paths_dic(), self.paths[f'ref_{file_name}_paths_pkl'], ignore_keys=['working_dir_path', 'sandbox_path'], ignore_substring='/Users/pau/projects/biobb_common/')

0 comments on commit 2b4f1b9

Please sign in to comment.