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

60 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-28 12:00 +0000

1#!/usr/bin/env python3 

2 

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

4import argparse 

5from typing import Optional 

6import shutil 

7from pathlib import PurePath 

8from biobb_common.generic.biobb_object import BiobbObject 

9from biobb_common.configuration import settings 

10from biobb_common.tools import file_utils as fu 

11from biobb_common.tools.file_utils import launchlogger 

12from biobb_vs.fpocket.common import check_input_path, check_output_path 

13 

14 

15class FPocketSelect(BiobbObject): 

16 """ 

17 | biobb_vs FPocketSelect 

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

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

20 

21 Args: 

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

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

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

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

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

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

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

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

30 

31 Examples: 

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

33 

34 from biobb_vs.fpocket.fpocket_select import fpocket_select 

35 prop = { 

36 'pocket': 2 

37 } 

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

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

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

41 properties=prop) 

42 

43 Info: 

44 * wrapped_software: 

45 * name: In house 

46 * license: Apache-2.0 

47 * ontology: 

48 * name: EDAM 

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

50 

51 """ 

52 

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

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

55 properties = properties or {} 

56 

57 # Call parent class constructor 

58 super().__init__(properties) 

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

60 

61 # Input/Output files 

62 self.io_dict = { 

63 "in": {"input_pockets_zip": input_pockets_zip}, 

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

65 } 

66 

67 # Properties specific for BB 

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

69 self.properties = properties 

70 

71 # Check the properties 

72 self.check_properties(properties) 

73 self.check_arguments() 

74 

75 def check_data_params(self, out_log, err_log): 

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

77 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__) 

78 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__) 

79 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__) 

80 

81 @launchlogger 

82 def launch(self) -> int: 

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

84 

85 # check input/output paths and parameters 

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

87 

88 # Setup Biobb 

89 if self.check_restart(): 

90 return 0 

91 self.stage_files() 

92 

93 # create tmp_folder 

94 self.tmp_folder = fu.create_unique_dir() 

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

96 

97 # decompress the input_pockets_zip file to tmp_folder 

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

99 

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

101 

102 for p in pockets_list: 

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

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

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

106 else: 

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

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

109 

110 # Copy files to host 

111 self.copy_to_host() 

112 

113 self.tmp_files.extend([ 

114 # self.stage_io_dict.get("unique_dir", ""), 

115 self.tmp_folder 

116 ]) 

117 self.remove_tmp_files() 

118 

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

120 

121 return 0 

122 

123 

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

125 """Execute the :class:`FPocketSelect <fpocket.fpocket_select.FPocketSelect>` class and 

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

127 

128 return FPocketSelect(input_pockets_zip=input_pockets_zip, 

129 output_pocket_pdb=output_pocket_pdb, 

130 output_pocket_pqr=output_pocket_pqr, 

131 properties=properties, **kwargs).launch() 

132 

133 fpocket_select.__doc__ = FPocketSelect.__doc__ 

134 

135 

136def main(): 

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

138 parser = argparse.ArgumentParser(description="Selects a single pocket in the outputs of the fpocket building block from a given parameter.", formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

139 parser.add_argument('--config', required=False, help='Configuration file') 

140 

141 # Specific args of each building block 

142 required_args = parser.add_argument_group('required arguments') 

143 required_args.add_argument('--input_pockets_zip', required=True, help='Path to all the pockets found by fpocket. Accepted formats: zip.') 

144 required_args.add_argument('--output_pocket_pdb', required=True, help='Path to the PDB file with the cavity found by fpocket. Accepted formats: pdb.') 

145 required_args.add_argument('--output_pocket_pqr', required=True, help='Path to the PQR file with the pocket found by fpocket. Accepted formats: pqr.') 

146 

147 args = parser.parse_args() 

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

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

150 

151 # Specific call of each building block 

152 fpocket_select(input_pockets_zip=args.input_pockets_zip, 

153 output_pocket_pdb=args.output_pocket_pdb, 

154 output_pocket_pqr=args.output_pocket_pqr, 

155 properties=properties) 

156 

157 

158if __name__ == '__main__': 

159 main()