Coverage for biobb_amber/ambpdb/common.py: 63%

27 statements  

« prev     ^ index     » next       coverage.py v7.5.1, created at 2024-05-07 08:11 +0000

1""" Common functions for package biobb_amber.amb2pdb """ 

2from pathlib import Path, PurePath 

3from biobb_common.tools import file_utils as fu 

4 

5 

6# CHECK INPUT PARAMETERS 

7def check_input_path(path, argument, optional, out_log, classname): 

8 """ Checks input file """ 

9 if optional and not path: 

10 return None 

11 if not Path(path).exists(): 

12 fu.log(classname + ': Unexisting %s file, exiting' % argument, out_log) 

13 raise SystemExit(classname + ': Unexisting %s file' % argument) 

14 file_extension = PurePath(path).suffix 

15 if not is_valid_file(file_extension[1:], argument): 

16 fu.log(classname + ': Format %s in %s file is not compatible' % (file_extension[1:], argument), out_log) 

17 raise SystemExit(classname + ': Format %s in %s file is not compatible' % (file_extension[1:], argument)) 

18 return path 

19 

20 

21# CHECK OUTPUT PARAMETERS 

22def check_output_path(path, argument, optional, out_log, classname): 

23 """ Checks output file """ 

24 if optional and not path: 

25 return None 

26 if PurePath(path).parent and not Path(PurePath(path).parent).exists(): 

27 fu.log(classname + ': Unexisting %s folder, exiting' % argument, out_log) 

28 raise SystemExit(classname + ': Unexisting %s folder' % argument) 

29 file_extension = PurePath(path).suffix 

30 if not is_valid_file(file_extension[1:], argument): 

31 fu.log(classname + ': Format %s in %s file is not compatible' % (file_extension[1:], argument), out_log) 

32 raise SystemExit(classname + ': Format %s in %s file is not compatible' % (file_extension[1:], argument)) 

33 return path 

34 

35 

36def is_valid_file(ext, argument): 

37 """ Checks if file format is compatible """ 

38 formats = { 

39 'input_top_path': ['top', 'prmtop', 'parmtop'], 

40 'input_crd_path': ['crd', 'mdcrd', 'inpcrd', 'rst', 'rst7'], 

41 'output_pdb_path': ['pdb'] 

42 } 

43 return ext in formats[argument]