Back to index

python-biopython  1.60
test_GARepair.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 """Tests for Genetic Algorithm Repair code.
00003 
00004 This tests classes which are designed for repairing organisms after
00005 mutation and crossover.
00006 """
00007 # standard library
00008 import unittest
00009 
00010 # biopython
00011 from Bio.Alphabet import Alphabet
00012 from Bio.Seq import MutableSeq
00013 
00014 # local stuff
00015 from Bio.NeuralNetwork.Gene.Schema import Schema
00016 from Bio.GA.Organism import Organism
00017 from Bio.GA.Repair.Stabilizing import AmbiguousRepair
00018 
00019 
00020 
00021 class TestAlphabet(Alphabet):
00022     """Simple test alphabet.
00023     """
00024     alphabet_matches = {"1": "1",
00025                         "2": "2",
00026                         "3": "3",
00027                         "*": "123"}
00028                         
00029     letters = ["1", "2", "3", "*"]
00030 
00031 def test_fitness(genome):
00032     """Simple class for calculating fitnesses.
00033     """
00034     return 1
00035 
00036 class AmbiguousRepairTest(unittest.TestCase):
00037     """Test for the ability to repair too many ambiguous genes in a genome.
00038     """
00039     def setUp(self):
00040         alphabet = TestAlphabet()
00041         test_genome = MutableSeq("11*22*33*", alphabet)
00042         self.organism = Organism(test_genome, test_fitness)
00043         
00044         self.ambig_info = Schema(alphabet.alphabet_matches)
00045 
00046     def test_single_repair(self):
00047         """Test repair of a single ambiguous position in a genome.
00048         """
00049         repairer = AmbiguousRepair(self.ambig_info, 2)
00050 
00051         for repair_attempt in range(5):
00052             new_org = repairer.repair(self.organism)
00053             new_genome_seq = new_org.genome.toseq()
00054 
00055             assert new_genome_seq.count("*") == 2, \
00056                    "Did not repair genome, got %s" % new_genome_seq.tostring()
00057 
00058     def test_multiple_repair(self):
00059         """Test repair of multiple ambiguous positions in a genome.
00060         """
00061         repairer = AmbiguousRepair(self.ambig_info, 0)
00062 
00063         for repair_attempt in range(5):
00064             new_org = repairer.repair(self.organism)
00065             new_genome_seq = new_org.genome.toseq()
00066 
00067             assert new_genome_seq.count("*") == 0, \
00068                    "Did not repair genome, got %s" % new_genome_seq.tostring()
00069 
00070 if __name__ == "__main__":
00071     runner = unittest.TextTestRunner(verbosity = 2)
00072     unittest.main(testRunner=runner)