-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenetics.sim.py
97 lines (66 loc) · 2.42 KB
/
genetics.sim.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
#Code i made for Genetics Lab Ex9 - Simulation of Hardy-Weinberg Equilibrium
#Really dirty and i hope to improve this. and maybe build a UI
import random
def withSubstitution(count): #try to verify accuracy of this part
matinglist = list()
for i in range(count):
A = masterlist.pop(random.randint(0,len(masterlist)-1))
B = masterlist.pop(random.randint(0,len(masterlist)-1))
#displays the result
mating = A+B
#print('{}'.format(mating),end='')
#returns the beads back to the tray
masterlist.append(A)
masterlist.append(B)
#appends the result to matinglist
matinglist.append(mating)
print(matinglist)
AA = matinglist.count('AA')
Aa = matinglist.count('Aa') + matinglist.count('aA')
aa = matinglist.count('aa')
print('\nAA = {}\nAa = {}\naa = {}\nTotal={}'.format(AA,Aa,aa,(AA+Aa+aa)))
countlist = [AA,Aa,aa]
return(countlist)
#there's a pop index out of range error that occurs once in a while.
#fixit
def chisquare(lista):
# with the data from the WithSubstitution, return a 0 and a 1 for accept,reject
#H-W model
#p^2 + 2pq + q^2 = 1
#where p = A_dom, q =A_rec
p = A_dom/n
q = A_rec/n
ExpRatioAA = p*p*1.00
ExpRatioAa = p*q*2.00
ExpRatioaa = q*q*1.0
ExpRatios = [ExpRatioAA,ExpRatioAa,ExpRatioaa]
ObservedValue = lista
ExpValue = list()
DevValues = list()
ChiValues = list()
for i in range(len(ExpRatios)):
ExpValue.append(ExpRatios[i] * countt)
for i in range(len(ExpRatios)):
DevValues.append(abs(ExpValue[i]-ObservedValue[i]))
for i in range(len(ExpRatios)):
ChiValues.append(DevValues[i]*DevValues[i]/ExpValue[i])
chicrit = 5.991
chicalc = sum(ChiValues)
print('Chisq(0.05, df2) ={}'.format(chicalc))
return(chicalc<chicrit) #if True, Accept HO, if False, Reject HO
#Amount of alleles
#Dominant Alleles
A_dom = 50
#Recessive Alleles
A_rec = 50
n = A_dom + A_rec #Total Amount
masterlist = list('A'*A_dom) + list('a'*A_rec) #Generates the random population
##If
countt = 50
# withSubstitution(countt)
AcceptReject = list()
for i in range(25):
AcceptReject.append(chisquare(withSubstitution(countt)))
print(AcceptReject.count(True))
print(AcceptReject.count(False))
# print(chisquare([17,20,13]))