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

57 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-03 15:55 +0000

1#!/usr/bin/env python3 

2 

3"""Module containing the haddock class and the command line interface.""" 

4 

5import glob 

6import os 

7import shutil 

8from typing import Optional 

9 

10from biobb_common.generic.biobb_object import BiobbObject 

11from biobb_common.tools.file_utils import launchlogger 

12 

13 

14class Haddock3Accessibility(BiobbObject): 

15 """ 

16 | biobb_haddock Haddock3Accessibility 

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

18 | `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. 

19 

20 Args: 

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

39 

40 

41 Examples: 

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

43 

44 from biobb_haddock.haddock_restraints.haddock3_accessibility import haddock3_accessibility 

45 prop = { 'cutoff': 0.4 } 

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

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

48 properties=prop) 

49 

50 Info: 

51 * wrapped_software: 

52 * name: Haddock3-restraints 

53 * version: 2025.5 

54 * license: Apache-2.0 

55 * ontology: 

56 * name: EDAM 

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

58 """ 

59 

60 def __init__( 

61 self, 

62 input_pdb_path: str, 

63 output_accessibility_path: str, 

64 output_actpass_path: Optional[str] = None, 

65 properties: Optional[dict] = None, 

66 **kwargs, 

67 ) -> None: 

68 properties = properties or {} 

69 

70 # Call parent class constructor 

71 super().__init__(properties) 

72 self.locals_var_dict = locals().copy() 

73 

74 # Input/Output files 

75 self.io_dict = { 

76 "in": { 

77 "input_pdb_path": input_pdb_path, 

78 }, 

79 "out": { 

80 "output_accessibility_path": output_accessibility_path, 

81 "output_actpass_path": output_actpass_path, 

82 }, 

83 } 

84 

85 # Properties specific for BB 

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

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

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

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

90 

91 # Properties specific for BB 

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

93 

94 # Check the properties 

95 self.check_init(properties) 

96 

97 @launchlogger 

98 def launch(self) -> int: 

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

100 

101 # Setup Biobb 

102 if self.check_restart(): 

103 return 0 

104 self.stage_files() 

105 

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

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

108 

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

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

111 

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

113 self.cmd.append(">") 

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

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

116 

117 # Run Biobb block 

118 self.run_biobb() 

119 

120 # Check chain 

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

122 found = False 

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

124 for line in file: 

125 if target_string in line: 

126 found = True 

127 

128 if found: 

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

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

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

132 if self.pass_to_act: 

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

134 lines = file.readlines() 

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

136 file.write(lines[1]) 

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

138 else: 

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

140 

141 # Copy files to host 

142 self.copy_to_host() 

143 

144 # Remove temporal files 

145 self.tmp_files.extend(glob.glob('*.actpass')) 

146 self.remove_tmp_files() 

147 

148 return self.return_code 

149 

150 

151def haddock3_accessibility( 

152 input_pdb_path: str, 

153 output_accessibility_path: str, 

154 output_actpass_path: Optional[str] = None, 

155 properties: Optional[dict] = None, 

156 **kwargs, 

157) -> int: 

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

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

160 return Haddock3Accessibility(**dict(locals())).launch() 

161 

162 

163haddock3_accessibility.__doc__ = Haddock3Accessibility.__doc__ 

164main = Haddock3Accessibility.get_main(haddock3_accessibility, "Wrapper of the Haddock-Restraints Accessibility module.") 

165 

166 

167if __name__ == "__main__": 

168 main()