Back to index

python-biopython  1.60
test_Crystal.py
Go to the documentation of this file.
00001 # Copyright 2002 by Katharine Lindner.  All rights reserved.
00002 # This code is part of the Biopython distribution and governed by its
00003 # license.  Please see the LICENSE file that should have been included
00004 # as part of this package.
00005 
00006 # python unittest framework
00007 import unittest
00008 import copy
00009 import sys
00010 
00011 # modules to be tested
00012 from Bio.Crystal import Hetero, Chain, Crystal, CrystalError
00013 
00014 class ChainTestCase(unittest.TestCase):
00015 
00016     def setUp(self):
00017         self.a = 'C A A C T A G G T C A C U A G G T C A G'
00018         self.b = 'C T G A C C T A G T G A C C T A G T T G'
00019         self.c = 'THR LYS LEU ASN GLY MET VAL LEU LEU CYS LYS VAL CYS GLY ASP'
00020         self.d = 'THR LYS LEU ASN GLY MET VAL LEU LEU CYS LYS VAL CYS GLY ASP '
00021         self.e = 'TYR LYS LEU ASN GLY MET VAL LEU LEU CYS LYS VAL CYS GLY ASP '
00022         self.f = 'THR LYS LEU ASN GLY MET VAL LEU LEU CYS LYS VAL CYS GLY SER '
00023         self.g = 'C A A C T A G G T C A C U A G G T C A T'
00024         self.h = 'G A A C T A G G T C A C U A G G T C A G'
00025 
00026     def testEquals(self):
00027         first = Chain(self.a)
00028         second = Chain(self.a)
00029         self.assertEqual(first, second)
00030 
00031         first = Chain(self.b)
00032         second = Chain(self.b)
00033         self.assertEqual(first, second)
00034 
00035         first = Chain(self.c)
00036         second = Chain(self.c)
00037         self.assertEqual(first, second)
00038 
00039         first = Chain(self.a)
00040         second = Chain(self.g)
00041         self.assertNotEqual(first, second)
00042 
00043         first = Chain(self.a)
00044         second = Chain(self.h)
00045         self.assertNotEqual(first, second)
00046 
00047         first = Chain(self.c)
00048         second = Chain(self.e)
00049         self.assertNotEqual(first, second)
00050 
00051         first = Chain(self.c)
00052         second = Chain(self.f)
00053         self.assertNotEqual(first, second)
00054 
00055 
00056     def testLen(self):
00057         chain = Chain(self.a)
00058         elements = self.a.strip().split()
00059         num_elements = len(elements)
00060         self.assertEqual(len(chain), num_elements)
00061 
00062         chain = Chain(self.b)
00063         elements = self.b.strip().split()
00064         num_elements = len(elements)
00065         self.assertEqual(len(chain), num_elements)
00066 
00067         chain = Chain(self.c)
00068         elements = self.c.strip().split()
00069         num_elements = len(elements)
00070         self.assertEqual(len(chain), num_elements)
00071 
00072 
00073     def testAppend(self):
00074         chain = Chain(self.a[:])
00075         chain.append('U')
00076         elements = self.a.strip().split()
00077         num_elements = len(elements)
00078         last_element = chain.data[ -1 ]
00079         self.assertEqual('u', last_element.data)
00080         self.assertEqual(len(chain), num_elements + 1)
00081 
00082         chain = Chain(self.a[:])
00083         chain.append(Hetero('A'))
00084         elements = self.a.strip().split()
00085         num_elements = len(elements)
00086         last_element = chain.data[ -1 ]
00087         self.assertEqual('a', last_element.data)
00088         self.assertEqual(len(chain), num_elements + 1)
00089 
00090         chain = Chain(self.b[:])
00091         chain.append('t')
00092         elements = self.b.strip().split()
00093         num_elements = len(elements)
00094         last_element = chain.data[ -1 ]
00095         self.assertEqual('t', last_element.data)
00096         self.assertEqual(len(chain), num_elements + 1)
00097 
00098         chain = Chain(self.b[:])
00099         chain.append(Hetero('C'))
00100         elements = self.b.strip().split()
00101         num_elements = len(elements)
00102         last_element = chain.data[ -1 ]
00103         self.assertEqual('c', last_element.data)
00104         self.assertEqual(len(chain), num_elements + 1)
00105 
00106         chain = Chain(self.c[:])
00107         chain.append('ser')
00108         elements = self.c.strip().split()
00109         num_elements = len(elements)
00110         last_element = chain.data[ -1 ]
00111         self.assertEqual('ser', last_element.data)
00112         self.assertEqual(len(chain), num_elements + 1)
00113 
00114 
00115     def testInsert(self):
00116         chain = Chain(self.a[:])
00117         i = 4
00118         chain.insert(i, 'g')
00119         elements = self.a.strip().split()
00120         num_elements = len(elements)
00121         target_element = chain.data[ i ]
00122         self.assertEqual('g', target_element.data)
00123         self.assertEqual(len(chain), num_elements + 1)
00124 
00125         chain = Chain(self.a[:])
00126         i = 0
00127         chain.insert(i, 't')
00128         elements = self.a.strip().split()
00129         num_elements = len(elements)
00130         target_element = chain.data[ i ]
00131         self.assertEqual('t', target_element.data)
00132         self.assertEqual(len(chain), num_elements + 1)
00133 
00134         chain = Chain(self.b[:])
00135         i = 9
00136         chain.insert(i, Hetero('a'))
00137         elements = self.a.strip().split()
00138         num_elements = len(elements)
00139         target_element = chain.data[ i ]
00140         self.assertEqual('a', target_element.data)
00141         self.assertEqual(len(chain), num_elements + 1)
00142 
00143         chain = Chain(self.c[:])
00144         i = 5
00145         chain.insert(i, 'gln')
00146         elements = self.c.strip().split()
00147         num_elements = len(elements)
00148         target_element = chain.data[ i ]
00149         self.assertEqual('gln', target_element.data)
00150         self.assertEqual(len(chain), num_elements + 1)
00151 
00152 
00153     def testRemove(self):
00154 
00155         chain = Chain(self.a[:])
00156         elements = self.a.strip().split()
00157         num_elements = len(elements)
00158         num_a = chain.data.count(Hetero('a'))
00159         chain.remove('a')
00160         num_a_remaining = chain.data.count(Hetero('a'))
00161         self.assertEqual(num_a_remaining, num_a - 1)
00162         self.assertEqual(len(chain), num_elements - 1)
00163 
00164         chain = Chain(self.b[:])
00165         elements = self.b.strip().split()
00166         num_elements = len(elements)
00167         num_b = chain.data.count(Hetero('t'))
00168         chain.remove('t')
00169         num_b_remaining = chain.data.count(Hetero('t'))
00170         self.assertEqual(num_b_remaining, num_b - 1)
00171         self.assertEqual(len(chain), num_elements - 1)
00172 
00173         chain = Chain(self.c[:])
00174         elements = self.c.strip().split()
00175         num_elements = len(elements)
00176         num_leu = chain.data.count(Hetero('leu'))
00177         chain.remove('leu')
00178         num_leu_remaining = chain.data.count(Hetero('leu'))
00179         self.assertEqual(num_leu_remaining, num_leu - 1)
00180         self.assertEqual(len(chain), num_elements - 1)
00181 
00182 
00183     def testCount(self):
00184         chain = Chain(self.a[:])
00185         num_a = chain.data.count(Hetero('a'))
00186         self.assertEqual(chain.count('a'), num_a)
00187 
00188         chain = Chain(self.b[:])
00189         num_a = chain.data.count(Hetero('t'))
00190         self.assertEqual(chain.count('t'), num_a)
00191 
00192         chain = Chain(self.c[:])
00193         num_a = chain.data.count(Hetero('leu'))
00194         self.assertEqual(chain.count('leu'), num_a)
00195 
00196         chain = Chain(self.c[:])
00197         num_a = chain.data.count(Hetero('cys'))
00198         self.assertEqual(chain.count('cys'), num_a)
00199 
00200 
00201     def testIndex(self):
00202         chain = Chain(self.a[:])
00203         index_g = chain.data.index(Hetero('g'))
00204         self.assertEqual(chain.index('g'), index_g)
00205 
00206         chain = Chain(self.b[:])
00207         index_c = chain.data.index(Hetero('c'))
00208         self.assertEqual(chain.index('c'), index_c)
00209 
00210         chain = Chain(self.c[:])
00211         index_met = chain.data.index(Hetero('met'))
00212         self.assertEqual(chain.index('met'), index_met)
00213 
00214     def testGetItem(self):
00215         chain = Chain(self.a[:])
00216         element_3 = chain.data[ 3 ]
00217         self.assertEqual(chain[ 3 ], element_3)
00218 
00219         chain = Chain(self.a[:])
00220         element_0 = chain.data[ 0 ]
00221         self.assertEqual(chain[ 0 ], element_0)
00222 
00223         chain = Chain(self.b[:])
00224         element_7 = chain.data[ 7 ]
00225         self.assertEqual(chain[ 7 ], element_7)
00226 
00227         chain = Chain(self.b[:])
00228         last_element = chain.data[ -1 ]
00229         self.assertEqual(chain[ -1 ], last_element)
00230 
00231         chain = Chain(self.c[:])
00232         element_8 = chain.data[ 8 ]
00233         self.assertEqual(chain[ 8 ], element_8)
00234 
00235     def testSetItem(self):
00236         chain = Chain(self.a[:])
00237         chain[ 2 ] = 't'
00238         element_2 = chain.data[ 2 ]
00239         self.assertEqual(chain[ 2 ], element_2)
00240 
00241         chain = Chain(self.a[:])
00242         chain[ 0 ] = Hetero('U')
00243         element_0 = chain.data[ 0 ]
00244         self.assertEqual(chain[ 0 ], element_0)
00245 
00246         chain = Chain(self.b[:])
00247         chain[ -1 ] = Hetero('c')
00248         last_element = chain.data[ -1 ]
00249         self.assertEqual(chain[ -1 ], last_element)
00250 
00251         chain = Chain(self.b[:])
00252         chain[ 1 ] = 'a'
00253         element_1 = chain.data[ 1 ]
00254         self.assertEqual(chain[ 1 ], element_1)
00255 
00256         chain = Chain(self.c[:])
00257         chain[ 5 ] = 'ser'
00258         element_5 = chain.data[ 5 ]
00259         self.assertEqual(chain[ 5 ], element_5)
00260 
00261     def testDelItem(self):
00262 
00263         chain = Chain(self.a[:])
00264         elements = self.a.strip().split()
00265         num_elements = len(elements)
00266         num_t = chain.data.count(Hetero('t'))
00267         del chain[ 4 ]
00268         num_t_remaining = chain.data.count(Hetero('t'))
00269         self.assertEqual(num_t_remaining, num_t - 1)
00270         self.assertEqual(len(chain), num_elements - 1)
00271 
00272         chain = Chain(self.a[:])
00273         elements = self.a.strip().split()
00274         num_elements = len(elements)
00275         num_u = chain.data.count(Hetero('u'))
00276         del chain[ 12 ]
00277         num_u_remaining = 0
00278         self.assertEqual(num_u_remaining, num_u - 1)
00279         self.assertEqual(len(chain), num_elements - 1)
00280 
00281         chain = Chain(self.b[:])
00282         elements = self.b.strip().split()
00283         num_elements = len(elements)
00284         num_c = chain.data.count(Hetero('c'))
00285         del chain[ 0 ]
00286         num_c_remaining = chain.data.count(Hetero('c'))
00287         self.assertEqual(num_c_remaining, num_c - 1)
00288         self.assertEqual(len(chain), num_elements - 1)
00289 
00290         chain = Chain(self.b[:])
00291         elements = self.b.strip().split()
00292         num_elements = len(elements)
00293         num_g = chain.data.count(Hetero('t'))
00294         del chain[ 6 ]
00295         num_g_remaining = chain.data.count(Hetero('t'))
00296         self.assertEqual(num_g_remaining, num_g - 1)
00297         self.assertEqual(len(chain), num_elements - 1)
00298 
00299         chain = Chain(self.c[:])
00300         elements = self.c.strip().split()
00301         num_elements = len(elements)
00302         num_thr = chain.data.count(Hetero('thr'))
00303         del chain[ 0 ]
00304         num_thr_remaining = chain.data.count(Hetero('thr'))
00305         self.assertEqual(num_thr_remaining, num_thr - 1)
00306         self.assertEqual(len(chain), num_elements - 1)
00307 
00308     def testGetSlice(self):
00309         chain = Chain(self.a[:] )
00310         first = 0
00311         last = len(chain)
00312         slice = chain[ : ]
00313         other = chain.data[:]
00314         self.assertEqual(slice.data, other)
00315 
00316 
00317         chain = Chain(self.a[:] )
00318         first = 0
00319         last = 4
00320         slice = chain[ first : last ]
00321         other = chain.data[ first : last ]
00322         self.assertEqual(slice.data, other)
00323 
00324         chain = Chain(self.b[:])
00325         first = 2
00326         last = len(chain)
00327         slice = chain[ first: last ]
00328         other = chain.data[ first : last ]
00329         self.assertEqual(slice.data, other)
00330 
00331         chain = Chain(self.b[:])
00332         first = -1
00333         slice = chain[ first : ]
00334         other = chain.data[ first:  ]
00335         self.assertEqual(slice.data, other)
00336 
00337         chain = Chain(self.c[:])
00338         first = 3
00339         last = 7
00340         slice = chain[ first : last ]
00341         other = chain.data[ first: last ]
00342         self.assertEqual(slice.data, other)
00343 
00344         chain = Chain(self.c[:])
00345         first = 3
00346         last = -1
00347         slice = chain[ first : last ]
00348         other = chain.data[ first: last ]
00349         self.assertEqual(slice.data, other)
00350 
00351 
00352     def testSetSlice(self):
00353         chain = Chain(self.a[:] )
00354         slice = 'G T C A G 5NC G C A T G G'
00355         chain[ : ] = slice[ 4 : 7 ]
00356         other = Chain(slice[ 4: 7 ] )
00357         self.assertEqual(chain, other)
00358 
00359 
00360         chain = Chain(self.c[:] )
00361         old_chain = Chain(self.c[ : ])
00362         slice = 'MET ILE GLU ILE LYS ASP'
00363         chain[ 2 : 5 ] = slice
00364         other = Chain(old_chain.data[ :2 ] + Chain(slice).data + old_chain.data[ 5: ])
00365         self.assertEqual(chain, other)
00366 
00367         chain = Chain(self.c[:] )
00368         old_chain = Chain(self.c[ : ])
00369         slice = 'CYS GLY ALA GLU CYS VAL TYR'
00370         chain[ 7 : ] = slice
00371         other = Chain(old_chain.data[ :7 ] + Chain(slice).data)
00372         self.assertEqual(chain, other)
00373 
00374         chain = Chain(self.c[:] )
00375         old_chain = Chain(self.c[ : ])
00376         slice = 'SER ASN GLU TRP ASP '
00377         chain[ : 9 ] = slice
00378         other = Chain(Chain(slice).data + old_chain.data[ 9: ])
00379         self.assertEqual(chain, other)
00380 
00381     def testDelSlice(self):
00382         chain = Chain(self.c[ : ])
00383         old_chain = Chain(self.c[ : ])
00384         del chain[ 3 : 8 ]
00385         other = Chain(old_chain.data[ :3 ] + old_chain.data[ 8: ])
00386         self.assertEqual(chain, other)
00387 
00388         chain = Chain(self.c[:] )
00389         old_chain = Chain(self.c[ : ])
00390         del chain[ :4 ]
00391         other = Chain(old_chain.data[ 4: ])
00392         self.assertEqual(chain, other)
00393 
00394         chain = Chain(self.c[:] )
00395         old_chain = Chain(self.c[ : ])
00396         del chain[ 9: ]
00397         other = Chain(old_chain.data[ :9 ])
00398         self.assertEqual(chain, other)
00399 
00400 
00401     def testContains(self):
00402         chain = Chain(self.c[ : ])
00403         self.assertFalse('ser' in chain)
00404         self.assertTrue('lys' in chain)
00405         self.assertTrue('asp' in chain)
00406 
00407     def testAdd(self):
00408         texta = 'G U G G U C U G A U G A G G C C'
00409         textb = 'G G C C G A A A C U C G U A A G A G U C A C C A C'
00410         targeta = texta + Chain(textb)
00411         targetb = Chain(texta) + textb
00412         targetc = Chain(texta) + Chain(textb)
00413         self.assertEqual(targeta, targetc)
00414         self.assertEqual(targetb, targetc)
00415         self.assertEqual(targeta, targetb)
00416         self.assertEqual(len(targeta), len(Chain(texta)) + len(Chain(textb)))
00417 
00418         targetd = Chain(texta)
00419         targetd += textb
00420 
00421         targete = Chain(texta)
00422         targete += Chain(textb)
00423         self.assertEqual(targetd, targetc)
00424         self.assertEqual(targete, targetb)
00425 
00426 
00427 
00428 
00429 class CrystalTestCase(unittest.TestCase):
00430 
00431     def setUp(self):
00432 
00433         self.crystal = Crystal({ 'a' : 'T T G A C T C T C T T A A', \
00434                              'b' : Chain('G A G A G T C A'), \
00435                              'c' : 'T T G A C T C T C T T A A', \
00436                              'd' : Chain('G A G A G T C A')
00437                             })
00438 
00439     def testLen(self):
00440         self.assertEqual(len(self.crystal), len(self.crystal.data))
00441 
00442     def testGetItem(self):
00443         self.assertEqual(self.crystal[ 'a' ], self.crystal.data[ 'a' ])
00444 
00445     def testSetItem(self):
00446         target = copy.deepcopy(self.crystal)
00447         e = 'MET ALA LEU THR ASN ALA GLN ILE LEU ALA VAL ILE ASP SER'
00448         f = 'LEU GLY GLY GLY LEU GLN GLY THR LEU HIS CYS TYR GLU ILE PRO LEU'
00449         target[ 'e' ] = e
00450         target[ 'f' ] = Chain(f)
00451         self.assertEqual(Chain(e), target[ 'e' ])
00452         self.assertEqual(Chain(f), target[ 'f' ])
00453 
00454     def testDelItem(self):
00455         target = copy.deepcopy(self.crystal)
00456         del target[ 'b' ]
00457         self.assertFalse('b' in target.data)
00458         self.assertTrue('a' in target.data)
00459         self.assertTrue('c' in target.data)
00460 
00461     def testClear(self):
00462         target = copy.deepcopy(self.crystal)
00463         target.clear()
00464         self.assertEqual(len(target.data), 0)
00465 
00466     def testKeys(self):
00467         self.assertEqual(self.crystal.keys(), self.crystal.data.keys())
00468 
00469     def testValues(self):
00470         self.assertEqual(self.crystal.values(), self.crystal.data.values())
00471 
00472     def testItems(self):
00473         self.assertEqual(self.crystal.items(), self.crystal.data.items())
00474 
00475     def testKeys(self):
00476         self.assertEqual(self.crystal.keys(), self.crystal.data.keys())
00477 
00478     def testHasKey(self):
00479         self.assertTrue('b' in self.crystal)
00480         self.assertTrue('c' in self.crystal)
00481         self.assertFalse('z' in self.crystal)
00482 
00483 
00484 class HeteroTestCase(unittest.TestCase):
00485 
00486     def testInit(self):
00487         self.assertRaises(CrystalError, Hetero, 'abcd')
00488         self.assertRaises(CrystalError, Hetero, '')
00489         self.assertRaises(CrystalError, Hetero, 'A@#')
00490         self.assertRaises(CrystalError, Hetero, [])
00491         self.assertRaises(CrystalError, Hetero, {})
00492 
00493     def testLen(self):
00494         bru = Hetero('bru')
00495         self.assertEqual(len(bru), 3)
00496         _14w = Hetero('14w')
00497         self.assertEqual(len(_14w), 3)
00498         a = Hetero('a')
00499         self.assertEqual(len(a), 1)
00500         ga = Hetero('ga')
00501         self.assertEqual(len(ga), 2)
00502 
00503     def testEquals(self):
00504         u = Hetero('u')
00505         u1 = Hetero('u')
00506         self.assertEqual(u, u1)
00507         self.assertEqual(u, Hetero('U'))
00508         self.assertNotEqual(u, Hetero('u1'))
00509         self.assertNotEqual(u, Hetero('x'))
00510         gna = Hetero('gna')
00511         self.assertEqual(gna, Hetero('gNA'))
00512         self.assertEqual(gna, Hetero('GnA'))
00513         self.assertNotEqual(gna, Hetero('gnb'))
00514         self.assertNotEqual(gna, Hetero('na'))
00515 
00516 
00517 if __name__ == "__main__":
00518     runner = unittest.TextTestRunner(verbosity = 2)
00519     unittest.main(testRunner=runner)
00520