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

58 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-06-01 16:02 +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 instructions_list.append('cd ' + self.container_volume_path + ' &&') 

100 else: 

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

102 

103 # executable path 

104 instructions_list.append(self.binary_path) 

105 

106 # generating inputs 

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

108 instructions_list.append(crdpath) 

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

110 instructions_list.append(prmtopath) 

111 

112 # generating output 

113 basename = '-b ' + out_pth 

114 instructions_list.append(basename) 

115 

116 return instructions_list 

117 

118 @launchlogger 

119 def launch(self) -> int: 

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

121 

122 # check input/output paths and parameters 

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

124 

125 # Setup Biobb 

126 if self.check_restart(): 

127 return 0 

128 self.stage_files() 

129 

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

131 self.unique_name = create_unique_name(6) 

132 

133 # create command line instruction 

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

135 

136 # Run Biobb block 

137 self.run_biobb() 

138 

139 # Copy files to host 

140 self.copy_to_host() 

141 

142 # move files to output_path and removes temporary folder 

143 if self.container_path: 

144 process_output_gmx(self.unique_name, 

145 os.path.join(self.stage_io_dict['unique_dir'], self.basename + "." + self.unique_name + ".amb2gmx"), 

146 self.basename, 

147 get_default_value(self.__class__.__name__), 

148 self.output_files, self.out_log) 

149 else: 

150 self.tmp_files.append(self.basename + "." + self.unique_name + ".amb2gmx") 

151 process_output_gmx(self.unique_name, 

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

153 self.basename, 

154 get_default_value(self.__class__.__name__), 

155 self.output_files, self.out_log) 

156 

157 self.remove_tmp_files() 

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 """Create 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 return AcpypeConvertAMBERtoGMX(**dict(locals())).launch() 

167 

168 

169acpype_convert_amber_to_gmx.__doc__ = AcpypeConvertAMBERtoGMX.__doc__ 

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

171 

172if __name__ == '__main__': 

173 main()