Coverage for biobb_amber/sander/common.py: 68%
28 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#!/usr/bin/env python3
3""" Common functions for package biobb_amber.sander """
4from pathlib import Path, PurePath
5from biobb_common.tools import file_utils as fu
8# CHECK INPUT PARAMETERS
9def check_input_path(path, argument, optional, out_log, classname):
10 """ Checks input file """
11 if optional and not path:
12 return None
13 if not Path(path).exists():
14 fu.log("Path: " + path)
15 fu.log("Path " + path + " --- " + 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(classname + ': Format %s in %s file is not compatible' % (file_extension[1:], argument), out_log)
20 raise SystemExit(classname + ': Format %s in %s file is not compatible' % (file_extension[1:], argument))
21 return path
24def check_output_path(path, argument, optional, out_log, classname):
25 """ Checks output file """
26 if optional and not path:
27 return None
28 if PurePath(path).parent and not Path(PurePath(path).parent).exists():
29 fu.log(classname + ': Unexisting %s folder, exiting' % argument, out_log)
30 raise SystemExit(classname + ': Unexisting %s folder' % argument)
31 file_extension = PurePath(path).suffix
32 if not is_valid_file(file_extension[1:], argument):
33 fu.log(classname + ': Format %s in %s file is not compatible' % (file_extension[1:], argument), out_log)
34 raise SystemExit(classname + ': Format %s in %s file is not compatible' % (file_extension[1:], argument))
35 return path
38def is_valid_file(ext, argument):
39 """ Checks if file format is compatible """
40 formats = {
41 'input_top_path': ['top', 'prmtop', 'parmtop'],
42 'input_crd_path': ['crd', 'inpcrd', 'mdcrd', 'rst', 'rst7', 'netcdf', 'nc', 'ncrst'],
43 'input_mdin_path': ['mdin', 'txt', 'in'],
44 'input_cpin_path': ['cpin', 'txt', 'in'],
45 'input_ref_path': ['crd', 'inpcrd', 'mdcrd', 'rst', 'rst7', 'netcdf', 'nc', 'ncrst'],
46 'output_log_path': ['log', 'out', 'txt', 'o'],
47 'output_traj_path': ['trj', 'crd', 'mdcrd', 'x', 'netcdf', 'nc'],
48 'output_rst_path': ['rst', 'rst7', 'netcdf', 'nc', 'ncrst'],
49 'output_cpout_path': ['cpout'],
50 'output_cprst_path': ['cprst', 'rst', 'rst7'],
51 'output_mdinfo_path': ['mdinfo']
52 }
53 return ext in formats[argument]