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

59 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 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(folder_path, file) for file in pdb_files] 

90 

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

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

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

94 

95 self.cmd = [ 

96 self.binary_path, 

97 *input_file_list, 

98 ">", 

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

100 ] 

101 

102 else: 

103 fu.log( 

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

105 self.out_log, 

106 self.global_log, 

107 ) 

108 

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

110 

111 fu.log( 

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

113 self.out_log, 

114 self.global_log, 

115 ) 

116 

117 self.run_biobb() 

118 self.copy_to_host() 

119 

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

121 self.remove_tmp_files() 

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

123 

124 return self.return_code 

125 

126 

127def biobb_pdb_mkensemble( 

128 input_file_path: str, 

129 output_file_path: str, 

130 properties: Optional[dict] = None, 

131 **kwargs, 

132) -> int: 

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

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

135 return Mkensemble( 

136 input_file_path=input_file_path, 

137 output_file_path=output_file_path, 

138 properties=properties, 

139 **kwargs, 

140 ).launch() 

141 

142biobb_pdb_mkensemble.__doc__ = Mkensemble.__doc__ 

143 

144 

145def main(): 

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

147 parser = argparse.ArgumentParser( 

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

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

150 ) 

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

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

153 required_args.add_argument( 

154 "--input_file_path", 

155 required=True, 

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

157 ) 

158 required_args.add_argument( 

159 "--output_file_path", 

160 required=True, 

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

162 ) 

163 

164 args = parser.parse_args() 

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

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

167 

168 biobb_pdb_mkensemble( 

169 input_file_path=args.input_file_path, 

170 output_file_path=args.output_file_path, 

171 properties=properties, 

172 ) 

173 

174 

175if __name__ == "__main__": 

176 main()