Package model_selection :: Module overall_disc
[hide private]
[frames] | no frames]

Source Code for Module model_selection.overall_disc

  1  # Calculate the overall discrepancy. 
  2  # 
  3  # The input relaxation data for this method should be the operating model data (the theoretical, back calculated 
  4  # relaxation values) which has been Gaussian randomised for a given error.  The original operating model data 
  5  # should be placed in the file 'op_data'.  The format of the file should be as follows.  The first line is a 
  6  # header line and is ignored.  The columns are: 
  7  #       0 - Residue number. 
  8  #       1 - Residue name. 
  9  #       2+ - The real data.  Each column from 2 on should correspond to the data specified in the file 'input'. 
 10  #       When the data in 'input' is 'none', there should be no corresponding column. 
 11  # 
 12  # 
 13  # The program is divided into the following stages: 
 14  #       Stage 1:   Creation of the files for the model-free calculations for models 1 to 5 for the randomised 
 15  #               data. 
 16  #       Stage 2a:  Calculation of the overall discrepancy for model selection, and the creation of the final 
 17  #               run.  Monte Carlo simulations are used to find errors, and the diffusion tensor is unoptimised. 
 18  #               Files are placed in the directory 'final'. 
 19  #       Stage 2b:  Calculation of the overall discrepancy for model selection, and the creation of the final 
 20  #               optimization run.  Monte Carlo simulations are used to find errors, and the diffusion tensor 
 21  #               is optimised.  Files are placed in the directory 'optimised'. 
 22  #       Stage 3:   Extraction of optimised data. 
 23   
 24  import sys 
 25  from math import log, pi 
 26   
 27  from common_ops import common_operations 
 28   
 29   
30 -class overall_disc(common_operations):
31 - def __init__(self, mf):
32 "Calculation of the theoretical overall discrepancy." 33 34 self.mf = mf
35 36
37 - def model_selection(self):
38 "Model selection." 39 40 data = self.mf.data.data 41 self.mf.data.calc_frq() 42 self.mf.data.calc_constants() 43 n = float(self.mf.data.num_data_sets) 44 tm = float(self.mf.usr_param.tm['val']) * 1e-9 45 46 if self.mf.debug: 47 self.mf.log.write("\n\n<<< " + self.mf.usr_param.method + " model selection >>>\n\n") 48 49 for res in range(len(self.mf.data.relax_data[0])): 50 self.mf.data.results.append({}) 51 52 if self.mf.debug: 53 self.mf.log.write('%-22s\n' % ( "Checking res " + data['m1'][res]['res_num'] )) 54 55 real = [] 56 err = [] 57 types = [] 58 for i in range(self.mf.data.num_ri): 59 real.append(float(self.mf.data.overall_disc.op_data[res][i+2])) 60 err.append(float(self.mf.data.relax_data[i][res][3])) 61 types.append([self.mf.data.data_types[i], float(self.mf.data.frq[self.mf.data.remap_table[i]])]) 62 63 for model in self.mf.data.runs: 64 back_calc = [] 65 for i in range(self.mf.data.num_ri): 66 label_fit = self.mf.data.frq_label[self.mf.data.remap_table[i]] + "_" + self.mf.data.data_types[i] + "_fit" 67 back_calc.append(float(self.mf.data.data[model][res][label_fit])) 68 69 chi2 = self.mf.calc_chi2.relax_data(real, err, back_calc) 70 71 if self.mf.debug: 72 self.mf.log.write("\nReal: " + `real`) 73 self.mf.log.write("\nError: " + `err`) 74 self.mf.log.write("\nBack calc: " + `back_calc`) 75 76 data[model][res]['crit'] = chi2 / (2.0 * n) 77 78 # Select model. 79 min = 'm1' 80 for run in self.mf.data.runs: 81 if data[run][res]['crit'] < data[min][res]['crit']: 82 min = run 83 if data[min][res]['crit'] == float('inf'): 84 self.mf.data.results[res] = self.fill_results(data[min][res], model='0') 85 else: 86 self.mf.data.results[res] = self.fill_results(data[min][res], model=min[1]) 87 88 if self.mf.debug: 89 self.mf.log.write(self.mf.usr_param.method + " (m1): " + `data['m1'][res]['crit']` + "\n") 90 self.mf.log.write(self.mf.usr_param.method + " (m2): " + `data['m2'][res]['crit']` + "\n") 91 self.mf.log.write(self.mf.usr_param.method + " (m3): " + `data['m3'][res]['crit']` + "\n") 92 self.mf.log.write(self.mf.usr_param.method + " (m4): " + `data['m4'][res]['crit']` + "\n") 93 self.mf.log.write(self.mf.usr_param.method + " (m5): " + `data['m5'][res]['crit']` + "\n") 94 self.mf.log.write("The selected model is: " + min + "\n\n")
95 96
97 - def print_data(self):
98 "Print all the data into the 'data_all' file." 99 100 file = open('data_all', 'w') 101 file_crit = open('crit', 'w') 102 103 sys.stdout.write("[") 104 for res in range(len(self.mf.data.results)): 105 sys.stdout.write("-") 106 file.write("\n\n<<< Residue " + self.mf.data.results[res]['res_num']) 107 file.write(", Model " + self.mf.data.results[res]['model'] + " >>>\n") 108 file.write('%-20s' % '') 109 file.write('%-19s' % 'Model 1') 110 file.write('%-19s' % 'Model 2') 111 file.write('%-19s' % 'Model 3') 112 file.write('%-19s' % 'Model 4') 113 file.write('%-19s' % 'Model 5') 114 115 file_crit.write('%-6s' % self.mf.data.results[res]['res_num']) 116 file_crit.write('%-6s' % self.mf.data.results[res]['model']) 117 118 # S2. 119 file.write('\n%-20s' % 'S2') 120 for run in self.mf.data.runs: 121 file.write('%9.3f' % self.mf.data.data[run][res]['s2']) 122 file.write('%1s' % '±') 123 file.write('%-9.3f' % self.mf.data.data[run][res]['s2_err']) 124 125 # S2f. 126 file.write('\n%-20s' % 'S2f') 127 for run in self.mf.data.runs: 128 file.write('%9.3f' % self.mf.data.data[run][res]['s2f']) 129 file.write('%1s' % '±') 130 file.write('%-9.3f' % self.mf.data.data[run][res]['s2f_err']) 131 132 # S2s. 133 file.write('\n%-20s' % 'S2s') 134 for run in self.mf.data.runs: 135 file.write('%9.3f' % self.mf.data.data[run][res]['s2s']) 136 file.write('%1s' % '±') 137 file.write('%-9.3f' % self.mf.data.data[run][res]['s2s_err']) 138 139 # te. 140 file.write('\n%-20s' % 'te') 141 for run in self.mf.data.runs: 142 file.write('%9.2f' % self.mf.data.data[run][res]['te']) 143 file.write('%1s' % '±') 144 file.write('%-9.2f' % self.mf.data.data[run][res]['te_err']) 145 146 # Rex. 147 file.write('\n%-20s' % 'Rex') 148 for run in self.mf.data.runs: 149 file.write('%9.3f' % self.mf.data.data[run][res]['rex']) 150 file.write('%1s' % '±') 151 file.write('%-9.3f' % self.mf.data.data[run][res]['rex_err']) 152 153 # Chi2. 154 file.write('\n%-20s' % 'Chi2') 155 for run in self.mf.data.runs: 156 file.write('%-19.3f' % self.mf.data.data[run][res]['chi2']) 157 158 # Model selection criteria. 159 file.write('\n%-20s' % self.mf.usr_param.method) 160 for run in self.mf.data.runs: 161 file.write('%-19.6f' % self.mf.data.data[run][res]['crit']) 162 163 file_crit.write('%-25s' % `self.mf.data.data[run][res]['crit']`) 164 file_crit.write('\n') 165 166 file.write('\n') 167 sys.stdout.write("]\n") 168 file.close()
169