Coverage for biobb_structure_utils/utils/str_check_add_hydrogens.py: 77%

64 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-28 11:54 +0000

1#!/usr/bin/env python3 

2 

3"""Module containing the StrCheckAddHydrogens 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 

12from biobb_structure_utils.utils.common import ( 

13 check_input_path, 

14 check_output_end, 

15 check_output_path_pdbqt, 

16) 

17 

18 

19class StrCheckAddHydrogens(BiobbObject): 

20 """ 

21 | biobb_structure_utils StrCheckAddHydrogens 

22 | This class is a wrapper of the Structure Checking tool to add hydrogens to a 3D structure. 

23 | Wrapper for the `Structure Checking <https://github.com/bioexcel/biobb_structure_checking>`_ tool to add hydrogens to a 3D structure. 

24 

25 Args: 

26 input_structure_path (str): Input structure file path. File type: input. `Sample file <https://github.com/bioexcel/biobb_structure_utils/raw/master/biobb_structure_utils/test/data/utils/str_no_H.pdb>`_. Accepted formats: pdb (edam:format_1476). 

27 output_structure_path (str): Output structure file path. File type: output. `Sample file <https://github.com/bioexcel/biobb_structure_utils/raw/master/biobb_structure_utils/test/reference/utils/ref_str_H.pdbqt>`_. Accepted formats: pdb (edam:format_1476), pdbqt (edam:format_1476). 

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

29 * **charges** (*bool*) - (False) Whether or not to add charges to the output file. If True the output is in PDBQT format. 

30 * **mode** (*string*) - (auto) Selection mode. Values: auto, list, ph 

31 * **ph** (*float*) - (7.4) [0~14|0.1] Add hydrogens appropriate for pH. Only in case mode ph selected. 

32 * **list** (*string*) - ("") List of residues to modify separated by commas (i.e HISA234HID,HISB33HIE). Only in case mode list selected. 

33 * **keep_canonical_resnames** (*bool*) - (False) Whether or not keep canonical residue names 

34 * **binary_path** (*string*) - ("check_structure") path to the check_structure application 

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

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

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

38 

39 Examples: 

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

41 

42 from biobb_structure_utils.utils.str_check_add_hydrogens import str_check_add_hydrogens 

43 prop = { 

44 'charges': False, 

45 'mode': 'auto' 

46 } 

47 str_check_add_hydrogens(input_structure_path='/path/to/myInputStr.pdb', 

48 output_structure_path='/path/to/newStructure.pdb', 

49 properties=prop) 

50 

51 Info: 

52 * wrapped_software: 

53 * name: Structure Checking from MDWeb 

54 * version: >=3.0.3 

55 * license: Apache-2.0 

56 * ontology: 

57 * name: EDAM 

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

59 

60 """ 

61 

62 def __init__( 

63 self, input_structure_path, output_structure_path, properties=None, **kwargs 

64 ) -> None: 

65 properties = properties or {} 

66 

67 # Call parent class constructor 

68 super().__init__(properties) 

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

70 

71 # Input/Output files 

72 self.io_dict = { 

73 "in": {"input_structure_path": input_structure_path}, 

74 "out": {"output_structure_path": output_structure_path}, 

75 } 

76 

77 # Properties specific for BB 

78 self.binary_path = properties.get("binary_path", "check_structure") 

79 self.charges = properties.get("charges", False) 

80 self.mode = properties.get("mode", "auto") 

81 self.ph = properties.get("ph", 7.4) 

82 self.list = properties.get("list", "") 

83 self.keep_canonical_resnames = properties.get("keep_canonical_resnames", False) 

84 self.properties = properties 

85 

86 # Check the properties 

87 self.check_properties(properties) 

88 self.check_arguments() 

89 

90 @launchlogger 

91 def launch(self) -> int: 

92 """Execute the :class:`StrCheckAddHydrogens <utils.str_check_add_hydrogens.StrCheckAddHydrogens>` utils.str_check_add_hydrogens.StrCheckAddHydrogens object.""" 

93 

94 self.io_dict["in"]["input_structure_path"] = check_input_path( 

95 self.io_dict["in"]["input_structure_path"], 

96 self.out_log, 

97 self.__class__.__name__, 

98 ) 

99 self.io_dict["out"]["output_structure_path"] = check_output_path_pdbqt( 

100 self.io_dict["out"]["output_structure_path"], 

101 self.out_log, 

102 self.__class__.__name__, 

103 ) 

104 

105 # Setup Biobb 

106 if self.check_restart(): 

107 return 0 

108 self.stage_files() 

109 

110 self.cmd = [ 

111 self.binary_path, 

112 "-i", 

113 self.stage_io_dict["in"]["input_structure_path"], 

114 "-o", 

115 self.stage_io_dict["out"]["output_structure_path"], 

116 "--non_interactive", 

117 "--force_save", 

118 ] 

119 

120 if self.keep_canonical_resnames: 

121 self.cmd.append("--keep_canonical_resnames") 

122 

123 self.cmd.extend(["command_list", "--list", "'add_hydrogen"]) 

124 

125 if self.charges: 

126 self.cmd.append("--add_charges") 

127 self.cmd.append("ADT") 

128 

129 if self.mode: 

130 self.cmd.extend(["--add_mode", self.mode]) 

131 if self.mode == "ph": 

132 self.cmd.extend(["--pH", self.ph]) 

133 if self.mode == "list": 

134 self.cmd.extend(["--list", self.list]) 

135 else: 

136 self.cmd.extend(["--add_mode", "None"]) 

137 

138 self.cmd.append("'") 

139 # Run Biobb block 

140 self.run_biobb() 

141 

142 # Copy files to host 

143 self.copy_to_host() 

144 

145 check_output_end(self.io_dict["out"]["output_structure_path"], self.out_log) 

146 

147 # Remove temporal files 

148 # self.tmp_files.append(self.stage_io_dict.get("unique_dir", "")) 

149 self.remove_tmp_files() 

150 

151 self.check_arguments(output_files_created=True, raise_exception=False) 

152 

153 return self.return_code 

154 

155 

156def str_check_add_hydrogens( 

157 input_structure_path: str, 

158 output_structure_path: str, 

159 properties: Optional[dict] = None, 

160 **kwargs, 

161) -> int: 

162 """Execute the :class:`StrCheckAddHydrogens <utils.str_check_add_hydrogens.StrCheckAddHydrogens>` class and 

163 execute the :meth:`launch() <utils.str_check_add_hydrogens.StrCheckAddHydrogens.launch>` method.""" 

164 

165 return StrCheckAddHydrogens( 

166 input_structure_path=input_structure_path, 

167 output_structure_path=output_structure_path, 

168 properties=properties, 

169 **kwargs, 

170 ).launch() 

171 

172 str_check_add_hydrogens.__doc__ = StrCheckAddHydrogens.__doc__ 

173 

174 

175def main(): 

176 """Command line execution of this building block. Please check the command line documentation.""" 

177 parser = argparse.ArgumentParser( 

178 description="Class to add hydrogens to a 3D structure.", 

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( 

191 "-i", 

192 "--input_structure_path", 

193 required=True, 

194 help="Input structure file path. Accepted formats: pdb.", 

195 ) 

196 required_args.add_argument( 

197 "-o", 

198 "--output_structure_path", 

199 required=True, 

200 help="Output structure file path. Accepted formats: pdb, pdbqt.", 

201 ) 

202 

203 args = parser.parse_args() 

204 config = args.config if args.config else None 

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

206 

207 # Specific call of each building block 

208 str_check_add_hydrogens( 

209 input_structure_path=args.input_structure_path, 

210 output_structure_path=args.output_structure_path, 

211 properties=properties, 

212 ) 

213 

214 

215if __name__ == "__main__": 

216 main()