Coverage for biobb_structure_utils/utils/cat_pdb.py: 78%

59 statements  

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

1#!/usr/bin/env python3 

2 

3"""Module containing the CatPDB 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.file_utils import launchlogger 

11 

12from biobb_structure_utils.utils.common import check_input_path, check_output_path 

13 

14 

15class CatPDB(BiobbObject): 

16 """ 

17 | biobb_structure_utils CatPDB 

18 | Class to concat two PDB structures in a single PDB file. 

19 | Class to concat two PDB structures in a single PDB file. 

20 

21 Args: 

22 input_structure1 (str): Input structure 1 file path. File type: input. `Sample file <https://github.com/bioexcel/biobb_structure_utils/raw/master/biobb_structure_utils/test/data/utils/cat_protein.pdb>`_. Accepted formats: pdb (edam:format_1476), pdbqt (edam:format_1476). 

23 input_structure2 (str): Input structure 2 file path. File type: input. `Sample file <https://github.com/bioexcel/biobb_structure_utils/raw/master/biobb_structure_utils/test/data/utils/cat_ligand.pdb>`_. Accepted formats: pdb (edam:format_1476), pdbqt (edam:format_1476). 

24 output_structure_path (str): Output protein file path. File type: output. `Sample file <https://github.com/bioexcel/biobb_structure_utils/raw/master/biobb_structure_utils/test/reference/utils/ref_cat_pdb.pdb>`_. Accepted formats: pdb (edam:format_1476), pdbqt (edam:format_1476). 

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

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

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

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

29 

30 Examples: 

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

32 

33 from biobb_structure_utils.utils.cat_pdb import cat_pdb 

34 prop = { } 

35 cat_pdb(input_structure1='/path/to/myInputStr1.pdb', 

36 input_structure2='/path/to/myInputStr2.pdb', 

37 output_structure_path='/path/to/newStructure.pdb', 

38 properties=prop) 

39 

40 Info: 

41 * wrapped_software: 

42 * name: In house 

43 * license: Apache-2.0 

44 * ontology: 

45 * name: EDAM 

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

47 

48 """ 

49 

50 def __init__( 

51 self, 

52 input_structure1, 

53 input_structure2, 

54 output_structure_path, 

55 properties=None, 

56 **kwargs, 

57 ) -> None: 

58 properties = properties or {} 

59 

60 # Call parent class constructor 

61 super().__init__(properties) 

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

63 

64 # Input/Output files 

65 self.io_dict = { 

66 "in": { 

67 "input_structure1": input_structure1, 

68 "input_structure2": input_structure2, 

69 }, 

70 "out": {"output_structure_path": output_structure_path}, 

71 } 

72 

73 # Properties specific for BB 

74 self.properties = properties 

75 

76 # Check the properties 

77 self.check_properties(properties) 

78 self.check_arguments() 

79 

80 @launchlogger 

81 def launch(self) -> int: 

82 """Execute the :class:`CatPDB <utils.cat_pdb.CatPDB>` utils.cat_pdb.CatPDB object.""" 

83 

84 self.io_dict["in"]["input_structure1"] = check_input_path( 

85 self.io_dict["in"]["input_structure1"], 

86 self.out_log, 

87 self.__class__.__name__, 

88 ) 

89 self.io_dict["in"]["input_structure2"] = check_input_path( 

90 self.io_dict["in"]["input_structure2"], 

91 self.out_log, 

92 self.__class__.__name__, 

93 ) 

94 self.io_dict["out"]["output_structure_path"] = check_output_path( 

95 self.io_dict["out"]["output_structure_path"], 

96 self.out_log, 

97 self.__class__.__name__, 

98 ) 

99 

100 # Setup Biobb 

101 if self.check_restart(): 

102 return 0 

103 self.stage_files() 

104 

105 # Business code 

106 filenames = [ 

107 self.io_dict["in"]["input_structure1"], 

108 self.io_dict["in"]["input_structure2"], 

109 ] 

110 # check if self.input_structure1 and self.input_structure2 end with newline 

111 newline = [False, False] 

112 for idx, fname in enumerate(filenames): 

113 with open(fname, "rb") as fh: 

114 fh.seek(-2, 2) 

115 last = fh.readlines()[-1].decode() 

116 newline[idx] = "\n" in last 

117 

118 # concat both input files and save them into output file 

119 with open(self.io_dict["out"]["output_structure_path"], "w") as outfile: 

120 for idx, fname in enumerate(filenames): 

121 with open(fname) as infile: 

122 for line in infile: 

123 if not line.startswith("END"): 

124 outfile.write(line) 

125 # if not ends in newline, add it 

126 if not newline[idx]: 

127 outfile.write("\n") 

128 self.return_code = 0 

129 

130 # Copy files to host 

131 self.copy_to_host() 

132 

133 # Remove temporal files 

134 # self.tmp_files.append(self.stage_io_dict.get("unique_dir", "")) 

135 self.remove_tmp_files() 

136 

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

138 

139 return self.return_code 

140 

141 

142def cat_pdb( 

143 input_structure1: str, 

144 input_structure2: str, 

145 output_structure_path: str, 

146 properties: Optional[dict] = None, 

147 **kwargs, 

148) -> int: 

149 """Execute the :class:`CatPDB <utils.cat_pdb.CatPDB>` class and 

150 execute the :meth:`launch() <utils.cat_pdb.CatPDB.launch>` method.""" 

151 

152 return CatPDB( 

153 input_structure1=input_structure1, 

154 input_structure2=input_structure2, 

155 output_structure_path=output_structure_path, 

156 properties=properties, 

157 **kwargs, 

158 ).launch() 

159 

160 cat_pdb.__doc__ = CatPDB.__doc__ 

161 

162 

163def main(): 

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

165 parser = argparse.ArgumentParser( 

166 description="Concat two PDB structures in a single PDB file.", 

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

168 ) 

169 parser.add_argument( 

170 "-c", 

171 "--config", 

172 required=False, 

173 help="This file can be a YAML file, JSON file or JSON string", 

174 ) 

175 

176 # Specific args of each building block 

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

178 required_args.add_argument( 

179 "-i1", 

180 "--input_structure1", 

181 required=True, 

182 help="Input structure 1 file path. Accepted formats: pdb.", 

183 ) 

184 required_args.add_argument( 

185 "-i2", 

186 "--input_structure2", 

187 required=True, 

188 help="Input structure 2 file path. Accepted formats: pdb.", 

189 ) 

190 required_args.add_argument( 

191 "-o", 

192 "--output_structure_path", 

193 required=True, 

194 help="Output structure file path. Accepted formats: pdb.", 

195 ) 

196 

197 args = parser.parse_args() 

198 config = args.config if args.config else None 

199 properties = settings.ConfReader(config=config).get_prop_dic() 

200 

201 # Specific call of each building block 

202 cat_pdb( 

203 input_structure1=args.input_structure1, 

204 input_structure2=args.input_structure2, 

205 output_structure_path=args.output_structure_path, 

206 properties=properties, 

207 ) 

208 

209 

210if __name__ == "__main__": 

211 main()