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

Source Code for Module specific_fns.noe

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2004-2005 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  from math import sqrt 
 24  from re import match 
 25   
 26   
27 -class Noe:
28 - def __init__(self, relax):
29 """Class containing functions for relaxation data.""" 30 31 self.relax = relax
32 33
34 - def assign_function(self, run=None, i=None, intensity=None):
35 """Function for assigning peak intensity data to either the reference or saturated spectra.""" 36 37 # Add the data. 38 if self.spectrum_type == 'ref': 39 self.relax.data.res[run][i].ref = intensity 40 elif self.spectrum_type == 'sat': 41 self.relax.data.res[run][i].sat = intensity
42 43
44 - def calculate(self, run=None, print_flag=1):
45 """Function for calculating the NOE and its error. 46 47 The error for each peak is calculated using the formula: 48 ___________________________________________ 49 \/ {sd(sat)*I(unsat)}^2 + {sd(unsat)*I(sat)}^2 50 sd(NOE) = ----------------------------------------------- 51 I(unsat)^2 52 """ 53 54 # Arguments. 55 self.run = run 56 57 # Test if the run exists. 58 if not self.run in self.relax.data.run_names: 59 raise RelaxNoRunError, self.run 60 61 # Test if sequence data is loaded. 62 if not self.relax.data.res.has_key(self.run): 63 raise RelaxNoSequenceError, self.run 64 65 # Loop over the sequence. 66 for i in xrange(len(self.relax.data.res[self.run])): 67 # Remap the data structure 'self.relax.data.res[self.run][i]'. 68 data = self.relax.data.res[self.run][i] 69 70 # Skip unselected residues. 71 if not data.select: 72 continue 73 74 # Skip residues which have no intensity values or errors. 75 if not (hasattr(data, 'ref') and hasattr(data, 'sat') and hasattr(data, 'ref_err') and hasattr(data, 'sat_err')): 76 continue 77 78 # Calculate the NOE. 79 data.noe = data.sat / data.ref 80 81 # Calculate the error. 82 data.noe_err = sqrt((data.sat_err * data.ref)**2 + (data.ref_err * data.sat)**2) / data.ref**2
83 84
85 - def read(self, run=None, file=None, dir=None, spectrum_type=None, format=None, heteronuc=None, proton=None, int_col=None):
86 """Function for reading peak intensity data.""" 87 88 # Arguments. 89 self.run = run 90 self.spectrum_type = spectrum_type 91 92 # Spectrum type argument. 93 spect_type_list = ['ref', 'sat'] 94 if self.spectrum_type not in spect_type_list: 95 raise RelaxArgNotInListError, ('spectrum type', self.spectrum_type, spect_type_list) 96 if self.spectrum_type == 'ref': 97 print "Reference spectrum." 98 if self.spectrum_type == 'sat': 99 print "Saturated spectrum." 100 101 # Generic intensity function. 102 self.relax.generic.intensity.read(run=run, file=file, dir=dir, format=format, heteronuc=heteronuc, proton=proton, int_col=int_col, assign_func=self.assign_function)
103 104
105 - def read_columnar_results(self, run, file_data):
106 """Function for reading the results file.""" 107 108 # Arguments. 109 self.run = run 110 111 # Extract and remove the header. 112 header = file_data[0] 113 file_data = file_data[1:] 114 115 # Sort the column numbers. 116 col = {} 117 for i in xrange(len(header)): 118 if header[i] == 'Num': 119 col['num'] = i 120 elif header[i] == 'Name': 121 col['name'] = i 122 elif header[i] == 'Selected': 123 col['select'] = i 124 elif header[i] == 'Ref_intensity': 125 col['ref_int'] = i 126 elif header[i] == 'Ref_error': 127 col['ref_err'] = i 128 elif header[i] == 'Sat_intensity': 129 col['sat_int'] = i 130 elif header[i] == 'Sat_error': 131 col['sat_err'] = i 132 elif header[i] == 'NOE': 133 col['noe'] = i 134 elif header[i] == 'NOE_error': 135 col['noe_err'] = i 136 137 # Test the file. 138 if len(col) < 2: 139 raise RelaxInvalidDataError 140 141 142 # Sequence. 143 ########### 144 145 # Generate the sequence. 146 for i in xrange(len(file_data)): 147 # Residue number and name. 148 try: 149 res_num = int(file_data[i][col['num']]) 150 except ValueError: 151 raise RelaxError, "The residue number " + file_data[i][col['num']] + " is not an integer." 152 res_name = file_data[i][col['name']] 153 154 # Add the residue. 155 self.relax.generic.sequence.add(self.run, res_num, res_name, select=int(file_data[i][col['select']])) 156 157 158 # Data. 159 ####### 160 161 # Loop over the file data. 162 for i in xrange(len(file_data)): 163 # Residue number and name. 164 try: 165 res_num = int(file_data[i][col['num']]) 166 except ValueError: 167 raise RelaxError, "The residue number " + file_data[i][col['num']] + " is not an integer." 168 res_name = file_data[i][col['name']] 169 170 # Find the residue index. 171 index = None 172 for j in xrange(len(self.relax.data.res[self.run])): 173 if self.relax.data.res[self.run][j].num == res_num and self.relax.data.res[self.run][j].name == res_name: 174 index = j 175 break 176 if index == None: 177 raise RelaxError, "Residue " + `res_num` + " " + res_name + " cannot be found in the sequence." 178 179 # Reassign data structure. 180 data = self.relax.data.res[self.run][index] 181 182 # Skip unselected residues. 183 if not data.select: 184 continue 185 186 # Reference intensity. 187 try: 188 data.ref = float(file_data[i][col['ref_int']]) 189 except ValueError: 190 data.ref = None 191 192 # Reference error. 193 try: 194 data.ref_err = float(file_data[i][col['ref_err']]) 195 except ValueError: 196 data.ref_err = None 197 198 # Saturated intensity. 199 try: 200 data.sat = float(file_data[i][col['sat_int']]) 201 except ValueError: 202 data.sat = None 203 204 # Saturated error. 205 try: 206 data.sat_err = float(file_data[i][col['sat_err']]) 207 except ValueError: 208 data.sat_err = None 209 210 # NOE. 211 try: 212 data.noe = float(file_data[i][col['noe']]) 213 except ValueError: 214 data.noe = None 215 216 # NOE error. 217 try: 218 data.noe_err = float(file_data[i][col['noe_err']]) 219 except ValueError: 220 data.noe_err = None
221 222
223 - def return_conversion_factor(self, stat_type):
224 """Dummy function for returning 1.0.""" 225 226 return 1.0
227 228
229 - def return_data_name(self, name):
230 """ 231 NOE calculation data type string matching patterns 232 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 233 234 ____________________________________________________________________________________________ 235 | | | | 236 | Data type | Object name | Patterns | 237 |________________________|______________|__________________________________________________| 238 | | | | 239 | Reference intensity | 'ref' | '^[Rr]ef$' or '[Rr]ef[ -_][Ii]nt' | 240 | | | | 241 | Saturated intensity | 'sat' | '^[Ss]at$' or '[Ss]at[ -_][Ii]nt' | 242 | | | | 243 | NOE | 'noe' | '^[Nn][Oo][Ee]$' | 244 |________________________|______________|__________________________________________________| 245 246 """ 247 248 # Reference intensity. 249 if match('^[Rr]ef$', name) or match('[Rr]ef[ -_][Ii]nt', name): 250 return 'ref' 251 252 # Saturated intensity. 253 if match('^[Ss]at$', name) or match('[Ss]at[ -_][Ii]nt', name): 254 return 'sat' 255 256 # NOE. 257 if match('^[Nn][Oo][Ee]$', name): 258 return 'noe'
259 260
261 - def return_grace_string(self, data_type):
262 """Function for returning the Grace string representing the data type for axis labelling.""" 263 264 # Get the object name. 265 object_name = self.return_data_name(data_type) 266 267 # Reference intensity. 268 if object_name == 'ref': 269 grace_string = 'Reference intensity' 270 271 # Saturated intensity. 272 if object_name == 'sat': 273 grace_string = 'Saturated intensity' 274 275 # NOE. 276 if object_name == 'noe': 277 grace_string = '\\qNOE\\Q' 278 279 # Return the Grace string. 280 return grace_string
281 282
283 - def return_units(self, stat_type):
284 """Dummy function which returns None as the stats have no units.""" 285 286 return None
287 288
289 - def return_value(self, run, i, data_type='noe'):
290 """Function for returning the NOE value and error.""" 291 292 # Arguments. 293 self.run = run 294 295 # Remap the data structure 'self.relax.data.res[run][i]'. 296 data = self.relax.data.res[run][i] 297 298 # Get the object. 299 object_name = self.return_data_name(data_type) 300 if not object_name: 301 raise RelaxError, "The NOE calculation data type " + `data_type` + " does not exist." 302 object_error = object_name + "_err" 303 304 # Get the value. 305 value = None 306 if hasattr(data, object_name): 307 value = getattr(data, object_name) 308 309 # Get the error. 310 error = None 311 if hasattr(data, object_error): 312 error = getattr(data, object_error) 313 314 # Return the data. 315 return value, error
316 317
318 - def set_error(self, run=None, error=0.0, spectrum_type=None, res_num=None, res_name=None):
319 """Function for setting the errors.""" 320 321 # Arguments. 322 self.run = run 323 self.spectrum_type = spectrum_type 324 self.res_num = res_num 325 self.res_name = res_name 326 327 # Test if the run exists. 328 if not run in self.relax.data.run_names: 329 raise RelaxNoRunError, run 330 331 # Test if the sequence data is loaded. 332 if not self.relax.data.res.has_key(run): 333 raise RelaxNoSequenceError, run 334 335 # Test if the residue number is a valid regular expression. 336 if type(res_num) == str: 337 try: 338 compile(res_num) 339 except: 340 raise RelaxRegExpError, ('residue number', res_num) 341 342 # Test if the residue name is a valid regular expression. 343 if res_name: 344 try: 345 compile(res_name) 346 except: 347 raise RelaxRegExpError, ('residue name', res_name) 348 349 # Loop over the sequence. 350 for i in xrange(len(self.relax.data.res[run])): 351 # Remap the data structure 'self.relax.data.res[self.run][i]'. 352 data = self.relax.data.res[self.run][i] 353 354 # Skip unselected residues. 355 if not data.select: 356 continue 357 358 # If 'res_num' is not None, skip the residue if there is no match. 359 if type(res_num) == int and not data.num == res_num: 360 continue 361 elif type(res_num) == str and not match(res_num, `data.num`): 362 continue 363 364 # If 'res_name' is not None, skip the residue if there is no match. 365 if res_name != None and not match(res_name, data.name): 366 continue 367 368 # Set the error. 369 if self.spectrum_type == 'ref': 370 data.ref_err = float(error) 371 elif self.spectrum_type == 'sat': 372 data.sat_err = float(error)
373 374
375 - def write(self, run=None, file=None, dir=None, force=0):
376 """Function for writing NOE values and errors to a file.""" 377 378 # Arguments 379 self.run = run 380 381 # Test if the run exists. 382 if not self.run in self.relax.data.run_names: 383 raise RelaxNoRunError, self.run 384 385 # Test if the sequence data is loaded. 386 if not self.relax.data.res.has_key(self.run): 387 raise RelaxNoSequenceError, self.run 388 389 # Open the file for writing. 390 noe_file = self.relax.IO.open_write_file(file, dir, force) 391 392 # Write the data. 393 self.relax.generic.value.write_data(self.run, None, noe_file, return_value=self.return_value) 394 395 # Close the file. 396 noe_file.close()
397 398
399 - def write_columnar_line(self, file=None, num=None, name=None, select=None, ref_int=None, ref_err=None, sat_int=None, sat_err=None, noe=None, noe_err=None):
400 """Function for printing a single line of the columnar formatted results.""" 401 402 # Residue number and name. 403 file.write("%-4s %-5s " % (num, name)) 404 405 # Selected flag and data set. 406 file.write("%-9s " % select) 407 if not select: 408 file.write("\n") 409 return 410 411 # Reference and saturated data. 412 file.write("%-25s %-25s " % (ref_int, ref_err)) 413 file.write("%-25s %-25s " % (sat_int, sat_err)) 414 415 # NOE and error. 416 file.write("%-25s %-25s " % (noe, noe_err)) 417 418 # End of the line. 419 file.write("\n")
420 421
422 - def write_columnar_results(self, file, run):
423 """Function for printing the results into a file.""" 424 425 # Arguments. 426 self.run = run 427 428 # Test if the run exists. 429 if not self.run in self.relax.data.run_names: 430 raise RelaxNoRunError, self.run 431 432 # Test if sequence data is loaded. 433 if not self.relax.data.res.has_key(self.run): 434 raise RelaxNoSequenceError, self.run 435 436 437 # Header. 438 ######### 439 440 441 # Write the header line. 442 self.write_columnar_line(file=file, num='Num', name='Name', select='Selected', ref_int='Ref_intensity', ref_err='Ref_error', sat_int='Sat_intensity', sat_err='Sat_error', noe='NOE', noe_err='NOE_error') 443 444 445 # Values. 446 ######### 447 448 # Loop over the sequence. 449 for i in xrange(len(self.relax.data.res[self.run])): 450 # Reassign data structure. 451 data = self.relax.data.res[self.run][i] 452 453 # Unselected residues. 454 if not data.select: 455 self.write_columnar_line(file=file, num=data.num, name=data.name, select=0) 456 continue 457 458 # Reference intensity. 459 ref_int = None 460 if hasattr(data, 'ref'): 461 ref_int = data.ref 462 463 # Reference error. 464 ref_err = None 465 if hasattr(data, 'ref_err'): 466 ref_err = data.ref_err 467 468 # Saturated intensity. 469 sat_int = None 470 if hasattr(data, 'sat'): 471 sat_int = data.sat 472 473 # Saturated error. 474 sat_err = None 475 if hasattr(data, 'sat_err'): 476 sat_err = data.sat_err 477 478 # NOE 479 noe = None 480 if hasattr(data, 'noe'): 481 noe = data.noe 482 483 # NOE error. 484 noe_err = None 485 if hasattr(data, 'noe_err'): 486 noe_err = data.noe_err 487 488 # Write the line. 489 self.write_columnar_line(file=file, num=data.num, name=data.name, select=data.select, ref_int=ref_int, ref_err=ref_err, sat_int=sat_int, sat_err=sat_err, noe=noe, noe_err=noe_err)
490