microsud / microbiomeutilities

The is mostly a wrapper tool using phyloseq and microbiome R packages.
https://microsud.github.io/microbiomeutilities/
Other
33 stars 7 forks source link

double genus name in heatmap #181

Closed najibveto closed 2 years ago

najibveto commented 2 years ago

hello, I generate the heatmap using the following code:

library(microbiomeutilities)
library(microbiome)
library(knitr)
library(tibble)
library(dplyr)
library(pheatmap)
library(RColorBrewer)
ps <-phyloseq
tax_tb <- as(tax_table(ps),"matrix") %>% 
  as.data.frame() %>% 
  rownames_to_column("ASV") %>% 
  mutate(Genus.Species = ifelse(!is.na(Species), paste0(Genus, ".", Species), Species)) %>% 
  select(-Species)   %>% 
  rename(Species = Genus.Species)

#tax_tb[1:30, 5:9]
rownames(tax_tb) <- tax_tb$ASV
tax_tb <- tax_tb[,-1]

tax_table(ps) <- tax_table(as.matrix(tax_tb))

ps.genus <- aggregate_taxa(ps, "Genus")
display.brewer.all()
grad_ab <- brewer.pal(11, "Spectral")
grad_ab_pal <- grad_ab 
gray_grad <- colorRampPalette(c("white", "steelblue"))
gray_grad_cols <- gray_grad(10)
meta_colors <- list(c("winter" = "#8dd3c7", 
                      "spring" = "#fb8072", 
                      "summer"="#b3de69",
                      "autumn"="#fbb4ae"))
names(meta_colors) <- c("season")
p <- plot_taxa_heatmap(ps.genus,
                       subset.top = 30,
                       VariableA = c("season"),
                       heatcolors = grad_ab_pal, 
                       transformation = "clr",
                       cluster_rows = T,
                       cluster_cols = T,
                       show_colnames = T,
                       annotation_colors=meta_colors)

however, the column names are double instead of single name: Rplot42 how to correct the code to have only a single name? the taxa table is as follow: image

thank you.

microsud commented 2 years ago

Hi @najibveto , The plot_taxa_heatmap is only for OTU/ASV level data. It does not support aggregated data. If you want to have genus-level data with clustering you can try our new TreeSE based tools here: https://microbiome.github.io/OMA/viz-chapter.html#viz-chapter. If you want to do it in phyloseq, then I have an in-house set of tools which are focused for our data analysis but should work widely. https://github.com/RIVM-IIV-Microbiome/biomeUtils and https://github.com/RIVM-IIV-Microbiome/biomeViz are under development. This plot function relies on TidyHeatmap and ComplexHeatmap

# install two tools
devtools::install_github("RIVM-IIV-Microbiome/biomeUtils")
devtools::install_github("RIVM-IIV-Microbiome/biomeViz")
# load 
library(biomeUtils)
library(biomeViz)
library(microbiome)
library(dplyr)
data("FuentesIliGutData") # example data
ps <- FuentesIliGutData
# ps <- phyloseq
# tax_tb <- as(tax_table(ps),"matrix") %>% 
#   as.data.frame() %>% 
#   rownames_to_column("ASV") %>% 
#   mutate(Genus.Species = ifelse(!is.na(Species), paste0(Genus, ".", Species), Species)) %>% 
#   select(-Species)   %>% 
#   rename(Species = Genus.Species)
# 
# #tax_tb[1:30, 5:9]
# rownames(tax_tb) <- tax_tb$ASV
# tax_tb <- tax_tb[,-1]
# 
# tax_table(ps) <- tax_table(as.matrix(tax_tb))
# you can input your phyloseq here
top_genera_w_mean_relabund <-  ps |> 
  aggregate_taxa("Genus") |> 
  microbiome::transform("compositional") |> 
  findTopTaxa(top= 30, method="mean")
# check the top five
top_genera_w_mean_relabund[1:5]
# For CLR-based plot
ps.clr <- ps %>%
  aggregate_taxa("Genus") |> 
  microbiome::transform("clr")
# Select top taxa to plot 
ps.clr.top.30 <- phyloseq::prune_taxa(top_genera_w_mean_relabund, ps.clr)

plotTidyHeatmap(ps.clr.top.30, 
                select_taxa = top_genera_w_mean_relabund,
                group_samples_by = "ILI",
                add_taxa_label = FALSE,
                cluster_rows = FALSE,
                .scale = "none",
                transform = NULL,
                palette_value = c("red", "white", "blue"),
                # Adjust taxa name size
                row_names_gp = grid::gpar(fontsize = 6, fontface="italic"))

image

Hope this helps. PS: You should think of cleaning your taxa table for cleaner and clearer outputs. Cheers, Sudarshan