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

Source Code for Module test_suite.system_tests.bmrb

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2008-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  # Python module imports. 
 23  from copy import deepcopy 
 24  import inspect 
 25  from numpy import ndarray 
 26  from os import sep 
 27  from re import search 
 28  from tempfile import mkdtemp, mktemp 
 29   
 30  # relax module imports. 
 31  from data_store import Relax_data_store; ds = Relax_data_store() 
 32  import dep_check 
 33  from lib.errors import RelaxError, RelaxNoFrqError 
 34  from status import Status; status = Status() 
 35  from test_suite.system_tests.base_classes import SystemTestCase 
 36   
 37   
38 -class Bmrb(SystemTestCase):
39 """TestCase class for functional tests of the reading and writing of BMRB STAR formatted files.""" 40
41 - def __init__(self, methodName='runTest'):
42 """Skip the tests if bmrblib is not installed. 43 44 @keyword methodName: The name of the test. 45 @type methodName: str 46 """ 47 48 # Execute the base class method. 49 super(Bmrb, self).__init__(methodName) 50 51 # Missing module. 52 if not dep_check.bmrblib_module: 53 # Store in the status object. 54 status.skipped_tests.append([methodName, 'Bmrblib', self._skip_type])
55 56
57 - def setUp(self):
58 """Common set up for these system tests.""" 59 60 # Create a temporary file name. 61 ds.tmpfile = mktemp() 62 63 # Create a temporary directory for dumping files. 64 self.tmpdir = mkdtemp()
65 66
67 - def data_check(self, old_pipe_name='results', new_pipe_name='new', version=None):
68 """Check that all data has been successfully restored from the BMRB files.""" 69 70 # Print out. 71 print("\n\nComparing data pipe contents:") 72 73 # Blacklists (data that is not restored, and relaxation data which has been reordered and will be checked in data_ri_comp()). 74 blacklist_spin = ['attached_proton', 'fixed', 'nucleus', 'proton_type', 'relax_sim_data', 'select', 'xh_vect'] + ['r_err', 'csa_err'] + ['chi2_sim', 'f_count', 'g_count', 'h_count', 'iter', 'warning'] + ['frq', 'frq_labels', 'noe_r1_table', 'remap_table', 'ri_labels', 'relax_data', 'relax_error'] + ['_spin_index'] 75 if version == '3.0': 76 blacklist_spin = blacklist_spin + ['r', 'local_tm', 'local_tm_err'] 77 blacklist_diff = [] 78 blacklist_global = ['diff_tensor', 'exp_info', 'hybrid_pipes', 'mol', 'interatomic', 'sim_number', 'sim_state'] + ['ri_ids', 'spectrometer_frq', 'ri_type'] + ['result_files'] 79 80 # The data pipes. 81 old_pipe = ds[old_pipe_name] 82 new_pipe = ds[new_pipe_name] 83 84 # The molecule data structure. 85 self.assertEqual(len(old_pipe.mol), len(new_pipe.mol)) 86 for i in range(len(old_pipe.mol)): 87 # Check the attributes. 88 self.assertEqual(old_pipe.mol[i].name, old_pipe.mol[i].name) 89 self.assertEqual(old_pipe.mol[i].type, old_pipe.mol[i].type) 90 91 # The residue data structure. 92 self.assertEqual(len(old_pipe.mol[i].res), len(new_pipe.mol[i].res)) 93 for j in range(len(old_pipe.mol[i].res)): 94 # Check the attributes. 95 self.assertEqual(old_pipe.mol[i].res[j].name, old_pipe.mol[i].res[j].name) 96 self.assertEqual(old_pipe.mol[i].res[j].num, old_pipe.mol[i].res[j].num) 97 98 # The spin data structure. 99 self.assertEqual(len(old_pipe.mol[i].res[j].spin), len(new_pipe.mol[i].res[j].spin)) 100 for k in range(len(old_pipe.mol[i].res[j].spin)): 101 # Skip deselected spins. 102 if not old_pipe.mol[i].res[j].spin[k].select: 103 continue 104 105 # Swap the spin indices for the N, H, NE1 and HE1 spins of residue 9. 106 k_new = k 107 if i == 0 and j == 0: 108 if k == 1: 109 k_new = 2 110 elif k == 2: 111 k_new = 1 112 113 # Check the containers. 114 self.data_cont_comp(label='Spin', cont_old=old_pipe.mol[i].res[j].spin[k], cont_new=new_pipe.mol[i].res[j].spin[k_new], blacklist=blacklist_spin) 115 if hasattr(old_pipe.mol[i].res[j].spin[k], 'ri_labels'): 116 self.data_ri_comp_spin(cont_old=old_pipe.mol[i].res[j].spin[k], cont_new=new_pipe.mol[i].res[j].spin[k_new]) 117 118 # The diffusion tensor. 119 if version != '3.0': 120 self.assert_(hasattr(new_pipe, 'diff_tensor')) 121 self.data_cont_comp(label='Diff tensor', cont_old=old_pipe.diff_tensor, cont_new=new_pipe.diff_tensor, prec=4, blacklist=blacklist_diff) 122 123 # The global data structures. 124 self.data_cont_comp(label='Global', cont_old=old_pipe, cont_new=new_pipe, blacklist=blacklist_global) 125 if hasattr(old_pipe, 'ri_ids'): 126 self.data_ri_comp_pipe(cont_old=old_pipe, cont_new=new_pipe)
127 128
129 - def data_cont_comp(self, label=None, cont_old=None, cont_new=None, prec=7, blacklist=[]):
130 """Compare the contents of the two data containers.""" 131 132 # Print a new line. 133 print('') 134 135 # Check the attributes. 136 names = dir(cont_old) 137 for name in names: 138 # Skip special names. 139 if search('^__', name): 140 continue 141 142 # Skip the data store methods. 143 if name in cont_old.__class__.__dict__: 144 continue 145 146 # Simulation data. 147 if search('_sim$', name): 148 continue 149 150 # Blacklisting. 151 if name in blacklist: 152 continue 153 154 # Print out. 155 print("%s object: '%s'" % (label, name)) 156 157 # Get the objects. 158 obj_old = getattr(cont_old, name) 159 obj_new = getattr(cont_new, name) 160 161 # Functions and methods. 162 if inspect.isfunction(obj_old) or inspect.ismethod(obj_old): 163 continue 164 165 # Does it exist? 166 self.assert_(hasattr(cont_new, name)) 167 168 # Compare lists. 169 if (isinstance(obj_old, list) or isinstance(obj_old, ndarray)): 170 self.assertEqual(len(obj_old), len(obj_new)) 171 172 # List of lists (or rank-2 array). 173 if (isinstance(obj_old[0], list) or isinstance(obj_old[0], ndarray)): 174 for i in range(len(obj_old)): 175 for j in range(len(obj_old[i])): 176 if isinstance(obj_old[i][j], float): 177 self.assertAlmostEqual(obj_old[i][j], obj_new[i][j], prec) 178 else: 179 self.assertEqual(obj_old[i][j], obj_new[i][j]) 180 181 # Standard list. 182 else: 183 for i in range(len(obj_old)): 184 if isinstance(obj_old[i], float): 185 self.assertAlmostEqual(obj_old[i], obj_new[i], prec) 186 else: 187 self.assertEqual(obj_old[i], obj_new[i]) 188 189 # Compare floats. 190 elif isinstance(obj_old, float): 191 self.assertAlmostEqual(obj_old, obj_new, prec) 192 193 # Compare ints and strings. 194 else: 195 self.assertEqual(obj_old, obj_new)
196 197
198 - def data_ri_comp_pipe(self, cont_old=None, cont_new=None):
199 """Compare the contents of the two pipe data containers.""" 200 201 # Check that the new container has relaxation data. 202 for name in ['spectrometer_frq', 'ri_ids', 'ri_type']: 203 self.assert_(hasattr(cont_new, name)) 204 205 # Check the IDs. 206 old_ids = deepcopy(cont_old.ri_ids) 207 new_ids = deepcopy(cont_new.ri_ids) 208 old_ids.sort() 209 new_ids.sort() 210 self.assertEqual(old_ids, new_ids) 211 212 # Check the frequencies and types. 213 for ri_id in old_ids: 214 self.assertEqual(cont_old.spectrometer_frq[ri_id], cont_new.spectrometer_frq[ri_id]) 215 self.assertEqual(cont_old.ri_type[ri_id], cont_new.ri_type[ri_id])
216 217
218 - def data_ri_comp_spin(self, cont_old=None, cont_new=None):
219 """Compare the contents of the two spin data containers.""" 220 221 # Check that the new container has relaxation data. 222 for name in ['ri_data', 'ri_data_err']: 223 self.assert_(hasattr(cont_new, name)) 224 225 # Check the IDs. 226 old_ids = sorted(cont_old.ri_data.keys()) 227 new_ids = sorted(cont_new.ri_data.keys()) 228 old_ids.sort() 229 new_ids.sort() 230 self.assertEqual(old_ids, new_ids) 231 232 # Check the data and errors. 233 for ri_id in old_ids: 234 self.assertEqual(cont_old.ri_data[ri_id], cont_new.ri_data[ri_id]) 235 self.assertEqual(cont_old.ri_data_err[ri_id], cont_new.ri_data_err[ri_id])
236 237
239 """Catch U{bug #20471<https://web.archive.org/web/https://gna.org/bugs/?20471>}, with structural data present prior to a bmrb.read call.""" 240 241 # Create the data pipe. 242 self.interpreter.pipe.create('test', 'mf') 243 244 # Load the PDB file. 245 path = status.install_path + sep+'test_suite'+sep+'shared_data'+sep+'structures' 246 self.interpreter.structure.read_pdb(file='1RTE_H_trunc.pdb', dir=path, read_mol=None, set_mol_name=None, read_model=None, set_model_num=None, alt_loc='A') 247 248 # Set up the 15N and 1H spins. 249 self.interpreter.structure.load_spins(spin_id='@N', ave_pos=True) 250 self.interpreter.structure.load_spins(spin_id='@H', ave_pos=True) 251 self.interpreter.spin.isotope(isotope='15N', spin_id='@N', force=False) 252 self.interpreter.spin.isotope(isotope='1H', spin_id='@H', force=False) 253 254 # Load the relaxation data. 255 path = status.install_path + sep+'test_suite'+sep+'shared_data'+sep+'bmrb' 256 self.assertRaises(RelaxError, self.interpreter.bmrb.read, file='17226.txt', dir=path, version=None, sample_conditions=None)
257 258
260 """Catch U{bug #22703<https://web.archive.org/web/https://gna.org/bugs/?22703>}, the failure of the bmrb.display user function with an AttributeError when no data is present.""" 261 262 # Create the data pipe. 263 self.interpreter.pipe.create('test', 'mf') 264 265 # Display. 266 self.assertRaises(RelaxNoFrqError, self.interpreter.bmrb.display, version='3.1') 267 268 # Reset and create a new data pipe. 269 self.interpreter.reset() 270 self.interpreter.pipe.create('test2', 'mf') 271 272 # Display again to show a GUI breakage. 273 self.assertRaises(RelaxNoFrqError, self.interpreter.bmrb.display, version='3.1')
274 275
277 """Catch U{bug #22704<https://web.archive.org/web/https://gna.org/bugs/?22704>}, the corrupted relax state files.""" 278 279 # Create the data pipe. 280 self.interpreter.pipe.create('test', 'mf') 281 282 # Set the relax references. 283 self.interpreter.bmrb.software('relax') 284 285 # Save the relax state, reset, and try loading it again. 286 self.interpreter.state.save('corrupted_state', dir=self.tmpdir) 287 self.interpreter.reset() 288 self.interpreter.state.load('corrupted_state', dir=self.tmpdir) 289 290 # Checks. 291 self.assert_(hasattr(cdp, 'exp_info')) 292 self.assert_(hasattr(cdp.exp_info, 'software')) 293 self.assertEqual(len(cdp.exp_info.software), 1) 294 self.assertEqual(cdp.exp_info.software[0].name, 'software') 295 self.assertEqual(cdp.exp_info.software[0].desc, 'Software program used in the analysis') 296 self.assertEqual(cdp.exp_info.software[0].software_name, 'relax') 297 self.assertEqual(cdp.exp_info.software[0].version, None) 298 self.assertEqual(cdp.exp_info.software[0].url, None) 299 self.assertEqual(cdp.exp_info.software[0].vendor_name, None) 300 self.assertEqual(cdp.exp_info.software[0].cite_ids, None) 301 self.assertEqual(cdp.exp_info.software[0].tasks, None)
302 303
305 """Write and then read a BRMB STAR formatted file containing model-free results.""" 306 307 # Set the NMR-STAR version. 308 ds.version = '3.0' 309 310 # Execute the script. 311 self.script_exec(status.install_path + sep+'test_suite'+sep+'system_tests'+sep+'scripts'+sep+'bmrb_rw.py') 312 313 # Test the data. 314 self.data_check(version='3.0')
315 316
318 """Write and then read a BRMB STAR formatted file containing model-free results.""" 319 320 # Set the NMR-STAR version. 321 ds.version = '3.1' 322 323 # Execute the script. 324 self.script_exec(status.install_path + sep+'test_suite'+sep+'system_tests'+sep+'scripts'+sep+'bmrb_rw.py') 325 326 # Test the data. 327 self.data_check(version='3.1')
328