Coverage for biobb_pdb_tools/pdb_tools/biobb_pdb_fetch.py: 76%

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

4 

5import argparse 

6from typing import Optional 

7 

8from biobb_common.configuration import settings 

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

15 """ 

16 | biobb_pdb_tools Pdbfetch 

17 | Downloads a structure in PDB format from the RCSB website. 

18 | This tool downloads a structure in PDB format from the RCSB website. It can be used to download a structure in PDB format from the RCSB website. 

19 

20 Args: 

21 output_file_path (str): PDB file of the protein selected. File type: output. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_pdb_tools/master/biobb_pdb_tools/test/reference/pdb_tools/ref_pdb_fetch.pdb>`_. Accepted formats: pdb (edam:format_1476). 

22 properties (dic): 

23 * **pdbid** (*string*) - ('1aki') ID of the protein. 

24 * **biounit** (*string*) - (False) Allows downloading the (first) biological structure if selected. 

25 * **binary_path** (*str*) - ("pdb_fetch") Path to the pdb_fetch 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_fetch import biobb_pdb_fetch 

33 

34 prop = { 

35 'biounit': False, 

36 'pdbid': '1aki' 

37 } 

38 biobb_pdb_fetch(output_file_path='/path/to/file.pdb', 

39 properties=prop) 

40 

41 Info: 

42 * wrapped_software: 

43 * name: pdb_tools 

44 * version: >=2.5.0 

45 * license: Apache-2.0 

46 * ontology: 

47 * name: EDAM 

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

49 

50 """ 

51 

52 def __init__(self, output_file_path, properties=None, **kwargs) -> None: 

53 properties = properties or {} 

54 

55 super().__init__(properties) 

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

57 self.io_dict = {"out": {"output_file_path": output_file_path}} 

58 

59 self.pdbid = properties.get("pdbid", "1aki") 

60 self.binary_path = properties.get("binary_path", "pdb_fetch") 

61 self.biounit = properties.get("biounit", False) 

62 self.properties = properties 

63 

64 self.check_properties(properties) 

65 self.check_arguments() 

66 

67 @launchlogger 

68 def launch(self) -> int: 

69 """Execute the :class:`Pdbfetch <biobb_pdb_tools.pdb_tools.pdb_fetch>` object.""" 

70 

71 if self.check_restart(): 

72 return 0 

73 instructions = [] 

74 if self.biounit: 

75 instructions.append("-biounit") 

76 fu.log("Appending optional boolean property", self.out_log, self.global_log) 

77 

78 self.cmd = [ 

79 self.binary_path, 

80 " ".join(instructions), 

81 self.pdbid, 

82 ">", 

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

84 ] 

85 

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

87 

88 fu.log( 

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

90 self.out_log, 

91 self.global_log, 

92 ) 

93 

94 self.run_biobb() 

95 self.copy_to_host() 

96 

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

98 self.remove_tmp_files() 

99 

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

101 return self.return_code 

102 

103 

104def biobb_pdb_fetch( 

105 output_file_path: str, properties: Optional[dict] = None, **kwargs 

106) -> int: 

107 """Create :class:`Pdbfetch <biobb_pdb_tools.pdb_tools.pdb_fetch>` class and 

108 execute the :meth:`launch() <biobb_pdb_tools.pdb_tools.pdb_fetch.launch>` method.""" 

109 return Pdbfetch( 

110 output_file_path=output_file_path, properties=properties, **kwargs 

111 ).launch() 

112 

113biobb_pdb_fetch.__doc__ = Pdbfetch.__doc__ 

114 

115 

116def main(): 

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

118 parser = argparse.ArgumentParser( 

119 description="Downloads a structure in PDB format from the RCSB website.", 

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

121 ) 

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

123 

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

125 required_args.add_argument( 

126 "--output_file_path", 

127 required=True, 

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

129 ) 

130 

131 args = parser.parse_args() 

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

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

134 

135 biobb_pdb_fetch(output_file_path=args.output_file_path, properties=properties) 

136 

137 

138if __name__ == "__main__": 

139 main()