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

50 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-04 08:26 +0000

1#!/usr/bin/env python3 

2 

3"""Module containing the Pdbsplitmodel class and the command line interface.""" 

4 

5import glob 

6import os 

7import zipfile 

8from pathlib import Path 

9from typing import Optional 

10from biobb_common.generic.biobb_object import BiobbObject 

11from biobb_common.tools import file_utils as fu 

12from biobb_common.tools.file_utils import launchlogger 

13 

14 

15class Pdbsplitmodel(BiobbObject): 

16 """ 

17 | biobb_pdb_tools Pdbsplitmodel 

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

19 | 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. 

20 

21 Args: 

22 input_file_path (str): Input 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). 

23 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). 

24 properties (dic): 

25 * **binary_path** (*str*) - ("pdb_splitmodel") Path to the pdb_splitmodel 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_splitmodel import biobb_pdb_splitmodel 

33 

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

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

36 

37 Info: 

38 * wrapped_software: 

39 * name: pdb_tools 

40 * version: >=2.5.0 

41 * license: Apache-2.0 

42 * ontology: 

43 * name: EDAM 

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

45 

46 """ 

47 

48 def __init__( 

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

50 ) -> None: 

51 properties = properties or {} 

52 

53 super().__init__(properties) 

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

55 self.io_dict = { 

56 "in": {"input_file_path": input_file_path}, 

57 "out": {"output_file_path": output_file_path}, 

58 } 

59 

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

61 self.properties = properties 

62 

63 self.check_properties(properties) 

64 self.check_arguments() 

65 

66 @launchlogger 

67 def launch(self) -> int: 

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

69 

70 if self.check_restart(): 

71 return 0 

72 self.stage_files() 

73 

74 self.cmd = [ 

75 "cd", 

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

77 ";", 

78 self.binary_path, 

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

80 ] 

81 

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

83 

84 fu.log( 

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

86 self.out_log, 

87 self.global_log, 

88 ) 

89 self.run_biobb() 

90 

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

92 pdb_files = glob.glob( 

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

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

95 ) 

96 

97 if len(pdb_files) > 1: 

98 output_zip_path = os.path.join( 

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

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

101 ) 

102 fu.log( 

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

104 self.out_log, 

105 self.global_log, 

106 ) 

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

108 for pdb_file in pdb_files: 

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

110 else: 

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

112 self.out_log, self.global_log) 

113 output_zip_path = os.path.join( 

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

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

116 ) 

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

118 zipf.write( 

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

120 os.path.basename( 

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

122 ) 

123 pass 

124 

125 self.copy_to_host() 

126 self.remove_tmp_files() 

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

128 

129 return self.return_code 

130 

131 

132def biobb_pdb_splitmodel( 

133 input_file_path: str, 

134 output_file_path: str, 

135 properties: Optional[dict] = None, 

136 **kwargs, 

137) -> int: 

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

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

140 

141 return Pdbsplitmodel(**dict(locals())).launch() 

142 

143 

144biobb_pdb_splitmodel.__doc__ = Pdbsplitmodel.__doc__ 

145main = Pdbsplitmodel.get_main(biobb_pdb_splitmodel, "Splits a PDB file into several, each containing one MODEL.") 

146 

147if __name__ == "__main__": 

148 main()