counting unique species in R

You need to make a phyloseq object first and then run the R codes you used.

Below is an example based on the metadata, feature table, taxonomy file and rooted-tree from the “Moving Pictures” tutorial.

library(tidyverse) 
library(qiime2R) 
library(phyloseq) 

# Make a phyloseq object in R
metadata <- read_tsv("metadata.tsv", comment = "#q2:type") 

table <- read_qza("table.qza")
count_tab <- table$data %>% as.data.frame() 

taxonomy <- read_qza("taxonomy.qza")
tax_tab <- taxonomy$data %>% 
  as.data.frame() %>%
  separate(Taxon, sep = ";", c("Kingdom", "Phylum", "Class", "Order", "Family", "Genus", "Species")) %>% 
  column_to_rownames("Feature.ID") %>%
  select(-Confidence)

tree <- read_qza("rooted-tree.qza")

ps <- phyloseq(otu_table(as.matrix(count_tab), taxa_are_rows = T),
               phy_tree(tree$data), 
               tax_table(as.matrix(tax_tab)), 
               sample_data(metadata %>% column_to_rownames("sample-id")))

# Compute number of taxa
tax_table(ps) %>%
  as("matrix") %>%
  as_tibble(rownames = "OTU") %>%
  gather("Rank", "Name", rank_names(ps)) %>%
  na.omit() %>% # remove rows with NA value
  group_by(Rank) %>%
  summarize(ntaxa = length(unique(Name))) %>% # compute number of unique taxa
  mutate(Rank = factor(Rank, rank_names(ps))) %>%
  arrange(Rank)

# A tibble: 7 x 2
  Rank    ntaxa
  <fct>   <int>
1 Kingdom     3
2 Phylum     19
3 Class      38
4 Order      60
5 Family    109
6 Genus     198
7 Species   105

-Yanxian

1 Like