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 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 re import match 
 24  from string import replace 
 25   
 26  from base_class import Common_functions 
 27  from maths_fns.jw_mapping import Mapping 
 28   
 29   
30 -class Jw_mapping(Common_functions):
31 - def __init__(self, relax):
32 """Class containing functions specific to reduced spectral density mapping.""" 33 34 self.relax = relax
35 36
37 - def calculate(self, run=None, print_flag=1, sim_index=None):
38 """Calculation of the spectral density values.""" 39 40 # Run argument. 41 self.run = run 42 43 # Test if the frequency has been set. 44 if not hasattr(self.relax.data, 'jw_frq') or not self.relax.data.jw_frq.has_key(self.run) or type(self.relax.data.jw_frq[self.run]) != float: 45 raise RelaxError, "The frequency for the run " + `self.run` + " has not been set up." 46 47 # Test if the nucleus type has been set. 48 if not hasattr(self.relax.data, 'gx'): 49 raise RelaxNucleusError 50 51 # Test if the sequence data is loaded. 52 if not self.relax.data.res.has_key(self.run): 53 raise RelaxNoSequenceError, self.run 54 55 # Test if the CSA and bond length values have been set. 56 for i in xrange(len(self.relax.data.res[self.run])): 57 # Skip unselected residues. 58 if not self.relax.data.res[self.run][i].select: 59 continue 60 61 # CSA value. 62 if not hasattr(self.relax.data.res[self.run][i], 'csa') or self.relax.data.res[self.run][i].csa == None: 63 raise RelaxNoValueError, "CSA" 64 65 # Bond length value. 66 if not hasattr(self.relax.data.res[self.run][i], 'r') or self.relax.data.res[self.run][i].r == None: 67 raise RelaxNoValueError, "bond length" 68 69 # Frequency index. 70 if self.relax.data.jw_frq[self.run] not in self.relax.data.frq[self.run]: 71 raise RelaxError, "No relaxation data corresponding to the frequency " + `self.relax.data.jw_frq[self.run]` + " has been loaded." 72 73 # Reduced spectral density mapping. 74 for i in xrange(len(self.relax.data.res[self.run])): 75 # Reassign data structure. 76 data = self.relax.data.res[self.run][i] 77 78 # Skip unselected residues. 79 if not data.select: 80 continue 81 82 # Residue specific frequency index. 83 frq_index = None 84 for j in xrange(data.num_frq): 85 if data.frq[j] == self.relax.data.jw_frq[self.run]: 86 frq_index = j 87 if frq_index == None: 88 continue 89 90 # Set the r1, r2, and NOE to None. 91 r1 = None 92 r2 = None 93 noe = None 94 95 # Get the R1, R2, and NOE values corresponding to the set frequency. 96 for j in xrange(data.num_ri): 97 # R1. 98 if data.remap_table[j] == frq_index and data.ri_labels[j] == 'R1': 99 if sim_index == None: 100 r1 = data.relax_data[j] 101 else: 102 r1 = data.relax_sim_data[sim_index][j] 103 104 # R2. 105 if data.remap_table[j] == frq_index and data.ri_labels[j] == 'R2': 106 if sim_index == None: 107 r2 = data.relax_data[j] 108 else: 109 r2 = data.relax_sim_data[sim_index][j] 110 111 # NOE. 112 if data.remap_table[j] == frq_index and data.ri_labels[j] == 'NOE': 113 if sim_index == None: 114 noe = data.relax_data[j] 115 else: 116 noe = data.relax_sim_data[sim_index][j] 117 118 # Skip the residue if not all of the three value exist. 119 if r1 == None or r2 == None or noe == None: 120 continue 121 122 # Initialise the function to calculate. 123 self.jw = Mapping(frq=self.relax.data.jw_frq[self.run], gx=self.relax.data.gx, gh=self.relax.data.gh, mu0=self.relax.data.mu0, h_bar=self.relax.data.h_bar) 124 125 # Calculate the spectral density values. 126 j0, jwx, jwh = self.jw.func(r=data.r, csa=data.csa, r1=r1, r2=r2, noe=noe) 127 128 # Reduced spectral density values. 129 if sim_index == None: 130 data.j0 = j0 131 data.jwx = jwx 132 data.jwh = jwh 133 134 # Monte Carlo simulated reduced spectral density values. 135 else: 136 # Initialise the simulation data structures. 137 self.initialise_data(data, self.run, sim=1) 138 if data.j0_sim == None: 139 data.j0_sim = [] 140 data.jwx_sim = [] 141 data.jwh_sim = [] 142 143 # Reduced spectral density values. 144 data.j0_sim.append(j0) 145 data.jwx_sim.append(jwx) 146 data.jwh_sim.append(jwh)
147 148
149 - def data_init(self, name):
150 """Function for returning an initial data structure corresponding to 'name'.""" 151 152 return None
153 154
155 - def data_names(self):
156 """Function for returning a list of names of data structures. 157 158 Description 159 ~~~~~~~~~~~ 160 161 r: Bond length. 162 163 csa: CSA value. 164 165 j0: Spectral density value at 0 MHz. 166 167 jwx: Spectral density value at the frequency of the heteronucleus. 168 169 jwh: Spectral density value at the frequency of the heteronucleus. 170 """ 171 172 # Initialise. 173 names = [] 174 175 # Values. 176 names.append('r') 177 names.append('csa') 178 179 # Spectral density values. 180 names.append('j0') 181 names.append('jwx') 182 names.append('jwh') 183 184 return names
185 186
187 - def default_value(self, param):
188 """ 189 Reduced spectral density mapping default values 190 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 191 192 _______________________________________________________________________________________ 193 | | | | 194 | Data type | Object name | Value | 195 |_______________________________________|______________|______________________________| 196 | | | | 197 | Bond length | r | 1.02 * 1e-10 | 198 |_______________________________________|______________|______________________________| 199 | | | | 200 | CSA | csa | -170 * 1e-6 | 201 |_______________________________________|______________|______________________________| 202 203 """ 204 205 # Bond length. 206 if param == 'r': 207 return 1.02 * 1e-10 208 209 # CSA. 210 if param == 'CSA': 211 return -170 * 1e-6
212 213
214 - def get_data_name(self, name):
215 """ 216 Reduced spectral density mapping data type string matching patterns 217 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 218 219 ____________________________________________________________________________________________ 220 | | | | 221 | Data type | Object name | Patterns | 222 |________________________|______________|__________________________________________________| 223 | | | | 224 | Bond length | r | '^r$' or '[Bb]ond[ -_][Ll]ength' | 225 |________________________|______________|__________________________________________________| 226 | | | | 227 | CSA | csa | '^[Cc][Ss][Aa]$' | 228 |________________________|______________|__________________________________________________| 229 230 """ 231 232 # Bond length. 233 if match('^r$', name) or match('[Bb]ond[ -_][Ll]ength', name): 234 return 'r' 235 236 # CSA. 237 if match('^[Cc][Ss][Aa]$', name): 238 return 'csa'
239 240
241 - def num_instances(self, run=None):
242 """Function for returning the number of instances.""" 243 244 # Arguments. 245 self.run = run 246 247 return len(self.relax.data.res[self.run])
248 249
250 - def return_value(self, run, i, data_type):
251 """Function for returning the value and error corresponding to 'data_type'.""" 252 253 # Arguments. 254 self.run = run 255 256 # Remap the data structure 'self.relax.data.res[run][i]'. 257 data = self.relax.data.res[run][i] 258 259 # Get the object. 260 object_name = self.get_data_name(data_type) 261 if not object_name: 262 raise RelaxError, "The reduced spectral density mapping data type " + `data_type` + " does not exist." 263 object_error = object_name + "_err" 264 265 # Get the value. 266 value = None 267 if hasattr(data, object_name): 268 value = getattr(data, object_name) 269 270 # Get the error. 271 error = None 272 if hasattr(data, object_error): 273 error = getattr(data, object_error) 274 275 # Return the data. 276 return value, error
277 278
279 - def set(self, run=None, value=None, error=None, data_type=None, index=None):
280 """ 281 Reduced spectral density mapping set details 282 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 283 284 In reduced spectral density mapping, only two values can be set, the bond length and CSA 285 value. These must be set prior to the calculation of spectral density values. 286 287 """ 288 289 # Arguments. 290 self.run = run 291 292 # Setting the model parameters prior to calculation. 293 #################################################### 294 295 if data_type == None: 296 # The values are supplied by the user: 297 if value: 298 # Test if the length of the value array is equal to 2. 299 if len(value) != 2: 300 raise RelaxError, "The length of " + `len(value)` + " of the value array must be equal to two." 301 302 # Default values. 303 else: 304 # Set 'value' to an empty array. 305 value = [] 306 307 # CSA and Bond length. 308 value.append(self.default_value('csa')) 309 value.append(self.default_value('r')) 310 311 # Initilise data. 312 if not hasattr(self.relax.data.res[self.run][index], 'csa') or not hasattr(self.relax.data.res[self.run][index], 'csa'): 313 self.initialise_data(self.relax.data.res[self.run][index], self.run) 314 315 # CSA and Bond length. 316 setattr(self.relax.data.res[self.run][index], 'csa', float(value[0])) 317 setattr(self.relax.data.res[self.run][index], 'r', float(value[1])) 318 319 320 # Individual data type. 321 ####################### 322 323 else: 324 # Get the object. 325 object_name = self.get_data_name(data_type) 326 if not object_name: 327 raise RelaxError, "The reduced spectral density mapping data type " + `data_type` + " does not exist." 328 329 # Initialise all data if it doesn't exist. 330 if not hasattr(self.relax.data.res[self.run][index], object_name): 331 self.initialise_data(self.relax.data.res[self.run][index], self.run) 332 333 # Default value. 334 if value == None: 335 value = self.default_value(object_name) 336 337 # Set the value. 338 setattr(self.relax.data.res[self.run][index], object_name, float(value)) 339 340 # Set the error. 341 if error != None: 342 setattr(self.relax.data.res[self.run][index], object_name+'_error', float(error))
343 344
345 - def set_frq(self, run=None, frq=None):
346 """Function for selecting which relaxation data to use in the J(w) mapping.""" 347 348 # Run argument. 349 self.run = run 350 351 # Test if the run exists. 352 if not self.run in self.relax.data.run_names: 353 raise RelaxNoRunError, self.run 354 355 # Test if the run type is set to 'jw'. 356 function_type = self.relax.data.run_types[self.relax.data.run_names.index(self.run)] 357 if function_type != 'jw': 358 raise RelaxFuncSetupError, self.relax.specific_setup.get_string(function_type) 359 360 # Test if the frequency has been set. 361 if hasattr(self.relax.data, 'jw_frq') and self.relax.data.jw_frq.has_key(self.run): 362 raise RelaxError, "The frequency for the run " + `self.run` + " has already been set." 363 364 # Create the data structure if it doesn't exist. 365 if not hasattr(self.relax.data, 'jw_frq'): 366 self.relax.data.jw_frq = {} 367 368 # Set the frequency. 369 self.relax.data.jw_frq[self.run] = frq
370 371
372 - def set_error(self, run, instance, index, error):
373 """Function for setting parameter errors.""" 374 375 # Arguments. 376 self.run = run 377 378 # Return J(0) sim data. 379 if index == 0: 380 self.relax.data.res[self.run][instance].j0_err = error 381 382 # Return J(wX) sim data. 383 if index == 1: 384 self.relax.data.res[self.run][instance].jwx_err = error 385 386 # Return J(wH) sim data. 387 if index == 2: 388 self.relax.data.res[self.run][instance].jwh_err = error
389 390
391 - def sim_return_param(self, run, instance, index):
392 """Function for returning the array of simulation parameter values.""" 393 394 # Arguments. 395 self.run = run 396 397 # Return J(0) sim data. 398 if index == 0: 399 return self.relax.data.res[self.run][instance].j0_sim 400 401 # Return J(wX) sim data. 402 if index == 1: 403 return self.relax.data.res[self.run][instance].jwx_sim 404 405 # Return J(wH) sim data. 406 if index == 2: 407 return self.relax.data.res[self.run][instance].jwh_sim
408 409
410 - def write_columnar_line(self, file=None, num=None, name=None, select=None, data_set=None, nucleus=None, wH=None, j0=None, jwx=None, jwh=None, r=None, csa=None, ri_labels=None, remap_table=None, frq_labels=None, frq=None, ri=None, ri_error=None):
411 """Function for printing a single line of the columnar formatted results.""" 412 413 # Residue number and name. 414 file.write("%-4s %-5s " % (num, name)) 415 416 # Selected flag and data set. 417 file.write("%-9s %-9s " % (select, data_set)) 418 if not select: 419 file.write("\n") 420 return 421 422 # Nucleus. 423 file.write("%-7s " % nucleus) 424 425 # Proton frequency. 426 file.write("%-25s " % wH) 427 428 # Parameters. 429 file.write("%-25s " % j0) 430 file.write("%-25s " % jwx) 431 file.write("%-25s " % jwh) 432 file.write("%-25s " % r) 433 file.write("%-25s " % csa) 434 435 # Relaxation data setup. 436 if ri_labels: 437 file.write("%-40s " % ri_labels) 438 file.write("%-25s " % remap_table) 439 file.write("%-25s " % frq_labels) 440 file.write("%-30s " % frq) 441 442 # Relaxation data. 443 if ri: 444 for i in xrange(len(ri)): 445 if ri[i] == None: 446 file.write("%-25s " % 'None') 447 else: 448 file.write("%-25s " % ri[i]) 449 450 # Relaxation errors. 451 if ri_error: 452 for i in xrange(len(ri_error)): 453 if ri_error[i] == None: 454 file.write("%-25s " % 'None') 455 else: 456 file.write("%-25s " % ri_error[i]) 457 458 # End of the line. 459 file.write("\n")
460 461
462 - def write_columnar_results(self, file, run):
463 """Function for printing the results into a file.""" 464 465 # Arguments. 466 self.run = run 467 468 # Test if the run exists. 469 if not self.run in self.relax.data.run_names: 470 raise RelaxNoRunError, self.run 471 472 # Test if sequence data is loaded. 473 if not self.relax.data.res.has_key(self.run): 474 raise RelaxNoSequenceError, self.run 475 476 477 # Header. 478 ######### 479 480 # Relaxation data and errors. 481 ri = [] 482 ri_error = [] 483 if hasattr(self.relax.data, 'num_ri'): 484 for i in xrange(self.relax.data.num_ri[self.run]): 485 ri.append('Ri_(' + self.relax.data.ri_labels[self.run][i] + "_" + self.relax.data.frq_labels[self.run][self.relax.data.remap_table[self.run][i]] + ")") 486 ri_error.append('Ri_error_(' + self.relax.data.ri_labels[self.run][i] + "_" + self.relax.data.frq_labels[self.run][self.relax.data.remap_table[self.run][i]] + ")") 487 488 # Write the header line. 489 self.write_columnar_line(file=file, num='Num', name='Name', select='Selected', data_set='Data_set', nucleus='Nucleus', wH='Proton_frq_(MHz)', j0='J(0)', jwx='J(wX)', jwh='J(wH)', r='Bond_length_(A)', csa='CSA_(ppm)', ri_labels='Ri_labels', remap_table='Remap_table', frq_labels='Frq_labels', frq='Frequencies', ri=ri, ri_error=ri_error) 490 491 492 # Values. 493 ######### 494 495 # Nucleus. 496 nucleus = self.relax.generic.nuclei.find_nucleus() 497 498 # The proton frequency in MHz. 499 wH = self.relax.data.jw_frq[self.run] / 1e6 500 501 # Relaxation data setup. 502 try: 503 ri_labels = replace(`self.relax.data.ri_labels[self.run]`, ' ', '') 504 remap_table = replace(`self.relax.data.remap_table[self.run]`, ' ', '') 505 frq_labels = replace(`self.relax.data.frq_labels[self.run]`, ' ', '') 506 frq = replace(`self.relax.data.frq[self.run]`, ' ', '') 507 except AttributeError: 508 ri_labels = `None` 509 remap_table = `None` 510 frq_labels = `None` 511 frq = `None` 512 513 # Loop over the sequence. 514 for i in xrange(len(self.relax.data.res[self.run])): 515 # Reassign data structure. 516 data = self.relax.data.res[self.run][i] 517 518 # Unselected residues. 519 if not data.select: 520 self.write_columnar_line(file=file, num=data.num, name=data.name, select=0, data_set='value') 521 continue 522 523 # J(0). 524 j0 = None 525 if hasattr(data, 'j0'): 526 j0 = data.j0 527 528 # J(wX). 529 jwx = None 530 if hasattr(data, 'jwx'): 531 jwx = data.jwx 532 533 # J(wH). 534 jwh = None 535 if hasattr(data, 'jwh'): 536 jwh = data.jwh 537 538 # Bond length. 539 r = None 540 if hasattr(data, 'r') and data.r != None: 541 r = data.r / 1e-10 542 543 # CSA. 544 csa = None 545 if hasattr(data, 'csa') and data.csa != None: 546 csa = data.csa / 1e-6 547 548 # Relaxation data and errors. 549 ri = [] 550 ri_error = [] 551 if hasattr(self.relax.data, 'num_ri'): 552 for i in xrange(self.relax.data.num_ri[self.run]): 553 # Find the residue specific data corresponding to i. 554 index = None 555 for j in xrange(data.num_ri): 556 if data.ri_labels[j] == self.relax.data.ri_labels[self.run][i] and data.frq_labels[data.remap_table[j]] == self.relax.data.frq_labels[self.run][self.relax.data.remap_table[self.run][i]]: 557 index = j 558 559 # Data exists for this data type. 560 try: 561 ri.append(`data.relax_data[index]`) 562 ri_error.append(`data.relax_error[index]`) 563 except: 564 ri.append(None) 565 ri_error.append(None) 566 567 # Write the line. 568 self.write_columnar_line(file=file, num=data.num, name=data.name, select=data.select, data_set='value', nucleus=nucleus, wH=`wH`, j0=`j0`, jwx=`jwx`, jwh=`jwh`, r=`r`, csa=`csa`, ri_labels=ri_labels, remap_table=remap_table, frq_labels=frq_labels, frq=frq, ri=ri, ri_error=ri_error) 569 570 571 # Errors. 572 ######### 573 574 # Skip this section and the next if no simulations have been setup. 575 if not hasattr(self.relax.data, 'sim_state'): 576 return 577 elif self.relax.data.sim_state[self.run] == 0: 578 return 579 580 # Loop over the sequence. 581 for i in xrange(len(self.relax.data.res[self.run])): 582 # Reassign data structure. 583 data = self.relax.data.res[self.run][i] 584 585 # Unselected residues. 586 if not data.select: 587 self.write_columnar_line(file=file, num=data.num, name=data.name, select=0, data_set='error') 588 continue 589 590 # J(0). 591 j0 = None 592 if hasattr(data, 'j0_err'): 593 j0 = data.j0_err 594 595 # J(wX). 596 jwx = None 597 if hasattr(data, 'jwx_err'): 598 jwx = data.jwx_err 599 600 # J(wH). 601 jwh = None 602 if hasattr(data, 'jwh_err'): 603 jwh = data.jwh_err 604 605 # Bond length. 606 r = None 607 if hasattr(data, 'r_err') and data.r_err != None: 608 r = data.r_err / 1e-10 609 610 # CSA. 611 csa = None 612 if hasattr(data, 'csa_err') and data.csa_err != None: 613 csa = data.csa_err / 1e-6 614 615 # Relaxation data and errors. 616 ri = [] 617 ri_error = [] 618 for i in xrange(self.relax.data.num_ri[self.run]): 619 ri.append(None) 620 ri_error.append(None) 621 622 # Write the line. 623 self.write_columnar_line(file=file, num=data.num, name=data.name, select=data.select, data_set='error', nucleus=nucleus, wH=`wH`, j0=`j0`, jwx=`jwx`, jwh=`jwh`, r=`r`, csa=`csa`, ri_labels=ri_labels, remap_table=remap_table, frq_labels=frq_labels, frq=frq, ri=ri, ri_error=ri_error) 624 625 626 # Simulation values. 627 #################### 628 629 # Loop over the simulations. 630 for i in xrange(self.relax.data.sim_number[self.run]): 631 # Loop over the sequence. 632 for j in xrange(len(self.relax.data.res[self.run])): 633 # Reassign data structure. 634 data = self.relax.data.res[self.run][j] 635 636 # Unselected residues. 637 if not data.select: 638 self.write_columnar_line(file=file, num=data.num, name=data.name, select=0, data_set='sim_'+`i`) 639 continue 640 641 # J(0). 642 j0 = None 643 if hasattr(data, 'j0_sim'): 644 j0 = data.j0_sim[i] 645 646 # J(wX). 647 jwx = None 648 if hasattr(data, 'jwx_sim'): 649 jwx = data.jwx_sim[i] 650 651 # J(wH). 652 jwh = None 653 if hasattr(data, 'jwh_sim'): 654 jwh = data.jwh_sim[i] 655 656 # Bond length. 657 r = None 658 if hasattr(data, 'r_sim') and data.r_sim != None and data.r_sim[i] != None: 659 r = data.r_sim[i] / 1e-10 660 661 # CSA. 662 csa = None 663 if hasattr(data, 'csa_sim') and data.csa_sim != None and data.csa_sim[i] != None: 664 csa = data.csa_sim[i] / 1e-6 665 666 # Relaxation data and errors. 667 ri = [] 668 ri_error = [] 669 for k in xrange(self.relax.data.num_ri[self.run]): 670 # Find the residue specific data corresponding to k. 671 index = None 672 for l in xrange(data.num_ri): 673 if data.ri_labels[l] == self.relax.data.ri_labels[self.run][k] and data.frq_labels[data.remap_table[l]] == self.relax.data.frq_labels[self.run][self.relax.data.remap_table[self.run][k]]: 674 index = l 675 676 # Data exists for this data type. 677 try: 678 ri.append(`data.relax_sim_data[i][index]`) 679 ri_error.append(`data.relax_error[index]`) 680 except: 681 ri.append(None) 682 ri_error.append(None) 683 684 # Write the line. 685 self.write_columnar_line(file=file, num=data.num, name=data.name, select=data.select, data_set='sim_'+`i`, nucleus=nucleus, wH=`wH`, j0=`j0`, jwx=`jwx`, jwh=`jwh`, r=`r`, csa=`csa`, ri_labels=ri_labels, remap_table=remap_table, frq_labels=frq_labels, frq=frq, ri=ri, ri_error=ri_error)
686