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   
12 -class AmbiguousRepair(object):
13 """Perform repair to reduce the number of Ambiguous genes in a genome. 14 15 In cases where ambiguous genes are allowed in a genome (for example, 16 where you have a wild card character like '*' that will match 17 anything), these can come to dominate a genome since, really, the 18 best fitness is someting like '*******'. This repair protects against 19 that by changing ambiguous characters into some non-ambiguous gene. 20 """
21 - def __init__(self, ambig_finder, num_ambiguous):
22 """Initialize the repair class. 23 24 Arguments: 25 26 o ambig_finder - A class implementing the function find_ambiguous 27 which will return a list of all ambiguous positions in a sequence. 28 It also must have the function all_unambiguous, which will return 29 all allowed unambiguous letters. 30 31 o num_ambiguous - The minimum number of ambiguous items that are 32 allowed in a genome. If there are more than this present, repair 33 will be performed. 34 """ 35 self._ambig_finder = ambig_finder 36 self._num_ambiguous = num_ambiguous 37 self._alphabet_letters = ambig_finder.all_unambiguous()
38
39 - def repair(self, organism):
40 """Perform a repair to remove excess ambiguous genes. 41 """ 42 new_org = organism.copy() 43 44 # start getting rid of ambiguous items 45 while 1: 46 # first find all of the ambigous items 47 seq_genome = new_org.genome.toseq() 48 all_ambiguous = self._ambig_finder.find_ambiguous(str(seq_genome)) 49 50 # if we have less then the number of ambiguous allowed, stop 51 if len(all_ambiguous) <= self._num_ambiguous: 52 break 53 54 # remove an ambiguous item and replace it with a non-ambiguous 55 to_change = random.choice(all_ambiguous) 56 new_gene = random.choice(self._alphabet_letters) 57 new_org.genome[to_change] = new_gene 58 59 return new_org
60