Coverage for biobb_pdb_tools/pdb_tools/biobb_pdb_merge.py: 80%

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

4 

5import argparse 

6import os 

7import zipfile 

8from pathlib import Path 

9from typing import Optional 

10 

11from biobb_common.configuration import settings 

12from biobb_common.generic.biobb_object import BiobbObject 

13from biobb_common.tools import file_utils as fu 

14from biobb_common.tools.file_utils import launchlogger 

15 

16 

17class Pdbmerge(BiobbObject): 

18 """ 

19 | biobb_pdb_tools Pdbmerge 

20 | Merges several PDB files into one. 

21 | This tool merges several PDB files into one. It can be used to merge several PDB files into one. 

22 

23 Args: 

24 input_file_path (str): ZIP file of selected protein. File type: input. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_pdb_tools/master/biobb_pdb_tools/test/data/pdb_tools/input_pdb_merge.pdb>`_. Accepted formats: zip (edam:format_3987). 

25 output_file_path (str): PDB file with input PDBs merged. File type: output. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_pdb_tools/master/biobb_pdb_tools/test/reference/pdb_tools/ref_pdb_merge.pdb>`_. Accepted formats: pdb (edam:format_1476). 

26 properties (dic): 

27 * **binary_path** (*str*) - ("pdb_merge") Path to the pdb_merge executable binary. 

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

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

30 

31 Examples: 

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

33 

34 from biobb_pdb_tools.pdb_tools.biobb_pdb_merge import biobb_pdb_merge 

35 

36 biobb_pdb_merge(input_file_path='/path/to/input1.zip', 

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

38 

39 Info: 

40 * wrapped_software: 

41 * name: pdb_tools 

42 * version: >=2.5.0 

43 * license: Apache-2.0 

44 * ontology: 

45 * name: EDAM 

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

47 

48 """ 

49 

50 def __init__( 

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

52 ) -> None: 

53 properties = properties or {} 

54 

55 super().__init__(properties) 

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

57 

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_merge") 

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:`Pdbmerge <biobb_pdb_tools.pdb_tools.pdb_merge>` object.""" 

72 

73 if self.check_restart(): 

74 return 0 

75 self.stage_files() 

76 

77 input_file_path = self.stage_io_dict["in"]["input_file_path"] 

78 folder_path = os.path.dirname(input_file_path) 

79 

80 if zipfile.is_zipfile(input_file_path): 

81 with zipfile.ZipFile(input_file_path, "r") as zip_ref: 

82 zip_ref.extractall(folder_path) 

83 

84 pdb_files = [ 

85 file 

86 for file in os.listdir(folder_path) 

87 if file.lower().endswith(".pdb") 

88 ] 

89 

90 input_file_list = [os.path.join( 

91 folder_path, file) for file in pdb_files] 

92 

93 input_file_list = [Path(i) for i in input_file_list] 

94 input_file_list = sorted( 

95 input_file_list, key=lambda i: i.stem.upper()) 

96 input_file_list = [str(i) for i in input_file_list] 

97 

98 self.cmd = [ 

99 self.binary_path, 

100 *input_file_list, 

101 ">", 

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

103 ] 

104 

105 else: 

106 fu.log( 

107 f"The archive {input_file_path} is not a ZIP!", 

108 self.out_log, 

109 self.global_log, 

110 ) 

111 

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

113 

114 fu.log( 

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

116 self.out_log, 

117 self.global_log, 

118 ) 

119 

120 self.run_biobb() 

121 self.copy_to_host() 

122 

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

124 self.remove_tmp_files() 

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

126 

127 return self.return_code 

128 

129 

130def biobb_pdb_merge( 

131 input_file_path: str, 

132 output_file_path: str, 

133 properties: Optional[dict] = None, 

134 **kwargs, 

135) -> int: 

136 """Create :class:`Pdbmerge <biobb_pdb_tools.pdb_tools.pdb_merge>` class and 

137 execute the :meth:`launch() <biobb_pdb_tools.pdb_tools.pdb_merge.launch>` method.""" 

138 return Pdbmerge( 

139 input_file_path=input_file_path, 

140 output_file_path=output_file_path, 

141 properties=properties, 

142 **kwargs, 

143 ).launch() 

144 

145 

146biobb_pdb_merge.__doc__ = Pdbmerge.__doc__ 

147 

148 

149def main(): 

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

151 parser = argparse.ArgumentParser( 

152 description="Merges several PDB files into one.", 

153 formatter_class=lambda prog: argparse.RawTextHelpFormatter( 

154 prog, width=99999), 

155 ) 

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

157 

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

159 required_args.add_argument( 

160 "--input_file_path", 

161 required=True, 

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

163 ) 

164 required_args.add_argument( 

165 "--output_file_path", 

166 required=True, 

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

168 ) 

169 

170 args = parser.parse_args() 

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

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

173 biobb_pdb_merge( 

174 input_file_path=args.input_file_path, 

175 output_file_path=args.output_file_path, 

176 properties=properties, 

177 ) 

178 

179 

180if __name__ == "__main__": 

181 main()