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

33 statements  

« prev     ^ index     » next       coverage.py v7.18.0, created at 2026-12-02 09:19 +0000

1#!/usr/bin/env python3 

2 

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

4import logging 

5import os 

6import subprocess 

7from typing import Optional 

8from biobb_common.generic.biobb_object import BiobbObject, launcher 

9from biobb_common.tools import file_utils as fu 

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_user_id** (*str*) - (None) Container user_id definition. 

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

31 

32 Examples: 

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

34 

35 from biobb_chemistry.ambertools.reduce_remove_hydrogens import reduce_remove_hydrogens 

36 prop = { } 

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

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

39 properties=prop) 

40 

41 Info: 

42 * wrapped_software: 

43 * name: AmberTools Reduce 

44 * version: >=20.0 

45 * license: GNU 

46 * ontology: 

47 * name: EDAM 

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

49 

50 """ 

51 

52 def __init__(self, input_path: str, output_path: str, 

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

54 super().__init__(**locals()) 

55 

56 # Properties specific for BB 

57 

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

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

60 instructions_list = [] 

61 

62 # executable path 

63 instructions_list.append(self.binary_path) 

64 

65 instructions_list.append('-Trim') 

66 

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

68 instructions_list.append('>') 

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

70 

71 return instructions_list 

72 

73 def run(self) -> None: 

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

75 

76 # create command line instruction 

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

78 

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

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

81 # raise_error is applied here, after that check, and not inside run_biobb. 

82 raise_error, self.raise_error = self.raise_error, False 

83 try: 

84 # Run Biobb block 

85 self.run_biobb() 

86 finally: 

87 self.raise_error = raise_error 

88 

89 output_path = self.sandbox_file("output_path") 

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

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

92 self.return_code = 0 

93 

94 if self.raise_error and self.return_code != 0: 

95 fu.log(f"Command failed with exit code {self.return_code}, raising an exception (raise_error is enabled)", 

96 self.out_log, self.global_log, level=logging.ERROR) 

97 raise subprocess.CalledProcessError(self.return_code, " ".join(self.cmd)) 

98 

99 

100reduce_remove_hydrogens = launcher(ReduceRemoveHydrogens) 

101 

102 

103if __name__ == '__main__': 

104 ReduceRemoveHydrogens.main()