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

67 statements  

« prev     ^ index     » next       coverage.py v7.5.1, created at 2024-05-07 08:11 +0000

1#!/usr/bin/env python3 

2 

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

4import argparse 

5from biobb_common.generic.biobb_object import BiobbObject 

6from biobb_common.configuration import settings 

7from biobb_common.tools import file_utils as fu 

8from biobb_common.tools.file_utils import launchlogger 

9from biobb_amber.parmed.common import check_input_path, check_output_path 

10 

11 

12class ParmedCpinUtil(BiobbObject): 

13 """ 

14 | biobb_amber ParmedCpinUtil 

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

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

17 

18 Args: 

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

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

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

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

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

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

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

26 * **binary_path** (*str*) - ("cpinutil.py") Path to the cpinutil.py 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 * **container_path** (*str*) - (None) Container path definition. 

30 * **container_image** (*str*) - ('afandiadib/ambertools:serial') 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_amber.parmed.parmed_cpinutil import parmed_cpinutil 

40 prop = { 

41 'igb' : 2, 

42 'resnames': 'AS4 GL4', 

43 'system': 'cln025', 

44 'remove_tmp': False 

45 } 

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

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

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

49 properties=prop) 

50 

51 Info: 

52 * wrapped_software: 

53 * name: AmberTools parmed 

54 * version: >20.9 

55 * license: LGPL 2.1 

56 * ontology: 

57 * name: EDAM 

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

59 

60 """ 

61 

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

63 

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

73 'out': {'output_cpin_path': output_cpin_path, 

74 'output_top_path': output_top_path} 

75 } 

76 

77 # Properties specific for BB 

78 self.properties = properties 

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

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

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

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

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 input/output paths correctness """ 

90 

91 # Check input(s) 

92 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__) 

93 

94 # Check output(s) 

95 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__) 

96 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__) 

97 

98 @launchlogger 

99 def launch(self): 

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

101 

102 # check input/output paths and parameters 

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

104 

105 # Setup Biobb 

106 if self.check_restart(): 

107 return 0 

108 self.stage_files() 

109 

110 # Creating temporary folder 

111 self.tmp_folder = fu.create_unique_dir() 

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

113 

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

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

116 

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

118 

119 self.cmd = [self.binary_path, 

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

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

122 ] 

123 

124 if self.igb: 

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

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

127 

128 if self.system: 

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

130 self.cmd.append(self.system) 

131 

132 if self.resnames: 

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

134 self.cmd.append(self.resnames) 

135 

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

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

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

139 

140 # Run Biobb block 

141 self.run_biobb() 

142 

143 # Copy files to host 

144 self.copy_to_host() 

145 

146 # remove temporary folder(s) 

147 self.tmp_files.extend([ 

148 self.stage_io_dict.get("unique_dir"), 

149 self.tmp_folder 

150 ]) 

151 self.remove_tmp_files() 

152 

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

154 

155 return self.return_code 

156 

157 

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

159 output_top_path: str = None, 

160 properties: dict = None, **kwargs) -> int: 

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

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

163 

164 return ParmedCpinUtil(input_top_path=input_top_path, 

165 output_cpin_path=output_cpin_path, 

166 output_top_path=output_top_path, 

167 properties=properties).launch() 

168 

169 

170def main(): 

171 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)) 

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

173 

174 # Specific args 

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

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

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

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

179 

180 args = parser.parse_args() 

181 config = args.config if args.config else None 

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

183 

184 # Specific call 

185 parmed_cpinutil(input_top_path=args.input_top_path, 

186 output_cpin_path=args.output_cpin_path, 

187 output_top_path=args.output_top_path, 

188 properties=properties) 

189 

190 

191if __name__ == '__main__': 

192 main()