Coverage for biobb_haddock/haddock/capri_eval.py: 72%

68 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 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 CapriEval(BiobbObject): 

19 """ 

20 | biobb_haddock CapriEval 

21 | Wrapper class for the Haddock CapriEval module. 

22 | The CapriEval module. `Haddock CapriEval module <https://www.bonvinlab.org/haddock3/modules/analysis/haddock.modules.analysis.caprieval.html>`_ computes Capri evaluation for a docking. 

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

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

27 reference_pdb_path (str) (Optional): Path to the input PDB file containing an structure for reference. File type: input. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_haddock/master/biobb_haddock/test/data/haddock/e2a-hpr_1GGR.pdb>`_. Accepted formats: pdb (edam:format_1476). 

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_caprieval.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": True}) `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.capri_eval import capri_eval 

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

50 capri_eval(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_evaluation_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": { 

82 "reference_pdb_path": reference_pdb_path, 

83 "haddock_config_path": haddock_config_path, 

84 }, 

85 "out": { 

86 "output_haddock_wf_data_zip": output_haddock_wf_data_zip, 

87 "output_evaluation_zip_path": output_evaluation_zip_path, 

88 }, 

89 } 

90 # Should not be copied inside container 

91 self.input_haddock_wf_data_zip = input_haddock_wf_data_zip 

92 

93 # Properties specific for BB 

94 self.haddock_step_name = "caprieval" 

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

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

97 self.global_cfg = properties.get("global_cfg", dict(postprocess=True)) 

98 

99 # Properties specific for BB 

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

101 

102 # Check the properties 

103 self.check_properties(properties) 

104 

105 @launchlogger 

106 def launch(self) -> int: 

107 """Execute the :class:`CapriEval <biobb_haddock.haddock.capri_eval>` object.""" 

108 # tmp_files = [] 

109 

110 # Setup Biobb 

111 if self.check_restart(): 

112 return 0 

113 self.stage_files() 

114 

115 # Unzip workflow data to workflow_data_out 

116 run_dir = unzip_workflow_data( 

117 zip_file=self.input_haddock_wf_data_zip, out_log=self.out_log 

118 ) 

119 

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

121 workflow_dict.update(self.global_cfg) 

122 

123 if reference_fname := self.stage_io_dict["in"].get("reference_pdb_path"): 

124 self.cfg["reference_fname"] = reference_fname 

125 

126 # Create data dir 

127 cfg_dir = fu.create_unique_dir() 

128 self.output_cfg_path = create_cfg( 

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

130 workflow_dict=workflow_dict, 

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

132 cfg_properties_dict=self.cfg, 

133 ) 

134 

135 if self.container_path: 

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

137 

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

139 self.output_cfg_path = str( 

140 Path(self.container_volume_path).joinpath( 

141 Path(self.output_cfg_path).name 

142 ) 

143 ) 

144 

145 shutil.copytree( 

146 run_dir, 

147 str( 

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

149 Path(run_dir).name 

150 ) 

151 ), 

152 ) 

153 run_dir = str( 

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

155 Path(run_dir).name 

156 ) 

157 ) 

158 

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

160 

161 # Run Biobb block 

162 self.run_biobb() 

163 

164 # Copy files to host 

165 # self.copy_to_host() 

166 

167 # Copy output 

168 haddock_output_list = [ 

169 str(path) 

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

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

172 ] 

173 haddock_output_list.sort(reverse=True) 

174 output_file_list = [ 

175 str(path) 

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

177 if path.is_file() and str(path).endswith(("izone", "aln", "tsv")) 

178 ] 

179 fu.zip_list( 

180 self.io_dict["out"]["output_evaluation_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 capri_eval( 

213 input_haddock_wf_data_zip: str, 

214 output_evaluation_zip_path: str, 

215 reference_pdb_path: Optional[str] = None, 

216 output_haddock_wf_data_zip: Optional[str] = None, 

217 haddock_config_path: Optional[str] = None, 

218 properties: Optional[dict] = None, 

219 **kwargs, 

220) -> int: 

221 """Create :class:`CapriEval <biobb_haddock.haddock.capri_eval>` class and 

222 execute the :meth:`launch() <biobb_haddock.haddock.capri_eval.launch>` method.""" 

223 

224 return CapriEval( 

225 input_haddock_wf_data_zip=input_haddock_wf_data_zip, 

226 output_evaluation_zip_path=output_evaluation_zip_path, 

227 reference_pdb_path=reference_pdb_path, 

228 output_haddock_wf_data_zip=output_haddock_wf_data_zip, 

229 haddock_config_path=haddock_config_path, 

230 properties=properties, 

231 **kwargs, 

232 ).launch() 

233 

234 

235def main(): 

236 parser = argparse.ArgumentParser( 

237 description="Wrapper of the haddock CapriEval module.", 

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

239 ) 

240 parser.add_argument( 

241 "-c", 

242 "--config", 

243 required=False, 

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

245 ) 

246 

247 # Specific args of each building block 

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

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

250 required_args.add_argument("--output_evaluation_zip_path", required=True) 

251 parser.add_argument("--reference_pdb_path", required=False) 

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

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

254 

255 args = parser.parse_args() 

256 config = args.config if args.config else None 

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

258 

259 # Specific call of each building block 

260 capri_eval( 

261 input_haddock_wf_data_zip=args.input_haddock_wf_data_zip, 

262 output_evaluation_zip_path=args.output_evaluation_zip_path, 

263 reference_pdb_path=args.reference_pdb_path, 

264 output_haddock_wf_data_zip=args.output_haddock_wf_data_zip, 

265 haddock_config_path=args.haddock_config_path, 

266 properties=properties, 

267 ) 

268 

269 

270if __name__ == "__main__": 

271 main()