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

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

4 

5import os 

6import zipfile 

7from pathlib import Path 

8from typing import Optional 

9from biobb_common.generic.biobb_object import BiobbObject 

10from biobb_common.tools import file_utils as fu 

11from biobb_common.tools.file_utils import launchlogger 

12 

13 

14class Mkensemble(BiobbObject): 

15 """ 

16 | biobb_pdb_tools Mkensemble 

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

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

19 

20 Args: 

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

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

23 properties (dic): 

24 * **binary_path** (*str*) - ("pdb_mkensemble") Path to the pdb_mkensemble executable binary. 

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

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

27 

28 Examples: 

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

30 

31 from biobb_pdb_tools.pdb_tools.biobb_pdb_mkensemble import biobb_pdb_mkensemble 

32 

33 biobb_pdb_mkensemble(input_file_path='/path/to/input1.zip', 

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

35 

36 Info: 

37 * wrapped_software: 

38 * name: pdb_tools 

39 * version: >=2.5.0 

40 * license: Apache-2.0 

41 * ontology: 

42 * name: EDAM 

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

44 

45 """ 

46 

47 def __init__( 

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

49 ) -> None: 

50 properties = properties or {} 

51 

52 super().__init__(properties) 

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

54 self.io_dict = { 

55 "in": {"input_file_path": input_file_path}, 

56 "out": {"output_file_path": output_file_path}, 

57 } 

58 

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

60 self.properties = properties 

61 self.check_init(properties) 

62 

63 @launchlogger 

64 def launch(self) -> int: 

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

66 

67 if self.check_restart(): 

68 return 0 

69 self.stage_files() 

70 

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

72 folder_path = os.path.dirname(input_file_path) 

73 

74 if zipfile.is_zipfile(input_file_path): 

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

76 zip_ref.extractall(folder_path) 

77 

78 pdb_files = [ 

79 file 

80 for file in os.listdir(folder_path) 

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

82 ] 

83 

84 input_file_list = [os.path.join( 

85 folder_path, file) for file in pdb_files] 

86 

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

88 input_file_list = sorted( 

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

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

91 

92 self.cmd = [ 

93 self.binary_path, 

94 *input_file_list, 

95 ">", 

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

97 ] 

98 

99 else: 

100 fu.log( 

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

102 self.out_log, 

103 self.global_log, 

104 ) 

105 

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

107 

108 fu.log( 

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

110 self.out_log, 

111 self.global_log, 

112 ) 

113 

114 self.run_biobb() 

115 self.copy_to_host() 

116 self.remove_tmp_files() 

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

118 

119 return self.return_code 

120 

121 

122def biobb_pdb_mkensemble( 

123 input_file_path: str, 

124 output_file_path: str, 

125 properties: Optional[dict] = None, 

126 **kwargs, 

127) -> int: 

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

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

130 return Mkensemble(**dict(locals())).launch() 

131 

132 

133biobb_pdb_mkensemble.__doc__ = Mkensemble.__doc__ 

134main = Mkensemble.get_main(biobb_pdb_mkensemble, "Merges several PDB files into one multi-model (ensemble) file.") 

135 

136if __name__ == "__main__": 

137 main()