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

62 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 Pdbsplitmodel class and the command line interface.""" 

4 

5import argparse 

6import glob 

7import os 

8import zipfile 

9from pathlib import Path 

10from typing import Optional 

11 

12from biobb_common.configuration import settings 

13from biobb_common.generic.biobb_object import BiobbObject 

14from biobb_common.tools import file_utils as fu 

15from biobb_common.tools.file_utils import launchlogger 

16 

17 

18class Pdbsplitmodel(BiobbObject): 

19 """ 

20 | biobb_pdb_tools Pdbsplitmodel 

21 | Splits a PDB file into several, each containing one MODEL. 

22 | This tool splits a PDB file into several, each containing one MODEL. It can be used to split a PDB file into several, each containing one MODEL. 

23 

24 Args: 

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

26 output_file_path (str): ZIP file containing all PDB files splited by protein model. File type: output. `Sample file <https://github.com/bioexcel/biobb_pdb_tools/blob/master/biobb_pdb_tools/test/reference/pdb_tools/ref_pdb_splitmodel.zip>`_. Accepted formats: zip (edam:format_3987). 

27 properties (dic): 

28 * **binary_path** (*str*) - ("pdb_splitmodel") Path to the pdb_splitmodel executable binary. 

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

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

31 

32 Examples: 

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

34 

35 from biobb_pdb_tools.pdb_tools.biobb_pdb_splitmodel import biobb_pdb_splitmodel 

36 

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

38 output_file_path='/path/to/output.zip) 

39 

40 Info: 

41 * wrapped_software: 

42 * name: pdb_tools 

43 * version: >=2.5.0 

44 * license: Apache-2.0 

45 * ontology: 

46 * name: EDAM 

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

48 

49 """ 

50 

51 def __init__( 

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

53 ) -> None: 

54 properties = properties or {} 

55 

56 super().__init__(properties) 

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

58 self.io_dict = { 

59 "in": {"input_file_path": input_file_path}, 

60 "out": {"output_file_path": output_file_path}, 

61 } 

62 

63 self.binary_path = properties.get("binary_path", "pdb_splitmodel") 

64 self.properties = properties 

65 

66 self.check_properties(properties) 

67 self.check_arguments() 

68 

69 @launchlogger 

70 def launch(self) -> int: 

71 """Execute the :class:`Pdbsplitmodel <biobb_pdb_tools.pdb_tools.pdb_splitmodel>` object.""" 

72 

73 if self.check_restart(): 

74 return 0 

75 self.stage_files() 

76 

77 self.cmd = [ 

78 "cd", 

79 self.stage_io_dict.get("unique_dir", ""), 

80 ";", 

81 self.binary_path, 

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

83 ] 

84 

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

86 

87 fu.log( 

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

89 self.out_log, 

90 self.global_log, 

91 ) 

92 self.run_biobb() 

93 

94 stem = Path(self.stage_io_dict["in"]["input_file_path"]).stem 

95 pdb_files = glob.glob( 

96 os.path.join(self.stage_io_dict.get( 

97 "unique_dir", ""), stem + "_*.pdb") 

98 ) 

99 

100 if len(pdb_files) > 1: 

101 output_zip_path = os.path.join( 

102 self.stage_io_dict.get("unique_dir", ""), 

103 self.stage_io_dict["out"]["output_file_path"], 

104 ) 

105 fu.log( 

106 "Saving %d pdb model files in a zip" % len(pdb_files), 

107 self.out_log, 

108 self.global_log, 

109 ) 

110 with zipfile.ZipFile(output_zip_path, "w") as zipf: 

111 for pdb_file in pdb_files: 

112 zipf.write(pdb_file, os.path.basename(pdb_file)) 

113 else: 

114 fu.log("The given input file has no models.", 

115 self.out_log, self.global_log) 

116 output_zip_path = os.path.join( 

117 self.stage_io_dict.get("unique_dir", ""), 

118 self.stage_io_dict["out"]["output_file_path"], 

119 ) 

120 with zipfile.ZipFile(output_zip_path, "w") as zipf: 

121 zipf.write( 

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

123 os.path.basename( 

124 self.stage_io_dict["in"]["input_file_path"]), 

125 ) 

126 pass 

127 

128 self.copy_to_host() 

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

130 self.remove_tmp_files() 

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

132 

133 return self.return_code 

134 

135 

136def biobb_pdb_splitmodel( 

137 input_file_path: str, 

138 output_file_path: str, 

139 properties: Optional[dict] = None, 

140 **kwargs, 

141) -> int: 

142 """Create :class:`Pdbsplitmodel <biobb_pdb_tools.pdb_tools.pdb_splitmodel>` class and 

143 execute the :meth:`launch() <biobb_pdb_tools.pdb_tools.pdb_splitmodel.launch>` method.""" 

144 

145 return Pdbsplitmodel( 

146 input_file_path=input_file_path, 

147 output_file_path=output_file_path, 

148 properties=properties, 

149 **kwargs, 

150 ).launch() 

151 

152 

153biobb_pdb_splitmodel.__doc__ = Pdbsplitmodel.__doc__ 

154 

155 

156def main(): 

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

158 parser = argparse.ArgumentParser( 

159 description="Splits a PDB file into several, each containing one MODEL.", 

160 formatter_class=lambda prog: argparse.RawTextHelpFormatter( 

161 prog, width=99999), 

162 ) 

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

164 

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

166 required_args.add_argument( 

167 "--input_file_path", 

168 required=True, 

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

170 ) 

171 required_args.add_argument( 

172 "--output_file_path", 

173 required=True, 

174 help="Description for the output file path. Accepted formats: zip.", 

175 ) 

176 

177 args = parser.parse_args() 

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

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

180 

181 biobb_pdb_splitmodel( 

182 input_file_path=args.input_file_path, 

183 output_file_path=args.output_file_path, 

184 properties=properties, 

185 ) 

186 

187 

188if __name__ == "__main__": 

189 main()