Back to index

python-biopython  1.60
test_PopGen_FDist_nodepend.py
Go to the documentation of this file.
00001 # Copyright 2006 by Tiago Antao <tiagoantao@gmail.com>.  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 import os
00007 import unittest
00008 from Bio.PopGen import GenePop
00009 from Bio.PopGen import FDist
00010 from Bio.PopGen.GenePop import FileParser
00011 from Bio.PopGen.FDist.Utils import convert_genepop_to_fdist
00012 
00013 #Tests fdist related code. Note: this case doesn't require fdist
00014 #test_PopGen_FDist tests code that requires fdist
00015 
00016 
00017 class RecordTest(unittest.TestCase):
00018     def test_record_basic(self):
00019         """Basic test on Record
00020         """
00021 
00022         r = FDist.Record()
00023         assert type(r.data_org)  == int
00024         assert type(r.num_pops)  == int
00025         assert type(r.num_loci)  == int
00026         assert type(r.loci_data) == list
00027 
00028 class ParserTest(unittest.TestCase):
00029     def setUp(self):
00030         files = ["fdist1"]
00031         self.handles = []
00032         for filename in files:
00033             self.handles.append(open(os.path.join("PopGen", filename)))
00034 
00035         self.pops_loci = [
00036             (3, 4)
00037         ]
00038         self.num_markers = [
00039             [2, 3, 4, 2]
00040         ]
00041         #format is locus, pop, position, value
00042         self.test_pos = [
00043             [
00044                 (0, 0, 0, 5),
00045                 (3, 2, 0, 5)
00046             ]
00047         ]
00048 
00049     def tearDown(self):
00050         for handle in self.handles:
00051             handle.close()
00052 
00053     def test_record_parser(self):
00054         """Basic operation of the Record Parser.
00055         """
00056         for index in range(len(self.handles)):
00057             handle = self.handles[index]
00058             rec = FDist.read(handle)
00059             assert isinstance(rec, FDist.Record)
00060             assert rec.data_org == 0 #We don't support any other
00061             assert rec.num_pops, rec.num_loci == self.pops_loci[index]
00062             for i in range(len(self.num_markers[index])):
00063                 assert rec.loci_data[i][0] == \
00064                        self.num_markers[index][i]
00065             for i in range(len(self.test_pos[index])):
00066                 my_test_pos = self.test_pos[index]
00067                 for test in my_test_pos:
00068                     locus, pop, pos, value = test
00069                     assert(rec.loci_data[locus][1][pop][pos] == value)
00070 
00071 class ConversionTest(unittest.TestCase):
00072     def setUp(self):
00073         files = ["c2line.gen", "haplo2.gen"]
00074         self.handles = []
00075         self.names = []
00076         for filename in files:
00077             self.names.append(os.path.join("PopGen", filename))
00078             self.handles.append(open(os.path.join("PopGen", filename)))
00079 
00080     def test_convert(self):
00081         """Basic conversion test.
00082         """
00083         for i in range(len(self.names)):
00084             handle = self.handles[i]
00085             gp_rec = GenePop.read(handle)
00086             fd_rec = convert_genepop_to_fdist(gp_rec)
00087             assert(fd_rec.num_loci == 3)
00088             assert(fd_rec.num_pops == 3)
00089 
00090     def test_convert_big(self):
00091         """Big interface conversion test.
00092         """
00093         for i in range(len(self.names)):
00094             gp_rec = FileParser.read(self.names[i])
00095             fd_rec = convert_genepop_to_fdist(gp_rec)
00096             assert(fd_rec.num_loci == 3)
00097             assert(fd_rec.num_pops == 3)
00098             gp_rec._handle.close() #TODO - Needs a proper fix
00099 
00100     def tearDown(self):
00101         for handle in self.handles:
00102             handle.close()
00103 
00104 if __name__ == "__main__":
00105     runner = unittest.TextTestRunner(verbosity = 2)
00106     unittest.main(testRunner=runner)