Comments (5)
Hi pauGaus,
There is no such option in Fantaxtic, but I've written you some code to do so - see below. I might implement this into the package at some point, but for now, just copy and run the functions below.
Hope that this solves your issue!
require("phyloseq")
require("fantaxtic")
require("tidyverse")
# This function gets the top n taxa for every sample in a phyloseq object
get_top_sample_taxa <- function(ps_obj, n_taxa = 1){
# Make sure taxa are rows
if (!phyloseq::taxa_are_rows(ps_obj)) {
otu_table(ps_obj) <- t(otu_tbl)
}
# Get the top taxa per sample
top_taxa <- otu_table(ps_obj) %>%
data.frame(taxid = row.names(.)) %>%
pivot_longer(!taxid, names_to = "sample_id", values_to = "abundance") %>%
group_by(sample_id) %>%
slice_max(abundance, n = n_taxa) %>%
arrange(desc(abundance)) %>%
mutate(tax_rank = rank(x = -abundance, ties.method = "first", na.last = T))
# Add taxonomic annotations
top_taxa <- ps_obj %>%
tax_table() %>%
data.frame(taxid = row.names(.)) %>%
right_join(top_taxa, by = "taxid")
# Add relative abundance
top_taxa <- ps_obj %>%
sample_sums() %>%
data.frame(sample_id = names(.),
library_size = .) %>%
right_join(top_taxa, by = "sample_id") %>%
rowwise() %>%
mutate(proportion = abundance / library_size)
# Reorder
top_taxa <- top_taxa %>%
select(!library_size) %>%
relocate(sample_id, tax_rank, taxid, abundance, proportion)
return(top_taxa)
}
# This function merges all taxa that are not specified in 'taxa' into a taxon
# named 'other_label' if 'discard_other = FALSE', else it removes them.
collapse_taxa <- function(ps_obj, taxa, discard_other = FALSE, other_label = "Other"){
# Make sure taxa are rows
if (!phyloseq::taxa_are_rows(ps_obj)) {
otu_table(ps_obj) <- t(otu_table(ps_obj)
}
# Merge or discard all other taxa
if (discard_other) {
ps_obj <- phyloseq::prune_taxa(taxa, ps_obj)
} else {
# Merge taxa
to_merge <- phyloseq::taxa_names(ps_obj)
to_merge <- to_merge[!(to_merge %in% taxa)]
ps_obj <- merge_taxa(ps_obj, to_merge)
# Update the taxon name to other_label
tax_tbl <- phyloseq::tax_table(ps_obj)
indx <- which(row.names(tax_tbl) %in% to_merge)
tax_tbl[indx, ] <- other_label
phyloseq::tax_table(ps_obj) <- tax_tbl
}
return(ps_obj)
}
# Load data
data(GlobalPatterns)
ps_obj <- GlobalPatterns
# Get the top n taxa per sample
top_taxa <- get_top_sample_taxa(ps_obj, n_taxa = 3)
top_taxa
### If you want to plot with fantaxtic, run the steps below ###
# Collapse taxa
ps_obj <- collapse_taxa(ps_obj, top_taxa$taxid)
# Generate labels
ps_obj <- name_taxa(ps_obj, label = "Unkown", species = T, other_label = "Other")
# Plot
fantaxtic_bar(ps_obj, color_by = "Phylum", label_by = "Species", other_label = "Other")
from fantaxtic.
Let me know if this was useful.
from fantaxtic.
Thank you so much for sending this code. I actually found some other code that worked before you sent this, so I decided not to risk confusing myself by trying yours! Thank you.
from fantaxtic.
I'm glad to hear that you found a solution to your problem. Would you mind trying out the code above to see if it does what you want? I'd like to implement it into the package, but before I do so, any feedback would be appreciated.
from fantaxtic.
This issue has now been resolved. To get the top taxa per sample, run:
data(GlobalPatterns)
top <- top_taxa(GlobalPatterns, n_taxa = 1, grouping = "sample_id")
from fantaxtic.
Related Issues (20)
- label_duplicate_taxa does not generate unique labels HOT 2
- 2-level relative abundance bar plot of microbial metagenomics data: help with code HOT 7
- Nested_top_taxa HOT 2
- Bold legend titles fails when changing theme HOT 1
- Change X axis order with fantaxtic_bar HOT 3
- plot_nested_bar: Specify colors for each bacterial order HOT 2
- Relative abundance with others HOT 1
- nested_top_taxa HOT 6
- How can I group samples by fixed effects on the x axis instead of the model? HOT 3
- a
- How to install fantaxtic package? HOT 1
- Working with a taxonomic subset? HOT 3
- Most abundant genera across a complete phyloseq object HOT 5
- Change legend text size HOT 1
- Sorting stacked barplot based on abundance values HOT 1
- Color palette, rgb, color name etc... or possible insertion from other packages? HOT 9
- Unable to install Fantaxtic HOT 2
- n_taxa = is not working for me HOT 2
- Names must be unique issue HOT 2
- {X} values required in clr.pal, {<X} provided HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fantaxtic.