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 list(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 # Sample conditions do not match (remove the $ sign). 137 if 'sample_cond_list_label' in data and sample_conditions and data['sample_cond_list_label'].replace('$', '') != sample_conditions: 138 continue 139 140 # Global data. 141 if 'global_chi2' in data: 142 setattr(cdp, 'chi2', data['global_chi2']) 143 144 # The number of spins. 145 N = bmrb.num_spins(data) 146 147 # No data in the saveframe. 148 if N == 0: 149 continue 150 151 # The molecule names. 152 mol_names = bmrb.molecule_names(data, N) 153 154 # Missing atom names. 155 if 'atom_names' not in data or data['atom_names'] == None: 156 data['atom_names'] = [None] * N 157 158 # Generate the sequence if needed. 159 bmrb.generate_sequence(N, spin_names=data['atom_names'], res_nums=data['res_nums'], res_names=data['res_names'], mol_names=mol_names) 160 161 # Correlation time scaling. 162 table = {'s': 1.0, 163 'ns': 1e-9, 164 'ps': 1e-12} 165 te_scale = 1.0 166 if data['te_units']: 167 te_scale = table[data['te_units']] 168 169 # Fast correlation time scaling. 170 if data['tf_units']: 171 tf_scale = table[data['tf_units']] 172 else: 173 tf_scale = te_scale 174 175 # Slow correlation time scaling. 176 if data['ts_units']: 177 ts_scale = table[data['ts_units']] 178 else: 179 ts_scale = te_scale 180 181 # Rex scaling. 182 rex_scale = 1.0 183 if hasattr(cdp, 'spectrometer_frq') and len(cdp.spectrometer_frq): 184 rex_scale = 1.0 / (2.0*pi*cdp.spectrometer_frq[cdp.ri_ids[0]])**2 185 186 # Loop over the spins. 187 for i in range(N): 188 # Generate a spin ID. 189 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]) 190 191 # Obtain the spin. 192 spin = mol_res_spin.return_spin(spin_id) 193 194 # No spin?!? 195 if spin == None: 196 raise RelaxError("The spin '%s' does not exist." % spin_id) 197 198 # Loop over and set the model-free parameters. 199 for j in range(len(mf_params)): 200 # The parameter. 201 param = mf_params[j] 202 203 # No parameter. 204 if not mf_bmrb_key[j] in data: 205 continue 206 207 # The parameter and its value. 208 if data[mf_bmrb_key[j]] != None: 209 # The value. 210 value = data[mf_bmrb_key[j]][i] 211 212 # Parameter scaling. 213 if value != None: 214 if param == 'te': 215 value = value * te_scale 216 elif param == 'tf': 217 value = value * tf_scale 218 elif param == 'ts': 219 value = value * ts_scale 220 elif param == 'rex': 221 value = value * rex_scale 222 223 # No parameter value. 224 else: 225 value = None 226 227 # Set the parameter. 228 setattr(spin, param, value) 229 230 # The error. 231 mf_bmrb_key_err = mf_bmrb_key[j] + '_err' 232 error = None 233 if mf_bmrb_key_err in data and data[mf_bmrb_key_err] != None: 234 error = data[mf_bmrb_key_err][i] 235 236 # Error scaling. 237 if error != None: 238 if param == 'te': 239 error = error * te_scale 240 elif param == 'tf': 241 error = error * tf_scale 242 elif param == 'ts': 243 error = error * ts_scale 244 elif param == 'rex': 245 error = error * rex_scale 246 247 # Set the error. 248 mf_param_err = param + '_err' 249 if mf_bmrb_key_err in data and data[mf_bmrb_key_err] != None: 250 setattr(spin, mf_param_err, error) 251 252 # The model. 253 if data['model_fit'] != None and data['model_fit'][i] != None: 254 model = from_bmrb_model(data['model_fit'][i]) 255 setattr(spin, 'model', model) 256 257 # The equation and parameters. 258 equation, params = model_map(model) 259 setattr(spin, 'equation', equation) 260 setattr(spin, 'params', params) 261 262 # Convert te values which should be ts! 263 if hasattr(spin, 'model') and spin.model in ['m5', 'm6', 'm7', 'm8'] and hasattr(spin, 'te') and spin.te != None: 264 # Change the parameter name of te to ts. 265 spin.ts = spin.te 266 if hasattr(spin, 'te_err'): 267 spin.ts_err = spin.te_err 268 269 # Set the te and te_err values to None. 270 spin.te = None 271 spin.te_err = None 272 273 # The element. 274 if'atom_types' in data and data['atom_types'] != None: 275 setattr(spin, 'element', data['atom_types'][i]) 276 277 # Heteronucleus type. 278 if'atom_types' in data and data['atom_types'] != None and data['atom_types'][i] != None and 'isotope' in data and data['isotope'] != None: 279 # The isotope number. 280 iso_num = data['isotope'][i] 281 282 # No isotope number. 283 iso_table = {'C': 13, 'N': 15} 284 if not data['isotope'][i]: 285 iso_num = iso_table[data['atom_types'][i]] 286 287 # Set the type. 288 setattr(spin, 'isotope', str(iso_num) + data['atom_types'][i])
289 290
291 -def to_bmrb_model(name=None):
292 """Convert the model-free model name to the BMRB name. 293 294 @keyword name: The model-free model name. 295 @type name: str 296 @return: The corresponding BMRB model name. 297 @rtype: str 298 """ 299 300 # The relax to BMRB model-free model name map. 301 map = {'m0': '', 302 'm1': 'S2', 303 'm2': 'S2, te', 304 'm3': 'S2, Rex', 305 'm4': 'S2, te, Rex', 306 'm5': 'S2f, S2, ts', 307 'm6': 'S2f, tf, S2, ts', 308 'm7': 'S2f, S2, ts, Rex', 309 'm8': 'S2f, tf, S2, ts, Rex', 310 'm9': 'Rex', 311 'tm0': 'tm', 312 'tm1': 'tm, S2', 313 'tm2': 'tm, S2, te', 314 'tm3': 'tm, S2, Rex', 315 'tm4': 'tm, S2, te, Rex', 316 'tm5': 'tm, S2f, S2, ts', 317 'tm6': 'tm, S2f, tf, S2, ts', 318 'tm7': 'tm, S2f, S2, ts, Rex', 319 'tm8': 'tm, S2f, tf, S2, ts, Rex', 320 'tm9': 'tm, Rex' 321 } 322 323 # No match. 324 if name not in map: 325 raise RelaxError("The model-free model '%s' is unknown." % name) 326 327 # Return the BMRB model name. 328 return map[name]
329