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

Source Code for Module pipe_control.relax_data

   1  ############################################################################### 
   2  #                                                                             # 
   3  # Copyright (C) 2003-2015 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  """Module for the manipulation of relaxation data.""" 
  24   
  25  # Python module imports. 
  26  from numpy import int32, ones, zeros 
  27  import string 
  28  import sys 
  29  from warnings import warn 
  30   
  31  # relax module imports. 
  32  from data_store import Relax_data_store; ds = Relax_data_store() 
  33  from data_store.exp_info import ExpInfo 
  34  from lib.errors import RelaxError, RelaxMultiSpinIDError, RelaxNoRiError, RelaxNoSequenceError, RelaxNoSpinError, RelaxRiError 
  35  from lib.io import write_data 
  36  from lib.physical_constants import element_from_isotope, number_from_isotope 
  37  from lib.sequence import read_spin_data 
  38  from lib.warnings import RelaxWarning 
  39  from pipe_control import bmrb, pipes, value 
  40  from pipe_control.interatomic import define, return_interatom, return_interatom_list 
  41  from pipe_control.mol_res_spin import Selection, exists_mol_res_spin_data, find_index, generate_spin_id_unique, get_molecule_names, return_spin, return_spin_from_selection, spin_index_loop, spin_loop 
  42  from pipe_control.pipes import check_pipe 
  43  from pipe_control.spectrometer import copy_frequencies, delete_frequencies, frequency_checks, loop_frequencies, set_frequency 
  44  from specific_analyses.api import return_api 
  45   
  46   
  47  # The relaxation data types supported. 
  48  VALID_TYPES = ['R1', 'R2', 'NOE', 'R2eff'] 
  49   
  50   
  51   
52 -def back_calc(ri_id=None, ri_type=None, frq=None):
53 """Back calculate the relaxation data. 54 55 If no relaxation data currently exists, then the ri_id, ri_type, and frq args are required. 56 57 58 @keyword ri_id: The relaxation data ID string. If not given, all relaxation data will be back calculated. 59 @type ri_id: None or str 60 @keyword ri_type: The relaxation data type. This should be one of 'R1', 'R2', or 'NOE'. 61 @type ri_type: None or str 62 @keyword frq: The spectrometer proton frequency in Hz. 63 @type frq: None or float 64 """ 65 66 # Test if the current pipe exists. 67 check_pipe() 68 69 # Test if sequence data is loaded. 70 if not exists_mol_res_spin_data(): 71 raise RelaxNoSequenceError 72 73 # Check that ri_type and frq are supplied if no relaxation data exists. 74 if ri_id and (not hasattr(cdp, 'ri_ids') or ri_id not in cdp.ri_ids) and (ri_type == None or frq == None): 75 raise RelaxError("The 'ri_type' and 'frq' arguments must be supplied as no relaxation data corresponding to '%s' exists." % ri_id) 76 77 # Check if the type is valid. 78 if ri_type and ri_type not in VALID_TYPES: 79 raise RelaxError("The relaxation data type '%s' must be one of %s." % (ri_type, VALID_TYPES)) 80 81 # Frequency checks. 82 frequency_checks(frq) 83 84 # Initialise the global data for the current pipe if necessary. 85 if not hasattr(cdp, 'ri_type'): 86 cdp.ri_type = {} 87 if not hasattr(cdp, 'ri_ids'): 88 cdp.ri_ids = [] 89 90 # Update the global data if needed. 91 if ri_id and ri_id not in cdp.ri_ids: 92 cdp.ri_ids.append(ri_id) 93 cdp.ri_type[ri_id] = ri_type 94 set_frequency(id=ri_id, frq=frq) 95 96 # The specific analysis API object. 97 api = return_api() 98 99 # The IDs to loop over. 100 if ri_id == None: 101 ri_ids = cdp.ri_ids 102 else: 103 ri_ids = [ri_id] 104 105 # The data types. 106 if ri_type == None: 107 ri_types = cdp.ri_type 108 else: 109 ri_types = {ri_id: ri_type} 110 111 # The frequencies. 112 if frq == None: 113 frqs = cdp.spectrometer_frq 114 else: 115 frqs = {ri_id: frq} 116 117 # Loop over the spins. 118 for spin, spin_id in spin_loop(return_id=True): 119 # Skip deselected spins. 120 if not spin.select: 121 continue 122 123 # The global index. 124 spin_index = find_index(spin_id) 125 126 # Initialise the spin data if necessary. 127 if not hasattr(spin, 'ri_data_bc'): 128 spin.ri_data_bc = {} 129 130 # Back-calculate the relaxation value. 131 for ri_id in ri_ids: 132 spin.ri_data_bc[ri_id] = api.back_calc_ri(spin_index=spin_index, ri_id=ri_id, ri_type=ri_types[ri_id], frq=frqs[ri_id])
133 134
135 -def bmrb_read(star, sample_conditions=None):
136 """Read the relaxation data from the NMR-STAR dictionary object. 137 138 @param star: The NMR-STAR dictionary object. 139 @type star: NMR_STAR instance 140 @keyword sample_conditions: The sample condition label to read. Only one sample condition can be read per data pipe. 141 @type sample_conditions: None or str 142 """ 143 144 # Get the relaxation data. 145 for data in star.relaxation.loop(): 146 # Sample conditions do not match (remove the $ sign). 147 if 'sample_cond_list_label' in data and sample_conditions and data['sample_cond_list_label'].replace('$', '') != sample_conditions: 148 continue 149 150 # Create the labels. 151 ri_type = data['data_type'] 152 frq = float(data['frq']) * 1e6 153 154 # Round the label to the nearest factor of 10. 155 frq_label = create_frq_label(float(data['frq']) * 1e6) 156 157 # The ID string. 158 ri_id = "%s_%s" % (ri_type, frq_label) 159 160 # The number of spins. 161 N = bmrb.num_spins(data) 162 163 # No data in the saveframe. 164 if N == 0: 165 continue 166 167 # The molecule names. 168 mol_names = bmrb.molecule_names(data, N) 169 170 # Generate the sequence if needed. 171 bmrb.generate_sequence(N, spin_names=data['atom_names'], res_nums=data['res_nums'], res_names=data['res_names'], mol_names=mol_names, isotopes=data['isotope'], elements=data['atom_types']) 172 173 # The attached protons. 174 if 'atom_names_2' in data: 175 # Generate the proton spins. 176 bmrb.generate_sequence(N, spin_names=data['atom_names_2'], res_nums=data['res_nums'], res_names=data['res_names'], mol_names=mol_names, isotopes=data['isotope_2'], elements=data['atom_types_2']) 177 178 # Define the dipolar interaction. 179 for i in range(len(data['atom_names'])): 180 # The spin IDs. 181 spin_id1 = generate_spin_id_unique(spin_name=data['atom_names'][i], res_num=data['res_nums'][i], res_name=data['res_names'][i], mol_name=mol_names[i]) 182 spin_id2 = generate_spin_id_unique(spin_name=data['atom_names_2'][i], res_num=data['res_nums'][i], res_name=data['res_names'][i], mol_name=mol_names[i]) 183 184 # Check if the container exists. 185 if return_interatom(spin_id1=spin_id1, spin_id2=spin_id2): 186 continue 187 188 # Define. 189 define(spin_id1=spin_id1, spin_id2=spin_id2, verbose=False) 190 191 # The data and error. 192 vals = data['data'] 193 errors = data['errors'] 194 if vals == None: 195 vals = [None] * N 196 if errors == None: 197 errors = [None] * N 198 199 # Data transformation. 200 if vals != None and 'units' in data: 201 # Scaling. 202 if data['units'] == 'ms': 203 # Loop over the data. 204 for i in range(N): 205 # The value. 206 if vals[i] != None: 207 vals[i] = vals[i] / 1000 208 209 # The error. 210 if errors[i] != None: 211 errors[i] = errors[i] / 1000 212 213 # Invert. 214 if data['units'] in ['s', 'ms']: 215 # Loop over the data. 216 for i in range(len(vals)): 217 # The value. 218 if vals[i] != None: 219 vals[i] = 1.0 / vals[i] 220 221 # The error. 222 if vals[i] != None and errors[i] != None: 223 errors[i] = errors[i] * vals[i]**2 224 225 # Pack the data. 226 pack_data(ri_id, ri_type, frq, vals, errors, mol_names=mol_names, res_nums=data['res_nums'], res_names=data['res_names'], spin_nums=None, spin_names=data['atom_names'], gen_seq=True, verbose=False) 227 228 # Store the temperature calibration and control. 229 if data['temp_calibration']: 230 temp_calibration(ri_id=ri_id, method=data['temp_calibration']) 231 if data['temp_control']: 232 temp_control(ri_id=ri_id, method=data['temp_control']) 233 234 # Peak intensity type. 235 if data['peak_intensity_type']: 236 peak_intensity_type(ri_id=ri_id, type=data['peak_intensity_type'])
237 238
239 -def bmrb_write(star):
240 """Generate the relaxation data saveframes for the NMR-STAR dictionary object. 241 242 @param star: The NMR-STAR dictionary object. 243 @type star: NMR_STAR instance 244 """ 245 246 # Get the current data pipe. 247 cdp = pipes.get_pipe() 248 249 # Initialise the spin specific data lists. 250 mol_name_list = [] 251 res_num_list = [] 252 res_name_list = [] 253 atom_name_list = [] 254 isotope_list = [] 255 element_list = [] 256 attached_atom_name_list = [] 257 attached_isotope_list = [] 258 attached_element_list = [] 259 ri_data_list = [] 260 ri_data_err_list = [] 261 for i in range(len(cdp.ri_ids)): 262 ri_data_list.append([]) 263 ri_data_err_list.append([]) 264 265 # Relax data labels. 266 labels = cdp.ri_ids 267 exp_label = [] 268 spectro_ids = [] 269 spectro_labels = [] 270 271 # Store the spin specific data in lists for later use. 272 for spin, mol_name, res_num, res_name, spin_id in spin_loop(full_info=True, return_id=True): 273 # Skip spins with no relaxation data. 274 if not hasattr(spin, 'ri_data'): 275 continue 276 277 # Check the data for None (not allowed in BMRB!). 278 if res_num == None: 279 raise RelaxError("For the BMRB, the residue of spin '%s' must be numbered." % spin_id) 280 if res_name == None: 281 raise RelaxError("For the BMRB, the residue of spin '%s' must be named." % spin_id) 282 if spin.name == None: 283 raise RelaxError("For the BMRB, the spin '%s' must be named." % spin_id) 284 if spin.isotope == None: 285 raise RelaxError("For the BMRB, the spin isotope type of '%s' must be specified." % spin_id) 286 287 # The molecule/residue/spin info. 288 mol_name_list.append(mol_name) 289 res_num_list.append(str(res_num)) 290 res_name_list.append(str(res_name)) 291 atom_name_list.append(str(spin.name)) 292 293 # Interatomic info. 294 interatoms = return_interatom_list(spin_id) 295 if len(interatoms) == 0: 296 raise RelaxError("No interatomic interactions are defined for the spin '%s'." % spin_id) 297 if len(interatoms) > 1: 298 raise RelaxError("The BMRB only handles a signal interatomic interaction for the spin '%s'." % spin_id) 299 300 # Get the attached spin. 301 spin_attached = return_spin(interatoms[0].spin_id1) 302 if id(spin_attached) == id(spin): 303 spin_attached = return_spin(interatoms[0].spin_id2) 304 305 # The attached atom info. 306 if hasattr(spin_attached, 'name'): 307 attached_atom_name_list.append(str(spin_attached.name)) 308 else: 309 attached_atom_name_list.append(None) 310 if hasattr(spin_attached, 'isotope'): 311 attached_element_list.append(element_from_isotope(spin_attached.isotope)) 312 attached_isotope_list.append(str(number_from_isotope(spin_attached.isotope))) 313 else: 314 attached_element_list.append(None) 315 attached_isotope_list.append(None) 316 317 # The relaxation data. 318 used_index = -ones(len(cdp.ri_ids)) 319 for i in range(len(cdp.ri_ids)): 320 # Data exists. 321 if cdp.ri_ids[i] in spin.ri_data: 322 ri_data_list[i].append(str(spin.ri_data[cdp.ri_ids[i]])) 323 ri_data_err_list[i].append(str(spin.ri_data_err[cdp.ri_ids[i]])) 324 else: 325 ri_data_list[i].append(None) 326 ri_data_err_list[i].append(None) 327 328 # Other info. 329 isotope_list.append(int(spin.isotope.strip(string.ascii_letters))) 330 element_list.append(spin.element) 331 332 # Convert the molecule names into the entity IDs. 333 entity_ids = zeros(len(mol_name_list), int32) 334 mol_names = get_molecule_names() 335 for i in range(len(mol_name_list)): 336 for j in range(len(mol_names)): 337 if mol_name_list[i] == mol_names[j]: 338 entity_ids[i] = j+1 339 340 # Check the temperature control methods. 341 if not hasattr(cdp, 'exp_info') or not hasattr(cdp.exp_info, 'temp_calibration'): 342 raise RelaxError("The temperature calibration methods have not been specified.") 343 if not hasattr(cdp, 'exp_info') or not hasattr(cdp.exp_info, 'temp_control'): 344 raise RelaxError("The temperature control methods have not been specified.") 345 346 # Check the peak intensity type. 347 if not hasattr(cdp, 'exp_info') or not hasattr(cdp.exp_info, 'peak_intensity_type'): 348 raise RelaxError("The peak intensity types measured for the relaxation data have not been specified.") 349 350 # Loop over the relaxation data. 351 for i in range(len(cdp.ri_ids)): 352 # Alias. 353 ri_id = cdp.ri_ids[i] 354 ri_type = cdp.ri_type[ri_id] 355 356 # Convert to MHz. 357 frq = cdp.spectrometer_frq[ri_id] * 1e-6 358 359 # Get the temperature control methods. 360 temp_calib = cdp.exp_info.temp_calibration[ri_id] 361 temp_control = cdp.exp_info.temp_control[ri_id] 362 363 # Get the peak intensity type. 364 peak_intensity_type = cdp.exp_info.peak_intensity_type[ri_id] 365 366 # Check. 367 if not temp_calib: 368 raise RelaxError("The temperature calibration method for the '%s' relaxation data ID string has not been specified." % ri_id) 369 if not temp_control: 370 raise RelaxError("The temperature control method for the '%s' relaxation data ID string has not been specified." % ri_id) 371 372 # Add the relaxation data. 373 star.relaxation.add(data_type=ri_type, frq=frq, entity_ids=entity_ids, res_nums=res_num_list, res_names=res_name_list, atom_names=atom_name_list, atom_types=element_list, isotope=isotope_list, entity_ids_2=entity_ids, res_nums_2=res_num_list, res_names_2=res_name_list, atom_names_2=attached_atom_name_list, atom_types_2=attached_element_list, isotope_2=attached_isotope_list, data=ri_data_list[i], errors=ri_data_err_list[i], temp_calibration=temp_calib, temp_control=temp_control, peak_intensity_type=peak_intensity_type) 374 375 # The experimental label. 376 if ri_type == 'NOE': 377 exp_name = 'steady-state NOE' 378 else: 379 exp_name = ri_type 380 exp_label.append("%s MHz %s" % (frq, exp_name)) 381 382 # Spectrometer info. 383 frq_num = 1 384 for frq in loop_frequencies(): 385 if frq == cdp.spectrometer_frq[ri_id]: 386 break 387 frq_num += 1 388 spectro_ids.append(frq_num) 389 spectro_labels.append("$spectrometer_%s" % spectro_ids[-1]) 390 391 # Add the spectrometer info. 392 num = 1 393 for frq in loop_frequencies(): 394 star.nmr_spectrometer.add(name="$spectrometer_%s" % num, manufacturer=None, model=None, frq=int(frq/1e6)) 395 num += 1 396 397 # Add the experiment saveframe. 398 star.experiment.add(name=exp_label, spectrometer_ids=spectro_ids, spectrometer_labels=spectro_labels)
399 400
401 -def copy(pipe_from=None, pipe_to=None, ri_id=None):
402 """Copy the relaxation data from one data pipe to another. 403 404 @keyword pipe_from: The data pipe to copy the relaxation data from. This defaults to the current data pipe. 405 @type pipe_from: str 406 @keyword pipe_to: The data pipe to copy the relaxation data to. This defaults to the current data pipe. 407 @type pipe_to: str 408 @param ri_id: The relaxation data ID string. 409 @type ri_id: str 410 """ 411 412 # Defaults. 413 if pipe_from == None and pipe_to == None: 414 raise RelaxError("The pipe_from and pipe_to arguments cannot both be set to None.") 415 elif pipe_from == None: 416 pipe_from = pipes.cdp_name() 417 elif pipe_to == None: 418 pipe_to = pipes.cdp_name() 419 420 # Test if the pipe_from and pipe_to data pipes exist. 421 check_pipe(pipe_from) 422 check_pipe(pipe_to) 423 424 # Get the data pipes. 425 dp_from = pipes.get_pipe(pipe_from) 426 dp_to = pipes.get_pipe(pipe_to) 427 428 # Test if pipe_from contains sequence data. 429 if not exists_mol_res_spin_data(pipe_from): 430 raise RelaxNoSequenceError 431 432 # Test if pipe_to contains sequence data. 433 if not exists_mol_res_spin_data(pipe_to): 434 raise RelaxNoSequenceError 435 436 # Test if relaxation data ID string exists for pipe_from. 437 if ri_id and (not hasattr(dp_from, 'ri_ids') or ri_id not in dp_from.ri_ids): 438 raise RelaxNoRiError(ri_id) 439 440 # The IDs. 441 if ri_id == None: 442 ri_ids = dp_from.ri_ids 443 else: 444 ri_ids = [ri_id] 445 446 # Init target pipe global structures. 447 if not hasattr(dp_to, 'ri_ids'): 448 dp_to.ri_ids = [] 449 if not hasattr(dp_to, 'ri_type'): 450 dp_to.ri_type = {} 451 452 # Loop over the Rx IDs. 453 for ri_id in ri_ids: 454 # Test if relaxation data ID string exists for pipe_to. 455 if ri_id in dp_to.ri_ids: 456 raise RelaxRiError(ri_id) 457 458 # Copy the global data. 459 dp_to.ri_ids.append(ri_id) 460 dp_to.ri_type[ri_id] = dp_from.ri_type[ri_id] 461 462 # Copy the frequency information. 463 copy_frequencies(pipe_from=pipe_from, pipe_to=pipe_to, id=ri_id) 464 465 # Spin loop. 466 for mol_index, res_index, spin_index in spin_index_loop(): 467 # Alias the spin containers. 468 spin_from = dp_from.mol[mol_index].res[res_index].spin[spin_index] 469 spin_to = dp_to.mol[mol_index].res[res_index].spin[spin_index] 470 471 # No data or errors. 472 if not hasattr(spin_from, 'ri_data') and not hasattr(spin_from, 'ri_data_err'): 473 continue 474 475 # Initialise the spin data if necessary. 476 if not hasattr(spin_to, 'ri_data'): 477 spin_to.ri_data = {} 478 if not hasattr(spin_to, 'ri_data_err'): 479 spin_to.ri_data_err = {} 480 481 # Copy the value and error from pipe_from. 482 spin_to.ri_data[ri_id] = spin_from.ri_data[ri_id] 483 spin_to.ri_data_err[ri_id] = spin_from.ri_data_err[ri_id]
484 485
486 -def create_frq_label(frq):
487 """Generate a frequency label in MHz, rounded to the nearest factor of 10. 488 489 @param frq: The frequency in Hz. 490 @type frq: float 491 @return: The MHz frequency label. 492 @rtype: str 493 """ 494 495 # Convert to MHz. 496 label = frq / 1e6 497 498 # Rounding to the nearest factor of 10. 499 label = int(round(label/10)*10) 500 501 # Convert to str and return. 502 return str(label)
503 504
505 -def delete(ri_id=None):
506 """Delete relaxation data corresponding to the relaxation data ID. 507 508 @keyword ri_id: The relaxation data ID string. 509 @type ri_id: str 510 """ 511 512 # Test if the current pipe exists. 513 check_pipe() 514 515 # Test if the sequence data is loaded. 516 if not exists_mol_res_spin_data(): 517 raise RelaxNoSequenceError 518 519 # Check the ID. 520 if ri_id == None: 521 raise RelaxError("The relaxation data ID string must be supplied.") 522 523 # Test if data exists. 524 if not hasattr(cdp, 'ri_ids') or ri_id not in cdp.ri_ids: 525 raise RelaxNoRiError(ri_id) 526 527 # Pop the ID, and remove it from the frequency and type lists. 528 cdp.ri_ids.pop(cdp.ri_ids.index(ri_id)) 529 del cdp.ri_type[ri_id] 530 531 # Prune empty structures. 532 if len(cdp.ri_ids) == 0: 533 del cdp.ri_ids 534 if len(cdp.ri_type) == 0: 535 del cdp.ri_type 536 537 # Loop over the spins, deleting the relaxation data and errors when present. 538 for spin in spin_loop(): 539 # Data deletion. 540 if hasattr(spin, 'ri_data') and ri_id in spin.ri_data: 541 del spin.ri_data[ri_id] 542 if hasattr(spin, 'ri_data_err') and ri_id in spin.ri_data_err: 543 del spin.ri_data_err[ri_id] 544 545 # Prune empty structures. 546 if hasattr(spin, 'ri_data') and len(spin.ri_data) == 0: 547 del spin.ri_data 548 if hasattr(spin, 'ri_data_err') and len(spin.ri_data_err) == 0: 549 del spin.ri_data_err 550 551 # Delete the metadata. 552 if hasattr(cdp, 'exp_info') and hasattr(cdp.exp_info, 'temp_calibration') and ri_id in cdp.exp_info.temp_calibration: 553 del cdp.exp_info.temp_calibration[ri_id] 554 if len(cdp.exp_info.temp_calibration) == 0: 555 del cdp.exp_info.temp_calibration 556 if hasattr(cdp, 'exp_info') and hasattr(cdp.exp_info, 'temp_control') and ri_id in cdp.exp_info.temp_control: 557 del cdp.exp_info.temp_control[ri_id] 558 if len(cdp.exp_info.temp_control) == 0: 559 del cdp.exp_info.temp_control 560 if hasattr(cdp, 'exp_info') and hasattr(cdp.exp_info, 'peak_intensity_type') and ri_id in cdp.exp_info.peak_intensity_type: 561 del cdp.exp_info.peak_intensity_type[ri_id] 562 if len(cdp.exp_info.peak_intensity_type) == 0: 563 del cdp.exp_info.peak_intensity_type 564 565 # Delete the frequency information. 566 delete_frequencies(id=ri_id)
567 568
569 -def display(ri_id=None):
570 """Display relaxation data corresponding to the ID. 571 572 @keyword ri_id: The relaxation data ID string. 573 @type ri_id: str 574 """ 575 576 # Test if the current pipe exists. 577 check_pipe() 578 579 # Test if the sequence data is loaded. 580 if not exists_mol_res_spin_data(): 581 raise RelaxNoSequenceError 582 583 # Test if data exists. 584 if not hasattr(cdp, 'ri_ids') or ri_id not in cdp.ri_ids: 585 raise RelaxNoRiError(ri_id) 586 587 # Print the data. 588 value.write_data(param=ri_id, file=sys.stdout, return_value=return_value, return_data_desc=return_data_desc)
589 590
591 -def get_data_names(global_flag=False, sim_names=False):
592 """Return a list of names of data structures associated with relaxation data. 593 594 Description 595 =========== 596 597 The names are as follows: 598 599 ri_data: Relaxation data. 600 601 ri_data_err: Relaxation data error. 602 603 ri_data_bc: The back calculated relaxation data. 604 605 ri_type: The relaxation data type, i.e. one of ['NOE', 'R1', 'R2'] 606 607 frq: NMR frequencies in Hz, eg [600.0 * 1e6, 500.0 * 1e6] 608 609 610 @keyword global_flag: A flag which if True corresponds to the pipe specific data structures and if False corresponds to the spin specific data structures. 611 @type global_flag: bool 612 @keyword sim_names: A flag which if True will add the Monte Carlo simulation object names as well. 613 @type sim_names: bool 614 @return: The list of object names. 615 @rtype: list of str 616 """ 617 618 # Initialise. 619 names = [] 620 621 # Global data names. 622 if not sim_names and global_flag: 623 names.append('ri_id') 624 names.append('ri_type') 625 names.append('frq') 626 627 # Spin specific data names. 628 if not sim_names and not global_flag: 629 names.append('ri_data') 630 names.append('ri_data_err') 631 names.append('ri_data_bc') 632 633 # Simulation object names. 634 if sim_names and not global_flag: 635 names.append('ri_data_sim') 636 637 # Return the list of names. 638 return names
639 640
641 -def get_ids():
642 """Return the list of all relaxation data IDs. 643 644 @return: The list of all relaxation data IDs. 645 @rtype: list of str 646 """ 647 648 # No pipe. 649 if cdp == None: 650 return [] 651 652 # No relaxation data. 653 if not hasattr(cdp, 'ri_ids'): 654 return [] 655 656 # The relaxation data IDs. 657 return cdp.ri_ids
658 659
660 -def pack_data(ri_id, ri_type, frq, values, errors, spin_ids=None, mol_names=None, res_nums=None, res_names=None, spin_nums=None, spin_names=None, spin_id=None, gen_seq=False, verbose=True):
661 """Pack the relaxation data into the data pipe and spin containers. 662 663 The values, errors, and spin_ids arguments must be lists of equal length or None. Each element i corresponds to a unique spin. 664 665 @param ri_id: The relaxation data ID string. 666 @type ri_id: str 667 @param ri_type: The relaxation data type, ie 'R1', 'R2', or 'NOE'. 668 @type ri_type: str 669 @param frq: The spectrometer proton frequency in Hz. 670 @type frq: float 671 @keyword values: The relaxation data for each spin. 672 @type values: None or list of float or float array 673 @keyword errors: The relaxation data errors for each spin. 674 @type errors: None or list of float or float array 675 @keyword spin_ids: The list of spin ID strings. If the other spin identifiers are given, i.e. mol_names, res_nums, res_names, spin_nums, and/or spin_names, then this argument is not necessary. 676 @type spin_ids: None or list of str 677 @keyword mol_names: The list of molecule names used for creating the spin IDs (if not given) or for generating the sequence data. 678 @type mol_names: None or list of str 679 @keyword res_nums: The list of residue numbers used for creating the spin IDs (if not given) or for generating the sequence data. 680 @type res_nums: None or list of str 681 @keyword res_names: The list of residue names used for creating the spin IDs (if not given) or for generating the sequence data. 682 @type res_names: None or list of str 683 @keyword spin_nums: The list of spin numbers used for creating the spin IDs (if not given) or for generating the sequence data. 684 @type spin_nums: None or list of str 685 @keyword spin_names: The list of spin names used for creating the spin IDs (if not given) or for generating the sequence data. 686 @type spin_names: None or list of str 687 @keyword gen_seq: A flag which if True will cause the molecule, residue, and spin sequence data to be generated. 688 @type gen_seq: bool 689 @keyword verbose: A flag which if True will cause all relaxation data loaded to be printed out. 690 @type verbose: bool 691 """ 692 693 # The number of spins. 694 N = len(values) 695 696 # Test the data. 697 if errors != None and len(errors) != N: 698 raise RelaxError("The length of the errors arg (%s) does not match that of the value arg (%s)." % (len(errors), N)) 699 if spin_ids and len(spin_ids) != N: 700 raise RelaxError("The length of the spin ID strings arg (%s) does not match that of the value arg (%s)." % (len(mol_names), N)) 701 if mol_names and len(mol_names) != N: 702 raise RelaxError("The length of the molecule names arg (%s) does not match that of the value arg (%s)." % (len(mol_names), N)) 703 if res_nums and len(res_nums) != N: 704 raise RelaxError("The length of the residue numbers arg (%s) does not match that of the value arg (%s)." % (len(res_nums), N)) 705 if res_names and len(res_names) != N: 706 raise RelaxError("The length of the residue names arg (%s) does not match that of the value arg (%s)." % (len(res_names), N)) 707 if spin_nums and len(spin_nums) != N: 708 raise RelaxError("The length of the spin numbers arg (%s) does not match that of the value arg (%s)." % (len(spin_nums), N)) 709 if spin_names and len(spin_names) != N: 710 raise RelaxError("The length of the spin names arg (%s) does not match that of the value arg (%s)." % (len(spin_names), N)) 711 712 # Generate some empty lists. 713 if not mol_names: 714 mol_names = [None] * N 715 if not res_nums: 716 res_nums = [None] * N 717 if not res_names: 718 res_names = [None] * N 719 if not spin_nums: 720 spin_nums = [None] * N 721 if not spin_names: 722 spin_names = [None] * N 723 if errors == None: 724 errors = [None] * N 725 726 # Generate the spin IDs. 727 if not spin_ids: 728 spin_ids = [] 729 for i in range(N): 730 spin_ids.append(generate_spin_id_unique(spin_num=spin_nums[i], spin_name=spin_names[i], res_num=res_nums[i], res_name=res_names[i], mol_name=mol_names[i])) 731 732 # Initialise the global data for the current pipe if necessary. 733 if not hasattr(cdp, 'ri_type'): 734 cdp.ri_type = {} 735 if not hasattr(cdp, 'ri_ids'): 736 cdp.ri_ids = [] 737 738 # Set the spectrometer frequency. 739 set_frequency(id=ri_id, frq=frq) 740 741 # Update the global data. 742 cdp.ri_ids.append(ri_id) 743 cdp.ri_type[ri_id] = ri_type 744 745 # The selection object. 746 select_obj = None 747 if spin_id: 748 select_obj = Selection(spin_id) 749 750 # Loop over the spin data. 751 data = [] 752 for i in range(N): 753 # A selection union. 754 select_id = spin_ids[i] 755 if spin_id != None: 756 select_id = "%s&%s" % (select_id, spin_id) 757 758 # Get the corresponding spin container. 759 match_mol_names, match_res_nums, match_res_names, spins = return_spin_from_selection(select_id, full_info=True, multi=True) 760 761 # No spin. 762 if len(spins) == 0: 763 continue 764 765 # Check that multiple spins are not present. 766 if len(spins) > 1: 767 # Generate the list of spin IDs. 768 new_ids = [] 769 for j in range(len(spins)): 770 new_ids.append(generate_spin_id_unique(mol_name=match_mol_names[j], res_num=match_res_nums[j], res_name=match_res_names[j], spin_num=spins[j].num, spin_name=spins[j].name)) 771 772 # Raise the error. 773 raise RelaxMultiSpinIDError(spin_ids[i], new_ids) 774 775 # Check that at least one spin is present. 776 if len(spins) == 0: 777 raise RelaxNoSpinError(spin_ids[i]) 778 779 # Loop over the spins. 780 for j in range(len(spins)): 781 # No match to the selection. 782 if select_obj and not select_obj.contains_spin(spin_num=spins[j].num, spin_name=spins[j].name, res_num=res_nums[j], res_name=res_names[j], mol=mol_names[j]): 783 continue 784 785 # Initialise the spin data if necessary. 786 if not hasattr(spins[j], 'ri_data') or spins[j].ri_data == None: 787 spins[j].ri_data = {} 788 if not hasattr(spins[j], 'ri_data_err') or spins[j].ri_data_err == None: 789 spins[j].ri_data_err = {} 790 791 # Update all data structures. 792 spins[j].ri_data[ri_id] = values[i] 793 spins[j].ri_data_err[ri_id] = errors[i] 794 795 # Append the data for printing out. 796 data.append([spin_ids[i], repr(values[i]), repr(errors[i])]) 797 798 # Print out. 799 if verbose: 800 print("\nThe following %s MHz %s relaxation data with the ID '%s' has been loaded into the relax data store:\n" % (frq/1e6, ri_type, ri_id)) 801 write_data(out=sys.stdout, headings=["Spin_ID", "Value", "Error"], data=data)
802 803
804 -def peak_intensity_type(ri_id=None, type=None):
805 """Set the type of intensity measured for the peaks. 806 807 @keyword ri_id: The relaxation data ID string. 808 @type ri_id: str 809 @keyword type: The peak intensity type, one of 'height' or 'volume'. 810 @type type: str 811 """ 812 813 # Test if the current pipe exists. 814 check_pipe() 815 816 # Test if sequence data is loaded. 817 if not exists_mol_res_spin_data(): 818 raise RelaxNoSequenceError 819 820 # Test if data exists. 821 if not hasattr(cdp, 'ri_ids') or ri_id not in cdp.ri_ids: 822 raise RelaxNoRiError(ri_id) 823 824 # Check the values, and warn if not in the list. 825 valid = ['height', 'volume'] 826 if type not in valid: 827 raise RelaxError("The '%s' peak intensity type is unknown. Please select one of %s." % (type, valid)) 828 829 # Set up the experimental info data container, if needed. 830 if not hasattr(cdp, 'exp_info'): 831 cdp.exp_info = ExpInfo() 832 833 # Store the type. 834 cdp.exp_info.setup_peak_intensity_type(ri_id, type)
835 836
837 -def read(ri_id=None, ri_type=None, frq=None, file=None, dir=None, file_data=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, data_col=None, error_col=None, sep=None, spin_id=None):
838 """Read R1, R2, or NOE, or R2eff relaxation data from a file. 839 840 @param ri_id: The relaxation data ID string. 841 @type ri_id: str 842 @param ri_type: The relaxation data type, ie 'R1', 'R2', 'NOE', or 'R2eff'. 843 @type ri_type: str 844 @param frq: The spectrometer proton frequency in Hz. 845 @type frq: float 846 @param file: The name of the file to open. 847 @type file: str 848 @param dir: The directory containing the file (defaults to the current directory if None). 849 @type dir: str or None 850 @param file_data: An alternative opening a file, if the data already exists in the correct format. The format is a list of lists where the first index corresponds to the row and the second the column. 851 @type file_data: list of lists 852 @keyword spin_id_col: The column containing the spin ID strings. If supplied, the mol_name_col, res_name_col, res_num_col, spin_name_col, and spin_num_col arguments must be none. 853 @type spin_id_col: int or None 854 @keyword mol_name_col: The column containing the molecule name information. If supplied, spin_id_col must be None. 855 @type mol_name_col: int or None 856 @keyword res_name_col: The column containing the residue name information. If supplied, spin_id_col must be None. 857 @type res_name_col: int or None 858 @keyword res_num_col: The column containing the residue number information. If supplied, spin_id_col must be None. 859 @type res_num_col: int or None 860 @keyword spin_name_col: The column containing the spin name information. If supplied, spin_id_col must be None. 861 @type spin_name_col: int or None 862 @keyword spin_num_col: The column containing the spin number information. If supplied, spin_id_col must be None. 863 @type spin_num_col: int or None 864 @keyword data_col: The column containing the relaxation data. 865 @type data_col: int or None 866 @keyword error_col: The column containing the relaxation data errors. 867 @type error_col: int or None 868 @keyword sep: The column separator which, if None, defaults to whitespace. 869 @type sep: str or None 870 @keyword spin_id: The spin ID string used to restrict data loading to a subset of all spins. 871 @type spin_id: None or str 872 """ 873 874 # Test if the current data pipe exists. 875 check_pipe() 876 877 # Test if sequence data exists. 878 if not exists_mol_res_spin_data(): 879 raise RelaxNoSequenceError 880 881 # Test if the ri_id already exists. 882 if hasattr(cdp, 'ri_ids') and ri_id in cdp.ri_ids: 883 raise RelaxError("The relaxation ID string '%s' already exists." % ri_id) 884 885 # Check if the type is valid. 886 if ri_type not in VALID_TYPES: 887 raise RelaxError("The relaxation data type '%s' must be one of %s." % (ri_type, VALID_TYPES)) 888 889 # Frequency checks. 890 frequency_checks(frq) 891 892 # Loop over the file data to create the data structures for packing. 893 values = [] 894 errors = [] 895 mol_names = [] 896 res_nums = [] 897 res_names = [] 898 spin_nums = [] 899 spin_names = [] 900 for data in read_spin_data(file=file, dir=dir, file_data=file_data, 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, data_col=data_col, error_col=error_col, sep=sep): 901 # Unpack. 902 if data_col and error_col: 903 mol_name, res_num, res_name, spin_num, spin_name, value, error = data 904 elif data_col: 905 mol_name, res_num, res_name, spin_num, spin_name, value = data 906 error = None 907 else: 908 mol_name, res_num, res_name, spin_num, spin_name, error = data 909 value = None 910 911 # No data. 912 if value == None and error == None: 913 continue 914 915 # Store all the info. 916 mol_names.append(mol_name) 917 res_nums.append(res_num) 918 res_names.append(res_name) 919 spin_nums.append(spin_num) 920 spin_names.append(spin_name) 921 values.append(value) 922 errors.append(error) 923 924 # Pack the data. 925 pack_data(ri_id, ri_type, frq, values, errors, mol_names=mol_names, res_nums=res_nums, res_names=res_names, spin_nums=spin_nums, spin_names=spin_names, spin_id=spin_id)
926 927
928 -def return_data_desc(name):
929 """Return a description of the spin specific object. 930 931 @param name: The name of the spin specific object. 932 @type name: str 933 """ 934 935 if name == 'ri_data': 936 return 'The relaxation data' 937 if name == 'ri_data_err': 938 return 'The relaxation data errors'
939 940
941 -def return_value(spin, data_type, bc=False):
942 """Return the value and error corresponding to 'data_type'. 943 944 @param spin: The spin container. 945 @type spin: SpinContainer instance 946 @param data_type: The relaxation data ID string. 947 @type data_type: str 948 @keyword bc: A flag which if True will cause the back calculated relaxation data to be written. 949 @type bc: bool 950 """ 951 952 # Relaxation data. 953 data = None 954 if not bc and hasattr(spin, 'ri_data') and spin.ri_data != None and data_type in spin.ri_data: 955 data = spin.ri_data[data_type] 956 957 # Back calculated relaxation data 958 if bc and hasattr(spin, 'ri_data_bc') and spin.ri_data_bc != None and data_type in spin.ri_data_bc: 959 data = spin.ri_data_bc[data_type] 960 961 # Relaxation errors. 962 error = None 963 if hasattr(spin, 'ri_data_err') and spin.ri_data_err != None and data_type in spin.ri_data_err: 964 error = spin.ri_data_err[data_type] 965 966 # Return the data. 967 return data, error
968 969
970 -def temp_calibration(ri_id=None, method=None):
971 """Set the temperature calibration method. 972 973 @keyword ri_id: The relaxation data type, ie 'R1', 'R2', or 'NOE'. 974 @type ri_id: str 975 @keyword method: The temperature calibration method. 976 @type method: str 977 """ 978 979 # Test if the current pipe exists. 980 check_pipe() 981 982 # Test if sequence data is loaded. 983 if not exists_mol_res_spin_data(): 984 raise RelaxNoSequenceError 985 986 # Test if data exists. 987 if not hasattr(cdp, 'ri_ids') or ri_id not in cdp.ri_ids: 988 raise RelaxNoRiError(ri_id) 989 990 # Check the values, and warn if not in the list. 991 valid = ['methanol', 'monoethylene glycol', 'no calibration applied'] 992 if method not in valid: 993 warn(RelaxWarning("The '%s' method is unknown. Please try to use one of %s." % (method, valid))) 994 995 # Set up the experimental info data container, if needed. 996 if not hasattr(cdp, 'exp_info'): 997 cdp.exp_info = ExpInfo() 998 999 # Store the method. 1000 cdp.exp_info.temp_calibration_setup(ri_id, method)
1001 1002
1003 -def temp_control(ri_id=None, method=None):
1004 """Set the temperature control method. 1005 1006 @keyword ri_id: The relaxation data ID string. 1007 @type ri_id: str 1008 @keyword method: The temperature control method. 1009 @type method: str 1010 """ 1011 1012 # Test if the current pipe exists. 1013 check_pipe() 1014 1015 # Test if sequence data is loaded. 1016 if not exists_mol_res_spin_data(): 1017 raise RelaxNoSequenceError 1018 1019 # Test if data exists. 1020 if not hasattr(cdp, 'ri_ids') or ri_id not in cdp.ri_ids: 1021 raise RelaxNoRiError(ri_id) 1022 1023 # Check the values, and warn if not in the list. 1024 valid = ['single scan interleaving', 'temperature compensation block', 'single scan interleaving and temperature compensation block', 'single fid interleaving', 'single experiment interleaving', 'no temperature control applied'] 1025 if method not in valid: 1026 raise RelaxError("The '%s' method is unknown. Please select one of %s." % (method, valid)) 1027 1028 # Set up the experimental info data container, if needed. 1029 if not hasattr(cdp, 'exp_info'): 1030 cdp.exp_info = ExpInfo() 1031 1032 # Store the method. 1033 cdp.exp_info.temp_control_setup(ri_id, method)
1034 1035
1036 -def type(ri_id=None, ri_type=None):
1037 """Set or reset the frequency associated with the ID. 1038 1039 @param ri_id: The relaxation data ID string. 1040 @type ri_id: str 1041 @param ri_type: The relaxation data type, ie 'R1', 'R2', or 'NOE'. 1042 @type ri_type: str 1043 """ 1044 1045 # Test if the current data pipe exists. 1046 check_pipe() 1047 1048 # Test if sequence data exists. 1049 if not exists_mol_res_spin_data(): 1050 raise RelaxNoSequenceError 1051 1052 # Test if data exists. 1053 if not hasattr(cdp, 'ri_ids') or ri_id not in cdp.ri_ids: 1054 raise RelaxNoRiError(ri_id) 1055 1056 # Check if the type is valid. 1057 if ri_type not in VALID_TYPES: 1058 raise RelaxError("The relaxation data type '%s' must be one of %s." % (ri_type, VALID_TYPES)) 1059 1060 # Initialise if needed. 1061 if not hasattr(cdp, 'ri_type'): 1062 cdp.ri_type = {} 1063 1064 # Set the type. 1065 cdp.ri_type[ri_id] = ri_type
1066 1067
1068 -def write(ri_id=None, file=None, dir=None, bc=False, force=False):
1069 """Write relaxation data to a file. 1070 1071 @keyword ri_id: The relaxation data ID string. 1072 @type ri_id: str 1073 @keyword file: The name of the file to create. 1074 @type file: str 1075 @keyword dir: The directory to write to. 1076 @type dir: str or None 1077 @keyword bc: A flag which if True will cause the back calculated relaxation data to be written. 1078 @type bc: bool 1079 @keyword force: A flag which if True will cause any pre-existing file to be overwritten. 1080 @type force: bool 1081 """ 1082 1083 # Test if the current pipe exists. 1084 check_pipe() 1085 1086 # Test if the sequence data is loaded. 1087 if not exists_mol_res_spin_data(): 1088 raise RelaxNoSequenceError 1089 1090 # Test if data exists. 1091 if not hasattr(cdp, 'ri_ids') or ri_id not in cdp.ri_ids: 1092 raise RelaxNoRiError(ri_id) 1093 1094 # Create the file name if none is given. 1095 if file == None: 1096 file = ri_id + ".out" 1097 1098 # Write the data. 1099 value.write(param=ri_id, file=file, dir=dir, bc=bc, force=force, return_value=return_value, return_data_desc=return_data_desc)
1100