Package pipe_control :: Module spectrum
[hide private]
[frames] | no frames]

Source Code for Module pipe_control.spectrum

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2004-2014 Edward d'Auvergne                                   # 
  4  # Copyright (C) 2008 Sebastien Morin                                          # 
  5  # Copyright (C) 2013 Troels E. Linnet                                         # 
  6  #                                                                             # 
  7  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  8  #                                                                             # 
  9  # This program is free software: you can redistribute it and/or modify        # 
 10  # it under the terms of the GNU General Public License as published by        # 
 11  # the Free Software Foundation, either version 3 of the License, or           # 
 12  # (at your option) any later version.                                         # 
 13  #                                                                             # 
 14  # This program is distributed in the hope that it will be useful,             # 
 15  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 16  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 17  # GNU General Public License for more details.                                # 
 18  #                                                                             # 
 19  # You should have received a copy of the GNU General Public License           # 
 20  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 21  #                                                                             # 
 22  ############################################################################### 
 23   
 24  # Module docstring. 
 25  """Module containing functions for the handling of peak intensities.""" 
 26   
 27   
 28  # Python module imports. 
 29  from math import sqrt 
 30  import sys 
 31  from warnings import warn 
 32   
 33  # relax module imports. 
 34  from lib.errors import RelaxError, RelaxImplementError, RelaxNoSpectraError 
 35  from lib.io import write_data 
 36  from lib.spectrum.peak_list import read_peak_list 
 37  from lib.statistics import std 
 38  from lib.warnings import RelaxWarning, RelaxNoSpinWarning 
 39  from pipe_control import pipes 
 40  from pipe_control.mol_res_spin import check_mol_res_spin_data, create_spin, generate_spin_id_unique, return_spin, spin_loop 
 41   
 42   
43 -def __errors_height_no_repl():
44 """Calculate the errors for peak heights when no spectra are replicated.""" 45 46 # Loop over the spins and set the error to the RMSD of the base plane noise. 47 for spin, spin_id in spin_loop(return_id=True): 48 # Skip deselected spins. 49 if not spin.select: 50 continue 51 52 # Skip spins missing intensity data. 53 if not hasattr(spin, 'peak_intensity'): 54 continue 55 56 # Test if the RMSD has been set. 57 if not hasattr(spin, 'baseplane_rmsd'): 58 raise RelaxError("The RMSD of the base plane noise for spin '%s' has not been set." % spin_id) 59 60 # Set the error to the RMSD. 61 spin.peak_intensity_err = spin.baseplane_rmsd
62 63
64 -def __errors_repl(subset=None, verbosity=0):
65 """Calculate the errors for peak intensities from replicated spectra. 66 67 @keyword subset: The list of spectrum ID strings to restrict the analysis to. 68 @type subset: list of str 69 @keyword verbosity: The amount of information to print. The higher the value, the greater the verbosity. 70 @type verbosity: int 71 """ 72 73 # Replicated spectra. 74 repl = replicated_flags() 75 76 # Are all spectra replicated? 77 if False in repl.values(): 78 all_repl = False 79 print("All spectra replicated: No.") 80 else: 81 all_repl = True 82 print("All spectra replicated: Yes.") 83 84 # Initialise. 85 if not hasattr(cdp, 'sigma_I'): 86 cdp.sigma_I = {} 87 if not hasattr(cdp, 'var_I'): 88 cdp.var_I = {} 89 90 # The subset. 91 subset_flag = False 92 if not subset: 93 subset_flag = True 94 subset = cdp.spectrum_ids 95 96 # Loop over the spectra. 97 for id in subset: 98 # Skip non-replicated spectra. 99 if not repl[id]: 100 continue 101 102 # Skip replicated spectra which already have been used. 103 if id in cdp.var_I and cdp.var_I[id] != 0.0: 104 continue 105 106 # The replicated spectra. 107 for j in range(len(cdp.replicates)): 108 if id in cdp.replicates[j]: 109 spectra = cdp.replicates[j] 110 111 # Number of spectra. 112 num_spectra = len(spectra) 113 114 # Printout. 115 print("\nReplicated spectra: " + repr(spectra)) 116 if verbosity: 117 print("%-20s%-20s" % ("Spin_ID", "SD")) 118 119 # Calculate the mean value. 120 count = 0 121 for spin, spin_id in spin_loop(return_id=True): 122 # Skip deselected spins. 123 if not spin.select: 124 continue 125 126 # Skip and deselect spins which have no data. 127 if not hasattr(spin, 'peak_intensity'): 128 spin.select = False 129 continue 130 131 # Missing data. 132 missing = False 133 for j in range(num_spectra): 134 if not spectra[j] in spin.peak_intensity: 135 missing = True 136 if missing: 137 continue 138 139 # The peak intensities. 140 values = [] 141 for j in range(num_spectra): 142 values.append(spin.peak_intensity[spectra[j]]) 143 144 # The standard deviation. 145 sd = std(values=values, dof=1) 146 147 # Printout. 148 if verbosity: 149 print("%-20s%-20s" % (spin_id, sd)) 150 151 # Sum of variances (for average). 152 if not id in cdp.var_I: 153 cdp.var_I[id] = 0.0 154 cdp.var_I[id] = cdp.var_I[id] + sd**2 155 count = count + 1 156 157 # No data catch. 158 if not count: 159 raise RelaxError("No data is present, unable to calculate errors from replicated spectra.") 160 161 # Average variance. 162 cdp.var_I[id] = cdp.var_I[id] / float(count) 163 164 # Set all spectra variances. 165 for j in range(num_spectra): 166 cdp.var_I[spectra[j]] = cdp.var_I[id] 167 168 # Print out. 169 print("Standard deviation: %s" % sqrt(cdp.var_I[id])) 170 171 172 # Average across all spectra if there are time points with a single spectrum. 173 if not all_repl: 174 # Print out. 175 if subset_flag: 176 print("\nVariance averaging over the spectra subset.") 177 else: 178 print("\nVariance averaging over all spectra.") 179 180 # Initialise. 181 var_I = 0.0 182 num_dups = 0 183 184 # Loop over all time points. 185 for id in cdp.var_I.keys(): 186 # Only use id's defined in subset 187 if id not in subset: 188 continue 189 190 # Single spectrum (or extraordinarily accurate NMR spectra!). 191 if cdp.var_I[id] == 0.0: 192 continue 193 194 # Sum and count. 195 var_I = var_I + cdp.var_I[id] 196 num_dups = num_dups + 1 197 198 # Average value. 199 var_I = var_I / float(num_dups) 200 201 # Assign the average value to all time points. 202 for id in subset: 203 cdp.var_I[id] = var_I 204 205 # Print out. 206 print("Standard deviation for all spins: " + repr(sqrt(var_I))) 207 208 # Loop over the spectra. 209 for id in cdp.var_I.keys(): 210 # Create the standard deviation data structure. 211 cdp.sigma_I[id] = sqrt(cdp.var_I[id]) 212 213 # Set the spin specific errors. 214 for spin in spin_loop(): 215 # Skip deselected spins. 216 if not spin.select: 217 continue 218 219 # Set the error. 220 spin.peak_intensity_err = cdp.sigma_I
221 222
223 -def __errors_volume_no_repl(subset=None):
224 """Calculate the errors for peak volumes when no spectra are replicated.""" 225 226 # Loop over the spins and set the error to the RMSD of the base plane noise. 227 for spin, spin_id in spin_loop(return_id=True): 228 # Skip deselected spins. 229 if not spin.select: 230 continue 231 232 # Skip spins missing intensity data. 233 if not hasattr(spin, 'peak_intensity'): 234 continue 235 236 # Test if the RMSD has been set. 237 if not hasattr(spin, 'baseplane_rmsd'): 238 raise RelaxError("The RMSD of the base plane noise for spin '%s' has not been set." % spin_id) 239 240 # Test that the total number of points have been set. 241 if not hasattr(spin, 'N'): 242 raise RelaxError("The total number of points used in the volume integration has not been specified for spin '%s'." % spin_id) 243 244 # Set the error to the RMSD multiplied by the square root of the total number of points. 245 for key in spin.peak_intensity.keys(): 246 spin.peak_intensity_err[key] = spin.baseplane_rmsd[key] * sqrt(spin.N)
247 248
249 -def add_spectrum_id(spectrum_id=None):
250 """Add the spectrum ID to the data store. 251 252 @keyword spectrum_id: The spectrum ID string. 253 @type spectrum_id: str 254 """ 255 256 # Initialise the structure, if needed. 257 if not hasattr(cdp, 'spectrum_ids'): 258 cdp.spectrum_ids = [] 259 260 # The ID already exists. 261 if spectrum_id in cdp.spectrum_ids: 262 return 263 264 # Add the ID. 265 cdp.spectrum_ids.append(spectrum_id)
266 267
268 -def baseplane_rmsd(error=0.0, spectrum_id=None, spin_id=None):
269 """Set the peak intensity errors, as defined as the baseplane RMSD. 270 271 @param error: The peak intensity error value defined as the RMSD of the base plane 272 noise. 273 @type error: float 274 @keyword spectrum_id: The spectrum id. 275 @type spectrum_id: str 276 @param spin_id: The spin identification string. 277 @type spin_id: str 278 """ 279 280 # Data checks. 281 pipes.test() 282 check_mol_res_spin_data() 283 check_spectrum_id(spectrum_id) 284 285 # The scaling by NC_proc. 286 if hasattr(cdp, 'ncproc') and spectrum_id in cdp.ncproc: 287 scale = 1.0 / 2**cdp.ncproc[spectrum_id] 288 else: 289 scale = 1.0 290 291 # Loop over the spins. 292 for spin in spin_loop(spin_id): 293 # Skip deselected spins. 294 if not spin.select: 295 continue 296 297 # Initialise or update the baseplane_rmsd data structure as necessary. 298 if not hasattr(spin, 'baseplane_rmsd'): 299 spin.baseplane_rmsd = {} 300 301 # Set the error. 302 spin.baseplane_rmsd[spectrum_id] = float(error) * scale
303 304
305 -def check_spectrum_id(id):
306 """Check that the give spectrum ID exists. 307 308 @param id: The spectrum ID to check for. 309 @type id: str 310 @raises RelaxNoSpectraError: If the ID does not exist. 311 """ 312 313 # Check that the spectrum ID structure exists. 314 if not hasattr(cdp, 'spectrum_ids'): 315 raise RelaxNoSpectraError(id) 316 317 # Test if the spectrum ID exists. 318 if id not in cdp.spectrum_ids: 319 raise RelaxNoSpectraError(id)
320 321
322 -def delete(spectrum_id=None):
323 """Delete spectral data corresponding to the spectrum ID. 324 325 @keyword spectrum_id: The spectrum ID string. 326 @type spectrum_id: str 327 """ 328 329 # Data checks. 330 pipes.test() 331 check_mol_res_spin_data() 332 check_spectrum_id(spectrum_id) 333 334 # Remove the ID. 335 cdp.spectrum_ids.pop(cdp.spectrum_ids.index(spectrum_id)) 336 337 # The ncproc parameter. 338 if hasattr(cdp, 'ncproc') and spectrum_id in cdp.ncproc: 339 del cdp.ncproc[spectrum_id] 340 341 # Replicates. 342 if hasattr(cdp, 'replicates'): 343 # Loop over the replicates. 344 for i in range(len(cdp.replicates)): 345 # The spectrum is replicated. 346 if spectrum_id in cdp.replicates[i]: 347 # Duplicate. 348 if len(cdp.replicates[i]) == 2: 349 cdp.replicates.pop(i) 350 351 # More than two spectra: 352 else: 353 cdp.replicates[i].pop(cdp.replicates[i].index(spectrum_id)) 354 355 # No need to check further. 356 break 357 358 # Errors. 359 if hasattr(cdp, 'sigma_I') and spectrum_id in cdp.sigma_I: 360 del cdp.sigma_I[spectrum_id] 361 if hasattr(cdp, 'var_I') and spectrum_id in cdp.var_I: 362 del cdp.var_I[spectrum_id] 363 364 # Loop over the spins. 365 for spin in spin_loop(): 366 # Intensity data. 367 if hasattr(spin, 'peak_intensity') and spectrum_id in spin.peak_intensity: 368 del spin.peak_intensity[spectrum_id]
369 370
371 -def error_analysis(subset=None):
372 """Determine the peak intensity standard deviation. 373 374 @keyword subset: The list of spectrum ID strings to restrict the analysis to. 375 @type subset: list of str 376 """ 377 378 # Tests. 379 pipes.test() 380 check_mol_res_spin_data() 381 382 # Test if spectra have been loaded. 383 if not hasattr(cdp, 'spectrum_ids'): 384 raise RelaxError("Error analysis is not possible, no spectra have been loaded.") 385 386 # Check the IDs. 387 if subset: 388 for id in subset: 389 if id not in cdp.spectrum_ids: 390 raise RelaxError("The spectrum ID '%s' has not been loaded into relax." % id) 391 392 # Peak height category. 393 if cdp.int_method == 'height': 394 # Print out. 395 print("Intensity measure: Peak heights.") 396 397 # Do we have replicated spectra? 398 if hasattr(cdp, 'replicates'): 399 # Print out. 400 print("Replicated spectra: Yes.") 401 402 # Set the errors. 403 __errors_repl(subset=subset) 404 405 # No replicated spectra. 406 else: 407 # Print out. 408 print("Replicated spectra: No.") 409 if subset: 410 print("Spectra ID subset ignored.") 411 412 # Set the errors. 413 __errors_height_no_repl() 414 415 # Peak volume category. 416 if cdp.int_method == 'point sum': 417 # Print out. 418 print("Intensity measure: Peak volumes.") 419 420 # Do we have replicated spectra? 421 if hasattr(cdp, 'replicates'): 422 # Print out. 423 print("Replicated spectra: Yes.") 424 425 # Set the errors. 426 __errors_repl(subset=subset) 427 428 # No replicated spectra. 429 else: 430 # Print out. 431 print("Replicated spectra: No.") 432 433 # No implemented. 434 raise RelaxImplementError 435 436 # Set the errors. 437 __errors_vol_no_repl()
438 439
440 -def get_ids():
441 """Return a list of all spectrum IDs. 442 443 @return: The list of spectrum IDs. 444 @rtype: list of str 445 """ 446 447 # No IDs. 448 if not hasattr(cdp, 'spectrum_ids'): 449 return [] 450 451 # Return the IDs. 452 return cdp.spectrum_ids
453 454
455 -def integration_points(N=0, spectrum_id=None, spin_id=None):
456 """Set the number of integration points for the given spectrum. 457 458 @param N: The number of integration points. 459 @type N: int 460 @keyword spectrum_id: The spectrum ID string. 461 @type spectrum_id: str 462 @keyword spin_id: The spin ID string used to restrict the value to. 463 @type spin_id: None or str 464 """ 465 466 raise RelaxImplementError
467 468
469 -def read(file=None, dir=None, spectrum_id=None, dim=1, int_col=None, int_method=None, spin_id_col=None, mol_name_col=None, res_num_col=None, res_name_col=None, spin_num_col=None, spin_name_col=None, sep=None, spin_id=None, ncproc=None, verbose=True):
470 """Read the peak intensity data. 471 472 @keyword file: The name of the file(s) containing the peak intensities. 473 @type file: str or list of str 474 @keyword dir: The directory where the file is located. 475 @type dir: str 476 @keyword spectrum_id: The spectrum identification string. 477 @type spectrum_id: str or list of str 478 @keyword dim: The dimension of the peak list to associate the data with. 479 @type dim: int 480 @keyword int_col: The column containing the peak intensity data (used by the generic intensity file format). 481 @type int_col: int or list of int 482 @keyword int_method: The integration method, one of 'height', 'point sum' or 'other'. 483 @type int_method: str 484 @keyword spin_id_col: The column containing the spin ID strings (used by the generic intensity file format). If supplied, the mol_name_col, res_name_col, res_num_col, spin_name_col, and spin_num_col arguments must be none. 485 @type spin_id_col: int or None 486 @keyword mol_name_col: The column containing the molecule name information (used by the generic intensity file format). If supplied, spin_id_col must be None. 487 @type mol_name_col: int or None 488 @keyword res_name_col: The column containing the residue name information (used by the generic intensity file format). If supplied, spin_id_col must be None. 489 @type res_name_col: int or None 490 @keyword res_num_col: The column containing the residue number information (used by the generic intensity file format). If supplied, spin_id_col must be None. 491 @type res_num_col: int or None 492 @keyword spin_name_col: The column containing the spin name information (used by the generic intensity file format). If supplied, spin_id_col must be None. 493 @type spin_name_col: int or None 494 @keyword spin_num_col: The column containing the spin number information (used by the generic intensity file format). If supplied, spin_id_col must be None. 495 @type spin_num_col: int or None 496 @keyword sep: The column separator which, if None, defaults to whitespace. 497 @type sep: str or None 498 @keyword spin_id: The spin ID string used to restrict data loading to a subset of all spins. If 'auto' is provided for a NMRPipe seriesTab formatted file, the ID's are auto generated in form of Z_Ai. 499 @type spin_id: None or str 500 @keyword ncproc: The Bruker ncproc binary intensity scaling factor. 501 @type ncproc: int or None 502 @keyword verbose: A flag which if True will cause all relaxation data loaded to be printed out. 503 @type verbose: bool 504 """ 505 506 # Data checks. 507 pipes.test() 508 check_mol_res_spin_data() 509 510 # Check the file name. 511 if file == None: 512 raise RelaxError("The file name must be supplied.") 513 514 # Test that the intensity measures are identical. 515 if hasattr(cdp, 'int_method') and cdp.int_method != int_method: 516 raise RelaxError("The '%s' measure of peak intensities does not match '%s' of the previously loaded spectra." % (int_method, cdp.int_method)) 517 518 # Multiple ID flags. 519 flag_multi = False 520 flag_multi_file = False 521 flag_multi_col = False 522 if isinstance(spectrum_id, list) or spectrum_id == 'auto': 523 flag_multi = True 524 if isinstance(file, list): 525 flag_multi_file = True 526 if isinstance(int_col, list) or spectrum_id == 'auto': 527 flag_multi_col = True 528 529 # List argument checks. 530 if flag_multi: 531 # Too many lists. 532 if flag_multi_file and flag_multi_col: 533 raise RelaxError("If a list of spectrum IDs is supplied, the file names and intensity column arguments cannot both be lists.") 534 535 # Not enough lists. 536 if not flag_multi_file and not flag_multi_col: 537 raise RelaxError("If a list of spectrum IDs is supplied, either the file name or intensity column arguments must be a list of equal length.") 538 539 # List lengths for multiple files. 540 if flag_multi_file and len(spectrum_id) != len(file): 541 raise RelaxError("The file list %s and spectrum ID list %s do not have the same number of elements." % (file, spectrum_id)) 542 543 # List lengths for multiple intensity columns. 544 if flag_multi_col and spectrum_id != 'auto' and len(spectrum_id) != len(int_col): 545 raise RelaxError("The spectrum ID list %s and intensity column list %s do not have the same number of elements." % (spectrum_id, int_col)) 546 547 # More list argument checks (when only one spectrum ID is supplied). 548 else: 549 # Multiple files. 550 if flag_multi_file: 551 raise RelaxError("If multiple files are supplied, then multiple spectrum IDs must also be supplied.") 552 553 # Multiple intensity columns. 554 if flag_multi_col: 555 raise RelaxError("If multiple intensity columns are supplied, then multiple spectrum IDs must also be supplied.") 556 557 # Intensity column checks. 558 if spectrum_id != 'auto' and not flag_multi and flag_multi_col: 559 raise RelaxError("If a list of intensity columns is supplied, the spectrum ID argument must also be a list of equal length.") 560 561 # Check the intensity measure. 562 if not int_method in ['height', 'point sum', 'other']: 563 raise RelaxError("The intensity measure '%s' is not one of 'height', 'point sum', 'other'." % int_method) 564 565 # Set the peak intensity measure. 566 cdp.int_method = int_method 567 568 # Convert the file argument to a list if necessary. 569 if not isinstance(file, list): 570 file = [file] 571 572 # Loop over all files. 573 for file_index in range(len(file)): 574 # Read the peak list data. 575 peak_list = read_peak_list(file=file[file_index], dir=dir, int_col=int_col, spin_id_col=spin_id_col, mol_name_col=mol_name_col, res_num_col=res_num_col, res_name_col=res_name_col, spin_num_col=spin_num_col, spin_name_col=spin_name_col, sep=sep, spin_id=spin_id) 576 577 # Automatic spectrum IDs. 578 if spectrum_id == 'auto': 579 spectrum_id = peak_list[0].intensity_name 580 581 # Loop over the assignments. 582 data = [] 583 data_flag = False 584 for assign in peak_list: 585 # Generate the spin_id. 586 spin_id = generate_spin_id_unique(res_num=assign.res_nums[dim-1], spin_name=assign.spin_names[dim-1]) 587 588 # Convert the intensity data to a list if needed. 589 intensity = assign.intensity 590 if not isinstance(intensity, list): 591 intensity = [intensity] 592 593 # Loop over the intensity data. 594 for int_index in range(len(intensity)): 595 # Sanity check. 596 if intensity[int_index] == 0.0: 597 warn(RelaxWarning("A peak intensity of zero has been encountered for the spin '%s' - this could be fatal later on." % spin_id)) 598 599 # Get the spin container. 600 spin = return_spin(spin_id) 601 if not spin: 602 warn(RelaxNoSpinWarning(spin_id)) 603 continue 604 605 # Skip deselected spins. 606 if not spin.select: 607 continue 608 609 # Initialise. 610 if not hasattr(spin, 'peak_intensity'): 611 spin.peak_intensity = {} 612 613 # Intensity scaling. 614 if ncproc != None: 615 intensity[int_index] = intensity[int_index] / float(2**ncproc) 616 617 # Add the data. 618 if flag_multi_file: 619 id = spectrum_id[file_index] 620 elif flag_multi_col: 621 id = spectrum_id[int_index] 622 else: 623 id = spectrum_id 624 spin.peak_intensity[id] = intensity[int_index] 625 626 # Switch the flag. 627 data_flag = True 628 629 # Append the data for printing out. 630 data.append([spin_id, repr(intensity[int_index])]) 631 632 # Add the spectrum id (and ncproc) to the relax data store. 633 spectrum_ids = spectrum_id 634 if isinstance(spectrum_id, str): 635 spectrum_ids = [spectrum_id] 636 if ncproc != None and not hasattr(cdp, 'ncproc'): 637 cdp.ncproc = {} 638 for i in range(len(spectrum_ids)): 639 add_spectrum_id(spectrum_ids[i]) 640 if ncproc != None: 641 cdp.ncproc[spectrum_ids[i]] = ncproc 642 643 # No data. 644 if not data_flag: 645 # Delete all the data. 646 delete(spectrum_id) 647 648 # Raise the error. 649 raise RelaxError("No data could be loaded from the peak list") 650 651 # Printout. 652 if verbose: 653 print("\nThe following intensities have been loaded into the relax data store:\n") 654 write_data(out=sys.stdout, headings=["Spin_ID", "Intensity"], data=data) 655 print('')
656 657
658 -def read_spins(file=None, dir=None, dim=1, spin_id_col=None, mol_name_col=None, res_num_col=None, res_name_col=None, spin_num_col=None, spin_name_col=None, sep=None, spin_id=None, verbose=True):
659 """Read the peak intensity data. 660 661 @keyword file: The name of the file containing the peak intensities. 662 @type file: str 663 @keyword dir: The directory where the file is located. 664 @type dir: str 665 @keyword dim: The dimension of the peak list to associate the data with. 666 @type dim: int 667 @keyword spin_id_col: The column containing the spin ID strings (used by the generic intensity file format). If supplied, the mol_name_col, res_name_col, res_num_col, spin_name_col, and spin_num_col arguments must be none. 668 @type spin_id_col: int or None 669 @keyword mol_name_col: The column containing the molecule name information (used by the generic intensity file format). If supplied, spin_id_col must be None. 670 @type mol_name_col: int or None 671 @keyword res_name_col: The column containing the residue name information (used by the generic intensity file format). If supplied, spin_id_col must be None. 672 @type res_name_col: int or None 673 @keyword res_num_col: The column containing the residue number information (used by the generic intensity file format). If supplied, spin_id_col must be None. 674 @type res_num_col: int or None 675 @keyword spin_name_col: The column containing the spin name information (used by the generic intensity file format). If supplied, spin_id_col must be None. 676 @type spin_name_col: int or None 677 @keyword spin_num_col: The column containing the spin number information (used by the generic intensity file format). If supplied, spin_id_col must be None. 678 @type spin_num_col: int or None 679 @keyword sep: The column separator which, if None, defaults to whitespace. 680 @type sep: str or None 681 @keyword spin_id: The spin ID string used to restrict data loading to a subset of all spins. If 'auto' is provided for a NMRPipe seriesTab formatted file, the ID's are auto generated in form of Z_Ai. 682 @type spin_id: None or str 683 @keyword verbose: A flag which if True will cause all relaxation data loaded to be printed out. 684 @type verbose: bool 685 """ 686 687 # Data checks. 688 pipes.test() 689 690 # Check the file name. 691 if file == None: 692 raise RelaxError("The file name must be supplied.") 693 694 # Read the peak list data. 695 peak_list = read_peak_list(file=file, dir=dir, spin_id_col=spin_id_col, mol_name_col=mol_name_col, res_num_col=res_num_col, res_name_col=res_name_col, spin_num_col=spin_num_col, spin_name_col=spin_name_col, sep=sep, spin_id=spin_id) 696 697 # Loop over the peak_list. 698 created_spins = [] 699 for assign in peak_list: 700 mol_name = assign.mol_names[dim-1] 701 res_num = assign.res_nums[dim-1] 702 res_name = assign.res_names[dim-1] 703 spin_num = assign.spin_nums[dim-1] 704 spin_name = assign.spin_names[dim-1] 705 706 # Generate the spin_id. 707 spin_id = generate_spin_id_unique(mol_name=mol_name, res_num=res_num, res_name=res_name, spin_name=spin_name) 708 709 # Check if the spin already exist. 710 if return_spin(spin_id=spin_id) == None: 711 # Create the spin if not exist. 712 create_spin(spin_num=spin_num, spin_name=spin_name, res_num=res_num, res_name=res_name, mol_name=mol_name) 713 714 # Test that data exists. 715 check_mol_res_spin_data()
716
717 -def replicated(spectrum_ids=None):
718 """Set which spectra are replicates. 719 720 @keyword spectrum_ids: A list of spectrum ids corresponding to replicated spectra. 721 @type spectrum_ids: list of str 722 """ 723 724 # Test if the current pipe exists. 725 pipes.test() 726 727 # Test for None. 728 if spectrum_ids == None: 729 warn(RelaxWarning("The spectrum ID list cannot be None.")) 730 return 731 732 # Test if spectra have been loaded. 733 if not hasattr(cdp, 'spectrum_ids'): 734 raise RelaxError("No spectra have been loaded therefore replicates cannot be specified.") 735 736 # Test the spectrum id strings. 737 for spectrum_id in spectrum_ids: 738 check_spectrum_id(spectrum_id) 739 740 # Test for more than one element. 741 if len(spectrum_ids) == 1: 742 warn(RelaxWarning("The number of spectrum IDs in the list %s must be greater than one." % spectrum_ids)) 743 return 744 745 # Initialise. 746 if not hasattr(cdp, 'replicates'): 747 cdp.replicates = [] 748 749 # Check if the spectrum IDs are already in the list. 750 found = False 751 for i in range(len(cdp.replicates)): 752 # Loop over all elements of the first. 753 for j in range(len(spectrum_ids)): 754 if spectrum_ids[j] in cdp.replicates[i]: 755 found = True 756 757 # One of the spectrum IDs already have a replicate specified. 758 if found: 759 # Add the remaining replicates to the list and quit this function. 760 for j in range(len(spectrum_ids)): 761 if spectrum_ids[j] not in cdp.replicates[i]: 762 cdp.replicates[i].append(spectrum_ids[j]) 763 764 # Nothing more to do. 765 return 766 767 # A new set of replicates. 768 cdp.replicates.append(spectrum_ids)
769 770
771 -def replicated_flags():
772 """Create and return a dictionary of flags of whether the spectrum is replicated or not. 773 774 @return: The dictionary of flags of whether the spectrum is replicated or not. 775 @rtype: dict of bool 776 """ 777 778 # Initialise all IDs to false. 779 repl = {} 780 for id in cdp.spectrum_ids: 781 repl[id] = False 782 783 # Loop over the replicates. 784 for i in range(len(cdp.replicates)): 785 for j in range(len(cdp.replicates[i])): 786 repl[cdp.replicates[i][j]] = True 787 788 # Return the dictionary. 789 return repl
790 791
792 -def replicated_ids(spectrum_id):
793 """Create and return a list of spectra ID which are replicates of the given ID. 794 795 @param spectrum_id: The spectrum ID to find all the replicates of. 796 @type spectrum_id: str 797 @return: The list of spectrum IDs which are replicates of spectrum_id. 798 @rtype: list of str 799 """ 800 801 # Initialise the ID list. 802 repl = [] 803 804 # Loop over the replicate lists. 805 for i in range(len(cdp.replicates)): 806 # The spectrum ID is in the list. 807 if spectrum_id in cdp.replicates[i]: 808 # Loop over the inner list. 809 for j in range(len(cdp.replicates[i])): 810 # Spectrum ID match. 811 if spectrum_id == cdp.replicates[i][j]: 812 continue 813 814 # Append the replicated ID. 815 repl.append(cdp.replicates[i][j]) 816 817 # Nothing. 818 if repl == []: 819 return repl 820 821 # Sort the list. 822 repl.sort() 823 824 # Remove duplicates (backward). 825 id = repl[-1] 826 for i in range(len(repl)-2, -1, -1): 827 # Duplicate. 828 if id == repl[i]: 829 del repl[i] 830 831 # Unique. 832 else: 833 id = repl[i] 834 835 # Return the list. 836 return repl
837