Skip to content

Instantly share code, notes, and snippets.

@lundquist-ecology-lab
Created January 31, 2023 17:50
Show Gist options
  • Save lundquist-ecology-lab/8b88ca66f5051d490a39579bc48332f7 to your computer and use it in GitHub Desktop.
Save lundquist-ecology-lab/8b88ca66f5051d490a39579bc48332f7 to your computer and use it in GitHub Desktop.
Simulation of population under HWE
## Hardy Weinberg script (1)
## Simulating changes in genotype frequencies over time
## under Hardy-Weinberg Equilibrium
## Instruction: Press "-->Source"
##################### CODE: Ignore all of this below ###########################
BB.f <- NA
bb.f <- NA
Bb.f <- NA
time <- NA
parental.alleles <- c(rep("b", times = 100), rep("B", times = 100))
numbers <- c(sprintf('%0.3d', 1:100), sprintf('%0.3d',1:100))
x <- paste(parental.alleles, numbers, sep = "")
selected <- NA
for (i in 1:100) {
s <- sample(x, replace = FALSE, size = 2)
a <- paste(s, collapse = "")
selected[i] <- paste(c(substring(a, 1, 1), (substring(a, 5, 5))), collapse = "")
}
BB.f <- round(length(selected[! selected %in% c('bb', 'Bb', "bB")]) / length(selected), 2)
bb.f <- round(length(selected[! selected %in% c('BB', 'Bb', "bB")]) / length(selected), 2)
Bb.f <- round(length(selected[! selected %in% c('bb', 'BB')]) / length(selected), 2)
plot(0:10, type = 'n', axes = FALSE, ann = FALSE)
text(x = 5, y = 6, # Text with different color & size
expression("BB ="),
col = "blue",
cex = 2)
text(x = 7, y = 6, # Text with different color & size
BB.f,
col = "blue",
cex = 2)
text(x = 5, y = 5, # Text with different color & size
expression("Bb ="),
col = "green",
cex = 2)
text(x = 7, y = 5, # Text with different color & size
Bb.f,
col = "green",
cex = 2)
text(x = 5, y = 4, # Text with different color & size
expression("bb ="),
col = "red",
cex = 2)
text(x = 7, y = 4, # Text with different color & size
bb.f,
col = "red",
cex = 2)
text(x = 5, y = 2, # Text with different color & size
expression("Total ="),
col = "black",
cex = 2)
text(x = 7, y = 2, # Text with different color & size
sum(c(bb.f, Bb.f, BB.f)),
col = "black",
cex = 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment