Back to index

python-biopython  1.60
test_PopGen_GenePop_nodepend.py
Go to the documentation of this file.
00001 # Copyright 2007 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 
00007 import os
00008 import unittest
00009 from Bio.PopGen import GenePop
00010 from Bio.PopGen.GenePop import FileParser
00011 
00012 
00013 class RecordTest(unittest.TestCase):
00014     def test_record_basic(self):
00015         """Basic test on Record
00016         """
00017 
00018         r = GenePop.Record()
00019         assert type(r.marker_len)   == int
00020         assert type(r.comment_line) == str
00021         assert type(r.loci_list)    == list
00022         assert type(r.populations)  == list
00023 
00024 
00025 class ParserTest(unittest.TestCase):
00026     def setUp(self):
00027         files = ["c2line.gen", "c3line.gen", "c2space.gen", "c3space.gen",
00028                  "haplo3.gen", "haplo2.gen"]
00029         self.handles = []
00030         for filename in files:
00031             self.handles.append(open(os.path.join("PopGen", filename)))
00032 
00033         self.pops_indivs = [
00034             (3, [4, 3, 5]),
00035             (3, [4, 3, 5]),
00036             (3, [4, 3, 5]),
00037             (3, [4, 3, 5]),
00038             (3, [4, 3, 5]),
00039             (3, [4, 3, 5])
00040         ]
00041         self.num_loci = [3, 3, 3, 3, 3, 3]
00042         self.marker_len = [2, 3, 2, 3, 3, 2]
00043         self.pop_names = ["4", "b3", "5"]
00044 
00045     def tearDown(self):
00046         for handle in self.handles:
00047             handle.close()
00048 
00049     def test_record_parser(self):
00050         """Basic operation of the Record Parser.
00051         """
00052         for index in range(len(self.handles)):
00053             handle = self.handles[index]
00054             rec = GenePop.read(handle)
00055             assert isinstance(rec, GenePop.Record)
00056             assert len(rec.loci_list) == self.num_loci[index]
00057             assert rec.marker_len == self.marker_len[index]
00058             assert len(rec.populations) == self.pops_indivs[index][0]
00059             assert rec.pop_list == self.pop_names
00060             for i in range(self.pops_indivs[index][0]):
00061                 assert len(rec.populations[i]) == \
00062                            self.pops_indivs[index][1][i]
00063 
00064     def test_wrong_file_parser(self):
00065         """Testing the ability to deal with wrongly formatted files
00066         """
00067         f = open(os.path.join("PopGen", "fdist1"))
00068         try:
00069             rec = GenePop.read(f)
00070             raise Error("Should have raised exception")
00071         except ValueError:
00072             pass
00073         f.close()
00074 
00075 
00076 class FileParserTest(unittest.TestCase):
00077     def setUp(self):
00078         self.files = map(lambda x: os.path.join("PopGen", x),
00079              ["c2line.gen", "c3line.gen", "c2space.gen",
00080               "c3space.gen", "haplo3.gen", "haplo2.gen"])
00081         self.pops_indivs = [
00082             (3, [4, 3, 5]),
00083             (3, [4, 3, 5]),
00084             (3, [4, 3, 5]),
00085             (3, [4, 3, 5]),
00086             (3, [4, 3, 5]),
00087             (3, [4, 3, 5])
00088         ]
00089         self.num_loci = [3, 3, 3, 3, 3, 3]
00090 
00091     def test_file_record_parser(self):
00092         """Basic operation of the File Record Parser.
00093         """
00094         for index in range(len(self.files)):
00095             fname = self.files[index]
00096             rec = FileParser.read(fname)
00097             assert isinstance(rec, FileParser.FileRecord)
00098             assert len(rec.loci_list) == self.num_loci[index]
00099             for skip in range(self.pops_indivs[index][0]):
00100                 if rec.skip_population() == False:
00101                     raise Error("Not enough populations")
00102             if rec.skip_population() == True:
00103                     raise Error("Too much populations")
00104             for i in range(self.pops_indivs[index][0]):
00105                 continue
00106                 assert len(rec.populations[i]) == \
00107                            self.pops_indivs[index][1][i]
00108             rec._handle.close() #TODO - Needs a proper fix
00109 
00110     def test_wrong_file_parser(self):
00111         """Testing the ability to deal with wrongly formatted files
00112         """
00113         f = open(os.path.join("PopGen", "fdist1"))
00114         try:
00115             rec = GenePop.read(f)
00116             raise Error("Should have raised exception")
00117         except ValueError:
00118             pass
00119         f.close()
00120 
00121 class UtilsTest(unittest.TestCase):
00122     def setUp(self):
00123         #All files have to have at least 3 loci and 2 pops
00124         files = ["c2line.gen"]
00125         self.handles = []
00126         for filename in files:
00127             self.handles.append(open(os.path.join("PopGen", filename)))
00128 
00129 
00130     def tearDown(self):
00131         for handle in self.handles:
00132             handle.close()
00133 
00134     def test_utils(self):
00135         """Basic operation of GenePop Utils.
00136         """
00137         for index in range(len(self.handles)):
00138             handle = self.handles[index]
00139             rec = GenePop.read(handle)
00140         initial_pops = len(rec.populations)
00141         initial_loci = len(rec.loci_list)
00142         first_loci = rec.loci_list[0]
00143         rec.remove_population(0)
00144         assert len(rec.populations) == initial_pops - 1
00145         rec.remove_locus_by_name(first_loci)
00146         assert len(rec.loci_list) == initial_loci - 1
00147         assert rec.loci_list[0] != first_loci
00148         rec.remove_locus_by_position(0)
00149         assert len(rec.loci_list) == initial_loci - 2
00150 
00151 if __name__ == "__main__":
00152     runner = unittest.TextTestRunner(verbosity = 2)
00153     unittest.main(testRunner=runner)