Package specific_fns :: Module relax_fit
[hide private]
[frames] | no frames]

Source Code for Module specific_fns.relax_fit

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2004-2005 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 match 
 24   
 25   
26 -class Relax_fit:
27 - def __init__(self, relax):
28 """Class containing functions for relaxation data.""" 29 30 self.relax = relax
31 32
33 - def read(self, run=None, file=None, dir=None, relax_time=0.0, fit_type=None, format=None, heteronuc=None, proton=None, int_col=None):
34 """Function for reading peak intensity data.""" 35 36 # Arguments. 37 self.run = run 38 self.relax_time = relax_time 39 self.fit_type = fit_type 40 self.format = format 41 self.heteronuc = heteronuc 42 self.proton = proton 43 self.int_col = int_col 44 45 # Fit type argument. 46 fit_type_list = ['exp', 'inv'] 47 if self.fit_type not in fit_type_list: 48 raise RelaxArgNotInListError, ('fit type', self.fit_type, fit_type_list) 49 if self.fit_type == 'exp': 50 print "Two parameter exponential fit." 51 if self.fit_type == 'inv': 52 print "Three parameter inversion recovery fit." 53 54 # Generic intensity function. 55 self.relax.generic.intensity.read(run=run, file=file, dir=dir, format=format, heteronuc=heteronuc, proton=proton, int_col=int_col)
56 57
58 - def read_columnar_results(self, run, file_data):
59 """Function for reading the results file.""" 60 61 # Arguments. 62 self.run = run 63 64 # Extract and remove the header. 65 header = file_data[0] 66 file_data = file_data[1:] 67 68 # Sort the column numbers. 69 col = {} 70 for i in xrange(len(header)): 71 if header[i] == 'Num': 72 col['num'] = i 73 elif header[i] == 'Name': 74 col['name'] = i 75 elif header[i] == 'Selected': 76 col['select'] = i 77 elif header[i] == 'Ref_intensity': 78 col['ref_int'] = i 79 elif header[i] == 'Ref_error': 80 col['ref_err'] = i 81 elif header[i] == 'Sat_intensity': 82 col['sat_int'] = i 83 elif header[i] == 'Sat_error': 84 col['sat_err'] = i 85 elif header[i] == 'NOE': 86 col['noe'] = i 87 elif header[i] == 'NOE_error': 88 col['noe_err'] = i 89 90 # Test the file. 91 if len(col) < 2: 92 raise RelaxInvalidDataError 93 94 95 # Sequence. 96 ########### 97 98 # Generate the sequence. 99 for i in xrange(len(file_data)): 100 # Residue number and name. 101 try: 102 res_num = int(file_data[i][col['num']]) 103 except ValueError: 104 raise RelaxError, "The residue number " + file_data[i][col['num']] + " is not an integer." 105 res_name = file_data[i][col['name']] 106 107 # Add the residue. 108 self.relax.generic.sequence.add(self.run, res_num, res_name, select=int(file_data[i][col['select']])) 109 110 111 # Data. 112 ####### 113 114 # Loop over the file data. 115 for i in xrange(len(file_data)): 116 # Residue number and name. 117 try: 118 res_num = int(file_data[i][col['num']]) 119 except ValueError: 120 raise RelaxError, "The residue number " + file_data[i][col['num']] + " is not an integer." 121 res_name = file_data[i][col['name']] 122 123 # Find the residue index. 124 index = None 125 for j in xrange(len(self.relax.data.res[self.run])): 126 if self.relax.data.res[self.run][j].num == res_num and self.relax.data.res[self.run][j].name == res_name: 127 index = j 128 break 129 if index == None: 130 raise RelaxError, "Residue " + `res_num` + " " + res_name + " cannot be found in the sequence." 131 132 # Reassign data structure. 133 data = self.relax.data.res[self.run][index] 134 135 # Skip unselected residues. 136 if not data.select: 137 continue 138 139 # Reference intensity. 140 try: 141 data.ref = float(file_data[i][col['ref_int']]) 142 except ValueError: 143 data.ref = None 144 145 # Reference error. 146 try: 147 data.ref_err = float(file_data[i][col['ref_err']]) 148 except ValueError: 149 data.ref_err = None 150 151 # Saturated intensity. 152 try: 153 data.sat = float(file_data[i][col['sat_int']]) 154 except ValueError: 155 data.sat = None 156 157 # Saturated error. 158 try: 159 data.sat_err = float(file_data[i][col['sat_err']]) 160 except ValueError: 161 data.sat_err = None 162 163 # NOE. 164 try: 165 data.noe = float(file_data[i][col['noe']]) 166 except ValueError: 167 data.noe = None 168 169 # NOE error. 170 try: 171 data.noe_err = float(file_data[i][col['noe_err']]) 172 except ValueError: 173 data.noe_err = None
174 175
176 - def return_conversion_factor(self, stat_type):
177 """Dummy function for returning 1.0.""" 178 179 return 1.0
180 181
182 - def return_data_name(self, name):
183 """ 184 Relaxation curve fitting data type string matching patterns 185 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 186 187 ____________________________________________________________________________________________ 188 | | | | 189 | Data type | Object name | Patterns | 190 |________________________|______________|__________________________________________________| 191 | | | | 192 | rate | rate | '^[Rr]ate$' | 193 |________________________|______________|__________________________________________________| 194 195 """ 196 197 # Rate. 198 if match('^[Rr]ate$', name): 199 return 'rate'
200 201
202 - def return_grace_string(self, data_type):
203 """Function for returning the Grace string representing the data type for axis labelling.""" 204 205 # Get the object name. 206 object_name = self.return_data_name(data_type) 207 208 # Rate. 209 if object_name == 'rate': 210 grace_string = 'Rate' 211 212 # Return the Grace string. 213 return grace_string
214 215
216 - def return_units(self, stat_type):
217 """Dummy function which returns None as the stats have no units.""" 218 219 return None
220 221
222 - def return_value(self, run, i, data_type='noe'):
223 """Function for returning the NOE value and error.""" 224 225 # Arguments. 226 self.run = run 227 228 # Remap the data structure 'self.relax.data.res[run][i]'. 229 data = self.relax.data.res[run][i] 230 231 # Get the object. 232 object_name = self.return_data_name(data_type) 233 if not object_name: 234 raise RelaxError, "The NOE calculation data type " + `data_type` + " does not exist." 235 object_error = object_name + "_err" 236 237 # Get the value. 238 value = None 239 if hasattr(data, object_name): 240 value = getattr(data, object_name) 241 242 # Get the error. 243 error = None 244 if hasattr(data, object_error): 245 error = getattr(data, object_error) 246 247 # Return the data. 248 return value, error
249 250
251 - def set_error(self, run=None, error=0.0, spectrum_type=None, res_num=None, res_name=None):
252 """Function for setting the errors.""" 253 254 # Arguments. 255 self.run = run 256 self.spectrum_type = spectrum_type 257 self.res_num = res_num 258 self.res_name = res_name 259 260 # Test if the run exists. 261 if not run in self.relax.data.run_names: 262 raise RelaxNoRunError, run 263 264 # Test if the sequence data is loaded. 265 if not self.relax.data.res.has_key(run): 266 raise RelaxNoSequenceError, run 267 268 # Test if the residue number is a valid regular expression. 269 if type(res_num) == str: 270 try: 271 compile(res_num) 272 except: 273 raise RelaxRegExpError, ('residue number', res_num) 274 275 # Test if the residue name is a valid regular expression. 276 if res_name: 277 try: 278 compile(res_name) 279 except: 280 raise RelaxRegExpError, ('residue name', res_name) 281 282 # Loop over the sequence. 283 for i in xrange(len(self.relax.data.res[run])): 284 # Remap the data structure 'self.relax.data.res[self.run][i]'. 285 data = self.relax.data.res[self.run][i] 286 287 # Skip unselected residues. 288 if not data.select: 289 continue 290 291 # If 'res_num' is not None, skip the residue if there is no match. 292 if type(res_num) == int and not data.num == res_num: 293 continue 294 elif type(res_num) == str and not match(res_num, `data.num`): 295 continue 296 297 # If 'res_name' is not None, skip the residue if there is no match. 298 if res_name != None and not match(res_name, data.name): 299 continue 300 301 # Set the error. 302 if self.spectrum_type == 'ref': 303 data.ref_err = float(error) 304 elif self.spectrum_type == 'sat': 305 data.sat_err = float(error)
306 307
308 - def write(self, run=None, file=None, dir=None, force=0):
309 """Function for writing NOE values and errors to a file.""" 310 311 # Arguments 312 self.run = run 313 314 # Test if the run exists. 315 if not self.run in self.relax.data.run_names: 316 raise RelaxNoRunError, self.run 317 318 # Test if the sequence data is loaded. 319 if not self.relax.data.res.has_key(self.run): 320 raise RelaxNoSequenceError, self.run 321 322 # Open the file for writing. 323 noe_file = self.relax.IO.open_write_file(file, dir, force) 324 325 # Write the data. 326 self.relax.generic.value.write_data(self.run, None, noe_file, return_value=self.return_value) 327 328 # Close the file. 329 noe_file.close()
330 331
332 - def write_columnar_line(self, file=None, num=None, name=None, select=None, ref_int=None, ref_err=None, sat_int=None, sat_err=None, noe=None, noe_err=None):
333 """Function for printing a single line of the columnar formatted results.""" 334 335 # Residue number and name. 336 file.write("%-4s %-5s " % (num, name)) 337 338 # Selected flag and data set. 339 file.write("%-9s " % select) 340 if not select: 341 file.write("\n") 342 return 343 344 # Reference and saturated data. 345 file.write("%-25s %-25s " % (ref_int, ref_err)) 346 file.write("%-25s %-25s " % (sat_int, sat_err)) 347 348 # NOE and error. 349 file.write("%-25s %-25s " % (noe, noe_err)) 350 351 # End of the line. 352 file.write("\n")
353 354
355 - def write_columnar_results(self, file, run):
356 """Function for printing the results into a file.""" 357 358 # Arguments. 359 self.run = run 360 361 # Test if the run exists. 362 if not self.run in self.relax.data.run_names: 363 raise RelaxNoRunError, self.run 364 365 # Test if sequence data is loaded. 366 if not self.relax.data.res.has_key(self.run): 367 raise RelaxNoSequenceError, self.run 368 369 370 # Header. 371 ######### 372 373 374 # Write the header line. 375 self.write_columnar_line(file=file, num='Num', name='Name', select='Selected', ref_int='Ref_intensity', ref_err='Ref_error', sat_int='Sat_intensity', sat_err='Sat_error', noe='NOE', noe_err='NOE_error') 376 377 378 # Values. 379 ######### 380 381 # Loop over the sequence. 382 for i in xrange(len(self.relax.data.res[self.run])): 383 # Reassign data structure. 384 data = self.relax.data.res[self.run][i] 385 386 # Unselected residues. 387 if not data.select: 388 self.write_columnar_line(file=file, num=data.num, name=data.name, select=0) 389 continue 390 391 # Reference intensity. 392 ref_int = None 393 if hasattr(data, 'ref'): 394 ref_int = data.ref 395 396 # Reference error. 397 ref_err = None 398 if hasattr(data, 'ref_err'): 399 ref_err = data.ref_err 400 401 # Saturated intensity. 402 sat_int = None 403 if hasattr(data, 'sat'): 404 sat_int = data.sat 405 406 # Saturated error. 407 sat_err = None 408 if hasattr(data, 'sat_err'): 409 sat_err = data.sat_err 410 411 # NOE 412 noe = None 413 if hasattr(data, 'noe'): 414 noe = data.noe 415 416 # NOE error. 417 noe_err = None 418 if hasattr(data, 'noe_err'): 419 noe_err = data.noe_err 420 421 # Write the line. 422 self.write_columnar_line(file=file, num=data.num, name=data.name, select=data.select, ref_int=ref_int, ref_err=ref_err, sat_int=sat_int, sat_err=sat_err, noe=noe, noe_err=noe_err)
423