Back to index

python-biopython  1.60
Simple.py
Go to the documentation of this file.
00001 """Perform Simple mutations on an organism's genome.
00002 """
00003 # standard modules
00004 import random
00005 
00006 class SinglePositionMutation(object):
00007     """Perform a conversion mutation, but only at a single point in the genome.
00008 
00009     This does not randomize the genome as much as ConversionMutation, since
00010     only one change is allowed per genome at the specified mutation rate.
00011     """
00012     def __init__(self, mutation_rate = 0.001):
00013         """Initialize a mutator.
00014 
00015         Arguments:
00016 
00017         o mutation_rate - The chance of a mutation happening once in the
00018         genome.
00019         """
00020         self._mutation_rate = mutation_rate
00021         # a random number generator to test if we have a mutation
00022         self._mutation_rand = random.Random()
00023         # a random number generator to switch to a new alphabet letter
00024         self._switch_rand = random.Random()
00025         # a random number generator to find the mutation position
00026         self._pos_rand = random.Random()
00027 
00028     def mutate(self, organism):
00029         """Mutate the organisms genome.
00030         """
00031         mutated_org = organism.copy()
00032         gene_choices = mutated_org.genome.alphabet.letters
00033 
00034         mutation_chance = self._mutation_rand.random()
00035         if mutation_chance <= self._mutation_rate:
00036             # pick a gene position to mutate at
00037             mutation_pos = \
00038                          self._pos_rand.choice(range(len(mutated_org.genome)))
00039             
00040             # get a new letter to replace the position at
00041             new_letter = self._switch_rand.choice(gene_choices)
00042 
00043             mutated_org.genome[mutation_pos] = new_letter
00044 
00045         return mutated_org
00046 
00047 class ConversionMutation(object):
00048     """Potentially mutate any item to another in the alphabet.
00049 
00050     This just performs switching mutation -- changing one gene of a genome
00051     to any other potential gene, at some defined frequency. If the organism
00052     is determined to mutate, then the alphabet item it is equally likely
00053     to switch to any other letter in the alphabet.
00054     """
00055     def __init__(self, mutation_rate = 0.001):
00056         """Inititialize a mutator.
00057 
00058         Arguments:
00059 
00060         o mutation_rate -- The chance of a mutation happening at any
00061         position in the genome.
00062         """
00063         self._mutation_rate = mutation_rate
00064         # a random number generator to test if we have a mutation
00065         self._mutation_rand = random.Random()
00066         # a random number generator to switch to a new alphabet letter
00067         self._switch_rand = random.Random()
00068 
00069     def mutate(self, organism):
00070         """Mutate the organisms genome.
00071         """
00072         mutated_org = organism.copy()
00073         
00074         gene_choices = mutated_org.genome.alphabet.letters
00075         
00076         # potentially mutate any gene in the genome
00077         for gene_index in range(len(mutated_org.genome)):
00078             mutation_chance = self._mutation_rand.random()
00079             # if we have a mutation
00080             if mutation_chance <= self._mutation_rate:
00081                 # get a new letter
00082                 new_letter = self._switch_rand.choice(gene_choices)
00083                 mutated_org.genome[gene_index] = new_letter
00084 
00085         return mutated_org
00086                 
00087         
00088