counting unique species in R

yes, i want to do it in R so in order to do use those codes, i have to download the taxonomy.qza?

I tried and i got this error:

Warning messages:
1: In .local(object) : Coercing from data.frame class to character matrix
prior to building taxonomyTable.
This could introduce artifacts.
Check your taxonomyTable, or coerce to matrix manually.
2: In .local(object) : Coercing from data.frame class to character matrix
prior to building taxonomyTable.
This could introduce artifacts.
Check your taxonomyTable, or coerce to matrix manually.
3: In .local(object) : Coercing from data.frame class to character matrix
prior to building taxonomyTable.
This could introduce artifacts.
Check your taxonomyTable, or coerce to matrix manually.

Did you get this error when trying to make a phyloseq object in R? Can you post the codes you used?

ps <- taxonomy #is my taxonomic table

tax_table(ps) %>%
as(“matrix”) %>%
as_tibble(rownames = “Taxon”) %>%
gather(“Rank”, “Name”, rank_names(ps)) %>%
na.omit() %>% # remove rows with NA value
group_by(Rank) %>%
summarize(ntaxa = length(unique(Name))) %>%
mutate(Rank = factor(Rank, rank_names(ps))) %>%
arrange(Rank)

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