Professor Z. Wu, BCB 504
This is an R Markdown document. Markdown is a simple formatting syntax for authoring web pages.
This is an attmept to answer Dr Wu's homework # 2 question.
N = 30 # Size of each line
lineN = 100 # Number of lines
Genes0 = array(NA, dim = c(lineN, 2 * N))
# Matrix to store alleles and genotypes (two adjacent alleles form a
# genotype). Each row represent a line.
# Initialize Genes for population 0
for (i in 1:lineN) {
Genes0[i, ] = sample(x = c(0:1), size = 10, replace = T, prob = c(0.3, 0.7))
# Randomly form genotypes by sample function with Pr=(0.3,0.7)
}
generationN = 300
ParentGenes = Genes0
AlleleFreq = array(NA, dim = c(generationN, 2))
GeneFreq = array(NA, dim = c(generationN, 3))
for (g in 1:generationN) {
ChildGenes = array(NA, dim = c(lineN, 2 * N))
for (i in 1:lineN) {
ChildGenes[i, ] = sample(ParentGenes[i, ], replace = T) # Randam sampling with replacement <=> simple inbreeding
}
ParentGenes = ChildGenes
AlleleFreq[g, ] = table(ChildGenes)/(lineN * 2 * N)
# Convert allele to genotypes: AA <=> 2 Aa / aA <=> 1 aa <=> 0.
Genotypes = array(NA, dim = c(lineN, N))
for (j in 1:N) {
Genotypes[, j] = ChildGenes[, 2 * j - 1] + ChildGenes[, 2 * j]
}
GeneFreq[g, ] = table(Genotypes)/(lineN * N) # Genotype frequencies.
}
## Error: number of items to replace is not a multiple of replacement length
par(mfrow = c(2, 1))
names = c("AA", "Aa", "aa")
colors <- rainbow(3)
linetype <- c(1:3)
plot(c(1, generationN), 0:1, type = "n", xlab = "Generations", ylab = "Genome Frequency")
title("Genotype frequencies of 100 inbreeding populations")
for (i in 1:3) {
lines(GeneFreq[, i], type = "l", lwd = 2, col = colors[i])
}
legend(c(0, 50), c(0.55, 1), names, cex = 0.8, col = colors, title = "Genotype",
lty = 1)
names = c("A", "a")
colors <- rainbow(2)
linetype <- c(1:2)
plot(c(1, generationN), 0:1, type = "n", xlab = "Generations", ylab = "Allele Frequency")
title("Allele frequencies of 100 inbreeding populations")
for (i in 1:2) {
lines(AlleleFreq[, i], type = "l", lwd = 2, col = colors[i])
}
legend(c(0, 50), c(0.6, 1), names, cex = 0.8, col = colors, title = "Genotype",
lty = 1, )