-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.Rhistory
More file actions
512 lines (512 loc) · 16.7 KB
/
Copy path.Rhistory
File metadata and controls
512 lines (512 loc) · 16.7 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# overdominance always results in 1 stable attractor at intermediate allele freq
} else if(w_genotypes["Aa"] >= w_genotypes["AA"]){
output <- list(outcome = "p intermediate")
}
# for h=0.2, it's tricky to draw the arrows bc the stable attractor is near 0.95
return(output)
}
outcome <- get_steady_state(w_gent, delta_p)
outcome
runApp()
runApp()
runApp()
delta_p
w_gent
which(near(delta_p$delta_p, 0, tol = 10^-3))
w_gent <- fitness_genotypes(sel_coef = 0.2, dom_coef = 0.3)
delta_p <- get_p_byp(w_gent)
which(near(delta_p$delta_p, 0, tol = 10^-3))
temp_list <- vector(mode='list', length=8)
h <- seq(from=0.3, to=1.0, by=0.1)
for(i in 1:8){
w_gent <- fitness_genotypes(sel_coef = 0.2, dom_coef = h[i])
temp_list[[i]] <- get_p_byp(w_gent)
}
h[8]
h[1]
for (i in 1:8){
print(which(near(temp_list[[i]]$delta_p, 0, tol = 10^-3)))
}
for (i in 1:8){
print(which(near(temp_list[[i]]$delta_p[5:980], 0, tol = 10^-3)))
}
w_gent <- fitness_genotypes(sel_coef = 0.2, dom_coef = 0.2)
delta_p <- get_p_byp(w_gent)
which(near(delta_p$delta_p, 0, tol = 10^-3))
which(near(delta_p$delta_p[10:1000], 0, tol = 10^-3))
delta_p$p[which(near(delta_p$delta_p[10:1000], 0, tol = 10^-3))]
delta_p$delta_p[which(near(delta_p$delta_p[10:1000], 0, tol = 10^-3))]
w_gent
delta_p$delta_p[990:1000]
runApp()
runApp()
outcome
w_gent
delta_p
min(delta_p$delta_p)
max(delta_p$delta_p)
runApp()
runApp()
runApp()
runApp()
runApp()
delta_p$delta_p
max(delta_p$delta_p)
max(delta_p$delta_p)/2
w_gent
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
w_gent
w_gent <- fitness_genotypes(sel_coef = 0.21, dom_coef = 0.8)
w_gent
delta_p <- get_p_byp(w_gent)
outcome <- get_steady_state(w_gent, delta_p)
outcome
# a function indicating the long-term steady state
get_steady_state <- function(w_genotypes, deltap.df){
# some presets for plotting the arrows
min_arrow_length = 0.07
arrow_vertical_whitespace = 0.1
# initialize output variable with NA values:
# depending on outcome, only some may be replaced with non-NA values
output <- list(outcome = NA, stable_pts = NA, unstable_pt = NA, arrow1 = NA, arrow2 = NA)
# underdominance always results in bistability
if(w_genotypes["Aa"] < w_genotypes["aa"]) {
# a hacky way to approximate where the unstable equilibrium point is:
unstable_pt <- mean(deltap.df$p[which(near(deltap.df$delta_p[12:990], 0, tol = 10^-3))])
# draw the arrows symmetrically about x=0 at halfway of the smallest max delta_p values
x_val = min(abs(min(deltap.df$delta_p)), abs(max(deltap.df$delta_p)))/2
# y values for bottom arrow (1): vertically in middle of the 2 equil pts
arrow1_length <- max(unstable_pt-2*arrow_vertical_whitespace, min_arrow_length)
# y values for top arrow (2): vertically in middle of the 2 equil pts
arrow2_length <- max(1-unstable_pt-2*arrow_vertical_whitespace, min_arrow_length)
output <- list(outcome = "bistable",
stable_pts = c(0, 1),
unstable_pt = unstable_pt,
arrow1 = c(x0 = -x_val, y0 = unstable_pt - arrow_vertical_whitespace,
x1 = -x_val, y1 = unstable_pt - arrow_vertical_whitespace - arrow1_length),
arrow2 = c(x0 = x_val, y0 = unstable_pt + arrow_vertical_whitespace,
x1 = x_val, y1 = unstable_pt + arrow_vertical_whitespace + arrow2_length)
)
# additivity / partial dominance always results in A going to fixation (1 stable attractor)
} else if(w_genotypes["Aa"] >= w_genotypes["aa"] &
w_genotypes["Aa"] < w_genotypes["AA"]) {
x_val <- 0.005 # this x-value looks nice
# increase the whitespace of the arrow
arrow_vertical_whitespace <- 3*arrow_vertical_whitespace
arrow1_length <- 1-2*arrow_vertical_whitespace
output <- list(outcome = "p=1",
stable_pts = 1,
arrow1 = c(x0 = x_val, y0 = arrow_vertical_whitespace,
x1 = x_val, y1 = arrow_vertical_whitespace + arrow1_length)
)
# overdominance always results in 1 stable attractor at intermediate allele freq
} else if(w_genotypes["Aa"] > w_genotypes["AA"]){
# a hacky way to approximate where the stable equilibrium point is:
stable_pt <- mean(deltap.df$p[which(near(deltap.df$delta_p[5:980], 0, tol = 10^-3))])
# draw the arrows symmetrically about x=0 at halfway of the smallest max delta_p values
x_val = min(abs(min(deltap.df$delta_p)), abs(max(deltap.df$delta_p)))/2
# y values for bottom arrow (1): vertically in middle of the 2 equil pts
arrow1_length <- max(stable_pt-2*arrow_vertical_whitespace, min_arrow_length)
# y values for top arrow (2): vertically in middle of the 2 equil pts
arrow2_length <- max(1-stable_pt-2*arrow_vertical_whitespace, min_arrow_length)
output <- list(outcome = "p intermediate",
stable_pts = stable_pt,
arrow1 = c(x0 = x_val, y0 = stable_pt - arrow_vertical_whitespace - arrow1_length,
x1 = x_val, y1 = stable_pt - arrow_vertical_whitespace)
)
}
return(output)
}
outcome <- get_steady_state(w_gent, delta_p)
outcome
plot_p_byp <- function(deltap.df, steady_state) {
# change the graphing settings
par(cex.lab = 2, # increase size of axis labels
cex.axis = 1.5, # increase size of tick mark labels
mar = c(4.2, 4.4, 0.7, 0.85)) # decrease the borders
# plot the points
plot(x = deltap.df$delta_p, y = deltap.df$p,
pch = 16, col = "black",
xlab = expression("Change in allele frequency (" * Delta * "p)"),
ylab = "Allele frequency", xlim = range(deltap.df$delta_p),
ylim = c(-0.01, 1.01), yaxs = "i")
# plot the arrows
if(steady_state$outcome == "bistable"){
arrows(x0 = steady_state$arrow1["x0"],
y0 = steady_state$arrow1["y0"],
x1 = steady_state$arrow1["x1"],
y1 = steady_state$arrow1["y1"])
arrows(x0 = steady_state$arrow2["x0"],
y0 = steady_state$arrow2["y0"],
x1 = steady_state$arrow2["x1"],
y1 = steady_state$arrow2["y1"])
} else if(steady_state$outcome == "p=1"){
arrows(x0 = steady_state$arrow1["x0"],
y0 = steady_state$arrow1["y0"],
x1 = steady_state$arrow1["x1"],
y1 = steady_state$arrow1["y1"])
} else if(steady_state$outcome == "p intermediate"){
arrows(x0 = steady_state$arrow1["x0"],
y0 = steady_state$arrow1["y0"],
x1 = steady_state$arrow1["x1"],
y1 = steady_state$arrow1["y1"])
arrows(x0 = steady_state$arrow2["x0"],
y0 = steady_state$arrow2["y0"],
x1 = steady_state$arrow2["x1"],
y1 = steady_state$arrow2["y1"])
}
# Add a dotted vertical line at x = 0
abline(v = 0, lty = 3, lwd = 0.6, col = "black")
}
plot_p_byp(delta_p, outcome)
runApp()
runApp()
runApp()
# a function indicating the long-term steady state
get_steady_state <- function(w_genotypes, deltap.df){
# some presets for plotting the arrows
min_arrow_length = 0.07
arrow_vertical_whitespace = 0.1
# initialize output variable with NA values:
# depending on outcome, only some may be replaced with non-NA values
output <- list(outcome = NA, stable_pts = NA, unstable_pt = NA, arrow1 = NA, arrow2 = NA)
# underdominance always results in bistability
if(w_genotypes["Aa"] < w_genotypes["aa"]) {
# a hacky way to approximate where the unstable equilibrium point is:
unstable_pt <- mean(deltap.df$p[which(near(deltap.df$delta_p[12:990], 0, tol = 10^-3))])
# draw the arrows symmetrically about x=0 at halfway of the smallest max delta_p values
x_val = min(abs(min(deltap.df$delta_p)), abs(max(deltap.df$delta_p)))/2
# y values for bottom arrow (1): vertically in middle of the 2 equil pts
arrow1_length <- max(unstable_pt-2*arrow_vertical_whitespace, min_arrow_length)
# y values for top arrow (2): vertically in middle of the 2 equil pts
arrow2_length <- max(1-unstable_pt-2*arrow_vertical_whitespace, min_arrow_length)
output <- list(outcome = "bistable",
stable_pts = c(0, 1),
unstable_pt = unstable_pt,
arrow1 = c(x0 = -x_val, y0 = unstable_pt - arrow_vertical_whitespace,
x1 = -x_val, y1 = unstable_pt - arrow_vertical_whitespace - arrow1_length),
arrow2 = c(x0 = x_val, y0 = unstable_pt + arrow_vertical_whitespace,
x1 = x_val, y1 = unstable_pt + arrow_vertical_whitespace + arrow2_length)
)
# additivity / partial dominance always results in A going to fixation (1 stable attractor)
} else if(w_genotypes["Aa"] >= w_genotypes["aa"] &
w_genotypes["Aa"] < w_genotypes["AA"]) {
x_val <- 0.005 # this x-value looks nice
# increase the whitespace of the arrow
arrow_vertical_whitespace <- 3*arrow_vertical_whitespace
arrow1_length <- 1-2*arrow_vertical_whitespace
output <- list(outcome = "p=1",
stable_pts = 1,
arrow1 = c(x0 = x_val, y0 = arrow_vertical_whitespace,
x1 = x_val, y1 = arrow_vertical_whitespace + arrow1_length)
)
# overdominance always results in 1 stable attractor at intermediate allele freq
} else if(w_genotypes["Aa"] > w_genotypes["AA"]){
# a hacky way to approximate where the stable equilibrium point is:
stable_pt <- mean(deltap.df$p[which(near(deltap.df$delta_p[5:980], 0, tol = 10^-3))])
# draw the arrows symmetrically about x=0 at one third of the smallest max delta_p values
x_val = min(abs(min(deltap.df$delta_p)), abs(max(deltap.df$delta_p)))/3
# y values for bottom arrow (1): vertically in middle of the 2 equil pts
arrow1_length <- max(stable_pt-2*arrow_vertical_whitespace, min_arrow_length)
# y values for top arrow (2): vertically in middle of the 2 equil pts
arrow2_length <- max(1-stable_pt-2*arrow_vertical_whitespace, min_arrow_length)
output <- list(outcome = "p intermediate",
stable_pts = stable_pt,
arrow1 = c(x0 = x_val, y0 = stable_pt - arrow_vertical_whitespace - arrow1_length,
x1 = x_val, y1 = stable_pt - arrow_vertical_whitespace),
arrow2 = c(x0 = -x_val, y0 = stable_pt + arrow_vertical_whitespace + arrow2_length,
x1 = -x_val, y1 = stable_pt + arrow_vertical_whitespace)
)
}
return(output)
}
outcome <- get_steady_state(w_gent, delta_p)
outcome
runApp()
?arrows
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
exp(-1)
exp(-10)
exp(-100)
runApp()
runApp()
runApp()
runApp()
?withMathJax
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
1.25/2
(1.25+1)/2
runApp('C:/Users/hermina/Downloads/for_shiny_server/for_shiny_server/underdom')
version(shiny)
packageVersion(shiny)
packageVersion("shiny")
library(shiny)
packageVersion("shiny")
packageVersion("ggplot2")
ip <- as.data.frame(installed.packages()[,c(1,3:4)])
rownames(ip) <- NULL
ip <- ip[is.na(ip$Priority),1:2,drop=FALSE]
print(ip, row.names=FALSE)
# load the environment
load("./app.R")
# Chunk 1: setup
# load the environment
source("./app.R")
# Chunk 2: the_app
# user interface with fluidPage
ui <- fluidPage(
# title
titlePanel("Diploid model of selection with underdominance"),
# top row is split into 3 columns of equal size (with 3 sliders)
fluidRow(
column(4,
sliderInput(inputId = "h",
label = "Underdominance coefficient (h)",
value = 0.2,
min = -1, max = 0.5,
step = 0.1)
),
column(4,
sliderInput(inputId = "p_init",
label = "Initial allele frequency of A (p)",
value = 0.32,
min = 0.02, max = 0.98,
step = 0.02)
),
column(4,
sliderInput(inputId = "gen",
label = "Number of generations",
value = 50,
min = 5, max = 950,
step = 5)
)
),
# middle row is 2 columns of equal size
fluidRow(
# interactively display the model parameters
column(6,
uiOutput("dynamEq"),
plotOutput("fitLand")),
# display the genotype frequencies over time
column(6,
plotOutput("genoTime")
)
),
# bottom row is 2 columns of equal size
fluidRow(
# display delta p by p
column(6,
plotOutput("delta_p")
),
# display the allele frequency over time
column(6,
plotOutput("alleleTime")
)
)
)
# Server logic
server <- function(input, output) {
# fix the selection coefficient
# be careful to choose a value that does *NOT* result in neutral system
s <- 0.305
######################
# expressions
######################
# a reactive expression to get the fitness of the genotypes
w_gent <- reactive(fitness_genotypes(sel_coef = s,
dom_coef = input$h))
# a reactive expression to get delta p as a function of allele frequecy (p)
delta_p <- reactive(get_p_byp(w_gent()))
# a reactive expression to get the long-term model outcome
outcome <- reactive(get_steady_state(w_genotypes = w_gent(), deltap.df = delta_p()))
# a reactive expression to simulate evolution of the system over time
sims <- reactive(sim_forward_time(num_gens = input$gen,
freq_init = input$p_init,
w_gent()))
######################
# outputs
######################
output$dynamEq <- renderUI({
temp_genotypes <- w_gent()
w_AA <- temp_genotypes["AA"]
w_Aa <- temp_genotypes["Aa"]
w_aa <- temp_genotypes["aa"]
withMathJax(sprintf("$${\\tiny p_{t+1} = \\frac{p_t^2 \\cdot (1+s) + p_t(1-p_t) \\cdot (1+s+h)}{\\overline{w}} = \\frac{p_t^2 \\cdot %.01f + p_t(1-p_t) \\cdot %.01f}{p_t^2 \\cdot %.01f + 2p_t(1-p_t) \\cdot %.01f + (1-p_t)^2 \\cdot %.01f}}$$",
w_AA, w_Aa, w_AA, w_Aa, w_aa))
})
output$fitLand <- renderPlot({plot_schematic(w_gent())})
output$genoTime <- renderPlot({plot_t_genotypes(sims())})
output$delta_p <- renderPlot({plot_p_byp(delta_p(), outcome())})
output$alleleTime <- renderPlot({plot_t_allele(sims())})
}
# Complete app with UI and server components
shinyApp(ui, server)
seq(from=-1, to=0.5, by=0.3)
plot_schematic(fitness_genotypes(sel_coef = 0.305, dom_coef = 0.15))
plot_schematic <- function(w_genotypes) {
# change the graphing settings
par(cex.lab = 2, # increase size of axis labels
cex.axis = 1.5, # increase size of tick mark labels
mar = c(4.2, 4.4, 1, 0.1)) # decrease the borders
# make the barplot in graphics (i.e., base R)
barplot(
w_genotypes,
col = colours_genotypes[names(w_genotypes)],
border = "black",
ylim = c(0, 1.6),
ylab = "Fitness",
xlab = "Genotype"
)
# add a box around the whole plot
box()
}
plot_schematic(fitness_genotypes(sel_coef = 0.305, dom_coef = 0.15))
plot_schematic(fitness_genotypes(sel_coef = 0.305, dom_coef = -0.5))
plot_schematic(fitness_genotypes(sel_coef = 0.305, dom_coef = 0.5))
plot_schematic(fitness_genotypes(sel_coef = 0.305, dom_coef = 0.55))
plot_schematic(fitness_genotypes(sel_coef = 0.305, dom_coef = 0.3))
runApp('temp_example_app.R')
runApp('temp_example_app.R')
runApp('temp_example_app.R')
runApp('temp_example_app.R')
runApp('temp_example_app.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
colours_genotypes
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
?bs_theme
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
?layout_columns
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
?par
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
1+1.5
2.5/3
runApp('stats_students_trial.R')
1.5/3
0.75*2
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('stats_students_trial.R')
runApp('temp_example_app.R')
runApp('temp_example_app.R')
runApp('temp_example_app.R')
runApp('temp_example_app.R')
runApp('temp_example_app.R')
runApp('temp_example_app.R')
runApp('temp_example_app.R')
runApp('temp_example_app.R')
runApp('temp_example_app.R')
runApp('temp_example_app.R')
?page_navbar
runApp('temp_example_app.R')
runApp('temp_example_app.R')
runApp('temp_example_app.R')
update.packages("bslib")
update.packages("shiny")