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  from warnings import warn 
 29   
 30  # relax module imports. 
 31  from lib.errors import RelaxError 
 32  from lib.warnings import RelaxWarning 
 33   
 34   
35 -def read_seriestab(peak_list=None, file_data=None, int_col=None):
36 """Extract the intensity information from the NMRPipe SeriesTab peak intensity file. 37 38 @keyword peak_list: The peak list object to place all data into. 39 @type peak_list: lib.spectrum.objects.Peak_list instance 40 @keyword file_data: The data extracted from the file converted into a list of lists. 41 @type file_data: list of lists of str 42 @keyword int_col: The column which to multiply the peak intensity data (used by the SeriesTab intensity file format). 43 @type int_col: int 44 @raises RelaxError: When the expected peak intensity is not a float. 45 """ 46 47 # Set start variables. 48 modeline = False 49 mode = False 50 varsline = False 51 header = False 52 53 # Loop over lines, to extract variables and find header size. 54 line_nr = 0 55 for line in file_data: 56 if len(line) > 0: 57 if line[0] == 'REMARK' and line[1] == 'Mode:': 58 modeline = line[2:] 59 mode = modeline[0] 60 elif line[0] == 'VARS': 61 varsline = line[1:] 62 elif line[0] == '1': 63 header = line_nr 64 break 65 line_nr += 1 66 67 # Raise RelaxError, if the MODE is not found. 68 if not (modeline and mode): 69 raise RelaxError("MODE not detected. Expecting line 2:\nREMARK Mode: Summation") 70 71 # Raise RelaxError, if the VARS line is not found. 72 if not (varsline): 73 raise RelaxError("VARS not detected. Expecting line 8:\nVARS INDEX X_AXIS Y_AXIS X_PPM Y_PPM VOL ASS Z_A0") 74 75 # Raise RelaxError, if the header size is not found. 76 if not header: 77 raise RelaxError("'1' not detected in start of line. Cannot determine header size.") 78 79 # Find index of assignment ASS. 80 ass_i = varsline.index('ASS') 81 82 # Chemical shifts preparation. 83 w1_col = None 84 w2_col = None 85 86 # Find index of chemical shift Y_PPM which in sparky is w1. 87 w1_col = varsline.index('Y_PPM') 88 89 # Find index of chemical shift X_PPM which in sparky is w2. 90 w2_col = varsline.index('X_PPM') 91 92 # Make a regular search for Z_A entries. 93 Z_A = re.compile("Z_A*") 94 spectra = list(filter(Z_A.search, varsline)) 95 96 # Find index of Z_A entries. 97 spectra_i = [] 98 for y in spectra: 99 spectra_i.append(varsline.index(y)) 100 101 # Remove the header. 102 file_data = file_data[header:] 103 104 # Loop over the file data. 105 for line in file_data: 106 # Skip non-assigned peaks. 107 if line[ass_i] == '?-?': 108 continue 109 110 # First split by the 2D separator. 111 assign1, assign2 = re.split('-', line[ass_i]) 112 113 # The assignment of the first dimension. 114 row1 = re.split('([a-zA-Z]+)', assign1) 115 name1 = row1[-2] + row1[-1] 116 117 # The assignment of the second dimension. 118 row2 = re.split('([a-zA-Z]+)', assign2) 119 name2 = row2[-2] + row2[-1] 120 121 # Get the residue number for dimension 1. 122 got_res_num1 = True 123 try: 124 res_num1 = int(row1[-3]) 125 except: 126 got_res_num1 = False 127 raise RelaxError("Improperly formatted NMRPipe SeriesTab file, cannot process the residue number for dimension 1 in assignment: %s." % line[0]) 128 129 # Get the residue number for dimension 2. 130 try: 131 res_num2 = int(row2[-3]) 132 except: 133 # We cannot always expect dimension 2 to have residue number. 134 if got_res_num1: 135 res_num2 = res_num1 136 else: 137 res_num2 = None 138 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))) 139 140 # The residue name for dimension 1. 141 got_res_name1 = True 142 try: 143 res_name1 = row1[-4] 144 except: 145 got_res_name1 = False 146 res_name1 = None 147 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))) 148 149 # The residue name for dimension 2. 150 try: 151 res_name2 = row2[-4] 152 except: 153 # We cannot always expect dimension 2 to have residue name. 154 if got_res_name1: 155 res_name2 = res_name1 156 else: 157 res_name2 = None 158 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))) 159 160 # Get the intensities. 161 try: 162 # Loop over the spectra. 163 intensities = [] 164 for i in range(len(spectra)): 165 # The intensity is given by column multiplication. 166 intensities.append(float(line[spectra_i[i]])*float(line[5])) 167 168 # Bad data. 169 except ValueError: 170 raise RelaxError("The peak intensity value %s from the line %s is invalid." % (intensity, line)) 171 172 # Chemical shifts. 173 w1 = None 174 w2 = None 175 if w1_col != None: 176 try: 177 w1 = float(line[w1_col]) 178 except ValueError: 179 raise RelaxError("The chemical shift from the line %s is invalid." % line) 180 if w2_col != None: 181 try: 182 w2 = float(line[w2_col]) 183 except ValueError: 184 raise RelaxError("The chemical shift from the line %s is invalid." % line) 185 186 # Add the assignment to the peak list object. 187 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)
188