1   
  2   
  3   
  4   
  5   
  6   
  7   
  8   
  9   
 10   
 11   
 12   
 13   
 14   
 15   
 16   
 17   
 18   
 19   
 20   
 21   
 22   
 23  """Module containing functions for handling NMRPipe SeriesTab files.""" 
 24   
 25   
 26   
 27  import re 
 28  from warnings import warn 
 29   
 30   
 31  from lib.errors import RelaxError 
 32  from lib.io import open_write_file, strip 
 33  from lib.warnings import RelaxWarning 
 34   
 35   
 37      """Extract the intensity information from the NMRPipe SeriesTab peak intensity file. 
 38   
 39      @keyword peak_list: The peak list object to place all data into. 
 40      @type peak_list:    lib.spectrum.objects.Peak_list instance 
 41      @keyword file_data: The data extracted from the file converted into a list of lists. 
 42      @type file_data:    list of lists of str 
 43      @keyword int_col:  The column which to multiply the peak intensity data (used by the SeriesTab intensity file format). 
 44      @type int_col:     int 
 45      @raises RelaxError: When the expected peak intensity is not a float. 
 46      """ 
 47   
 48       
 49      modeline = False 
 50      mode = False 
 51      varsline = False 
 52      header = False 
 53   
 54       
 55      line_nr = 0 
 56      for line in file_data: 
 57          if len(line) > 0: 
 58              if line[0] == 'REMARK' and line[1] == 'Mode:': 
 59                  modeline = line[2:] 
 60                  mode = modeline[0] 
 61              elif line[0] == 'VARS': 
 62                  varsline = line[1:] 
 63              elif line[0] == '1': 
 64                  header = line_nr 
 65                  break 
 66          line_nr += 1 
 67   
 68       
 69      if not (modeline and mode): 
 70          raise RelaxError("MODE not detected. Expecting line 2:\nREMARK Mode: Summation") 
 71   
 72       
 73      if not (varsline): 
 74          raise RelaxError("VARS not detected. Expecting line 8:\nVARS INDEX X_AXIS Y_AXIS X_PPM Y_PPM VOL ASS Z_A0") 
 75   
 76       
 77      if not header: 
 78          raise RelaxError("'1' not detected in start of line. Cannot determine header size.") 
 79   
 80       
 81      ass_i = varsline.index('ASS') 
 82   
 83       
 84      w1_col = None 
 85      w2_col = None 
 86   
 87       
 88      w1_col = varsline.index('Y_PPM') 
 89   
 90       
 91      w2_col = varsline.index('X_PPM') 
 92   
 93       
 94      Z_A = re.compile("Z_A*") 
 95      spectra = list(filter(Z_A.search, varsline)) 
 96   
 97       
 98      spectra_i = [] 
 99      for y in spectra: 
100          spectra_i.append(varsline.index(y)) 
101   
102       
103      file_data = file_data[header:] 
104   
105       
106      for line in file_data: 
107           
108          if line[ass_i] == '?-?': 
109              continue 
110   
111           
112          assign1, assign2 = re.split('-', line[ass_i]) 
113   
114           
115          row1 = re.split('([a-zA-Z]+)', assign1) 
116          name1 = row1[-2] + row1[-1] 
117   
118           
119          row2 = re.split('([a-zA-Z]+)', assign2) 
120          name2 = row2[-2] + row2[-1] 
121   
122           
123          got_res_num1 = True 
124          try: 
125              res_num1 = int(row1[-3]) 
126          except: 
127              got_res_num1 = False 
128              raise RelaxError("Improperly formatted NMRPipe SeriesTab file, cannot process the residue number for dimension 1 in assignment: %s." % line[0]) 
129   
130           
131          try: 
132              res_num2 = int(row2[-3]) 
133          except: 
134               
135              if got_res_num1: 
136                  res_num2 = res_num1 
137              else: 
138                  res_num2 = None 
139                  warn(RelaxWarning("Improperly formatted NMRPipe SeriesTab file, cannot process the residue number for dimension 2 in assignment: %s. Setting residue number to %s." % (line[0], res_num2))) 
140   
141           
142          got_res_name1 = True 
143          try: 
144              res_name1 = row1[-4] 
145          except: 
146              got_res_name1 = False 
147              res_name1 = None 
148              warn(RelaxWarning("Improperly formatted NMRPipe SeriesTab file, cannot process the residue name for dimension 1 in assignment: %s. Setting residue name to %s." % (line[0], res_name1))) 
149   
150           
151          try: 
152              res_name2 = row2[-4] 
153          except: 
154               
155              if got_res_name1: 
156                  res_name2 = res_name1 
157              else: 
158                  res_name2 = None 
159                  warn(RelaxWarning("Improperly formatted NMRPipe SeriesTab file, cannot process the residue name for dimension 2 in assignment: %s. Setting residue name to %s." % (line[0], res_name2))) 
160   
161           
162          try: 
163               
164              intensities = [] 
165              for i in range(len(spectra)): 
166                   
167                  intensities.append(float(line[spectra_i[i]])*float(line[5])) 
168   
169           
170          except ValueError: 
171              raise RelaxError("The peak intensity value %s from the line %s is invalid." % (intensity, line)) 
172   
173           
174          w1 = None 
175          w2 = None 
176          if w1_col != None: 
177              try: 
178                  w1 = float(line[w1_col]) 
179              except ValueError: 
180                  raise RelaxError("The chemical shift from the line %s is invalid." % line) 
181          if w2_col != None: 
182              try: 
183                  w2 = float(line[w2_col]) 
184              except ValueError: 
185                  raise RelaxError("The chemical shift from the line %s is invalid." % line) 
186   
187           
188          peak_list.add(res_nums=[res_num1, res_num2], res_names=[res_name1, res_name2], spin_names=[name1, name2], shifts=[w1, w2], intensity=intensities, intensity_name=spectra) 
 189