1   
  2   
  3   
  4   
  5   
  6   
  7   
  8   
  9   
 10   
 11   
 12   
 13   
 14   
 15   
 16   
 17   
 18   
 19   
 20   
 21   
 22   
 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   
 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           
 49          super(Bmrb, self).__init__(methodName) 
 50   
 51           
 52          if not dep_check.bmrblib_module: 
 53               
 54              status.skipped_tests.append([methodName, 'Bmrblib', self._skip_type]) 
  55   
 56   
 58          """Common set up for these system tests.""" 
 59   
 60           
 61          ds.tmpfile = mktemp() 
 62   
 63           
 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           
 71          print("\n\nComparing data pipe contents:") 
 72   
 73           
 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           
 81          old_pipe = ds[old_pipe_name] 
 82          new_pipe = ds[new_pipe_name] 
 83   
 84           
 85          self.assertEqual(len(old_pipe.mol), len(new_pipe.mol)) 
 86          for i in range(len(old_pipe.mol)): 
 87               
 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               
 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                   
 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                   
 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                       
102                      if not old_pipe.mol[i].res[j].spin[k].select: 
103                          continue 
104   
105                       
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                       
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           
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           
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           
133          print('') 
134   
135           
136          names = dir(cont_old) 
137          for name in names: 
138               
139              if search('^__', name): 
140                  continue 
141   
142               
143              if name in cont_old.__class__.__dict__: 
144                  continue 
145   
146               
147              if search('_sim$', name): 
148                  continue 
149   
150               
151              if name in blacklist: 
152                  continue 
153   
154               
155              print("%s object: '%s'" % (label, name)) 
156   
157               
158              obj_old = getattr(cont_old, name) 
159              obj_new = getattr(cont_new, name) 
160   
161               
162              if inspect.isfunction(obj_old) or inspect.ismethod(obj_old): 
163                  continue 
164   
165               
166              self.assert_(hasattr(cont_new, name)) 
167   
168               
169              if (isinstance(obj_old, list) or isinstance(obj_old, ndarray)): 
170                  self.assertEqual(len(obj_old), len(obj_new)) 
171   
172                   
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                   
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               
190              elif isinstance(obj_old, float): 
191                  self.assertAlmostEqual(obj_old, obj_new, prec) 
192   
193               
194              else: 
195                  self.assertEqual(obj_old, obj_new) 
 196   
197   
199          """Compare the contents of the two pipe data containers.""" 
200   
201           
202          for name in ['spectrometer_frq', 'ri_ids', 'ri_type']: 
203              self.assert_(hasattr(cont_new, name)) 
204   
205           
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           
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   
219          """Compare the contents of the two spin data containers.""" 
220   
221           
222          for name in ['ri_data', 'ri_data_err']: 
223              self.assert_(hasattr(cont_new, name)) 
224   
225           
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           
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           
242          self.interpreter.pipe.create('test', 'mf') 
243   
244           
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           
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           
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   
274   
275   
277          """Catch U{bug #22704<https://web.archive.org/web/https://gna.org/bugs/?22704>}, the corrupted relax state files.""" 
278   
279           
280          self.interpreter.pipe.create('test', 'mf') 
281   
282           
283          self.interpreter.bmrb.software('relax') 
284   
285           
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           
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           
308          ds.version = '3.0' 
309   
310           
311          self.script_exec(status.install_path + sep+'test_suite'+sep+'system_tests'+sep+'scripts'+sep+'bmrb_rw.py') 
312   
313           
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           
321          ds.version = '3.1' 
322   
323           
324          self.script_exec(status.install_path + sep+'test_suite'+sep+'system_tests'+sep+'scripts'+sep+'bmrb_rw.py') 
325   
326           
327          self.data_check(version='3.1') 
  328