File: test_GARepair.py

package info (click to toggle)
python-biopython 1.54-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 25,400 kB
  • ctags: 10,975
  • sloc: python: 116,757; xml: 33,167; ansic: 8,622; sql: 1,488; makefile: 147
file content (72 lines) | stat: -rw-r--r-- 2,179 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
"""Tests for Genetic Algorithm Repair code.

This tests classes which are designed for repairing organisms after
mutation and crossover.
"""
# standard library
import unittest

# biopython
from Bio.Alphabet import Alphabet
from Bio.Seq import MutableSeq

# local stuff
from Bio.NeuralNetwork.Gene.Schema import Schema
from Bio.GA.Organism import Organism
from Bio.GA.Repair.Stabilizing import AmbiguousRepair



class TestAlphabet(Alphabet):
    """Simple test alphabet.
    """
    alphabet_matches = {"1": "1",
                        "2": "2",
                        "3": "3",
                        "*": "123"}
                        
    letters = ["1", "2", "3", "*"]

def test_fitness(genome):
    """Simple class for calculating fitnesses.
    """
    return 1

class AmbiguousRepairTest(unittest.TestCase):
    """Test for the ability to repair too many ambiguous genes in a genome.
    """
    def setUp(self):
        alphabet = TestAlphabet()
        test_genome = MutableSeq("11*22*33*", alphabet)
        self.organism = Organism(test_genome, test_fitness)
        
        self.ambig_info = Schema(alphabet.alphabet_matches)

    def test_single_repair(self):
        """Test repair of a single ambiguous position in a genome.
        """
        repairer = AmbiguousRepair(self.ambig_info, 2)

        for repair_attempt in range(5):
            new_org = repairer.repair(self.organism)
            new_genome_seq = new_org.genome.toseq()

            assert new_genome_seq.data.count("*") == 2, \
                   "Did not repair genome, got %s" % new_genome_seq.data

    def test_multiple_repair(self):
        """Test repair of multiple ambiguous positions in a genome.
        """
        repairer = AmbiguousRepair(self.ambig_info, 0)

        for repair_attempt in range(5):
            new_org = repairer.repair(self.organism)
            new_genome_seq = new_org.genome.toseq()

            assert new_genome_seq.data.count("*") == 0, \
                   "Did not repair genome, got %s" % new_genome_seq.data

if __name__ == "__main__":
    runner = unittest.TextTestRunner(verbosity = 2)
    unittest.main(testRunner=runner)