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

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

4import argparse 

5from pathlib import PurePath 

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.cpptraj.common import check_input_path, check_output_path 

11 

12 

13class CpptrajRandomizeIons(BiobbObject): 

14 """ 

15 | biobb_amber.cpptraj.cpptraj_randomize_ions CpptrajRandomizeIons 

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

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

18 

19 Args: 

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

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

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

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

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

25 * **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>`_. 

26 * **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>`_. 

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

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

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

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

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

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

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

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

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

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

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

38 

39 Examples: 

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

41 

42 from biobb_amber.cpptraj.cpptraj_randomize_ions import cpptraj_randomize_ions 

43 prop = { 

44 'remove_tmp': True 

45 } 

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

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

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

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

50 properties=prop) 

51 

52 Info: 

53 * wrapped_software: 

54 * name: AmberTools cpptraj 

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: str, input_crd_path: str, output_pdb_path: str, output_crd_path: str, properties, **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': { 

74 'input_top_path': input_top_path, 

75 'input_crd_path': input_crd_path 

76 }, 

77 'out': { 

78 'output_pdb_path': output_pdb_path, 

79 'output_crd_path': output_crd_path 

80 } 

81 } 

82 

83 # Properties specific for BB 

84 self.properties = properties 

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

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

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

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

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

90 

91 # Check the properties 

92 self.check_properties(properties) 

93 self.check_arguments() 

94 

95 def check_data_params(self, out_log, err_log): 

96 """ Checks input/output paths correctness """ 

97 

98 # Check input(s) 

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

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

101 

102 # Check output(s) 

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

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

105 

106 @launchlogger 

107 def launch(self): 

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

109 

110 # check input/output paths and parameters 

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

112 

113 # Setup Biobb 

114 if self.check_restart(): 

115 return 0 

116 self.stage_files() 

117 

118 if self.container_path: 

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

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

121 else: 

122 self.tmp_folder = fu.create_unique_dir() 

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

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

125 instructions_file_path = instructions_file 

126 

127 # create cpptraj.in file 

128 # trajin randomizeIons.crd 

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

130 # trajout solv_randion.crd restart 

131 # trajout solv_randion.pdb pdb 

132 # go 

133 

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

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

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

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

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

139 cpptrajin.write("go\n") 

140 

141 # Command line 

142 self.cmd = [self.binary_path, 

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

144 '-i', instructions_file_path 

145 ] 

146 

147 # Run Biobb block 

148 self.run_biobb() 

149 

150 # Copy files to host 

151 self.copy_to_host() 

152 

153 # Remove temporary file(s) 

154 self.tmp_files.extend([ 

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

156 self.tmp_folder, 

157 "cpptraj.log" 

158 ]) 

159 self.remove_tmp_files() 

160 

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

162 

163 return self.return_code 

164 

165 

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

167 output_pdb_path: str, output_crd_path: str, 

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

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

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

171 

172 return CpptrajRandomizeIons(input_top_path=input_top_path, 

173 input_crd_path=input_crd_path, 

174 output_pdb_path=output_pdb_path, 

175 output_crd_path=output_crd_path, 

176 properties=properties).launch() 

177 

178 

179def main(): 

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

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

182 

183 # Specific args 

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

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

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

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

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

189 

190 args = parser.parse_args() 

191 config = args.config if args.config else None 

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

193 

194 # Specific call 

195 cpptraj_randomize_ions(input_top_path=args.input_top_path, 

196 input_crd_path=args.input_crd_path, 

197 output_pdb_path=args.output_pdb_path, 

198 output_crd_path=args.output_crd_path, 

199 properties=properties) 

200 

201 

202if __name__ == '__main__': 

203 main()