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

63 statements  

« prev     ^ index     » next       coverage.py v7.5.3, created at 2024-06-14 19:03 +0000

1#!/usr/bin/env python3 

2 

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

4import argparse 

5from biobb_common.configuration import settings 

6from biobb_common.generic.biobb_object import BiobbObject 

7from biobb_common.tools.file_utils import launchlogger 

8from biobb_structure_utils.utils.common import check_input_path, check_output_path_pdbqt, check_output_end 

9 

10 

11class StrCheckAddHydrogens(BiobbObject): 

12 """ 

13 | biobb_structure_utils StrCheckAddHydrogens 

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

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

16 

17 Args: 

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

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

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

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

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

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

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

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

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

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

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

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

30 

31 Examples: 

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

33 

34 from biobb_structure_utils.utils.str_check_add_hydrogens import str_check_add_hydrogens 

35 prop = { 

36 'charges': False, 

37 'mode': 'auto' 

38 } 

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

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

41 properties=prop) 

42 

43 Info: 

44 * wrapped_software: 

45 * name: Structure Checking from MDWeb 

46 * version: >=3.0.3 

47 * license: Apache-2.0 

48 * ontology: 

49 * name: EDAM 

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

51 

52 """ 

53 

54 def __init__(self, input_structure_path, output_structure_path, properties=None, **kwargs) -> None: 

55 properties = properties or {} 

56 

57 # Call parent class constructor 

58 super().__init__(properties) 

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

60 

61 # Input/Output files 

62 self.io_dict = { 

63 "in": {"input_structure_path": input_structure_path}, 

64 "out": {"output_structure_path": output_structure_path} 

65 } 

66 

67 # Properties specific for BB 

68 self.binary_path = properties.get('binary_path', 'check_structure') 

69 self.charges = properties.get('charges', False) 

70 self.mode = properties.get('mode', 'auto') 

71 self.ph = properties.get('ph', 7.4) 

72 self.list = properties.get('list', '') 

73 self.keep_canonical_resnames = properties.get('keep_canonical_resnames', False) 

74 self.properties = properties 

75 

76 # Check the properties 

77 self.check_properties(properties) 

78 self.check_arguments() 

79 

80 @launchlogger 

81 def launch(self) -> int: 

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

83 

84 self.io_dict['in']['input_structure_path'] = check_input_path(self.io_dict['in']['input_structure_path'], 

85 self.out_log, self.__class__.__name__) 

86 self.io_dict['out']['output_structure_path'] = check_output_path_pdbqt(self.io_dict['out']['output_structure_path'], 

87 self.out_log, self.__class__.__name__) 

88 

89 # Setup Biobb 

90 if self.check_restart(): 

91 return 0 

92 self.stage_files() 

93 

94 self.cmd = [self.binary_path, 

95 '-i', self.stage_io_dict['in']['input_structure_path'], 

96 '-o', self.stage_io_dict['out']['output_structure_path'], 

97 '--non_interactive', 

98 '--force_save'] 

99 

100 if self.keep_canonical_resnames: 

101 self.cmd.append('--keep_canonical_resnames') 

102 

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

104 

105 if self.charges: 

106 self.cmd.append('--add_charges') 

107 self.cmd.append('ADT') 

108 

109 if self.mode: 

110 self.cmd.extend(['--add_mode', self.mode]) 

111 if self.mode == 'ph': 

112 self.cmd.extend(['--pH', self.ph]) 

113 if self.mode == 'list': 

114 self.cmd.extend(['--list', self.list]) 

115 else: 

116 self.cmd.extend(['--add_mode', 'None']) 

117 

118 self.cmd.append("'") 

119 # Run Biobb block 

120 self.run_biobb() 

121 

122 # Copy files to host 

123 self.copy_to_host() 

124 

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

126 

127 # Remove temporal files 

128 self.tmp_files.append(self.stage_io_dict.get("unique_dir")) 

129 self.remove_tmp_files() 

130 

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

132 

133 return self.return_code 

134 

135 

136def str_check_add_hydrogens(input_structure_path: str, output_structure_path: str, properties: dict = None, **kwargs) -> int: 

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

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

139 

140 return StrCheckAddHydrogens(input_structure_path=input_structure_path, 

141 output_structure_path=output_structure_path, 

142 properties=properties, **kwargs).launch() 

143 

144 

145def main(): 

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

147 parser = argparse.ArgumentParser(description="Class to add hydrogens to a 3D structure.", formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

148 parser.add_argument('-c', '--config', required=False, help="This file can be a YAML file, JSON file or JSON string") 

149 

150 # Specific args of each building block 

151 required_args = parser.add_argument_group('required arguments') 

152 required_args.add_argument('-i', '--input_structure_path', required=True, help="Input structure file path. Accepted formats: pdb.") 

153 required_args.add_argument('-o', '--output_structure_path', required=True, help="Output structure file path. Accepted formats: pdb, pdbqt.") 

154 

155 args = parser.parse_args() 

156 config = args.config if args.config else None 

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

158 

159 # Specific call of each building block 

160 str_check_add_hydrogens(input_structure_path=args.input_structure_path, 

161 output_structure_path=args.output_structure_path, 

162 properties=properties) 

163 

164 

165if __name__ == '__main__': 

166 main()