Paul J. McMurdie and Susan Holmes

phyloseq Home Page

If you find phyloseq and/or its tutorials useful, please acknowledge and cite phyloseq in your publications:

phyloseq: An R package for reproducible interactive analysis and graphics of microbiome census data (2013) PLoS ONE 8(4):e61217

1 Other resources

The phyloseq project also has a number of supporting online resources, most of which can by found at the phyloseq home page, or from the phyloseq stable release page on Bioconductor.

To post feature requests or ask for help, try the phyloseq Issue Tracker.

2 The experimental data used in this example

In this example I use the publicly available data from a study on colorectal cancer:

Genomic analysis identifies association of Fusobacterium with colorectal carcinoma. Kostic, A. D., Gevers, D., Pedamallu, C. S., Michaud, M., Duke, F., Earl, A. M., et al. (2012). Genome research, 22(2), 292-298.

As a side-note, this work was published ahead of print in Genome Research alongside a highly-related article from a separate group of researchers (long-live reproducible observations!): Fusobacterium nucleatum infection is prevalent in human colorectal carcinoma. In case you are interested. For the purposes of example, however, we will stick to the data from the former study, with data available at the server.

Data source, from methods section in article:

The 16S gene data set consists of 454 FLX Titanium sequences spanning the V3 to V5 variable regions obtained for 190 samples (95 pairs). Detailed protocols used for 16S amplification and se- quencing are available on the HMP Data Analysis and Coordination Center website ( protocols.php).

Study ID: 1457

Project Name: Kostic_colorectal_cancer_fusobacterium

Study Abstract:

The tumor microenvironment of colorectal carcinoma is a complex community of genomically altered cancer cells, nonneoplastic cells, and a diverse collection of microorganisms. Each of these components may contribute to carcino genesis; however, the role of the microbiota is the least well understood. We have characterized the composition of the microbiota in colorectal carcinoma using whole genome sequences from nine tumor/normal pairs. Fusobacterium sequences were enriched in carcinomas, confirmed by quantitative PCR and 16S rDNA sequence analysis of 95 carcinoma/normal DNA pairs, while the Bacteroidetes and Firmicutes phyla were depleted in tumors. Fusobacteria were also visualized within colorectal tumors using FISH. These findings reveal alterations in the colorectal cancer microbiota; however, the precise role of Fusobacteria in colorectal carcinoma pathogenesis requires further investigation.

3 Import data with phyloseq, convert to DESeq2

Start by loading phyloseq.

library("phyloseq"); packageVersion("phyloseq")
## [1] '1.20.0'

Defined file path, and import the published OTU count data into R.

filepath = system.file("extdata", "", package="phyloseq")
kostic = microbio_me_qiime(filepath)
## Found biom-format file, now parsing it... 
## Done parsing biom... 
## Importing Sample Metdadata from mapping file...
## Merging the imported objects... 
## Successfully merged, phyloseq-class created. 
##  Returning...

Here I had to use a relative file path so that this example works on all systems that have phyloseq installed. In practice, your file path will look like this (if you’ve downloaded the data ahead of time):

filepath = "~/Downloads/"
kostic = microbio_me_qiime(filepath)

Or like this (if you’re accessing data directly from the server directly):

kostic = microbio_me_qiime(1457)

4 Convert to DESeq2’s DESeqDataSet class

In this example I’m using the major sample covariate, DIAGNOSIS, as the study design factor. The focus of this study was to compare the microbiomes of pairs of healthy and cancerous tissues, so this makes sense. Your study could have a more complex or nested design, and you should think carefully about the study design formula, because this is critical to the test results and their meaning. You might even need to define a new factor if none of the variables in your current table appropriately represent your study’s design. See the DESeq2 home page for more details.

Here is the summary of the data variable kostic that we are about to use, as well as the first few entries of the DIAGNOSIS factor.

## phyloseq-class experiment-level object
## otu_table()   OTU Table:         [ 2505 taxa and 190 samples ]
## sample_data() Sample Data:       [ 190 samples by 71 sample variables ]
## tax_table()   Taxonomy Table:    [ 2505 taxa by 7 taxonomic ranks ]
head(sample_data(kostic)$DIAGNOSIS, 10)
##  [1] Healthy Tumor   Tumor   Healthy Healthy Healthy Tumor   Healthy
##  [9] Healthy Healthy
## Levels: Healthy None Tumor

5 DESeq2 conversion and call

First load DESeq2.

library("DESeq2"); packageVersion("DESeq2")
## [1] '1.16.0'

The following two lines actually do all the complicated DESeq2 work. The function phyloseq_to_deseq2 converts your phyloseq-format microbiome data into a DESeqDataSet with dispersions estimated, using the experimental design formula, also shown (the ~DIAGNOSIS term). The DESeq function does the rest of the testing, in this case with default testing framework, but you can actually use alternatives.

First remove the 5 samples that had no DIAGNOSIS attribute assigned. These introduce a spurious third design class that is actually a rare artifact in the dataset. Also remove samples with less than 500 reads (counts). Note that this kind of data cleanup is useful, necessary, and should be well-documented because it can also be dangerous to alter or omit data without clear documentation. In this case I actually explored the data first, and am omitting some of the details (and explanatory plots) here for clarity.

kostic <- subset_samples(kostic, DIAGNOSIS != "None")
kostic <- prune_samples(sample_sums(kostic) > 500, kostic)
## phyloseq-class experiment-level object
## otu_table()   OTU Table:         [ 2505 taxa and 177 samples ]
## sample_data() Sample Data:       [ 177 samples by 71 sample variables ]
## tax_table()   Taxonomy Table:    [ 2505 taxa by 7 taxonomic ranks ]
diagdds = phyloseq_to_deseq2(kostic, ~ DIAGNOSIS)
# calculate geometric means prior to estimate size factors
gm_mean = function(x, na.rm=TRUE){
  exp(sum(log(x[x > 0]), na.rm=na.rm) / length(x))
geoMeans = apply(counts(diagdds), 1, gm_mean)
diagdds = estimateSizeFactors(diagdds, geoMeans = geoMeans)
diagdds = DESeq(diagdds, fitType="local")

Note: The default multiple-inference correction is Benjamini-Hochberg, and occurs within the DESeq function.

6 Investigate test results table

The following results function call creates a table of the results of the tests. Very fast. The hard work was already stored with the rest of the DESeq2-related data in our latest version of the diagdds object (see above). I then order by the adjusted p-value, removing the entries with an NA value. The rest of this example is just formatting the results table with taxonomic information for nice(ish) display in the HTML output.

res = results(diagdds)
res = res[order(res$padj, na.last=NA), ]
alpha = 0.01
sigtab = res[(res$padj < alpha), ]
sigtab = cbind(as(sigtab, "data.frame"), as(tax_table(kostic)[rownames(sigtab), ], "matrix"))
##          baseMean log2FoldChange     lfcSE      stat       pvalue
## 64396  181.564648       2.195547 0.4282121  5.127241 2.940184e-07
## 72853   28.633097      -1.604873 0.3310712 -4.847515 1.250173e-06
## 374052  75.002469       2.523486 0.5411855  4.662885 3.118072e-06
## 307981   3.258919       2.153038 0.4832019  4.455772 8.359169e-06
## 180285 168.356513      -1.204357 0.2782362 -4.328542 1.501000e-05
##                padj  Kingdom         Phylum                Class
## 64396  0.0007238734 Bacteria   Fusobacteria Fusobacteria (class)
## 72853  0.0015389636 Bacteria     Firmicutes           Clostridia
## 374052 0.0025588980 Bacteria   Fusobacteria Fusobacteria (class)
## 307981 0.0051450684 Bacteria Proteobacteria  Gammaproteobacteria
## 180285 0.0073909244 Bacteria     Firmicutes           Clostridia
##                    Order             Family            Genus Species
## 64396    Fusobacteriales   Fusobacteriaceae    Fusobacterium    <NA>
## 72853      Clostridiales    Ruminococcaceae Faecalibacterium    <NA>
## 374052   Fusobacteriales   Fusobacteriaceae    Fusobacterium    <NA>
## 307981 Enterobacteriales Enterobacteriaceae       Klebsiella    <NA>
## 180285     Clostridiales    Ruminococcaceae Faecalibacterium    <NA>

Let’s look at just the OTUs that were significantly enriched in the carcinoma tissue. First, cleaning up the table a little for legibility.

posigtab = sigtab[sigtab[, "log2FoldChange"] > 0, ]
posigtab = posigtab[, c("baseMean", "log2FoldChange", "lfcSE", "padj", "Phylum", "Class", "Family", "Genus")]
OTU baseMean log2FoldChange lfcSE padj Phylum Class Family Genus
64396 181.564648 2.195547 0.4282121 0.0007238734 Fusobacteria Fusobacteria (class) Fusobacteriaceae Fusobacterium
374052 75.002469 2.523486 0.5411855 0.0025588980 Fusobacteria Fusobacteria (class) Fusobacteriaceae Fusobacterium
307981 3.258919 2.153038 0.4832019 0.0051450684 Proteobacteria Gammaproteobacteria Enterobacteriaceae Klebsiella

As expected from the original study abstract and title, a Fusobacterium OTU was among the most-significantly differentially abundant between the cancerous and healthy samples.

7 Plot Results

Here is a bar plot showing the log2-fold-change, showing Genus and Phylum. Uses some ggplot2 commands.

sigtabgen = subset(sigtab, !
# Phylum order
x = tapply(sigtabgen$log2FoldChange, sigtabgen$Phylum, function(x) max(x))
x = sort(x, TRUE)
sigtabgen$Phylum = factor(as.character(sigtabgen$Phylum), levels=names(x))
# Genus order
x = tapply(sigtabgen$log2FoldChange, sigtabgen$Genus, function(x) max(x))
x = sort(x, TRUE)
sigtabgen$Genus = factor(as.character(sigtabgen$Genus), levels=names(x))
ggplot(sigtabgen, aes(y=Genus, x=log2FoldChange, color=Phylum)) + 
  geom_vline(xintercept = 0.0, color = "gray", size = 0.5) +
  geom_point(size=6) + 
  theme(axis.text.x = element_text(angle = -90, hjust = 0, vjust=0.5))