-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPopG_test.py
114 lines (100 loc) · 2.95 KB
/
PopG_test.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#################################
# #
# Author: Benjamin S. Toups #
# 8/30/2019 #
# #
#################################
import os
import numpy as np
import matplotlib.pyplot as plt
import random
os.system('cls')
N = int(input('What is the size of the population? '))
# freqA = float(input('What is the starting frequency of the A allele? '))
freqA = .5
# gens = int(input('How many generations is the population simulated over? '))
gens = 100
# runs = int(input('How many runs should be simulated simultaneously? '))
runs = 5
# Creating the random numbe generator that will be used to generate
# the starting population as well as select individuals to breed
def randnum():
num1 = random.randint(1, (2 ** 15 - 1))
num2 = random.randint(1, (2 ** 15 - 1))
numer = (num1 * 32768) + num2
choice = numer / 1073741823
return choice
# selects a parent from the population
def PickParents():
num = (randnum() * N) // 1
return num
# Generates individuals based on an initial frequency
def GenIndiv():
first = randnum()
if first <= freqA:
first = 1
else:
first = 0
second = randnum()
if second <= freqA:
second = 1
else:
second = 0
if first + second == 2:
return "AA"
elif first + second == 1:
return "Aa"
else:
return "aa"
# calculates the allele frequencies of a population
def calcFreqs(pops):
A = 0
for i in pops:
if i == 'AA':
A += 2
elif i == 'Aa' or i == 'aA':
A += 1
else:
pass
frequency = A / (N * 2)
return frequency
finalfreqs = [] # the list that will contain the final frequencies each run
for i in range(runs):
g = 1
indivs = []
freqs = []
for i in range(N): # generates an initial population
indivs.append(GenIndiv())
freqs.append(calcFreqs(indivs))
while g != gens: # iteratively calculates p(A) for each generation
newGen = []
for i in range(N):
first = int(PickParents())
second = int(PickParents())
while first == second:
second = int(PickParents())
firstparent = indivs[first]
secondparent = indivs[second]
ran1 = randnum()
if ran1 < .5:
firstgamete = firstparent[0]
else:
firstgamete = firstparent[1]
ran2 = randnum()
if ran2 < .5:
secondgamete = secondparent[0]
else:
secondgamete = secondparent[1]
newAllele = firstgamete + secondgamete
newGen.append(newAllele)
freqs.append(calcFreqs(newGen))
indivs = newGen
g += 1
finalfreqs.append(freqs[-1])
# plt.plot(range(gens), freqs)
print(finalfreqs)
# plt.xlabel('Generation')
# plt.ylabel('p(A)')
# plt.ylim(0, 1)
# plt.xlim(0, gens)
# plt.show()