Back to index

python-biopython  1.60
Stabilizing.py
Go to the documentation of this file.
00001 """Methods for performing repairs that will Stabilize genomes.
00002 
00003 These methods perform repair to keep chromosomes from drifting too far in
00004 any direction -- ie. bring them back to a stabilizing center. This may be
00005 useful in cases where fitness functions alone won't keep chromosomes in
00006 check.
00007 """
00008 # standard library
00009 import random
00010 
00011 class AmbiguousRepair(object):
00012     """Perform repair to reduce the number of Ambiguous genes in a genome.
00013 
00014     In cases where ambiguous genes are allowed in a genome (for example,
00015     where you have a wild card character like '*' that will match
00016     anything), these can come to dominate a genome since, really, the
00017     best fitness is someting like '*******'. This repair protects against
00018     that by changing ambiguous characters into some non-ambiguous gene.
00019     """
00020     def __init__(self, ambig_finder, num_ambiguous):
00021         """Initialize the repair class.
00022 
00023         Arguments:
00024 
00025         o ambig_finder - A class implementing the function find_ambiguous
00026         which will return a list of all ambiguous positions in a sequence.
00027         It also must have the function all_unambiguous, which will return
00028         all allowed unambiguous letters.
00029 
00030         o num_ambiguous - The minimum number of ambiguous items that are
00031         allowed in a genome. If there are more than this present, repair
00032         will be performed.
00033         """
00034         self._ambig_finder = ambig_finder
00035         self._num_ambiguous = num_ambiguous
00036         self._alphabet_letters = ambig_finder.all_unambiguous()
00037 
00038     def repair(self, organism):
00039         """Perform a repair to remove excess ambiguous genes.
00040         """
00041         new_org = organism.copy()
00042         
00043         # start getting rid of ambiguous items
00044         while 1:
00045             # first find all of the ambigous items
00046             seq_genome = new_org.genome.toseq()
00047             all_ambiguous = self._ambig_finder.find_ambiguous(seq_genome.tostring())
00048 
00049             # if we have less then the number of ambiguous allowed, stop
00050             if len(all_ambiguous) <= self._num_ambiguous:
00051                 break
00052 
00053             # remove an ambiguous item and replace it with a non-ambiguous
00054             to_change = random.choice(all_ambiguous)
00055             new_gene = random.choice(self._alphabet_letters)
00056             new_org.genome[to_change] = new_gene
00057 
00058         return new_org
00059