Coverage for biobb_pdb_tools/pdb_tools/biobb_pdb_tidy.py: 74%

50 statements  

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

1#!/usr/bin/env python3 

2 

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

4 

5import argparse 

6from typing import Optional 

7 

8from biobb_common.configuration import settings 

9from biobb_common.generic.biobb_object import BiobbObject 

10from biobb_common.tools import file_utils as fu 

11from biobb_common.tools.file_utils import launchlogger 

12 

13 

14class Pdbtidy(BiobbObject): 

15 """ 

16 | biobb_pdb_tools Pdbtidy 

17 | Modifies the file to adhere (as much as possible) to the format specifications. 

18 | This tool modifies the file to adhere (as much as possible) to the format specifications. It can be used to fix a PDB file that does not adhere to the format specifications. 

19 

20 Args: 

21 input_file_path (str): PDB file. File type: input. `Sample file <https://github.com/bioexcel/biobb_pdb_tools/blob/master/biobb_pdb_tools/test/data/pdb_tools/1AKI.pdb>`_. Accepted formats: pdb (edam:format_1476). 

22 output_file_path (str): PDB file modified according to the specifications. File type: output. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_pdb_tools/master/biobb_pdb_tools/test/reference/pdb_tools/ref_pdb_tidy.pdb>`_. Accepted formats: pdb (edam:format_1476). 

23 properties (dic): 

24 * **strict** (*bool*) - (False) Does not add TER on chain breaks. 

25 * **binary_path** (*str*) - ("pdb_tidy") Path to the pdb_tidy 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 

29 Examples: 

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

31 

32 from biobb_pdb_tools.pdb_tools.biobb_pdb_tidy import biobb_pdb_tidy 

33 

34 prop = { 

35 'strict': False 

36 } 

37 biobb_pdb_tidy(input_file_path='/path/to/input.pdb', 

38 output_file_path='/path/to/output.pdb', 

39 properties=prop) 

40 

41 Info: 

42 * wrapped_software: 

43 * name: pdb_tools 

44 * version: >=2.5.0 

45 * license: Apache-2.0 

46 * ontology: 

47 * name: EDAM 

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

49 

50 """ 

51 

52 def __init__( 

53 self, input_file_path, output_file_path, properties=None, **kwargs 

54 ) -> None: 

55 properties = properties or {} 

56 

57 super().__init__(properties) 

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

59 

60 self.io_dict = { 

61 "in": {"input_file_path": input_file_path}, 

62 "out": {"output_file_path": output_file_path}, 

63 } 

64 

65 self.binary_path = properties.get("binary_path", "pdb_tidy") 

66 self.strict = properties.get("strict", False) 

67 self.properties = properties 

68 

69 self.check_properties(properties) 

70 self.check_arguments() 

71 

72 @launchlogger 

73 def launch(self) -> int: 

74 """Execute the :class:`Pdbtidy <biobb_pdb_tools.pdb_tools.pdb_tidy>` object.""" 

75 

76 if self.check_restart(): 

77 return 0 

78 self.stage_files() 

79 

80 instructions = [] 

81 if self.strict: 

82 instructions.append("-strict") 

83 fu.log("Appending optional boolean property", 

84 self.out_log, self.global_log) 

85 

86 self.cmd = [ 

87 self.binary_path, 

88 " ".join(instructions), 

89 self.io_dict["in"]["input_file_path"], 

90 ">", 

91 self.io_dict["out"]["output_file_path"], 

92 ] 

93 

94 fu.log(" ".join(self.cmd), self.out_log, self.global_log) 

95 

96 fu.log( 

97 "Creating command line with instructions and required arguments", 

98 self.out_log, 

99 self.global_log, 

100 ) 

101 

102 self.run_biobb() 

103 self.copy_to_host() 

104 

105 self.tmp_files.extend([self.stage_io_dict.get("unique_dir", "")]) 

106 self.remove_tmp_files() 

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

108 

109 return self.return_code 

110 

111 

112def biobb_pdb_tidy( 

113 input_file_path: str, 

114 output_file_path: str, 

115 properties: Optional[dict] = None, 

116 **kwargs, 

117) -> int: 

118 """Create :class:`Pdbtidy <biobb_pdb_tools.pdb_tools.pdb_tidy>` class and 

119 execute the :meth:`launch() <biobb_pdb_tools.pdb_tools.pdb_tidy.launch>` method.""" 

120 

121 return Pdbtidy( 

122 input_file_path=input_file_path, 

123 output_file_path=output_file_path, 

124 properties=properties, 

125 **kwargs, 

126 ).launch() 

127 

128 

129biobb_pdb_tidy.__doc__ = Pdbtidy.__doc__ 

130 

131 

132def main(): 

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

134 parser = argparse.ArgumentParser( 

135 description="Modifies the file to adhere (as much as possible) to the format specifications.", 

136 formatter_class=lambda prog: argparse.RawTextHelpFormatter( 

137 prog, width=99999), 

138 ) 

139 parser.add_argument("--config", required=True, help="Configuration file") 

140 

141 required_args = parser.add_argument_group("required arguments") 

142 required_args.add_argument( 

143 "--input_file_path", 

144 required=True, 

145 help="Description for the first input file path. Accepted formats: pdb.", 

146 ) 

147 required_args.add_argument( 

148 "--output_file_path", 

149 required=True, 

150 help="Description for the output file path. Accepted formats: pdb.", 

151 ) 

152 

153 args = parser.parse_args() 

154 args.config = args.config or "{}" 

155 properties = settings.ConfReader(config=args.config).get_prop_dic() 

156 

157 biobb_pdb_tidy( 

158 input_file_path=args.input_file_path, 

159 output_file_path=args.output_file_path, 

160 properties=properties, 

161 ) 

162 

163 

164if __name__ == "__main__": 

165 main()