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

58 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-10-03 15:30 +0000

1#!/usr/bin/env python3 

2 

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

4import argparse 

5from biobb_common.configuration import settings 

6from biobb_common.generic.biobb_object import BiobbObject 

7from biobb_common.tools.file_utils import launchlogger 

8from biobb_structure_utils.utils.common import check_input_path, check_output_path 

9 

10 

11class CatPDB(BiobbObject): 

12 """ 

13 | biobb_structure_utils CatPDB 

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

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

16 

17 Args: 

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

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

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

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

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

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

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

25 

26 Examples: 

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

28 

29 from biobb_structure_utils.utils.cat_pdb import cat_pdb 

30 prop = { } 

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

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

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

34 properties=prop) 

35 

36 Info: 

37 * wrapped_software: 

38 * name: In house 

39 * license: Apache-2.0 

40 * ontology: 

41 * name: EDAM 

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

43 

44 """ 

45 

46 def __init__(self, input_structure1, input_structure2, output_structure_path, properties=None, **kwargs) -> None: 

47 properties = properties or {} 

48 

49 # Call parent class constructor 

50 super().__init__(properties) 

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

52 

53 # Input/Output files 

54 self.io_dict = { 

55 "in": {"input_structure1": input_structure1, 

56 "input_structure2": input_structure2}, 

57 "out": {"output_structure_path": output_structure_path} 

58 } 

59 

60 # Properties specific for BB 

61 self.properties = properties 

62 

63 # Check the properties 

64 self.check_properties(properties) 

65 self.check_arguments() 

66 

67 @launchlogger 

68 def launch(self) -> int: 

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

70 

71 self.io_dict['in']['input_structure1'] = check_input_path(self.io_dict['in']['input_structure1'], 

72 self.out_log, self.__class__.__name__) 

73 self.io_dict['in']['input_structure2'] = check_input_path(self.io_dict['in']['input_structure2'], 

74 self.out_log, self.__class__.__name__) 

75 self.io_dict['out']['output_structure_path'] = check_output_path(self.io_dict['out']['output_structure_path'], 

76 self.out_log, self.__class__.__name__) 

77 

78 # Setup Biobb 

79 if self.check_restart(): 

80 return 0 

81 self.stage_files() 

82 

83 # Business code 

84 filenames = [self.io_dict['in']['input_structure1'], self.io_dict['in']['input_structure2']] 

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

86 newline = [False, False] 

87 for idx, fname in enumerate(filenames): 

88 with open(fname, 'rb') as fh: 

89 fh.seek(-2, 2) 

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

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

92 

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

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

95 for idx, fname in enumerate(filenames): 

96 with open(fname) as infile: 

97 for line in infile: 

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

99 outfile.write(line) 

100 # if not ends in newline, add it 

101 if not newline[idx]: 

102 outfile.write("\n") 

103 self.return_code = 0 

104 

105 # Copy files to host 

106 self.copy_to_host() 

107 

108 # Remove temporal files 

109 self.tmp_files.append(self.stage_io_dict.get("unique_dir")) 

110 self.remove_tmp_files() 

111 

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

113 

114 return self.return_code 

115 

116 

117def cat_pdb(input_structure1: str, input_structure2: str, output_structure_path: str, properties: dict = None, **kwargs) -> int: 

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

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

120 

121 return CatPDB(input_structure1=input_structure1, 

122 input_structure2=input_structure2, 

123 output_structure_path=output_structure_path, 

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

125 

126 

127def main(): 

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

129 parser = argparse.ArgumentParser(description="Concat two PDB structures in a single PDB file.", formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

130 parser.add_argument('-c', '--config', required=False, help="This file can be a YAML file, JSON file or JSON string") 

131 

132 # Specific args of each building block 

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

134 required_args.add_argument('-i1', '--input_structure1', required=True, help="Input structure 1 file path. Accepted formats: pdb.") 

135 required_args.add_argument('-i2', '--input_structure2', required=True, help="Input structure 2 file path. Accepted formats: pdb.") 

136 required_args.add_argument('-o', '--output_structure_path', required=True, help="Output structure file path. Accepted formats: pdb.") 

137 

138 args = parser.parse_args() 

139 config = args.config if args.config else None 

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

141 

142 # Specific call of each building block 

143 cat_pdb(input_structure1=args.input_structure1, 

144 input_structure2=args.input_structure2, 

145 output_structure_path=args.output_structure_path, 

146 properties=properties) 

147 

148 

149if __name__ == '__main__': 

150 main()