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

51 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2025-12-15 15:57 +0000

1#!/usr/bin/env python3 

2 

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

4from typing import Optional 

5from biobb_common.generic.biobb_object import BiobbObject 

6from biobb_common.tools import file_utils as fu 

7from biobb_common.tools.file_utils import launchlogger 

8from biobb_amber.pdb4amber.common import check_input_path, check_output_path 

9 

10 

11class Pdb4amberRun(BiobbObject): 

12 """ 

13 | biobb_amber.pdb4amber.pdb4amber_run Pdb4amberRun 

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

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

16 

17 Args: 

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

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

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

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

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

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

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

25 * **binary_path** (*str*) - ("pdb4amber") Path to the pdb4amber executable binary. 

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

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

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

29 * **container_path** (*str*) - (None) Container path definition. 

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

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

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

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

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

35 

36 Examples: 

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

38 

39 from biobb_amber.pdb4amber.pdb4amber_run import pdb4amber_run 

40 prop = { 

41 'remove_tmp': True 

42 } 

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

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

45 properties=prop) 

46 

47 Info: 

48 * wrapped_software: 

49 * name: AmberTools pdb4amber 

50 * version: >20.9 

51 * license: LGPL 2.1 

52 * ontology: 

53 * name: EDAM 

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

55 

56 """ 

57 

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

59 

60 properties = properties or {} 

61 

62 # Call parent class constructor 

63 super().__init__(properties) 

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

65 

66 # Input/Output files 

67 self.io_dict = { 

68 'in': {'input_pdb_path': input_pdb_path}, 

69 'out': {'output_pdb_path': output_pdb_path} 

70 } 

71 

72 # Properties specific for BB 

73 self.properties = properties 

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

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

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

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

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

79 

80 # Check the properties 

81 self.check_properties(properties) 

82 self.check_arguments() 

83 

84 def check_data_params(self, out_log, err_log): 

85 """ Checks input/output paths correctness """ 

86 

87 # Check input(s) 

88 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__) 

89 

90 # Check output(s) 

91 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__) 

92 

93 @launchlogger 

94 def launch(self): 

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

96 

97 # check input/output paths and parameters 

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

99 

100 # Setup Biobb 

101 if self.check_restart(): 

102 return 0 

103 self.stage_files() 

104 

105 # Creating temporary folder 

106 self.tmp_folder = fu.create_unique_dir() 

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

108 

109 # Command line 

110 # 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 

111 self.cmd = [self.binary_path, 

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

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

114 ] 

115 

116 if self.remove_hydrogens: 

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

118 if self.remove_waters: 

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

120 if self.constant_pH: 

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

122 if self.reduce: 

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

124 

125 # Run Biobb block 

126 self.run_biobb() 

127 

128 # Copy files to host 

129 self.copy_to_host() 

130 

131 # remove temporary folder(s) 

132 self.tmp_files.extend([str(self.tmp_folder)]) 

133 self.remove_tmp_files() 

134 

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

136 

137 return self.return_code 

138 

139 

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

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

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

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

144 return Pdb4amberRun(**dict(locals())).launch() 

145 

146 

147pdb4amber_run.__doc__ = Pdb4amberRun.__doc__ 

148 

149main = Pdb4amberRun.get_main(pdb4amber_run, "Analyse PDB files and clean them for further usage, especially with the LEaP programs of Amber, using pdb4amber tool from the AmberTools MD package.") 

150 

151if __name__ == '__main__': 

152 main()