Coverage for biobb_haddock/haddock/haddock3_extend.py: 73%

56 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 haddock3 run class and the command line interface.""" 

4 

5import argparse 

6import zipfile 

7import shutil 

8from pathlib import Path 

9from typing import Optional 

10import os 

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 

16 

17class Haddock3Extend(BiobbObject): 

18 """ 

19 | biobb_haddock Haddock3Extend 

20 | Wrapper class for the Haddock3 extend module. 

21 | The `Haddock3 extend <https://www.bonvinlab.org/haddock3/tutorials/continuing_runs.html>`_. module continues the HADDOCK3 execution for docking of an already started run. 

22 

23 Args: 

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

25 haddock_config_path (str): 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). 

26 output_haddock_wf_data_zip (str): 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/reference/haddock/ref_topology.zip>`_. Accepted formats: zip (edam:format_3987). 

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

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

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

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

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

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

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

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

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

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

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

38 

39 

40 Examples: 

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

42 

43 from biobb_haddock.haddock.haddock3_extend import haddock3_extend 

44 haddock3_extend(input_haddock_wf_data_zip='/path/to/myworkflowdata.zip', 

45 haddock_config_path='/path/to/myHaddockConfig.cfg', 

46 output_haddock_wf_data_zip='/path/to/haddock_output.zip', 

47 properties=prop) 

48 

49 Info: 

50 * wrapped_software: 

51 * name: Haddock3 

52 * version: 2025.5 

53 * license: Apache-2.0 

54 * ontology: 

55 * name: EDAM 

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

57 """ 

58 

59 def __init__( 

60 self, 

61 input_haddock_wf_data_zip: str, 

62 haddock_config_path: str, 

63 output_haddock_wf_data_zip: str, 

64 properties: Optional[dict] = None, 

65 **kwargs, 

66 ) -> None: 

67 properties = properties or {} 

68 

69 # Call parent class constructor 

70 super().__init__(properties) 

71 

72 # Input/Output files 

73 self.io_dict = { 

74 "in": { 

75 "input_haddock_wf_data_zip": input_haddock_wf_data_zip, 

76 "haddock_config_path": haddock_config_path, 

77 }, 

78 "out": { 

79 "output_haddock_wf_data_zip": output_haddock_wf_data_zip, 

80 }, 

81 } 

82 

83 # Properties specific for BB 

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

85 

86 # Check the properties 

87 self.check_properties(properties) 

88 

89 @launchlogger 

90 def launch(self) -> int: 

91 """Execute the :class:`Haddock3Extend <biobb_haddock.haddock.haddock3_extend>` object.""" 

92 

93 # Setup Biobb 

94 if self.check_restart(): 

95 return 0 

96 self.stage_files() 

97 

98 # Decompress input zip 

99 run_dir = fu.create_unique_dir(self.stage_io_dict["unique_dir"]) 

100 with zipfile.ZipFile(self.stage_io_dict["in"]["input_haddock_wf_data_zip"], 'r') as zip_ref: 

101 zip_ref.extractall(run_dir) 

102 cwd = os.getcwd() 

103 # Move the unzip folder 

104 os.chdir(run_dir) 

105 

106 if self.container_path: 

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

108 

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

110 self.output_cfg_path = str( 

111 Path(self.container_volume_path).joinpath( 

112 Path(self.output_cfg_path).name 

113 ) 

114 ) 

115 

116 self.cmd = [self.binary_path, self.stage_io_dict["in"]["haddock_config_path"]] 

117 self.cmd.extend(["--extend-run", run_dir]) 

118 

119 # Run Biobb block 

120 self.run_biobb() 

121 # Move back to the stage directory 

122 os.chdir(cwd) 

123 # Create zip output 

124 fu.log( 

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

126 self.out_log, self.global_log) 

127 shutil.make_archive( 

128 str(Path(self.io_dict["out"]["output_haddock_wf_data_zip"]).with_suffix("")), 

129 "zip", 

130 str(run_dir), 

131 ) 

132 

133 # Remove temporal files 

134 self.tmp_files.extend([self.stage_io_dict.get("unique_dir"), run_dir]) 

135 self.remove_tmp_files() 

136 

137 return self.return_code 

138 

139 

140def haddock3_extend( 

141 input_haddock_wf_data_zip: str, 

142 haddock_config_path: str, 

143 output_haddock_wf_data_zip: str, 

144 properties: Optional[dict] = None, 

145 **kwargs, 

146) -> int: 

147 """Create :class:`Haddock3Extend <biobb_haddock.haddock.haddock3_extend>` class and 

148 execute the :meth:`launch() <biobb_haddock.haddock.haddock3_extend.launch>` method.""" 

149 

150 return Haddock3Extend( 

151 input_haddock_wf_data_zip=input_haddock_wf_data_zip, 

152 haddock_config_path=haddock_config_path, 

153 output_haddock_wf_data_zip=output_haddock_wf_data_zip, 

154 properties=properties, 

155 **kwargs, 

156 ).launch() 

157 

158 

159haddock3_extend.__doc__ = Haddock3Extend.__doc__ 

160 

161 

162def main(): 

163 parser = argparse.ArgumentParser( 

164 description="Wrapper of the haddock3 HADDOCK3 module.", 

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

166 ) 

167 parser.add_argument( 

168 "-c", 

169 "--config", 

170 required=False, 

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

172 ) 

173 

174 # Specific args of each building block 

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

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

177 required_args.add_argument("--haddock_config_path", required=True) 

178 required_args.add_argument("--output_haddock_wf_data_zip", required=True) 

179 

180 args = parser.parse_args() 

181 config = args.config if args.config else None 

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

183 

184 # Specific call of each building block 

185 haddock3_extend( 

186 input_haddock_wf_data_zip=args.input_haddock_wf_data_zip, 

187 haddock_config_path=args.haddock_config_path, 

188 output_haddock_wf_data_zip=args.output_haddock_wf_data_zip, 

189 properties=properties, 

190 ) 

191 

192 

193if __name__ == "__main__": 

194 main()