Package specific_fns :: Module consistency_tests
[hide private]
[frames] | no frames]

Source Code for Module specific_fns.consistency_tests

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2004-2012 Edward d'Auvergne                                   # 
  4  # Copyright (C) 2007-2009 Sebastien Morin                                     # 
  5  #                                                                             # 
  6  # This file is part of the program relax.                                     # 
  7  #                                                                             # 
  8  # relax is free software; you can redistribute it and/or modify               # 
  9  # it under the terms of the GNU General Public License as published by        # 
 10  # the Free Software Foundation; either version 2 of the License, or           # 
 11  # (at your option) any later version.                                         # 
 12  #                                                                             # 
 13  # relax is distributed in the hope that it will be useful,                    # 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 16  # GNU General Public License for more details.                                # 
 17  #                                                                             # 
 18  # You should have received a copy of the GNU General Public License           # 
 19  # along with relax; if not, write to the Free Software                        # 
 20  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 21  #                                                                             # 
 22  ############################################################################### 
 23   
 24  # Python module imports. 
 25  from re import search 
 26  from warnings import warn 
 27   
 28  # relax module imports. 
 29  from api_base import API_base 
 30  from api_common import API_common 
 31  from generic_fns.mol_res_spin import exists_mol_res_spin_data, return_spin, spin_loop 
 32  from generic_fns import pipes 
 33  from maths_fns.consistency_tests import Consistency 
 34  from physical_constants import N15_CSA, NH_BOND_LENGTH, h_bar, mu0, return_gyromagnetic_ratio 
 35  from relax_errors import RelaxError, RelaxFuncSetupError, RelaxNoSequenceError, RelaxNoValueError, RelaxProtonTypeError, RelaxSpinTypeError 
 36  from relax_warnings import RelaxDeselectWarning 
 37   
 38   
39 -class Consistency_tests(API_base, API_common):
40 """Class containing functions specific to consistency testing.""" 41
42 - def __init__(self):
43 """Initialise the class by placing API_common methods into the API.""" 44 45 # Place methods into the API. 46 self.base_data_loop = self._base_data_loop_spin 47 self.create_mc_data = self._create_mc_relax_data 48 self.default_value = self._default_value_spin 49 self.model_loop = self._model_loop_spin 50 self.return_conversion_factor = self._return_no_conversion_factor 51 self.return_data_name = self._return_data_name_spin 52 self.return_error = self._return_error_relax_data 53 self.return_grace_string = self._return_grace_string_spin 54 self.return_units = self._return_units_spin 55 self.return_value = self._return_value_general 56 self.set_param_values = self._set_param_values_spin 57 self.set_selected_sim = self._set_selected_sim_spin 58 self.sim_pack_data = self._sim_pack_relax_data 59 60 # Set up the spin parameters. 61 self.SPIN_PARAMS.add('j0', grace_string='\\qJ(0)\\Q') 62 self.SPIN_PARAMS.add('f_eta', grace_string='\\qF\\s\\xh\\Q') 63 self.SPIN_PARAMS.add('f_r2', grace_string='\\qF\\sR2\\Q') 64 self.SPIN_PARAMS.add('r', default=NH_BOND_LENGTH, units='Angstrom', grace_string='Bond length') 65 self.SPIN_PARAMS.add('csa', default=N15_CSA, units='ppm', grace_string='\\qCSA\\Q') 66 self.SPIN_PARAMS.add('heteronuc_type', default='15N') 67 self.SPIN_PARAMS.add('proton_type', default='1H') 68 self.SPIN_PARAMS.add('orientation', default=15.7, units='degrees', grace_string='\\q\\xq\\Q') 69 self.SPIN_PARAMS.add('tc', default=13 * 1e-9, units='ns', grace_string='\\q\\xt\\f{}c\\Q')
70 71
72 - def _set_frq(self, frq=None):
73 """Function for selecting which relaxation data to use in the consistency tests.""" 74 75 # Test if the current pipe exists. 76 pipes.test() 77 78 # Test if the pipe type is set to 'ct'. 79 function_type = cdp.pipe_type 80 if function_type != 'ct': 81 raise RelaxFuncSetupError(specific_fns.setup.get_string(function_type)) 82 83 # Test if the frequency has been set. 84 if hasattr(cdp, 'ct_frq'): 85 raise RelaxError("The frequency for the run has already been set.") 86 87 # Create the data structure if it doesn't exist. 88 if not hasattr(cdp, 'ct_frq'): 89 cdp.ct_frq = {} 90 91 # Set the frequency. 92 cdp.ct_frq = frq
93 94
95 - def calculate(self, spin_id=None, verbosity=1, sim_index=None):
96 """Calculation of the consistency functions. 97 98 @keyword spin_id: The spin identification string. 99 @type spin_id: None or str 100 @keyword verbosity: The amount of information to print. The higher the value, the greater the verbosity. 101 @type verbosity: int 102 @keyword sim_index: The optional MC simulation index. 103 @type sim_index: None or int 104 """ 105 106 # Test if the frequency has been set. 107 if not hasattr(cdp, 'ct_frq') or not isinstance(cdp.ct_frq, float): 108 raise RelaxError("The frequency has not been set up.") 109 110 # Test if the sequence data is loaded. 111 if not exists_mol_res_spin_data(): 112 raise RelaxNoSequenceError 113 114 # Test if the CSA, bond length, angle Theta and correlation time values have been set. 115 for spin in spin_loop(spin_id): 116 # Skip deselected spins. 117 if not spin.select: 118 continue 119 120 # Test if the CSA value has been set. 121 if not hasattr(spin, 'csa') or spin.csa == None: 122 raise RelaxNoValueError("CSA") 123 124 # Test if the bond length has been set. 125 if not hasattr(spin, 'r') or spin.r == None: 126 raise RelaxNoValueError("bond length") 127 128 # Test if the angle Theta has been set. 129 if not hasattr(spin, 'orientation') or spin.orientation == None: 130 raise RelaxNoValueError("angle Theta") 131 132 # Test if the correlation time has been set. 133 if not hasattr(spin, 'tc') or spin.tc == None: 134 raise RelaxNoValueError("correlation time") 135 136 # Test if the spin type has been set. 137 if not hasattr(spin, 'heteronuc_type'): 138 raise RelaxSpinTypeError 139 140 # Test if the type attached proton has been set. 141 if not hasattr(spin, 'proton_type'): 142 raise RelaxProtonTypeError 143 144 # Frequency index. 145 if cdp.ct_frq not in cdp.frq.values(): 146 raise RelaxError("No relaxation data corresponding to the frequency %s has been loaded." % cdp.ct_frq) 147 148 # Consistency testing. 149 for spin in spin_loop(spin_id): 150 # Skip deselected spins. 151 if not spin.select: 152 continue 153 154 # Set the r1, r2, and NOE to None. 155 r1 = None 156 r2 = None 157 noe = None 158 159 # Get the R1, R2, and NOE values corresponding to the set frequency. 160 for ri_id in cdp.ri_ids: 161 # The frequency does not match. 162 if cdp.frq[ri_id] != cdp.ct_frq: 163 continue 164 165 # R1. 166 if cdp.ri_type[ri_id] == 'R1': 167 if sim_index == None: 168 r1 = spin.ri_data[ri_id] 169 else: 170 r1 = spin.ri_data_sim[ri_id][sim_index] 171 172 # R2. 173 if cdp.ri_type[ri_id] == 'R2': 174 if sim_index == None: 175 r2 = spin.ri_data[ri_id] 176 else: 177 r2 = spin.ri_data_sim[ri_id][sim_index] 178 179 # NOE. 180 if cdp.ri_type[ri_id] == 'NOE': 181 if sim_index == None: 182 noe = spin.ri_data[ri_id] 183 else: 184 noe = spin.ri_data_sim[ri_id][sim_index] 185 186 # Skip the spin if not all of the three value exist. 187 if r1 == None or r2 == None or noe == None: 188 continue 189 190 # Initialise the function to calculate. 191 self.ct = Consistency(frq=cdp.ct_frq, gx=return_gyromagnetic_ratio(spin.heteronuc_type), gh=return_gyromagnetic_ratio(spin.proton_type), mu0=mu0, h_bar=h_bar) 192 193 # Calculate the consistency tests values. 194 j0, f_eta, f_r2 = self.ct.func(orientation=spin.orientation, tc=spin.tc, r=spin.r, csa=spin.csa, r1=r1, r2=r2, noe=noe) 195 196 # Consistency tests values. 197 if sim_index == None: 198 spin.j0 = j0 199 spin.f_eta = f_eta 200 spin.f_r2 = f_r2 201 202 # Monte Carlo simulated consistency tests values. 203 else: 204 # Initialise the simulation data structures. 205 self.data_init(spin, sim=1) 206 if spin.j0_sim == None: 207 spin.j0_sim = [] 208 spin.f_eta_sim = [] 209 spin.f_r2_sim = [] 210 211 # Consistency tests values. 212 spin.j0_sim.append(j0) 213 spin.f_eta_sim.append(f_eta) 214 spin.f_r2_sim.append(f_r2)
215 216 217
218 - def data_init(self, data_cont, sim=False):
219 """Initialise the data structures. 220 221 @param data_cont: The data container. 222 @type data_cont: instance 223 @keyword sim: The Monte Carlo simulation flag, which if true will initialise the simulation data structure. 224 @type sim: bool 225 """ 226 227 # Get the data names. 228 data_names = self.data_names() 229 230 # Loop over the data structure names. 231 for name in data_names: 232 # Simulation data structures. 233 if sim: 234 # Add '_sim' to the names. 235 name = name + '_sim' 236 237 # If the name is not in 'data_cont', add it. 238 if not hasattr(data_cont, name): 239 # Set the attribute. 240 setattr(data_cont, name, None)
241 242
243 - def data_names(self, set=None, error_names=False, sim_names=False):
244 """Return a list of all spin container specific consistency testing object names. 245 246 Description 247 =========== 248 249 The names are as follows: 250 251 - 'r', bond length. 252 - 'csa', CSA value. 253 - 'heteronuc_type', the heteronucleus type. 254 - 'orientation', angle between the 15N-1H vector and the principal axis of the 15N chemical shift tensor. 255 - 'tc', correlation time. 256 - 'j0', spectral density value at 0 MHz (from Farrow et al. (1995) JBNMR, 6: 153-162). 257 - 'f_eta', eta-test (from Fushman et al. (1998) JACS, 120: 10947-10952). 258 - 'f_r2', R2-test (from Fushman et al. (1998) JACS, 120: 10947-10952). 259 260 261 @keyword set: An unused variable. 262 @type set: ignored 263 @keyword error_names: A flag which if True will add the error object names as well. 264 @type error_names: bool 265 @keyword sim_names: A flag which if True will add the Monte Carlo simulation object 266 names as well. 267 @type sim_names: bool 268 @return: The list of object names. 269 @rtype: list of str 270 """ 271 272 # Initialise. 273 names = [] 274 275 # Values. 276 names.append('r') 277 names.append('csa') 278 names.append('heteronuc_type') 279 names.append('orientation') 280 names.append('tc') 281 282 # Consistency tests values. 283 names.append('j0') 284 names.append('f_eta') 285 names.append('f_r2') 286 287 # Return the names. 288 return names
289 290 291 default_value_doc = """ 292 Consistency testing default values 293 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 294 These default values are found in the file 'physical_constants.py'. 295 296 ______________________________________________________________________________________ 297 | | | | 298 | Data type | Object name | Value | 299 |_______________________________________|____________________|_________________________| 300 | | | | 301 | Bond length | 'r' | 1.02 * 1e-10 | 302 | | | | 303 | CSA | 'csa' | -172 * 1e-6 | 304 | | | | 305 | Heteronucleus type | 'heteronuc_type' | '15N' | 306 | | | | 307 | Proton type | 'proton_type' | '1H' | 308 | | | | 309 | Angle Theta | 'orientation' | 15.7 | 310 | | | | 311 | Correlation time | 'tc' | 13 * 1e-9 | 312 |_______________________________________|____________________|_________________________| 313 314 """ 315 316
317 - def overfit_deselect(self):
318 """Deselect spins which have insufficient data to support calculation.""" 319 320 # Print out. 321 print("\n\nOver-fit spin deselection.\n") 322 323 # Test the sequence data exists. 324 if not exists_mol_res_spin_data(): 325 raise RelaxNoSequenceError 326 327 # Loop over spin data. 328 for spin, spin_id in spin_loop(return_id=True): 329 # Skip deselected spins. 330 if not spin.select: 331 continue 332 333 # Check if data exists. 334 if not hasattr(spin, 'ri_data'): 335 warn(RelaxDeselectWarning(spin_id, 'missing relaxation data')) 336 spin.select = False 337 338 # Require 3 or more data points. 339 else: 340 # Count the points. 341 data_points = 0 342 for id in cdp.ri_ids: 343 if spin.ri_data.has_key(id) and spin.ri_data[id] != None: 344 data_points += 1 345 346 # Not enough. 347 if data_points < 3: 348 warn(RelaxDeselectWarning(spin_id, 'insufficient relaxation data, 3 or more data points are required')) 349 spin.select = False
350 351 352 return_data_name_doc = """ 353 Consistency testing data type string matching patterns 354 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 355 356 ___________________________________________ 357 | | | 358 | Data type | Object name | 359 |_______________________|__________________| 360 | | | 361 | J(0) | 'j0' | 362 | | | 363 | F_eta | 'f_eta' | 364 | | | 365 | F_R2 | 'f_r2' | 366 | | | 367 | Bond length | 'r' | 368 | | | 369 | CSA | 'csa' | 370 | | | 371 | Heteronucleus type | 'heteronuc_type' | 372 | | | 373 | Proton type | 'proton_type' | 374 | | | 375 | Angle Theta | 'orientation' | 376 | | | 377 | Correlation time | 'tc' | 378 |_______________________|__________________| 379 """ 380 381 382 set_doc = """ 383 Consistency testing set details 384 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 385 386 In consistency testing, only four values can be set, the bond length, CSA, angle 387 Theta ('orientation') and correlation time values. These must be set prior to the 388 calculation of consistency functions. 389 390 """ 391 392
393 - def set_error(self, model_info, index, error):
394 """Set the parameter errors. 395 396 @param model_info: The spin container originating from model_loop(). 397 @type model_info: SpinContainer instance 398 @param index: The index of the parameter to set the errors for. 399 @type index: int 400 @param error: The error value. 401 @type error: float 402 """ 403 404 # Alias. 405 spin = model_info 406 407 # Return J(0) sim data. 408 if index == 0: 409 spin.j0_err = error 410 411 # Return F_eta sim data. 412 if index == 1: 413 spin.f_eta_err = error 414 415 # Return F_R2 sim data. 416 if index == 2: 417 spin.f_r2_err = error
418 419
420 - def sim_return_param(self, model_info, index):
421 """Return the array of simulation parameter values. 422 423 @param model_info: The spin container originating from model_loop(). 424 @type model_info: SpinContainer instance 425 @param index: The index of the parameter to return the array of values for. 426 @type index: int 427 @return: The array of simulation parameter values. 428 @rtype: list of float 429 """ 430 431 # Alias. 432 spin = model_info 433 434 # Skip deselected spins. 435 if not spin.select: 436 return 437 438 # Return J(0) sim data. 439 if index == 0: 440 return spin.j0_sim 441 442 # Return F_eta sim data. 443 if index == 1: 444 return spin.f_eta_sim 445 446 # Return F_R2 sim data. 447 if index == 2: 448 return spin.f_r2_sim
449 450
451 - def sim_return_selected(self, model_info):
452 """Return the array of selected simulation flags. 453 454 @param model_info: The spin container originating from model_loop(). 455 @type model_info: SpinContainer instance 456 @return: The array of selected simulation flags. 457 @rtype: list of int 458 """ 459 460 # Alias. 461 spin = model_info 462 463 # Multiple spins. 464 return spin.select_sim
465