Coverage for biobb_vs / fpocket / fpocket_select.py: 96%

48 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2025-12-22 13:24 +0000

1#!/usr/bin/env python3 

2 

3"""Module containing the FPocketSelect class and the command line interface.""" 

4from typing import Optional 

5import shutil 

6from pathlib import PurePath 

7from biobb_common.generic.biobb_object import BiobbObject 

8from biobb_common.tools import file_utils as fu 

9from biobb_common.tools.file_utils import launchlogger 

10from biobb_vs.fpocket.common import check_input_path, check_output_path 

11 

12 

13class FPocketSelect(BiobbObject): 

14 """ 

15 | biobb_vs FPocketSelect 

16 | Selects a single pocket in the outputs of the fpocket building block. 

17 | Selects a single pocket in the outputs of the fpocket building block from a given parameter. 

18 

19 Args: 

20 input_pockets_zip (str): Path to the pockets found by fpocket. File type: input. `Sample file <https://github.com/bioexcel/biobb_vs/raw/master/biobb_vs/test/data/fpocket/input_pockets.zip>`_. Accepted formats: zip (edam:format_3987). 

21 output_pocket_pdb (str): Path to the PDB file with the cavity found by fpocket. File type: output. `Sample file <https://github.com/bioexcel/biobb_vs/raw/master/biobb_vs/test/reference/fpocket/ref_output_pocket.pdb>`_. Accepted formats: pdb (edam:format_1476). 

22 output_pocket_pqr (str): Path to the PQR file with the pocket found by fpocket. File type: output. `Sample file <https://github.com/bioexcel/biobb_vs/raw/master/biobb_vs/test/reference/fpocket/ref_output_pocket.pqr>`_. Accepted formats: pqr (edam:format_1476). 

23 properties (dic - Python dictionary object containing the tool parameters, not input/output files): 

24 * **pocket** (*int*) - (1) [1~1000|1] Pocket id from the summary json given by the fpocket building block. 

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

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

27 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory. 

28 

29 Examples: 

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

31 

32 from biobb_vs.fpocket.fpocket_select import fpocket_select 

33 prop = { 

34 'pocket': 2 

35 } 

36 fpocket_select(input_pockets_zip='/path/to/myPockets.zip', 

37 output_pocket_pdb='/path/to/myCavity.pdb', 

38 output_pocket_pqr='/path/to/myPocket.pqr', 

39 properties=prop) 

40 

41 Info: 

42 * wrapped_software: 

43 * name: In house 

44 * license: Apache-2.0 

45 * ontology: 

46 * name: EDAM 

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

48 

49 """ 

50 

51 def __init__(self, input_pockets_zip, output_pocket_pdb, output_pocket_pqr, 

52 properties=None, **kwargs) -> None: 

53 properties = properties or {} 

54 

55 # Call parent class constructor 

56 super().__init__(properties) 

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

58 

59 # Input/Output files 

60 self.io_dict = { 

61 "in": {"input_pockets_zip": input_pockets_zip}, 

62 "out": {"output_pocket_pdb": output_pocket_pdb, "output_pocket_pqr": output_pocket_pqr} 

63 } 

64 

65 # Properties specific for BB 

66 self.pocket = properties.get('pocket', None) 

67 self.properties = properties 

68 

69 # Check the properties 

70 self.check_properties(properties) 

71 self.check_arguments() 

72 

73 def check_data_params(self, out_log, err_log): 

74 """ Checks all the input/output paths and parameters """ 

75 self.io_dict["in"]["input_pockets_zip"] = check_input_path(self.io_dict["in"]["input_pockets_zip"], "input_pockets_zip", out_log, self.__class__.__name__) 

76 self.io_dict["out"]["output_pocket_pdb"] = check_output_path(self.io_dict["out"]["output_pocket_pdb"], "output_pocket_pdb", False, out_log, self.__class__.__name__) 

77 self.io_dict["out"]["output_pocket_pqr"] = check_output_path(self.io_dict["out"]["output_pocket_pqr"], "output_pocket_pqr", True, out_log, self.__class__.__name__) 

78 

79 @launchlogger 

80 def launch(self) -> int: 

81 """Execute the :class:`FPocketSelect <fpocket.fpocket_select.FPocketSelect>` fpocket.fpocket_select.FPocketSelect object.""" 

82 

83 # check input/output paths and parameters 

84 self.check_data_params(self.out_log, self.err_log) 

85 

86 # Setup Biobb 

87 if self.check_restart(): 

88 return 0 

89 self.stage_files() 

90 

91 # create tmp_folder 

92 tmp_folder = fu.create_unique_dir() 

93 fu.log('Creating %s temporary folder' % tmp_folder, self.out_log) 

94 

95 # decompress the input_pockets_zip file to tmp_folder 

96 all_pockets = fu.unzip_list(zip_file=self.io_dict["in"]["input_pockets_zip"], dest_dir=tmp_folder, out_log=self.out_log) 

97 

98 pockets_list = [i for i in all_pockets if ('pocket' + str(self.pocket)) in i] 

99 

100 for p in pockets_list: 

101 if PurePath(p).suffix == '.pdb': 

102 fu.log('Saving %s file' % self.io_dict["out"]["output_pocket_pdb"], self.out_log) 

103 shutil.copy(p, self.io_dict["out"]["output_pocket_pdb"]) 

104 else: 

105 fu.log('Saving %s file' % self.io_dict["out"]["output_pocket_pqr"], self.out_log) 

106 shutil.copy(p, self.io_dict["out"]["output_pocket_pqr"]) 

107 

108 # Copy files to host 

109 self.copy_to_host() 

110 

111 self.tmp_files.append(tmp_folder) 

112 self.remove_tmp_files() 

113 

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

115 

116 return 0 

117 

118 

119def fpocket_select(input_pockets_zip: str, output_pocket_pdb: str, output_pocket_pqr: str, properties: Optional[dict] = None, **kwargs) -> int: 

120 """Create the :class:`FPocketSelect <fpocket.fpocket_select.FPocketSelect>` class and 

121 execute the :meth:`launch() <fpocket.fpocket_select.FPocketSelect.launch>` method.""" 

122 return FPocketSelect(**dict(locals())).launch() 

123 

124 

125fpocket_select.__doc__ = FPocketSelect.__doc__ 

126main = FPocketSelect.get_main(fpocket_select, "Selects a single pocket in the outputs of the fpocket building block from a given parameter.") 

127 

128 

129if __name__ == '__main__': 

130 main()