install.packages ("tydiverse", independencies = T) library(vegan) library(tidyverse) ####Fungi(ITS)####### Read in the relevant files: ```{r, cache = TRUE} OTUS_ITS <- read.csv("input/OTU_table.csv") head (OTUS_ITS) tax_ITS <- read.csv("input/ITS_taxonomy_clean.csv") head (tax_ITS) ##################Rarefaction######################## plot(log(sort(colSums(OTUS_ITS[,-1]), decreasing = T), base = 10)) OTUS_MITS <- as.matrix(OTUS_ITS[,-1]) dimnames(OTUS_MITS) <- list(OTUS_ITS[,1], colnames(OTUS_ITS[,-1])) head(OTUS_MITS) rar_Richness_ITS <- rrarefy(OTUS_MITS, min(colSums(OTUS_MITS))) head(rar_Richness_ITS) tail(rar_Richness_ITS) write.csv(rar_Richness_ITS, "otu_ITS_rarrefied_soil.csv") srar_Richness_ITS <- rarefy(t(OTUS_MITS), sample = min(colSums(OTUS_MITS)), se = T) head(srar_Richness_ITS) Richness_ITS <- as.matrix(srar_Richness_ITS) head(Richness_ITS) write.csv(Richness_ITS, "rich_ITS_rarrefied.csv") ############Figure S1A pdf('FigS1A_ITS_raref.pdf') rarecurve(t(OTUS_MITS), step = 1000, label = "Fungi - ITS", ylab = "OTU", title = "Fungi - ITS") dev.off() ######################################################################### ####################Taxonomic Assignment################################# ######################################################################### ############################################################ #ITS ############################################################ ##load and prepare data ###taxonomy tax <- read_csv("input/ITS_taxonomy_clean.csv") tax <- tax%>% dplyr::select(OTU.ID = OTU_ID, taxonomy=classifications) View(tax) head(tax) tail(tax) ###abundance per site #soil abus <- read_csv("otu_ITS_rarrefied_soil.csv") head(abus) abus <- abus%>% rename(OTU.ID = X1) nams <- abus$OTU.ID abus[abus > 1] <-1 #convert to presence/absence abus$OTU.ID <- nams ##merge taxonomy and abundance abutaxs <- full_join(abus, tax, by = "OTU.ID") head (abutaxs) write.csv(abutaxs, "input/ITS_Fungi_taxonomy_metadata2.csv") ###Identity and number of OTUs not in the taxonomy file sum(is.na(abutaxs$taxonomy)) sum(is.na(abutaxs$taxonomy))/nrow(abus) ###Identity and number of OTUs not in the taxonomy file sum(is.na(abutaxs$SBCIGP1)) sum(is.na(abutaxs$SBCIGP1))/nrow(tax) ##Create taxonomy table ###not rarefied ####put table in right format for summary snon_rar_ITS <- gather(abutaxs, key = site, value = presence, -OTU.ID, -taxonomy)%>% filter(presence > 0) ####define the taxonomic level to use for all groups,and add to table tax.list <- c("p__Ascomycota","p__Basidiomycota","p__Chytridiomycota","p__Entorrhizomycota","p__Glomeromycota","p__Kickxellomycota", "p__Monoblepharomycota","p__Mortierellomycota","p__Mucoromycota","p__Mucoromycotina","p__Neocallimastigomycota", "p__Rozellomycota","p__Zoopagomycota","p__unidentified") snon_rar_ITS <- snon_rar_ITS%>% mutate(tax.class = "others")%>% mutate(tax.class = ifelse(grepl(tax.list[1], taxonomy), tax.list[1], tax.class))%>% mutate(tax.class = ifelse(grepl(tax.list[2], taxonomy), tax.list[2], tax.class))%>% mutate(tax.class = ifelse(grepl(tax.list[3], taxonomy), tax.list[3], tax.class))%>% mutate(tax.class = ifelse(grepl(tax.list[4], taxonomy), tax.list[4], tax.class))%>% mutate(tax.class = ifelse(grepl(tax.list[5], taxonomy), tax.list[5], tax.class))%>% mutate(tax.class = ifelse(grepl(tax.list[6], taxonomy), tax.list[6], tax.class))%>% mutate(tax.class = ifelse(grepl(tax.list[7], taxonomy), tax.list[7], tax.class))%>% mutate(tax.class = ifelse(grepl(tax.list[8], taxonomy), tax.list[8], tax.class))%>% mutate(tax.class = ifelse(grepl(tax.list[9], taxonomy), tax.list[9], tax.class))%>% mutate(tax.class = ifelse(grepl(tax.list[10], taxonomy), tax.list[10], tax.class))%>% mutate(tax.class = ifelse(grepl(tax.list[11], taxonomy), tax.list[11], tax.class))%>% mutate(tax.class = ifelse(grepl(tax.list[12], taxonomy), tax.list[12], tax.class))%>% mutate(tax.class = ifelse(grepl(tax.list[13], taxonomy), tax.list[13], tax.class))%>% mutate(tax.class = ifelse(grepl(tax.list[14], taxonomy), tax.list[14], tax.class))%>% mutate(tax.class = ifelse(is.na(taxonomy), "unknown", tax.class)) ####create output tables head(abus) #create site to locality and habitat key skeys <- names(abus)%>% as_tibble()%>% rename(site = value)%>% mutate(locality = "BC")%>% mutate(locality = ifelse(grepl("CUI", site), "CUI", locality))%>% mutate(locality = ifelse(grepl("CXN", site), "CXN", locality))%>% mutate(locality = ifelse(grepl("JAU", site), "JAU", locality))%>% mutate(habitat = "IG")%>% mutate(habitat = ifelse(grepl("TF", site), "TF", habitat))%>% mutate(habitat = ifelse(grepl("VZ", site), "VZ", habitat))%>% mutate(habitat = ifelse(grepl("CAM", site), "CAM", habitat))%>% mutate(sample = "In")%>% mutate(sample = ifelse(grepl("L", site), "L", sample))%>% mutate(sample = ifelse(grepl("S", site), "S", sample)) out_abus <- snon_rar_ITS%>% left_join(skeys, by = "site") tail(out_abus) write_csv(out_abus, "ITS_taxonomy_per_site_rarefied.csv") ###################################################################### #####################Plots taxonomic groups############################ ###################################################################### ########################################## #rarified ########################################## ITS_rars <- read_csv("ITS_taxonomy_per_site_rarefied.csv") head(ITS_rars) ITS_rars$locality #load ITS plot_ITS <- ITS_rars%>% distinct(OTU.ID, site, .keep_all = T)%>% group_by(site, tax.class)%>% summarise(OTUs.number = n()) %>% mutate(fraction.OTUs = round(OTUs.number / sum(OTUs.number), 3))%>% filter(!is.na(site))%>% rename(value = site)%>% mutate(type = "site") write.csv(plot_ITS, "plot_ITS.csv") plot_ITS <- read.csv("plot_ITS.csv") samp_ITS <- ITS_rars%>% distinct(OTU.ID, sample, .keep_all = T)%>% group_by(sample, tax.class)%>% summarise(OTUs.number = n()) %>% mutate(fraction.OTUs = round(OTUs.number / sum(OTUs.number), 3))%>% filter(!is.na(sample))%>% rename(value = sample)%>% mutate(type = "sample") sloc_ITS <- ITS_rars%>% distinct(OTU.ID, locality, .keep_all = T)%>% group_by(locality, tax.class)%>% summarise(OTUs.number = n()) %>% mutate(fraction.OTUs = round(OTUs.number / sum(OTUs.number), 3))%>% filter(!is.na(locality))%>% rename(value = locality)%>% mutate(type = "locality") splo_ITS <- bind_rows(plot_ITS,samp_ITS)%>% mutate(marker = "ITS") #View(splo_ITS) write.csv(splo_ITS, "figure.csv") splo_ITS$tax.class <-factor(splo_ITS$tax.class, levels = unique(splo_ITS$tax.class[order(splo_ITS$fraction.OTUs)])) #number mycolors = c(brewer.pal(name="Dark2", n = 8), brewer.pal(name="Paired", n = 6)) ITS_plot <- ggplot(data = plot_ITS, aes(x= value, y=OTUs.number, fill=tax.class))+ geom_bar(stat = "identity", position="dodge")+ scale_color_manual(values = "mycolors", name = "Taxa Fungi (ITS) Soil")+ ylab("Number of OTUs")+ xlab("")+ coord_flip()+ theme_bw()+ theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black"), panel.border = element_rect(colour = "black", fill=NA, size=1), legend.position = "right")+ facet_grid(~type, scales = "free_x") ggsave("ITS_number_of_OTUs_perPlot4.pdf", plot =ITS_plot, width =8, height =12) #fraction ITS_S <- ggplot(data = splo_ITS, aes(x= value, y=fraction.OTUs, fill=tax.class))+ geom_bar(stat = "identity", position=position_dodge())+ scale_color_manual(values = "mycolors", name = "Taxa Prokaryote (ITS) Soil")+ ylab("Fraction of OTUs")+ xlab("")+ theme_bw()+ theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black"), panel.border = element_rect(colour = "black", fill=NA, size=1), legend.position = "right")+ facet_grid(~type, scales = "free_x") ggsave("ITS_fraction_of_OTUs_rarefied.pdf", plot = ITS_S, width =12, height = 8) ######################################### #OTU "ranges" ################################################# ITS_unrar <- read_csv("ITS_taxonomy_per_site_rarefied.csv") #ITS pn <- ITS_unrar%>% select(-taxonomy, -locality, -habitat)%>% group_by(tax.class, OTU.ID)%>% summarise(plot_number = n())%>% filter(!tax.class == "unknown")%>% filter(!tax.class == "others")%>% ungroup()%>% mutate(tax.class = gsub("bacteria", "bac", tax.class)) #number of habitats hab_n <- ITS_unrar%>% filter(!tax.class == "unknown")%>% filter(!tax.class == "others")%>% filter(!is.na(habitat))%>% select(-taxonomy, -locality)%>% group_by(tax.class, OTU.ID, habitat)%>% summarise(OTUs.number = n())%>% ungroup()%>% select(-OTUs.number)%>% group_by(tax.class, OTU.ID)%>% summarise(habitat_number = n())%>% group_by(tax.class, habitat_number )%>% summarise(num_hab_num = n())%>% mutate(fraction_hab_num = round(num_hab_num/ sum(num_hab_num), 3))%>% ungroup()%>% mutate(tax.class = gsub("bacteria", "bac", tax.class)) #number of localities loc_n <- ITS_unrar%>% filter(!tax.class == "unknown")%>% filter(!tax.class == "others")%>% filter(!is.na(locality))%>% select(-taxonomy, -habitat)%>% group_by(tax.class, OTU.ID, locality)%>% summarise(OTUs.number = n())%>% ungroup()%>% select(-OTUs.number)%>% group_by(tax.class, OTU.ID)%>% summarise(locality_number = n())%>% group_by(tax.class, locality_number )%>% summarise(num_loc_num = n())%>% mutate(fraction_loc_num = round(num_loc_num/ sum(num_loc_num), 3))%>% ungroup()%>% mutate(tax.class = gsub("bacteria", "bac", tax.class)) plo_pn <- ggplot()+ geom_bar(aes(x = taxonomy, y = fraction, fill = sample), stat = "identity", position = "dodge")+ ylab("Number of sites")+ ggtitle("Number of sites")+ theme_bw()+ theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black"), panel.border = element_rect(colour = "black", fill=NA, size=1)) plo_pn_den <-ggplot()+ geom_density(data = pn, aes(plot_number, group = tax.class, col = tax.class))+ scale_colour_brewer(palette = "Paired", name = "")+ ggtitle("Number of sites - density")+ xlab("Number of sites")+ ylab("Density")+ xlim(c(1, 39))+ guides(color=guide_legend(ncol=2))+ theme_bw()+ theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black"), panel.border = element_rect(colour = "black", fill=NA, size=1), legend.position = c(0.65,0.78), legend.title = element_blank()) plo_habn <- ggplot(data = hab_n, aes(x= tax.class, y=fraction_hab_num, fill=as.factor(habitat_number)))+ geom_bar(stat = "identity", position=position_dodge())+ scale_fill_brewer(palette = "Set3", name = "")+ ylab("Fraction of OTUs")+ ggtitle("Number of habitats")+ guides(fill=guide_legend(ncol=4))+ theme_bw()+ theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black"), panel.border = element_rect(colour = "black", fill=NA, size=1), legend.position = c(0.8, 0.93), legend.title = element_blank()) plo_locn <- ggplot(data = loc_n, aes(x= tax.class, y=fraction_loc_num, fill=as.factor(locality_number)))+ geom_bar(stat = "identity", position=position_dodge())+ scale_fill_brewer(palette = "Accent", name = "")+ ggtitle("Number of localities")+ ylab("Fraction of OTUs")+ guides(fill=guide_legend(ncol=4))+ theme_bw()+ theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black"), panel.border = element_rect(colour = "black", fill=NA, size=1), legend.position = c(0.8, 0.93), legend.title = element_blank()) out <- grid.arrange(plo_pn, plo_pn_den, plo_habn, plo_locn, top = "OTU range size ITS") library(gridExtra) ggsave("Otu_ranges_ITS_rarified.pdf", plot = out, width = 12, height = 8) ######################################################################### ################Making the presence/absence matrix####################### ######################################################################### comITS <- rar_Richness_ITS comITS [comITS > 1] <- 1 comITS <- as.data.frame(comITS) nrow (comITS) write.csv(comITS , "otu_ITS_presence_absence.csv") ##########Betadiversity analysis comITS <- comITS%>% filter(rowSums(comITS) != 0) ITS.nmds <- metaMDS(as.matrix(t(comITS)), distance="jaccard",k=2, autotransform=F) ITS.nmds <- metaMDS(as.matrix(t(comITS)), distance="jaccard",k=2, autotransform=F, previou.best = ITS.nmds) summary(ITS.nmds) scITS <- data.frame(scores(ITS.nmds)) scITS$ID <- rownames(scITS) write.csv(scITS, "ITS_NMDS_metadata.csv") biol <- read.csv("input/metadata.csv") scITS <- scITS %>% as_data_frame()%>% inner_join(biol, by = "ID") ##envfit ###Eukaryote(ITS) envfit(scITS[,1:2] ~ as.factor(scITS$Locality)) envfit(scITS[,1:2] ~ as.factor(scITS$Habitat)) envfit(scITS[,1:2] ~ as.factor(scITS$Sample)) cols1 <- c("#e66101", "#fdb863", "#b2abd2", "#5e3c99") cols2 <- c("#ca0020", "#dfc27d", "#92c5de", "#0571b0") figA <- scITS %>% ggplot()+ geom_point(aes(x = NMDS1, y = NMDS2, colour = Habitat, shape = Sample), size = 3)+ ggtitle("A - ITS fo Habitat")+ xlab("")+ ylab("")+ scale_fill_discrete(name = "Environmental\ntype")+ scale_color_manual(values=cols1)+ theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black"), panel.border = element_rect(colour = "black", fill=NA, size=1), legend.position = ("left")) #subfigure B figB <- scITS %>% ggplot()+ geom_point(aes(x = NMDS1, y = NMDS2, colour = Locality, shape = Sample), size = 3)+ ggtitle("B - ITS fo Locality")+ xlab("")+ ylab("")+ scale_fill_discrete(name = "Environmental\ntype")+ scale_shape_manual(values=c(16:17))+ scale_color_manual(values=cols2)+ theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black"), panel.border = element_rect(colour = "black", fill=NA, size=1), legend.position = ("left")) fig2 <- gridExtra::grid.arrange(figA,figB, bottom = "NMDS1", left = "NMDS2") ggsave("Fig_NMDS_Habitat_and_Locality.pdf", plot = fig2, width =10, height = 12) disITS <- vegdist(as.matrix(t(comITS)), method = "jaccard") summary(disITS) ######################################################### ###################PERMANOVA############################# ######################################################### tcomITS <- t(comITS) tcomITS <- as.data.frame (tcomITS) adoITS <- adonis2(tcomITS ~ biol$Locality+ biol$Habitat + biol$Sample, strata = biol$ID, data = tcomITS, type = 'Jaccard', binary = TRUE) summary(adoITS) adoITS adoITS2 <- adonis(tcomITS ~ biol$Locality+ biol$Habitat + biol$Sample, strata = biol$ID, data = tcomITS, type = 'Jaccard', binary = TRUE) adoITS2 ################################################################ ##################Variance partitioning######################### ################################################################ mod <- varpart(disITS, ~ biol$Locality, ~ biol$Sample, ~ biol$Habitat) mod plot(mod) rda.all <- rda(t(comITS) ~ biol$Locality + biol$Sample, data = biol) anova(rda.all) rda.sample <- rda(t(comITS) ~ Sample, data = biol) anova(rda.sample) rda.locality <- rda(t(comITS) ~ Locality, data = biol) anova(rda.locality) rda.habitat <- rda(t(comITS) ~ Habitat, data = biol) anova(rda.habitat)