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