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

Source Code for Module generic_fns.value

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2003, 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   
 24  from re import compile, match 
 25  import sys 
 26   
 27   
28 -class Value:
29 - def __init__(self, relax):
30 """Class containing functions for the setting up of data structures.""" 31 32 self.relax = relax
33 34
35 - def copy(self, run1=None, run2=None, data_type=None):
36 """Function for copying residue specific data values from run1 to run2.""" 37 38 # Arguments. 39 self.data_type = data_type 40 41 # Test if run1 exists. 42 if not run1 in self.relax.data.run_names: 43 raise RelaxNoRunError, run1 44 45 # Test if run2 exists. 46 if not run2 in self.relax.data.run_names: 47 raise RelaxNoRunError, run2 48 49 # Test if the sequence data for run1 is loaded. 50 if not self.relax.data.res.has_key(run1): 51 raise RelaxNoSequenceError, run1 52 53 # Test if the sequence data for run2 is loaded. 54 if not self.relax.data.res.has_key(run2): 55 raise RelaxNoSequenceError, run2 56 57 # Function type. 58 function_type = self.relax.data.run_types[self.relax.data.run_names.index(run1)] 59 60 # Specific value and error returning function. 61 return_value = self.relax.specific_setup.setup('return_value', function_type) 62 63 # Specific set function. 64 set = self.relax.specific_setup.setup('set', function_type) 65 66 # Test if the data exists for run2. 67 for i in xrange(len(self.relax.data.res[run2])): 68 # Get the value and error for run2. 69 value, error = return_value(run2, i, data_type) 70 71 # Data exists. 72 if value != None or error != None: 73 raise RelaxValueError, (data_type, run2) 74 75 # Copy the values. 76 for i in xrange(len(self.relax.data.res[run1])): 77 # Get the value and error for run1. 78 value, error = return_value(run1, i, data_type) 79 80 # Set the values of run2. 81 set(run=run2, value=value, error=error, data_type=data_type, index=i) 82 83 # Reset the minimisation statistics. 84 self.reset_min_stats(run2, i)
85 86
87 - def display(self, run=None, data_type=None):
88 """Function for displaying residue specific data values.""" 89 90 # Arguments. 91 self.run = run 92 self.data_type = data_type 93 94 # Test if the run exists. 95 if not self.run in self.relax.data.run_names: 96 raise RelaxNoRunError, self.run 97 98 # Test if the sequence data is loaded. 99 if not self.relax.data.res.has_key(self.run): 100 raise RelaxNoSequenceError, self.run 101 102 # Print the data. 103 self.write_data(run, data_type, sys.stdout)
104 105
106 - def read(self, run=None, data_type=None, file=None, num_col=0, name_col=1, data_col=2, error_col=3, sep=None):
107 """Function for reading residue specific data values from a file.""" 108 109 # Arguments. 110 self.run = run 111 self.data_type = data_type 112 113 # Test if the run exists. 114 if not self.run in self.relax.data.run_names: 115 raise RelaxNoRunError, self.run 116 117 # Test if sequence data is loaded. 118 if not self.relax.data.res.has_key(self.run): 119 raise RelaxNoSequenceError, self.run 120 121 # Function type. 122 function_type = self.relax.data.run_types[self.relax.data.run_names.index(self.run)] 123 124 # Specific value and error returning function. 125 return_value = self.relax.specific_setup.setup('return_value', function_type) 126 127 # Specific set function. 128 set = self.relax.specific_setup.setup('set', function_type) 129 130 # Test data corresponding to data_type already exists. 131 for i in xrange(len(self.relax.data.res[self.run])): 132 # Get the value and error. 133 value, error = return_value(self.run, i, data_type) 134 135 # Data exists. 136 if value != None or error != None: 137 raise RelaxValueError, (data_type, self.run) 138 139 # Extract the data from the file. 140 file_data = self.relax.IO.extract_data(file) 141 142 # Count the number of header lines. 143 header_lines = 0 144 for i in xrange(len(file_data)): 145 try: 146 int(file_data[i][num_col]) 147 except: 148 header_lines = header_lines + 1 149 else: 150 break 151 152 # Remove the header. 153 file_data = file_data[header_lines:] 154 155 # Strip the data. 156 file_data = self.relax.IO.strip(file_data) 157 158 # Do nothing if the file does not exist. 159 if not file_data: 160 raise RelaxFileEmptyError 161 162 # Test the validity of the data. 163 for i in xrange(len(file_data)): 164 try: 165 # Number column. 166 int(file_data[i][num_col]) 167 168 # Value column. 169 if file_data[i][data_col] != 'None': 170 float(file_data[i][data_col]) 171 172 # Error column. 173 if error_col != None and file_data[i][error_col] != 'None': 174 float(file_data[i][error_col]) 175 176 except ValueError: 177 if error_col != None: 178 raise RelaxError, "The data is invalid (num=" + file_data[i][num_col] + ", name=" + file_data[i][name_col] + ", data=" + file_data[i][data_col] + ", error=" + file_data[i][error_col] + ")." 179 else: 180 raise RelaxError, "The data is invalid (num=" + file_data[i][num_col] + ", name=" + file_data[i][name_col] + ", data=" + file_data[i][data_col] + ")." 181 182 # Loop over the data. 183 for i in xrange(len(file_data)): 184 # Residue number. 185 res_num = int(file_data[i][num_col]) 186 187 # Residue name. 188 res_name = file_data[i][name_col] 189 190 # Value. 191 if file_data[i][data_col] != 'None': 192 value = float(file_data[i][data_col]) 193 else: 194 value = None 195 196 # Error. 197 if error_col != None and file_data[i][error_col] != 'None': 198 error = float(file_data[i][error_col]) 199 else: 200 error = None 201 202 # Find the index of self.relax.data.res[self.run] which corresponds to the relaxation data set i. 203 index = None 204 for j in xrange(len(self.relax.data.res[self.run])): 205 if self.relax.data.res[self.run][j].num == res_num and self.relax.data.res[self.run][j].name == res_name: 206 index = j 207 break 208 if index == None: 209 raise RelaxNoResError, (res_num, res_name) 210 211 # Set the values of run2. 212 set(run=run, value=value, error=error, data_type=data_type, index=i) 213 214 # Reset the minimisation statistics. 215 self.reset_min_stats(self.run, i)
216 217
218 - def set(self, run=None, value=None, data_type=None, res_num=None, res_name=None, force=0):
219 """Function for setting residue specific data values.""" 220 221 # Test if the run exists. 222 if not run in self.relax.data.run_names: 223 raise RelaxNoRunError, run 224 225 # Test if the sequence data is loaded. 226 if not self.relax.data.res.has_key(run): 227 raise RelaxNoSequenceError, run 228 229 # Test if the residue number is a valid regular expression. 230 if type(res_num) == str: 231 try: 232 compile(res_num) 233 except: 234 raise RelaxRegExpError, ('residue number', res_num) 235 236 # Test if the residue name is a valid regular expression. 237 if res_name: 238 try: 239 compile(res_name) 240 except: 241 raise RelaxRegExpError, ('residue name', res_name) 242 243 # Function type. 244 function_type = self.relax.data.run_types[self.relax.data.run_names.index(run)] 245 246 # Specific value and error returning function. 247 return_value = self.relax.specific_setup.setup('return_value', function_type) 248 249 # Specific set function. 250 set = self.relax.specific_setup.setup('set', function_type) 251 252 # Test data corresponding to data_type already exists. 253 if data_type and not force: 254 # Create an array with all data types. 255 if type(data_type) == str: 256 data_type_array = [data_type] 257 else: 258 data_type_array = data_type 259 260 # Test if the values already exist. 261 for i in xrange(len(self.relax.data.res[run])): 262 # Skip unselected residues. 263 if not self.relax.data.res[run][i].select: 264 continue 265 266 # If 'res_num' is not None, skip the residue if there is no match. 267 if type(res_num) == int and not self.relax.data.res[run][i].num == res_num: 268 continue 269 elif type(res_num) == str and not match(res_num, `self.relax.data.res[run][i].num`): 270 continue 271 272 # If 'res_name' is not None, skip the residue if there is no match. 273 if res_name != None and not match(res_name, self.relax.data.res[run][i].name): 274 continue 275 276 # Loop over the data types. 277 for name in data_type_array: 278 # Get the value and error. 279 temp_value, temp_error = return_value(run, i, name) 280 281 # Data exists. 282 if temp_value != None or temp_error != None: 283 raise RelaxValueError, (name, run) 284 285 # Loop over the sequence. 286 for i in xrange(len(self.relax.data.res[run])): 287 # Residue skipping. 288 ################### 289 290 # Skip unselected residues. 291 if not self.relax.data.res[run][i].select: 292 continue 293 294 # If 'res_num' is not None, skip the residue if there is no match. 295 if type(res_num) == int and not self.relax.data.res[run][i].num == res_num: 296 continue 297 elif type(res_num) == str and not match(res_num, `self.relax.data.res[run][i].num`): 298 continue 299 300 # If 'res_name' is not None, skip the residue if there is no match. 301 if res_name != None and not match(res_name, self.relax.data.res[run][i].name): 302 continue 303 304 305 # Go to the specific code. 306 ########################## 307 308 # Setting the model parameters prior to minimisation. 309 if data_type == None: 310 set(run=run, value=value, error=None, data_type=data_type, index=i) 311 312 # Single data type. 313 if type(data_type) == str: 314 set(run=run, value=value, error=None, data_type=data_type, index=i) 315 316 # Multiple data type. 317 if type(data_type) == list: 318 for j in xrange(len(data_type)): 319 # Get the value of the data type 'j'. 320 if type(value) == None: 321 val = None 322 elif type(value) == list: 323 val = value[j] 324 else: 325 val = value 326 327 # Set the value of data type 'j' to 'val'. 328 set(run=run, value=val, error=None, data_type=data_type[j], index=i) 329 330 # Reset the minimisation statistics. 331 self.reset_min_stats(run, i)
332 333
334 - def reset_min_stats(self, run, i):
335 """Reset the minimisation statistics.""" 336 337 # Chi-squared. 338 if hasattr(self.relax.data.res[run][i], 'chi2'): 339 self.relax.data.res[run][i].chi2 = None 340 341 # Iteration count. 342 if hasattr(self.relax.data.res[run][i], 'iter'): 343 self.relax.data.res[run][i].iter = None 344 345 # Function count. 346 if hasattr(self.relax.data.res[run][i], 'f_count'): 347 self.relax.data.res[run][i].f_count = None 348 349 # Gradient count. 350 if hasattr(self.relax.data.res[run][i], 'g_count'): 351 self.relax.data.res[run][i].g_count = None 352 353 # Hessian count. 354 if hasattr(self.relax.data.res[run][i], 'h_count'): 355 self.relax.data.res[run][i].h_count = None 356 357 # Warning. 358 if hasattr(self.relax.data.res[run][i], 'warning'): 359 self.relax.data.res[run][i].warning = None
360 361
362 - def write(self, run=None, data_type=None, file=None, dir=None, force=0):
363 """Function for writing data to a file.""" 364 365 # Arguments. 366 self.run = run 367 368 # Test if the run exists. 369 if not self.run in self.relax.data.run_names: 370 raise RelaxNoRunError, self.run 371 372 # Test if the sequence data is loaded. 373 if not self.relax.data.res.has_key(self.run): 374 raise RelaxNoSequenceError, self.run 375 376 # Open the file for writing. 377 file = self.relax.IO.open_write_file(file, dir, force) 378 379 # Write the data. 380 self.write_data(run, data_type, file) 381 382 # Close the file. 383 file.close()
384 385
386 - def write_data(self, run, data_type, file, return_value=None):
387 """Function for writing data.""" 388 389 # Get the value and error returning function if required. 390 if not return_value: 391 # Function type. 392 function_type = self.relax.data.run_types[self.relax.data.run_names.index(run)] 393 394 # Specific value and error returning function. 395 return_value = self.relax.specific_setup.setup('return_value', function_type) 396 397 # Write a header line. 398 file.write("%-5s%-6s%-30s%-30s\n" % ('Num', 'Name', 'Value', 'Error')) 399 400 # Loop over the sequence. 401 for i in xrange(len(self.relax.data.res[run])): 402 # Remap the data structure 'self.relax.data.res[run][i]'. 403 data = self.relax.data.res[run][i] 404 405 # Get the value and error. 406 value, error = return_value(run, i, data_type) 407 408 # Write the data. 409 file.write("%-5i%-6s%-30s%-30s\n" % (data.num, data.name, `value`, `error`))
410