Package specific_analyses :: Package noe :: Module api
[hide private]
[frames] | no frames]

Source Code for Module specific_analyses.noe.api

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2007-2014 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  6  #                                                                             # 
  7  # This program 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 3 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 19  #                                                                             # 
 20  ############################################################################### 
 21   
 22  # Module docstring. 
 23  """The steady-state heteronuclear NOE API object.""" 
 24   
 25  # Python module imports. 
 26  from math import sqrt 
 27  from warnings import warn 
 28   
 29  # relax module imports. 
 30  from lib.errors import RelaxError, RelaxNoSequenceError 
 31  from lib.warnings import RelaxDeselectWarning 
 32  from pipe_control import pipes 
 33  from pipe_control.mol_res_spin import exists_mol_res_spin_data, spin_loop 
 34  from specific_analyses.api_base import API_base 
 35  from specific_analyses.api_common import API_common 
 36  from specific_analyses.noe.parameter_object import Noe_params 
 37   
 38   
39 -class Noe(API_base, API_common):
40 """Specific analysis API class for the steady-state heternuclear NOE analysis.""" 41 42 # Class variable for storing the class instance (for the singleton design pattern). 43 instance = None 44
45 - def __init__(self):
46 """Initialise the class by placing API_common methods into the API.""" 47 48 # Place methods into the API. 49 self.return_conversion_factor = self._return_no_conversion_factor 50 self.return_value = self._return_value_general 51 52 # Place a copy of the parameter list object in the instance namespace. 53 self._PARAMS = Noe_params()
54 55
56 - def calculate(self, spin_id=None, verbosity=1, sim_index=None):
57 """Calculate the NOE and its error. 58 59 The error for each peak is calculated using the formula:: 60 ___________________________________________ 61 \/ {sd(sat)*I(unsat)}^2 + {sd(unsat)*I(sat)}^2 62 sd(NOE) = ----------------------------------------------- 63 I(unsat)^2 64 65 @keyword spin_id: The spin identification string. 66 @type spin_id: None or str 67 @keyword verbosity: The amount of information to print. The higher the value, the greater the verbosity. 68 @type verbosity: int 69 @keyword sim_index: The MC simulation index (unused). 70 @type sim_index: None 71 """ 72 73 # Test if the current pipe exists. 74 pipes.test() 75 76 # The spectrum types have not been set. 77 if not hasattr(cdp, 'spectrum_type'): 78 raise RelaxError("The spectrum types have not been set.") 79 80 # Test if the 2 spectra types 'ref' and 'sat' exist. 81 if not 'ref' in cdp.spectrum_type.values() or not 'sat' in cdp.spectrum_type.values(): 82 raise RelaxError("The reference and saturated NOE spectra have not been loaded.") 83 84 # Loop over the spins. 85 for spin in spin_loop(): 86 # Skip deselected spins. 87 if not spin.select: 88 continue 89 90 # Average intensities and squared errors (if required). 91 sat = 0.0 92 sat_err2 = 0.0 93 sat_count = 0 94 ref = 0.0 95 ref_err2 = 0.0 96 ref_count = 0 97 for id in cdp.spectrum_ids: 98 # Sat spectra. 99 if cdp.spectrum_type[id] == 'sat': 100 sat += spin.peak_intensity[id] 101 sat_err2 += spin.peak_intensity_err[id]**2 102 sat_count += 1 103 104 # Ref spectra. 105 if cdp.spectrum_type[id] == 'ref': 106 ref += spin.peak_intensity[id] 107 ref_err2 += spin.peak_intensity_err[id]**2 108 ref_count += 1 109 110 # Average the values and errors (variance averaging). 111 sat = sat / sat_count 112 sat_err2 = sat_err2 / sat_count 113 ref = ref / ref_count 114 ref_err2 = ref_err2 / ref_count 115 116 # Calculate the NOE. 117 spin.noe = sat / ref 118 119 # Calculate the error. 120 spin.noe_err = sqrt(sat_err2 * ref**2 + ref_err2 * sat**2) / ref**2
121 122
123 - def overfit_deselect(self, data_check=True, verbose=True):
124 """Deselect spins which have insufficient data to support calculation. 125 126 @keyword data_check: A flag to signal if the presence of base data is to be checked for. 127 @type data_check: bool 128 @keyword verbose: A flag which if True will allow printouts. 129 @type verbose: bool 130 """ 131 132 # Print out. 133 if verbose: 134 print("\nOver-fit spin deselection:") 135 136 # Test the sequence data exists. 137 if not exists_mol_res_spin_data(): 138 raise RelaxNoSequenceError 139 140 # Loop over spin data. 141 deselect_flag = False 142 all_desel = True 143 for spin, spin_id in spin_loop(return_id=True): 144 # Skip deselected spins. 145 if not spin.select: 146 continue 147 148 # No intensity data. 149 if not hasattr(spin, 'peak_intensity'): 150 warn(RelaxDeselectWarning(spin_id, 'the absence of intensity data')) 151 spin.select = False 152 deselect_flag = True 153 continue 154 155 # Check for sufficient data. 156 if not len(spin.peak_intensity) >= 2: 157 warn(RelaxDeselectWarning(spin_id, 'insufficient data (less than two data points)')) 158 spin.select = False 159 deselect_flag = True 160 continue 161 162 # No error data. 163 if not hasattr(spin, 'peak_intensity_err'): 164 warn(RelaxDeselectWarning(spin_id, 'the absence of errors')) 165 spin.select = False 166 deselect_flag = True 167 continue 168 169 # Check for sufficient errors. 170 if not len(spin.peak_intensity_err) >= 2: 171 warn(RelaxDeselectWarning(spin_id, 'missing errors (less than two error points)')) 172 spin.select = False 173 deselect_flag = True 174 continue 175 176 # Not all spins have been deselected. 177 all_desel = False 178 179 # Final printout. 180 if verbose and not deselect_flag: 181 print("No spins have been deselected.") 182 183 # Catch complete failures - i.e. no spins are selected. 184 if all_desel: 185 raise RelaxError("All spins have been deselected.")
186