Coverage for biobb_chemistry/acpype/acpype_params_gmx_opls.py: 79%

71 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 AcpypeParamsGMXOPLS 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_gmx 

10 

11 

12class AcpypeParamsGMXOPLS(BiobbObject): 

13 """ 

14 | biobb_chemistry AcpypeParamsGMXOPLS 

15 | This class is a wrapper of `Acpype <https://github.com/alanwilter/acpype>`_ tool for generation of topologies for OPLS/AA. 

16 | Generation of topologies for OPLS/AA. 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_itp (str): Path to the ITP output file. File type: output. `Sample file <https://github.com/bioexcel/biobb_chemistry/raw/master/biobb_chemistry/test/reference/acpype/ref_acpype.gmx.opls.itp>`_. Accepted formats: itp (edam:format_3883). 

21 output_path_top (str): Path to the TOP output file. File type: output. `Sample file <https://github.com/bioexcel/biobb_chemistry/raw/master/biobb_chemistry/test/reference/acpype/ref_acpype.gmx.opls.top>`_. Accepted formats: top (edam:format_3880). 

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

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

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

25 * **binary_path** (*str*) - ("acpype") Path to the acpype executable binary. 

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

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

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

29 * **container_path** (*str*) - (None) Container path definition. 

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

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

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

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

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

35 

36 Examples: 

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

38 

39 from biobb_chemistry.acpype.acpype_params_gmx_opls import acpype_params_gmx_opls 

40 prop = { 

41 'basename': 'BBB', 

42 'charge': 0 

43 } 

44 acpype_params_gmx_opls(input_path='/path/to/myStructure.mol2', 

45 output_path_itp='/path/to/newITP.itp', 

46 output_path_top='/path/to/newTOP.top', 

47 properties=prop) 

48 

49 Info: 

50 * wrapped_software: 

51 * name: Acpype 

52 * version: 2019.10.05.12.26 

53 * license: GNU 

54 * ontology: 

55 * name: EDAM 

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

57 

58 """ 

59 

60 def __init__(self, input_path, output_path_itp, output_path_top, 

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

62 properties = properties or {} 

63 

64 # Call parent class constructor 

65 super().__init__(properties) 

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

67 

68 # Input/Output files 

69 self.io_dict = { 

70 "in": {"input_path": input_path}, 

71 "out": {"output_path_itp": output_path_itp, "output_path_top": output_path_top} 

72 } 

73 

74 # Properties specific for BB 

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

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

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

78 self.properties = properties 

79 

80 # Check the properties 

81 self.check_properties(properties) 

82 self.check_arguments() 

83 

84 def check_data_params(self, out_log, err_log): 

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

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

87 self.io_dict["out"]["output_path_itp"] = check_output_path(self.io_dict["out"]["output_path_itp"], 'itp', out_log, self.__class__.__name__) 

88 self.io_dict["out"]["output_path_top"] = check_output_path(self.io_dict["out"]["output_path_top"], 'top', out_log, self.__class__.__name__) 

89 self.output_files = { 

90 'itp': self.io_dict["out"]["output_path_itp"], 

91 'top': self.io_dict["out"]["output_path_top"], 

92 } 

93 

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

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

96 instructions_list = [] 

97 

98 # generating output path 

99 if self.container_path: 

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

101 else: 

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

103 

104 # executable path 

105 instructions_list.append(self.binary_path) 

106 

107 # generating input 

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

109 instructions_list.append(ipath) 

110 

111 # generating output 

112 basename = '-b ' + out_pth 

113 instructions_list.append(basename) 

114 

115 # adding charge if not none 

116 charge = get_charge(self.charge, out_log) 

117 if charge: 

118 charge = '-n ' + charge 

119 instructions_list.append(charge) 

120 

121 return instructions_list 

122 

123 @launchlogger 

124 def launch(self) -> int: 

125 """Execute the :class:`AcpypeParamsGMXOPLS <acpype.acpype_params_gmx_opls.AcpypeParamsGMXOPLS>` acpype.acpype_params_gmx_opls.AcpypeParamsGMXOPLS object.""" 

126 

127 # check input/output paths and parameters 

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

129 

130 # Setup Biobb 

131 if self.check_restart(): 

132 return 0 

133 self.stage_files() 

134 

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

136 self.unique_name = create_unique_name(6) 

137 

138 # create command line instruction 

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

140 

141 # Run Biobb block 

142 self.run_biobb() 

143 

144 # Copy files to host 

145 self.copy_to_host() 

146 

147 # move files to output_path and removes temporary folder 

148 if self.container_path: 

149 process_output_gmx(self.unique_name, 

150 # self.stage_io_dict['unique_dir'], 

151 self.remove_tmp, 

152 self.basename, 

153 get_default_value(self.__class__.__name__), 

154 self.output_files, self.out_log) 

155 else: 

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

157 process_output_gmx(self.unique_name, 

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

159 self.remove_tmp, 

160 self.basename, 

161 get_default_value(self.__class__.__name__), 

162 self.output_files, self.out_log) 

163 

164 self.remove_tmp_files() 

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

166 

167 return self.return_code 

168 

169 

170def acpype_params_gmx_opls(input_path: str, output_path_itp: str, output_path_top: str, properties: Optional[dict] = None, **kwargs) -> int: 

171 """Execute the :class:`AcpypeParamsGMXOPLS <acpype.acpype_params_gmx_opls.AcpypeParamsGMXOPLS>` class and 

172 execute the :meth:`launch() <acpype.acpype_params_gmx_opls.AcpypeParamsGMXOPLS.launch>` method.""" 

173 

174 return AcpypeParamsGMXOPLS(input_path=input_path, 

175 output_path_itp=output_path_itp, 

176 output_path_top=output_path_top, 

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

178 

179 acpype_params_gmx_opls.__doc__ = AcpypeParamsGMXOPLS.__doc__ 

180 

181 

182def main(): 

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

184 parser = argparse.ArgumentParser(description="Small molecule parameterization for OPLS/AA MD package.", formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

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

186 

187 # Specific args of each building block 

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

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

190 required_args.add_argument('--output_path_itp', required=True, help='Path to the ITP output file. Accepted formats: itp.') 

191 required_args.add_argument('--output_path_top', required=True, help='Path to the TOP output file. Accepted formats: top.') 

192 

193 args = parser.parse_args() 

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

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

196 

197 # Specific call of each building block 

198 acpype_params_gmx_opls(input_path=args.input_path, 

199 output_path_itp=args.output_path_itp, 

200 output_path_top=args.output_path_top, 

201 properties=properties) 

202 

203 

204if __name__ == '__main__': 

205 main()