Coverage for biobb_pdb_tools/pdb_tools/biobb_pdb_fixinsert.py: 78%

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 Pdbfixinsert 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 

14# 1. Rename class as required 

15class Pdbfixinsert(BiobbObject): 

16 """ 

17 | biobb_pdb_tools Pdbfixinsert 

18 | Deletes insertion codes and shifts the residue numbering of downstream residues. 

19 | Works by deleting an insertion code and shifting the residue numbering of downstream residues. Allows for picking specific residues to delete insertion codes for. 

20 

21 Args: 

22 input_file_path (str): PDB file. File type: input. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_pdb_tools/master/biobb_pdb_tools/test/data/pdb_tools/1IGY.pdb>`_. Accepted formats: pdb (edam:format_1476). 

23 output_file_path (str): PDB file with fixed insertion codes. File type: output. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_pdb_tools/master/biobb_pdb_tools/test/reference/pdb_tools/ref_pdb_fixinsert.pdb>`_. Accepted formats: pdb (edam:format_1476). 

24 properties (dic): 

25 * **residues** (*string*) - (None) Specific residues to delete insertion codes for, format: "A9,B12" (chain and residue number). 

26 * **binary_path** (*str*) - ("pdb_fixinsert") Path to the pdb_fixinsert executable binary. 

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

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

29 

30 Examples: 

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

32 

33 from biobb_pdb_tools.pdb_tools.biobb_pdb_fixinsert import biobb_pdb_fixinsert 

34 

35 # Delete specific insertion codes 

36 prop = { 

37 'residues': 'A9,B12' 

38 } 

39 biobb_pdb_fixinsert(input_file_path='/path/to/input.pdb', 

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

41 properties=prop) 

42 

43 Info: 

44 * wrapped_software: 

45 * name: pdb_tools 

46 * version: >=2.5.0 

47 * license: Apache-2.0 

48 * ontology: 

49 * name: EDAM 

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

51 

52 """ 

53 

54 def __init__( 

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

56 ) -> None: 

57 properties = properties or {} 

58 

59 super().__init__(properties) 

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

61 

62 self.io_dict = { 

63 "in": {"input_file_path": input_file_path}, 

64 "out": {"output_file_path": output_file_path}, 

65 } 

66 

67 self.binary_path = properties.get("binary_path", "pdb_fixinsert") 

68 self.residues = properties.get("residues", None) 

69 self.properties = properties 

70 

71 self.check_properties(properties) 

72 self.check_arguments() 

73 

74 @launchlogger 

75 def launch(self) -> int: 

76 """Execute the :class:`Pdbfixinsert <biobb_pdb_tools.pdb_tools.pdb_fixinsert>` object.""" 

77 

78 if self.check_restart(): 

79 return 0 

80 self.stage_files() 

81 

82 instructions = [] 

83 if self.residues: 

84 instructions.append("-" + str(self.residues)) 

85 fu.log("Appending specific residues to delete insertion codes for", 

86 self.out_log, self.global_log) 

87 

88 self.cmd = [ 

89 self.binary_path, 

90 " ".join(instructions), 

91 self.stage_io_dict["in"]["input_file_path"], 

92 ">", 

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

94 ] 

95 

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

97 

98 fu.log( 

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

100 self.out_log, 

101 self.global_log, 

102 ) 

103 

104 self.run_biobb() 

105 self.copy_to_host() 

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

107 self.remove_tmp_files() 

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

109 

110 return self.return_code 

111 

112 

113def biobb_pdb_fixinsert( 

114 input_file_path: str, 

115 output_file_path: str, 

116 properties: Optional[dict] = None, 

117 **kwargs, 

118) -> int: 

119 """Create :class:`Pdbfixinsert <biobb_pdb_tools.pdb_tools.pdb_fixinsert>` class and 

120 execute the :meth:`launch() <biobb_pdb_tools.pdb_tools.pdb_fixinsert.launch>` method.""" 

121 

122 return Pdbfixinsert( 

123 input_file_path=input_file_path, 

124 output_file_path=output_file_path, 

125 properties=properties, 

126 **kwargs, 

127 ).launch() 

128 

129 

130biobb_pdb_fixinsert.__doc__ = Pdbfixinsert.__doc__ 

131 

132 

133def main(): 

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

135 parser = argparse.ArgumentParser( 

136 description="Deletes insertion codes and shifts the residue numbering of downstream residues.", 

137 formatter_class=lambda prog: argparse.RawTextHelpFormatter( 

138 prog, width=99999), 

139 ) 

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

141 

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

143 required_args.add_argument( 

144 "--input_file_path", 

145 required=True, 

146 help="PDB file. Accepted formats: pdb.", 

147 ) 

148 required_args.add_argument( 

149 "--output_file_path", 

150 required=True, 

151 help="PDB file with fixed insertion codes. Accepted formats: pdb.", 

152 ) 

153 

154 args = parser.parse_args() 

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

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

157 

158 biobb_pdb_fixinsert( 

159 input_file_path=args.input_file_path, 

160 output_file_path=args.output_file_path, 

161 properties=properties, 

162 ) 

163 

164 

165if __name__ == "__main__": 

166 main()