Coverage for biobb_chemistry/acpype/acpype_params_ac.py: 77%

75 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-03-12 09:28 +0000

1#!/usr/bin/env python3 

2 

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

4import argparse 

5from typing import Optional 

6from biobb_common.generic.biobb_object import BiobbObject 

7from biobb_common.configuration import settings 

8from biobb_common.tools.file_utils import launchlogger 

9from biobb_chemistry.acpype.common import get_binary_path, check_input_path, check_output_path, get_basename, get_charge, create_unique_name, get_default_value, process_output 

10 

11 

12class AcpypeParamsAC(BiobbObject): 

13 """ 

14 | biobb_chemistry AcpypeParamsAC 

15 | This class is a wrapper of `Acpype <https://github.com/alanwilter/acpype>`_ tool for small molecule parameterization for AMBER MD package. 

16 | Generation of topologies for Antechamber. Acpype is a tool based in Python to use Antechamber to generate topologies for chemical compounds and to interface with others python applications like CCPN or ARIA. `Visit the official page <https://github.com/alanwilter/acpype>`_. 

17 

18 Args: 

19 input_path (str): Path to the input file. File type: input. `Sample file <https://github.com/bioexcel/biobb_chemistry/raw/master/biobb_chemistry/test/data/acpype/acpype.params.mol2>`_. Accepted formats: pdb (edam:format_1476), mdl (edam:format_3815), mol2 (edam:format_3816). 

20 output_path_frcmod (str): Path to the FRCMOD output file. File type: output. `Sample file <https://github.com/bioexcel/biobb_chemistry/raw/master/biobb_chemistry/test/reference/acpype/ref_acpype.ac.frcmod>`_. Accepted formats: frcmod (edam:format_3888). 

21 output_path_inpcrd (str): Path to the INPCRD output file. File type: output. `Sample file <https://github.com/bioexcel/biobb_chemistry/raw/master/biobb_chemistry/test/reference/acpype/ref_acpype.ac.inpcrd>`_. Accepted formats: inpcrd (edam:format_3878). 

22 output_path_lib (str): Path to the LIB output file. File type: output. `Sample file <https://github.com/bioexcel/biobb_chemistry/raw/master/biobb_chemistry/test/reference/acpype/ref_acpype.ac.lib>`_. Accepted formats: lib (edam:format_3889). 

23 output_path_prmtop (str): Path to the PRMTOP output file. File type: output. `Sample file <https://github.com/bioexcel/biobb_chemistry/raw/master/biobb_chemistry/test/reference/acpype/ref_acpype.ac.prmtop>`_. Accepted formats: prmtop (edam:format_3881). 

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

25 * **basename** (*str*) - ("BBB") A basename for the project (folder and output files). 

26 * **charge** (*int*) - (0) [-20~20|1] Net molecular charge, for gas default is 0. If None the charge is guessed by acpype. 

27 * **binary_path** (*str*) - ("acpype") Path to the acpype 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 * **container_path** (*str*) - (None) Container path definition. 

32 * **container_image** (*str*) - ('acpype/acpype:2022.7.21') Container image definition. 

33 * **container_volume_path** (*str*) - ('/tmp') Container volume path definition. 

34 * **container_working_dir** (*str*) - (None) Container working directory definition. 

35 * **container_user_id** (*str*) - (None) Container user_id definition. 

36 * **container_shell_path** (*str*) - ('/bin/bash') Path to default shell inside the container. 

37 

38 Examples: 

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

40 

41 from biobb_chemistry.acpype.acpype_params_ac import acpype_params_ac 

42 prop = { 

43 'basename': 'BBB', 

44 'charge': 0 

45 } 

46 acpype_params_ac(input_path='/path/to/myStructure.mol2', 

47 output_path_frcmod='/path/to/newFRCMOD.frcmod', 

48 output_path_inpcrd='/path/to/newINPCRD.inpcrd', 

49 output_path_lib='/path/to/newLIB.lib', 

50 output_path_prmtop='/path/to/newPRMTOP.prmtop', 

51 properties=prop) 

52 

53 Info: 

54 * wrapped_software: 

55 * name: Acpype 

56 * version: 2019.10.05.12.26 

57 * license: GNU 

58 * ontology: 

59 * name: EDAM 

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

61 

62 """ 

63 

64 def __init__(self, input_path, output_path_frcmod, output_path_inpcrd, output_path_lib, output_path_prmtop, 

65 properties=None, **kwargs) -> None: 

66 properties = properties or {} 

67 

68 # Call parent class constructor 

69 super().__init__(properties) 

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

71 

72 # Input/Output files 

73 self.io_dict = { 

74 "in": {"input_path": input_path}, 

75 "out": {"output_path_frcmod": output_path_frcmod, "output_path_inpcrd": output_path_inpcrd, "output_path_lib": output_path_lib, "output_path_prmtop": output_path_prmtop} 

76 } 

77 

78 # Properties specific for BB 

79 self.basename = properties.get('basename', 'BBB') 

80 self.charge = properties.get('charge', '') 

81 self.binary_path = get_binary_path(properties, 'binary_path') 

82 self.properties = properties 

83 

84 # Check the properties 

85 self.check_properties(properties) 

86 self.check_arguments() 

87 

88 def check_data_params(self, out_log, err_log): 

89 """ Checks all the input/output paths and parameters """ 

90 self.io_dict["in"]["input_path"] = check_input_path(self.io_dict["in"]["input_path"], out_log, self.__class__.__name__) 

91 self.io_dict["out"]["output_path_frcmod"] = check_output_path(self.io_dict["out"]["output_path_frcmod"], 'frcmod', out_log, self.__class__.__name__) 

92 self.io_dict["out"]["output_path_inpcrd"] = check_output_path(self.io_dict["out"]["output_path_inpcrd"], 'inpcrd', out_log, self.__class__.__name__) 

93 self.io_dict["out"]["output_path_lib"] = check_output_path(self.io_dict["out"]["output_path_lib"], 'lib', out_log, self.__class__.__name__) 

94 self.io_dict["out"]["output_path_prmtop"] = check_output_path(self.io_dict["out"]["output_path_prmtop"], 'prmtop', out_log, self.__class__.__name__) 

95 self.output_files = { 

96 'frcmod': self.io_dict["out"]["output_path_frcmod"], 

97 'inpcrd': self.io_dict["out"]["output_path_inpcrd"], 

98 'lib': self.io_dict["out"]["output_path_lib"], 

99 'prmtop': self.io_dict["out"]["output_path_prmtop"], 

100 } 

101 

102 def create_cmd(self, container_io_dict, out_log, err_log): 

103 """Creates the command line instruction using the properties file settings""" 

104 instructions_list = [] 

105 

106 # generating output path 

107 if self.container_path: 

108 # instructions_list.append('cd ' + self.container_volume_path + ';') 

109 out_pth = self.container_volume_path + '/' + get_basename(self.basename, out_log) + '.' + self.unique_name 

110 else: 

111 out_pth = get_basename(self.basename, out_log) + '.' + self.unique_name 

112 

113 # executable path 

114 instructions_list.append(self.binary_path) 

115 

116 # generating input 

117 ipath = '-i ' + container_io_dict["in"]["input_path"] 

118 instructions_list.append(ipath) 

119 

120 basename = '-b ' + out_pth 

121 instructions_list.append(basename) 

122 

123 # adding charge if not none 

124 charge = get_charge(self.charge, out_log) 

125 if charge: 

126 charge = '-n ' + charge 

127 instructions_list.append(charge) 

128 

129 return instructions_list 

130 

131 @launchlogger 

132 def launch(self) -> int: 

133 """Execute the :class:`AcpypeParamsAC <acpype.acpype_params_ac.AcpypeParamsAC>` acpype.acpype_params_ac.AcpypeParamsAC object.""" 

134 

135 # check input/output paths and parameters 

136 self.check_data_params(self.out_log, self.err_log) 

137 

138 # Setup Biobb 

139 if self.check_restart(): 

140 return 0 

141 self.stage_files() 

142 

143 # create unique name for temporary folder (created by acpype) 

144 self.unique_name = create_unique_name(6) 

145 

146 # create command line instruction 

147 self.cmd = self.create_cmd(self.stage_io_dict, self.out_log, self.err_log) 

148 

149 # Run Biobb block 

150 self.run_biobb() 

151 

152 # Copy files to host 

153 self.copy_to_host() 

154 

155 # move files to output_path and removes temporary folder 

156 if self.container_path: 

157 process_output(self.unique_name, 

158 # self.stage_io_dict['unique_dir'], 

159 self.remove_tmp, 

160 self.basename, 

161 get_default_value(self.__class__.__name__), 

162 self.output_files, self.out_log) 

163 else: 

164 self.tmp_files.extend([self.basename + "." + self.unique_name + ".acpype"]) 

165 process_output(self.unique_name, 

166 self.basename + "." + self.unique_name + ".acpype", 

167 self.remove_tmp, 

168 self.basename, 

169 get_default_value(self.__class__.__name__), 

170 self.output_files, self.out_log) 

171 

172 self.remove_tmp_files() 

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

174 

175 return self.return_code 

176 

177 

178def acpype_params_ac(input_path: str, output_path_frcmod: str, output_path_inpcrd: str, output_path_lib: str, output_path_prmtop: str, properties: Optional[dict] = None, **kwargs) -> int: 

179 """Execute the :class:`AcpypeParamsAC <acpype.acpype_params_ac.AcpypeParamsAC>` class and 

180 execute the :meth:`launch() <acpype.acpype_params_ac.AcpypeParamsAC.launch>` method.""" 

181 

182 return AcpypeParamsAC(input_path=input_path, 

183 output_path_frcmod=output_path_frcmod, 

184 output_path_inpcrd=output_path_inpcrd, 

185 output_path_lib=output_path_lib, 

186 output_path_prmtop=output_path_prmtop, 

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

188 

189 acpype_params_ac.__doc__ = AcpypeParamsAC.__doc__ 

190 

191 

192def main(): 

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

194 parser = argparse.ArgumentParser(description="Small molecule parameterization for AMBER MD package.", formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

195 parser.add_argument('--config', required=False, help='Configuration file') 

196 

197 # Specific args of each building block 

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

199 required_args.add_argument('--input_path', required=True, help='Path to the input file. Accepted formats: pdb, mdl, mol2.') 

200 required_args.add_argument('--output_path_frcmod', required=True, help='Path to the FRCMOD output file. Accepted formats: frcmod.') 

201 required_args.add_argument('--output_path_inpcrd', required=True, help='Path to the INPCRD output file. Accepted formats: inpcrd.') 

202 required_args.add_argument('--output_path_lib', required=True, help='Path to the LIB output file. Accepted formats: lib.') 

203 required_args.add_argument('--output_path_prmtop', required=True, help='Path to the PRMTOP output file. Accepted formats: prmtop.') 

204 

205 args = parser.parse_args() 

206 args.config = args.config or "{}" 

207 properties = settings.ConfReader(config=args.config).get_prop_dic() 

208 

209 # Specific call of each building block 

210 acpype_params_ac(input_path=args.input_path, 

211 output_path_frcmod=args.output_path_frcmod, 

212 output_path_inpcrd=args.output_path_inpcrd, 

213 output_path_lib=args.output_path_lib, 

214 output_path_prmtop=args.output_path_prmtop, 

215 properties=properties) 

216 

217 

218if __name__ == '__main__': 

219 main()