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

62 statements  

« prev     ^ index     » next       coverage.py v7.14.0, created at 2026-05-13 15:13 +0000

1#!/usr/bin/env python3 

2 

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

4from typing import Optional 

5from biobb_common.generic.biobb_object import BiobbObject 

6from biobb_common.tools.file_utils import launchlogger 

7from 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 

8import os 

9 

10 

11class AcpypeParamsGMX(BiobbObject): 

12 """ 

13 | biobb_chemistry AcpypeParamsGMX 

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

15 | 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>`_. 

16 

17 Args: 

18 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). 

19 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). 

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.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.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 import acpype_params_gmx 

40 prop = { 

41 'basename': 'BBB', 

42 'charge': 0 

43 } 

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

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

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

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

48 properties=prop) 

49 

50 Info: 

51 * wrapped_software: 

52 * name: Acpype 

53 * version: 2019.10.05.12.26 

54 * license: GNU 

55 * ontology: 

56 * name: EDAM 

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

58 

59 """ 

60 

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

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

63 properties = properties or {} 

64 

65 # Call parent class constructor 

66 super().__init__(properties) 

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

68 

69 # Input/Output files 

70 self.io_dict = { 

71 "in": {"input_path": input_path}, 

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

73 } 

74 

75 # Properties specific for BB 

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

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

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

79 self.properties = properties 

80 

81 # Check the properties 

82 self.check_properties(properties) 

83 self.check_arguments() 

84 

85 def check_data_params(self, out_log, err_log): 

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

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

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

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

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

91 self.output_files = { 

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

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

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

95 } 

96 

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

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

99 instructions_list = [] 

100 

101 # generating output path 

102 if self.container_path: 

103 self.container_working_dir = self.container_volume_path 

104 out_pth = 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 os.path.join(self.stage_io_dict['unique_dir'], self.basename + "." + self.unique_name + ".amb2gmx"), 

155 self.basename, 

156 get_default_value(self.__class__.__name__), 

157 self.output_files, self.out_log) 

158 else: 

159 self.tmp_files.append(self.basename + "." + self.unique_name + ".acpype") 

160 process_output_gmx(self.unique_name, 

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

162 self.basename, 

163 get_default_value(self.__class__.__name__), 

164 self.output_files, self.out_log) 

165 

166 self.remove_tmp_files() 

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

168 

169 return self.return_code 

170 

171 

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

173 """Create the :class:`AcpypeParamsGMX <acpype.acpype_params_gmx.AcpypeParamsGMX>` class and 

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

175 return AcpypeParamsGMX(**dict(locals())).launch() 

176 

177 

178acpype_params_gmx.__doc__ = AcpypeParamsGMX.__doc__ 

179main = AcpypeParamsGMX.get_main(acpype_params_gmx, "Small molecule parameterization for GROMACS MD package.") 

180 

181if __name__ == '__main__': 

182 main()