-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
283 lines (275 loc) · 17 KB
/
main.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
from nicegui import ui
import numpy as np
from tasker import Tasker
tasker = Tasker()
simulations = 1000
# N = 100
# mutation_rate = 0.01
# migration_rate = 0.1
# generations = 100
intro = ui.markdown('''
# **GPOP 05/12/2024** 🗣️🗣️\n
*/ The population is formed by N haploid individuals, each one carrying one allele for a given locus.\n
*/ The population evolves with constant population size and discrete non-overlapping generations.\n
*/ Each individual in generation t+1 is a copy of a randomly selected individual in generation t.\n
''')
# N_input = ''
# mutation_rate_input = ''
# migration_rate_input = ''
# generations_input = ''
with ui.row() as input_buttons:
N = ui.number(label='input N', value=100, placeholder='wanna change value of N?',)#.bind_value_to(globals(), N)
mutation_rate = ui.number(label='input mutation_rate', value=0.01, placeholder='wanna change value of mutation rate?')
migration_rate = ui.number(label='input migration_rate', value=0.1, placeholder='wanna change value of migration rate?')
generations = ui.number(label='input generations', value=100, placeholder='wanna change value of generations?')
def task1(p, N):
with ui.row() as task1_row:
with ui.card().classes('w-[1300px] mx-auto'):
ui.markdown('## Genetic Drift')
with ui.matplotlib(figsize=(12, 6)).figure as fig:
ax = fig.gca()
for i in range(5):
gen_drift = tasker.task_1(p=p, N=N)
ax.plot(gen_drift, label=f"pop {i}")
ax.set_ylim(ymin=0, ymax=1)
ax.set_xlim(xmin=0, xmax=2*N)
ax.set_xlabel('Generations')
ax.set_ylabel('p: frequency of allele A')
ax.legend()
with ui.card().classes('w-[500px] mx-auto'):
ui.markdown('##### Cheatsheet')
ui.button('mr. clean', on_click=lambda: (task1_row.delete()))
fixations = 0
times = []
for i in range(simulations):
gen_drift = tasker.task_1(p=p, N=N)
if gen_drift[-1] == 1 or 0:
fixations += 1
times.append(len(gen_drift) - 1)
ui.markdown(f'The fixation probability of allele A with pA=**{p}** over {simulations} simulations:\
**{round(fixations/simulations, 2)}**')
ui.markdown("""Fixation probability of allele A in pop = initial allele frequency pA.
For pA = 0.2, fixation probability of allele A is in range(0.12, 0.17)""")
ui.markdown(f'The expected fixation time (number of generations): {round(np.mean(times), 2)}')
ui.markdown(r'''
Harmonic mean of actual pop size:
$$Ne = (\frac{1}{t}\sum_{i=0}^{t-1} \frac{1}{Ni})^-1$$
''', extras=['latex'])
ui.markdown('Theoretically, with N = 100 and generations = 200, Ne ~ 100')
ui.markdown('Genetic drift occurs in all populations of non-infinite size, but its effects are strongest in small populations')
def task2(N):
with ui.row() as task2_row:
with ui.card().classes('w-[1300px] mx-auto'):
ui.markdown('## Coalescent model')
with ui.matplotlib(figsize=(12, 6)).figure as fig:
ax = fig.gca()
generation, freq = tasker.task_2_identicalByDescent(N = N)
ax.plot(freq, label='frequency of an allele from 1/N to 1')
ax.set_ylim(ymin=0, ymax=1)
ax.set_xlim(xmin=0, xmax=2*N)
ax.set_xlabel('Generations')
ax.set_ylabel('Frequency of an allele')
ax.legend()
ax.set_title('All alleles are identical by descent')
with ui.matplotlib(figsize=(12, 6)).figure as fig2:
sample_size = [2, 3, 4, 5]
E_Tn = []
generations_arr = []
for n in sample_size:
E_Tn.append(2 * N / (n * (n - 1)))
generationsForEachN = []
for i in range(simulations):
generationsForEachN.append(tasker.task_2_firstCoalescentEvent(n=n, N=N))
generations_arr.append(np.mean(generationsForEachN))
ax = fig2.gca()
ax.plot(sample_size, E_Tn, label='Theoretical', marker='o', color='green')
ax.plot(sample_size, generations_arr, label='Empirical', marker='x', color='red')
ax.set_yticks(np.arange(0, 110, 5))
ax.set_xlabel("Sample size n = [2, 3, 4, 5]")
ax.set_ylabel("Generations to First Coalescent Event")
ax.set_title("Empirical / Theoretical")
ax.legend()
with ui.matplotlib(figsize=(12, 6)).figure as fig2:
sample_size = [2, 3, 4, 5]
E_Tn = []
generations_arr = []
for n in sample_size:
E_Tn.append(2 * N / (n * (n - 1)))
generationsForEachN = []
for i in range(simulations):
generationsForEachN.append(tasker.task_2_coalescentModel(n=n, N=N))
generations_arr.append(np.mean(generationsForEachN))
ax = fig2.gca()
ax.plot(sample_size, E_Tn, label='Theoretical', marker='o', color='green')
ax.plot(sample_size, generations_arr, label='Empirical', marker='x', color='red')
ax.set_xlabel("Sample size n = [2, 3, 4, 5]")
ax.set_ylabel("Generations of Coalescent Model")
ax.set_title("Empirical / Theoretical")
ax.legend()
with ui.card().classes('w-[500px] mx-auto'):
ui.markdown('##### Cheatsheet')
ui.button('mr. clean', on_click=lambda: (task2_row.delete()))
ui.markdown('Tracking a population of N = 100 individuals until all alleles are identical by descent\
is like tracking genetic drift.')
ui.markdown('A population has N alleles with first frequency of each allele = 1/N. \
To count generations until frequency of an allele reach 1. \
That means this population has 1 allele left.')
ui.markdown(r'''
Expected generations to the first coalescent event:
$$E\{Tn\} = \frac{2N}{n(n-1)}$$
$$E\{T2\} = 100 $$
$$E\{T3\} = 33.34$$
$$E\{T4\} = 16.67$$
$$E\{T5\} = 10 $$
''', extras=['latex'])
ui.markdown('To the first coalescent event, number of generations in empirical experiment and theory are the same.\
But in case of running the model until the number of lineages = 1, number of generations in empirical experiment is far away from theory.\
because the assumption n ≪ N is violated.')
def task3(N, mutation_rate, generations):
# print(N, mutation_rate, generations)
with ui.row() as task3_row:
with ui.card().classes('w-[1300px] mx-auto'):
ui.markdown('## Mutation and drift')
with ui.matplotlib(figsize=(12, 6)).figure as fig:
ax = fig.gca()
equilibrium = 1 / (1+2*N*mutation_rate)
fixation = tasker.task_3(N = N, mutation_rate = mutation_rate, generations = generations)
# print(fixation)
ax.plot(fixation, label='fixation index')
ax.set_ylim(ymin=0, ymax=1)
ax.hlines(y = equilibrium, xmin=0, xmax=generations, color='red', linestyle='--', label='Equilibrium')
ax.hlines(y = np.mean(fixation), xmin=0, xmax=generations, color='green', linestyle='--', label='mean value of fixation index')
ax.set_xlim(xmin=0, xmax=generations)
ax.set_xlabel('Generations')
ax.set_ylabel('Fixation index')
ax.legend()
ax.set_title('The fixation index (probability that two randomly chosen alleles are identical) over time')
with ui.card().classes('w-[500px] mx-auto'):
ui.markdown('##### Cheatsheet')
ui.button('mr. clean', on_click=lambda: (task3_row.delete()))
ui.markdown(r'''
The fixation index: the value of $\mathcal{G}$ after one round of random mating and mutation
$$\mathcal{G'} = (1-\mu)^2[\frac{1}{2} + (1 - \frac{1}{2})\mathcal{G}]$$
With G and $\mathcal{G}$ are "almost the same" + the total frequency of homozygotes is given by:
$$G = \sum_{i=1}^{k}p_i^2$$
At equilibrium, the probability that 2 alleles different by origin are identical by state is given by:
$$\hat{\mathcal{G}} = \frac{1}{1+2N\mu}$$
''', extras=['latex'])
ui.markdown(f'For this example, the equilibrium = {round(equilibrium, 2)} which is smaller the mean value of fixation index = {round(np.mean(fixation), 2)}')
ui.markdown(r'''Genetic drift increases fixation index $\mathcal{G}$ while **mutation decreases $\mathcal{G}$.**
''', extras=['latex'])
ui.markdown(f'Run the experiment for several times until 1000 generations. \
Most of the plots have peaks that reach the equilibrium and then return, with a few times that can surpass the equilibrium. \
Anyway, equilibrium =/= static -> new mutations/alleles appear, drift happens.')
def task4(N, p, s, generations):
with ui.row() as task4_row:
with ui.card().classes('w-[1300px] mx-auto'):
ui.markdown('## Selection')
with ui.matplotlib(figsize=(12, 6)).figure as fig:
ax = fig.gca()
freq = tasker.task_4(N = N, p = p, s = s, generations= generations)
ax.plot(freq, label='Frequency of allele B')
ax.set_ylim(ymin=0, ymax=1.1)
ax.set_xlim(xmin=0, xmax=generations)
ax.set_xlabel('Generations')
ax.set_ylabel('Frequency of allele B')
ax.legend()
ax.set_title('The fraction of allele B over time.')
with ui.card().classes('w-[500px] mx-auto'):
ui.markdown('##### Cheatsheet')
ui.button('mr. clean', on_click=lambda: (task4_row.delete()))
ui.markdown('fitness-increasing alleles becoming more common in the population')
ui.markdown(f'pB = {p}')
ui.markdown('fitness ~ number of offsprings, higher fitness -> growing frequency in population\
=> using fitness values to set selection probability')
def task5(N, generations):
with ui.row() as task5_row:
with ui.card().classes('w-[1300px] mx-auto'):
ui.markdown('## Clonal interference')
for N in [40, 100, 300, 500, 1000, 2000]:
with ui.matplotlib(figsize=(12, 6)).figure as fig:
ax = fig.gca()
freq_A, freq_B, freq_C = tasker.task_5(N = N, generations= generations)
ax.plot(freq_A, label='Frequency of allele A')
ax.plot(freq_B, label='Frequency of allele B')
ax.plot(freq_C, label='Frequency of allele C')
ax.set_ylim(ymin=-0.1, ymax=1.1)
ax.set_xlim(xmin=0, xmax=generations)
ax.set_xlabel(f'{generations} Generations')
ax.set_ylabel('Frequencies of alleles')
ax.legend()
ax.set_title(f'The fraction of alleles over time + N = {N}')
with ui.card().classes('w-[500px] mx-auto'):
ui.markdown('##### Cheatsheet')
ui.button('mr. clean', on_click=lambda: (task5_row.delete()))
ui.markdown(r'''
According to the lecture:
$$\frac{q(t)}{p(t)} = \frac{\lambda_B}{\lambda_a}.\frac{q(0)}{(p(0)}, \
assume that: \lambda_B > \lambda_A$$
''', extras=['latex'])
ui.markdown('This equation means frequency of allele which has higher fitness grows exponentially when compared to\
frequency of small fitness allele.')
ui.markdown('clonal interference occurs in an asexual lineage ("clone") with a beneficial mutation. \
This mutation would be likely to get fixed if it occurred alone, but it may fail to be fixed, or even be lost, \
if another beneficial-mutation lineage arises in the same population; the multiple clones interfere with each other')
ui.markdown('This typically leads to the loss of one of them,\
the fate of an advantageous mutation can be determined by other mutations present in the same population')
def task6(p, N, generations):
with ui.row() as task6_row:
with ui.card().classes('w-[1300px] mx-auto'):
ui.markdown('## Population structure')
with ui.matplotlib(figsize=(12, 6)).figure as fig:
ax = fig.gca()
freqs = tasker.task_6(p = p, N = N, generations= generations)
for i in range(len(freqs)):
ax.plot(freqs[i], label=f'sub-population {i}')
ax.set_ylim(ymin=-0.1, ymax=1.1)
ax.set_xlim(xmin=0, xmax=generations)
ax.set_xlabel(f'{generations} Generations')
ax.set_ylabel('Frequencies of allele A with p = 0.2')
ax.legend()
ax.set_title(f'The evolution of the population with subpopulations')
with ui.card().classes('w-[500px] mx-auto'):
ui.markdown('##### Cheatsheet')
ui.button('mr. clean', on_click=lambda: (task6_row.delete()))
ui.markdown('For example, a barrier like a river can separate two groups of the same species and make it difficult for potential mates to cross;\
if a mutation occurs, over many generations it can spread and become common in one subpopulation while being completely absent in the other')
def task7(p, N, generations, migration_rate):
with ui.row() as task7_row:
with ui.card().classes('w-[1300px] mx-auto'):
ui.markdown('## Migration')
with ui.matplotlib(figsize=(12, 6)).figure as fig:
ax = fig.gca()
freqs = tasker.task_7(p = p, N = N, generations= generations, migration_rate= migration_rate)
for i in range(len(freqs)):
ax.plot(freqs[i], label=f'sub-population {i}')
ax.set_ylim(ymin=-0.1, ymax=1.1)
ax.set_xlim(xmin=0, xmax=generations)
ax.set_xlabel(f'{generations} Generations')
ax.set_ylabel(f'Frequencies of allele A with p = {p}')
ax.legend()
ax.set_title(f'The evolution of the population with subpopulations and migration')
with ui.card().classes('w-[500px] mx-auto'):
ui.markdown('##### Cheatsheet')
ui.button('mr. clean', on_click=lambda: (task7_row.delete()))
ui.markdown('In population genetics, gene flow (also known as migration and allele flow) \
is the transfer of genetic material from one population to another')
ui.markdown('Migration changes the distribution of genetic diversity among populations, by modifying allele frequencies')
ui.markdown(r'''
In this case, for small migration rate m << 1 and according to the equilibrium:
$$F_{eq} = \frac{1}{1+2N_{e}m}$$
which means $F_{eq}$ << 1 $\rightarrow{}$ little differentiation between sub-populations, \
little fixation within sub-population.
''', extras=['latex'])
with ui.row():
ui.button('Task 1', on_click=lambda: (task1(p=0.2, N=int(N.value))))
ui.button('Task 2', on_click=lambda: (task2(N=int(N.value))))
ui.button('Task 3', on_click=lambda: (task3(N = int(N.value), mutation_rate= mutation_rate.value, generations = int(generations.value)*10)))
ui.button('Task 4', on_click=lambda: (task4(N=int(N.value), p=0.5, s=0.05, generations=int(generations.value))))
ui.button('Task 5', on_click=lambda: (task5(N=int(N.value), generations=int(generations.value))))
ui.button('Task 6', on_click=lambda: (task6(p=0.2, N=int(N.value)*10, generations=int(generations.value)*2)))
ui.button('Task 7', on_click=lambda: (task7(p=0.5, N=int(N.value)*10, generations=int(generations.value)*2, migration_rate=migration_rate.value)))
url = 'https://github.com/khnam-ng/gpop'
ui.button('Open GitHub', on_click=lambda: ui.navigate.to(url, new_tab=True))
ui.dark_mode().enable()
ui.run(title='give me 20', favicon='🗣️')