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

49 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-20 08:28 +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", 

77 self.out_log, self.global_log) 

78 

79 self.cmd = [ 

80 self.binary_path, 

81 " ".join(instructions), 

82 self.pdbid, 

83 ">", 

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

85 ] 

86 

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

88 

89 fu.log( 

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

91 self.out_log, 

92 self.global_log, 

93 ) 

94 

95 self.run_biobb() 

96 self.copy_to_host() 

97 

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

99 self.remove_tmp_files() 

100 

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

102 return self.return_code 

103 

104 

105def biobb_pdb_fetch( 

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

107) -> int: 

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

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

110 return Pdbfetch( 

111 output_file_path=output_file_path, properties=properties, **kwargs 

112 ).launch() 

113 

114 

115biobb_pdb_fetch.__doc__ = Pdbfetch.__doc__ 

116 

117 

118def main(): 

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

120 parser = argparse.ArgumentParser( 

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

122 formatter_class=lambda prog: argparse.RawTextHelpFormatter( 

123 prog, width=99999), 

124 ) 

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

126 

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

128 required_args.add_argument( 

129 "--output_file_path", 

130 required=True, 

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

132 ) 

133 

134 args = parser.parse_args() 

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

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

137 

138 biobb_pdb_fetch(output_file_path=args.output_file_path, 

139 properties=properties) 

140 

141 

142if __name__ == "__main__": 

143 main()