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

49 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 Pdbsplitseg 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 Pdbsplitseg(BiobbObject): 

16 """ 

17 | biobb_pdb_tools Pdbsplitseg 

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

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

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

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

24 properties (dic): 

25 * **binary_path** (*str*) - ("pdb_splitseg") Path to the pdb_splitseg 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_splitseg import biobb_pdb_splitseg 

33 

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

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

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 

56 self.io_dict = { 

57 "in": {"input_file_path": input_file_path}, 

58 "out": {"output_file_path": output_file_path}, 

59 } 

60 

61 self.binary_path = properties.get("binary_path", "pdb_splitseg") 

62 self.properties = properties 

63 self.check_init(properties) 

64 

65 @launchlogger 

66 def launch(self) -> int: 

67 """Execute the :class:`Pdbsplitseg <biobb_pdb_tools.pdb_tools.pdb_splitseg>` object.""" 

68 

69 if self.check_restart(): 

70 return 0 

71 self.stage_files() 

72 

73 self.cmd = [ 

74 "cd", 

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

76 ";", 

77 self.binary_path, 

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

79 ] 

80 

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

82 fu.log( 

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

84 self.out_log, 

85 self.global_log, 

86 ) 

87 self.run_biobb() 

88 

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

90 pdb_files = glob.glob( 

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

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

93 ) 

94 

95 if len(pdb_files) > 1: 

96 output_zip_path = os.path.join( 

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

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

99 ) 

100 fu.log( 

101 "Saving %d pdb segment files in a zip" % len(pdb_files), 

102 self.out_log, 

103 self.global_log, 

104 ) 

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

106 for pdb_file in pdb_files: 

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

108 else: 

109 fu.log( 

110 "The given input file has no segments. Saving the input file into a zip.", 

111 self.out_log, 

112 self.global_log, 

113 ) 

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( 

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

123 ) 

124 pass 

125 

126 self.copy_to_host() 

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

134 input_file_path: str, 

135 output_file_path: str, 

136 properties: Optional[dict] = None, 

137 **kwargs, 

138) -> int: 

139 """Create :class:`Pdbsplitseg <biobb_pdb_tools.pdb_tools.pdb_splitseg>` class and 

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

141 

142 return Pdbsplitseg(**dict(locals())).launch() 

143 

144 

145biobb_pdb_splitseg.__doc__ = Pdbsplitseg.__doc__ 

146main = Pdbsplitseg.get_main(biobb_pdb_splitseg, "Splits a PDB file into several, each containing one segment.") 

147 

148if __name__ == "__main__": 

149 main()