Package Bio :: Package GA :: Package Mutation :: Module Simple
[hide private]
[frames] | no frames]

Source Code for Module Bio.GA.Mutation.Simple

 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  """Perform Simple mutations on an organism's genome. 
 7  """ 
 8  # standard modules 
 9  import random 
10   
11  from Bio._py3k import range 
12   
13   
14 -class SinglePositionMutation(object):
15 """Perform a conversion mutation, but only at a single point in the genome. 16 17 This does not randomize the genome as much as ConversionMutation, since 18 only one change is allowed per genome at the specified mutation rate. 19 """
20 - def __init__(self, mutation_rate=0.001):
21 """Initialize a mutator. 22 23 Arguments: 24 25 o mutation_rate - The chance of a mutation happening once in the 26 genome. 27 """ 28 self._mutation_rate = mutation_rate 29 # a random number generator to test if we have a mutation 30 self._mutation_rand = random.Random() 31 # a random number generator to switch to a new alphabet letter 32 self._switch_rand = random.Random() 33 # a random number generator to find the mutation position 34 self._pos_rand = random.Random()
35
36 - def mutate(self, organism):
37 """Mutate the organisms genome. 38 """ 39 mutated_org = organism.copy() 40 gene_choices = mutated_org.genome.alphabet.letters 41 42 mutation_chance = self._mutation_rand.random() 43 if mutation_chance <= self._mutation_rate: 44 # pick a gene position to mutate at 45 mutation_pos = \ 46 self._pos_rand.choice(list(range(len(mutated_org.genome)))) 47 48 # get a new letter to replace the position at 49 new_letter = self._switch_rand.choice(gene_choices) 50 51 mutated_org.genome[mutation_pos] = new_letter 52 53 return mutated_org
54 55
56 -class ConversionMutation(object):
57 """Potentially mutate any item to another in the alphabet. 58 59 This just performs switching mutation -- changing one gene of a genome 60 to any other potential gene, at some defined frequency. If the organism 61 is determined to mutate, then the alphabet item it is equally likely 62 to switch to any other letter in the alphabet. 63 """
64 - def __init__(self, mutation_rate=0.001):
65 """Inititialize a mutator. 66 67 Arguments: 68 69 o mutation_rate -- The chance of a mutation happening at any 70 position in the genome. 71 """ 72 self._mutation_rate = mutation_rate 73 # a random number generator to test if we have a mutation 74 self._mutation_rand = random.Random() 75 # a random number generator to switch to a new alphabet letter 76 self._switch_rand = random.Random()
77
78 - def mutate(self, organism):
79 """Mutate the organisms genome. 80 """ 81 mutated_org = organism.copy() 82 83 gene_choices = mutated_org.genome.alphabet.letters 84 85 # potentially mutate any gene in the genome 86 for gene_index in range(len(mutated_org.genome)): 87 mutation_chance = self._mutation_rand.random() 88 # if we have a mutation 89 if mutation_chance <= self._mutation_rate: 90 # get a new letter 91 new_letter = self._switch_rand.choice(gene_choices) 92 mutated_org.genome[gene_index] = new_letter 93 94 return mutated_org
95