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

55 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 RemoveLigand class and the command line interface.""" 

4 

5import argparse 

6from pathlib import Path 

7from typing import Optional 

8 

9from biobb_common.configuration import settings 

10from biobb_common.generic.biobb_object import BiobbObject 

11from biobb_common.tools import file_utils as fu 

12from biobb_common.tools.file_utils import launchlogger 

13 

14from biobb_structure_utils.gro_lib.gro import Gro 

15 

16 

17class RemoveLigand(BiobbObject): 

18 """ 

19 | biobb_structure_utils RemoveLigand 

20 | Class to remove the selected ligand atoms from a 3D structure. 

21 | Remove the selected ligand atoms from a 3D structure. 

22 

23 Args: 

24 input_structure_path (str): Input structure file path. File type: input. `Sample file <https://github.com/bioexcel/biobb_structure_utils/raw/master/biobb_structure_utils/test/data/utils/WT_aq4_md_1.pdb>`_. Accepted formats: pdb (edam:format_1476), gro (edam:format_2033). 

25 output_structure_path (str): Output structure file path. File type: output. `Sample file <https://github.com/bioexcel/biobb_structure_utils/raw/master/biobb_structure_utils/test/reference/utils/WT_apo_md_1.pdb>`_. Accepted formats: pdb (edam:format_1476), gro (edam:format_2033). 

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

27 * **ligand** (*str*) - ("AQ4") Residue code of the ligand to be removed. 

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_structure_utils.utils.remove_ligand import remove_ligand 

36 prop = { 

37 'ligand': 'AQ4' 

38 } 

39 remove_ligand(input_structure_path='/path/to/myStructure.pdb', 

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

41 properties=prop) 

42 

43 Info: 

44 * wrapped_software: 

45 * name: In house 

46 * license: Apache-2.0 

47 * ontology: 

48 * name: EDAM 

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

50 

51 """ 

52 

53 def __init__( 

54 self, input_structure_path, output_structure_path, properties=None, **kwargs 

55 ) -> None: 

56 properties = properties or {} 

57 

58 # Call parent class constructor 

59 super().__init__(properties) 

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

61 

62 # Input/Output files 

63 self.io_dict = { 

64 "in": {"input_structure_path": input_structure_path}, 

65 "out": {"output_structure_path": output_structure_path}, 

66 } 

67 

68 # Properties specific for BB 

69 self.ligand = properties.get("ligand", "AQ4") 

70 

71 # Check the properties 

72 self.check_properties(properties) 

73 self.check_arguments() 

74 

75 @launchlogger 

76 def launch(self) -> int: 

77 """Execute the :class:`RemoveLigand <utils.remove_ligand.RemoveLigand>` utils.remove_ligand.RemoveLigand object.""" 

78 

79 # Setup Biobb 

80 if self.check_restart(): 

81 return 0 

82 self.stage_files() 

83 

84 # Business code 

85 extension = Path( 

86 self.stage_io_dict["in"]["input_structure_path"] 

87 ).suffix.lower() 

88 if extension.lower() == ".gro": 

89 fu.log( 

90 "GRO format detected, removing all atoms from residues named %s" 

91 % self.ligand, 

92 self.out_log, 

93 ) 

94 gro_st = Gro() 

95 gro_st.read_gro_file(self.stage_io_dict["in"]["input_structure_path"]) 

96 gro_st.remove_residues([self.ligand]) 

97 gro_st.write_gro_file(self.stage_io_dict["out"]["output_structure_path"]) 

98 

99 else: 

100 fu.log( 

101 "PDB format detected, removing all atoms from residues named %s" 

102 % self.ligand, 

103 self.out_log, 

104 ) 

105 # Direct aproach solution implemented to avoid the issues 

106 # presented in commit message (c92aab9604a6a31d13f4170ff47b231df0a588ef) 

107 # with the Biopython library 

108 with open( 

109 self.stage_io_dict["in"]["input_structure_path"], "r" 

110 ) as input_pdb, open( 

111 self.stage_io_dict["out"]["output_structure_path"], "w" 

112 ) as output_pdb: 

113 for line in input_pdb: 

114 if len(line) > 19 and self.ligand.lower() in line[17:20].lower(): 

115 continue 

116 output_pdb.write(line) 

117 

118 self.return_code = 0 

119 ########## 

120 

121 # Copy files to host 

122 self.copy_to_host() 

123 

124 # Remove temporal files 

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

126 self.remove_tmp_files() 

127 

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

129 

130 return self.return_code 

131 

132 

133def remove_ligand( 

134 input_structure_path: str, 

135 output_structure_path: str, 

136 properties: Optional[dict] = None, 

137 **kwargs, 

138) -> int: 

139 """Execute the :class:`RemoveLigand <utils.remove_ligand.RemoveLigand>` class and 

140 execute the :meth:`launch() <utils.remove_ligand.RemoveLigand.launch>` method.""" 

141 

142 return RemoveLigand( 

143 input_structure_path=input_structure_path, 

144 output_structure_path=output_structure_path, 

145 properties=properties, 

146 **kwargs, 

147 ).launch() 

148 

149 remove_ligand.__doc__ = RemoveLigand.__doc__ 

150 

151 

152def main(): 

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

154 parser = argparse.ArgumentParser( 

155 description="Remove the selected ligand atoms from a 3D structure.", 

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

157 ) 

158 parser.add_argument( 

159 "-c", 

160 "--config", 

161 required=False, 

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

163 ) 

164 

165 # Specific args of each building block 

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

167 required_args.add_argument( 

168 "-i", "--input_structure_path", required=True, help="Input structure file name" 

169 ) 

170 required_args.add_argument( 

171 "-o", 

172 "--output_structure_path", 

173 required=True, 

174 help="Output structure file name", 

175 ) 

176 

177 args = parser.parse_args() 

178 config = args.config if args.config else None 

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

180 

181 # Specific call of each building block 

182 remove_ligand( 

183 input_structure_path=args.input_structure_path, 

184 output_structure_path=args.output_structure_path, 

185 properties=properties, 

186 ) 

187 

188 

189if __name__ == "__main__": 

190 main()