Package specific_analyses :: Package model_free :: Module bmrb
[hide private]
[frames] | no frames]

Source Code for Module specific_analyses.model_free.bmrb

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2009-2014 Edward d'Auvergne                                   # 
  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  """The model-free analysis BMRB functions.""" 
 24   
 25  # Python module imports. 
 26  from math import pi 
 27   
 28  # relax module imports. 
 29  from lib.errors import RelaxError 
 30  from pipe_control import bmrb, mol_res_spin 
 31  from specific_analyses.model_free.model import model_map 
 32   
 33   
34 -def from_bmrb_model(name=None):
35 """The model-free model name to BMRB name mapping. 36 37 @keyword name: The BMRB model name. 38 @type name: str 39 @return: The corresponding model-free model name. 40 @rtype: str 41 """ 42 43 # The same name. 44 if name in ['m0', 'm1', 'm2', 'm3', 'm4', 'm5', 'm6', 'm7', 'm8', 'm9', 'm0', 'tm1', 'tm2', 'tm3', 'tm4', 'tm5', 'tm6', 'tm7', 'tm8', 'tm9']: 45 return name 46 47 # Conversion of Modelfree4 (and relax) model numbers. 48 if name in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']: 49 return 'm' + name 50 51 # The BMRB to model-free model name map. 52 map = {'': 'm0', 53 'S2': 'm1', 54 'S2, te': 'm2', 55 'S2, Rex': 'm3', 56 'S2, te, Rex': 'm4', 57 'S2, te, S2f': 'm5', 58 'S2f, S2, ts': 'm5', 59 'S2f, tf, S2, ts': 'm6', 60 'S2f, S2, ts, Rex': 'm7', 61 'S2f, tf, S2, ts, Rex': 'm8', 62 'Rex': 'm9', 63 'tm': 'tm0', 64 'tm, S2': 'tm1', 65 'tm, S2, te': 'tm2', 66 'tm, S2, Rex': 'tm3', 67 'tm, S2, te, Rex': 'tm4', 68 'tm, S2f, S2, ts': 'tm5', 69 'tm, S2f, tf, S2, ts': 'tm6', 70 'tm, S2f, S2, ts, Rex': 'tm7', 71 'tm, S2f, tf, S2, ts, Rex': 'tm8', 72 'tm, Rex': 'tm9' 73 } 74 75 # Loop over the dictionary. 76 for item in map.items(): 77 # Normal match. 78 if item[0] == name: 79 return item[1] 80 81 # No whitespace. 82 if item[0].replace(' ', '') == name: 83 return item[1] 84 85 # Should not be here! 86 if name: 87 raise RelaxError("The BMRB model-free model '%s' is unknown." % name)
88 89
90 -def sf_csa_read(star):
91 """Place the CSA data from the saveframe records into the spin container. 92 93 @param star: The NMR-STAR dictionary object. 94 @type star: NMR_STAR instance 95 """ 96 97 # Get the entities. 98 for data in star.chem_shift_anisotropy.loop(): 99 # The number of spins. 100 N = bmrb.num_spins(data) 101 102 # No data in the saveframe. 103 if N == 0: 104 continue 105 106 # The molecule names. 107 mol_names = bmrb.molecule_names(data, N) 108 109 # Loop over the spins. 110 for i in range(len(data['data_ids'])): 111 # Generate a spin ID. 112 spin_id = mol_res_spin.generate_spin_id_unique(mol_name=mol_names[i], res_num=data['res_nums'][i], spin_name=data['atom_names'][i]) 113 114 # Obtain the spin. 115 spin = mol_res_spin.return_spin(spin_id) 116 117 # The CSA value (converted from ppm). 118 setattr(spin, 'csa', data['csa'][i] * 1e-6)
119 120
121 -def sf_model_free_read(star, sample_conditions=None):
122 """Fill the spin containers with the model-free data from the saveframe records. 123 124 @param star: The NMR-STAR dictionary object. 125 @type star: NMR_STAR instance 126 @keyword sample_conditions: The sample condition label to read. Only one sample condition can be read per data pipe. 127 @type sample_conditions: None or str 128 """ 129 130 # The list of model-free parameters (both bmrblib names and relax names). 131 mf_bmrb_key = ['bond_length', 'local_tm', 's2', 's2f', 's2s', 'te', 'tf', 'ts', 'rex', 'chi2'] 132 mf_params = ['r', 'local_tm', 's2', 's2f', 's2s', 'te', 'tf', 'ts', 'rex', 'chi2'] 133 134 # Get the entities. 135 for data in star.model_free.loop(): 136 # Store the keys. 137 keys = data.keys() 138 139 # Sample conditions do not match (remove the $ sign). 140 if 'sample_cond_list_label' in keys and sample_conditions and data['sample_cond_list_label'].replace('$', '') != sample_conditions: 141 continue 142 143 # Global data. 144 if 'global_chi2' in keys: 145 setattr(cdp, 'chi2', data['global_chi2']) 146 147 # The number of spins. 148 N = bmrb.num_spins(data) 149 150 # No data in the saveframe. 151 if N == 0: 152 continue 153 154 # The molecule names. 155 mol_names = bmrb.molecule_names(data, N) 156 157 # Missing atom names. 158 if 'atom_names' not in keys or data['atom_names'] == None: 159 data['atom_names'] = [None] * N 160 161 # Generate the sequence if needed. 162 bmrb.generate_sequence(N, spin_names=data['atom_names'], res_nums=data['res_nums'], res_names=data['res_names'], mol_names=mol_names) 163 164 # Correlation time scaling. 165 table = {'s': 1.0, 166 'ns': 1e-9, 167 'ps': 1e-12} 168 te_scale = 1.0 169 if data['te_units']: 170 te_scale = table[data['te_units']] 171 172 # Fast correlation time scaling. 173 if data['tf_units']: 174 tf_scale = table[data['tf_units']] 175 else: 176 tf_scale = te_scale 177 178 # Slow correlation time scaling. 179 if data['ts_units']: 180 ts_scale = table[data['ts_units']] 181 else: 182 ts_scale = te_scale 183 184 # Rex scaling. 185 rex_scale = 1.0 186 if hasattr(cdp, 'spectrometer_frq') and len(cdp.spectrometer_frq): 187 rex_scale = 1.0 / (2.0*pi*cdp.spectrometer_frq[cdp.ri_ids[0]])**2 188 189 # Loop over the spins. 190 for i in range(N): 191 # Generate a spin ID. 192 spin_id = mol_res_spin.generate_spin_id_unique(mol_name=mol_names[i], res_name=data['res_names'][i], res_num=data['res_nums'][i], spin_name=data['atom_names'][i]) 193 194 # Obtain the spin. 195 spin = mol_res_spin.return_spin(spin_id) 196 197 # No spin?!? 198 if spin == None: 199 raise RelaxError("The spin '%s' does not exist." % spin_id) 200 201 # Loop over and set the model-free parameters. 202 for j in range(len(mf_params)): 203 # The parameter. 204 param = mf_params[j] 205 206 # No parameter. 207 if not mf_bmrb_key[j] in keys: 208 continue 209 210 # The parameter and its value. 211 if data[mf_bmrb_key[j]] != None: 212 # The value. 213 value = data[mf_bmrb_key[j]][i] 214 215 # Parameter scaling. 216 if value != None: 217 if param == 'te': 218 value = value * te_scale 219 elif param == 'tf': 220 value = value * tf_scale 221 elif param == 'ts': 222 value = value * ts_scale 223 elif param == 'rex': 224 value = value * rex_scale 225 226 # No parameter value. 227 else: 228 value = None 229 230 # Set the parameter. 231 setattr(spin, param, value) 232 233 # The error. 234 mf_bmrb_key_err = mf_bmrb_key[j] + '_err' 235 error = None 236 if mf_bmrb_key_err in keys and data[mf_bmrb_key_err] != None: 237 error = data[mf_bmrb_key_err][i] 238 239 # Error scaling. 240 if error != None: 241 if param == 'te': 242 error = error * te_scale 243 elif param == 'tf': 244 error = error * tf_scale 245 elif param == 'ts': 246 error = error * ts_scale 247 elif param == 'rex': 248 error = error * rex_scale 249 250 # Set the error. 251 mf_param_err = param + '_err' 252 if mf_bmrb_key_err in keys and data[mf_bmrb_key_err] != None: 253 setattr(spin, mf_param_err, error) 254 255 # The model. 256 if data['model_fit'] != None and data['model_fit'][i] != None: 257 model = from_bmrb_model(data['model_fit'][i]) 258 setattr(spin, 'model', model) 259 260 # The equation and parameters. 261 equation, params = model_map(model) 262 setattr(spin, 'equation', equation) 263 setattr(spin, 'params', params) 264 265 # Convert te values which should be ts! 266 if hasattr(spin, 'model') and spin.model in ['m5', 'm6', 'm7', 'm8'] and hasattr(spin, 'te') and spin.te != None: 267 # Change the parameter name of te to ts. 268 spin.ts = spin.te 269 if hasattr(spin, 'te_err'): 270 spin.ts_err = spin.te_err 271 272 # Set the te and te_err values to None. 273 spin.te = None 274 spin.te_err = None 275 276 # The element. 277 if'atom_types' in keys and data['atom_types'] != None: 278 setattr(spin, 'element', data['atom_types'][i]) 279 280 # Heteronucleus type. 281 if'atom_types' in keys and data['atom_types'] != None and data['atom_types'][i] != None and 'isotope' in keys and data['isotope'] != None: 282 # The isotope number. 283 iso_num = data['isotope'][i] 284 285 # No isotope number. 286 iso_table = {'C': 13, 'N': 15} 287 if not data['isotope'][i]: 288 iso_num = iso_table[data['atom_types'][i]] 289 290 # Set the type. 291 setattr(spin, 'isotope', str(iso_num) + data['atom_types'][i])
292 293
294 -def to_bmrb_model(name=None):
295 """Convert the model-free model name to the BMRB name. 296 297 @keyword name: The model-free model name. 298 @type name: str 299 @return: The corresponding BMRB model name. 300 @rtype: str 301 """ 302 303 # The relax to BMRB model-free model name map. 304 map = {'m0': '', 305 'm1': 'S2', 306 'm2': 'S2, te', 307 'm3': 'S2, Rex', 308 'm4': 'S2, te, Rex', 309 'm5': 'S2f, S2, ts', 310 'm6': 'S2f, tf, S2, ts', 311 'm7': 'S2f, S2, ts, Rex', 312 'm8': 'S2f, tf, S2, ts, Rex', 313 'm9': 'Rex', 314 'tm0': 'tm', 315 'tm1': 'tm, S2', 316 'tm2': 'tm, S2, te', 317 'tm3': 'tm, S2, Rex', 318 'tm4': 'tm, S2, te, Rex', 319 'tm5': 'tm, S2f, S2, ts', 320 'tm6': 'tm, S2f, tf, S2, ts', 321 'tm7': 'tm, S2f, S2, ts, Rex', 322 'tm8': 'tm, S2f, tf, S2, ts, Rex', 323 'tm9': 'tm, Rex' 324 } 325 326 # No match. 327 if name not in map.keys(): 328 raise RelaxError("The model-free model '%s' is unknown." % name) 329 330 # Return the BMRB model name. 331 return map[name]
332