Coverage for biobb_model/model/fix_altlocs.py: 71%

48 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 FixAltLocs 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 

9 

10class FixAltLocs(BiobbObject): 

11 """ 

12 | biobb_model FixAltLocs 

13 | Fix alternate locations from residues. 

14 

15 Args: 

16 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/3ebp.pdb>`_. Accepted formats: pdb (edam:format_1476). 

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

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

19 * **altlocs** (*list*) - (None) List of alternate locations to fix. Format: ["A339:A", "A171:B", "A768:A"]; where for each residue the format is as follows: "<chain><residue id>:<chosen alternate location>". If empty, no action will be executed. 

20 * **modeller_key** (*str*) - (None) Modeller license key. 

21 * **binary_path** (*str*) - ("check_structure") Path to the check_structure executable binary. 

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

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

24 

25 Examples: 

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

27 

28 from biobb_model.model.fix_altlocs import fix_altlocs 

29 prop = { 'altlocs': ['A339:A', 'A171:B', 'A768:A'] } 

30 fix_altlocs(input_pdb_path='/path/to/myStructure.pdb', 

31 output_pdb_path='/path/to/newStructure.pdb', 

32 properties=prop) 

33 

34 Info: 

35 * wrapped_software: 

36 * name: In house 

37 * license: Apache-2.0 

38 * ontology: 

39 * name: EDAM 

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

41 """ 

42 

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

44 properties = properties or {} 

45 

46 # Call parent class constructor 

47 super().__init__(properties) 

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

49 

50 # Input/Output files 

51 self.io_dict = { 

52 "in": {"input_pdb_path": input_pdb_path}, 

53 "out": {"output_pdb_path": output_pdb_path} 

54 } 

55 

56 # Properties specific for BB 

57 self.binary_path = properties.get('binary_path', 'check_structure') 

58 self.altlocs = properties.get('altlocs', None) 

59 self.modeller_key = properties.get('modeller_key') 

60 

61 # Check the properties 

62 self.check_properties(properties) 

63 self.check_arguments() 

64 

65 @launchlogger 

66 def launch(self) -> int: 

67 """Execute the :class:`FixAltLocs <model.fix_altlocs.FixAltLocs>` object.""" 

68 

69 # Setup Biobb 

70 if self.check_restart(): 

71 return 0 

72 self.stage_files() 

73 

74 self.cmd = [self.binary_path, 

75 '-i', self.stage_io_dict["in"]["input_pdb_path"], 

76 '-o', self.stage_io_dict["out"]["output_pdb_path"], 

77 '--force_save', 

78 '--non_interactive', 

79 'altloc', '--select'] 

80 

81 if self.altlocs: 

82 self.cmd.append(','.join(self.altlocs)) 

83 else: 

84 self.cmd.append('occupancy') 

85 

86 if self.modeller_key: 

87 self.cmd.insert(1, self.modeller_key) 

88 self.cmd.insert(1, '--modeller_key') 

89 

90 # Run Biobb block 

91 self.run_biobb() 

92 

93 # Copy files to host 

94 self.copy_to_host() 

95 

96 # Remove temporal files 

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

98 self.remove_tmp_files() 

99 

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

101 return self.return_code 

102 

103 

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

105 """Create :class:`FixAltLocs <model.fix_altlocs.FixAltLocs>` class and 

106 execute the :meth:`launch() <model.fix_altlocs.FixAltLocs.launch>` method.""" 

107 return FixAltLocs(input_pdb_path=input_pdb_path, 

108 output_pdb_path=output_pdb_path, 

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

110 

111 

112def main(): 

113 parser = argparse.ArgumentParser(description="Fix alternate locations from residues", 

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

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

116 

117 # Specific args of each building block 

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

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

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

121 

122 args = parser.parse_args() 

123 config = args.config if args.config else None 

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

125 

126 # Specific call of each building block 

127 fix_altlocs(input_pdb_path=args.input_pdb_path, 

128 output_pdb_path=args.output_pdb_path, 

129 properties=properties) 

130 

131 

132if __name__ == '__main__': 

133 main()