Coverage for biobb_haddock/haddock/sele_top.py: 75%

72 statements  

« prev     ^ index     » next       coverage.py v7.10.2, created at 2025-08-07 08:48 +0000

1#!/usr/bin/env python3 

2 

3"""Module containing the haddock class and the command line interface.""" 

4 

5import jsonpickle 

6import argparse 

7import shutil 

8from pathlib import Path 

9from typing import Optional 

10 

11from biobb_common.configuration import settings 

12from biobb_common.generic.biobb_object import BiobbObject 

13from biobb_common.tools import file_utils as fu 

14from biobb_common.tools.file_utils import launchlogger 

15 

16from biobb_haddock.haddock.common import create_cfg, unzip_workflow_data 

17 

18 

19class SeleTop(BiobbObject): 

20 """ 

21 | biobb_haddock SeleTop 

22 | Wrapper class for the Haddock SeleTop module. 

23 | The SeleTop module. `Haddock SeleTop module <https://www.bonvinlab.org/haddock3/modules/analysis/haddock.modules.analysis.seletop.html>`_ selects the top models of a docking. 

24 

25 Args: 

26 input_haddock_wf_data_zip (str): Path to the input zipball containing all the current Haddock workflow data. File type: input. `Sample file <https://github.com/bioexcel/biobb_haddock/raw/master/biobb_haddock/test/data/haddock/haddock_wf_data_rigid.zip>`_. Accepted formats: zip (edam:format_3987). 

27 output_selection_zip_path (str): Path to the output PDB file collection in zip format. File type: output. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_haddock/master/biobb_haddock/test/reference/haddock/ref_seletop.zip>`_. Accepted formats: zip (edam:format_3987). 

28 output_haddock_wf_data_zip (str) (Optional): Path to the output zipball containing all the current Haddock workflow data. File type: output. `Sample file <https://github.com/bioexcel/biobb_haddock/raw/master/biobb_haddock/test/data/haddock/haddock_wf_data_emref.zip>`_. Accepted formats: zip (edam:format_3987). 

29 haddock_config_path (str) (Optional): Haddock configuration CFG file path. File type: input. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_haddock/master/biobb_haddock/test/data/haddock/run.cfg>`_. Accepted formats: cfg (edam:format_1476). 

30 properties (dict - Python dictionary object containing the tool parameters, not input/output files): 

31 * **cfg** (*dict*) - ({}) Haddock configuration options specification. 

32 * **global_cfg** (*dict*) - ({"postprocess": False}) `Global configuration options <https://www.bonvinlab.org/haddock3-user-manual/global_parameters.html>`_ specification. 

33 * **binary_path** (*str*) - ("haddock") Path to the haddock haddock executable binary. 

34 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files. 

35 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist. 

36 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory. 

37 * **container_path** (*str*) - (None) Path to the binary executable of your container. 

38 * **container_image** (*str*) - (None) Container Image identifier. 

39 * **container_volume_path** (*str*) - ("/data") Path to an internal directory in the container. 

40 * **container_working_dir** (*str*) - (None) Path to the internal CWD in the container. 

41 * **container_user_id** (*str*) - (None) User number id to be mapped inside the container. 

42 * **container_shell_path** (*str*) - ("/bin/bash") Path to the binary executable of the container shell. 

43 

44 

45 Examples: 

46 This is a use example of how to use the building block from Python:: 

47 

48 from biobb_haddock.haddock.sele_top import sele_top 

49 prop = { 'binary_path': 'haddock' } 

50 sele_top(input_haddock_wf_data_zip='/path/to/myworkflowdata.zip', 

51 output_evaluation_zip='/path/to/myevalfiles.zip', 

52 properties=prop) 

53 

54 Info: 

55 * wrapped_software: 

56 * name: Haddock3 

57 * version: 2025.5 

58 * license: Apache-2.0 

59 * ontology: 

60 * name: EDAM 

61 * schema: http://edamontology.org/EDAM.owl 

62 """ 

63 

64 def __init__( 

65 self, 

66 input_haddock_wf_data_zip: str, 

67 output_selection_zip_path: str, 

68 reference_pdb_path: Optional[str] = None, 

69 output_haddock_wf_data_zip: Optional[str] = None, 

70 haddock_config_path: Optional[str] = None, 

71 properties: Optional[dict] = None, 

72 **kwargs, 

73 ) -> None: 

74 properties = properties or {} 

75 

76 # Call parent class constructor 

77 super().__init__(properties) 

78 

79 # Input/Output files 

80 self.io_dict = { 

81 "in": {"haddock_config_path": haddock_config_path}, 

82 "out": { 

83 "output_haddock_wf_data_zip": output_haddock_wf_data_zip, 

84 "output_selection_zip_path": output_selection_zip_path, 

85 }, 

86 } 

87 # Should not be copied inside container 

88 self.input_haddock_wf_data_zip = input_haddock_wf_data_zip 

89 

90 # Properties specific for BB 

91 self.haddock_step_name = "seletop" 

92 self.output_cfg_path = properties.get("output_cfg_path", "haddock.cfg") 

93 self.cfg = {k: v for k, v in properties.get("cfg", dict()).items()} 

94 self.global_cfg = properties.get("global_cfg", dict(postprocess=False)) 

95 

96 # Properties specific for BB 

97 self.binary_path = properties.get("binary_path", "haddock3") 

98 

99 # Check the properties 

100 self.check_properties(properties) 

101 

102 @launchlogger 

103 def launch(self) -> int: 

104 """Execute the :class:`SeleTop <biobb_haddock.haddock.sele_top>` object.""" 

105 # tmp_files = [] 

106 

107 # Setup Biobb 

108 if self.check_restart(): 

109 return 0 

110 self.stage_files() 

111 

112 # Unzip workflow data to workflow_data_out 

113 run_dir = unzip_workflow_data( 

114 zip_file=self.input_haddock_wf_data_zip, out_log=self.out_log 

115 ) 

116 

117 workflow_dict = {"haddock_step_name": self.haddock_step_name} 

118 workflow_dict.update(self.global_cfg) 

119 

120 # Create data dir 

121 cfg_dir = fu.create_unique_dir() 

122 self.output_cfg_path = create_cfg( 

123 output_cfg_path=str(Path(cfg_dir).joinpath(self.output_cfg_path)), 

124 workflow_dict=workflow_dict, 

125 input_cfg_path=self.stage_io_dict["in"].get("haddock_config_path"), 

126 cfg_properties_dict=self.cfg, 

127 ) 

128 

129 if self.container_path: 

130 fu.log("Container execution enabled", self.out_log) 

131 

132 shutil.copy2(self.output_cfg_path, self.stage_io_dict.get("unique_dir", "")) 

133 self.output_cfg_path = str( 

134 Path(self.container_volume_path).joinpath( 

135 Path(self.output_cfg_path).name 

136 ) 

137 ) 

138 

139 shutil.copytree( 

140 run_dir, 

141 str( 

142 Path(self.stage_io_dict.get("unique_dir", "")).joinpath( 

143 Path(run_dir).name 

144 ) 

145 ), 

146 ) 

147 run_dir = str( 

148 Path(self.stage_io_dict.get("unique_dir", "")).joinpath( 

149 Path(run_dir).name 

150 ) 

151 ) 

152 

153 self.cmd = [self.binary_path, self.output_cfg_path, "--extend-run", run_dir] 

154 

155 # Run Biobb block 

156 self.run_biobb() 

157 

158 # Copy files to host 

159 # self.copy_to_host() 

160 

161 # Copy output 

162 haddock_output_list = [ 

163 str(path) 

164 for path in Path(run_dir).iterdir() 

165 if path.is_dir() and str(path).endswith(workflow_dict["haddock_step_name"]) 

166 ] 

167 haddock_output_list.sort(reverse=True) 

168 output_file_list = [ 

169 str(path) 

170 for path in Path(haddock_output_list[0]).iterdir() 

171 if path.is_file() and str(path.name) not in ["io.json", "params.cfg"] 

172 ] 

173 with open(haddock_output_list[0]+'/io.json') as json_file: 

174 content = jsonpickle.decode(json_file.read()) 

175 output = content["output"] 

176 for file in output: 

177 rel_path = str(file.rel_path).split('/') 

178 output_file_list.extend(list(Path(run_dir+'/'+rel_path[-2]).glob(rel_path[-1]+'*'))) 

179 fu.zip_list( 

180 self.io_dict["out"]["output_selection_zip_path"], 

181 output_file_list, 

182 self.out_log, 

183 ) 

184 

185 # Create zip output 

186 if self.io_dict["out"].get("output_haddock_wf_data_zip"): 

187 fu.log( 

188 f"Zipping {run_dir} to {str(Path(self.io_dict['out']['output_haddock_wf_data_zip']).with_suffix(''))} ", 

189 self.out_log, 

190 self.global_log, 

191 ) 

192 shutil.make_archive( 

193 str( 

194 Path(self.io_dict["out"]["output_haddock_wf_data_zip"]).with_suffix( 

195 "" 

196 ) 

197 ), 

198 "zip", 

199 run_dir, 

200 ) 

201 

202 # Remove temporal files 

203 self.tmp_files.extend([run_dir, 

204 cfg_dir, 

205 self.stage_io_dict.get("unique_dir") 

206 ]) 

207 self.remove_tmp_files() 

208 

209 return self.return_code 

210 

211 

212def sele_top( 

213 input_haddock_wf_data_zip: str, 

214 output_selection_zip_path: str, 

215 output_haddock_wf_data_zip: Optional[str] = None, 

216 haddock_config_path: Optional[str] = None, 

217 properties: Optional[dict] = None, 

218 **kwargs, 

219) -> int: 

220 """Create :class:`SeleTop <biobb_haddock.haddock.sele_top>` class and 

221 execute the :meth:`launch() <biobb_haddock.haddock.sele_top.launch>` method.""" 

222 

223 return SeleTop( 

224 input_haddock_wf_data_zip=input_haddock_wf_data_zip, 

225 output_selection_zip_path=output_selection_zip_path, 

226 output_haddock_wf_data_zip=output_haddock_wf_data_zip, 

227 addock_config_path=haddock_config_path, 

228 properties=properties, 

229 **kwargs, 

230 ).launch() 

231 

232 

233def main(): 

234 parser = argparse.ArgumentParser( 

235 description="Wrapper of the haddock SeleTop module.", 

236 formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999), 

237 ) 

238 parser.add_argument( 

239 "-c", 

240 "--config", 

241 required=False, 

242 help="This file can be a YAML file, JSON file or JSON string", 

243 ) 

244 

245 # Specific args of each building block 

246 required_args = parser.add_argument_group("required arguments") 

247 required_args.add_argument("--input_haddock_wf_data_zip", required=True) 

248 required_args.add_argument("--output_selection_zip_path", required=True) 

249 parser.add_argument("--output_haddock_wf_data_zip", required=False) 

250 parser.add_argument("--haddock_config_path", required=False) 

251 

252 args = parser.parse_args() 

253 config = args.config if args.config else None 

254 properties = settings.ConfReader(config=config).get_prop_dic() 

255 

256 # Specific call of each building block 

257 sele_top( 

258 input_haddock_wf_data_zip=args.input_haddock_wf_data_zip, 

259 output_selection_zip_path=args.output_selection_zip_path, 

260 reference_pdb_path=args.reference_pdb_path, 

261 output_haddock_wf_data_zip=args.output_haddock_wf_data_zip, 

262 haddock_config_path=args.haddock_config_path, 

263 properties=properties, 

264 ) 

265 

266 

267if __name__ == "__main__": 

268 main()