library("metabaR")
library("dplyr")
library("ggplot2")
library("ggpubr")
library("tidyr")
library("RColorBrewer")
library("vegan")
library("betapart")
library("iNEXT")
library("ggrepel")
library("broom")
library("lme4")
library("lmerTest")

## 1.0 LOADING DATA -----

## This R code is meant to work with the output from Appendix2_metabaR_code_2022.R

inverts_all <- readRDS("Appendix5_Clean_Dataset_June12.rds") ## load aggregate metabarlist (already clean, no  negatives)

summary_metabarlist(inverts_all) ## summary of sequences/OTUs

##        nb_reads nb_motus avg_reads sd_reads avg_motus sd_motus
##pcrs    41978040     1681  177122.5 46880.63  65.05063 29.35445
##samples 41978040     1681  177122.5 46880.63  65.05063 29.35445


## 1.1 Invert OTUs ----

## subset by month

inverts_may <- subset_metabarlist(inverts_all, 
                                  table = "samples",
                                  indices = inverts_all$samples$Month == "May")

summary_metabarlist(inverts_may)


##$dataset_statistics
##nb_reads nb_motus avg_reads sd_reads avg_motus sd_motus
##pcrs    13750197     1076  174053.1 36993.95  63.29114 30.37634
##samples 13750197     1076  174053.1 36993.95  63.29114 30.37634

inverts_july <- subset_metabarlist(inverts_all, 
                                   table = "samples",
                                   indices = inverts_all$samples$Month == "July")

summary_metabarlist(inverts_july)

##$dataset_statistics
##nb_reads nb_motus avg_reads sd_reads avg_motus sd_motus
##pcrs    12827644      967  162375.2 47685.19  62.08861 28.57836
##samples 12827644      967  162375.2 47685.19  62.08861 28.57836

inverts_sept <- subset_metabarlist(inverts_all, 
                                   table = "samples",
                                   indices = inverts_all$samples$Month == "Sept")

summary_metabarlist(inverts_sept)

##$dataset_statistics
##nb_reads nb_motus avg_reads sd_reads avg_motus sd_motus
##pcrs    15400199     1045  194939.2 49557.91  69.77215 28.86349
##samples 15400199     1045  194939.2 49557.91  69.77215 28.86349

## Convert dataframes to presence/absence
inverts_all$reads_PA <- decostand(inverts_all$reads, "pa")
inverts_may$reads_PA <- decostand(inverts_may$reads, "pa")
inverts_july$reads_PA <- decostand(inverts_july$reads, "pa")
inverts_sept$reads_PA <- decostand(inverts_sept$reads, "pa")

## 1.2 Invert Families ----

## Scaling dataset to family-level taxonomy and subsetting by month

temp <- subset_metabarlist(inverts_all, "motus", 
                           indices = inverts_all$motus$Family != "")

family_all <- aggregate_motus(temp, groups = temp$motus$Family)

summary_metabarlist(family_all)

##$dataset_statistics
##nb_reads nb_motus avg_reads sd_reads avg_motus sd_motus
##pcrs    41289268      145  174216.3 48932.78  13.18987 4.703252
##samples 41289268      145  174216.3 48932.78  13.18987 4.703252


family_may <- subset_metabarlist(family_all, 
                                 table = "samples",
                                 indices = family_all$samples$Month == "May")

summary_metabarlist(family_may)

##$dataset_statistics
##nb_reads nb_motus avg_reads sd_reads avg_motus sd_motus
##pcrs    13339102       92  168849.4 43798.04  12.32911 4.268914
##samples 13339102       92  168849.4 43798.04  12.32911 4.268914

family_july <- subset_metabarlist(family_all, 
                                  table = "samples",
                                  indices = family_all$samples$Month == "July")


summary_metabarlist(family_july)

##dataset_statistics
##nb_reads nb_motus avg_reads sd_reads avg_motus sd_motus
##pcrs    12765125      110  161583.9 48234.57  13.37975 5.097269
##samples 12765125      110  161583.9 48234.57  13.37975 5.097269

family_sept <- subset_metabarlist(family_all, 
                                  table = "samples",
                                  indices = family_all$samples$Month == "Sept")

summary_metabarlist(family_sept)

##$dataset_statistics
##nb_reads nb_motus avg_reads sd_reads avg_motus sd_motus
##pcrs    15185041      102  192215.7 49808.38  13.86076 4.634691
##samples 15185041      102  192215.7 49808.38  13.86076 4.634691

## Convert dataframes to presence/absence
family_all$reads_PA <- decostand(family_all$reads, "pa")
family_may$reads_PA <- decostand(family_may$reads, "pa")
family_july$reads_PA <- decostand(family_july$reads, "pa")
family_sept$reads_PA <- decostand(family_sept$reads, "pa")

## 1.3 Chironomid OTUs ----

## Subsetting OTU dataframe to only include chironomids and subsetting by month
chiros_all <- subset_metabarlist(inverts_all, 
                                 table = "motus",
                                 indices = inverts_all$motus$Family == "Chironomidae")

summary_metabarlist(chiros_all)

##$dataset_statistics
##nb_reads nb_motus avg_reads sd_reads avg_motus sd_motus
##pcrs    12731980      586  53721.43 49234.44  24.67089 16.57422
##samples 12731980      586  53721.43 49234.44  24.67089 16.57422


chiros_may <- subset_metabarlist(chiros_all, 
                                 table = "samples",
                                 indices = chiros_all$samples$Month == "May")

summary_metabarlist(chiros_may)

##$dataset_statistics
##nb_reads nb_motus avg_reads sd_reads avg_motus sd_motus
##pcrs     5891922      429  74581.29 51312.84  29.03797 18.24017
##samples  5891922      429  74581.29 51312.84  29.03797 18.24017

chiros_july <- subset_metabarlist(chiros_all, 
                                  table = "samples",
                                  indices = chiros_all$samples$Month == "July")

summary_metabarlist(chiros_july)

##$dataset_statistics
##nb_reads nb_motus avg_reads sd_reads avg_motus sd_motus
##pcrs     3603458      342  45613.39 46020.18  21.21519 13.34711
##samples  3603458      342  45613.39 46020.18  21.21519 13.34711

chiros_sept <- subset_metabarlist(chiros_all, 
                                  table = "samples",
                                  indices = chiros_all$samples$Month == "Sept")

summary_metabarlist(chiros_sept)

##$dataset_statistics
##nb_reads nb_motus avg_reads sd_reads avg_motus sd_motus
##pcrs     3236600      358  40969.62 43719.46  23.75949 16.97224
##samples  3236600      358  40969.62 43719.46  23.75949 16.97224

## Convert dataframes to presence/absence
chiros_all$reads_PA <- decostand(chiros_all$reads, "pa")
chiros_may$reads_PA <- decostand(chiros_may$reads, "pa")
chiros_july$reads_PA <- decostand(chiros_july$reads, "pa")
chiros_sept$reads_PA <- decostand(chiros_sept$reads, "pa")

### 2.0 TAXA RICHNESS ----- 

## 2.1 Richness over time ----

## Exploratory plot of richness over time - not in manuscript

RichnessOTU <- as.data.frame(inverts_all$samples) 
RichnessOTU$total <- inverts_all$pcrs$total_motus

## Can subset by taxa for visualization individually

OTU_plot <- RichnessOTU %>% 
  group_by(Site_code, Month, Site_type) %>% 
  summarise(avg = mean(total), se = sd(total))

RichnessFam <- as.data.frame(family_all$samples) 
RichnessFam$total <- rowSums(family_all$reads > 0)

Fam_plot <- RichnessFam %>% 
  group_by(Site_code, Month, Site_type) %>% 
  summarise(avg = mean(total), se = sd(total))

RichnessChiro <- as.data.frame(chiros_all$samples) 
RichnessChiro$total <- rowSums(chiros_all$reads > 0)

Chiro_plot <- RichnessChiro %>% 
  group_by(Site_code, Month, Site_type) %>% 
  summarise(avg = mean(total), se = sd(total))

OTU_plot$ID_level <- "OTUs"
Fam_plot$ID_level <- "Families"
Chiro_plot$ID_level <- "Chironomids"

Richness_all <- rbind(OTU_plot, Fam_plot, Chiro_plot)
tibble(Richness_all)

Richness_all$ID_level = factor(Richness_all$ID_level, levels = c("Families", "OTUs", "Chironomids"))
Richness_all$Month = factor(Richness_all$Month, levels = c("May", "July", "Sept"))

ggplot(Richness_all, aes(x = Month, y = avg)) +
  geom_point(aes(colour = Site_type, shape = Site_type, size = 2)) +
  geom_line(aes(group = Site_code, colour = Site_type)) +
  theme_classic() +
  ylab("Average Richness") +
  guides(size = FALSE) +
  theme(legend.position = "bottom") +
  scale_colour_manual(values = c("steelblue", "orange"),
                      name = "Site Type",
                      breaks = c("CA", "Farm"),
                      labels = c("Conservation Area", "Agriculture")) +
  scale_shape_discrete(name = "Site Type",
                       breaks = c("CA", "Farm"),
                       labels = c("Conservation Area", "Agriculture")) +
  facet_wrap(~ID_level, scales = "free") 

## ggsave("ExtraPlot1_RichnessTime.jpg", plot = last_plot())

## 2.2 Richness comparison ----

## Extra plot - visualizing richness with all months together

ggplot(Richness_all, aes(fill = Site_type, x = ID_level, y = avg)) +
  geom_boxplot() +
  theme_classic(base_size = 15) +
  xlab("ID Level") +
  ylab("Richness") +
  scale_fill_manual(values = c("steelblue", "orange")) +
  guides(fill = guide_legend(title="Site Type"))

## ggsave("Extraplot2_TotalRichness.jpg")


ggplot(Richness_all, aes(x = Month, y = avg, 
                              fill = Site_type)) +
  geom_boxplot(position = position_dodge(0.8), lwd = 0.5, alpha = 0.8) +
  geom_jitter(position = position_dodge(0.8), aes(shape = Site_type)) +
  theme_classic(14) +
  xlab("Month") +
  ylab("Average Richness") +
  guides(fill = guide_legend(title="Site Type")) +
  guides(shape = guide_legend(title = "Site Type")) +
  guides(colour = guide_legend(title="Site Type")) +
  scale_fill_manual(values = c("steelblue", "orange")) +
  scale_colour_manual(values = c("steelblue", "orange")) +
  facet_wrap(~ID_level, scales = "free_y")  ## update to each facet having unique grid

## ggsave("Figure2_Richness.jpg", plot = last_plot())

## assessing normality

shapiro.test(OTU_plot$avg)

shapiro.test(Fam_plot$avg)

shapiro.test(Chiro_plot$avg)

qqPlot(OTU_plot$avg)
qqPlot(Fam_plot$avg)
qqPlot(Chiro_plot$avg)


m1 <- lmer(avg ~ Site_type * Month + (1|Site_code), data = OTU_plot)
summary(m1)
confint(m1)
anova(m1)

m2 <- lmer(avg ~ Site_type * Month + (1|Site_code), data = Fam_plot)
summary(m2)
anova(m2)

m3 <- lmer(avg ~ Site_type * Month + (1|Site_code), data = Chiro_plot)
summary(m3)
anova(m3)



### 3.0 BETA DIVERSITY -----

##Testing - function to subset to sites, calculate pairwise dissimilarity between field reps and take the mean

ArcC <- subset_metabarlist(inverts_july,
                           table = "samples",
                           indices = (inverts_july$samples$Site_code == "ArcC"))

ArcC$reads_PA <- decostand(ArcC$reads, "pa")

test <- raupcrick(ArcC$reads_PA, null = "r1", nsimul = 999)

test

mean(test)


## 3.1 Raup Crick Avg Dissimilarity Families ----

## MAY

x <- unique(inverts_may$samples$Site_code)
y <- unique(inverts_may$samples$Ag_watershed)

temp1 <- lapply(x, function(x) {subset_metabarlist(
  family_may,
  table = "samples",
  indices = (family_may$samples$Site_code == x))})

df1 <- sapply(temp1, function(x) {temp1$reads_PA <- decostand(x$reads, "pa") ## create presence/absence table of reads

mean(raupcrick(temp1$reads_PA, null = "r1", nsimul = 999)) ## take mean of raupcrick diversity 

})

df1 <- as.data.frame(df1) ## create dataframe, avg dissimilarity per site 
colnames(df1) <- "Dissimilarity"
df1$Site_code <- x ## assign site codes 
df1$Ag <- y ## attach ag %
df1$Month <- "May"
tibble(df1)

##July

temp2 <- lapply(x, function(x) {subset_metabarlist(
  family_july,
  table = "samples",
  indices = (family_july$samples$Site_code == x))})

df2 <- sapply(temp2, function(x) {temp2$reads_PA <- decostand(x$reads, "pa") ## create presence/absence table of read

mean(raupcrick(temp2$reads_PA, null = "r1", nsimul = 999))

})

df2 <- as.data.frame(df2) ## create dataframe 
colnames(df2) <- "Dissimilarity"
df2$Site_code <- x ## assign site codes 
df2$Ag <- y ## attach ag %
df2$Month <- "July"
tibble(df2)

## Sept

temp3 <- lapply(x, function(x) {subset_metabarlist(
  family_sept,
  table = "samples",
  indices = (family_sept$samples$Site_code == x))})

df3 <- sapply(temp3, function(x) {temp3$reads_PA <- decostand(x$reads, "pa") ## create presence/absence table of read

mean(raupcrick(temp3$reads_PA, null = "r1", nsimul = 999))

})

df3 <- as.data.frame(df3) ## create dataframe 
colnames(df3) <- "Dissimilarity"
df3$Site_code <- x ## assign site codes 
df3$Ag <- y ## attach ag %
df3$Month <- "Sept"
tibble(df3)

df_raup_fam <- rbind(df1, df2, df3)
df_raup_fam$ID_level <- "Families"

r1 <- lmer(Dissimilarity~Ag*Month + (1|Site_code), data = df_raup_fam) 
summary(r1)
anova(r1)


## 3.2 Raup Crick Avg Dissimilarity OTUs ----

## MAY

x <- unique(inverts_may$samples$Site_code)
y <- unique(inverts_may$samples$Ag_watershed)

temp1 <- lapply(x, function(x) {subset_metabarlist(
  inverts_may,
  table = "samples",
  indices = (inverts_may$samples$Site_code == x))})

df1 <- sapply(temp1, function(x) {temp1$reads_PA <- decostand(x$reads, "pa") ## create presence/absence table of reads

mean(raupcrick(temp1$reads_PA, null = "r1", nsimul = 999))

})

df1 <- as.data.frame(df1) ## create dataframe 
colnames(df1) <- "Dissimilarity"
df1$Site_code <- x
df1$Ag <- y ## attach ag %
df1$Month <- "May"
tibble(df1)

##July

temp2 <- lapply(x, function(x) {subset_metabarlist(
  inverts_july,
  table = "samples",
  indices = (inverts_july$samples$Site_code == x))})

df2 <- sapply(temp2, function(x) {temp2$reads_PA <- decostand(x$reads, "pa") ## create presence/absence table of read

mean(raupcrick(temp2$reads_PA, null = "r1", nsimul = 999))

})

df2 <- as.data.frame(df2) ## create dataframe 
colnames(df2) <- "Dissimilarity"
df2$Site_code <- x
df2$Ag <- y ## attach ag %
df2$Month <- "July"
tibble(df2)

## Sept

temp3 <- lapply(x, function(x) {subset_metabarlist(
  inverts_sept,
  table = "samples",
  indices = (inverts_sept$samples$Site_code == x))})

df3 <- sapply(temp3, function(x) {temp3$reads_PA <- decostand(x$reads, "pa") ## create presence/absence table of read

mean(raupcrick(temp3$reads_PA, null = "r1", nsimul = 999))

})

df3 <- as.data.frame(df3) ## create dataframe 
colnames(df3) <- "Dissimilarity"
df3$Site_code <- x
df3$Ag <- y ## attach ag %
df3$Month <- "Sept"
tibble(df3)

df_raup_OTU <- rbind(df1, df2, df3)
df_raup_OTU$ID_level <- "OTUs"

r2 <- lmer(Dissimilarity~Ag*Month + (1|Site_code), data = df_raup_OTU) 
summary(r2)
anova(r2)

  
## 3.3 Raup Crick Avg Dissimilarity Chiros ----

## MAY

x <- unique(inverts_may$samples$Site_code)
y <- unique(inverts_may$samples$Ag_watershed)

temp1 <- lapply(x, function(x) {subset_metabarlist(
  chiros_may,
  table = "samples",
  indices = (chiros_may$samples$Site_code == x))})

df1 <- sapply(temp1, function(x) {temp1$reads_PA <- decostand(x$reads, "pa") ## create presence/absence table of reads

mean(raupcrick(temp1$reads_PA, null = "r1", nsimul = 999))

})

df1 <- as.data.frame(df1) ## create dataframe 
colnames(df1) <- "Dissimilarity"
df1$Site_code <- x
df1$Ag <- y ## attach ag %
df1$Month <- "May"
tibble(df1)

##July

temp2 <- lapply(x, function(x) {subset_metabarlist(
  chiros_july,
  table = "samples",
  indices = (chiros_july$samples$Site_code == x))})

df2 <- sapply(temp2, function(x) {temp2$reads_PA <- decostand(x$reads, "pa") ## create presence/absence table of read

mean(raupcrick(temp2$reads_PA, null = "r1", nsimul = 999))

})

df2 <- as.data.frame(df2) ## create dataframe 
colnames(df2) <- "Dissimilarity"
df2$Site_code <- x
df2$Ag <- y ## attach ag %
df2$Month <- "July"
tibble(df2)

## Sept

temp3 <- lapply(x, function(x) {subset_metabarlist(
  chiros_sept,
  table = "samples",
  indices = (chiros_sept$samples$Site_code == x))})

df3 <- sapply(temp3, function(x) {temp3$reads_PA <- decostand(x$reads, "pa") ## create presence/absence table of read

mean(raupcrick(temp3$reads_PA, null = "r1", nsimul = 999))

})

df3 <- as.data.frame(df3) ## create dataframe 
colnames(df3) <- "Dissimilarity"
df3$Site_code <- x
df3$Ag <- y ## attach ag %
df3$Month <- "Sept"
tibble(df3)

df_raup_chiros <- rbind(df1, df2, df3)
df_raup_chiros$ID_level <- "Chironomids"

r3 <- lmer(Dissimilarity~Ag*Month + (1|Site_code), data = df_raup_chiros) 
summary(r3)
anova(r3)

## 3.4 Beta diversity plot ----

raup_all <- full_join(df_raup_fam, df_raup_OTU) %>% 
  full_join(df_raup_chiros)

tibble(raup_all)

raup_all$Month = factor(raup_all$Month, levels = c("May", "July", "Sept"))
raup_all$ID_level = factor(raup_all$ID_level, levels = c("Families", "OTUs", "Chironomids"))


ggplot(raup_all, aes(x = Ag, y = Dissimilarity)) +
  geom_point(size = 3, aes(shape = Month, fill = Month)) +
  geom_smooth(method = lm, se = FALSE, fullrange = TRUE, aes(colour = Month)) +
  labs(x = "Percent Agriculture",
       y = "Raup Crick Dissimilarity") +
  theme_classic(14) +
  theme(legend.position = "right") +
  facet_wrap(~ID_level, scales = "free_y") +
  scale_shape_manual(values = c(21, 22, 24)) +
  scale_colour_manual(values = c("steelblue", "orange", "darkred")) +
  scale_fill_manual(values = c("steelblue", "orange", "darkred")) +
  guides(colour = guide_legend(title="Month")) +
  guides(shape = guide_legend(title="Month")) +
  guides(fill = guide_legend(title="Month"))

##ggsave("Figure3_raup.jpg", plot = last_plot())


## 4.0 RAREFACTION CURVES ----

## 4.1 May ----

OTU_M <- list(t(as.data.frame(inverts_may$reads_PA)))
fam_M <- list(t(as.data.frame(family_may$reads_PA)))
chiro_M <- list(t(as.data.frame(chiros_may$reads_PA)))

OTU_M <- iNEXT(OTU_M, q = c(0), datatype = "incidence_raw")
names(OTU_M$iNextEst) <- "OTUs"

fam_M <- iNEXT(fam_M, q = c(0), datatype = "incidence_raw")
names(fam_M$iNextEst) <- "Families"

chiro_M <- iNEXT(chiro_M , q = c(0), datatype = "incidence_raw")
names(chiro_M$iNextEst) <- "Chironomids"

df1 <- fortify(OTU_M, type = 1)
df2 <- fortify(fam_M, type = 1)
df3 <- fortify(chiro_M, type = 1)

df_may <- rbind(df1, df2, df3)
tibble(df_may)

df_may$ID <- df_may$site
df_may$Month <- "May"

## 4.2 July ----

OTU_J <- list(t(as.data.frame(inverts_july$reads_PA)))
fam_J <- list(t(as.data.frame(family_july$reads_PA)))
chiro_J <- list(t(as.data.frame(chiros_july$reads_PA)))

OTU_J <- iNEXT(OTU_J, q = c(0), datatype = "incidence_raw")
names(OTU_J$iNextEst) <- "OTUs"

fam_J <- iNEXT(fam_J, q = c(0), datatype = "incidence_raw")
names(fam_J$iNextEst) <- "Families"

chiro_J <- iNEXT(chiro_J, q = c(0), datatype = "incidence_raw")
names(chiro_J$iNextEst) <- "Chironomids"

df1 <- fortify(OTU_J, type = 1)
df2 <- fortify(fam_J, type = 1)
df3 <- fortify(chiro_J, type = 1)

df_july <- rbind(df1, df2, df3)

df_july$ID <- df_july$site
df_july$Month <- "July"

## 4.3 Sept ----

OTU_S <- list(t(as.data.frame(inverts_sept$reads_PA)))
fam_S <- list(t(as.data.frame(family_sept$reads_PA)))
chiro_S <- list(t(as.data.frame(chiros_sept$reads_PA)))

OTU_S <- iNEXT(OTU_S, q = c(0), datatype = "incidence_raw")
names(OTU_S$iNextEst) <- "OTUs"

fam_S <- iNEXT(fam_S, q = c(0), datatype = "incidence_raw")
names(fam_S$iNextEst) <- "Families"

chiro_S <- iNEXT(chiro_S, q = c(0), datatype = "incidence_raw")
names(chiro_S$iNextEst) <- "Chironomids"

df1 <- fortify(OTU_S, type = 1)
df2 <- fortify(fam_S, type = 1)
df3 <- fortify(chiro_S, type = 1)

df_sept <- rbind(df1, df2, df3)

df_sept$ID <- df_sept$site
df_sept$Month <- "Sept"

## 4.4 Plot Rarefaction plot ----

tibble(df_may) ## check data

df <- rbind(df_may, df_july, df_sept)

df$ID = factor(df$ID, levels = c("Families", "OTUs", "Chironomids"))
df$Month = factor(df$Month, levels = c("May", "July", "Sept"))

df.point <- df[which(df$method=="observed"),]
df.line <- df[which(df$method!="observed"),]
df.line$method <- factor(df.line$method,
                         c("interpolated", "extrapolated"),
                         c("interpolation", "extrapolation"))

tibble(df)
tibble(df.line)

ggplot(df, aes(x = x, y = y)) +
  geom_point(aes(shape = Month, fill = Month), size = 5, data = df.point) +
  geom_line(aes(linetype = method, colour = Month), lwd = 0.8, data = df.line) +
  geom_ribbon(aes(ymin = y.lwr, ymax = y.upr,
                  fill = Month), alpha = 0.2) +
  labs(x = "Number of Samples", y = "Number of Taxa") +
  theme_classic() +
  scale_shape_manual(values = c(21, 22, 24)) +
  scale_fill_manual(values = c("steelblue", "orange", "darkred")) +
  scale_colour_manual(values = c("black", "black", "black")) +
  guides(linetype = guide_legend(title = NULL)) +
  facet_wrap(~ID, scales = "free_y")  

##ggsave("Figure4_rarefaction.jpg", plot = last_plot())


### 5.0 - OBSERVED V. EXPECTED -----

## Extra plot

df1 <- specpool(family_all$reads, pool = family_all$samples$Month)
df1 <- fortify(df1)
df1$Month <- rownames(df1)
df1$ID_level<- "Families"

df2 <- specpool(inverts_all$reads, pool = inverts_all$samples$Month)
df2 <- fortify(df2)
df2$Month <- rownames(df2)
df2$ID_level <- "OTUs"

df3 <- specpool(chiros_all$reads, pool = chiros_all$samples$Month)
df3 <- fortify(df3)
df3$Month <- rownames(df3)
df3$ID_level <- "Chironomids"

df_pool <- rbind(df1, df2, df3)
tibble(df_pool)

df_pool$Observed <- df_pool$Species
df_pool$Extrapolated <- df_pool$chao 

df_pool <- df_pool %>% 
  pivot_longer(cols = c("Observed", "Extrapolated"),
               names_to = "Type",
               values_to = "Richness")

df_pool$Month = factor(df_pool$Month, levels = c("May", "July", "Sept"))
df_pool$Type = factor(df_pool$Type, levels = c("Observed", "Extrapolated"))
df_pool$ID_level = factor(df_pool$ID_level, levels = c("Families", "OTUs", "Chironomids"))

ggplot(df_pool, aes(x = Type, y = Richness)) +
  geom_jitter(aes(fill = Month, shape = Month, size = 5), width = 0.1) +
  geom_line(aes(size = 2, group = Month)) +
  ##geom_errorbar(aes(ymin = Richness-chao.se, ymax = Richness+chao.se), width = 0.2) +
  theme_classic(base_size = 14) +
  ylab("Richness") +
  ##guides(colour = guide_legend(title="Month")) +
  guides(shape = guide_legend(title="Month")) +
  guides(fill = guide_legend(title="Month")) +
  guides(shape = guide_legend(override.aes = list(size = 5))) +
  guides(size = FALSE) +
  theme(legend.position = "right") +
  facet_wrap(~ID_level, scales = "free_y") +
  scale_shape_manual(values = c(21, 22, 24)) +
  scale_fill_manual(values = c("steelblue", "orange", "darkred")) 

## ggsave("Extraplot_ObservedExpected_update.jpg", plot = last_plot())




### 6.0 - SAMPLING COVERAGE -----

#May

df1 <- specpool(inverts_may$reads, pool = inverts_may$samples$Site_code)
df1 <- as.data.frame(df1)
df1$ID_level<- "OTUs"
df1$Month <- "May"
df1$Site_code <- row.names(df1)

df2 <- specpool(family_may$reads, pool = family_may$samples$Site_code)
df2 <- fortify(df2)
df2$ID_level<- "Families"
df2$Month <- "May"
df2$Site_code <- row.names(df2)

df3 <- specpool(chiros_may$reads, pool = chiros_may$samples$Site_code)
df3 <- fortify(df3)
df3$ID_level <- "Chironomids"
df3$Month <- "May"
df3$Site_code <- row.names(df3)

df_may <- rbind(df1, df2, df3)
tibble(df_may)

#July
df1 <- specpool(inverts_july$reads, pool = inverts_july$samples$Site_code)
df1 <- as.data.frame(df1)
df1$ID_level<- "OTUs"
df1$Month <- "July"
df1$Site_code <- row.names(df1)

df2 <- specpool(family_july$reads, pool = family_july$samples$Site_code)
df2 <- fortify(df2)
df2$ID_level<- "Families"
df2$Month <- "July"
df2$Site_code <- row.names(df2)

df3 <- specpool(chiros_july$reads, pool = chiros_july$samples$Site_code)
df3 <- fortify(df3)
df3$ID_level <- "Chironomids"
df3$Month <- "July"
df3$Site_code <- row.names(df3)

df_july <- rbind(df1, df2, df3)

#Sept
df1 <- specpool(inverts_sept$reads, pool = inverts_sept$samples$Site_code)
df1 <- as.data.frame(df1)
df1$ID_level<- "OTUs"
df1$Month <- "Sept"
df1$Site_code <- row.names(df1)

df2 <- specpool(family_sept$reads, pool = family_sept$samples$Site_code)
df2 <- fortify(df2)
df2$ID_level<- "Families"
df2$Month <- "Sept"
df2$Site_code <- row.names(df2)

df3 <- specpool(chiros_sept$reads, pool = chiros_sept$samples$Site_code)
df3 <- fortify(df3)
df3$ID_level <- "Chironomids"
df3$Month <- "Sept"
df3$Site_code <- row.names(df3)

df_sept <- rbind(df1, df2, df3)

## plot

df_pool <- rbind(df_may, df_july, df_sept)
df_pool <- df_pool %>% 
  mutate(Coverage = Species/chao)

df_may <- df_may %>% mutate(Coverage = Species/chao)
df_july <- df_july %>% mutate(Coverage = Species/chao)
df_sept <- df_sept %>% mutate(Coverage = Species/chao)

df_pool


df_pool$Month = factor(df_pool$Month, levels = c("May", "July", "Sept"))
df_pool$ID_level = factor(df_pool$ID_level, levels = c("Families", "OTUs", "Chironomids"))

ggplot(df_pool, aes(x = Month, y = Coverage, 
                               fill = Month)) +
  geom_boxplot(alpha = 0.8) +
  geom_jitter(position = position_dodge(0.8)) +
  geom_line(aes(group = Site_code), color = "grey", alpha = 0.8) +
  theme_classic(14) +
  xlab("Month") +
  ylab("Percentage of Coverage") +
  guides(fill = guide_legend(title="Month")) +
  scale_fill_manual(values = c("steelblue", "orange", "darkred")) +
  facet_wrap(~ID_level) 

##ggsave("Figure5_ObservedExpected.jpg", plot = last_plot())

m4 <- lmer(Coverage ~ Month*ID_level + (1|Site_code), data = df_pool)
summary(m4)
anova(m4)


df_pool %>% filter(ID_level == "Families") %>% summary() ## get min/max values
df_pool %>% filter(ID_level == "OTUs") %>% summary()
df_pool %>% filter(ID_level == "Chironomids") %>% summary()


### 7.0 - RARE TAXA ---- 

## 7.1 OTUs ---- 

x <- unique(inverts_may$samples$Site_code)
y <- unique(inverts_may$samples$Ag_watershed)

temp <- lapply(x, function(x) {subset_metabarlist(
  inverts_may,
  table = "samples",
  indices = (inverts_may$samples$Site_code == x))})


df1 <- t(sapply(temp, function(x) {temp$reads_PA <- decostand(x$reads, "pa") ## create presence/absence table of reads

a <- sum(colSums(temp$reads_PA) == 1)
b <- sum(colSums(temp$reads_PA) == 2)
c <- sum(colSums(temp$reads_PA) == 3)
d <- sum(colSums(temp$reads_PA) == 4)
e <- sum(a,b,c,d)
rbind(a,b,c,d,e)

}))

df1 <- as.data.frame(df1)
row.names(df1) <- x
colnames(df1) <- c("One", "Two", "Three", "Four", "Total")
df1$Site_code <- x
df1$Ag <- y
df1$Month <- "May"
df1$ID_level <- "OTUs"
df1

df1 <- df1 %>% pivot_longer(cols = c("One", "Two", "Three", "Four"),
                            names_to = "Occurrence",
                            values_to = "Rarity")

df1 <- df1 %>% mutate(Percent = Rarity/Total)
df1

##July

temp <- lapply(x, function(x) {subset_metabarlist(
  inverts_july,
  table = "samples",
  indices = (inverts_july$samples$Site_code == x))})


df2 <- t(sapply(temp, function(x) {temp$reads_PA <- decostand(x$reads, "pa") ## create presence/absence table of reads

a <- sum(colSums(temp$reads_PA) == 1)
b <- sum(colSums(temp$reads_PA) == 2)
c <- sum(colSums(temp$reads_PA) == 3)
d <- sum(colSums(temp$reads_PA) == 4)
e <- sum(a,b,c,d)
rbind(a,b,c,d,e)

}))

df2 <- as.data.frame(df2)
row.names(df2) <- x
colnames(df2) <- c("One", "Two", "Three", "Four", "Total")
df2$Site_code <- x
df2$Ag <- y
df2$Month <- "July"
df2$ID_level <- "OTUs"
df2

df2 <- df2 %>% pivot_longer(cols = c("One", "Two", "Three", "Four"),
                            names_to = "Occurrence",
                            values_to = "Rarity")

df2 <- df2 %>% mutate(Percent = Rarity/Total)
df2

## Sept

temp <- lapply(x, function(x) {subset_metabarlist(
  inverts_sept,
  table = "samples",
  indices = (inverts_sept$samples$Site_code == x))})


df3 <- t(sapply(temp, function(x) {temp$reads_PA <- decostand(x$reads, "pa") ## create presence/absence table of reads

a <- sum(colSums(temp$reads_PA) == 1)
b <- sum(colSums(temp$reads_PA) == 2)
c <- sum(colSums(temp$reads_PA) == 3)
d <- sum(colSums(temp$reads_PA) == 4)
e <- sum(a,b,c,d)
rbind(a,b,c,d,e)

}))

df3 <- as.data.frame(df3)
row.names(df3) <- x
colnames(df3) <- c("One", "Two", "Three", "Four", "Total")
df3$Site_code <- x
df3$Ag <- y
df3$Month <- "Sept"
df3$ID_level <- "OTUs"
df3

df3 <- df3 %>% pivot_longer(cols = c("One", "Two", "Three", "Four"),
                            names_to = "Occurrence",
                            values_to = "Rarity")

df3 <- df3 %>% mutate(Percent = Rarity/Total)
df3

df_o <- rbind(df1, df2, df3)
df_o



## 7.2 Families ---- 

x <- unique(family_may$samples$Site_code)
y <- unique(family_may$samples$Ag_watershed)

temp <- lapply(x, function(x) {subset_metabarlist(
  family_may,
  table = "samples",
  indices = (inverts_may$samples$Site_code == x))})


df1 <- t(sapply(temp, function(x) {temp$reads_PA <- decostand(x$reads, "pa") ## create presence/absence table of reads

a <- sum(colSums(temp$reads_PA) == 1)
b <- sum(colSums(temp$reads_PA) == 2)
c <- sum(colSums(temp$reads_PA) == 3)
d <- sum(colSums(temp$reads_PA) == 4)
e <- sum(a,b,c,d)
rbind(a,b,c,d,e)

}))

df1 <- as.data.frame(df1)
row.names(df1) <- x
colnames(df1) <- c("One", "Two", "Three", "Four", "Total")
df1$Site_code <- x
df1$Ag <- y
df1$Month <- "May"
df1$ID_level <- "Families"
df1

df1 <- df1 %>% pivot_longer(cols = c("One", "Two", "Three", "Four"),
                            names_to = "Occurrence",
                            values_to = "Rarity")

df1 <- df1 %>% mutate(Percent = Rarity/Total)
df1

##July

temp <- lapply(x, function(x) {subset_metabarlist(
  family_july,
  table = "samples",
  indices = (inverts_july$samples$Site_code == x))})


df2 <- t(sapply(temp, function(x) {temp$reads_PA <- decostand(x$reads, "pa") ## create presence/absence table of reads

a <- sum(colSums(temp$reads_PA) == 1)
b <- sum(colSums(temp$reads_PA) == 2)
c <- sum(colSums(temp$reads_PA) == 3)
d <- sum(colSums(temp$reads_PA) == 4)
e <- sum(a,b,c,d)
rbind(a,b,c,d,e)

}))

df2 <- as.data.frame(df2)
row.names(df2) <- x
colnames(df2) <- c("One", "Two", "Three", "Four", "Total")
df2$Site_code <- x
df2$Ag <- y
df2$Month <- "July"
df2$ID_level <- "Families"
df2

df2 <- df2 %>% pivot_longer(cols = c("One", "Two", "Three", "Four"),
                            names_to = "Occurrence",
                            values_to = "Rarity")

df2 <- df2 %>% mutate(Percent = Rarity/Total)
df2

## Sept

temp <- lapply(x, function(x) {subset_metabarlist(
  family_sept,
  table = "samples",
  indices = (inverts_sept$samples$Site_code == x))})


df3 <- t(sapply(temp, function(x) {temp$reads_PA <- decostand(x$reads, "pa") ## create presence/absence table of reads

a <- sum(colSums(temp$reads_PA) == 1)
b <- sum(colSums(temp$reads_PA) == 2)
c <- sum(colSums(temp$reads_PA) == 3)
d <- sum(colSums(temp$reads_PA) == 4)
e <- sum(a,b,c,d)
rbind(a,b,c,d,e)

}))

df3 <- as.data.frame(df3)
row.names(df3) <- x
colnames(df3) <- c("One", "Two", "Three", "Four", "Total")
df3$Site_code <- x
df3$Ag <- y
df3$Month <- "Sept"
df3$ID_level <- "Families"
df3

df3 <- df3 %>% pivot_longer(cols = c("One", "Two", "Three", "Four"),
                            names_to = "Occurrence",
                            values_to = "Rarity")

df3 <- df3 %>% mutate(Percent = Rarity/Total)
df3


df_f <- rbind(df1, df2, df3)
df_f

## 7.3 Chiros ---- 

x <- unique(chiros_may$samples$Site_code)
y <- unique(chiros_may$samples$Ag_watershed)

temp <- lapply(x, function(x) {subset_metabarlist(
  chiros_may,
  table = "samples",
  indices = (chiros_may$samples$Site_code == x))})


df1 <- t(sapply(temp, function(x) {temp$reads_PA <- decostand(x$reads, "pa") ## create presence/absence table of reads

a <- sum(colSums(temp$reads_PA) == 1)
b <- sum(colSums(temp$reads_PA) == 2)
c <- sum(colSums(temp$reads_PA) == 3)
d <- sum(colSums(temp$reads_PA) == 4)
e <- sum(a,b,c,d)
rbind(a,b,c,d,e)

}))

df1 <- as.data.frame(df1)
row.names(df1) <- x
colnames(df1) <- c("One", "Two", "Three", "Four", "Total")
df1$Site_code <- x
df1$Ag <- y
df1$Month <- "May"
df1$ID_level <- "Chironomids"
df1

df1 <- df1 %>% pivot_longer(cols = c("One", "Two", "Three", "Four"),
                            names_to = "Occurrence",
                            values_to = "Rarity")

df1 <- df1 %>% mutate(Percent = Rarity/Total)
df1

##July

temp <- lapply(x, function(x) {subset_metabarlist(
  chiros_july,
  table = "samples",
  indices = (chiros_july$samples$Site_code == x))})


df2 <- t(sapply(temp, function(x) {temp$reads_PA <- decostand(x$reads, "pa") ## create presence/absence table of reads

a <- sum(colSums(temp$reads_PA) == 1)
b <- sum(colSums(temp$reads_PA) == 2)
c <- sum(colSums(temp$reads_PA) == 3)
d <- sum(colSums(temp$reads_PA) == 4)
e <- sum(a,b,c,d)
rbind(a,b,c,d,e)

}))

df2 <- as.data.frame(df2)
row.names(df2) <- x
colnames(df2) <- c("One", "Two", "Three", "Four", "Total")
df2$Site_code <- x
df2$Ag <- y
df2$Month <- "July"
df2$ID_level <- "Chironomids"
df2

df2 <- df2 %>% pivot_longer(cols = c("One", "Two", "Three", "Four"),
                            names_to = "Occurrence",
                            values_to = "Rarity")

df2 <- df2 %>% mutate(Percent = Rarity/Total)
df2

## Sept

temp <- lapply(x, function(x) {subset_metabarlist(
  chiros_sept,
  table = "samples",
  indices = (chiros_sept$samples$Site_code == x))})


df3 <- t(sapply(temp, function(x) {temp$reads_PA <- decostand(x$reads, "pa") ## create presence/absence table of reads

a <- sum(colSums(temp$reads_PA) == 1)
b <- sum(colSums(temp$reads_PA) == 2)
c <- sum(colSums(temp$reads_PA) == 3)
d <- sum(colSums(temp$reads_PA) == 4)
e <- sum(a,b,c,d)
rbind(a,b,c,d,e)

}))

df3 <- as.data.frame(df3)
row.names(df3) <- x
colnames(df3) <- c("One", "Two", "Three", "Four", "Total")
df3$Site_code <- x
df3$Ag <- y
df3$Month <- "Sept"
df3$ID_level <- "Chironomids"
df3

df3 <- df3 %>% pivot_longer(cols = c("One", "Two", "Three", "Four"),
                            names_to = "Occurrence",
                            values_to = "Rarity")

df3 <- df3 %>% mutate(Percent = Rarity/Total)
df3

df_c <- rbind(df1, df2, df3)
df_c


## 7.4 Plot ----

df_rare <- rbind(df_f, df_o, df_c)
df_rare <- df_rare %>% 
  filter(Occurrence == "One")

tibble(df_rare)

df_rare$ID_level = factor(df_rare$ID_level, levels = c("Families", "OTUs", "Chironomids"))
df_rare$Month = factor(df_rare$Month, levels = c("May", "July", "Sept"))

ggplot(df_rare, aes(x = Month, y = Percent, 
                    fill = Month)) +
  geom_boxplot(alpha = 0.8) +
  geom_jitter(position = position_dodge(0.8)) +
  geom_line(aes(group = Site_code), color = "grey", alpha = 0.8) +
  theme_classic(14) +
  xlab("Month") +
  ylab("Percentage of Unique Taxa") +
  guides(fill = guide_legend(title="Month")) +
  scale_fill_manual(values = c("steelblue", "orange", "darkred")) +
  facet_wrap(~ID_level) 

##gsave("Fig6_Rarity_plot.jpg", plot = last_plot())

df_rare_o <- df_rare %>% 
  filter(ID_level == "OTUs")

summary(df_rare_o)

df_rare_f <- df_rare %>% 
  filter(ID_level == "Families")

summary(df_rare_f)

df_rare_c <- df_rare %>% 
  filter(ID_level == "Chironomids")

summary(df_rare_c)

m5 <- lmer(Percent ~ ID_level+Month + (1|Site_code), df_rare)
summary(m5)
anova(m5)

##END 
