Coverage for biobb_vs/fpocket/common.py: 88%

51 statements  

« prev     ^ index     » next       coverage.py v7.18.0, created at 2026-12-03 15:52 +0000

1""" Common functions for package biobb_vs.fpocket """ 

2from pathlib import Path, PurePath 

3import json 

4import re 

5from biobb_common.tools import file_utils as fu 

6 

7 

8# CHECK PROPERTIES 

9 

10def check_range(name, property, values, out_log, classname): 

11 """ Checks the format of a range for fpocket_filter """ 

12 

13 if not isinstance(property, list) or len(property) != 2 or not all(isinstance(n, (int, float)) for n in property): 

14 fu.log(classname + ': Incorrect format for %s property, exiting' % name, out_log) 

15 raise SystemExit(classname + ': Incorrect format for %s property, exiting' % name) 

16 

17 if property[0] < values[0] or property[1] > values[1]: 

18 fu.log(classname + ': %s is out of [%s] range, exiting' % (name, ', '.join(str(v) for v in values)), out_log) 

19 raise SystemExit(classname + ': %s is out of [%s] range, exiting' % (name, ', '.join(str(v) for v in values))) 

20 

21 return property 

22 

23 

24# PROCESS OUTPUTS 

25 

26def process_output_fpocket(tmp_folder, output_pockets_zip, output_summary, sort_by, out_log, classname): 

27 """ Creates the output_pockets_zip and generates the output_summary from the outputs of tmp_folder/input.pdb """ 

28 

29 path = str(PurePath(tmp_folder).joinpath('input_out')) 

30 

31 if not Path(path).is_dir(): 

32 fu.log(classname + ': Error executing fpocket, please check your properties', out_log) 

33 raise SystemExit(classname + ': Error executing fpocket, please check your properties') 

34 

35 # summary 

36 # read input_info.txt file 

37 info = PurePath(path).joinpath('input_info.txt') 

38 with open(info, 'r') as info_text: 

39 lines = info_text.readlines() 

40 lines = [x for x in lines if x != '\n'] 

41 

42 data = {} 

43 

44 # parse input_info.txt file to python object 

45 pocket = '' 

46 for line in lines: 

47 if not line.startswith('\t'): 

48 # first level: pocket 

49 num = re.findall('\\d+', line)[0] 

50 pocket = 'pocket' + num 

51 data[pocket] = {} 

52 else: 

53 # second level: pocket properties 

54 groups = re.findall('(.*)(?:\\ *\\:\\ *)(.*)', line)[0] 

55 key = groups[0].lower().strip() 

56 key = re.sub(r'\-|\.', '', key) 

57 key = re.sub(r'\s+', '_', key) 

58 value = float(groups[1]) if '.' in groups[1] else int(groups[1]) 

59 data[pocket][key] = value 

60 

61 # get number of pockets 

62 fu.log('%d pockets found' % (len(data)), out_log) 

63 

64 # sort data by sort_by property 

65 fu.log('Sorting output data by %s' % (sort_by), out_log) 

66 data = dict(sorted(data.items(), key=lambda item: float(item[1][sort_by]), reverse=True)) 

67 

68 # compress pockets 

69 pockets = PurePath(path).joinpath('pockets') 

70 files_list = [str(i) for i in Path(pockets).iterdir()] 

71 fu.zip_list(zip_file=output_pockets_zip, file_list=files_list, out_log=out_log) 

72 

73 # save summary 

74 fu.log('Saving summary to %s file' % (output_summary), out_log) 

75 with open(output_summary, 'w') as outfile: 

76 json.dump(data, outfile, indent=4) 

77 

78 '''if remove_tmp: 

79 # remove temporary folder 

80 fu.rm(tmp_folder) 

81 fu.log('Removed temporary folder: %s' % tmp_folder, out_log)''' 

82 

83 

84def process_output_fpocket_filter(search_list, tmp_folder, input_pockets_zip, output_filter_pockets_zip, remove_tmp, out_log): 

85 """ Creates the output_filter_pockets_zip """ 

86 

87 # decompress the input_pockets_zip file to tmp_folder 

88 fu.unzip_list(zip_file=input_pockets_zip, dest_dir=tmp_folder, out_log=out_log) 

89 

90 # list all files of tmp_folder 

91 pockets_list = [str(i) for i in Path(tmp_folder).iterdir()] 

92 

93 # select search_list items from pockets_list: fpocket names the files of pocket N pocketN_* 

94 sel_pockets_list = [p for p in pockets_list for s in search_list if PurePath(p).name.startswith(s + '_')] 

95 

96 fu.log('Creating %s output file' % output_filter_pockets_zip, out_log) 

97 

98 # compress output to output_filter_pockets_zip 

99 fu.zip_list(zip_file=output_filter_pockets_zip, file_list=sel_pockets_list, out_log=out_log) 

100 

101 '''if remove_tmp: 

102 # remove temporary folder 

103 fu.rm(tmp_folder) 

104 fu.log('Removed temporary folder: %s' % tmp_folder, out_log)'''