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

81 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-20 11:08 +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 PARAMETERS 

9 

10def check_input_path(path, argument, out_log, classname): 

11 """ Checks input file """ 

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

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

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

15 file_extension = PurePath(path).suffix 

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

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

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

19 return path 

20 

21 

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_pdb_path': ['pdb'], 

40 'output_pockets_zip': ['zip'], 

41 'output_summary': ['json'], 

42 'input_pockets_zip': ['zip'], 

43 'input_summary': ['json'], 

44 'output_filter_pockets_zip': ['zip'], 

45 'output_pocket_pdb': ['pdb'], 

46 'output_pocket_pqr': ['pqr'] 

47 } 

48 return ext in formats[argument] 

49 

50 

51# CHECK PROPERTIES 

52 

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

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

55 

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

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

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

59 

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

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

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

63 

64 return property 

65 

66 

67# PROCESS OUTPUTS 

68 

69def process_output_fpocket(tmp_folder, output_pockets_zip, output_summary, sort_by, remove_tmp, container_path, out_log, classname): 

70 """ Creates the output_pockets_zip and generates the output_summary """ 

71 

72 if container_path: 

73 path = str(PurePath(tmp_folder).joinpath('fpocket_input_out')) 

74 else: 

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

76 

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

78 if remove_tmp: 

79 # remove temporary folder 

80 fu.rm(tmp_folder) 

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

82 

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

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

85 

86 # summary 

87 # read input_info.txt file 

88 if container_path: 

89 info = PurePath(path).joinpath('fpocket_input_info.txt') 

90 else: 

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

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

93 lines = info_text.readlines() 

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

95 

96 data = {} 

97 

98 # parse input_info.txt file to python object 

99 pocket = '' 

100 for line in lines: 

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

102 # first level: pocket 

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

104 pocket = 'pocket' + num 

105 data[pocket] = {} 

106 else: 

107 # second level: pocket properties 

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

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

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

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

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

113 data[pocket][key] = value 

114 

115 # get number of pockets 

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

117 

118 # sort data by sort_by property 

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

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

121 

122 # compress pockets 

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

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

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

126 

127 # save summary 

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

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

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

131 

132 '''if remove_tmp: 

133 # remove temporary folder 

134 fu.rm(tmp_folder) 

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

136 

137 

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

139 """ Creates the output_filter_pockets_zip """ 

140 

141 # decompress the input_pockets_zip file to tmp_folder 

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

143 

144 # list all files of tmp_folder 

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

146 

147 # select search_list items from pockets_list 

148 sel_pockets_list = [p for p in pockets_list for s in search_list if s + '_' in p] 

149 

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

151 

152 # compress output to output_filter_pockets_zip 

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

154 

155 '''if remove_tmp: 

156 # remove temporary folder 

157 fu.rm(tmp_folder) 

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