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

Source Code for Module Bio.GA.Repair.Stabilizing

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