Coverage for biobb_chemistry/acpype/acpype_params_gmx.py: 78%

73 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 AcpypeParamsGMX 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 AcpypeParamsGMX(BiobbObject): 

13 """ 

14 | biobb_chemistry AcpypeParamsGMX 

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

16 | Generation of topologies for GROMACS. 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_gro (str): Path to the GRO output file. File type: output. `Sample file <https://github.com/bioexcel/biobb_chemistry/raw/master/biobb_chemistry/test/reference/acpype/ref_acpype.gmx.gro>`_. Accepted formats: gro (edam:format_2033). 

21 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.itp>`_. Accepted formats: itp (edam:format_3883). 

22 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.top>`_. Accepted formats: top (edam:format_3880). 

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

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

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

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

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

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

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

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

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

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

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

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

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

36 

37 Examples: 

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

39 

40 from biobb_chemistry.acpype.acpype_params_gmx import acpype_params_gmx 

41 prop = { 

42 'basename': 'BBB', 

43 'charge': 0 

44 } 

45 acpype_params_gmx(input_path='/path/to/myStructure.mol2', 

46 output_path_gro='/path/to/newGRO.gro', 

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

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

49 properties=prop) 

50 

51 Info: 

52 * wrapped_software: 

53 * name: Acpype 

54 * version: 2019.10.05.12.26 

55 * license: GNU 

56 * ontology: 

57 * name: EDAM 

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

59 

60 """ 

61 

62 def __init__(self, input_path, output_path_gro, output_path_itp, output_path_top, 

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

64 properties = properties or {} 

65 

66 # Call parent class constructor 

67 super().__init__(properties) 

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

69 

70 # Input/Output files 

71 self.io_dict = { 

72 "in": {"input_path": input_path}, 

73 "out": {"output_path_gro": output_path_gro, "output_path_itp": output_path_itp, "output_path_top": output_path_top} 

74 } 

75 

76 # Properties specific for BB 

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

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

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

80 self.properties = properties 

81 

82 # Check the properties 

83 self.check_properties(properties) 

84 self.check_arguments() 

85 

86 def check_data_params(self, out_log, err_log): 

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

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

89 self.io_dict["out"]["output_path_gro"] = check_output_path(self.io_dict["out"]["output_path_gro"], 'gro', out_log, self.__class__.__name__) 

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

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

92 self.output_files = { 

93 'gro': self.io_dict["out"]["output_path_gro"], 

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

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

96 } 

97 

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

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

100 instructions_list = [] 

101 

102 # generating output path 

103 if self.container_path: 

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

105 else: 

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

107 

108 # executable path 

109 instructions_list.append(self.binary_path) 

110 

111 # generating input 

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

113 instructions_list.append(ipath) 

114 

115 # generating output 

116 basename = '-b ' + out_pth 

117 instructions_list.append(basename) 

118 

119 # adding charge if not None 

120 charge = get_charge(self.charge, out_log) 

121 if charge: 

122 charge = '-n ' + charge 

123 instructions_list.append(charge) 

124 

125 return instructions_list 

126 

127 @launchlogger 

128 def launch(self) -> int: 

129 """Execute the :class:`AcpypeParamsGMX <acpype.acpype_params_gmx.AcpypeParamsGMX>` acpype.acpype_params_gmx.AcpypeParamsGMX object.""" 

130 

131 # check input/output paths and parameters 

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

133 

134 # Setup Biobb 

135 if self.check_restart(): 

136 return 0 

137 self.stage_files() 

138 

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

140 self.unique_name = create_unique_name(6) 

141 

142 # create command line instruction 

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

144 

145 # Run Biobb block 

146 self.run_biobb() 

147 

148 # Copy files to host 

149 self.copy_to_host() 

150 

151 # move files to output_path and removes temporary folder 

152 if self.container_path: 

153 process_output_gmx(self.unique_name, 

154 # self.stage_io_dict['unique_dir'], 

155 self.remove_tmp, 

156 self.basename, 

157 get_default_value(self.__class__.__name__), 

158 self.output_files, self.out_log) 

159 else: 

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

161 process_output_gmx(self.unique_name, 

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

163 self.remove_tmp, 

164 self.basename, 

165 get_default_value(self.__class__.__name__), 

166 self.output_files, self.out_log) 

167 

168 self.remove_tmp_files() 

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

170 

171 return self.return_code 

172 

173 

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

175 """Execute the :class:`AcpypeParamsGMX <acpype.acpype_params_gmx.AcpypeParamsGMX>` class and 

176 execute the :meth:`launch() <acpype.acpype_params_gmx.AcpypeParamsGMX.launch>` method.""" 

177 

178 return AcpypeParamsGMX(input_path=input_path, 

179 output_path_gro=output_path_gro, 

180 output_path_itp=output_path_itp, 

181 output_path_top=output_path_top, 

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

183 

184 acpype_params_gmx.__doc__ = AcpypeParamsGMX.__doc__ 

185 

186 

187def main(): 

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

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

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

191 

192 # Specific args of each building block 

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

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

195 required_args.add_argument('--output_path_gro', required=True, help='Path to the GRO output file. Accepted formats: gro.') 

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

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

198 

199 args = parser.parse_args() 

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

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

202 

203 # Specific call of each building block 

204 acpype_params_gmx(input_path=args.input_path, 

205 output_path_gro=args.output_path_gro, 

206 output_path_itp=args.output_path_itp, 

207 output_path_top=args.output_path_top, 

208 properties=properties) 

209 

210 

211if __name__ == '__main__': 

212 main()