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

49 statements  

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

1#!/usr/bin/env python3 

2 

3"""Module containing the FixAltLocs 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_model.model.common import _from_string_to_list 

13 

14 

15class FixAltLocs(BiobbObject): 

16 """ 

17 | biobb_model FixAltLocs 

18 | Fix alternate locations from residues. 

19 | Fix alternate locations using the altlocs list or occupancy. 

20 

21 Args: 

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

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

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

25 * **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. 

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

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

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

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

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

31 

32 Examples: 

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

34 

35 from biobb_model.model.fix_altlocs import fix_altlocs 

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

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

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

39 properties=prop) 

40 

41 Info: 

42 * wrapped_software: 

43 * name: In house 

44 * license: Apache-2.0 

45 * ontology: 

46 * name: EDAM 

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

48 """ 

49 

50 def __init__( 

51 self, 

52 input_pdb_path: str, 

53 output_pdb_path: str, 

54 properties: Optional[dict] = None, 

55 **kwargs, 

56 ) -> None: 

57 properties = properties or {} 

58 

59 # Call parent class constructor 

60 super().__init__(properties) 

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

62 

63 # Input/Output files 

64 self.io_dict = { 

65 "in": {"input_pdb_path": input_pdb_path}, 

66 "out": {"output_pdb_path": output_pdb_path}, 

67 } 

68 

69 # Properties specific for BB 

70 self.binary_path = properties.get("binary_path", "check_structure") 

71 self.altlocs = _from_string_to_list(properties.get("altlocs", None)) 

72 self.modeller_key = properties.get("modeller_key") 

73 

74 # Check the properties 

75 self.check_properties(properties) 

76 self.check_arguments() 

77 

78 @launchlogger 

79 def launch(self) -> int: 

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

81 

82 # Setup Biobb 

83 if self.check_restart(): 

84 return 0 

85 self.stage_files() 

86 

87 self.cmd = [ 

88 self.binary_path, 

89 "-i", 

90 self.stage_io_dict["in"]["input_pdb_path"], 

91 "-o", 

92 self.stage_io_dict["out"]["output_pdb_path"], 

93 "--force_save", 

94 "--non_interactive", 

95 "altloc", 

96 "--select", 

97 ] 

98 

99 if self.altlocs: 

100 self.cmd.append(",".join(self.altlocs)) 

101 else: 

102 self.cmd.append("occupancy") 

103 

104 if self.modeller_key: 

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

106 self.cmd.insert(1, "--modeller_key") 

107 

108 # Run Biobb block 

109 self.run_biobb() 

110 

111 # Copy files to host 

112 self.copy_to_host() 

113 

114 # Remove temporal files 

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

116 self.remove_tmp_files() 

117 

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

119 return self.return_code 

120 

121 

122def fix_altlocs( 

123 input_pdb_path: str, 

124 output_pdb_path: str, 

125 properties: Optional[dict] = None, 

126 **kwargs, 

127) -> int: 

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

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

130 return FixAltLocs( 

131 input_pdb_path=input_pdb_path, 

132 output_pdb_path=output_pdb_path, 

133 properties=properties, 

134 **kwargs, 

135 ).launch() 

136 

137 fix_altlocs.__doc__ = FixAltLocs.__doc__ 

138 

139 

140def main(): 

141 parser = argparse.ArgumentParser( 

142 description="Fix alternate locations from residues", 

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

144 ) 

145 parser.add_argument( 

146 "-c", 

147 "--config", 

148 required=False, 

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

150 ) 

151 

152 # Specific args of each building block 

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

154 required_args.add_argument( 

155 "-i", "--input_pdb_path", required=True, help="Input PDB file name" 

156 ) 

157 required_args.add_argument( 

158 "-o", "--output_pdb_path", required=True, help="Output PDB file name" 

159 ) 

160 

161 args = parser.parse_args() 

162 config = args.config if args.config else None 

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

164 

165 # Specific call of each building block 

166 fix_altlocs( 

167 input_pdb_path=args.input_pdb_path, 

168 output_pdb_path=args.output_pdb_path, 

169 properties=properties, 

170 ) 

171 

172 

173if __name__ == "__main__": 

174 main()