Coverage for biobb_pdb_tools/pdb_tools/biobb_pdb_selres.py: 78%

50 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 Pdbselres 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 

14# 1. Rename class as required 

15class Pdbselres(BiobbObject): 

16 """ 

17 | biobb_pdb_tools Pdbselres 

18 | Selects residues by their index, piecewise or in a range. 

19 | Works by selecting residues based on their index. Can select individual residues, a range of residues, or residues at regular intervals. 

20 

21 Args: 

22 input_file_path (str): PDB file. File type: input. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_pdb_tools/master/biobb_pdb_tools/test/data/pdb_tools/1IGY.pdb>`_. Accepted formats: pdb (edam:format_1476). 

23 output_file_path (str): PDB file with selected residues. File type: output. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_pdb_tools/master/biobb_pdb_tools/test/reference/pdb_tools/ref_pdb_selres.pdb>`_. Accepted formats: pdb (edam:format_1476). 

24 properties (dic): 

25 * **selection** (*string*) - (None) Residue selection format: individual residues "1,2,4,6", range "1:10", multiple ranges "1:10,20:30", open ranges "1:", ":5", or intervals "::5", "1:10:5". 

26 * **binary_path** (*str*) - ("pdb_selres") Path to the pdb_selres executable binary. 

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

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

29 

30 Examples: 

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

32 

33 from biobb_pdb_tools.pdb_tools.biobb_pdb_selres import biobb_pdb_selres 

34 

35 prop = { 

36 'selection': '1:20:2' 

37 } 

38 biobb_pdb_selres(input_file_path='/path/to/input.pdb', 

39 output_file_path='/path/to/output.pdb', 

40 properties=prop) 

41 

42 Info: 

43 * wrapped_software: 

44 * name: pdb_tools 

45 * version: >=2.5.0 

46 * license: Apache-2.0 

47 * ontology: 

48 * name: EDAM 

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

50 

51 """ 

52 

53 def __init__( 

54 self, input_file_path, output_file_path, properties=None, **kwargs 

55 ) -> None: 

56 properties = properties or {} 

57 

58 super().__init__(properties) 

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

60 

61 self.io_dict = { 

62 "in": {"input_file_path": input_file_path}, 

63 "out": {"output_file_path": output_file_path}, 

64 } 

65 

66 self.binary_path = properties.get("binary_path", "pdb_selres") 

67 self.selection = properties.get("selection", None) 

68 self.properties = properties 

69 

70 self.check_properties(properties) 

71 self.check_arguments() 

72 

73 @launchlogger 

74 def launch(self) -> int: 

75 """Execute the :class:`Pdbselres <biobb_pdb_tools.pdb_tools.pdb_selres>` object.""" 

76 

77 if self.check_restart(): 

78 return 0 

79 self.stage_files() 

80 

81 instructions = [] 

82 if self.selection: 

83 instructions.append("-" + str(self.selection)) 

84 fu.log("Selecting residues with pattern: " + self.selection, self.out_log, self.global_log) 

85 

86 self.cmd = [ 

87 self.binary_path, 

88 " ".join(instructions), 

89 self.stage_io_dict["in"]["input_file_path"], 

90 ">", 

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

92 ] 

93 

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

95 

96 fu.log( 

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

98 self.out_log, 

99 self.global_log, 

100 ) 

101 

102 self.run_biobb() 

103 self.copy_to_host() 

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

105 self.remove_tmp_files() 

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

107 

108 return self.return_code 

109 

110 

111def biobb_pdb_selres( 

112 input_file_path: str, 

113 output_file_path: str, 

114 properties: Optional[dict] = None, 

115 **kwargs, 

116) -> int: 

117 """Create :class:`Pdbselres <biobb_pdb_tools.pdb_tools.pdb_selres>` class and 

118 execute the :meth:`launch() <biobb_pdb_tools.pdb_tools.pdb_selres.launch>` method.""" 

119 

120 return Pdbselres( 

121 input_file_path=input_file_path, 

122 output_file_path=output_file_path, 

123 properties=properties, 

124 **kwargs, 

125 ).launch() 

126 

127biobb_pdb_selres.__doc__ = Pdbselres.__doc__ 

128 

129 

130def main(): 

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

132 parser = argparse.ArgumentParser( 

133 description="Selects residues by their index, piecewise or in a range.", 

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

135 ) 

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

137 

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

139 required_args.add_argument( 

140 "--input_file_path", 

141 required=True, 

142 help="PDB file. Accepted formats: pdb.", 

143 ) 

144 required_args.add_argument( 

145 "--output_file_path", 

146 required=True, 

147 help="PDB file with selected residues. Accepted formats: pdb.", 

148 ) 

149 

150 args = parser.parse_args() 

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

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

153 

154 biobb_pdb_selres( 

155 input_file_path=args.input_file_path, 

156 output_file_path=args.output_file_path, 

157 properties=properties, 

158 ) 

159 

160 

161if __name__ == "__main__": 

162 main()