Package specific_analyses :: Package relax_disp :: Module nessy
[hide private]
[frames] | no frames]

Source Code for Module specific_analyses.relax_disp.nessy

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2013-2014 Edward d'Auvergne                                   # 
  4  # Copyright (C) 2014 Troels E. Linnet                                         # 
  5  #                                                                             # 
  6  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  7  #                                                                             # 
  8  # This program is free software: you can redistribute it and/or modify        # 
  9  # it under the terms of the GNU General Public License as published by        # 
 10  # the Free Software Foundation, either version 3 of the License, or           # 
 11  # (at your option) any later version.                                         # 
 12  #                                                                             # 
 13  # This program is distributed in the hope that it will be useful,             # 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 16  # GNU General Public License for more details.                                # 
 17  #                                                                             # 
 18  # You should have received a copy of the GNU General Public License           # 
 19  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 20  #                                                                             # 
 21  ############################################################################### 
 22   
 23  # Module docstring. 
 24  """Functions for interfacing with Michael Bieri's NESSY program.""" 
 25   
 26  # Python module imports. 
 27  from os import getcwd 
 28   
 29  # relax module imports. 
 30  from lib.errors import RelaxError, RelaxNoSequenceError 
 31  from lib.float import isNaN 
 32  from lib.io import mkdir_nofail, open_write_file 
 33  from lib.periodic_table import periodic_table 
 34  from pipe_control.pipes import check_pipe 
 35  from pipe_control.spectrometer import get_frequencies 
 36  from pipe_control.mol_res_spin import exists_mol_res_spin_data, spin_loop 
 37  from specific_analyses.relax_disp.data import find_intensity_keys, loop_exp_frq_offset, loop_exp_frq_point_time, loop_point 
 38   
 39   
40 -def nessy_input(file='save.NESSY', dir=None, spin_id=None, force=False):
41 """Create the NESSY input files. 42 43 @keyword dir: The optional directory to place the files into. If None, then the files will be placed into the current directory. 44 @type dir: str or None 45 @keyword binary: The name of the CPMGFit binary file. This can include the path to the binary. 46 @type binary: str 47 @keyword spin_id: The spin ID string to restrict the file creation to. 48 @type spin_id: str 49 @keyword force: A flag which if True will cause all pre-existing files to be overwritten. 50 @type force: bool 51 """ 52 53 # Test if the current pipe exists. 54 check_pipe() 55 56 # Test if sequence data is loaded. 57 if not exists_mol_res_spin_data(): 58 raise RelaxNoSequenceError 59 60 # Test if the experiment type has been set. 61 if not hasattr(cdp, 'exp_type'): 62 raise RelaxError("The relaxation dispersion experiment type has not been specified.") 63 64 # Directory creation. 65 if dir != None: 66 mkdir_nofail(dir, verbosity=0) 67 68 # The save file. 69 save_file = open_write_file(file, dir, force) 70 71 # Create the NESSY data object. 72 data = Nessy_data(spin_id=spin_id) 73 74 # Create the NESSY file. 75 write_program_setup(file=save_file, dir=dir, data=data) 76 77 # Loop over the experiments. 78 for ei in range(data.num_exp): 79 write_sequence(file=save_file, data=data, ei=ei) 80 write_cpmg_datasets(file=save_file, data=data, ei=ei) 81 write_spinlock_datasets(file=save_file, data=data, ei=ei) 82 write_experiment_setup(file=save_file, data=data, ei=ei)
83 84
85 -def write_cpmg_datasets(file=None, data=None, ei=0):
86 """Create the NESSY CPMG datasets. 87 88 @keyword file: The file object to write to. 89 @type file: file object 90 @keyword data: The NESSY data object. 91 @type data: Nessy_data instance 92 @keyword ei: The index of the experiment to output. 93 @type ei: int 94 """ 95 96 # Loop over the 30 elements. 97 for i in range(30): 98 # Empty data. 99 file.write("Datasets:<>%s<>%i<>%s\n" % (ei, i+1, data.cpmg_data[ei][i]))
100 101
102 -def write_experiment_setup(file=None, data=None, ei=0):
103 """Create the NESSY experimental setup entries. 104 105 @keyword file: The file object to write to. 106 @type file: file object 107 @keyword data: The NESSY data object. 108 @type data: Nessy_data instance 109 @keyword ei: The index of the experiment to output. 110 @type ei: int 111 """ 112 113 # The CPMG relaxation delay. 114 file.write("CPMG relaxation delay:<>%s<>%s\n" % (ei, data.cpmg_delay[ei])) 115 116 # The HD exchange entry 117 file.write("HD Exchange:<>%s<>%s\n" % (ei, data.hd_exchange[ei])) 118 119 # The experiment type. 120 file.write("Experiment:<>%s<>%s\n" % (ei, data.experiment[ei])) 121 122 # The nu_CPMG frequencies. 123 file.write("CPMG frequencies:<>%s<>%s\n" % (ei, data.cpmg_frqs[ei])) 124 125 # The spin-lock field strengths. 126 file.write("Spin Lock / Offset:<>%s<>%s\n" % (ei, data.spin_lock[ei]))
127 128
129 -def write_program_setup(file=None, dir=None, data=None):
130 """Create the NESSY setup entries at the start of the file. 131 132 @keyword file: The file object to write to. 133 @type file: file object 134 @keyword dir: The optional directory to place the files into. If None, then the files will be placed into the current directory. 135 @type dir: str or None 136 @keyword data: The NESSY data object. 137 @type data: Nessy_data instance 138 """ 139 140 # File definition. 141 file.write("NESSY save file<><>\n") 142 143 # Program settings. 144 file.write("Settings:<>['AICc', '500', '30', '2.0', '50.0', '0', '40']\n") 145 file.write("Models:<>[1, 1, 1, 0, 0, 0, 0]\n") 146 file.write("Fitting accuracy:<>1.49012e-20\n") 147 148 # The project directory. 149 if dir == None: 150 dir = getcwd() 151 file.write("Project folder:<>%s\n" % dir) 152 153 # Experimental settings. 154 file.write("CPMG delay:<>%s\n" % data.cpmg_delay) 155 file.write("HD noise:<>%s\n" % data.hd_noise) 156 file.write("Shift difference:<>%s\n" % data.shift_diff) 157 file.write("Spec freq:<>%s\n" % data.spec_frq) 158 file.write("B0:<>%s\n" % data.B0) 159 file.write("PDB file:<>%s\n" % data.pdb_file) 160 file.write("Number of experiments:<>%s\n" % data.num_exp) 161 162 # Empty results files. 163 file.write("Results:<>Plot<>[]\n") 164 file.write("Results:<>Model1<>[]\n") 165 file.write("Results:<>Model2<>[]\n") 166 file.write("Results:<>Model3<>[]\n") 167 file.write("Results:<>Model4<>[]\n") 168 file.write("Results:<>Model5<>[]\n") 169 file.write("Results:<>Model6<>[]\n") 170 file.write("Results:<>Final<>[]\n") 171 file.write("Results:<>ColorCode<>[]\n") 172 file.write("Results:<>Textfiles:<>[]\n") 173 file.write("Results:<>2D Plots:<>[]\n") 174 file.write("Results:<>3D Plots:<>[]\n") 175 file.write("Results:<>Intensities:<>[]\n") 176 file.write("Final Results:<>[]\n")
177 178
179 -def write_sequence(file=None, data=None, ei=0):
180 """Create the NESSY sequence entry. 181 182 @keyword file: The file object to write to. 183 @type file: file object 184 @keyword data: The NESSY data object. 185 @type data: Nessy_data instance 186 @keyword ei: The index of the experiment to output. 187 @type ei: int 188 """ 189 190 # Empty sequence. 191 file.write("Sequence:<>%s<>%s\n" % (ei, data.sequence))
192 193
194 -def write_spinlock_datasets(file=None, data=None, ei=0):
195 """Create the NESSY R1rho datasets. 196 197 @keyword file: The file object to write to. 198 @type file: file object 199 @keyword data: The NESSY data object. 200 @type data: Nessy_data instance 201 @keyword ei: The index of the experiment to output. 202 @type ei: int 203 """ 204 205 # Loop over the 30 elements. 206 for i in range(30): 207 # Loop over the second set of 30 elements. 208 for j in range(30): 209 # Empty data. 210 file.write("Datasets Spinlock:<>%s<>%s<>%s<>%s\n" % (ei, i+1, j+1, data.r1rho_data[ei][i][j]))
211 212 213
214 -class Nessy_data:
215 - def __init__(self, spin_id=None):
216 """Create the NESSY data object container. 217 218 @keyword spin_id: The spin ID string to restrict data to. 219 @type spin_id: str 220 """ 221 222 # Checks. 223 if len(cdp.relax_time_list) != 1: 224 raise RelaxError("NESSY only supports the fixed time relaxation dispersion experiments.") 225 226 # The number of NESSY experiments (i.e. number of spectrometer frequencies). 227 self.num_exp = 1 228 if hasattr(cdp, 'spectrometer_frq_count'): 229 self.num_exp = cdp.spectrometer_frq_count 230 231 # Default values. 232 self.cpmg_delay = ['0.04'] * self.num_exp 233 self.hd_noise = ['1000'] * self.num_exp 234 self.shift_diff = [None] * 700 235 self.spec_frq = ['60.77'] * self.num_exp 236 self.B0 = ['14.1'] * self.num_exp 237 self.pdb_file = '' 238 self.sequence = [''] * 700 239 self.cpmg_data = [] 240 self.r1rho_data = [] 241 self.hd_exchange = [] 242 self.experiment = [] 243 self.cpmg_frqs = [] 244 self.spin_lock = [] 245 for ei in range(self.num_exp): 246 self.cpmg_data.append([]) 247 self.r1rho_data.append([]) 248 self.hd_exchange.append([''] * 30) 249 self.experiment.append('cpmg') 250 self.cpmg_frqs.append([''] * 30) 251 self.spin_lock.append([''] * 31) 252 for i in range(30): 253 self.cpmg_data[ei].append([''] * 700) 254 self.r1rho_data[ei].append([]) 255 for j in range(30): 256 self.r1rho_data[ei][-1].append([''] * 700) 257 258 # Assemble the data. 259 self._assemble_experiment() 260 self._assemble_cpmg_data(spin_id=spin_id)
261 262
263 - def _assemble_cpmg_data(self, spin_id=None):
264 """Assemble the CPMG data. 265 266 @keyword spin_id: The spin ID string to restrict data to. 267 @type spin_id: str 268 """ 269 270 # Spin loop. 271 for spin, mol_name, res_num, res_name, id in spin_loop(full_info=True, selection=spin_id, return_id=True, skip_desel=True): 272 # The residue index. 273 res_index = res_num - 1 274 275 # Sanity checks. 276 if res_index < 0: 277 raise RelaxError("A residue number of less than 1 is not supported in NESSY.") 278 elif res_index > 699: 279 raise RelaxError("A residue number of greater than 700 is not supported in NESSY.") 280 281 # Loop over all spectrometer frequencies. 282 for exp_type, frq, offset, ei, mi, oi in loop_exp_frq_offset(return_indices=True): 283 # Loop over all dispersion points. 284 di_new = 0 285 for point, di in loop_point(exp_type=exp_type, frq=frq, offset=offset, skip_ref=False, return_indices=True): 286 # The keys. 287 keys = find_intensity_keys(exp_type=exp_type, frq=frq, point=point, time=cdp.relax_time_list[0]) 288 289 # Convert the reference point for NESSY input. 290 if point == None or isNaN(point): 291 point = 0 292 293 # Loop over the keys. 294 for key in keys: 295 # Another check. 296 if self.cpmg_data[mi][di_new][res_index] != '': 297 raise RelaxError("Only one spin system per residue is supported in NESSY.") 298 299 # Store the data (if it exists). 300 if key in spin.peak_intensity: 301 self.cpmg_data[mi][di_new][res_index] = str(spin.peak_intensity[key]) 302 303 # The CPMG frequency. 304 self.cpmg_frqs[mi][di_new] = str(point) 305 306 # Increment the field index. 307 di_new += 1
308 309
310 - def _assemble_experiment(self):
311 """Assemble the experimental data.""" 312 313 # Get the spectrometer info. 314 frq_Hz = get_frequencies(units='MHz') 315 frq_T = get_frequencies(units='T') 316 317 # Loop over all data points. 318 for exp_type, frq, point, time, ei, mi, di, ti in loop_exp_frq_point_time(return_indices=True): 319 # The frequency data. 320 self.cpmg_delay[mi] = str(time) 321 322 # Loop over the experiments. 323 for i in range(cdp.spectrometer_frq_count): 324 # Spectrometer info. 325 self.spec_frq[i] = str(frq_Hz[i] / periodic_table.gyromagnetic_ratio('1H') * periodic_table.gyromagnetic_ratio('15N')) 326 self.B0[i] = str(frq_T[i])
327