Coverage for biobb_amber/parmed/parmed_cpinutil.py: 78%

68 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-28 08:28 +0000

1#!/usr/bin/env python3 

2 

3"""Module containing the ParmedCpinUtil 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 import file_utils as fu 

9from biobb_common.tools.file_utils import launchlogger 

10from biobb_amber.parmed.common import check_input_path, check_output_path 

11 

12 

13class ParmedCpinUtil(BiobbObject): 

14 """ 

15 | biobb_amber ParmedCpinUtil 

16 | Wrapper of the `AmberTools (AMBER MD Package) parmed tool <https://ambermd.org/AmberTools.php>`_ module. 

17 | Creates a cpin file for constant pH simulations from an AMBER topology file using parmed tool from the AmberTools MD package. 

18 

19 Args: 

20 input_top_path (str): Input AMBER topology file. File type: input. `Sample file <https://github.com/bioexcel/biobb_amber/raw/master/biobb_amber/test/data/parmed/structure.solv.top>`_. Accepted formats: top (edam:format_3881), parmtop (edam:format_3881), prmtop (edam:format_3881). 

21 output_cpin_path (str): Output AMBER constant pH input (CPin) file. File type: output. `Sample file <https://github.com/bioexcel/biobb_amber/raw/master/biobb_amber/test/reference/parmed/cln025.cpin>`_. Accepted formats: cpin (edam:format_2330). 

22 output_top_path (str) (Optional): Output topology file (AMBER ParmTop). File type: output. `Sample file <https://github.com/bioexcel/biobb_amber/raw/master/biobb_amber/test/reference/parmed/cln025.cpH.prmtop>`_. Accepted formats: top (edam:format_3881), parmtop (edam:format_3881), prmtop (edam:format_3881). 

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

24 * **resnames** (*str*) - ("None") Residue names to include in CPIN file. Values: AS4, GL4, HIP, CYS, LYS, TYR. 

25 * **igb** (*int*) - (2) Generalized Born model which you intend to use to evaluate dynamics or protonation state swaps. Values: 1, 2, 5, 7, 8. 

26 * **system** (*str*) - ("Unknown") Name of system to titrate. 

27 * **binary_path** (*str*) - ("cpinutil.py") Path to the cpinutil.py 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*) - ('afandiadib/ambertools:serial') 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_amber.parmed.parmed_cpinutil import parmed_cpinutil 

42 prop = { 

43 'igb' : 2, 

44 'resnames': 'AS4 GL4', 

45 'system': 'cln025', 

46 'remove_tmp': False 

47 } 

48 parmed_cpinutil(input_top_path='/path/to/topology.top', 

49 output_cpin_path='/path/to/newCpin.cpin', 

50 output_top_path='/path/to/newTopology.top', 

51 properties=prop) 

52 

53 Info: 

54 * wrapped_software: 

55 * name: AmberTools parmed 

56 * version: >20.9 

57 * license: LGPL 2.1 

58 * ontology: 

59 * name: EDAM 

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

61 

62 """ 

63 

64 def __init__(self, input_top_path, output_cpin_path, output_top_path=None, properties=None, **kwargs) -> None: 

65 

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_top_path': input_top_path}, 

75 'out': {'output_cpin_path': output_cpin_path, 

76 'output_top_path': output_top_path} 

77 } 

78 

79 # Properties specific for BB 

80 self.properties = properties 

81 self.resnames = properties.get('resnames') 

82 self.igb = properties.get('igb', 2) 

83 self.system = properties.get('system', "Unknown") 

84 self.binary_path = properties.get('binary_path', 'cpinutil.py') 

85 

86 # Check the properties 

87 self.check_properties(properties) 

88 self.check_arguments() 

89 

90 def check_data_params(self, out_log, err_log): 

91 """ Checks input/output paths correctness """ 

92 

93 # Check input(s) 

94 self.io_dict["in"]["input_top_path"] = check_input_path(self.io_dict["in"]["input_top_path"], "input_top_path", False, out_log, self.__class__.__name__) 

95 

96 # Check output(s) 

97 self.io_dict["out"]["output_cpin_path"] = check_output_path(self.io_dict["out"]["output_cpin_path"], "output_cpin_path", False, out_log, self.__class__.__name__) 

98 self.io_dict["out"]["output_top_path"] = check_output_path(self.io_dict["out"]["output_top_path"], "output_top_path", True, out_log, self.__class__.__name__) 

99 

100 @launchlogger 

101 def launch(self): 

102 """Launches the execution of the ParmedCpinUtil module.""" 

103 

104 # check input/output paths and parameters 

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

106 

107 # Setup Biobb 

108 if self.check_restart(): 

109 return 0 

110 self.stage_files() 

111 

112 # Creating temporary folder 

113 self.tmp_folder = fu.create_unique_dir() 

114 fu.log('Creating %s temporary folder' % self.tmp_folder, self.out_log) 

115 

116 # cpinutil.py -igb 2 -resname AS4 GL4 -p $1.prmtop -op $1.cpH.prmtop 

117 # cpinutil.py -p cln025.cpH.prmtop -igb 2 -system "CLN" -o cpin 

118 

119 fu.log('Creating command line with instructions and required arguments', self.out_log, self.global_log) 

120 

121 self.cmd = [self.binary_path, 

122 '-p', self.stage_io_dict['in']['input_top_path'], 

123 '-o', self.stage_io_dict['out']['output_cpin_path'] 

124 ] 

125 

126 if self.igb: 

127 self.cmd.append('-igb') 

128 self.cmd.append(str(self.igb)) 

129 

130 if self.system: 

131 self.cmd.append('-system') 

132 self.cmd.append(self.system) 

133 

134 if self.resnames: 

135 self.cmd.append('-resnames') 

136 self.cmd.append(self.resnames) 

137 

138 if self.io_dict["out"]["output_top_path"]: 

139 self.cmd.append('-op') 

140 self.cmd.append(self.stage_io_dict["out"]["output_top_path"]) 

141 

142 # Run Biobb block 

143 self.run_biobb() 

144 

145 # Copy files to host 

146 self.copy_to_host() 

147 

148 # remove temporary folder(s) 

149 self.tmp_files.extend([ 

150 # self.stage_io_dict.get("unique_dir", ""), 

151 self.tmp_folder 

152 ]) 

153 self.remove_tmp_files() 

154 

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

156 

157 return self.return_code 

158 

159 

160def parmed_cpinutil(input_top_path: str, output_cpin_path: str, 

161 output_top_path: Optional[str] = None, 

162 properties: Optional[dict] = None, **kwargs) -> int: 

163 """Create :class:`ParmedCpinUtil <parmed.parmed_cpinutil.ParmedCpinUtil>`parmed.parmed_cpinutil.ParmedCpinUtil class and 

164 execute :meth:`launch() <parmed.parmed_cpinutil.ParmedCpinUtil.launch>` method""" 

165 

166 return ParmedCpinUtil(input_top_path=input_top_path, 

167 output_cpin_path=output_cpin_path, 

168 output_top_path=output_top_path, 

169 properties=properties).launch() 

170 

171 parmed_cpinutil.__doc__ = ParmedCpinUtil.__doc__ 

172 

173 

174def main(): 

175 parser = argparse.ArgumentParser(description='create a cpin file for constant pH simulations from an AMBER topology file using parmed program from AmberTools MD package.', formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

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

177 

178 # Specific args 

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

180 required_args.add_argument('--input_top_path', required=True, help='Input AMBER topology file. Accepted formats: top, parmtop, prmtop.') 

181 required_args.add_argument('--output_cpin_path', required=True, help='Output AMBER constant pH input (CPin) file. Accepted formats: cpin.') 

182 required_args.add_argument('--output_top_path', required=False, help='Output topology file (AMBER ParmTop). Accepted formats: top, parmtop, prmtop.') 

183 

184 args = parser.parse_args() 

185 config = args.config if args.config else None 

186 properties = settings.ConfReader(config=config).get_prop_dic() 

187 

188 # Specific call 

189 parmed_cpinutil(input_top_path=args.input_top_path, 

190 output_cpin_path=args.output_cpin_path, 

191 output_top_path=args.output_top_path, 

192 properties=properties) 

193 

194 

195if __name__ == '__main__': 

196 main()