Back to index

python-biopython  1.60
test_SCOP_Scop.py
Go to the documentation of this file.
00001 # Copyright 2001 by Gavin E. Crooks.  All rights reserved.
00002 # Modifications Copyright 2010 Jeffrey Finkelstein. All rights reserved.
00003 #
00004 # This code is part of the Biopython distribution and governed by its
00005 # license.  Please see the LICENSE file that should have been included
00006 # as part of this package.
00007 
00008 """Unit test for Scop"""
00009 
00010 import unittest
00011 from StringIO import *
00012 
00013 from Bio.SCOP import *
00014 
00015 
00016 
00017 class ScopTests(unittest.TestCase):
00018 
00019     def _compare_cla_lines(self, cla_line_1, cla_line_2):
00020         """Compares the two specified Cla lines for equality.
00021 
00022         The order of the key-value pairs in the sixth field of the lines does
00023         not matter. For more information, see
00024         http://scop.mrc-lmb.cam.ac.uk/scop/release-notes.html.
00025         """
00026         fields1 = cla_line_1.rstrip().split('\t')
00027         fields2 = cla_line_2.rstrip().split('\t')
00028         print fields1
00029         print fields2
00030         # compare the first five fields in a Cla line, which should be exactly
00031         # the same
00032         if fields1[:5] != fields2[:5]:
00033             return False
00034         # compare the hierarchy key-value pairs, which are unordered
00035         if set(fields1[5].split(',')) != set(fields2[5].split(',')):
00036             return False
00037         return True
00038 
00039     def testParse(self):
00040   
00041         f = open("./SCOP/dir.cla.scop.txt_test")
00042         try:
00043             cla = f.read()
00044             f.close()
00045             
00046             f = open("./SCOP/dir.des.scop.txt_test")
00047             des = f.read()
00048             f.close()
00049 
00050             f = open("./SCOP/dir.hie.scop.txt_test")
00051             hie = f.read()
00052         finally:
00053             f.close()
00054 
00055         scop = Scop(StringIO(cla), StringIO(des), StringIO(hie))
00056 
00057         cla_out = StringIO()
00058         scop.write_cla(cla_out)
00059         lines = zip(cla.rstrip().split('\n'),
00060                     cla_out.getvalue().rstrip().split('\n'))
00061         for expected_line, line in lines:
00062             self.assertTrue(self._compare_cla_lines(expected_line, line))
00063         
00064         des_out = StringIO()
00065         scop.write_des(des_out)
00066         self.assertEqual(des_out.getvalue(), des)
00067 
00068         hie_out = StringIO()
00069         scop.write_hie(hie_out)
00070         self.assertEqual(hie_out.getvalue(), hie)
00071 
00072         domain = scop.getDomainBySid("d1hbia_")
00073         self.assertEqual(domain.sunid, 14996)
00074 
00075         domains = scop.getDomains()
00076         self.assertEqual(len(domains), 14)
00077         self.assertEqual(domains[4].sunid, 14988)
00078 
00079 
00080         dom = scop.getNodeBySunid(-111)
00081         self.assertEqual(dom, None)
00082         dom = scop.getDomainBySid("no such domain")
00083         self.assertEqual(dom, None)
00084                 
00085 
00086 
00087     def testSccsOrder(self):
00088         self.assertEqual(cmp_sccs("a.1.1.1", "a.1.1.1"), 0)
00089         self.assertEqual(cmp_sccs("a.1.1.2", "a.1.1.1"), 1)
00090         self.assertEqual(cmp_sccs("a.1.1.2", "a.1.1.11"), -1)
00091         self.assertEqual(cmp_sccs("a.1.2.2", "a.1.1.11"), 1)
00092         self.assertEqual(cmp_sccs("a.1.2.2", "a.5.1.11"), -1)
00093         self.assertEqual(cmp_sccs("b.1.2.2", "a.5.1.11"), 1)
00094         self.assertEqual(cmp_sccs("b.1.2.2", "b.1.2"), 1)
00095 
00096     def testParseDomain(self):
00097         s=">d1tpt_1 a.46.2.1 (1-70) Thymidine phosphorylase {Escherichia coli}"
00098         dom = parse_domain(s)
00099 
00100         self.assertEqual(dom.sid, 'd1tpt_1')
00101         self.assertEqual(dom.sccs, 'a.46.2.1')
00102         self.assertEqual(dom.residues.pdbid, '1tpt')
00103         self.assertEqual(dom.description, 'Thymidine phosphorylase {Escherichia coli}')
00104 
00105         s2="d1tpt_1 a.46.2.1 (1tpt 1-70) Thymidine phosphorylase {E. coli}"
00106         self.assertEqual(s2, str(parse_domain(s2)))
00107 
00108 
00109 
00110         #Genetic domains (See Astral release notes)
00111         s3="g1cph.1 g.1.1.1 (1cph B:,A:) Insulin {Cow (Bos taurus)}"
00112         self.assertEqual(s3, str(parse_domain(s3)))
00113 
00114         s4="e1cph.1a g.1.1.1 (1cph A:) Insulin {Cow (Bos taurus)}"
00115         self.assertEqual(s4, str(parse_domain(s4)))
00116 
00117         #Raw Astral header
00118         s5=">e1cph.1a g.1.1.1 (A:) Insulin {Cow (Bos taurus)}"
00119         self.assertEqual(s4,  str(parse_domain(s5)))
00120 
00121         self.assertRaises(ValueError, parse_domain, "Totally wrong")
00122 
00123     def testConstructFromDirectory(self):
00124         scop = Scop (dir_path="SCOP", version="test")
00125         self.assertTrue(isinstance(scop, Scop))
00126          
00127         domain = scop.getDomainBySid("d1hbia_")
00128         self.assertEqual(domain.sunid, 14996)
00129          
00130     def testGetAscendent(self):
00131         scop = Scop (dir_path="SCOP", version="test")
00132         domain = scop.getDomainBySid("d1hbia_")
00133 
00134         # get the fold
00135         fold = domain.getAscendent('cf')
00136         self.assertEqual(fold.sunid, 46457)
00137         
00138         #get the superfamily
00139         sf = domain.getAscendent('superfamily')
00140         self.assertEqual(sf.sunid, 46458)
00141 
00142         # px has no px ascendent
00143         px = domain.getAscendent('px')
00144         self.assertEqual(px, None)
00145 
00146         # an sf has no px ascendent
00147         px2 = sf.getAscendent('px')
00148         self.assertEqual(px2, None)
00149 
00150 
00151     def test_get_descendents(self):
00152         """Test getDescendents method"""
00153         scop = Scop (dir_path="SCOP", version="test")
00154         fold = scop.getNodeBySunid(46457)
00155 
00156         # get px descendents
00157         domains = fold.getDescendents('px')
00158         self.assertEqual(len(domains), 14)
00159         for d in domains:
00160             self.assertEqual(d.type, 'px')
00161             
00162         sfs = fold.getDescendents('superfamily')
00163         self.assertEqual(len(sfs), 1)
00164         for d in sfs:
00165             self.assertEqual(d.type, 'sf')
00166 
00167         # cl has no cl descendent
00168         cl = fold.getDescendents('cl')
00169         self.assertEqual(cl, [])
00170         
00171         
00172 
00173 
00174 if __name__=='__main__':
00175     runner = unittest.TextTestRunner(verbosity = 2)
00176     unittest.main(testRunner=runner)