Coverage for biobb_haddock/haddock_restraints/haddock3_actpass_to_ambig.py: 64%

61 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 

6from typing import Optional 

7 

8from biobb_common.configuration import settings 

9from biobb_common.generic.biobb_object import BiobbObject 

10from biobb_common.tools.file_utils import launchlogger 

11 

12 

13class Haddock3ActpassToAmbig(BiobbObject): 

14 """ 

15 | biobb_haddock Haddock3ActpassToAmbig 

16 | Wrapper class for the Haddock-Restraints active_passive_to_ambig module. 

17 | `Haddock-Restraints active_passive_to_ambig <https://www.bonvinlab.org/haddock3/clients/haddock.clis.restraints.active_passive_to_ambig.html>`_ generates a corresponding ambig.tbl file to be used by HADDOCK from two given files containing active (in the first line) and passive (second line) residues. 

18 

19 Args: 

20 input_actpass1_path (str): Path to the first input HADDOCK active-passive file containing active (in the first line) and passive (second line) residues. File type: input. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_haddock/master/biobb_haddock/test/data/haddock/haddock_actpass1.txt>`_. Accepted formats: txt (edam:format_2330), dat (edam:format_2330), in (edam:format_2330), pass (edam:format_2330). 

21 input_actpass2_path (str): Path to the second input HADDOCK active-passive file containing active (in the first line) and passive (second line) residues. File type: input. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_haddock/master/biobb_haddock/test/data/haddock/haddock_actpass2.txt>`_. Accepted formats: txt (edam:format_2330), dat (edam:format_2330), in (edam:format_2330), pass (edam:format_2330). 

22 output_tbl_path (str): Path to the output HADDOCK tbl file with Ambiguous Interaction Restraints (AIR) information. File type: output. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_haddock/master/biobb_haddock/test/reference/haddock_restraints/haddock_actpass.tbl>`_. Accepted formats: tbl (edam:format_2330), txt (edam:format_2330), out (edam:format_2330). 

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

24 * **pass_to_act** (*bool*) - (False) Path to the haddock haddock executable binary. 

25 * **segid_one** (*str*) - (None) Segid of the first model. 

26 * **segid_two** (*str*) - (None) Segid of the second model. 

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

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

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

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

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

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

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

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

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

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

37 

38 

39 Examples: 

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

41 

42 from biobb_haddock.haddock_restraints.haddock3_actpass_to_ambig import haddock3_actpass_to_ambig 

43 haddock3_actpass_to_ambig( 

44 input_actpass1_path='/path/to/haddock_actpass1.txt', 

45 input_actpass2_path='/path/to/haddock_actpass2.txt', 

46 output_tbl_path='/path/to/output_AIR.tbl' 

47 ) 

48 

49 Info: 

50 * wrapped_software: 

51 * name: Haddock33-restraints 

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_actpass1_path: str, 

62 input_actpass2_path: str, 

63 output_tbl_path: 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_actpass1_path": input_actpass1_path, 

76 "input_actpass2_path": input_actpass2_path, 

77 }, 

78 "out": { 

79 "output_tbl_path": output_tbl_path, 

80 }, 

81 } 

82 

83 # Properties specific for BB 

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

85 self.pass_to_act = properties.get("pass_to_act", False) 

86 self.segid_one = properties.get("segid_one", None) 

87 self.segid_two = properties.get("segid_two", None) 

88 

89 # Check the properties 

90 self.check_properties(properties) 

91 

92 @launchlogger 

93 def launch(self) -> int: 

94 """Execute the :class:`Haddock3ActpassToAmbig <biobb_haddock.haddock_restraints.haddock3_actpass_to_ambig>` object.""" 

95 

96 # Setup Biobb 

97 if self.check_restart(): 

98 return 0 

99 self.stage_files() 

100 

101 if self.pass_to_act: 

102 with open(self.stage_io_dict['in']['input_actpass1_path'], 'r') as file1, \ 

103 open(self.stage_io_dict['in']['input_actpass2_path'], 'r') as file2: 

104 actpass1_lines = file1.readlines() 

105 actpass2_lines = file2.readlines() 

106 

107 with open(self.stage_io_dict['in']['input_actpass1_path'], 'w') as file1, \ 

108 open(self.stage_io_dict['in']['input_actpass2_path'], 'w') as file2: 

109 file1.writelines([actpass1_lines[1], actpass1_lines[0], '\n']) 

110 file2.writelines([actpass2_lines[1], actpass2_lines[0], '\n']) 

111 

112 # haddock3-restraints active_passive_to_ambig haddock_actpass.txt 

113 self.cmd = [self.binary_path, "active_passive_to_ambig", self.stage_io_dict['in'] 

114 ['input_actpass1_path'], self.stage_io_dict['in']['input_actpass2_path']] 

115 

116 if self.segid_one is not None: 

117 self.cmd.extend(["--segid-one", self.segid_one]) 

118 if self.segid_two is not None: 

119 self.cmd.extend(["--segid-two", self.segid_two]) 

120 

121 self.cmd.append(">") 

122 self.cmd.append(self.stage_io_dict['out']['output_tbl_path']) 

123 self.cmd.append("2>&1") 

124 

125 # Run Biobb block 

126 self.run_biobb() 

127 

128 # Remove deprecation warning if present 

129 with open(self.stage_io_dict['out']['output_tbl_path'], 'r') as file: 

130 lines = file.readlines() 

131 if lines and "DEPRECATION NOTICE" in lines[0]: 

132 with open(self.stage_io_dict['out']['output_tbl_path'], 'w') as file: 

133 file.writelines(lines[1:]) 

134 

135 # Copy files to host 

136 self.copy_to_host() 

137 

138 # Remove temporal files 

139 self.tmp_files.extend([self.stage_io_dict["unique_dir"]]) 

140 self.remove_tmp_files() 

141 

142 return self.return_code 

143 

144 

145def haddock3_actpass_to_ambig( 

146 input_actpass1_path: str, 

147 input_actpass2_path: str, 

148 output_tbl_path: str, 

149 properties: Optional[dict] = None, 

150 **kwargs, 

151) -> int: 

152 """Create :class:`Haddock3ActpassToAmbig <biobb_haddock.haddock_restraints.haddock3_actpass_to_ambig>` class and 

153 execute the :meth:`launch() <biobb_haddock.haddock_restraints.haddock3_actpass_to_ambig.launch>` method.""" 

154 

155 return Haddock3ActpassToAmbig( 

156 input_actpass1_path=input_actpass1_path, 

157 input_actpass2_path=input_actpass2_path, 

158 output_tbl_path=output_tbl_path, 

159 properties=properties, 

160 **kwargs, 

161 ).launch() 

162 

163 

164haddock3_actpass_to_ambig.__doc__ = Haddock3ActpassToAmbig.__doc__ 

165 

166 

167def main(): 

168 parser = argparse.ArgumentParser( 

169 description="Wrapper of the haddock-restraints active_passive_to_ambig module.", 

170 formatter_class=lambda prog: argparse.RawTextHelpFormatter( 

171 prog, width=99999), 

172 ) 

173 parser.add_argument( 

174 "-c", 

175 "--config", 

176 required=False, 

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

178 ) 

179 

180 # Specific args of each building block 

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

182 required_args.add_argument("--input_actpass1_path", required=True) 

183 required_args.add_argument("--input_actpass2_path", required=True) 

184 required_args.add_argument("--output_tbl_path", required=True) 

185 

186 args = parser.parse_args() 

187 config = args.config if args.config else None 

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

189 

190 # Specific call of each building block 

191 haddock3_actpass_to_ambig( 

192 input_actpass1_path=args.input_actpass1_path, 

193 input_actpass2_path=args.input_actpass2_path, 

194 output_tbl_path=args.output_tbl_path, 

195 properties=properties, 

196 ) 

197 

198 

199if __name__ == "__main__": 

200 main()