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

64 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 AcpypeParamsAC 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 

8import os 

9 

10 

11class AcpypeParamsAC(BiobbObject): 

12 """ 

13 | biobb_chemistry AcpypeParamsAC 

14 | This class is a wrapper of `Acpype <https://github.com/alanwilter/acpype>`_ tool for small molecule parameterization for AMBER MD package. 

15 | Generation of topologies for Antechamber. 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_frcmod (str): Path to the FRCMOD output file. File type: output. `Sample file <https://github.com/bioexcel/biobb_chemistry/raw/master/biobb_chemistry/test/reference/acpype/ref_acpype.ac.frcmod>`_. Accepted formats: frcmod (edam:format_3888). 

20 output_path_inpcrd (str): Path to the INPCRD output file. File type: output. `Sample file <https://github.com/bioexcel/biobb_chemistry/raw/master/biobb_chemistry/test/reference/acpype/ref_acpype.ac.inpcrd>`_. Accepted formats: inpcrd (edam:format_3878). 

21 output_path_lib (str): Path to the LIB output file. File type: output. `Sample file <https://github.com/bioexcel/biobb_chemistry/raw/master/biobb_chemistry/test/reference/acpype/ref_acpype.ac.lib>`_. Accepted formats: lib (edam:format_3889). 

22 output_path_prmtop (str): Path to the PRMTOP output file. File type: output. `Sample file <https://github.com/bioexcel/biobb_chemistry/raw/master/biobb_chemistry/test/reference/acpype/ref_acpype.ac.prmtop>`_. Accepted formats: prmtop (edam:format_3881). 

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_ac import acpype_params_ac 

41 prop = { 

42 'basename': 'BBB', 

43 'charge': 0 

44 } 

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

46 output_path_frcmod='/path/to/newFRCMOD.frcmod', 

47 output_path_inpcrd='/path/to/newINPCRD.inpcrd', 

48 output_path_lib='/path/to/newLIB.lib', 

49 output_path_prmtop='/path/to/newPRMTOP.prmtop', 

50 properties=prop) 

51 

52 Info: 

53 * wrapped_software: 

54 * name: Acpype 

55 * version: 2019.10.05.12.26 

56 * license: GNU 

57 * ontology: 

58 * name: EDAM 

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

60 

61 """ 

62 

63 def __init__(self, input_path, output_path_frcmod, output_path_inpcrd, output_path_lib, output_path_prmtop, 

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

65 properties = properties or {} 

66 

67 # Call parent class constructor 

68 super().__init__(properties) 

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

70 

71 # Input/Output files 

72 self.io_dict = { 

73 "in": {"input_path": input_path}, 

74 "out": {"output_path_frcmod": output_path_frcmod, "output_path_inpcrd": output_path_inpcrd, "output_path_lib": output_path_lib, "output_path_prmtop": output_path_prmtop} 

75 } 

76 

77 # Properties specific for BB 

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

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

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

81 self.properties = properties 

82 

83 # Check the properties 

84 self.check_properties(properties) 

85 self.check_arguments() 

86 

87 def check_data_params(self, out_log, err_log): 

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

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

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

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

92 self.io_dict["out"]["output_path_lib"] = check_output_path(self.io_dict["out"]["output_path_lib"], 'lib', out_log, self.__class__.__name__) 

93 self.io_dict["out"]["output_path_prmtop"] = check_output_path(self.io_dict["out"]["output_path_prmtop"], 'prmtop', out_log, self.__class__.__name__) 

94 self.output_files = { 

95 'frcmod': self.io_dict["out"]["output_path_frcmod"], 

96 'inpcrd': self.io_dict["out"]["output_path_inpcrd"], 

97 'lib': self.io_dict["out"]["output_path_lib"], 

98 'prmtop': self.io_dict["out"]["output_path_prmtop"], 

99 } 

100 

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

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

103 instructions_list = [] 

104 

105 # generating output path 

106 if self.container_path: 

107 # instructions_list.append('cd ' + self.container_volume_path + ';') 

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

109 self.container_working_dir = self.container_volume_path 

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

111 instructions_list.append('cd ' + self.container_volume_path + ' &&') 

112 else: 

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

114 

115 # executable path 

116 instructions_list.append(self.binary_path) 

117 

118 # generating input 

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

120 instructions_list.append(ipath) 

121 

122 basename = '-b ' + out_pth 

123 instructions_list.append(basename) 

124 

125 # adding charge if not none 

126 charge = get_charge(self.charge, out_log) 

127 if charge: 

128 charge = '-n ' + charge 

129 instructions_list.append(charge) 

130 

131 return instructions_list 

132 

133 @launchlogger 

134 def launch(self) -> int: 

135 """Execute the :class:`AcpypeParamsAC <acpype.acpype_params_ac.AcpypeParamsAC>` acpype.acpype_params_ac.AcpypeParamsAC object.""" 

136 

137 # check input/output paths and parameters 

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

139 

140 # Setup Biobb 

141 if self.check_restart(): 

142 return 0 

143 self.stage_files() 

144 

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

146 self.unique_name = create_unique_name(6) 

147 

148 # create command line instruction 

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

150 

151 # Run Biobb block 

152 self.run_biobb() 

153 

154 # Copy files to host 

155 self.copy_to_host() 

156 

157 # move files to output_path and removes temporary folder 

158 if self.container_path: 

159 process_output(self.unique_name, 

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

161 self.basename, 

162 get_default_value(self.__class__.__name__), 

163 self.output_files, self.out_log) 

164 else: 

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

166 process_output(self.unique_name, 

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

168 self.basename, 

169 get_default_value(self.__class__.__name__), 

170 self.output_files, self.out_log) 

171 

172 self.remove_tmp_files() 

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

174 

175 return self.return_code 

176 

177 

178def acpype_params_ac(input_path: str, output_path_frcmod: str, output_path_inpcrd: str, output_path_lib: str, output_path_prmtop: str, properties: Optional[dict] = None, **kwargs) -> int: 

179 """Create the :class:`AcpypeParamsAC <acpype.acpype_params_ac.AcpypeParamsAC>` class and 

180 execute the :meth:`launch() <acpype.acpype_params_ac.AcpypeParamsAC.launch>` method.""" 

181 return AcpypeParamsAC(**dict(locals())).launch() 

182 

183 

184acpype_params_ac.__doc__ = AcpypeParamsAC.__doc__ 

185main = AcpypeParamsAC.get_main(acpype_params_ac, "Small molecule parameterization for AMBER MD package.") 

186 

187if __name__ == '__main__': 

188 main()