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

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

4 

5import os 

6import zipfile 

7from pathlib import Path 

8from typing import Optional 

9 

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

16 """ 

17 | biobb_pdb_tools Pdbmerge 

18 | Merges several PDB files into one. 

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

20 

21 Args: 

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

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

24 properties (dic): 

25 * **binary_path** (*str*) - ("pdb_merge") Path to the pdb_merge 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_merge import biobb_pdb_merge 

33 

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

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

62 self.properties = properties 

63 self.check_init(properties) 

64 

65 @launchlogger 

66 def launch(self) -> int: 

67 """Execute the :class:`Pdbmerge <biobb_pdb_tools.pdb_tools.pdb_merge>` object.""" 

68 

69 if self.check_restart(): 

70 return 0 

71 self.stage_files() 

72 

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

74 folder_path = os.path.dirname(input_file_path) 

75 

76 if zipfile.is_zipfile(input_file_path): 

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

78 zip_ref.extractall(folder_path) 

79 extracted_files = zip_ref.namelist() 

80 

81 pdb_files = [ 

82 file 

83 for file in extracted_files 

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

85 ] 

86 

87 input_file_list = [os.path.join( 

88 folder_path, file) for file in pdb_files] 

89 

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

91 input_file_list = sorted( 

92 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, self.global_log) 

114 

115 self.run_biobb() 

116 self.copy_to_host() 

117 self.remove_tmp_files() 

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

119 

120 return self.return_code 

121 

122 

123def biobb_pdb_merge( 

124 input_file_path: str, 

125 output_file_path: str, 

126 properties: Optional[dict] = None, 

127 **kwargs, 

128) -> int: 

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

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

131 return Pdbmerge(**dict(locals())).launch() 

132 

133 

134biobb_pdb_merge.__doc__ = Pdbmerge.__doc__ 

135main = Pdbmerge.get_main(biobb_pdb_merge, "Merges several PDB files into one.") 

136 

137if __name__ == "__main__": 

138 main()