Coverage for biobb_amber/leap/common.py: 46%
37 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-28 08:28 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-28 08:28 +0000
1"""Common functions for package biobb_amber.leap"""
3from pathlib import Path, PurePath
4from typing import Optional, Union
6from biobb_common.tools import file_utils as fu
9# CHECK INPUT PARAMETERS
10def check_input_path(path, argument, optional, out_log, classname):
11 """Checks input file"""
12 if optional and not path:
13 return None
14 if not Path(path).exists():
15 fu.log(classname + ": Unexisting %s file, exiting" % argument, out_log)
16 raise SystemExit(classname + ": Unexisting %s file" % argument)
17 file_extension = PurePath(path).suffix
18 if not is_valid_file(file_extension[1:], argument):
19 fu.log(
20 classname + ": Format %s in %s file is not compatible"
21 % (file_extension[1:], argument),
22 out_log,
23 )
24 raise SystemExit(
25 classname + ": Format %s in %s file is not compatible"
26 % (file_extension[1:], argument)
27 )
28 return path
31# CHECK OUTPUT PARAMETERS
32def check_output_path(path, argument, optional, out_log, classname):
33 """Checks output file"""
34 if optional and not path:
35 return None
36 if PurePath(path).parent and not Path(PurePath(path).parent).exists():
37 fu.log(classname + ": Unexisting %s folder, exiting" % argument, out_log)
38 raise SystemExit(classname + ": Unexisting %s folder" % argument)
39 file_extension = PurePath(path).suffix
40 if not is_valid_file(file_extension[1:], argument):
41 fu.log(
42 classname + ": Format %s in %s file is not compatible"
43 % (file_extension[1:], argument),
44 out_log,
45 )
46 raise SystemExit(
47 classname + ": Format %s in %s file is not compatible"
48 % (file_extension[1:], argument)
49 )
50 return path
53def is_valid_file(ext, argument):
54 """Checks if file format is compatible"""
55 formats = {
56 "input_pdb_path": ["pdb"],
57 "input_lib_path": ["lib", "zip"],
58 "input_frcmod_path": ["frcmod", "zip"],
59 "input_params_path": ["in", "leapin", "txt", "zip"],
60 "input_source_path": ["in", "leapin", "txt", "zip"],
61 "output_pdb_path": ["pdb"],
62 "output_top_path": ["top", "prmtop", "parmtop"],
63 "output_crd_path": ["crd", "mdcrd", "inpcrd", "rst", "rst7"],
64 }
65 return ext in formats[argument]
68def _from_string_to_list(input_data: Optional[Union[str, list[str]]]) -> list[str]:
69 """
70 Converts a string to a list, splitting by commas or spaces. If the input is already a list, returns it as is.
71 Returns an empty list if input_data is None.
73 Parameters:
74 input_data (str, list, or None): The string, list, or None value to convert.
76 Returns:
77 list: A list of string elements or an empty list if input_data is None.
78 """
79 if input_data is None:
80 return []
82 if isinstance(input_data, list):
83 # If input is already a list, return it
84 return input_data
86 # If input is a string, determine the delimiter based on presence of commas
87 delimiter = "," if "," in input_data else " "
88 items = input_data.split(delimiter)
90 # Remove whitespace from each item and ignore empty strings
91 processed_items = [item.strip() for item in items if item.strip()]
93 return processed_items