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