Back to index

python-biopython  1.60
test_SCOP_Cla.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 
00009 """Unit test for Cla"""
00010 
00011 import unittest
00012 
00013 from Bio.SCOP import Cla
00014 
00015 
00016 
00017 
00018 class ClaTests(unittest.TestCase):
00019 
00020     def setUp(self):
00021         self.filename = './SCOP/dir.cla.scop.txt_test'
00022 
00023     def testParse(self):
00024         """Test if all records in a CLA file are being read"""
00025         f=open(self.filename)
00026         try: 
00027             count = 0
00028             records = Cla.parse(f)
00029             for record in records:
00030                 count +=1
00031             self.assertEqual(count, 14)
00032         finally:
00033             f.close()
00034     
00035     def testStr(self):
00036         """Test if we can convert each record to a string correctly"""
00037         f = open(self.filename)
00038         try: 
00039             for line in f:
00040                 record = Cla.Record(line)
00041                 # The SCOP Classification file format which can be found at
00042                 # http://scop.mrc-lmb.cam.ac.uk/scop/release-notes.html states
00043                 # that the list of classification hierarchy key-value pairs is
00044                 # unordered, therefore we need only check that they are all
00045                 # there, NOT that they are in the same order.
00046                 #End of line is platform dependent. Strip it off
00047                 expected_hierarchy = line.rstrip().split('\t')[5].split(',')
00048                 expected_hierarchy = dict(pair.split('=') for pair
00049                                           in expected_hierarchy)
00050                 actual_hierarchy = str(record).rstrip().split('\t')[5].split(',')
00051                 actual_hierarchy = dict(pair.split('=') for pair
00052                                         in actual_hierarchy)
00053                 self.assertEqual(len(actual_hierarchy),
00054                                   len(expected_hierarchy))
00055                 for key, actual_value in actual_hierarchy.iteritems():
00056                     self.assertEqual(actual_value, expected_hierarchy[key])
00057         finally:
00058             f.close()        
00059 
00060     def testError(self):
00061         """Test if a corrupt record raises the appropriate exception"""
00062         corruptRec = "49268\tsp\tb.1.2.1\t-\n"
00063         self.assertRaises(ValueError, Cla.Record, corruptRec)
00064 
00065     def testRecord(self):
00066         """Test one record in detail"""
00067         recLine = 'd1dan.1\t1dan\tT:,U:91-106\tb.1.2.1\t21953\tcl=48724,cf=48725,sf=49265,fa=49266,dm=49267,sp=49268,px=21953'
00068 
00069         record = Cla.Record(recLine)
00070         self.assertEqual(record.sid, 'd1dan.1')
00071         self.assertEqual(record.residues.pdbid, '1dan')
00072         self.assertEqual(record.residues.fragments, (('T','',''),('U','91','106')))
00073         self.assertEqual(record.sccs, 'b.1.2.1')
00074         self.assertEqual(record.sunid, 21953)
00075         self.assertEqual(record.hierarchy, {'cl' : 48724,
00076                                             'cf' : 48725,
00077                                             'sf' : 49265,
00078                                             'fa' : 49266,
00079                                             'dm' : 49267,
00080                                             'sp' : 49268,
00081                                             'px' : 21953})
00082 
00083     def testIndex(self):
00084         """Test CLA file indexing"""
00085         index = Cla.Index(self.filename)
00086         
00087         self.assertEqual(len(index), 14)
00088         self.assertTrue('d4hbia_' in index)
00089 
00090         rec = index['d1hbia_']
00091         self.assertEqual(rec.sunid, 14996)
00092 
00093 
00094 
00095 if __name__=='__main__':
00096     runner = unittest.TextTestRunner(verbosity = 2)
00097     unittest.main(testRunner=runner)