gmteunisse / fantaxtic

Fantaxtic - Nested Bar Plots for Phyloseq Data
26 stars 3 forks source link

Working with a taxonomic subset? #25

Closed lxsteiner closed 7 months ago

lxsteiner commented 7 months ago

Hi, thank you for the package, it's very easy and useful for manipulating the top ranked groups in a dataset!

Is it possible to work with a taxonomic subset, coupled with the core function of picking out the most abundant features, from within fantaxtic while retaining all the other (relative) abundance information of groups that were not subsetted?

E.g. from the GlobalPatterns dataset, the 10 most abundant ASVs are phyla that include Cyanobacteria. Now, only from Cyanobacteria pick the top 20 most abundant ASVs, plot a nested bar with top_level = "Class", nested_level = "Species" so that all other (abundant and non abundant) features that were not subsetted are placed in other?

I know how to subset specific taxonomic groups from a phyloseq object, but the total rel. abundance information is clearly not retained then. Even if do export the relative abundance instead of raw counts, or manually change the abundance values in a top_taxa dataframe, not sure if fantaxtic would accept those if the assumption is that the sum should always be 1?

Thanks!

gmteunisse commented 7 months ago

Thanks for using fantaxtic! There's no native implementation of what you want to do, but you can create such an analysis using the code below. Let me know if that works for you.

require("fantaxtic")
require("phyloseq")
require("tidyverse")

# Load the data
data(GlobalPatterns)
ps <- GlobalPatterns

# Merge all non-cyanobacteria
cyano <- phyloseq::subset_taxa(ps, Phylum == "Cyanobacteria")
cyano_ids <- phyloseq::taxa_names(cyano) %>% as.integer()
ps_merged <- fantaxtic::collapse_taxa(ps, cyano_ids, discard_other = FALSE)

# Plot the top 10 ASVs (one of which is Other)
top_asv <- top_taxa(ps_merged, n_taxa = 10, merged_label = "Other cyanobacteria")
plot_nested_bar(ps_obj = top_asv$ps_obj,
                top_level = "Phylum",
                nested_level = "Species")

Created on 2023-12-03 by the reprex package (v2.0.1)

lxsteiner commented 7 months ago

Wonderful! Thank you, exactly what I was looking for.

Is it possible to pass color arguments to the nested tax. level via palette or taxon_colours? When I was testing things, I only managed to change top tax. level colors via either, meaning the nested tax. level would just assume gradients of the specified color(s).
E.g. want to color the top 10 species in Cyanobacteria now as c("#fff100", "#ff8c00", "#e81123", "#ec008c", "#68217a", "#00188f", "#00bcf2", "#00b294", "#009e49", "#bad80a") or even just change/define the color of a single species.

I also tried creating a plot object then replacing some of the defined colors in [["plot_env"]][["vals"]] or [["plot_env"]][["colours"]] or [["data"]][["subgroup_colour"]] and others, but it doesn't seem to work that way...

Thanks!

gmteunisse commented 7 months ago

Glad it solved your problem. You can only change the top-level taxon colours, but you can change the top level from phylum to something lower (e.g. class) to obtain more differentiation. If you want to use a single colour per ASV, you should use the regular phyloseq::plot_bar function - it will accept the top_asv$ps_obj object.