Package generic_fns :: Module intensity
[hide private]
[frames] | no frames]

Source Code for Module generic_fns.intensity

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2004 Edward d'Auvergne                                        # 
  4  #                                                                             # 
  5  # This file is part of the program relax.                                     # 
  6  #                                                                             # 
  7  # relax 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 2 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # relax 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 relax; if not, write to the Free Software                        # 
 19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 20  #                                                                             # 
 21  ############################################################################### 
 22   
 23  from re import split 
 24   
25 -class Intensity:
26 - def __init__(self, relax):
27 """Class containing functions for handelling peak intensities.""" 28 29 self.relax = relax
30 31
32 - def det_dimensions(self):
33 """Determine which are the proton and heteronuclei dimensions of the XEasy text file. 34 35 @return: None 36 """ 37 38 # Loop over the lines of the file until the proton and heteronucleus is reached. 39 for i in xrange(len(self.file_data)): 40 # Extract the data. 41 res_num, w1_name, w2_name, intensity = self.intensity(self.file_data[i]) 42 43 # Proton in w1, heteronucleus in w2. 44 if w1_name == self.proton and w2_name == self.heteronuc: 45 # Set the proton dimension. 46 self.H_dim = 'w1' 47 48 # Print out. 49 print "The proton dimension is w1" 50 51 # Don't continue (waste of time). 52 break 53 54 # Heteronucleus in w1, proton in w2. 55 if w1_name == self.heteronuc and w2_name == self.proton: 56 # Set the proton dimension. 57 self.H_dim = 'w2' 58 59 # Print out. 60 print "The proton dimension is w2" 61 62 # Don't continue (waste of time). 63 break
64 65
66 - def intensity_sparky(self, line):
67 """Function for returning relevant data from the Sparky peak intensity line. 68 69 The residue number, heteronucleus and proton names, and peak intensity will be returned. 70 """ 71 72 # The Sparky assignment. 73 assignment = split('([A-Z]+)', line[0]) 74 assignment = assignment[1:-1] 75 76 # The residue number. 77 try: 78 res_num = int(assignment[1]) 79 except: 80 raise RelaxError, "Improperly formatted Sparky file." 81 82 # Nuclei names. 83 x_name = assignment[2] 84 h_name = assignment[4] 85 86 # The peak intensity column. 87 if self.int_col == None: 88 self.int_col = 3 89 90 # Intensity. 91 try: 92 intensity = float(line[self.int_col]) 93 except ValueError: 94 raise RelaxError, "The peak intensity value " + `intensity` + " from the line " + `line` + " is invalid." 95 96 # Return the data. 97 return res_num, h_name, x_name, intensity
98 99
100 - def intensity_xeasy(self, line):
101 """Function for returning relevant data from the XEasy peak intensity line. 102 103 The residue number, heteronucleus and proton names, and peak intensity will be returned. 104 """ 105 106 # Test for invalid assignment lines which have the column numbers changed and return empty data. 107 if line[4] == 'inv.': 108 return None, None, None, 0.0 109 110 # The residue number. 111 try: 112 res_num = int(line[5]) 113 except: 114 raise RelaxError, "Improperly formatted XEasy file." 115 116 # Nuclei names. 117 if self.H_dim == 'w1': 118 h_name = line[4] 119 x_name = line[7] 120 else: 121 x_name = line[4] 122 h_name = line[7] 123 124 # Intensity (located in column 10). 125 try: 126 intensity = float(line[10]) 127 except ValueError: 128 raise RelaxError, "The peak intensity value " + `intensity` + " from the line " + `line` + " is invalid." 129 130 # Return the data. 131 return res_num, h_name, x_name, intensity
132 133
134 - def number_of_header_lines(self):
135 """Function for determining how many header lines are in the intensity file. 136 137 @return: The number of header lines. 138 @rtype: int 139 """ 140 141 # Sparky. 142 ######### 143 144 # Assume the Sparky file has two header lines! 145 if self.format == 'sparky': 146 return 2 147 148 149 # XEasy. 150 ######## 151 152 # Loop over the lines of the file until a peak intensity value is reached. 153 header_lines = 0 154 for i in xrange(len(self.file_data)): 155 # Try to see if the intensity can be extracted. 156 try: 157 self.intensity(self.file_data[i]) 158 except RelaxError: 159 header_lines = header_lines + 1 160 except IndexError: 161 header_lines = header_lines + 1 162 else: 163 break 164 165 # Return the number of lines. 166 return header_lines
167 168
169 - def read(self, run=None, file=None, dir=None, format=None, heteronuc=None, proton=None, int_col=None, assign_func=None):
170 """Function for reading peak intensity data.""" 171 172 # Arguments. 173 self.run = run 174 self.format = format 175 self.heteronuc = heteronuc 176 self.proton = proton 177 self.int_col = int_col 178 self.assign_func = assign_func 179 180 # Format argument. 181 format_list = ['sparky', 'xeasy'] 182 if self.format not in format_list: 183 raise RelaxArgNotInListError, ('format', self.format, format_list) 184 185 # Sparky. 186 if self.format == 'sparky': 187 # Print out. 188 print "Sparky formatted data file.\n" 189 190 # Set the intensity reading function. 191 self.intensity = self.intensity_sparky 192 193 # XEasy. 194 elif self.format == 'xeasy': 195 # Print out. 196 print "XEasy formatted data file.\n" 197 198 # Set the intensity reading function. 199 self.intensity = self.intensity_xeasy 200 201 # Set the default proton dimension. 202 self.H_dim = 'w1' 203 204 # Test if the run exists. 205 if not self.run in self.relax.data.run_names: 206 raise RelaxNoRunError, self.run 207 208 # Test if sequence data is loaded. 209 if not self.relax.data.res.has_key(self.run): 210 raise RelaxNoSequenceError, self.run 211 212 # Extract the data from the file. 213 self.file_data = self.relax.IO.extract_data(file, dir) 214 215 # Determine the number of header lines. 216 num = self.number_of_header_lines() 217 print "Number of header lines found: " + `num` 218 219 # Remove the header. 220 self.file_data = self.file_data[num:] 221 222 # Strip the data. 223 self.file_data = self.relax.IO.strip(self.file_data) 224 225 # Determine the proton and heteronucleus dimensions in the XEasy text file. 226 if self.format == 'xeasy': 227 self.det_dimensions() 228 229 # Loop over the peak intensity data. 230 for i in xrange(len(self.file_data)): 231 # Extract the data. 232 res_num, H_name, X_name, intensity = self.intensity(self.file_data[i]) 233 234 # Skip data. 235 if X_name != self.heteronuc or H_name != self.proton: 236 warn(RelaxWarning("Proton and heteronucleus names do not match, skipping the data %s: " % `self.file_data[i]`)) 237 continue 238 239 # Find the index of self.relax.data.res[self.run] which corresponds to res_num. 240 index = None 241 for j in xrange(len(self.relax.data.res[self.run])): 242 if self.relax.data.res[self.run][j].num == res_num: 243 index = j 244 break 245 if index == None: 246 warn(RelaxWarning("Cannot find residue number %s within the sequence." % res_num)) 247 continue 248 249 # Remap the data structure 'self.relax.data.res[self.run][index]'. 250 data = self.relax.data.res[self.run][index] 251 252 # Skip unselected residues. 253 if not data.select: 254 continue 255 256 # Assign the data. 257 self.assign_func(run=self.run, i=index, intensity=intensity)
258