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 # Originally there was an RNG for each case. Python 3.6 was 30 # released with a bug under Windows where all RNGs created 31 # within a 15ms window have the same initial seed. 32 # To reduce possible correlations, re-use a single RNG. 33 rng = random.Random() 34 # a random number generator to test if we have a mutation 35 self._mutation_rand = rng 36 # a random number generator to switch to a new alphabet letter 37 self._switch_rand = rng 38 # a random number generator to find the mutation position 39 self._pos_rand = rng
40
41 - def mutate(self, organism):
42 """Mutate the organisms genome. 43 """ 44 mutated_org = organism.copy() 45 gene_choices = mutated_org.genome.alphabet.letters 46 47 mutation_chance = self._mutation_rand.random() 48 if mutation_chance <= self._mutation_rate: 49 # pick a gene position to mutate at 50 mutation_pos = \ 51 self._pos_rand.choice(list(range(len(mutated_org.genome)))) 52 53 # get a new letter to replace the position at 54 new_letter = self._switch_rand.choice(gene_choices) 55 56 mutated_org.genome[mutation_pos] = new_letter 57 58 return mutated_org
59 60
61 -class ConversionMutation(object):
62 """Potentially mutate any item to another in the alphabet. 63 64 This just performs switching mutation -- changing one gene of a genome 65 to any other potential gene, at some defined frequency. If the organism 66 is determined to mutate, then the alphabet item it is equally likely 67 to switch to any other letter in the alphabet. 68 """
69 - def __init__(self, mutation_rate=0.001):
70 """Inititialize a mutator. 71 72 Arguments: 73 74 o mutation_rate -- The chance of a mutation happening at any 75 position in the genome. 76 """ 77 self._mutation_rate = mutation_rate 78 # a random number generator to test if we have a mutation 79 self._mutation_rand = random.Random() 80 # a random number generator to switch to a new alphabet letter 81 self._switch_rand = random.Random()
82
83 - def mutate(self, organism):
84 """Mutate the organisms genome. 85 """ 86 mutated_org = organism.copy() 87 88 gene_choices = mutated_org.genome.alphabet.letters 89 90 # potentially mutate any gene in the genome 91 for gene_index in range(len(mutated_org.genome)): 92 mutation_chance = self._mutation_rand.random() 93 # if we have a mutation 94 if mutation_chance <= self._mutation_rate: 95 # get a new letter 96 new_letter = self._switch_rand.choice(gene_choices) 97 mutated_org.genome[gene_index] = new_letter 98 99 return mutated_org
100