Coverage for biobb_chemistry/acpype/acpype_convert_amber_to_gmx.py: 76%

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

4import argparse 

5from biobb_common.generic.biobb_object import BiobbObject 

6from biobb_common.configuration import settings 

7from biobb_common.tools.file_utils import launchlogger 

8from biobb_chemistry.acpype.common import get_binary_path, check_output_path, get_basename, create_unique_name, get_default_value, process_output_gmx 

9from typing import Optional 

10 

11 

12class AcpypeConvertAMBERtoGMX(BiobbObject): 

13 """ 

14 | biobb_chemistry AcpypeConvertAMBERtoGMX 

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

16 | 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_crd_path (str): Path to the input coordinates file (AMBER crd). File type: input. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_chemistry/master/biobb_chemistry/test/data/acpype/acpype.coords.inpcrd>`_. Accepted formats: inpcrd (edam:format_3878). 

20 input_top_path (str): Path to the input topology file (AMBER ParmTop). File type: input. `Sample file <https://github.com/bioexcel/biobb_chemistry/raw/master/biobb_chemistry/test/data/acpype/acpype.top.prmtop>`_. Accepted formats: top (edam:format_3881), parmtop (edam:format_3881), prmtop (edam:format_3881). 

21 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.amber2gmx.gro>`_. Accepted formats: gro (edam:format_2033). 

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.amber2gmx.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 * **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 * **container_path** (*str*) - (None) Container path definition. 

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

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

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

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

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

34 

35 Examples: 

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

37 

38 from biobb_chemistry.acpype.acpype_convert_amber_to_gmx import acpype_convert_amber_to_gmx 

39 prop = { 

40 'basename': 'BBB', 

41 } 

42 acpype_convert_amber_to_gmx(input_crd_path='/path/to/myStructure.inpcrd', 

43 input_top_path='/path/to/myStructure.prmtop', 

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

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

46 properties=prop) 

47 

48 Info: 

49 * wrapped_software: 

50 * name: Acpype 

51 * version: 2019.10.05.12.26 

52 * license: GNU 

53 * ontology: 

54 * name: EDAM 

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

56 

57 """ 

58 

59 def __init__(self, input_crd_path, input_top_path, output_path_gro, output_path_top, 

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

61 properties = properties or {} 

62 

63 # Call parent class constructor 

64 super().__init__(properties) 

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

66 

67 # Input/Output files 

68 self.io_dict = { 

69 "in": {"input_crd_path": input_crd_path, "input_top_path": input_top_path}, 

70 "out": {"output_path_gro": output_path_gro, "output_path_top": output_path_top} 

71 } 

72 

73 # Properties specific for BB 

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

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

76 self.properties = properties 

77 

78 # Check the properties 

79 self.check_properties(properties) 

80 self.check_arguments() 

81 

82 def check_data_params(self, out_log, err_log): 

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

84 # NOTE: missing check input paths 

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

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

87 self.output_files = { 

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

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

90 } 

91 

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

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

94 instructions_list = [] 

95 

96 # generating output path 

97 if self.container_path: 

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

99 else: 

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

101 

102 # executable path 

103 instructions_list.append(self.binary_path) 

104 

105 # generating inputs 

106 crdpath = '-x ' + container_io_dict["in"]["input_crd_path"] 

107 instructions_list.append(crdpath) 

108 prmtopath = '-p ' + container_io_dict["in"]["input_top_path"] 

109 instructions_list.append(prmtopath) 

110 

111 # generating output 

112 basename = '-b ' + out_pth 

113 instructions_list.append(basename) 

114 

115 return instructions_list 

116 

117 @launchlogger 

118 def launch(self) -> int: 

119 """Execute the :class:`AcpypeConvertAMBERtoGMX <acpype.acpype_convert_amber_to_gmx.AcpypeConvertAMBERtoGMX>` acpype.acpype_convert_amber_to_gmx.AcpypeConvertAMBERtoGMX object.""" 

120 

121 # check input/output paths and parameters 

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

123 

124 # Setup Biobb 

125 if self.check_restart(): 

126 return 0 

127 self.stage_files() 

128 

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

130 self.unique_name = create_unique_name(6) 

131 

132 # create command line instruction 

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

134 

135 # Run Biobb block 

136 self.run_biobb() 

137 

138 # Copy files to host 

139 self.copy_to_host() 

140 

141 # move files to output_path and removes temporary folder 

142 if self.container_path: 

143 process_output_gmx(self.unique_name, 

144 # self.stage_io_dict['unique_dir'], 

145 self.remove_tmp, 

146 self.basename, 

147 get_default_value(self.__class__.__name__), 

148 self.output_files, self.out_log) 

149 else: 

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

151 process_output_gmx(self.unique_name, 

152 self.basename + "." + self.unique_name + ".amb2gmx", 

153 self.remove_tmp, 

154 self.basename, 

155 get_default_value(self.__class__.__name__), 

156 self.output_files, self.out_log) 

157 

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

159 

160 return self.return_code 

161 

162 

163def acpype_convert_amber_to_gmx(input_crd_path: str, input_top_path: str, output_path_gro: str, output_path_top: str, properties: Optional[dict] = None, **kwargs) -> int: 

164 """Execute the :class:`AcpypeConvertAMBERtoGMX <acpype.acpype_convert_amber_to_gmx.AcpypeConvertAMBERtoGMX>` class and 

165 execute the :meth:`launch() <acpype.acpype_convert_amber_to_gmx.AcpypeConvertAMBERtoGMX.launch>` method.""" 

166 

167 return AcpypeConvertAMBERtoGMX(input_crd_path=input_crd_path, 

168 input_top_path=input_top_path, 

169 output_path_gro=output_path_gro, 

170 output_path_top=output_path_top, 

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

172 

173 acpype_convert_amber_to_gmx.__doc__ = AcpypeConvertAMBERtoGMX.__doc__ 

174 

175 

176def main(): 

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

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

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

180 

181 # Specific args of each building block 

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

183 required_args.add_argument('--input_crd_path', required=True, help='Path to the input coordinates file (AMBER crd). Accepted formats: inpcrd.') 

184 required_args.add_argument('--input_top_path', required=True, help='Path to the input topology file (AMBER ParmTop). Accepted formats: top, parmtop, prmtop.') 

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

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

187 

188 args = parser.parse_args() 

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

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

191 

192 # Specific call of each building block 

193 acpype_convert_amber_to_gmx(input_crd_path=args.input_crd_path, 

194 input_top_path=args.input_top_path, 

195 output_path_gro=args.output_path_gro, 

196 output_path_top=args.output_path_top, 

197 properties=properties) 

198 

199 

200if __name__ == '__main__': 

201 main()