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

Source Code for Module specific_fns.jw_mapping

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