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

56 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2025-12-15 15:57 +0000

1#!/usr/bin/env python3 

2 

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

4 

5from typing import Optional 

6from biobb_common.generic.biobb_object import BiobbObject 

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 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory. 

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

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

41 prop = { 

42 'igb' : 2, 

43 'resnames': 'AS4 GL4', 

44 'system': 'cln025', 

45 'remove_tmp': False 

46 } 

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

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

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

50 properties=prop) 

51 

52 Info: 

53 * wrapped_software: 

54 * name: AmberTools parmed 

55 * version: >20.9 

56 * license: LGPL 2.1 

57 * ontology: 

58 * name: EDAM 

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

60 

61 """ 

62 

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

64 

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

74 'out': {'output_cpin_path': output_cpin_path, 

75 'output_top_path': output_top_path} 

76 } 

77 

78 # Properties specific for BB 

79 self.properties = properties 

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

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

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

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

84 

85 # Check the properties 

86 self.check_properties(properties) 

87 self.check_arguments() 

88 

89 def check_data_params(self, out_log, err_log): 

90 """ Checks input/output paths correctness """ 

91 

92 # Check input(s) 

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

94 

95 # Check output(s) 

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

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

98 

99 @launchlogger 

100 def launch(self): 

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

102 

103 # check input/output paths and parameters 

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

105 

106 # Setup Biobb 

107 if self.check_restart(): 

108 return 0 

109 self.stage_files() 

110 

111 # Creating temporary folder 

112 self.tmp_folder = fu.create_unique_dir() 

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

114 

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

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

117 

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

119 

120 self.cmd = [self.binary_path, 

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

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

123 ] 

124 

125 if self.igb: 

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

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

128 

129 if self.system: 

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

131 self.cmd.append(self.system) 

132 

133 if self.resnames: 

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

135 self.cmd.append(self.resnames) 

136 

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

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

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

140 

141 # Run Biobb block 

142 self.run_biobb() 

143 

144 # Copy files to host 

145 self.copy_to_host() 

146 

147 # remove temporary folder(s) 

148 self.tmp_files.extend([self.tmp_folder]) 

149 self.remove_tmp_files() 

150 

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

152 

153 return self.return_code 

154 

155 

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

157 output_top_path: Optional[str] = None, 

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

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

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

161 return ParmedCpinUtil(**dict(locals())).launch() 

162 

163 

164parmed_cpinutil.__doc__ = ParmedCpinUtil.__doc__ 

165main = ParmedCpinUtil.get_main(parmed_cpinutil, "Create a cpin file for constant pH simulations from an AMBER topology file using parmed program from AmberTools MD package.") 

166 

167if __name__ == '__main__': 

168 main()