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

50 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-06 12:37 +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", self.out_log, self.global_log) 

86 

87 self.cmd = [ 

88 self.binary_path, 

89 " ".join(instructions), 

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

91 ">", 

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

93 ] 

94 

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

96 

97 fu.log( 

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

99 self.out_log, 

100 self.global_log, 

101 ) 

102 

103 self.run_biobb() 

104 self.copy_to_host() 

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_fixinsert( 

113 input_file_path: str, 

114 output_file_path: str, 

115 properties: Optional[dict] = None, 

116 **kwargs, 

117) -> int: 

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

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

120 

121 return Pdbfixinsert( 

122 input_file_path=input_file_path, 

123 output_file_path=output_file_path, 

124 properties=properties, 

125 **kwargs, 

126 ).launch() 

127 

128biobb_pdb_fixinsert.__doc__ = Pdbfixinsert.__doc__ 

129 

130 

131def main(): 

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

133 parser = argparse.ArgumentParser( 

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

135 formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999), 

136 ) 

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

138 

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

140 required_args.add_argument( 

141 "--input_file_path", 

142 required=True, 

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

144 ) 

145 required_args.add_argument( 

146 "--output_file_path", 

147 required=True, 

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

149 ) 

150 

151 args = parser.parse_args() 

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

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

154 

155 biobb_pdb_fixinsert( 

156 input_file_path=args.input_file_path, 

157 output_file_path=args.output_file_path, 

158 properties=properties, 

159 ) 

160 

161 

162if __name__ == "__main__": 

163 main()