4 Creating phyloseq Objects
In this section, we demonstrate how to create phyloseq objects to organize and structure microbiome data for analysis. These objects serve as the foundation for downstream analyses and visualization.
4.1 From mothur output
library(tidyverse)
library(phyloseq)
library(microbiome)
library(ape)
metadata <- read_csv("data/mothur/mothur_tidy_metadata.csv",show_col_types = FALSE) %>%
tibble::column_to_rownames("group") %>%
sample_data(metadata)
otutable <- read_csv("data/mothur/mothur_tidy_otutable.csv",show_col_types = FALSE) %>%
pivot_wider(id_cols = group, names_from = OTU, values_from = count) %>%
tibble::column_to_rownames("group") %>%
otu_table(otutable, taxa_are_rows = FALSE)
taxonomy <- read_csv("data/mothur/mothur_tidy_taxonomy.csv", show_col_types = FALSE) %>%
tibble::column_to_rownames("OTU") %>%
as.matrix() %>%
tax_table(taxonomy)
mt_ps_raw_basic <- merge_phyloseq(metadata, otutable, taxonomy)
library(ape)
mt_ps_tree = rtree(ntaxa(mt_ps_raw_basic), rooted=TRUE, tip.label=taxa_names(mt_ps_raw_basic))
mt_ps_raw <- phyloseq::merge_phyloseq(mt_ps_raw_basic, mt_ps_tree)
mt_ps_rel <- phyloseq::transform_sample_counts(mt_ps_raw, function(x){x / sum(x)})
mt_ps_df_raw <- psmelt(mt_ps_raw)
mt_ps_df_rel <- psmelt(mt_ps_raw)
save(mt_ps_tree, mt_ps_raw, mt_ps_rel, mt_ps_df_raw, mt_ps_df_rel, file = "data/mothur_phyloseq_objects.rda")
4.2 From QIIME2 output
library(tidyverse)
library(phyloseq)
library(microbiome)
library(ape)
metadata <- read_csv("data/qiime2/qiime2_tidy_metadata.csv",show_col_types = FALSE) %>%
tibble::column_to_rownames("sample_id") %>%
sample_data(metadata)
otutable <- read_csv("data/qiime2/qiime2_tidy_otutable.csv",show_col_types = FALSE) %>%
pivot_wider(id_cols = sample_id, names_from = feature, values_from = count) %>%
tibble::column_to_rownames("sample_id") %>%
otu_table(otutable, taxa_are_rows = FALSE)
taxonomy <- read_csv("data/qiime2/qiime2_tidy_taxonomy.csv", show_col_types = FALSE) %>%
tibble::column_to_rownames("feature") %>%
as.matrix() %>%
tax_table(taxonomy)
q2_ps_raw_basic <- merge_phyloseq(metadata, otutable, taxonomy)
library(ape)
q2_ps_tree = rtree(ntaxa(q2_ps_raw_basic), rooted=TRUE, tip.label=taxa_names(q2_ps_raw_basic))
q2_ps_raw <- phyloseq::merge_phyloseq(q2_ps_raw_basic, q2_ps_tree)
q2_ps_rel <- phyloseq::transform_sample_counts(q2_ps_raw, function(x){x / sum(x)})
q2_ps_df_raw <- psmelt(q2_ps_raw)
q2_ps_df_rel <- psmelt(q2_ps_raw)
save(q2_ps_tree, q2_ps_raw, q2_ps_rel, q2_ps_df_raw, q2_ps_df_rel, file = "data/qiime2_phyloseq_objects.rda")