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

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

4from biobb_common.generic.biobb_object import BiobbObject 

5from biobb_common.tools.file_utils import launchlogger 

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

7from typing import Optional 

8import os 

9 

10 

11class AcpypeConvertAMBERtoGMX(BiobbObject): 

12 """ 

13 | biobb_chemistry AcpypeConvertAMBERtoGMX 

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

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

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

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

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.amber2gmx.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 * **binary_path** (*str*) - ("acpype") Path to the acpype executable binary. 

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

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

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

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

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

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

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

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

33 

34 Examples: 

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

36 

37 from biobb_chemistry.acpype.acpype_convert_amber_to_gmx import acpype_convert_amber_to_gmx 

38 prop = { 

39 'basename': 'BBB', 

40 } 

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

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

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

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

45 properties=prop) 

46 

47 Info: 

48 * wrapped_software: 

49 * name: Acpype 

50 * version: 2019.10.05.12.26 

51 * license: GNU 

52 * ontology: 

53 * name: EDAM 

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

55 

56 """ 

57 

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

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

60 properties = properties or {} 

61 

62 # Call parent class constructor 

63 super().__init__(properties) 

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

65 

66 # Input/Output files 

67 self.io_dict = { 

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

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

70 } 

71 

72 # Properties specific for BB 

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

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

75 self.properties = properties 

76 

77 # Check the properties 

78 self.check_properties(properties) 

79 self.check_arguments() 

80 

81 def check_data_params(self, out_log, err_log): 

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

83 # NOTE: missing check input paths 

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

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

86 self.output_files = { 

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

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

89 } 

90 

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

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

93 instructions_list = [] 

94 

95 # generating output path 

96 if self.container_path: 

97 self.container_working_dir = self.container_volume_path 

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

145 self.basename, 

146 get_default_value(self.__class__.__name__), 

147 self.output_files, self.out_log) 

148 else: 

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

150 process_output_gmx(self.unique_name, 

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

152 self.basename, 

153 get_default_value(self.__class__.__name__), 

154 self.output_files, self.out_log) 

155 

156 self.remove_tmp_files() 

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

158 

159 return self.return_code 

160 

161 

162def 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: 

163 """Create the :class:`AcpypeConvertAMBERtoGMX <acpype.acpype_convert_amber_to_gmx.AcpypeConvertAMBERtoGMX>` class and 

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

165 return AcpypeConvertAMBERtoGMX(**dict(locals())).launch() 

166 

167 

168acpype_convert_amber_to_gmx.__doc__ = AcpypeConvertAMBERtoGMX.__doc__ 

169main = AcpypeConvertAMBERtoGMX.get_main(acpype_convert_amber_to_gmx, "Small molecule parameterization for GROMACS MD package.") 

170 

171if __name__ == '__main__': 

172 main()