Coverage for biobb_haddock/haddock_restraints/haddock3_accessibility.py: 74%

70 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 glob 

7import os 

8import shutil 

9from typing import Optional 

10 

11from biobb_common.configuration import settings 

12from biobb_common.generic.biobb_object import BiobbObject 

13from biobb_common.tools.file_utils import launchlogger 

14 

15 

16class Haddock3Accessibility(BiobbObject): 

17 """ 

18 | biobb_haddock Haddock3Accessibility 

19 | Wrapper class for the Haddock-Restraints Accessibility module. 

20 | `Haddock-Restraints Accessibility <https://www.bonvinlab.org/haddock3/clients/haddock.clis.restraints.calc_accessibility.html>`_ computes residues accessibility using freesasa included in the Haddock3 package. 

21 

22 Args: 

23 input_pdb_path (str): Path to the input PDB file. File type: input. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_haddock/master/biobb_haddock/test/data/haddock/e2aP_1F3G_noH.pdb>`_. Accepted formats: pdb (edam:format_1476). 

24 output_accessibility_path (str): Path to the output file with accessibility information. File type: output. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_haddock/master/biobb_haddock/test/reference/haddock_restraints/mol1_sasa.txt>`_. Accepted formats: txt (edam:format_2330), dat (edam:format_2330), out (edam:format_2330). 

25 output_actpass_path (str) (Optional): Path to the output file with active/passive residues to be used as haddock3 restraint information. File type: output. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_haddock/master/biobb_haddock/test/reference/haddock_restraints/mol1_haddock_actpass.txt>`_. Accepted formats: txt (edam:format_2330), dat (edam:format_2330), out (edam:format_2330). 

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

27 * **chain** (*str*) - ("A") Chain to be used from the input PDB file. 

28 * **cutoff** (*float*) - (0.4) Relative cutoff for sidechain accessibility. 

29 * **probe_radius** (*float*) - (1.4) Probe radius for the accessibility calculation. 

30 * **pass_to_act** (*bool*) - (False) If True, the passive residues become active in the actpass file and vice versa. 

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

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

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

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

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

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

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

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

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

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

41 

42 

43 Examples: 

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

45 

46 from biobb_haddock.haddock_restraints.haddock3_accessibility import haddock3_accessibility 

47 prop = { 'cutoff': 0.4 } 

48 haddock3_accessibility(input_pdb_path='/path/to/mypdb.pdb', 

49 output_accessibility_path='/path/to/output_report.txt', 

50 properties=prop) 

51 

52 Info: 

53 * wrapped_software: 

54 * name: Haddock33-restraints 

55 * version: 2025.5 

56 * license: Apache-2.0 

57 * ontology: 

58 * name: EDAM 

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

60 """ 

61 

62 def __init__( 

63 self, 

64 input_pdb_path: str, 

65 output_accessibility_path: str, 

66 output_actpass_path: Optional[str] = None, 

67 properties: Optional[dict] = None, 

68 **kwargs, 

69 ) -> None: 

70 properties = properties or {} 

71 

72 # Call parent class constructor 

73 super().__init__(properties) 

74 

75 # Input/Output files 

76 self.io_dict = { 

77 "in": { 

78 "input_pdb_path": input_pdb_path, 

79 }, 

80 "out": { 

81 "output_accessibility_path": output_accessibility_path, 

82 "output_actpass_path": output_actpass_path, 

83 }, 

84 } 

85 

86 # Properties specific for BB 

87 self.chain = properties.get("chain", "A") 

88 self.cutoff = properties.get("cutoff", 0.4) 

89 self.probe_radius = properties.get("probe_radius", 1.4) 

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

91 

92 # Properties specific for BB 

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

94 

95 # Check the properties 

96 self.check_properties(properties) 

97 

98 @launchlogger 

99 def launch(self) -> int: 

100 """Execute the :class:`Haddock3Accessibility <biobb_haddock.haddock_restraints.haddock3_accessibility>` object.""" 

101 

102 # Setup Biobb 

103 if self.check_restart(): 

104 return 0 

105 self.stage_files() 

106 

107 # haddock3-restraints calc_accessibility 1UBQ.pdb --export_to_actpass 

108 self.cmd = [self.binary_path, "calc_accessibility", self.stage_io_dict['in']['input_pdb_path']] 

109 

110 if self.io_dict["out"]["output_actpass_path"] is not None: 

111 self.cmd.append("--export_to_actpass") 

112 

113 self.cmd.append(f"-c {self.cutoff}") 

114 self.cmd.append(">") 

115 self.cmd.append(self.stage_io_dict['out']['output_accessibility_path']) 

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

117 

118 # Run Biobb block 

119 self.run_biobb() 

120 

121 # Check chain 

122 target_string = f"Chain {self.chain}" 

123 found = False 

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

125 for line in file: 

126 if target_string in line: 

127 found = True 

128 

129 if found: 

130 # Rename/Copy output file to the given output file name 

131 file_name = os.path.basename(self.io_dict['in']['input_pdb_path']) 

132 shutil.copyfile(f"{file_name[:-4]}_passive_{self.chain}.actpass", self.io_dict["out"]["output_actpass_path"]) 

133 if self.pass_to_act: 

134 with open(self.io_dict["out"]["output_actpass_path"], 'r') as file: 

135 lines = file.readlines() 

136 with open(self.io_dict["out"]["output_actpass_path"], 'w') as file: 

137 file.write(lines[1]) 

138 file.write('\n\n') 

139 else: 

140 print(f"\nWARNING: Chain {self.chain} not found in input PDB file. Please check and modify the chain property accordingly.\n") 

141 

142 # Copy files to host 

143 self.copy_to_host() 

144 

145 # Remove temporal files 

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

147 actpass_files = glob.glob('*.actpass') 

148 self.tmp_files.extend(actpass_files) 

149 self.remove_tmp_files() 

150 

151 return self.return_code 

152 

153 

154def haddock3_accessibility( 

155 input_pdb_path: str, 

156 output_accessibility_path: str, 

157 output_actpass_path: Optional[str] = None, 

158 properties: Optional[dict] = None, 

159 **kwargs, 

160) -> int: 

161 """Create :class:`Haddock3Accessibility <biobb_haddock.haddock_restraints.haddock3_accessibility>` class and 

162 execute the :meth:`launch() <biobb_haddock.haddock_restraints.haddock3_accessibility.launch>` method.""" 

163 

164 return Haddock3Accessibility( 

165 input_pdb_path=input_pdb_path, 

166 output_accessibility_path=output_accessibility_path, 

167 output_actpass_path=output_actpass_path, 

168 properties=properties, 

169 **kwargs, 

170 ).launch() 

171 

172 

173haddock3_accessibility.__doc__ = Haddock3Accessibility.__doc__ 

174 

175 

176def main(): 

177 parser = argparse.ArgumentParser( 

178 description="Wrapper of the haddock-restraints Accessibility module.", 

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

180 ) 

181 parser.add_argument( 

182 "-c", 

183 "--config", 

184 required=False, 

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

186 ) 

187 

188 # Specific args of each building block 

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

190 required_args.add_argument("--input_pdb_path", required=True) 

191 required_args.add_argument("--output_accessibility_path", required=True) 

192 parser.add_argument("--output_actpass_path", required=False) 

193 

194 args = parser.parse_args() 

195 config = args.config if args.config else None 

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

197 

198 # Specific call of each building block 

199 haddock3_accessibility( 

200 input_pdb_path=args.input_pdb_path, 

201 output_accessibility_path=args.output_accessibility_path, 

202 output_actpass_path=args.output_actpass_path, 

203 properties=properties, 

204 ) 

205 

206 

207if __name__ == "__main__": 

208 main()