Package Bio :: Package GA :: Package Repair :: Module Stabilizing
[hide private]
[frames] | no frames]

Source Code for Module Bio.GA.Repair.Stabilizing

 1  """Methods for performing repairs that will Stabilize genomes. 
 2   
 3  These methods perform repair to keep chromosomes from drifting too far in 
 4  any direction -- ie. bring them back to a stabilizing center. This may be 
 5  useful in cases where fitness functions alone won't keep chromosomes in 
 6  check. 
 7  """ 
 8  # standard library 
 9  import random 
10   
11 -class AmbiguousRepair:
12 """Perform repair to reduce the number of Ambiguous genes in a genome. 13 14 In cases where ambiguous genes are allowed in a genome (for example, 15 where you have a wild card character like '*' that will match 16 anything), these can come to dominate a genome since, really, the 17 best fitness is someting like '*******'. This repair protects against 18 that by changing ambiguous characters into some non-ambiguous gene. 19 """
20 - def __init__(self, ambig_finder, num_ambiguous):
21 """Initialize the repair class. 22 23 Arguments: 24 25 o ambig_finder - A class implementing the function find_ambiguous 26 which will return a list of all ambiguous positions in a sequence. 27 It also must have the function all_unambiguous, which will return 28 all allowed unambiguous letters. 29 30 o num_ambiguous - The minimum number of ambiguous items that are 31 allowed in a genome. If there are more than this present, repair 32 will be performed. 33 """ 34 self._ambig_finder = ambig_finder 35 self._num_ambiguous = num_ambiguous 36 self._alphabet_letters = ambig_finder.all_unambiguous()
37
38 - def repair(self, organism):
39 """Perform a repair to remove excess ambiguous genes. 40 """ 41 new_org = organism.copy() 42 43 # start getting rid of ambiguous items 44 while 1: 45 # first find all of the ambigous items 46 seq_genome = new_org.genome.toseq() 47 all_ambiguous = self._ambig_finder.find_ambiguous(seq_genome.data) 48 49 # if we have less then the number of ambiguous allowed, stop 50 if len(all_ambiguous) <= self._num_ambiguous: 51 break 52 53 # remove an ambiguous item and replace it with a non-ambiguous 54 to_change = random.choice(all_ambiguous) 55 new_gene = random.choice(self._alphabet_letters) 56 new_org.genome[to_change] = new_gene 57 58 return new_org
59