Package lib :: Package spectrum :: Module nmrpipe
[hide private]
[frames] | no frames]

Source Code for Module lib.spectrum.nmrpipe

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2013 Troels E. Linnet                                         # 
  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 containing functions for handling NMRPipe SeriesTab files.""" 
 24   
 25   
 26  # Python module imports. 
 27  import re 
 28   
 29  # relax module imports. 
 30  from lib.errors import RelaxError 
 31  from lib.io import open_write_file, strip 
 32   
 33   
34 -def read_seriestab(peak_list=None, file_data=None, int_col=None):
35 """Extract the intensity information from the NMRPipe SeriesTab peak intensity file. 36 37 @keyword peak_list: The peak list object to place all data into. 38 @type peak_list: lib.spectrum.objects.Peak_list instance 39 @keyword file_data: The data extracted from the file converted into a list of lists. 40 @type file_data: list of lists of str 41 @keyword int_col: The column which to multiply the peak intensity data (used by the SeriesTab intensity file format). 42 @type int_col: int 43 @raises RelaxError: When the expected peak intensity is not a float. 44 """ 45 46 # Set start variables. 47 modeline = False 48 mode = False 49 varsline = False 50 header = False 51 52 # Loop over lines, to extract variables and find header size. 53 line_nr = 0 54 for line in file_data: 55 if len(line) > 0: 56 if line[0] == 'REMARK' and line[1] == 'Mode:': 57 modeline = line[2:] 58 mode = modeline[0] 59 elif line[0] == 'VARS': 60 varsline = line[1:] 61 elif line[0] == '1': 62 header = line_nr 63 break 64 line_nr += 1 65 66 # Raise RelaxError, if the MODE is not found. 67 if not (modeline and mode): 68 raise RelaxError("MODE not detected. Expecting line 2:\nREMARK Mode: Summation") 69 70 # Raise RelaxError, if the VARS line is not found. 71 if not (varsline): 72 raise RelaxError("VARS not detected. Expecting line 8:\nVARS INDEX X_AXIS Y_AXIS X_PPM Y_PPM VOL ASS Z_A0") 73 74 # Raise RelaxError, if the header size is not found. 75 if not header: 76 raise RelaxError("'1' not detected in start of line. Cannot determine header size.") 77 78 # Find index of assignment ASS. 79 ass_i = varsline.index('ASS') 80 81 # Make a regular search for Z_A entries. 82 Z_A = re.compile("Z_A*") 83 spectra = list(filter(Z_A.search, varsline)) 84 85 # Find index of Z_A entries. 86 spectra_i = [] 87 for y in spectra: 88 spectra_i.append(varsline.index(y)) 89 90 # Remove the header. 91 file_data = file_data[header:] 92 93 # Loop over the file data. 94 for line in file_data: 95 # Skip non-assigned peaks. 96 if line[ass_i] == '?-?': 97 continue 98 99 # First split by the 2D separator. 100 assign1, assign2 = re.split('-', line[ass_i]) 101 102 # The assignment of the first dimension. 103 row1 = re.split('([a-zA-Z]+)', assign1) 104 name1 = row1[-2] + row1[-1] 105 106 # The assignment of the second dimension. 107 row2 = re.split('([a-zA-Z]+)', assign2) 108 name2 = row2[-2] + row2[-1] 109 110 # Get the residue number. 111 try: 112 res_num = int(row1[-3]) 113 except: 114 raise RelaxError("Improperly formatted NMRPipe SeriesTab file., cannot process the assignment '%s'." % line[0]) 115 116 # Get the intensities. 117 try: 118 # Loop over the spectra. 119 intensities = [] 120 for i in range(len(spectra)): 121 # The intensity is given by column multiplication. 122 intensities.append(float(line[spectra_i[i]])*float(line[5])) 123 124 # Bad data. 125 except ValueError: 126 raise RelaxError("The peak intensity value %s from the line %s is invalid." % (intensity, line)) 127 128 # Add the assignment to the peak list object. 129 peak_list.add(res_nums=[res_num, res_num], spin_names=[name1, name2], intensity=intensities, intensity_name=spectra)
130