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-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  # 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 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 
 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
64 - def data_check(self, old_pipe_name='results', new_pipe_name='new', version=None):
65 """Check that all data has been successfully restored from the BMRB files.""" 66 67 # Print out. 68 print("\n\nComparing data pipe contents:") 69 70 # Blacklists (data that is not restored, and relaxation data which has been reordered and will be checked in data_ri_comp()). 71 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'] 72 if version == '3.0': 73 blacklist_spin = blacklist_spin + ['r', 'local_tm', 'local_tm_err'] 74 blacklist_diff = [] 75 blacklist_global = ['diff_tensor', 'exp_info', 'hybrid_pipes', 'mol', 'interatomic', 'sim_number', 'sim_state'] + ['ri_ids', 'spectrometer_frq', 'ri_type'] + ['result_files'] 76 77 # The data pipes. 78 old_pipe = ds[old_pipe_name] 79 new_pipe = ds[new_pipe_name] 80 81 # The molecule data structure. 82 self.assertEqual(len(old_pipe.mol), len(new_pipe.mol)) 83 for i in range(len(old_pipe.mol)): 84 # Check the attributes. 85 self.assertEqual(old_pipe.mol[i].name, old_pipe.mol[i].name) 86 self.assertEqual(old_pipe.mol[i].type, old_pipe.mol[i].type) 87 88 # The residue data structure. 89 self.assertEqual(len(old_pipe.mol[i].res), len(new_pipe.mol[i].res)) 90 for j in range(len(old_pipe.mol[i].res)): 91 # Check the attributes. 92 self.assertEqual(old_pipe.mol[i].res[j].name, old_pipe.mol[i].res[j].name) 93 self.assertEqual(old_pipe.mol[i].res[j].num, old_pipe.mol[i].res[j].num) 94 95 # The spin data structure. 96 self.assertEqual(len(old_pipe.mol[i].res[j].spin), len(new_pipe.mol[i].res[j].spin)) 97 for k in range(len(old_pipe.mol[i].res[j].spin)): 98 # Skip deselected spins. 99 if not old_pipe.mol[i].res[j].spin[k].select: 100 continue 101 102 # Swap the spin indices for the N, H, NE1 and HE1 spins of residue 9. 103 k_new = k 104 if i == 0 and j == 0: 105 if k == 1: 106 k_new = 2 107 elif k == 2: 108 k_new = 1 109 110 # Check the containers. 111 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) 112 if hasattr(old_pipe.mol[i].res[j].spin[k], 'ri_labels'): 113 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]) 114 115 # The diffusion tensor. 116 if version != '3.0': 117 self.assert_(hasattr(new_pipe, 'diff_tensor')) 118 self.data_cont_comp(label='Diff tensor', cont_old=old_pipe.diff_tensor, cont_new=new_pipe.diff_tensor, prec=4, blacklist=blacklist_diff) 119 120 # The global data structures. 121 self.data_cont_comp(label='Global', cont_old=old_pipe, cont_new=new_pipe, blacklist=blacklist_global) 122 if hasattr(old_pipe, 'ri_ids'): 123 self.data_ri_comp_pipe(cont_old=old_pipe, cont_new=new_pipe)
124 125
126 - def data_cont_comp(self, label=None, cont_old=None, cont_new=None, prec=7, blacklist=[]):
127 """Compare the contents of the two data containers.""" 128 129 # Print a new line. 130 print('') 131 132 # Check the attributes. 133 names = dir(cont_old) 134 for name in names: 135 # Skip special names. 136 if search('^__', name): 137 continue 138 139 # Skip the data store methods. 140 if name in list(cont_old.__class__.__dict__.keys()): 141 continue 142 143 # Simulation data. 144 if search('_sim$', name): 145 continue 146 147 # Blacklisting. 148 if name in blacklist: 149 continue 150 151 # Print out. 152 print("%s object: '%s'" % (label, name)) 153 154 # Get the objects. 155 obj_old = getattr(cont_old, name) 156 obj_new = getattr(cont_new, name) 157 158 # Functions and methods. 159 if inspect.isfunction(obj_old) or inspect.ismethod(obj_old): 160 continue 161 162 # Does it exist? 163 self.assert_(hasattr(cont_new, name)) 164 165 # Compare lists. 166 if (isinstance(obj_old, list) or isinstance(obj_old, ndarray)): 167 self.assertEqual(len(obj_old), len(obj_new)) 168 169 # List of lists (or rank-2 array). 170 if (isinstance(obj_old[0], list) or isinstance(obj_old[0], ndarray)): 171 for i in range(len(obj_old)): 172 for j in range(len(obj_old[i])): 173 if isinstance(obj_old[i][j], float): 174 self.assertAlmostEqual(obj_old[i][j], obj_new[i][j], prec) 175 else: 176 self.assertEqual(obj_old[i][j], obj_new[i][j]) 177 178 # Standard list. 179 else: 180 for i in range(len(obj_old)): 181 if isinstance(obj_old[i], float): 182 self.assertAlmostEqual(obj_old[i], obj_new[i], prec) 183 else: 184 self.assertEqual(obj_old[i], obj_new[i]) 185 186 # Compare floats. 187 elif isinstance(obj_old, float): 188 self.assertAlmostEqual(obj_old, obj_new, prec) 189 190 # Compare ints and strings. 191 else: 192 self.assertEqual(obj_old, obj_new)
193 194
195 - def data_ri_comp_pipe(self, cont_old=None, cont_new=None):
196 """Compare the contents of the two pipe data containers.""" 197 198 # Check that the new container has relaxation data. 199 for name in ['spectrometer_frq', 'ri_ids', 'ri_type']: 200 self.assert_(hasattr(cont_new, name)) 201 202 # Check the IDs. 203 old_ids = deepcopy(cont_old.ri_ids) 204 new_ids = deepcopy(cont_new.ri_ids) 205 old_ids.sort() 206 new_ids.sort() 207 self.assertEqual(old_ids, new_ids) 208 209 # Check the frequencies and types. 210 for ri_id in old_ids: 211 self.assertEqual(cont_old.spectrometer_frq[ri_id], cont_new.spectrometer_frq[ri_id]) 212 self.assertEqual(cont_old.ri_type[ri_id], cont_new.ri_type[ri_id])
213 214
215 - def data_ri_comp_spin(self, cont_old=None, cont_new=None):
216 """Compare the contents of the two spin data containers.""" 217 218 # Check that the new container has relaxation data. 219 for name in ['ri_data', 'ri_data_err']: 220 self.assert_(hasattr(cont_new, name)) 221 222 # Check the IDs. 223 old_ids = cont_old.ri_data.keys() 224 new_ids = cont_new.ri_data.keys() 225 old_ids.sort() 226 new_ids.sort() 227 self.assertEqual(old_ids, new_ids) 228 229 # Check the data and errors. 230 for ri_id in old_ids: 231 self.assertEqual(cont_old.ri_data[ri_id], cont_new.ri_data[ri_id]) 232 self.assertEqual(cont_old.ri_data_err[ri_id], cont_new.ri_data_err[ri_id])
233 234
236 """Catch U{bug #20471<https://web.archive.org/web/https://gna.org/bugs/?20471>}, with structural data present prior to a bmrb.read call.""" 237 238 # Create the data pipe. 239 self.interpreter.pipe.create('test', 'mf') 240 241 # Load the PDB file. 242 path = status.install_path + sep+'test_suite'+sep+'shared_data'+sep+'structures' 243 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') 244 245 # Set up the 15N and 1H spins. 246 self.interpreter.structure.load_spins(spin_id='@N', ave_pos=True) 247 self.interpreter.structure.load_spins(spin_id='@H', ave_pos=True) 248 self.interpreter.spin.isotope(isotope='15N', spin_id='@N', force=False) 249 self.interpreter.spin.isotope(isotope='1H', spin_id='@H', force=False) 250 251 # Load the relaxation data. 252 path = status.install_path + sep+'test_suite'+sep+'shared_data'+sep+'bmrb' 253 self.assertRaises(RelaxError, self.interpreter.bmrb.read, file='17226.txt', dir=path, version=None, sample_conditions=None)
254 255
257 """Write and then read a BRMB STAR formatted file containing model-free results.""" 258 259 # Set the NMR-STAR version. 260 ds.version = '3.0' 261 262 # Execute the script. 263 self.script_exec(status.install_path + sep+'test_suite'+sep+'system_tests'+sep+'scripts'+sep+'bmrb_rw.py') 264 265 # Test the data. 266 self.data_check(version='3.0')
267 268
270 """Write and then read a BRMB STAR formatted file containing model-free results.""" 271 272 # Set the NMR-STAR version. 273 ds.version = '3.1' 274 275 # Execute the script. 276 self.script_exec(status.install_path + sep+'test_suite'+sep+'system_tests'+sep+'scripts'+sep+'bmrb_rw.py') 277 278 # Test the data. 279 self.data_check(version='3.1')
280