File: Uniform.py

package info (click to toggle)
python-biopython 1.30-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 14,680 kB
  • ctags: 11,177
  • sloc: python: 60,275; ansic: 7,728; cpp: 1,844; sql: 1,144; makefile: 193
file content (63 lines) | stat: -rw-r--r-- 1,935 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""Perform uniform crossovers between the genomes of two organisms.


genome 1 --       A B C D E F G
                  .   . .   .
genome 2 --       a b c d e f g

After crossover:

new genome 1 --  a B c d E f G
new genome 2 --  A b C D e F g

Uniform Crossover is a standard crossover technique for
rapid mutation-behavior.  
"""
# standard modules
import whrandom

class UniformCrossover:
    """Perform single point crossover between genomes at some defined rates.

    This performs a single crossover between two genomes at some
    defined frequency. The location of the crossover is chosen randomly
    if the crossover meets the probability to occur.
    """
    def __init__(self, crossover_prob = .1, uniform_prob = 0.7):
        """Initialize to do uniform crossover at the specified probability and frequency.
        """
        self._crossover_prob = crossover_prob
	self._uniform_prob   = uniform_prob
        # a random number generator to test if we have a crossover
        self._crossover_rand = whrandom.whrandom()
        # a random number generator to pick a crossover location
        self._uniform_rand = whrandom.whrandom()
	return
	
    def do_crossover(self, org_1, org_2):
        """Potentially do a crossover between the two organisms.
        """
        new_org_1 = org_1.copy()
        new_org_2 = org_2.copy()
        
        # determine if we have a crossover
        crossover_chance = self._crossover_rand.random()
        if crossover_chance <= self._crossover_prob:
	    
	    minlen = min(len(new_org_1.genome),len(new_org_2.genome))
	    for i in range( minlen ):
		
		uniform_chance = self._uniform_rand.random()
		if uniform_chance <= self._uniform_prob:
		    # cycle element
		    temp                  = new_org_1.genome[ i ]
		    new_org_1.genome[ i ] = new_org_2.genome[ i ]
		    new_org_2.genome[ i ] = temp
	    
        return new_org_1, new_org_2