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

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

#You don't need to set your working directory because the .qmd file lives in the folder with necessary files already! 
library(BiocManager)
library(phyloseq)
library(Biostrings)
library(dplyr)
library(tidyverse)
library(tibble)
library(microViz)
library(microbiome)
library(ggpubr)

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

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 Alpha- and Beta-Diversity 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 ]
#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))
                     Rhizobium         Variovorax sp900115375 
                             3                              3 
          Palsa-89 sp003166865    13-2-20CM-66-19 sp028293765 
                             4                              4 
     Aquamicrobium_A sediminum           Pseudomonas_E lurida 
                             6                             17 
      Sphingomonas sp000797515    Bradyrhizobium manausense_A 
                            34                             47 
           UBA1930 sp002337145  Sediminibacterium sp000515315 
                            67                            240 
     Paraburkholderia fungorum    Pseudohongiella sp021729085 
                           338                            341 
            Nioella ostreopsis          Hoeflea phototrophica 
                           460                            611 
          Muricauda aquimarina                 Stappia indica 
                           852                           1691 
   Synoicihabitans sp913058185       Qipengyuania sp001542855 
                          1905                           2159 
        Marinobacter adhaerens    Roseibium_497070 alexandrii 
                          2737                           4120 
            Hoeflea alexandrii         Roseibium aggregatum_A 
                          5135                           5845 
            DT-110 sp013214265    Ilumatobacter_A nonamiensis 
                         12511                          12995 
      Thalassobaculum litoreum         Pyruvatibacter mobilis 
                         15667                          16445 
          Roseovarius tolerans    Roseovarius aestuariivivens 
                         22020                          27350 
       Marinobacter flavimaris Phaeodactylibacter sp024742755 
                         28948                          35135 
              MH13 sp017642935 Phaeodactylibacter xiamenensis 
                         35629                          43341 
       Oceanicaulis alexandrii     Rhodophyticola sp013179285 
                         77279                          94443 
       Roseitalea stylonematis         Algoriphagus marincola 
                        174948                         202350 
           SHAN690 sp913057205 
                        212812 
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      Paraburkholderia fungorum 
                           240                            338 
   Pseudohongiella sp021729085             Nioella ostreopsis 
                           341                            460 
         Hoeflea phototrophica           Muricauda aquimarina 
                           611                            852 
                Stappia indica    Synoicihabitans sp913058185 
                          1691                           1905 
      Qipengyuania sp001542855         Marinobacter adhaerens 
                          2159                           2737 
   Roseibium_497070 alexandrii             Hoeflea alexandrii 
                          4120                           5135 
        Roseibium aggregatum_A             DT-110 sp013214265 
                          5845                          12511 
   Ilumatobacter_A nonamiensis       Thalassobaculum litoreum 
                         12995                          15667 
        Pyruvatibacter mobilis           Roseovarius tolerans 
                         16445                          22020 
   Roseovarius aestuariivivens        Marinobacter flavimaris 
                         27350                          28948 
Phaeodactylibacter sp024742755               MH13 sp017642935 
                         35135                          35629 
Phaeodactylibacter xiamenensis        Oceanicaulis alexandrii 
                         43341                          77279 
    Rhodophyticola sp013179285        Roseitalea stylonematis 
                         94443                         174948 
        Algoriphagus marincola            SHAN690 sp913057205 
                        202350                         212812 

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

Pt_Microbiome_PS_Culture_Rare %>% tax_fix() %>% ord_explore()