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
27 - def __init__(self, ambig_finder, num_ambiguous):
28 """Initialize the repair class. 29 30 Arguments: 31 - 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 - num_ambiguous - The minimum number of ambiguous items that are 36 allowed in a genome. If there are more than this present, repair 37 will be performed. 38 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 new_org = organism.copy() 47 48 # start getting rid of ambiguous items 49 while True: 50 # first find all of the ambigous items 51 seq_genome = new_org.genome.toseq() 52 all_ambiguous = self._ambig_finder.find_ambiguous(str(seq_genome)) 53 54 # if we have less then the number of ambiguous allowed, stop 55 if len(all_ambiguous) <= self._num_ambiguous: 56 break 57 58 # remove an ambiguous item and replace it with a non-ambiguous 59 to_change = random.choice(all_ambiguous) 60 new_gene = random.choice(self._alphabet_letters) 61 new_org.genome[to_change] = new_gene 62 63 return new_org
64