Coverage for biobb_pdb_tools/pdb_tools/biobb_pdb_mkensemble.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 Mkensemble 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 Mkensemble(BiobbObject): 

18 """ 

19 | biobb_pdb_tools Mkensemble 

20 | Merges several PDB files into one multi-model (ensemble) file. 

21 | This tool merges several PDB files into one multi-model (ensemble) file. It can be used to merge several PDB files into one multi-model (ensemble) file. 

22 

23 Args: 

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

25 output_file_path (str): Multi-model (ensemble) 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_mkensemble.pdb>`_. Accepted formats: pdb (edam:format_3987). 

26 properties (dic): 

27 * **binary_path** (*str*) - ("pdb_mkensemble") Path to the pdb_mkensemble 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_mkensemble import biobb_pdb_mkensemble 

35 

36 biobb_pdb_mkensemble(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 self.io_dict = { 

58 "in": {"input_file_path": input_file_path}, 

59 "out": {"output_file_path": output_file_path}, 

60 } 

61 

62 self.binary_path = properties.get("binary_path", "pdb_mkensemble") 

63 self.properties = properties 

64 

65 self.check_properties(properties) 

66 self.check_arguments() 

67 

68 @launchlogger 

69 def launch(self) -> int: 

70 """Execute the :class:`Mkensemble <biobb_pdb_tools.pdb_tools.pdb_mkensemble>` object.""" 

71 

72 if self.check_restart(): 

73 return 0 

74 self.stage_files() 

75 

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

77 folder_path = os.path.dirname(input_file_path) 

78 

79 if zipfile.is_zipfile(input_file_path): 

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

81 zip_ref.extractall(folder_path) 

82 

83 pdb_files = [ 

84 file 

85 for file in os.listdir(folder_path) 

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

87 ] 

88 

89 input_file_list = [os.path.join( 

90 folder_path, file) for file in pdb_files] 

91 

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

93 input_file_list = sorted( 

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

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

96 

97 self.cmd = [ 

98 self.binary_path, 

99 *input_file_list, 

100 ">", 

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

102 ] 

103 

104 else: 

105 fu.log( 

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

107 self.out_log, 

108 self.global_log, 

109 ) 

110 

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

112 

113 fu.log( 

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

115 self.out_log, 

116 self.global_log, 

117 ) 

118 

119 self.run_biobb() 

120 self.copy_to_host() 

121 

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

123 self.remove_tmp_files() 

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

125 

126 return self.return_code 

127 

128 

129def biobb_pdb_mkensemble( 

130 input_file_path: str, 

131 output_file_path: str, 

132 properties: Optional[dict] = None, 

133 **kwargs, 

134) -> int: 

135 """Create :class:`Mkensemble <biobb_pdb_tools.pdb_tools.pdb_mkensemble>` class and 

136 execute the :meth:`launch() <biobb_pdb_tools.pdb_tools.pdb_mkensemble.launch>` method.""" 

137 return Mkensemble( 

138 input_file_path=input_file_path, 

139 output_file_path=output_file_path, 

140 properties=properties, 

141 **kwargs, 

142 ).launch() 

143 

144 

145biobb_pdb_mkensemble.__doc__ = Mkensemble.__doc__ 

146 

147 

148def main(): 

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

150 parser = argparse.ArgumentParser( 

151 description="Merges several PDB files into one multi-model (ensemble) file.", 

152 formatter_class=lambda prog: argparse.RawTextHelpFormatter( 

153 prog, width=99999), 

154 ) 

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

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

157 required_args.add_argument( 

158 "--input_file_path", 

159 required=True, 

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

161 ) 

162 required_args.add_argument( 

163 "--output_file_path", 

164 required=True, 

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

166 ) 

167 

168 args = parser.parse_args() 

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

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

171 

172 biobb_pdb_mkensemble( 

173 input_file_path=args.input_file_path, 

174 output_file_path=args.output_file_path, 

175 properties=properties, 

176 ) 

177 

178 

179if __name__ == "__main__": 

180 main()