Coverage for biobb_haddock/haddock/em_ref.py: 68%

74 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 EMRef class and the command line interface.""" 

4 

5import argparse 

6import shutil 

7from pathlib import Path 

8from typing import Optional 

9 

10from biobb_common.configuration import settings 

11from biobb_common.generic.biobb_object import BiobbObject 

12from biobb_common.tools import file_utils as fu 

13from biobb_common.tools.file_utils import launchlogger 

14 

15from biobb_haddock.haddock.common import create_cfg, unzip_workflow_data 

16 

17 

18class EMRef(BiobbObject): 

19 """ 

20 | biobb_haddock EMRef 

21 | Wrapper class for the Haddock EMRef module. 

22 | The EMRef module. `Haddock CapriEval module <https://www.bonvinlab.org/haddock3/modules/refinement/haddock.modules.refinement.emref.html>`_ computes an energy minimization refinement over selected structures. 

23 

24 Args: 

25 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_topology.zip>`_. Accepted formats: zip (edam:format_3987). 

26 refinement_output_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_rigidbody.zip>`_. Accepted formats: zip (edam:format_3987). 

27 ambig_restraints_table_path (str) (Optional): Path to the input TBL file containing a list of ambiguous restraints for docking. File type: input. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_haddock/master/biobb_haddock/test/data/haddock/e2a-hpr_air.tbl>`_. Accepted formats: tbl (edam:format_2330). 

28 unambig_restraints_table_path (str) (Optional): Path to the input TBL file containing a list of unambiguous restraints for docking. File type: input. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_haddock/master/biobb_haddock/test/data/haddock/e2a-hpr_air.tbl>`_. Accepted formats: tbl (edam:format_2330). 

29 hb_restraints_table_path (str) (Optional): Path to the input TBL file containing a list of hydrogen bond restraints for docking. File type: input. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_haddock/master/biobb_haddock/test/data/haddock/e2a-hpr_air.tbl>`_. Accepted formats: tbl (edam:format_2330). 

30 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). 

31 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). 

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

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

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

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

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

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

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

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

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

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

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

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

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

45 

46 

47 Examples: 

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

49 

50 from biobb_haddock.haddock.em_ref import em_ref 

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

52 em_ref(input_haddock_wf_data_zip='/path/to/myworkflowdata.zip', 

53 refinement_output_zip_path='/path/to/mydockingstructures.zip', 

54 properties=prop) 

55 

56 Info: 

57 * wrapped_software: 

58 * name: Haddock3 

59 * version: 2025.5 

60 * license: Apache-2.0 

61 * ontology: 

62 * name: EDAM 

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

64 """ 

65 

66 def __init__( 

67 self, 

68 input_haddock_wf_data_zip: str, 

69 refinement_output_zip_path: str, 

70 ambig_restraints_table_path: Optional[str] = None, 

71 unambig_restraints_table_path: Optional[str] = None, 

72 hb_restraints_table_path: Optional[str] = None, 

73 output_haddock_wf_data_zip: Optional[str] = None, 

74 haddock_config_path: Optional[str] = None, 

75 properties: Optional[dict] = None, 

76 **kwargs, 

77 ) -> None: 

78 properties = properties or {} 

79 

80 # Call parent class constructor 

81 super().__init__(properties) 

82 

83 # Input/Output files 

84 self.io_dict = { 

85 "in": { 

86 "ambig_restraints_table_path": ambig_restraints_table_path, 

87 "unambig_restraints_table_path": unambig_restraints_table_path, 

88 "hb_restraints_table_path": hb_restraints_table_path, 

89 "haddock_config_path": haddock_config_path, 

90 }, 

91 "out": { 

92 "output_haddock_wf_data_zip": output_haddock_wf_data_zip, 

93 "refinement_output_zip_path": refinement_output_zip_path, 

94 }, 

95 } 

96 # Should not be copied inside container 

97 self.input_haddock_wf_data_zip = input_haddock_wf_data_zip 

98 

99 # Properties specific for BB 

100 self.haddock_step_name = "emref" 

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

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

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

104 

105 # Properties specific for BB 

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

107 

108 # Check the properties 

109 self.check_properties(properties) 

110 

111 @launchlogger 

112 def launch(self) -> int: 

113 """Execute the :class:`EMRef <biobb_haddock.haddock.em_ref>` object.""" 

114 # tmp_files = [] 

115 

116 # Setup Biobb 

117 if self.check_restart(): 

118 return 0 

119 self.stage_files() 

120 

121 # Unzip workflow data to workflow_data_out 

122 run_dir = unzip_workflow_data( 

123 zip_file=self.input_haddock_wf_data_zip, out_log=self.out_log 

124 ) 

125 

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

127 workflow_dict.update(self.global_cfg) 

128 

129 if ambig_path := self.stage_io_dict["in"].get("ambig_restraints_table_path"): 

130 self.cfg["ambig_fname"] = ambig_path 

131 

132 if unambig_fname := self.stage_io_dict["in"].get("unambig_restraints_table_path"): 

133 self.cfg["unambig_fname"] = unambig_fname 

134 

135 if hbond_fname := self.stage_io_dict["in"].get("hb_restraints_table_path"): 

136 self.cfg["hbond_fname"] = hbond_fname 

137 

138 # Create data dir 

139 cfg_dir = fu.create_unique_dir() 

140 self.output_cfg_path = create_cfg( 

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

142 workflow_dict=workflow_dict, 

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

144 cfg_properties_dict=self.cfg, 

145 local_log=self.out_log, 

146 global_log=self.global_log, 

147 ) 

148 

149 if self.container_path: 

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

151 

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

153 self.output_cfg_path = str( 

154 Path(self.container_volume_path).joinpath( 

155 Path(self.output_cfg_path).name 

156 ) 

157 ) 

158 

159 shutil.copytree( 

160 run_dir, 

161 str( 

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

163 Path(run_dir).name 

164 ) 

165 ), 

166 ) 

167 run_dir = str( 

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

169 Path(run_dir).name 

170 ) 

171 ) 

172 

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

174 

175 # Run Biobb block 

176 self.run_biobb() 

177 

178 # Copy files to host 

179 # self.copy_to_host() 

180 

181 # Copy output 

182 

183 haddock_output_list = [ 

184 str(path) 

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

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

187 ] 

188 haddock_output_list.sort(reverse=True) 

189 output_file_list = list( 

190 Path(haddock_output_list[0]).glob( 

191 workflow_dict["haddock_step_name"] + r"*.pdb*" 

192 ) 

193 ) 

194 fu.zip_list( 

195 self.io_dict["out"]["refinement_output_zip_path"], 

196 output_file_list, 

197 self.out_log, 

198 ) 

199 

200 # Create zip output 

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

202 fu.log( 

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

204 self.out_log, 

205 self.global_log, 

206 ) 

207 shutil.make_archive( 

208 str( 

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

210 "" 

211 ) 

212 ), 

213 "zip", 

214 run_dir, 

215 ) 

216 

217 # Remove temporal files 

218 self.tmp_files.extend([run_dir, 

219 cfg_dir, 

220 self.stage_io_dict.get("unique_dir") 

221 ]) 

222 self.remove_tmp_files() 

223 

224 return self.return_code 

225 

226 

227def em_ref( 

228 input_haddock_wf_data_zip: str, 

229 refinement_output_zip_path: str, 

230 ambig_restraints_table_path: Optional[str] = None, 

231 unambig_restraints_table_path: Optional[str] = None, 

232 hb_restraints_table_path: Optional[str] = None, 

233 output_haddock_wf_data_zip: Optional[str] = None, 

234 haddock_config_path: Optional[str] = None, 

235 properties: Optional[dict] = None, 

236 **kwargs, 

237) -> int: 

238 """Create :class:`EMRef <biobb_haddock.haddock.em_ref>` class and 

239 execute the :meth:`launch() <biobb_haddock.haddock.em_ref.launch>` method.""" 

240 

241 return EMRef( 

242 input_haddock_wf_data_zip=input_haddock_wf_data_zip, 

243 refinement_output_zip_path=refinement_output_zip_path, 

244 ambig_restraints_table_path=ambig_restraints_table_path, 

245 unambig_restraints_table_path=unambig_restraints_table_path, 

246 hb_restraints_table_path=hb_restraints_table_path, 

247 output_haddock_wf_data_zip=output_haddock_wf_data_zip, 

248 haddock_config_path=haddock_config_path, 

249 properties=properties, 

250 **kwargs, 

251 ).launch() 

252 

253 

254def main(): 

255 parser = argparse.ArgumentParser( 

256 description="Wrapper of the haddock EMRef module.", 

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

258 ) 

259 parser.add_argument( 

260 "-c", 

261 "--config", 

262 required=False, 

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

264 ) 

265 

266 # Specific args of each building block 

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

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

269 required_args.add_argument("--refinement_output_zip_path", required=True) 

270 parser.add_argument("--ambig_restraints_table_path", required=False) 

271 parser.add_argument("--unambig_restraints_table_path", required=False) 

272 parser.add_argument("--hb_restraints_table_path", required=False) 

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

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

275 

276 args = parser.parse_args() 

277 config = args.config if args.config else None 

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

279 

280 # Specific call of each building block 

281 em_ref( 

282 input_haddock_wf_data_zip=args.input_haddock_wf_data_zip, 

283 refinement_output_zip_path=args.refinement_output_zip_path, 

284 ambig_restraints_table_path=args.ambig_restraints_table_path, 

285 unambig_restraints_table_path=args.unambig_restraints_table_path, 

286 hb_restraints_table_path=args.hb_restraints_table_path, 

287 output_haddock_wf_data_zip=args.output_haddock_wf_data_zip, 

288 haddock_config_path=args.haddock_config_path, 

289 properties=properties, 

290 ) 

291 

292 

293if __name__ == "__main__": 

294 main()