Package test_suite :: Package system_tests :: Module spectrum
[hide private]
[frames] | no frames]

Source Code for Module test_suite.system_tests.spectrum

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2008-2014 Edward d'Auvergne                                   # 
  4  # Copyright (C) 2008 Sebastien Morin                                          # 
  5  # Copyright (C) 2013-2014 Troels E. Linnet                                    # 
  6  #                                                                             # 
  7  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  8  #                                                                             # 
  9  # This program is free software: you can redistribute it and/or modify        # 
 10  # it under the terms of the GNU General Public License as published by        # 
 11  # the Free Software Foundation, either version 3 of the License, or           # 
 12  # (at your option) any later version.                                         # 
 13  #                                                                             # 
 14  # This program is distributed in the hope that it will be useful,             # 
 15  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 16  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 17  # GNU General Public License for more details.                                # 
 18  #                                                                             # 
 19  # You should have received a copy of the GNU General Public License           # 
 20  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 21  #                                                                             # 
 22  ############################################################################### 
 23   
 24  # Python module imports. 
 25  from os import sep 
 26  from os.path import dirname 
 27  from tempfile import mkdtemp, NamedTemporaryFile 
 28   
 29  # relax module imports. 
 30  from data_store import Relax_data_store; ds = Relax_data_store() 
 31  from pipe_control.mol_res_spin import spin_loop 
 32  from status import Status; status = Status() 
 33  from test_suite.system_tests.base_classes import SystemTestCase 
 34   
 35   
36 -class Spectrum(SystemTestCase):
37 """TestCase class for the functional tests for the support of different spectrum intensity calculation or errors, signal to noise and plotting.""" 38 39
40 - def setUp(self):
41 """Set up for all the functional tests.""" 42 43 # Create a temporary directory for dumping files. 44 ds.tmpdir = mkdtemp() 45 self.tmpdir = ds.tmpdir
46
47 - def setup_signal_noise_ratio(self):
48 """Setup intensity data. 49 50 This uses the data from paper at U{http://dx.doi.org/10.1073/pnas.0509100103}. This is CPMG data with a fixed relaxation time period. Experiment in 0.48 M GuHCl (guanidine hydrochloride). 51 """ 52 53 # Set pipe name, bundle and type. 54 pipe_name = 'base pipe' 55 pipe_bundle = 'relax_disp' 56 pipe_type = 'relax_disp' 57 58 # Create the data pipe. 59 self.interpreter.pipe.create(pipe_name=pipe_name, bundle=pipe_bundle, pipe_type=pipe_type) 60 61 data_path = status.install_path + sep+'test_suite'+sep+'shared_data'+sep+'dispersion'+sep+'KTeilum_FMPoulsen_MAkke_2006'+sep+'bug_neg_int_acbp_cpmg_disp_048MGuHCl_40C_041223' 62 63 # Create the spins 64 self.interpreter.spectrum.read_spins(file="peaks_list_max_standard.ser", dir=data_path) 65 66 # Name the isotope for field strength scaling. 67 self.interpreter.spin.isotope(isotope='15N') 68 69 # Read the spectrum from NMRSeriesTab file. The "auto" will generate spectrum name of form: Z_A{i} 70 self.interpreter.spectrum.read_intensities(file="peaks_list_max_standard.ser", dir=data_path, spectrum_id='auto', int_method='height') 71 72 # Loop over the spectra settings. 73 ncycfile=open(data_path + sep + 'ncyc.txt', 'r') 74 75 # Make empty ncyclist 76 ncyclist = [] 77 78 i = 0 79 for line in ncycfile: 80 ncyc = line.split()[0] 81 time_T2 = float(line.split()[1]) 82 vcpmg = line.split()[2] 83 set_sfrq = float(line.split()[3]) 84 rmsd_err = float(line.split()[4]) 85 86 # Test if spectrum is a reference 87 if float(vcpmg) == 0.0: 88 vcpmg = None 89 else: 90 vcpmg = round(float(vcpmg), 3) 91 92 # Add ncyc to list 93 ncyclist.append(int(ncyc)) 94 95 # Set the current spectrum id 96 current_id = "Z_A%s"%(i) 97 98 # Set the current experiment type. 99 self.interpreter.relax_disp.exp_type(spectrum_id=current_id, exp_type='SQ CPMG') 100 101 # Set the peak intensity errors, as defined as the baseplane RMSD. 102 self.interpreter.spectrum.baseplane_rmsd(error=rmsd_err, spectrum_id=current_id) 103 104 # Set the NMR field strength of the spectrum. 105 self.interpreter.spectrometer.frequency(id=current_id, frq=set_sfrq, units='MHz') 106 107 # Relaxation dispersion CPMG constant time delay T (in s). 108 self.interpreter.relax_disp.relax_time(spectrum_id=current_id, time=time_T2) 109 110 # Set the relaxation dispersion CPMG frequencies. 111 self.interpreter.relax_disp.cpmg_setup(spectrum_id=current_id, cpmg_frq=vcpmg) 112 113 i += 1 114 115 # Perform the error analysis. 116 self.interpreter.spectrum.error_analysis_per_field() 117 118 # Store the reference intensities and RMSD. 119 ds.data = [ 120 [1, 1711.104, 3040.110, 8.514, 121.681, +5.586445e+05, 'A3N-HN', 1.0000, 1.7492, 0.9997, 1.0075, 1.0081, 0.8991, 1.0205, 0.9893, 1.0145, 1.0078, 1.0133, 1.0020, 0.9995, 1.0020, 1.0249, 1.7571, 1.0016], 121 [2, 2011.491, 4112.494, 8.025, 117.805, +6.213668e+05, 'E4N-HN', 1.0000, 1.6032, -0.8322, 0.9920, 0.9951, 0.8300, 0.9458, 0.9689, 0.9246, 0.9936, 0.9914, 0.9842, 1.0011, 0.8529, 0.9727, 1.5845, 0.9822], 122 [3, 1974.019, 2629.250, 8.086, 123.166, +4.592940e+05, 'F5N-HN', 1.0000, 1.8235, 0.8614, 1.0135, 1.0134, 0.8825, 0.9332, 0.9772, 0.9273, 0.9803, 0.9986, 1.0000, 1.0012, 0.8923, 0.9421, 1.8102, 1.0062], 123 [4, 1608.518, 3683.374, 8.681, 119.356, +5.293190e+05, 'D6N-HN', 1.0000, 1.6865, 0.8531, 1.0052, 1.0065, 0.8530, 0.9280, 0.9594, 0.8989, 0.9914, 1.0115, 0.9743, 1.0063, 0.8177, 0.9264, 1.6855, 0.9981], 124 ] 125 126 ds.rmsd = [2.47e+03, 2.34e+03, 2.41e+03, 2.42e+03, 2.45e+03, 2.42e+03, 2.42e+03, 2.44e+03, 2.39e+03, 2.4e+03, 2.42e+03, 2.46e+03, 2.41e+03, 2.45e+03, 2.45e+03, 2.39e+03, 2.45e+03]
127 128
129 - def test_signal_noise_ratio(self):
130 """Test calculation of signal to noise ratio. 131 """ 132 133 134 # Setup data. 135 self.setup_signal_noise_ratio() 136 137 # Test the signal to noise ratio calculation. 138 self.interpreter.spectrum.sn_ratio() 139 140 # Assign counter 141 i = 0 142 for cur_spin, cur_spin_id in spin_loop(return_id=True, skip_desel=True): 143 # Test assignment. 144 self.assertEqual(cur_spin_id, ":%s@N"%ds.data[i][6][1]) 145 146 # Loop over intensity columns. 147 for j in range(17): 148 # Test intensity. 149 data_int = ds.data[i][j+7] * ds.data[i][5] 150 pint = cur_spin.peak_intensity['Z_A%i'%j] 151 self.assertEqual(pint, data_int) 152 153 # Test baseplane_rmsd. 154 data_rmsd = ds.rmsd[j] 155 self.assertEqual(cur_spin.baseplane_rmsd['Z_A%i'%j], data_rmsd) 156 157 # Test the calculated peak_intensity_err. 158 # Since we have measured intensity height, and have not specified replications, this is the same as rmsd. 159 pint_err = cur_spin.peak_intensity_err['Z_A%i'%j] 160 self.assertEqual(pint_err, ds.rmsd[j]) 161 162 # Test the signal to noise ratio. 163 sn_ratio = data_int / data_rmsd 164 self.assertEqual(cur_spin.sn_ratio['Z_A%i'%j], sn_ratio) 165 self.assertEqual(cur_spin.sn_ratio['Z_A%i'%j], pint/pint_err) 166 167 # Add to counter 168 i += 1
169 170
171 - def test_grace_int(self):
172 """Test grace plotting function for plotting the intensities per residue. 173 """ 174 175 176 # Setup data. 177 self.setup_signal_noise_ratio() 178 179 # Deselect spin with negative intensity. 180 self.interpreter.deselect.spin(spin_id=':4@N', boolean='AND', change_all=False) 181 182 # Test show grace. If showing, the temporary directory created, should not be deleted. 183 show_grace = False 184 if show_grace: 185 outfile= NamedTemporaryFile(delete=False).name 186 filedir = dirname(outfile) 187 else: 188 filedir = self.tmpdir 189 outfile = 'int.agr' 190 191 self.interpreter.grace.write(x_data_type='res_num', y_data_type='peak_intensity', file=outfile, dir=filedir, force=True) 192 193 # View the plotting. 194 if show_grace: 195 self.interpreter.grace.view(file=outfile, dir=filedir, grace_exe='xmgrace')
196 197
198 - def test_grace_sn_ratio(self):
199 """Test grace plotting function for plotting the signal to noise ratio per residue. 200 """ 201 202 # Setup data. 203 self.setup_signal_noise_ratio() 204 205 # Calculate the signal to noise ratio calculation. 206 self.interpreter.spectrum.sn_ratio() 207 208 # Deselect spin with negative intensity. 209 self.interpreter.deselect.spin(spin_id=':4@N', boolean='AND', change_all=False) 210 211 # Test show grace. If showing, the temporary directory created, should not be deleted. 212 show_grace = False 213 if show_grace: 214 outfile= NamedTemporaryFile(delete=False).name 215 filedir = dirname(outfile) 216 else: 217 filedir = self.tmpdir 218 outfile = 'int_sn.agr' 219 220 self.interpreter.grace.write(x_data_type='res_num', y_data_type='sn_ratio', file=outfile, dir=filedir, force=True) 221 222 # View the plotting. 223 if show_grace: 224 self.interpreter.grace.view(file=outfile, dir=filedir, grace_exe='xmgrace')
225 226
228 """Test the deselect.sn_ratio for signal to noise ratios, where all ID should evaluate to True. 229 """ 230 231 # Setup data. 232 self.setup_signal_noise_ratio() 233 234 # Calculate the signal to noise ratio calculation. 235 self.interpreter.spectrum.sn_ratio() 236 237 # deselect spins. 238 self.interpreter.deselect.sn_ratio(ratio=400.0, operation='<', all_sn=True) 239 240 # Test 241 spin_ids_sel = [] 242 spin_ids_desel = [] 243 244 # Collect spin ids which are selected. 245 for cur_spin, cur_spin_id in spin_loop(return_id=True, skip_desel=False): 246 if cur_spin.select: 247 spin_ids_sel.append(cur_spin_id) 248 else: 249 spin_ids_desel.append(cur_spin_id) 250 251 # Make the test: 252 self.assertEqual(spin_ids_sel, [':3@N', ':4@N']) 253 self.assertEqual(spin_ids_desel, [':5@N', ':6@N'])
254 255
257 """Test the deselect.sn_ratio for signal to noise ratios, where any ID should evaluate to True. 258 """ 259 260 # Setup data. 261 self.setup_signal_noise_ratio() 262 263 # Calculate the signal to noise ratio calculation. 264 self.interpreter.spectrum.sn_ratio() 265 266 # Deselect spins. 267 self.interpreter.deselect.sn_ratio(ratio=200.0, operation='<', all_sn=False) 268 269 # Test 270 spin_ids_sel = [] 271 spin_ids_desel = [] 272 273 # Collect spin ids which are selected. 274 for cur_spin, cur_spin_id in spin_loop(return_id=True, skip_desel=False): 275 if cur_spin.select: 276 spin_ids_sel.append(cur_spin_id) 277 else: 278 spin_ids_desel.append(cur_spin_id) 279 280 # Make the test: 281 self.assertEqual(spin_ids_sel, [':3@N']) 282 self.assertEqual(spin_ids_desel, [':4@N', ':5@N', ':6@N'])
283 284
285 - def test_select_sn_ratio_all(self):
286 """Test the select.sn_ratio for signal to noise ratios, where all ID should evaluate to True. 287 """ 288 289 # Setup data. 290 self.setup_signal_noise_ratio() 291 292 # Calculate the signal to noise ratio calculation. 293 self.interpreter.spectrum.sn_ratio() 294 295 # First deselect all spins. 296 self.interpreter.deselect.all() 297 298 # Select spins. 299 self.interpreter.select.sn_ratio(ratio=400.0, operation='<', all_sn=True) 300 301 # Test 302 spin_ids_sel = [] 303 spin_ids_desel = [] 304 305 # Collect spin ids which are selected. 306 for cur_spin, cur_spin_id in spin_loop(return_id=True, skip_desel=False): 307 if cur_spin.select: 308 spin_ids_sel.append(cur_spin_id) 309 else: 310 spin_ids_desel.append(cur_spin_id) 311 312 # Make the test: 313 self.assertEqual(spin_ids_sel, [':5@N', ':6@N']) 314 self.assertEqual(spin_ids_desel, [':3@N', ':4@N'])
315 316
317 - def test_select_sn_ratio_any(self):
318 """Test the select.sn_ratio for signal to noise ratios, where any ID should evaluate to True. 319 """ 320 321 # Setup data. 322 self.setup_signal_noise_ratio() 323 324 # Calculate the signal to noise ratio calculation. 325 self.interpreter.spectrum.sn_ratio() 326 327 # First deselect all spins. 328 self.interpreter.deselect.all() 329 330 # Select spins. 331 self.interpreter.select.sn_ratio(ratio=200.0, operation='<', all_sn=False) 332 333 # Test 334 spin_ids_sel = [] 335 spin_ids_desel = [] 336 337 # Collect spin ids which are selected. 338 for cur_spin, cur_spin_id in spin_loop(return_id=True, skip_desel=False): 339 if cur_spin.select: 340 spin_ids_sel.append(cur_spin_id) 341 else: 342 spin_ids_desel.append(cur_spin_id) 343 344 # Make the test: 345 self.assertEqual(spin_ids_sel, [':4@N', ':5@N', ':6@N']) 346 self.assertEqual(spin_ids_desel, [':3@N'])
347