Coverage for biobb_chemistry/acpype/acpype_params_cns.py: 77%

75 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-03-12 09:28 +0000

1#!/usr/bin/env python3 

2 

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

4import argparse 

5from typing import Optional 

6from biobb_common.generic.biobb_object import BiobbObject 

7from biobb_common.configuration import settings 

8from biobb_common.tools.file_utils import launchlogger 

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

10 

11 

12class AcpypeParamsCNS(BiobbObject): 

13 """ 

14 | biobb_chemistry AcpypeParamsCNS 

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

16 | Generation of topologies for CNS/XPLOR. 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>`_. 

17 

18 Args: 

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

20 output_path_par (str): Path to the PAR output file. File type: output. `Sample file <https://github.com/bioexcel/biobb_chemistry/raw/master/biobb_chemistry/test/reference/acpype/ref_acpype.cns.par>`_. Accepted formats: par (edam:format_3881). 

21 output_path_inp (str): Path to the INP output file. File type: output. `Sample file <https://github.com/bioexcel/biobb_chemistry/raw/master/biobb_chemistry/test/reference/acpype/ref_acpype.cns.inp>`_. Accepted formats: inp (edam:format_3878). 

22 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.cns.top>`_. Accepted formats: top (edam:format_3881). 

23 output_path_pdb (str): Path to the PDB output file. File type: output. `Sample file <https://github.com/bioexcel/biobb_chemistry/raw/master/biobb_chemistry/test/reference/acpype/ref_acpype.cns.pdb>`_. Accepted formats: pdb (edam:format_1476). 

24 properties (dic - Python dictionary object containing the tool parameters, not input/output files): 

25 * **basename** (*str*) - ("BBB") A basename for the project (folder and output files). 

26 * **charge** (*int*) - (0) [-20~20|1] Net molecular charge, for gas default is 0. If None the charge is guessed by acpype. 

27 * **binary_path** (*str*) - ("acpype") Path to the acpype executable binary. 

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

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

30 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory. 

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

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

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

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

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

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

37 

38 Examples: 

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

40 

41 from biobb_chemistry.acpype.acpype_params_cns import acpype_params_cns 

42 prop = { 

43 'basename': 'BBB', 

44 'charge': 0 

45 } 

46 acpype_params_cns(input_path='/path/to/myStructure.mol2', 

47 output_path_par='/path/to/newPAR.par', 

48 output_path_inp='/path/to/newINP.inp', 

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

50 output_path_top='/path/to/newPDB.pdb', 

51 properties=prop) 

52 

53 Info: 

54 * wrapped_software: 

55 * name: Acpype 

56 * version: 2019.10.05.12.26 

57 * license: GNU 

58 * ontology: 

59 * name: EDAM 

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

61 

62 """ 

63 

64 def __init__(self, input_path, output_path_par, output_path_inp, output_path_top, output_path_pdb, 

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

66 properties = properties or {} 

67 

68 # Call parent class constructor 

69 super().__init__(properties) 

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

71 

72 # Input/Output files 

73 self.io_dict = { 

74 "in": {"input_path": input_path}, 

75 "out": {"output_path_par": output_path_par, "output_path_inp": output_path_inp, "output_path_top": output_path_top, "output_path_pdb": output_path_pdb} 

76 } 

77 

78 # Properties specific for BB 

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

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

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

82 self.properties = properties 

83 

84 # Check the properties 

85 self.check_properties(properties) 

86 self.check_arguments() 

87 

88 def check_data_params(self, out_log, err_log): 

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

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

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

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

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

94 self.io_dict["out"]["output_path_pdb"] = check_output_path(self.io_dict["out"]["output_path_pdb"], 'pdb', out_log, self.__class__.__name__) 

95 self.output_files = { 

96 'par': self.io_dict["out"]["output_path_par"], 

97 'inp': self.io_dict["out"]["output_path_inp"], 

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

99 'pdb': self.io_dict["out"]["output_path_pdb"], 

100 } 

101 

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

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

104 instructions_list = [] 

105 

106 # generating output path 

107 if self.container_path: 

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

109 else: 

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

111 

112 # executable path 

113 instructions_list.append(self.binary_path) 

114 

115 # generating input 

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

117 instructions_list.append(ipath) 

118 

119 # generating output 

120 basename = '-b ' + out_pth 

121 instructions_list.append(basename) 

122 

123 # adding charge if not none 

124 charge = get_charge(self.charge, out_log) 

125 if charge: 

126 charge = '-n ' + charge 

127 instructions_list.append(charge) 

128 

129 return instructions_list 

130 

131 @launchlogger 

132 def launch(self) -> int: 

133 """Execute the :class:`AcpypeParamsCNS <acpype.acpype_params_cns.AcpypeParamsCNS>` acpype.acpype_params_cns.AcpypeParamsCNS object.""" 

134 

135 # check input/output paths and parameters 

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

137 

138 # Setup Biobb 

139 if self.check_restart(): 

140 return 0 

141 self.stage_files() 

142 

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

144 self.unique_name = create_unique_name(6) 

145 

146 # create command line instruction 

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

148 

149 # Run Biobb block 

150 self.run_biobb() 

151 

152 # Copy files to host 

153 self.copy_to_host() 

154 

155 # move files to output_path and removes temporary folder 

156 if self.container_path: 

157 process_output_cns( 

158 self.unique_name, 

159 # self.stage_io_dict['unique_dir'], 

160 self.remove_tmp, 

161 self.basename, 

162 get_default_value(self.__class__.__name__), 

163 self.output_files, self.out_log) 

164 else: 

165 self.tmp_files.extend([self.basename + "." + self.unique_name + ".acpype"]) 

166 process_output_cns( 

167 self.unique_name, 

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

169 self.remove_tmp, 

170 self.basename, 

171 get_default_value(self.__class__.__name__), 

172 self.output_files, self.out_log) 

173 

174 self.remove_tmp_files() 

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

176 

177 return self.return_code 

178 

179 

180def acpype_params_cns(input_path: str, output_path_par: str, output_path_inp: str, output_path_top: str, output_path_pdb: str, properties: Optional[dict] = None, **kwargs) -> int: 

181 """Execute the :class:`AcpypeParamsCNS <acpype.acpype_params_cns.AcpypeParamsCNS>` class and 

182 execute the :meth:`launch() <acpype.acpype_params_cns.AcpypeParamsCNS.launch>` method.""" 

183 

184 return AcpypeParamsCNS(input_path=input_path, 

185 output_path_par=output_path_par, 

186 output_path_inp=output_path_inp, 

187 output_path_top=output_path_top, 

188 output_path_pdb=output_path_pdb, 

189 properties=properties, **kwargs).launch() 

190 

191 acpype_params_cns.__doc__ = AcpypeParamsCNS.__doc__ 

192 

193 

194def main(): 

195 """Command line execution of this building block. Please check the command line documentation.""" 

196 parser = argparse.ArgumentParser(description="Small molecule parameterization for CNS/XPLOR MD package.", formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

197 parser.add_argument('--config', required=False, help='Configuration file') 

198 

199 # Specific args of each building block 

200 required_args = parser.add_argument_group('required arguments') 

201 required_args.add_argument('--input_path', required=True, help='Path to the input file. Accepted formats: pdb, mdl, mol2.') 

202 required_args.add_argument('--output_path_par', required=True, help='Path to the PAR output file. Accepted formats: par.') 

203 required_args.add_argument('--output_path_inp', required=True, help='Path to the INP output file. Accepted formats: inp.') 

204 required_args.add_argument('--output_path_top', required=True, help='Path to the TOP output file. Accepted formats: top.') 

205 required_args.add_argument('--output_path_pdb', required=True, help='Path to the PDB output file. Accepted formats: pdb.') 

206 

207 args = parser.parse_args() 

208 args.config = args.config or "{}" 

209 properties = settings.ConfReader(config=args.config).get_prop_dic() 

210 

211 # Specific call of each building block 

212 acpype_params_cns(input_path=args.input_path, 

213 output_path_par=args.output_path_par, 

214 output_path_inp=args.output_path_inp, 

215 output_path_top=args.output_path_top, 

216 output_path_pdb=args.output_path_pdb, 

217 properties=properties) 

218 

219 

220if __name__ == '__main__': 

221 main()