#' Used for cleaning and renaming taxonomy in phyloseq objects
#'
#' Used internally in other ampvis functions.
#'
#' @usage amp_rename(data)
#'
#' @param data (required) A ampvis formated list with all data.
#' @param tax.class Converts a specific phyla to class level instead (e.g. "p__Proteobacteria").
#' @param tax.empty Either "remove" OTUs without taxonomic information at X level, with "best" classification or add the "OTU" name (default: best).
#' @param tax.level The taxonomic level to remove OTUs with empty taxonomy, only used when tax.empty = "remove" (default: Genus).
#'
#' @return A phyloseq object with cleaned and renamed taxonomy.
#'
#' @export
#' @import phyloseq
#' @import dplyr
#'
#' @author Mads Albertsen \email{MadsAlbertsen85@@gmail.com}
amp_rename <- function(data, tax.class = NULL, tax.empty = "best", tax.level = "Genus"){
tax <- data[["tax"]]
## First make sure that all entires are strings
for ( i in 1:ncol(tax) ){
tax[,i] <- as.character(tax[,i])
}
## Change a specific phylum to class level
if(!is.null(tax.class)){
for (i in 1:nrow(tax)){
if (!is.na(tax$Phylum[i]) & tax$Phylum[i] %in% tax.class){
tax$Phylum[i] <- tax$Class[i]
}
}
}
## Remove the underscore classifier from the data
tax$Kingdom <- gsub("k__", "", tax$Kingdom)
tax$Phylum <- gsub("p__", "", tax$Phylum)
tax$Phylum <- gsub("c__", "", tax$Phylum)
tax$Class <- gsub("c__", "", tax$Class)
tax$Order <- gsub("o__", "", tax$Order)
tax$Family <- gsub("f__", "", tax$Family)
tax$Genus <- gsub("g__", "", tax$Genus)
tax$Kingdom <- gsub("uncultured", "", tax$Kingdom)
tax$Phylum <- gsub("uncultured", "", tax$Phylum)
tax$Phylum <- gsub("uncultured", "", tax$Phylum)
tax$Class <- gsub("uncultured", "", tax$Class)
tax$Order <- gsub("uncultured", "", tax$Order)
tax$Family <- gsub("uncultured", "", tax$Family)
tax$Genus <- gsub("uncultured", "", tax$Genus)
## Check if there is a species level otherwise add it for consistency
if (!is.null(tax$Species)){
tax$Species <- gsub("s__", "", tax$Species)
} else {
tax$Species <- ""
}
tax[is.na(tax)] <- ""
## How to handle empty taxonomic assignments
if (tax.empty == "OTU"){
for (i in 1:nrow(tax)) {
if (tax[i,"Species"] == "") {tax[i,"Species"] <- rownames(tax)[i]}
if (tax[i,"Genus"] == "") {tax[i,"Genus"] <- rownames(tax)[i]}
if (tax[i,"Family"] == "") {tax[i,"Family"] <- rownames(tax)[i]}
if (tax[i,"Order"] == "") {tax[i,"Order"] <- rownames(tax)[i]}
if (tax[i,"Class"] == "") {tax[i,"Class"] <- rownames(tax)[i]}
if (tax[i,"Phylum"] == "") {tax[i,"Phylum"] <- rownames(tax)[i]}
}
}
## Handle empty taxonomic strings
if(tax.empty == "best"){
tax <- mutate(tax, Kingdom, Kingdom = ifelse(Kingdom == "", "Unclassified", Kingdom)) %>%
mutate(Phylum, Phylum = ifelse(Phylum == "", paste("k__", Kingdom, "_", rownames(tax), sep = ""), Phylum)) %>%
mutate(Class, Class = ifelse(Class == "", ifelse(grepl("__", Phylum), Phylum, paste("c__", Phylum, "_", rownames(tax), sep = "")), Class)) %>%
mutate(Order, Order = ifelse(Order == "", ifelse(grepl("__", Class), Class, paste("c__", Class, "_", rownames(tax), sep = "")), Order)) %>%
mutate(Family, Family = ifelse(Family == "", ifelse(grepl("__", Order), Order, paste("o__", Order, "_", rownames(tax), sep = "")), Family)) %>%
mutate(Genus, Genus = ifelse(Genus == "", ifelse(grepl("__", Family), Family, paste("f__", Family, "_", rownames(tax), sep = "")), Genus)) %>%
mutate(Species, Species = ifelse(Species == "", ifelse(grepl("__", Genus), Genus, paste("g__", Genus, "_", rownames(tax), sep = "")), Species))
}
if(tax.empty == "remove"){
abund <- data[["abund"]]
tax <- subset(tax, tax[,tax.level] != "")
abund <- subset(abund, rownames(abund) %in% rownames(tax))
data[["abund"]] <- abund
}
data[["tax"]] <- tax
return(data)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.