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

61 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2025-12-22 12:49 +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 

8 

9 

10class AcpypeParamsAC(BiobbObject): 

11 """ 

12 | biobb_chemistry AcpypeParamsAC 

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

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

15 

16 Args: 

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

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

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

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

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

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

40 prop = { 

41 'basename': 'BBB', 

42 'charge': 0 

43 } 

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

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

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

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

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

49 properties=prop) 

50 

51 Info: 

52 * wrapped_software: 

53 * name: Acpype 

54 * version: 2019.10.05.12.26 

55 * license: GNU 

56 * ontology: 

57 * name: EDAM 

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

59 

60 """ 

61 

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

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

64 properties = properties or {} 

65 

66 # Call parent class constructor 

67 super().__init__(properties) 

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

69 

70 # Input/Output files 

71 self.io_dict = { 

72 "in": {"input_path": input_path}, 

73 "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} 

74 } 

75 

76 # Properties specific for BB 

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

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

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

80 self.properties = properties 

81 

82 # Check the properties 

83 self.check_properties(properties) 

84 self.check_arguments() 

85 

86 def check_data_params(self, out_log, err_log): 

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

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

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

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

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

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

93 self.output_files = { 

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

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

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

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

98 } 

99 

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

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

102 instructions_list = [] 

103 

104 # generating output path 

105 if self.container_path: 

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

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

108 else: 

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

110 

111 # executable path 

112 instructions_list.append(self.binary_path) 

113 

114 # generating input 

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

116 instructions_list.append(ipath) 

117 

118 basename = '-b ' + out_pth 

119 instructions_list.append(basename) 

120 

121 # adding charge if not none 

122 charge = get_charge(self.charge, out_log) 

123 if charge: 

124 charge = '-n ' + charge 

125 instructions_list.append(charge) 

126 

127 return instructions_list 

128 

129 @launchlogger 

130 def launch(self) -> int: 

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

132 

133 # check input/output paths and parameters 

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

135 

136 # Setup Biobb 

137 if self.check_restart(): 

138 return 0 

139 self.stage_files() 

140 

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

142 self.unique_name = create_unique_name(6) 

143 

144 # create command line instruction 

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

146 

147 # Run Biobb block 

148 self.run_biobb() 

149 

150 # Copy files to host 

151 self.copy_to_host() 

152 

153 # move files to output_path and removes temporary folder 

154 if self.container_path: 

155 process_output(self.unique_name, 

156 # self.stage_io_dict['unique_dir'], 

157 self.remove_tmp, 

158 self.basename, 

159 get_default_value(self.__class__.__name__), 

160 self.output_files, self.out_log) 

161 else: 

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

163 process_output(self.unique_name, 

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

165 self.remove_tmp, 

166 self.basename, 

167 get_default_value(self.__class__.__name__), 

168 self.output_files, self.out_log) 

169 

170 self.remove_tmp_files() 

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

172 

173 return self.return_code 

174 

175 

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

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

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

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

180 

181 

182acpype_params_ac.__doc__ = AcpypeParamsAC.__doc__ 

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

184 

185if __name__ == '__main__': 

186 main()