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

62 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 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("unique_dir", ""), stem + "_*.pdb") 

97 ) 

98 

99 if len(pdb_files) > 1: 

100 output_zip_path = os.path.join( 

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

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

103 ) 

104 fu.log( 

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

106 self.out_log, 

107 self.global_log, 

108 ) 

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

110 for pdb_file in pdb_files: 

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

112 else: 

113 fu.log("The given input file has no models.", self.out_log, self.global_log) 

114 output_zip_path = os.path.join( 

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

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

117 ) 

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

119 zipf.write( 

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

121 os.path.basename(self.stage_io_dict["in"]["input_file_path"]), 

122 ) 

123 pass 

124 

125 self.copy_to_host() 

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

127 self.remove_tmp_files() 

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

129 

130 return self.return_code 

131 

132 

133def biobb_pdb_splitmodel( 

134 input_file_path: str, 

135 output_file_path: str, 

136 properties: Optional[dict] = None, 

137 **kwargs, 

138) -> int: 

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

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

141 

142 return Pdbsplitmodel( 

143 input_file_path=input_file_path, 

144 output_file_path=output_file_path, 

145 properties=properties, 

146 **kwargs, 

147 ).launch() 

148 

149biobb_pdb_splitmodel.__doc__ = Pdbsplitmodel.__doc__ 

150 

151 

152def main(): 

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

154 parser = argparse.ArgumentParser( 

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

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

157 ) 

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

159 

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

161 required_args.add_argument( 

162 "--input_file_path", 

163 required=True, 

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

165 ) 

166 required_args.add_argument( 

167 "--output_file_path", 

168 required=True, 

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

170 ) 

171 

172 args = parser.parse_args() 

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

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

175 

176 biobb_pdb_splitmodel( 

177 input_file_path=args.input_file_path, 

178 output_file_path=args.output_file_path, 

179 properties=properties, 

180 ) 

181 

182 

183if __name__ == "__main__": 

184 main()