Coverage for biobb_cmip / cmip / cmip_titration.py: 26%

69 statements  

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

1#!/usr/bin/env python3 

2 

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

4import os 

5from typing import Optional 

6from typing import Any 

7from pathlib import Path 

8from biobb_common.generic.biobb_object import BiobbObject 

9from biobb_common.tools import file_utils as fu 

10from biobb_common.tools.file_utils import launchlogger 

11from biobb_cmip.cmip.common import create_params_file 

12from biobb_cmip.cmip.common import params_preset 

13from biobb_cmip.cmip.common import get_pdb_total_charge 

14 

15 

16class CmipTitration(BiobbObject): 

17 """ 

18 | biobb_cmip Titration 

19 | Wrapper class for the CMIP titration module. 

20 | The CMIP titration module. CMIP titration module adds water molecules, positive ions (Na+) and negative ions (Cl-) in the energetically most favorable structure locations. 

21 

22 Args: 

23 input_pdb_path (str): Path to the input PDB file. File type: input. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_cmip/master/biobb_cmip/test/data/cmip/1kim_h.pdb>`_. Accepted formats: pdb (edam:format_1476). 

24 output_pdb_path (str): Path to the output PDB file. File type: output. `Sample file <https://raw.githubusercontent.com/bioexcel/biobb_cmip/master/biobb_cmip/test/reference/cmip/1kim_neutral.pdb>`_. Accepted formats: pdb (edam:format_1476). 

25 input_vdw_params_path (str) (Optional): Path to the CMIP input Van der Waals force parameters, if not provided the CMIP conda installation one is used ("$CONDA_PREFIX/share/cmip/dat/vdwprm"). File type: input. Accepted formats: txt (edam:format_2330). 

26 input_params_path (str) (Optional): Path to the CMIP input parameters file. File type: input. Accepted formats: txt (edam:format_2330). 

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

28 * **params** (*dict*) - ({}) CMIP options specification. 

29 * **energy_cutoff** (*float*) - (9999.9) Energy cutoff, extremely hight value to enable the addition of all the ions and waters before reaching the cutoff. 

30 * **num_wats** (*int*) - (10) Number of water molecules to be added. 

31 * **neutral** (*bool*) - (False) Neutralize the charge of the system. If selected *num_positive_ions* and *num_negative_ions* values will not be taken into account. 

32 * **num_positive_ions** (*int*) - (10) Number of positive ions to be added (Tipatom IP=Na+). 

33 * **num_negative_ions** (*int*) - (10) Number of negative ions to be added (Tipatom IM=Cl-). 

34 * **binary_path** (*str*) - ("titration") Path to the CMIP Titration executable binary. 

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

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

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

38 * **container_path** (*str*) - (None) Path to the binary executable of your container. 

39 * **container_image** (*str*) - ("cmip/cmip:latest") Container Image identifier. 

40 * **container_volume_path** (*str*) - ("/data") Path to an internal directory in the container. 

41 * **container_working_dir** (*str*) - (None) Path to the internal CWD in the container. 

42 * **container_user_id** (*str*) - (None) User number id to be mapped inside the container. 

43 * **container_shell_path** (*str*) - ("/bin/bash") Path to the binary executable of the container shell. 

44 

45 

46 Examples: 

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

48 

49 from biobb_cmip.cmip.titration import titration 

50 prop = { 'binary_path': 'titration' } 

51 titration(input_pdb_path='/path/to/myStructure.pdb', 

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

53 properties=prop) 

54 

55 Info: 

56 * wrapped_software: 

57 * name: CMIP Titration 

58 * version: 2.7.0 

59 * license: Apache-2.0 

60 * ontology: 

61 * name: EDAM 

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

63 """ 

64 

65 def __init__(self, input_pdb_path: str, output_pdb_path: str, 

66 input_vdw_params_path: Optional[str] = None, input_params_path: Optional[str] = None, 

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

68 properties = properties or {} 

69 

70 # Call parent class constructor 

71 super().__init__(properties) 

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

73 

74 # Input/Output files 

75 self.io_dict = { 

76 "in": {"input_pdb_path": input_pdb_path, "input_vdw_params_path": input_vdw_params_path, 

77 "input_params_path": input_params_path}, 

78 "out": {"output_pdb_path": output_pdb_path} 

79 } 

80 

81 # Properties specific for BB 

82 self.neutral = properties.get('neutral', False) 

83 self.num_wats = properties.get('num_wats') 

84 self.num_positive_ions = properties.get('num_positive_ions') 

85 self.num_negative_ions = properties.get('num_negative_ions') 

86 self.binary_path = properties.get('binary_path', 'titration') 

87 self.output_params_path = properties.get('output_params_path', 'params') 

88 if not self.io_dict['in'].get('input_vdw_params_path'): 

89 self.io_dict['in']['input_vdw_params_path'] = f"{os.environ.get('CONDA_PREFIX')}/share/cmip/dat/vdwprm" 

90 self.params: dict[str, Any] = {k: str(v) for k, v in properties.get('params', dict()).items()} 

91 self.energy_cutoff = properties.get('energy_cutoff', 9999.9) 

92 

93 # Check the properties 

94 self.check_properties(properties) 

95 self.check_arguments() 

96 

97 @launchlogger 

98 def launch(self) -> int: 

99 """Execute the :class:`Titration <cmip.titration.Titration>` object.""" 

100 # Setup Biobb 

101 if self.check_restart(): 

102 return 0 

103 

104 # Check if output_pdb_path ends with ".pdb" 

105 if not self.io_dict['out']['output_pdb_path'].endswith('.pdb'): 

106 fu.log('ERROR: output_pdb_path name must end in .pdb', self.out_log, self.global_log) 

107 raise ValueError("ERROR: output_pdb_path name must end in .pdb") 

108 

109 # Adding neutral, num_negative_ions, num_positive_ions, num_wats, cutoff 

110 if self.num_wats: 

111 self.params['titwat'] = str(self.num_wats) 

112 if self.num_positive_ions: 

113 self.params['titip'] = str(self.num_positive_ions) 

114 if self.num_negative_ions: 

115 self.params['titim'] = str(self.num_negative_ions) 

116 if self.neutral: 

117 charge = get_pdb_total_charge(self.io_dict['in']['input_pdb_path']) 

118 self.params['titip'] = '0' 

119 self.params['titim'] = '0' 

120 if int(round(charge)) > 0: 

121 self.params['titim'] = str(int(round(charge))) 

122 elif int(round(charge)) < 0: 

123 self.params['titip'] = abs(int(round(charge))) 

124 else: 

125 fu.log(f'Neutral flag activated however no positive or negative ions will be added because the system ' 

126 f'is already neutralized. System charge: {round(charge, 3)}', self.out_log, self.global_log) 

127 fu.log(f'Neutral flag activated. Current system charge: {round(charge, 3)}, ' 

128 f'positive ions to be added: {self.params["titip"]}, ' 

129 f'negative ions to be added: {self.params["titim"]}, ' 

130 f'final residual charge: {round(charge + int(self.params["titip"]) - int(self.params["titim"]), 3)}', 

131 self.out_log, self.global_log) 

132 if self.energy_cutoff: 

133 self.params['titcut'] = str(self.energy_cutoff) 

134 

135 combined_params_dir = fu.create_unique_dir() 

136 self.io_dict['in']['combined_params_path'] = create_params_file( 

137 output_params_path=str(Path(combined_params_dir).joinpath(self.output_params_path)), 

138 input_params_path=self.io_dict['in']['input_params_path'], 

139 params_preset_dict=params_preset(execution_type='titration'), 

140 params_properties_dict=self.params) 

141 

142 self.stage_files() 

143 

144 self.cmd = [self.binary_path, 

145 '-i', self.stage_io_dict['in']['combined_params_path'], 

146 '-vdw', self.stage_io_dict['in']['input_vdw_params_path'], 

147 '-hs', self.stage_io_dict['in']['input_pdb_path'], 

148 '-outpdb', self.stage_io_dict['out']['output_pdb_path'][:-4]] 

149 

150 # Run Biobb block 

151 self.run_biobb() 

152 

153 # Copy files to host 

154 self.copy_to_host() 

155 

156 # remove temporary folder(s) 

157 self.tmp_files.append(combined_params_dir) 

158 self.remove_tmp_files() 

159 

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

161 return self.return_code 

162 

163 

164def cmip_titration(input_pdb_path: str, output_pdb_path: str, 

165 input_vdw_params_path: Optional[str] = None, input_params_path: Optional[str] = None, 

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

167 """Create :class:`Titration <cmip.titration.Titration>` class and 

168 execute the :meth:`launch() <cmip.titration.Titration.launch>` method.""" 

169 return CmipTitration(input_pdb_path=input_pdb_path, output_pdb_path=output_pdb_path, 

170 input_vdw_params_path=input_vdw_params_path, input_params_path=input_params_path, 

171 properties=properties, **kwargs).launch() 

172 

173 

174cmip_titration.__doc__ = CmipTitration.__doc__ 

175main = CmipTitration.get_main(cmip_titration, "Wrapper of the CMIP Titration module.") 

176 

177if __name__ == '__main__': 

178 main()