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.mol_res_spin import exists_mol_res_spin_data, spin_loop 
 33  from pipe_control.pipes import check_pipe 
 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.model_loop = self._model_loop_spin 50 self.return_conversion_factor = self._return_no_conversion_factor 51 self.return_value = self._return_value_general 52 53 # Place a copy of the parameter list object in the instance namespace. 54 self._PARAMS = Noe_params()
55 56
57 - def calculate(self, spin_id=None, scaling_matrix=None, verbosity=1, sim_index=None):
58 """Calculate the NOE and its error. 59 60 The error for each peak is calculated using the formula:: 61 ___________________________________________ 62 \/ {sd(sat)*I(unsat)}^2 + {sd(unsat)*I(sat)}^2 63 sd(NOE) = ----------------------------------------------- 64 I(unsat)^2 65 66 @keyword spin_id: The spin identification string. 67 @type spin_id: None or str 68 @keyword scaling_matrix: The per-model list of diagonal and square scaling matrices. 69 @type scaling_matrix: list of numpy rank-2, float64 array or list of None 70 @keyword verbosity: The amount of information to print. The higher the value, the greater the verbosity. 71 @type verbosity: int 72 @keyword sim_index: The MC simulation index (unused). 73 @type sim_index: None 74 """ 75 76 # Test if the current pipe exists. 77 check_pipe() 78 79 # The spectrum types have not been set. 80 if not hasattr(cdp, 'spectrum_type'): 81 raise RelaxError("The spectrum types have not been set.") 82 83 # Test if the 2 spectra types 'ref' and 'sat' exist. 84 if not 'ref' in list(cdp.spectrum_type.values()) or not 'sat' in list(cdp.spectrum_type.values()): 85 raise RelaxError("The reference and saturated NOE spectra have not been loaded.") 86 87 # Loop over the spins. 88 for spin in spin_loop(): 89 # Skip deselected spins. 90 if not spin.select: 91 continue 92 93 # Average intensities and squared errors (if required). 94 sat = 0.0 95 sat_err2 = 0.0 96 sat_count = 0 97 ref = 0.0 98 ref_err2 = 0.0 99 ref_count = 0 100 for id in cdp.spectrum_ids: 101 # Sat spectra. 102 if cdp.spectrum_type[id] == 'sat': 103 sat += spin.peak_intensity[id] 104 sat_err2 += spin.peak_intensity_err[id]**2 105 sat_count += 1 106 107 # Ref spectra. 108 if cdp.spectrum_type[id] == 'ref': 109 ref += spin.peak_intensity[id] 110 ref_err2 += spin.peak_intensity_err[id]**2 111 ref_count += 1 112 113 # Average the values and errors (variance averaging). 114 sat = sat / sat_count 115 sat_err2 = sat_err2 / sat_count 116 ref = ref / ref_count 117 ref_err2 = ref_err2 / ref_count 118 119 # Calculate the NOE. 120 spin.noe = sat / ref 121 122 # Calculate the error. 123 spin.noe_err = sqrt(sat_err2 * ref**2 + ref_err2 * sat**2) / ref**2
124 125
126 - def get_param_names(self, model_info=None):
127 """Return a vector of parameter names. 128 129 @keyword model_info: The spin container and the spin ID string from the _model_loop_spin() method. 130 @type model_info: SpinContainer instance, str 131 @return: The vector of parameter names. 132 @rtype: list of str 133 """ 134 135 # Simply return the two parameter names. 136 return ['noe']
137 138
139 - def overfit_deselect(self, data_check=True, verbose=True):
140 """Deselect spins which have insufficient data to support calculation. 141 142 @keyword data_check: A flag to signal if the presence of base data is to be checked for. 143 @type data_check: bool 144 @keyword verbose: A flag which if True will allow printouts. 145 @type verbose: bool 146 """ 147 148 # Print out. 149 if verbose: 150 print("\nOver-fit spin deselection:") 151 152 # Test the sequence data exists. 153 if not exists_mol_res_spin_data(): 154 raise RelaxNoSequenceError 155 156 # Loop over spin data. 157 deselect_flag = False 158 all_desel = True 159 for spin, spin_id in spin_loop(return_id=True): 160 # Skip deselected spins. 161 if not spin.select: 162 continue 163 164 # No intensity data. 165 if not hasattr(spin, 'peak_intensity'): 166 warn(RelaxDeselectWarning(spin_id, 'the absence of intensity data')) 167 spin.select = False 168 deselect_flag = True 169 continue 170 171 # Check for sufficient data. 172 if not len(spin.peak_intensity) >= 2: 173 warn(RelaxDeselectWarning(spin_id, 'insufficient data (less than two data points)')) 174 spin.select = False 175 deselect_flag = True 176 continue 177 178 # No error data. 179 if not hasattr(spin, 'peak_intensity_err'): 180 warn(RelaxDeselectWarning(spin_id, 'the absence of errors')) 181 spin.select = False 182 deselect_flag = True 183 continue 184 185 # Check for sufficient errors. 186 if not len(spin.peak_intensity_err) >= 2: 187 warn(RelaxDeselectWarning(spin_id, 'missing errors (less than two error points)')) 188 spin.select = False 189 deselect_flag = True 190 continue 191 192 # Not all spins have been deselected. 193 all_desel = False 194 195 # Final printout. 196 if verbose and not deselect_flag: 197 print("No spins have been deselected.") 198 199 # Catch complete failures - i.e. no spins are selected. 200 if all_desel: 201 raise RelaxError("All spins have been deselected.")
202