-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPedigree_Accumulation_Analysis.R
172 lines (152 loc) · 6.22 KB
/
Pedigree_Accumulation_Analysis.R
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
# Load packages
library(tidyverse)
library(readxl)
library(adegenet)
library(poppr)
library(devtools)
library(vegan)
library(patchwork)
#####################################################################
#### Run pedigree accumulation analysis using COLONY output data ####
#####################################################################
#### Read in data ####
Data_2111 <- read.genepop("X:/2111_F1F2D_BKT/2111analysis/Thometz_scripts/2111_genepop.gen",
ncode = 3L,
quiet = FALSE)
Samples_2111 <- read_delim("X:/2111_F1F2D_BKT/2111analysis/Thometz_scripts/Samples_2111.csv") %>%
filter(SampleID %in% rownames(Data_2111@tab)) %>%
arrange(match(SampleID, rownames(Data_2111@tab)))
#### Read in BestConfig files #### (manually cleaned them up in notepad++)
config_2019 <- read_delim("X:/2111_F1F2D_BKT/2111analysis/Thometz_scripts/Analyses/Colony_3_runs/Output_files/2020/cleaned_up.BestConfig_Ordered") %>%
select(-ClusterIndex)
config_2020 <- read_delim("X:/2111_F1F2D_BKT/2111analysis/Thometz_scripts/Analyses/Colony_3_runs/Output_files/2021/cleaned_up.BestConfig_Ordered") %>%
select(-ClusterIndex)
config_2021 <- read_delim("X:/2111_F1F2D_BKT/2111analysis/Thometz_scripts/Analyses/Colony_3_runs/Output_files/2022/cleaned_up.BestConfig_Ordered") %>%
select(-ClusterIndex)
#### Function to calculate Ns (number of successfully breeding adults) ####
Ns_calc <- function(family){
require(vegan)
family$FatherID <- paste0("Dad", family$FatherID)
family$MotherID <- paste0("Mom", family$MotherID)
#making matrix for dads
dads <- data.frame(matrix(0,nrow = length(family$OffspringID), ncol = length(unique(family$FatherID))))
colnames(dads) <- unique(family$FatherID)
rownames(dads) <- family$OffspringID
#making matrix for moms
moms <- data.frame(matrix(0, nrow = length(family$OffspringID), ncol = length(unique(family$MotherID))))
colnames(moms) <- unique(family$MotherID)
rownames(moms) <- family$OffspringID
# Loop to fill in matrix with parents
for (i in 1:length(family$OffspringID)) {
off <- family[i,]
dadn <- which(off$FatherID == colnames(dads))
dads[i, dadn] <- 1
}
for (i in 1:length(family$OffspringID)) {
off <- family[i,]
momn <- which(off$MotherID == colnames(moms))
moms[i, momn] <- 1
}
parents <- cbind(moms, dads)
Ns <- as.matrix(ncol(parents))
colnames(Ns) <- "Ns"
ns_points <- specaccum(parents, method = "random")
asymp <- specpool(parents)
output <- list(ns_points, asymp, Ns)
output
}
#### Run config files through the function ####
# 2020 Offspring
Ns_2019 <- Ns_calc(family = config_2019)
Ns_2019_data <- tibble(y = Ns_2019[[1]]$richness,
y_sd = Ns_2019[[1]]$sd,
x = Ns_2019[[1]]$sites)
# 2021 Offspring
Ns_2020 <- Ns_calc(family = config_2020)
Ns_2020_data <- tibble(y = Ns_2020[[1]]$richness,
y_sd = Ns_2020[[1]]$sd,
x = Ns_2020[[1]]$sites)
# 2022 Offspring
Ns_2021 <- Ns_calc(family = config_2021)
Ns_2021_data <- tibble(y = Ns_2021[[1]]$richness,
y_sd = Ns_2021[[1]]$sd,
x = Ns_2021[[1]]$sites)
#### Plot for each year ####
# 2020 Offspring
Plot_2019 <- Ns_2019_data %>%
ggplot(aes(x = x, y = y)) +
geom_ribbon(aes(ymin = y-y_sd, ymax = y + y_sd),
alpha = 0.95,
fill = "lightgrey") +
geom_point(size = 0.5) +
geom_hline(yintercept = Ns_2019[[2]]$chao,
linetype = "dashed") +
geom_text(data = data.frame(x = 0, y = Ns_2019[[2]]$chao),
aes(x, y),
label = paste("Chao estimate =", round(Ns_2019[[2]]$chao, digits = 2)),
hjust = -0.25,
vjust = 1.5) +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(limits = c(0, 80), expand = c(0, 0)) +
labs(x = NULL,
y = bquote(N[S]),
title = bquote("(A) " ~ 2019 ~ (N[offspring] ~ "= 145"))) +
theme_classic()
# 2021 Offspring
Plot_2020 <- Ns_2020_data %>%
ggplot(aes(x = x, y = y)) +
geom_ribbon(aes(ymin = y-y_sd, ymax = y + y_sd),
alpha = 0.95,
fill = "lightgrey") +
geom_point(size = 0.5) +
geom_hline(yintercept = Ns_2020[[2]]$chao,
linetype = "dashed") +
geom_text(data = data.frame(x = 0, y = Ns_2020[[2]]$chao),
aes(x, y),
label = paste("Chao estimate =", round(Ns_2020[[2]]$chao, digits = 2)),
hjust = -0.25,
vjust = 1.5) +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(limits = c(0, 80), expand = c(0, 0)) +
labs(x = bquote(N["offspring sampled"]),
y = NULL,
title = bquote("(B) " ~ 2020 ~ (N[offspring] ~ "= 72"))) +
theme_classic() +
theme(axis.text.y = element_blank())
# 2022 Offspring
Plot_2021 <- Ns_2021_data %>%
ggplot(aes(x = x, y = y)) +
geom_ribbon(aes(ymin = y-y_sd, ymax = y + y_sd),
alpha = 0.95,
fill = "lightgrey") +
geom_point(size = 0.5) +
geom_hline(yintercept = Ns_2021[[2]]$chao,
linetype = "dashed") +
geom_text(data = data.frame(x = 0, y = Ns_2021[[2]]$chao),
aes(x, y),
label = paste("Chao estimate =", round(Ns_2021[[2]]$chao, digits = 2)),
hjust = -0.25,
vjust = 1.5) +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(limits = c(0, 80), expand = c(0, 0)) +
labs(x = NULL,
y = NULL,
title = bquote("(C) " ~ 2021 ~ (N[offspring] ~ "= 41"))) +
theme_classic() +
theme(axis.text.y = element_blank())
# Combine the plots
all_plots <- Plot_2019 + Plot_2020 + Plot_2021
ggsave(filename = "PAA_plots_all.pdf",
plot = all_plots,
device = "pdf",
path = "X:/2111_F1F2D_BKT/2111analysis/Thometz_scripts/Polished_plots_figures/Pedigree_accum_3runs",
height = 4,
width = 9,
units = "in")
ggsave(filename = "PAA_plots_all.png",
plot = all_plots,
device = "png",
path = "X:/2111_F1F2D_BKT/2111analysis/Thometz_scripts/Polished_plots_figures/Pedigree_accum_3runs",
height = 4,
width = 9,
units = "in")