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.io import open_write_file, strip 
 33  from lib.warnings import RelaxWarning 
 34   
 35   
36 -def read_seriestab(peak_list=None, file_data=None, int_col=None):
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 # Set start variables. 49 modeline = False 50 mode = False 51 varsline = False 52 header = False 53 54 # Loop over lines, to extract variables and find header size. 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 # Raise RelaxError, if the MODE is not found. 69 if not (modeline and mode): 70 raise RelaxError("MODE not detected. Expecting line 2:\nREMARK Mode: Summation") 71 72 # Raise RelaxError, if the VARS line is not found. 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 # Raise RelaxError, if the header size is not found. 77 if not header: 78 raise RelaxError("'1' not detected in start of line. Cannot determine header size.") 79 80 # Find index of assignment ASS. 81 ass_i = varsline.index('ASS') 82 83 # Chemical shifts preparation. 84 w1_col = None 85 w2_col = None 86 87 # Find index of chemical shift Y_PPM which in sparky is w1. 88 w1_col = varsline.index('Y_PPM') 89 90 # Find index of chemical shift X_PPM which in sparky is w2. 91 w2_col = varsline.index('X_PPM') 92 93 # Make a regular search for Z_A entries. 94 Z_A = re.compile("Z_A*") 95 spectra = list(filter(Z_A.search, varsline)) 96 97 # Find index of Z_A entries. 98 spectra_i = [] 99 for y in spectra: 100 spectra_i.append(varsline.index(y)) 101 102 # Remove the header. 103 file_data = file_data[header:] 104 105 # Loop over the file data. 106 for line in file_data: 107 # Skip non-assigned peaks. 108 if line[ass_i] == '?-?': 109 continue 110 111 # First split by the 2D separator. 112 assign1, assign2 = re.split('-', line[ass_i]) 113 114 # The assignment of the first dimension. 115 row1 = re.split('([a-zA-Z]+)', assign1) 116 name1 = row1[-2] + row1[-1] 117 118 # The assignment of the second dimension. 119 row2 = re.split('([a-zA-Z]+)', assign2) 120 name2 = row2[-2] + row2[-1] 121 122 # Get the residue number for dimension 1. 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 # Get the residue number for dimension 2. 131 try: 132 res_num2 = int(row2[-3]) 133 except: 134 # We cannot always expect dimension 2 to have residue number. 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 # The residue name for dimension 1. 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 # The residue name for dimension 2. 151 try: 152 res_name2 = row2[-4] 153 except: 154 # We cannot always expect dimension 2 to have residue name. 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 # Get the intensities. 162 try: 163 # Loop over the spectra. 164 intensities = [] 165 for i in range(len(spectra)): 166 # The intensity is given by column multiplication. 167 intensities.append(float(line[spectra_i[i]])*float(line[5])) 168 169 # Bad data. 170 except ValueError: 171 raise RelaxError("The peak intensity value %s from the line %s is invalid." % (intensity, line)) 172 173 # Chemical shifts. 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 # Add the assignment to the peak list object. 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