Coverage for biobb_amber/pdb4amber/pdb4amber_run.py: 74%

62 statements  

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

1#!/usr/bin/env python3 

2 

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

4import argparse 

5from typing import Optional 

6from biobb_common.generic.biobb_object import BiobbObject 

7from biobb_common.configuration import settings 

8from biobb_common.tools import file_utils as fu 

9from biobb_common.tools.file_utils import launchlogger 

10from biobb_amber.pdb4amber.common import check_input_path, check_output_path 

11 

12 

13class Pdb4amberRun(BiobbObject): 

14 """ 

15 | biobb_amber.pdb4amber.pdb4amber_run Pdb4amberRun 

16 | Wrapper of the `AmberTools (AMBER MD Package) pdb4amber tool <https://ambermd.org/AmberTools.php>`_ module. 

17 | Analyse PDB files and clean them for further usage, especially with the LEaP programs of Amber, using pdb4amber tool from the AmberTools MD package. 

18 

19 Args: 

20 input_pdb_path (str): Input 3D structure PDB file. File type: input. `Sample file <https://github.com/bioexcel/biobb_amber/raw/master/biobb_amber/test/data/pdb4amber/1aki_fixed.pdb>`_. Accepted formats: pdb (edam:format_1476). 

21 output_pdb_path (str): Output 3D structure PDB file. File type: output. `Sample file <https://github.com/bioexcel/biobb_amber/raw/master/biobb_amber/test/reference/pdb4amber/structure.pdb4amber.pdb>`_. Accepted formats: pdb (edam:format_1476). 

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

23 * **remove_hydrogens** (*bool*) - (False) Remove hydrogen atoms from the PDB file. 

24 * **remove_waters** (*bool*) - (False) Remove water molecules from the PDB file. 

25 * **constant_pH** (*bool*) - (False) Rename ionizable residues e.g. GLU,ASP,HIS for constant pH simulation. 

26 * **reduce** (*bool*) - (False) Run Reduce first to add hydrogen atoms. 

27 * **binary_path** (*str*) - ("pdb4amber") Path to the pdb4amber 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) Container path definition. 

32 * **container_image** (*str*) - ('afandiadib/ambertools:serial') Container image definition. 

33 * **container_volume_path** (*str*) - ('/tmp') Container volume path definition. 

34 * **container_working_dir** (*str*) - (None) Container working directory definition. 

35 * **container_user_id** (*str*) - (None) Container user_id definition. 

36 * **container_shell_path** (*str*) - ('/bin/bash') Path to default shell inside the container. 

37 

38 Examples: 

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

40 

41 from biobb_amber.pdb4amber.pdb4amber_run import pdb4amber_run 

42 prop = { 

43 'remove_tmp': True 

44 } 

45 pdb4amber_run(input_pdb_path='/path/to/structure.pdb', 

46 output_pdb_path='/path/to/newStructure.pdb', 

47 properties=prop) 

48 

49 Info: 

50 * wrapped_software: 

51 * name: AmberTools pdb4amber 

52 * version: >20.9 

53 * license: LGPL 2.1 

54 * ontology: 

55 * name: EDAM 

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

57 

58 """ 

59 

60 def __init__(self, input_pdb_path: str, output_pdb_path: str, properties: Optional[dict], **kwargs): 

61 

62 properties = properties or {} 

63 

64 # Call parent class constructor 

65 super().__init__(properties) 

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

67 

68 # Input/Output files 

69 self.io_dict = { 

70 'in': {'input_pdb_path': input_pdb_path}, 

71 'out': {'output_pdb_path': output_pdb_path} 

72 } 

73 

74 # Properties specific for BB 

75 self.properties = properties 

76 self.remove_hydrogens = properties.get('remove_hydrogens', False) 

77 self.remove_waters = properties.get('remove_waters', False) 

78 self.constant_pH = properties.get('constant_pH', False) 

79 self.reduce = properties.get('reduce', False) 

80 self.binary_path = properties.get('binary_path', 'pdb4amber') 

81 

82 # Check the properties 

83 self.check_properties(properties) 

84 self.check_arguments() 

85 

86 def check_data_params(self, out_log, err_log): 

87 """ Checks input/output paths correctness """ 

88 

89 # Check input(s) 

90 self.io_dict["in"]["input_pdb_path"] = check_input_path(self.io_dict["in"]["input_pdb_path"], "input_pdb_path", False, out_log, self.__class__.__name__) 

91 

92 # Check output(s) 

93 self.io_dict["out"]["output_pdb_path"] = check_output_path(self.io_dict["out"]["output_pdb_path"], "output_pdb_path", False, out_log, self.__class__.__name__) 

94 

95 @launchlogger 

96 def launch(self): 

97 """Launches the execution of the Pdb4amberRun module.""" 

98 

99 # check input/output paths and parameters 

100 self.check_data_params(self.out_log, self.err_log) 

101 

102 # Setup Biobb 

103 if self.check_restart(): 

104 return 0 

105 self.stage_files() 

106 

107 # Creating temporary folder 

108 self.tmp_folder = fu.create_unique_dir() 

109 fu.log('Creating %s temporary folder' % self.tmp_folder, self.out_log) 

110 

111 # Command line 

112 # sander -O -i mdin/min.mdin -p $1.cpH.prmtop -c ph$i/$1.inpcrd -r ph$i/$1.min.rst7 -o ph$i/$1.min.o 

113 self.cmd = [self.binary_path, 

114 '-i', self.stage_io_dict['in']['input_pdb_path'], 

115 '-o', self.stage_io_dict['out']['output_pdb_path'] 

116 ] 

117 

118 if self.remove_hydrogens: 

119 self.cmd.append("-y ") 

120 if self.remove_waters: 

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

122 if self.constant_pH: 

123 self.cmd.append("--constantph ") 

124 if self.reduce: 

125 self.cmd.append("--reduce ") 

126 

127 # Run Biobb block 

128 self.run_biobb() 

129 

130 # Copy files to host 

131 self.copy_to_host() 

132 

133 # remove temporary folder(s) 

134 self.tmp_files.extend([ 

135 # self.stage_io_dict.get("unique_dir", ""), 

136 str(self.tmp_folder) 

137 ]) 

138 self.remove_tmp_files() 

139 

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

141 

142 return self.return_code 

143 

144 

145def pdb4amber_run(input_pdb_path: str, output_pdb_path: str, 

146 properties: Optional[dict] = None, **kwargs) -> int: 

147 """Create :class:`Pdb4amberRun <pdb4amber.pdb4amber_run.Pdb4amberRun>`pdb4amber.pdb4amber_run.Pdb4amberRun class and 

148 execute :meth:`launch() <pdb4amber.pdb4amber_run.Pdb4amberRun.launch>` method""" 

149 

150 return Pdb4amberRun(input_pdb_path=input_pdb_path, 

151 output_pdb_path=output_pdb_path, 

152 properties=properties).launch() 

153 

154 pdb4amber_run.__doc__ = Pdb4amberRun.__doc__ 

155 

156 

157def main(): 

158 parser = argparse.ArgumentParser(description='Analyse PDB files and clean them for further usage, especially with the LEaP programs of Amber, using pdb4amber tool from the AmberTools MD package.', formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

159 parser.add_argument('--config', required=False, help='Configuration file') 

160 

161 # Specific args 

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

163 required_args.add_argument('--input_pdb_path', required=True, help='Input 3D structure PDB file. Accepted formats: pdb.') 

164 required_args.add_argument('--output_pdb_path', required=True, help='Output 3D structure PDB file. Accepted formats: pdb.') 

165 

166 args = parser.parse_args() 

167 config = args.config if args.config else None 

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

169 

170 # Specific call 

171 pdb4amber_run(input_pdb_path=args.input_pdb_path, 

172 output_pdb_path=args.output_pdb_path, 

173 properties=properties) 

174 

175 

176if __name__ == '__main__': 

177 main()