Phyloseq Tutorial

Instructions on how to use Phyloseq with a Phaeodactylum tricornutum microbiome quantitative stable isotope probing (qSIP) PacBio 16S dataset.

Author

Kelly C. Shannon

Published

November 18, 2026

Data Folder for the Tutorial:

install.packages("BiocManager")
PS_packages <- c("phyloseq", "microViz", "microbiome")
BiocManager::install(PS_packages)

#Set your working directory to where folder containing the .qmd file!
library(BiocManager)
library(phyloseq)
library(Biostrings)
library(dplyr)
library(tidyverse)
library(tibble)
library(microViz)
library(microbiome)
library(ggpubr)
library(patchwork)

Prepare the Phyloseq Object

Remember, Phyloseq is picky with formatting!

Format:

  • OTU (or ASV) Count Table: Matrix

  • Taxonomy Table: Matrix

    • Note that Domain in this column in this data is actually the database used to annotate the ASV.
  • Sample Data (or Metadata) Table: Data Frame

A helpful graphic from Holmes and McMurdie (2013) for remembering this structure:

Figure 3 from the PLOS One paper
Code to read in and format data for Phyloseq
Pt_taxa <- as.data.frame(read.csv("Pt_microbiome_taxa.csv", row.names = 1))
Pt_counts <- as.data.frame(read.csv("Pt_microbiome_counts.csv", row.names = 1))
Pt_meta <- as.data.frame(read.csv("Pt_microbiome_metadata.csv", row.names = 1))

dim(Pt_taxa)
dim(Pt_counts)
dim(Pt_meta)

head(Pt_taxa)
head(Pt_counts)
head(Pt_meta)

#Set count table rownames to ASVs
taxa <- rownames(Pt_counts)
taxa_ASV_names <- taxa %>% str_replace_all("sp", "ASV")
rownames(Pt_counts) <- taxa_ASV_names #This has to be the same as the taxonomy table
rownames(Pt_taxa) <- rownames(Pt_counts)

samples <- rownames(Pt_meta)
Pt_meta$"Sample" <- samples
if (all(colnames(Pt_counts) == samples)) {
  print("Good to go")
} else {
  print("The count and metadata tables do not have equal sample data")
}
colnames(Pt_counts) <- Pt_meta$Sample 
  • The OTU table column names have to equal the sample data rownames.

    • If taxa are rows in the OTU table.

Now let’s build the Phyloseq object with the taxonomy, OTU, and metadata tables.

  • You can also add a phylogenetic tree and fasta sequences to the object, but we won’t cover that here.
Code to check the classes of the Phyloseq object files and building the object
if (class(Pt_taxa) == "data.frame") {
  Pt_taxa_mat <- as.matrix(Pt_taxa)
} else{
  print("Not a data frame")
}

if (class(Pt_counts) == "data.frame") {
  Pt_counts_mat <- as.matrix(Pt_counts)
} else{
  print("Not a data frame")
}

if (class(Pt_meta) == "data.frame") {
  print("Good to go!")
} else{
  print("You need a data frame")
}

Pt_Microbiome_PS <- phyloseq(otu_table(Pt_counts_mat, taxa_are_rows = TRUE), 
                             tax_table(Pt_taxa_mat), 
                             sam_data(Pt_meta))
Pt_Microbiome_PS
Code to examine Phyloseq object contents
Pt_Microbiome_PS@otu_table
Pt_Microbiome_PS@tax_table
Pt_Microbiome_PS@sam_data
Pt_Microbiome_PS@refseq
Pt_Microbiome_PS@phy_tree

#You could re-save these contents as files like this: 

Pt_Microbiome_PS_taxa <- data.frame(Pt_Microbiome_PS@tax_table)
head(Pt_Microbiome_PS_taxa)
class(Pt_Microbiome_PS_taxa)

Phyloseq Microbiome Analysis

Preparing the Phyloseq object for analyses

  • Do you spot any issues with the present taxa?
#Check for contaminant species
Pt_Microbiome_PS_sp <- Pt_Microbiome_PS %>% 
  microbiome::aggregate_taxa(level = "Species")
Pt_Microbiome_PS_sp
phyloseq-class experiment-level object
otu_table()   OTU Table:         [ 40 taxa and 129 samples ]
sample_data() Sample Data:       [ 129 samples by 11 sample variables ]
tax_table()   Taxonomy Table:    [ 40 taxa by 8 taxonomic ranks ]
Species_DF <- data.frame(Pt_Microbiome_PS_sp@tax_table)
sort(unique(Species_DF$Species)) #Easier to look at this in the console
 [1] "13-2-20CM-66-19 sp028293765"    "Algoriphagus marincola"        
 [3] "Aquamicrobium_A sediminum"      "Bradyrhizobium manausense_A"   
 [5] "DT-110 sp013214265"             "Escherichia coli"              
 [7] "Hoeflea alexandrii"             "Hoeflea phototrophica"         
 [9] "Ilumatobacter_A nonamiensis"    "Marinobacter adhaerens"        
[11] "Marinobacter flavimaris"        "MH13 sp017642935"              
[13] "Muricauda aquimarina"           "Nioella ostreopsis"            
[15] "Oceanicaulis alexandrii"        "Palsa-89 sp003166865"          
[17] "Paraburkholderia fungorum"      "Phaeodactylibacter sp024742755"
[19] "Phaeodactylibacter xiamenensis" "Pseudohongiella sp021729085"   
[21] "Pseudomonas_E lurida"           "Pt Chloroplast"                
[23] "Pyruvatibacter mobilis"         "Qipengyuania sp001542855"      
[25] "Rhizobium"                      "Rhodophyticola sp013179285"    
[27] "Roseibium aggregatum_A"         "Roseibium_497070 alexandrii"   
[29] "Roseitalea stylonematis"        "Roseovarius aestuariivivens"   
[31] "Roseovarius tolerans"           "Sediminibacterium sp000515315" 
[33] "SHAN690 sp913057205"            "Sphingomonas sp000797515"      
[35] "Stappia indica"                 "Synoicihabitans sp913058185"   
[37] "Thalassobaculum litoreum"       "UBA1930 sp002337145"           
[39] "Unassigned"                     "Variovorax sp900115375"        
unwanted_species <- c("Escherichia coli", "Pt Chloroplast", "Unassigned")

Pt_Microbiome_PS_sp_clean <- Pt_Microbiome_PS_sp %>% 
  subset_taxa(!Species %in% unwanted_species) #Trim away these taxa we don't want
Pt_Microbiome_PS_sp_clean
phyloseq-class experiment-level object
otu_table()   OTU Table:         [ 37 taxa and 129 samples ]
sample_data() Sample Data:       [ 129 samples by 11 sample variables ]
tax_table()   Taxonomy Table:    [ 37 taxa by 8 taxonomic ranks ]
#Since this is qSIP data, we don't want to merge all the density bins into their original source tubes (each from one single culture)--biological replicates in Iron-Replete and Iron-Deplete cultures (13C/12C-bicarbonate added to cultures count as additional replicates).
Pt_Microbiome_PS_Culture <- Pt_Microbiome_PS_sp_clean %>% 
  merge_samples("Source", fun = "sum")
Pt_Microbiome_PS_Culture
phyloseq-class experiment-level object
otu_table()   OTU Table:         [ 37 taxa and 12 samples ]
sample_data() Sample Data:       [ 12 samples by 11 sample variables ]
tax_table()   Taxonomy Table:    [ 37 taxa by 8 taxonomic ranks ]
#For some reason, this screws up some of the variables, so let's add them back

Sample <- rownames(Pt_Microbiome_PS_Culture@sam_data) #This variable will be corrected later on
dput(Sample) #Copy this to easily fill in new variables manually
c("HA", "HB", "HC", "HD", "HE", "HF", "LA", "LB", "LC", "LD", 
"LE", "LF")
Iron <- c("High", "High", "High", "High", "High", "High", 
          "Low", "Low", "Low", "Low", "Low", "Low")

sample_data(Pt_Microbiome_PS_Culture)$Iron <- Iron
sample_data(Pt_Microbiome_PS_Culture)$Iron #Check that it worked
 [1] "High" "High" "High" "High" "High" "High" "Low"  "Low"  "Low"  "Low" 
[11] "Low"  "Low" 
#Now let's rarefy the object and remove very low-count taxa
sort(sample_sums(Pt_Microbiome_PS_sp_clean))
    LD9     LF9     LC9     LD8     LF8     LB9     LE9     LC8     LA9     LB8 
   1053    1157    1176    1229    1251    1318    1417    1434    1483    1494 
    LA8     LE8    LA10    LB10    LD10    LF10    LC10     LA7     LE7    LE10 
   1569    1593    1970    2259    2368    2615    2742    4249    4301    4397 
    LB7     LC7   LE11p   LD11p   LA11p   LC11p   LB11p   LF11p     LD7     HD7 
   4549    4606    4691    4743    4810    5155    5156    5192    5356    5557 
    HB8     HD8     LF7     HE8     HB9     HF8     HC8     HE9    HD1p    HE10 
   5639    5761    6044    6202    7330    7395    7654    7982    8138    8430 
   HF1p     HD9     HE7     HC9    HC1p    HB10    HE1p     HD2    LF1p    LE1p 
   8682    9158    9363    9445   10121   10397   10446   10655   10839   10875 
    LA6     HC2     HF2   HD10p    HB1p   HE11p   HF11p    HC10     LE6     HC7 
  10882   10917   10982   11180   11304   11346   11587   11938   12085   12170 
    HD6     LC6     HF7    LC1p     HA8     HE2    LD1p    HA10   HB11p     HB2 
  12377   12420   12490   12511   12606   12852   12871   12880   12931   13058 
    HC3    LA1p     LB6     HA9   HC11p     HF3     HB7 LE2_LE3     LC3     LC2 
  13067   13075   13136   13144   13293   13586   13599   13607   13891   13979 
    LF2     HE6     LA2     LF6     LD6    LB1p     LD3     HB3     LD2    HA11 
  14076   14286   14608   14616   14616   14627   14652   14655   15274   15286 
    LA3     LF3     HE3     HA2    HA1p     LA5     HF6     LB2     HD3     HF4 
  15356   15383   15624   15645   15723   15789   15851   15910   15918   16148 
    HC4     LA4     HF5     LE4     LC4     HA7     HA3     LB3     HC6     LC5 
  16209   16330   16386   16408   16513   16697   16702   16760   16855   17046 
    LB4     LF4     LE5   HA12p     HE4     HD5     LD4     HB4     LB5     HE5 
  17068   17137   17167   17247   17796   17821   17823   18151   18182   18245 
    HA4     HC5     LD5     LF5     HD4     HB6     HA6     HB5     HA5 
  18325   18512   18719   18729   19164   19511   19992   20240   20557 
sort(sample_sums(Pt_Microbiome_PS_Culture))
    LE     LA     LC     LF     LD     LB     HF     HD     HE     HC     HB 
 86541 100121 101473 107039 108704 110459 113107 115729 132572 140181 146815 
    HA 
194804 
mindepth <- min(sample_sums(Pt_Microbiome_PS_Culture))
Pt_Microbiome_PS_Culture_Rare <- Pt_Microbiome_PS_Culture %>% 
  rarefy_even_depth(sample.size = mindepth, replace = FALSE)

sort(sample_sums(Pt_Microbiome_PS_Culture_Rare))
   HA    HB    HC    HD    HE    HF    LA    LB    LC    LD    LE    LF 
86541 86541 86541 86541 86541 86541 86541 86541 86541 86541 86541 86541 
sort(taxa_sums(Pt_Microbiome_PS_Culture_Rare))
          Palsa-89 sp003166865                      Rhizobium 
                             3                              4 
   13-2-20CM-66-19 sp028293765         Variovorax sp900115375 
                             4                              5 
     Aquamicrobium_A sediminum           Pseudomonas_E lurida 
                             6                             15 
      Sphingomonas sp000797515    Bradyrhizobium manausense_A 
                            43                             55 
           UBA1930 sp002337145  Sediminibacterium sp000515315 
                            59                            228 
   Pseudohongiella sp021729085      Paraburkholderia fungorum 
                           332                            334 
            Nioella ostreopsis          Hoeflea phototrophica 
                           458                            635 
          Muricauda aquimarina                 Stappia indica 
                           829                           1673 
   Synoicihabitans sp913058185       Qipengyuania sp001542855 
                          1900                           2078 
        Marinobacter adhaerens    Roseibium_497070 alexandrii 
                          2756                           4088 
            Hoeflea alexandrii         Roseibium aggregatum_A 
                          5096                           5758 
            DT-110 sp013214265    Ilumatobacter_A nonamiensis 
                         12507                          13137 
      Thalassobaculum litoreum         Pyruvatibacter mobilis 
                         15542                          16331 
          Roseovarius tolerans    Roseovarius aestuariivivens 
                         21987                          27366 
       Marinobacter flavimaris Phaeodactylibacter sp024742755 
                         28938                          35285 
              MH13 sp017642935 Phaeodactylibacter xiamenensis 
                         35813                          43353 
       Oceanicaulis alexandrii     Rhodophyticola sp013179285 
                         77559                          94296 
       Roseitalea stylonematis         Algoriphagus marincola 
                        174888                         202513 
           SHAN690 sp913057205 
                        212618 
Pt_Microbiome_PS_Culture_Rare <- prune_taxa(taxa_sums(Pt_Microbiome_PS_Culture_Rare) > 100,
                                            Pt_Microbiome_PS_Culture_Rare)

sort(taxa_sums(Pt_Microbiome_PS_Culture_Rare))
 Sediminibacterium sp000515315    Pseudohongiella sp021729085 
                           228                            332 
     Paraburkholderia fungorum             Nioella ostreopsis 
                           334                            458 
         Hoeflea phototrophica           Muricauda aquimarina 
                           635                            829 
                Stappia indica    Synoicihabitans sp913058185 
                          1673                           1900 
      Qipengyuania sp001542855         Marinobacter adhaerens 
                          2078                           2756 
   Roseibium_497070 alexandrii             Hoeflea alexandrii 
                          4088                           5096 
        Roseibium aggregatum_A             DT-110 sp013214265 
                          5758                          12507 
   Ilumatobacter_A nonamiensis       Thalassobaculum litoreum 
                         13137                          15542 
        Pyruvatibacter mobilis           Roseovarius tolerans 
                         16331                          21987 
   Roseovarius aestuariivivens        Marinobacter flavimaris 
                         27366                          28938 
Phaeodactylibacter sp024742755               MH13 sp017642935 
                         35285                          35813 
Phaeodactylibacter xiamenensis        Oceanicaulis alexandrii 
                         43353                          77559 
    Rhodophyticola sp013179285        Roseitalea stylonematis 
                         94296                         174888 
        Algoriphagus marincola            SHAN690 sp913057205 
                        202513                         212618 

Now let’s do some alpha- and beta-diversity exploring with MicroViz

  • The code for MicroViz plots generated in the GUI can be copied, pasted, and modified in RStudio!
Pt_Microbiome_PS_Culture_Rare %>% tax_fix() %>% ord_explore()

Some example alpha-div. plots using the MicroViz and Microbiome packages

Making alpha-div. figures
#Diversity (applies to sam_data)
Pt_Microbiome_Div_DF <- Pt_Microbiome_PS_Culture_Rare %>% 
  microbiome::diversity(index = "all")

Pt_Meta_PS <- data.frame(Pt_Microbiome_PS_Culture_Rare@sam_data)
Pt_Meta_PS_Div <- cbind(Pt_Meta_PS, Pt_Microbiome_Div_DF) %>% 
  dplyr::select(-Sample) %>% 
  rownames_to_column("Sample")

Shanplot <- Pt_Meta_PS_Div %>% ggplot(aes(x = Sample, y = shannon)) +
  geom_point(size = 4, shape = 21, color = "black", aes(fill = Iron)) + 
  theme_classic2() +
  scale_fill_manual(values = c("purple", "darkgreen")) +
  scale_y_continuous(limits = c(2, 2.5), 
                     breaks = c(2.1, 2.2, 2.3, 2.4, 2.5)) +
  labs(y = "Shannon Diversity", x = "") +
  theme(axis.text = element_text(size = 12, color = "black"), 
        legend.text = element_text(size = 12, color = "black"))

Simpplot <- Pt_Meta_PS_Div %>% ggplot(aes(x = Sample, y = inverse_simpson)) +
  geom_point(size = 4, shape = 21, color = "black", aes(fill = Iron)) + 
  theme_classic2() +
  scale_fill_manual(values = c("purple", "darkgreen")) +
  scale_y_continuous(limits = c(6.2, 8.4), 
                     breaks = seq(6.2, 8.4, by = 0.4)) +
  labs(y = "Inv. Simpson Diversity", x = "") +
  theme(axis.text = element_text(size = 12, color = "black"), 
        legend.text = element_text(size = 12, color = "black"))

(Shanplot + Simpplot) +
  plot_layout(guides = "collect") + #Add one shared legend
  plot_annotation(tag_levels = "A", tag_suffix = ".") #Control your caption labels

Two dot plots of alpha-diversity across culture replicates

Alpha diversity measurements of the P. tricornutum enrichment community without the inclusion of chloroplast sequences, colored by Iron availability. A. is Shannon diversity and B. is inverse Simpson diversity—each showing richness and evenness of heterotroph species.