Coverage for biobb_pdb_tools/pdb_tools/biobb_pdb_keepcoord.py: 76%

45 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 Pdbkeepcoord 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 Pdbkeepcoord(BiobbObject): 

16 """ 

17 | biobb_pdb_tools Pdbkeepcoord 

18 | Removes all non-coordinate records from the file. 

19 | Keeps only MODEL, ENDMDL, END, ATOM, HETATM, CONECT records from a PDB file. 

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/1AKI.pdb>`_. Accepted formats: pdb (edam:format_1476). 

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

24 properties (dic): 

25 * **binary_path** (*str*) - ("pdb_keepcoord") Path to the pdb_keepcoord 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_keepcoord import biobb_pdb_keepcoord 

33 

34 # Keep only coordinate records 

35 biobb_pdb_keepcoord(input_file_path='/path/to/input.pdb', 

36 output_file_path='/path/to/output.pdb') 

37 

38 Info: 

39 * wrapped_software: 

40 * name: pdb_tools 

41 * version: >=2.5.0 

42 * license: Apache-2.0 

43 * ontology: 

44 * name: EDAM 

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

46 

47 """ 

48 

49 def __init__( 

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

51 ) -> None: 

52 properties = properties or {} 

53 

54 super().__init__(properties) 

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

56 

57 self.io_dict = { 

58 "in": {"input_file_path": input_file_path}, 

59 "out": {"output_file_path": output_file_path}, 

60 } 

61 

62 self.binary_path = properties.get("binary_path", "pdb_keepcoord") 

63 self.properties = properties 

64 

65 self.check_properties(properties) 

66 self.check_arguments() 

67 

68 @launchlogger 

69 def launch(self) -> int: 

70 """Execute the :class:`Pdbkeepcoord <biobb_pdb_tools.pdb_tools.pdb_keepcoord>` object.""" 

71 

72 if self.check_restart(): 

73 return 0 

74 self.stage_files() 

75 

76 self.cmd = [ 

77 self.binary_path, 

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

79 ">", 

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

81 ] 

82 

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

84 

85 fu.log( 

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

87 self.out_log, 

88 self.global_log, 

89 ) 

90 

91 self.run_biobb() 

92 self.copy_to_host() 

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

94 self.remove_tmp_files() 

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

96 

97 return self.return_code 

98 

99 

100def biobb_pdb_keepcoord( 

101 input_file_path: str, 

102 output_file_path: str, 

103 properties: Optional[dict] = None, 

104 **kwargs, 

105) -> int: 

106 """Create :class:`Pdbkeepcoord <biobb_pdb_tools.pdb_tools.pdb_keepcoord>` class and 

107 execute the :meth:`launch() <biobb_pdb_tools.pdb_tools.pdb_keepcoord.launch>` method.""" 

108 

109 return Pdbkeepcoord( 

110 input_file_path=input_file_path, 

111 output_file_path=output_file_path, 

112 properties=properties, 

113 **kwargs, 

114 ).launch() 

115 

116 

117biobb_pdb_keepcoord.__doc__ = Pdbkeepcoord.__doc__ 

118 

119 

120def main(): 

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

122 parser = argparse.ArgumentParser( 

123 description="Removes all non-coordinate records from the file. Keeps only MODEL, ENDMDL, END, ATOM, HETATM, CONECT records.", 

124 formatter_class=lambda prog: argparse.RawTextHelpFormatter( 

125 prog, width=99999), 

126 ) 

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

128 

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

130 required_args.add_argument( 

131 "--input_file_path", 

132 required=True, 

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

134 ) 

135 required_args.add_argument( 

136 "--output_file_path", 

137 required=True, 

138 help="PDB file with only coordinate records. Accepted formats: pdb.", 

139 ) 

140 

141 args = parser.parse_args() 

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

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

144 

145 biobb_pdb_keepcoord( 

146 input_file_path=args.input_file_path, 

147 output_file_path=args.output_file_path, 

148 properties=properties, 

149 ) 

150 

151 

152if __name__ == "__main__": 

153 main()