Coverage for biobb_model/model/fix_pdb.py: 75%

59 statements  

« prev     ^ index     » next       coverage.py v7.4.3, created at 2024-03-13 17:26 +0000

1#!/usr/bin/env python3 

2 

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

4import argparse 

5from biobb_common.generic.biobb_object import BiobbObject 

6from biobb_common.configuration import settings 

7from biobb_common.tools.file_utils import launchlogger 

8 

9from .fix_pdb_utils import Structure, generate_map_online 

10 

11 

12class FixPdb(BiobbObject): 

13 """ 

14 | biobb_model FixPdb 

15 | Class to renumerate residues in a PDB structure according to a reference sequence from UniProt. 

16 Args: 

17 input_pdb_path (str): Input PDB file path. File type: input. `Sample file <https://github.com/bioexcel/biobb_model/raw/master/biobb_model/test/data/model/2ki5.pdb>`_. Accepted formats: pdb (edam:format_1476). 

18 output_pdb_path (str): Output PDB file path. File type: output. `Sample file <https://github.com/bioexcel/biobb_model/raw/master/biobb_model/test/reference/model/output_pdb_path.pdb>`_. Accepted formats: pdb (edam:format_1476). 

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

20 * **forced_uniprot_references** (*str*) - (None) Set the UniProt accessions for sequences to be used as reference. 

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

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

23 Examples: 

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

25 from biobb_model.model.fix_pdb import fix_pdb 

26 prop = { 'forced_uniprot_references': ["P00533"] } 

27 fix_pdb(input_pdb_path='/path/to/myStructure.pdb', output_pdb_path='/path/to/newStructure.pdb', properties=prop) 

28 Info: 

29 * wrapped_software: 

30 * name: In house 

31 * license: Apache-2.0 

32 * ontology: 

33 * name: EDAM 

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

35 """ 

36 

37 def __init__(self, input_pdb_path: str, output_pdb_path: str, properties: dict = None, **kwargs) -> None: 

38 properties = properties or {} 

39 

40 # Call parent class constructor 

41 super().__init__(properties) 

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

43 

44 # Input/Output files 

45 self.io_dict = { 

46 "in": {"input_pdb_path": input_pdb_path}, 

47 "out": {"output_pdb_path": output_pdb_path} 

48 } 

49 

50 # Properties specific for BB 

51 self.forced_uniprot_references = properties.get('forced_uniprot_references') 

52 # If input forced uniprot references is a string and not a list then convert it 

53 if type(self.forced_uniprot_references) == str: 

54 self.forced_uniprot_references = self.forced_uniprot_references.split(' ') 

55 

56 # Check the properties 

57 self.check_properties(properties) 

58 self.check_arguments() 

59 

60 @launchlogger 

61 def launch(self) -> int: 

62 """Execute the :class:`FixPdb <model.fix_pdb.FixPdb>` object.""" 

63 

64 # Setup Biobb 

65 if self.check_restart(): 

66 return 0 

67 

68 # Run code 

69 self.return_code = 0 

70 

71 # Get the user arguments 

72 input_pdb_path = self.io_dict["in"]["input_pdb_path"] 

73 output_pdb_path = self.io_dict["out"]["output_pdb_path"] 

74 forced_uniprot_references = self.forced_uniprot_references 

75 

76 # Read and parse the input pdb file 

77 structure = Structure.from_pdb_file(input_pdb_path) 

78 

79 # Add protein chains in case they are missing 

80 chains = structure.chains 

81 if len(chains) == 0 or (len(chains) == 1 and (chains[0].name == ' ' or chains[0].name == 'X')): 

82 structure.raw_protein_chainer() 

83 

84 # Run all the mapping function 

85 mapping = generate_map_online(structure, forced_uniprot_references) 

86 

87 # In case something went wrong with the mapping stop here 

88 if not mapping: 

89 self.return_code = -1 

90 return self.return_code 

91 

92 # Change residue numbers in the structure according to the mapping results 

93 mapped_residue_numbers = mapping['residue_reference_numbers'] 

94 for r, residue in enumerate(structure.residues): 

95 mapped_residue_number = mapped_residue_numbers[r] 

96 if mapped_residue_number is None: 

97 continue 

98 residue.number = mapped_residue_number 

99 

100 # Write the modified structure to a new pdb file 

101 structure.generate_pdb_file(output_pdb_path) 

102 

103 print('Fixed :)') 

104 

105 # Remove temporal files 

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

107 self.remove_tmp_files() 

108 

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

110 return self.return_code 

111 

112 

113def fix_pdb(input_pdb_path: str, output_pdb_path: str, properties: dict = None, **kwargs) -> int: 

114 """Create :class:`FixPdb <model.fix_pdb.FixPdb>` class and 

115 execute the :meth:`launch() <model.fix_pdb.FixPdb.launch>` method.""" 

116 return FixPdb(input_pdb_path=input_pdb_path, 

117 output_pdb_path=output_pdb_path, 

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

119 

120 

121def main(): 

122 parser = argparse.ArgumentParser(description="Model the missing atoms in the backbone of a PDB structure.", 

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

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

125 

126 # Specific args of each building block 

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

128 required_args.add_argument('-i', '--input_pdb_path', required=True, help="Input PDB file name") 

129 required_args.add_argument('-o', '--output_pdb_path', required=True, help="Output PDB file name") 

130 

131 args = parser.parse_args() 

132 config = args.config if args.config else None 

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

134 

135 # Specific call of each building block 

136 fix_pdb(input_pdb_path=args.input_pdb_path, 

137 output_pdb_path=args.output_pdb_path, 

138 properties=properties) 

139 

140 

141if __name__ == '__main__': 

142 main()