Coverage for biobb_chemistry/ambertools/reduce_remove_hydrogens.py: 96%

49 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-06 12:40 +0000

1#!/usr/bin/env python3 

2 

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

4import os 

5from typing import Optional 

6from biobb_common.generic.biobb_object import BiobbObject 

7from biobb_common.tools import file_utils as fu 

8from biobb_common.tools.file_utils import launchlogger 

9from biobb_chemistry.ambertools.common import get_binary_path, check_input_path, check_output_path 

10 

11 

12class ReduceRemoveHydrogens(BiobbObject): 

13 """ 

14 | biobb_chemistry ReduceRemoveHydrogens 

15 | This class is a wrapper of the `Ambertools <http://ambermd.org/doc12/AmberTools12.pdf>`_ reduce module for removing hydrogens from a given structure. 

16 | Reduce is a program for `adding or removing hydrogens to a Protein DataBank (PDB) molecular structure file <http://ambermd.org/doc12/AmberTools12.pdf>`_. 

17 

18 Args: 

19 input_path (str): Path to the input file. File type: input. `Sample file <https://github.com/bioexcel/biobb_chemistry/raw/master/biobb_chemistry/test/data/ambertools/reduce.H.pdb>`_. Accepted formats: pdb (edam:format_1476). 

20 output_path (str): Path to the output file. File type: output. `Sample file <https://github.com/bioexcel/biobb_chemistry/raw/master/biobb_chemistry/test/reference/ambertools/ref_reduce.remove.pdb>`_. Accepted formats: pdb (edam:format_1476). 

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

22 * **binary_path** (*str*) - ("reduce") Path to the reduce executable binary. 

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

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

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

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

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

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

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

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

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

32 

33 Examples: 

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

35 

36 from biobb_chemistry.ambertools.reduce_remove_hydrogens import reduce_remove_hydrogens 

37 prop = { } 

38 reduce_remove_hydrogens(input_path='/path/to/myStructure.pdb', 

39 output_path='/path/to/newStructure.pdb', 

40 properties=prop) 

41 

42 Info: 

43 * wrapped_software: 

44 * name: AmberTools Reduce 

45 * version: >=20.0 

46 * license: GNU 

47 * ontology: 

48 * name: EDAM 

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

50 

51 """ 

52 

53 def __init__(self, input_path, output_path, 

54 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_path": input_path}, 

64 "out": {"output_path": output_path} 

65 } 

66 

67 # Properties specific for BB 

68 self.binary_path = get_binary_path(properties, 'binary_path') 

69 self.properties = properties 

70 

71 # Check the properties 

72 self.check_properties(properties) 

73 self.check_arguments() 

74 

75 def check_data_params(self, out_log, err_log): 

76 """ Checks all the input/output paths and parameters """ 

77 self.io_dict["in"]["input_path"] = check_input_path(self.io_dict["in"]["input_path"], out_log, self.__class__.__name__) 

78 self.io_dict["out"]["output_path"] = check_output_path(self.io_dict["out"]["output_path"], out_log, self.__class__.__name__) 

79 

80 def create_cmd(self, container_io_dict, out_log, err_log): 

81 """Creates the command line instruction using the properties file settings""" 

82 instructions_list = [] 

83 

84 # executable path 

85 instructions_list.append(self.binary_path) 

86 

87 instructions_list.append('-Trim') 

88 

89 instructions_list.append(container_io_dict["in"]["input_path"]) 

90 instructions_list.append('>') 

91 instructions_list.append(container_io_dict["out"]["output_path"]) 

92 

93 return instructions_list 

94 

95 @launchlogger 

96 def launch(self) -> int: 

97 """Execute the :class:`ReduceRemoveHydrogens <ambertools.reduce_remove_hydrogens.ReduceRemoveHydrogens>` ambertools.reduce_remove_hydrogens.ReduceRemoveHydrogens object.""" 

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 # create command line instruction 

108 self.cmd = self.create_cmd(self.stage_io_dict, self.out_log, self.err_log) 

109 

110 # Run Biobb block 

111 self.run_biobb() 

112 

113 # Copy files to host 

114 self.copy_to_host() 

115 

116 # reduce -Trim always exits with -1 (255): its return code is only set by the 

117 # optimization step, which -Trim skips. Success is given by the output file. 

118 output_path = self.io_dict["out"]["output_path"] 

119 if self.return_code == 255 and os.path.isfile(output_path) and os.path.getsize(output_path) > 0: 

120 fu.log("reduce -Trim exit code 255 ignored: it is always returned when the optimization is skipped", self.out_log, self.global_log) 

121 self.return_code = 0 

122 

123 # remove temporary folder(s) 

124 self.remove_tmp_files() 

125 

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

127 

128 return self.return_code 

129 

130 

131def reduce_remove_hydrogens(input_path: str, output_path: str, properties: Optional[dict] = None, **kwargs) -> int: 

132 """Create the :class:`ReduceRemoveHydrogens <ambertools.reduce_remove_hydrogens.ReduceRemoveHydrogens>` class and 

133 execute the :meth:`launch() <ambertools.reduce_remove_hydrogens.ReduceRemoveHydrogens.launch>` method.""" 

134 return ReduceRemoveHydrogens(**dict(locals())).launch() 

135 

136 

137reduce_remove_hydrogens.__doc__ = ReduceRemoveHydrogens.__doc__ 

138main = ReduceRemoveHydrogens.get_main(reduce_remove_hydrogens, "Removes hydrogen atoms to small molecules.") 

139 

140 

141if __name__ == '__main__': 

142 main()