Coverage for biobb_amber/cpptraj/cpptraj_randomize_ions.py: 77%

70 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 CpptrajRandomizeIons class and the command line interface.""" 

4import argparse 

5from typing import Optional 

6from pathlib import PurePath 

7from biobb_common.generic.biobb_object import BiobbObject 

8from biobb_common.configuration import settings 

9from biobb_common.tools import file_utils as fu 

10from biobb_common.tools.file_utils import launchlogger 

11from biobb_amber.cpptraj.common import check_input_path, check_output_path 

12 

13 

14class CpptrajRandomizeIons(BiobbObject): 

15 """ 

16 | biobb_amber.cpptraj.cpptraj_randomize_ions CpptrajRandomizeIons 

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

18 | Swap specified ions with randomly selected solvent molecules using cpptraj tool from the AmberTools MD package. 

19 

20 Args: 

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

22 input_crd_path (str): Input coordinates file (AMBER crd). File type: input. `Sample file <https://github.com/bioexcel/biobb_amber/raw/master/biobb_amber/test/data/cpptraj/structure.ions.crd>`_. Accepted formats: crd (edam:format_3878), mdcrd (edam:format_3878), inpcrd (edam:format_3878). 

23 output_pdb_path (str): Structure PDB file with randomized ions. File type: output. `Sample file <https://github.com/bioexcel/biobb_amber/raw/master/biobb_amber/test/reference/cpptraj/structure.randIons.pdb>`_. Accepted formats: pdb (edam:format_1476). 

24 output_crd_path (str): Structure CRD file with coordinates including randomized ions. File type: output. `Sample file <https://github.com/bioexcel/biobb_amber/raw/master/biobb_amber/test/reference/cpptraj/structure.randIons.crd>`_. Accepted formats: crd (edam:format_3878), mdcrd (edam:format_3878), inpcrd (edam:format_3878). 

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

26 * **ion_mask** (*str*) - (":K+,Cl-,Na+") Ions to be randomized. Cpptraj mask syntax can be found at the official `Cpptraj manual <https://amber-md.github.io/cpptraj/CPPTRAJ.xhtml>`_. 

27 * **solute_mask** (*str*) - (":DA,DC,DG,DT,D?3,D?5") Solute (or set of atoms) around which the ions can get no closer than the distance specified. Cpptraj mask syntax can be found at the official `Cpptraj manual <https://amber-md.github.io/cpptraj/CPPTRAJ.xhtml>`_. 

28 * **distance** (*float*) - (5.0) Minimum distance cutoff for the ions around the defined solute. 

29 * **overlap** (*float*) - (3.5) Minimum distance between ions. 

30 * **binary_path** (*str*) - ("cpptraj") Path to the cpptraj executable binary. 

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

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

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

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

35 * **container_image** (*str*) - ('afandiadib/ambertools:serial') Container image definition. 

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

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

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

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

40 

41 Examples: 

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

43 

44 from biobb_amber.cpptraj.cpptraj_randomize_ions import cpptraj_randomize_ions 

45 prop = { 

46 'remove_tmp': True 

47 } 

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

49 input_crd_path='/path/to/coordinates.crd', 

50 output_pdb_path='/path/to/newStructure.pdb', 

51 output_crd_path='/path/to/newCoordinates.crd', 

52 properties=prop) 

53 

54 Info: 

55 * wrapped_software: 

56 * name: AmberTools cpptraj 

57 * version: >20.9 

58 * license: LGPL 2.1 

59 * ontology: 

60 * name: EDAM 

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

62 

63 """ 

64 

65 def __init__(self, input_top_path: str, input_crd_path: str, output_pdb_path: str, output_crd_path: str, properties, **kwargs) -> None: 

66 

67 properties = properties or {} 

68 

69 # Call parent class constructor 

70 super().__init__(properties) 

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

72 

73 # Input/Output files 

74 self.io_dict = { 

75 'in': { 

76 'input_top_path': input_top_path, 

77 'input_crd_path': input_crd_path 

78 }, 

79 'out': { 

80 'output_pdb_path': output_pdb_path, 

81 'output_crd_path': output_crd_path 

82 } 

83 } 

84 

85 # Properties specific for BB 

86 self.properties = properties 

87 self.ion_mask = properties.get('ion_mask', ":K+,Cl-,Na+") 

88 self.solute_mask = properties.get('solute_mask', ":DA,DC,DG,DT,D?3,D?5") 

89 self.distance = properties.get('distance', 5.0) 

90 self.overlap = properties.get('overlap', 3.5) 

91 self.binary_path = properties.get('binary_path', 'cpptraj') 

92 

93 # Check the properties 

94 self.check_properties(properties) 

95 self.check_arguments() 

96 

97 def check_data_params(self, out_log, err_log): 

98 """ Checks input/output paths correctness """ 

99 

100 # Check input(s) 

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

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

103 

104 # Check output(s) 

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

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

107 

108 @launchlogger 

109 def launch(self): 

110 """Launches the execution of the CpptrajRandomizeIons module.""" 

111 

112 # check input/output paths and parameters 

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

114 

115 # Setup Biobb 

116 if self.check_restart(): 

117 return 0 

118 self.stage_files() 

119 

120 if self.container_path: 

121 instructions_file = str(PurePath(self.stage_io_dict['unique_dir']).joinpath("cpptraj.in")) 

122 instructions_file_path = str(PurePath(self.container_volume_path).joinpath("cpptraj.in")) 

123 else: 

124 self.tmp_folder = fu.create_unique_dir() 

125 instructions_file = str(PurePath(self.tmp_folder).joinpath("cpptraj.in")) 

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

127 instructions_file_path = instructions_file 

128 

129 # create cpptraj.in file 

130 # trajin randomizeIons.crd 

131 # randomizeions :K+,Cl-,Na+ around :DA,DC,DG,DT,D?3,D?5 by 5.0 overlap 3.5 

132 # trajout solv_randion.crd restart 

133 # trajout solv_randion.pdb pdb 

134 # go 

135 

136 with open(instructions_file, 'w') as cpptrajin: 

137 cpptrajin.write("trajin " + self.stage_io_dict['in']['input_crd_path'] + " \n") 

138 cpptrajin.write("randomizeions " + self.ion_mask + " around " + self.solute_mask + " by " + str(self.distance) + " overlap " + str(self.overlap) + " \n") 

139 cpptrajin.write("trajout " + self.stage_io_dict['out']['output_crd_path'] + " restart \n") 

140 cpptrajin.write("trajout " + self.stage_io_dict['out']['output_pdb_path'] + " pdb \n") 

141 cpptrajin.write("go\n") 

142 

143 # Command line 

144 self.cmd = [self.binary_path, 

145 self.stage_io_dict['in']['input_top_path'], 

146 '-i', instructions_file_path 

147 ] 

148 

149 # Run Biobb block 

150 self.run_biobb() 

151 

152 # Copy files to host 

153 self.copy_to_host() 

154 

155 # Remove temporary file(s) 

156 self.tmp_files.extend([ 

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

158 self.tmp_folder, 

159 "cpptraj.log" 

160 ]) 

161 self.remove_tmp_files() 

162 

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

164 

165 return self.return_code 

166 

167 

168def cpptraj_randomize_ions(input_top_path: str, input_crd_path: str, 

169 output_pdb_path: str, output_crd_path: str, 

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

171 """Create :class:`CpptrajRandomizeIons <cpptraj.cpptraj_randomize_ions.CpptrajRandomizeIons>`cpptraj.cpptraj_randomize_ions.CpptrajRandomizeIons class and 

172execute :meth:`launch() <cpptraj.cpptraj_randomize_ions.CpptrajRandomizeIons.launch>` method""" 

173 

174 return CpptrajRandomizeIons(input_top_path=input_top_path, 

175 input_crd_path=input_crd_path, 

176 output_pdb_path=output_pdb_path, 

177 output_crd_path=output_crd_path, 

178 properties=properties).launch() 

179 

180 cpptraj_randomize_ions.__doc__ = CpptrajRandomizeIons.__doc__ 

181 

182 

183def main(): 

184 parser = argparse.ArgumentParser(description='Swap specified ions with randomly selected solvent molecules using cpptraj tool from the AmberTools MD package.', formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

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

186 

187 # Specific args 

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

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

190 required_args.add_argument('--input_crd_path', required=True, help='Input coordinates file (AMBER crd). Accepted formats: crd, mdcrd, inpcrd.') 

191 required_args.add_argument('--output_pdb_path', required=True, help='Structure PDB file with randomized ions. Accepted formats: pdb.') 

192 required_args.add_argument('--output_crd_path', required=True, help='Structure CRD file with coordinates including randomized ions. Accepted formats: crd, mdcrd, inpcrd.') 

193 

194 args = parser.parse_args() 

195 config = args.config if args.config else None 

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

197 

198 # Specific call 

199 cpptraj_randomize_ions(input_top_path=args.input_top_path, 

200 input_crd_path=args.input_crd_path, 

201 output_pdb_path=args.output_pdb_path, 

202 output_crd_path=args.output_crd_path, 

203 properties=properties) 

204 

205 

206if __name__ == '__main__': 

207 main()