Contents

Paul J. McMurdie and Susan Holmes

mcmurdie@stanford.edu

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 http://dx.plos.org/10.1371/journal.pone.0061217

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 microbio.me/qiime server.

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.15.14'

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

filepath = system.file("extdata", "study_1457_split_library_seqs_and_mapping.zip", 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/study_1457_split_library_seqs_and_mapping.zip"
kostic = microbio_me_qiime(filepath)

Or like this (if you’re accessing data directly from the microbio.me/qiime 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.

kostic
## 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.11.45'

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)
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"))
head(sigtab)
##          baseMean log2FoldChange     lfcSE      stat       pvalue
## 64396  123.230731       2.095439 0.4087749  5.126144 2.957368e-07
## 72853   19.433725      -1.560522 0.3212729 -4.857309 1.189921e-06
## 180285 114.266166      -1.180600 0.2724579 -4.333147 1.469929e-05
## 194648   6.191726      -1.237142 0.3010606 -4.109278 3.968976e-05
## 184729   9.837951      -1.788233 0.4280147 -4.177971 2.941210e-05
## 374052  42.072484       2.039745 0.4978720  4.096926 4.186720e-05
##                padj  Kingdom       Phylum                Class
## 64396  0.0001076482 Bacteria Fusobacteria Fusobacteria (class)
## 72853  0.0002165656 Bacteria   Firmicutes           Clostridia
## 180285 0.0017835137 Bacteria   Firmicutes           Clostridia
## 194648 0.0025399434 Bacteria   Firmicutes           Clostridia
## 184729 0.0025399434 Bacteria   Firmicutes           Clostridia
## 374052 0.0025399434 Bacteria Fusobacteria Fusobacteria (class)
##                  Order           Family            Genus
## 64396  Fusobacteriales Fusobacteriaceae    Fusobacterium
## 72853    Clostridiales  Ruminococcaceae Faecalibacterium
## 180285   Clostridiales  Ruminococcaceae Faecalibacterium
## 194648   Clostridiales             <NA>             <NA>
## 184729   Clostridiales  Lachnospiraceae     Ruminococcus
## 374052 Fusobacteriales Fusobacteriaceae    Fusobacterium
##                    Species
## 64396                 <NA>
## 72853                 <NA>
## 180285                <NA>
## 194648                <NA>
## 184729 Ruminococcus gnavus
## 374052                <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 123.230731 2.095439 0.4087749 0.0001076482 Fusobacteria Fusobacteria (class) Fusobacteriaceae Fusobacterium
374052 42.072484 2.039745 0.4978720 0.0025399434 Fusobacteria Fusobacteria (class) Fusobacteriaceae Fusobacterium
307981 1.910564 1.773154 0.4492352 0.0030477567 Proteobacteria Gammaproteobacteria Enterobacteriaceae Klebsiella
9778 1.248705 1.850825 0.5158074 0.0071291946 Proteobacteria Gammaproteobacteria Enterobacteriaceae Salmonella

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.

library("ggplot2")
theme_set(theme_bw())
sigtabgen = subset(sigtab, !is.na(Genus))
# 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))