Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: add evenness metrics #575

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ env:
global:
- CRAN: http://cran.rstudio.com

after_failure: "cat /home/travis/build/joey711/phyloseq/phyloseq.Rcheck/00check.log"

notifications:
email:
on_success: change
Expand Down
18 changes: 13 additions & 5 deletions R/extend_vegan.R
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ setMethod("vegdist", "phyloseq", function(x, method = "bray", binary = FALSE,
#'
#' Performs a number of standard alpha diversity estimates,
#' and returns the results as a \code{data.frame}.
#' Strictly speaking, this function is not only estimating richness,
#' This function estimates richness and evenness,
#' despite its name.
#' It can operate on the cumulative population of all
#' samples in the dataset, or by repeating the richness estimates for each
Expand All @@ -160,7 +160,7 @@ setMethod("vegdist", "phyloseq", function(x, method = "bray", binary = FALSE,
#' Alternatively, you can specify one or more measures
#' as a character vector of measure names.
#' Values must be among those supported:
#' \code{c("Observed", "Chao1", "ACE", "Shannon", "Simpson", "InvSimpson", "Fisher")}.
#' \code{c("Observed", "Chao1", "ACE", "Shannon", "Pielou", "Simpson", "InvSimpson", "SimpsonE", "Fisher")}.
#'
#' @return A \code{data.frame} of the richness estimates, and their standard error.
#'
Expand All @@ -175,7 +175,7 @@ setMethod("vegdist", "phyloseq", function(x, method = "bray", binary = FALSE,
#' \code{\link[vegan]{diversity}}
#'
#' \code{\link[vegan]{fisherfit}}
#'
#'
#' @importFrom vegan estimateR
#' @importFrom vegan diversity
#' @importFrom vegan fisher.alpha
Expand Down Expand Up @@ -214,8 +214,8 @@ estimate_richness <- function(physeq, split=TRUE, measures=NULL){
}

# Define renaming vector:
renamevec = c("Observed", "Chao1", "ACE", "Shannon", "Simpson", "InvSimpson", "Fisher")
names(renamevec) <- c("S.obs", "S.chao1", "S.ACE", "shannon", "simpson", "invsimpson", "fisher")
renamevec = c("Observed", "Chao1", "ACE", "Shannon", "Pielou", "Simpson", "InvSimpson", "SimpsonE", "Fisher")
names(renamevec) <- c("S.obs", "S.chao1", "S.ACE", "shannon", "pielou", "simpson", "invsimpson", "simpsone", "fisher")
# If measures was not explicitly provided (is NULL), set to all supported methods
if( is.null(measures) ){
measures = as.character(renamevec)
Expand All @@ -240,12 +240,20 @@ estimate_richness <- function(physeq, split=TRUE, measures=NULL){
if( "Shannon" %in% measures ){
outlist <- c(outlist, list(shannon = diversity(OTU, index="shannon")))
}
if( "Pielou" %in% measures){
#print("Starting Pielou")
outlist <- c(outlist, list(pielou = diversity(OTU, index = "shannon")/log(estimateR(OTU)["S.obs",])))
}
if( "Simpson" %in% measures ){
outlist <- c(outlist, list(simpson = diversity(OTU, index="simpson")))
}
if( "InvSimpson" %in% measures ){
outlist <- c(outlist, list(invsimpson = diversity(OTU, index="invsimpson")))
}
if( "SimpsonE" %in% measures ){
#print("Starting SimpsonE")
outlist <- c(outlist, list(simpsone = diversity(OTU, index="invsimpson")/estimateR(OTU)["S.obs",]))
}
if( "Fisher" %in% measures ){
fisher = tryCatch(fisher.alpha(OTU, se=TRUE),
warning=function(w){
Expand Down
6 changes: 4 additions & 2 deletions R/plot-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,8 @@ plot_net <- function(physeq, distance="bray", type="samples", maxdist = 0.7,
#' plot might be kindof strange, and not the intended behavior of this function).
#' The following are the names you will want to avoid using in \code{x} or \code{color}:
#'
#' \code{c("Observed", "Chao1", "ACE", "Shannon", "Simpson", "InvSimpson", "Fisher")}.
#' \code{c("Observed", "Chao1", "ACE", "Shannon", "Simpson", "Pielou",
#' "InvSimpson", "SimpsonE", "Fisher")}.
#'
#' @param physeq (Required). \code{\link{phyloseq-class}}, or alternatively,
#' an \code{\link{otu_table-class}}. The data about which you want to estimate.
Expand Down Expand Up @@ -624,7 +625,8 @@ plot_net <- function(physeq, distance="bray", type="samples", maxdist = 0.7,
#' Alternatively, you can specify one or more measures
#' as a character vector of measure names.
#' Values must be among those supported:
#' \code{c("Observed", "Chao1", "ACE", "Shannon", "Simpson", "InvSimpson", "Fisher")}.
#' \code{c("Observed", "Chao1", "ACE", "Shannon", "Simpson", "Pielou",
#' "InvSimpson", "SimpsonE", "Fisher")}.
#'
#' @param sortby (Optional). A character string subset of \code{measures} argument.
#' Sort x-indices by the mean of one or more \code{measures},
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ test_that("estimate_richness: test values, classes", {
expect_is(erdf, "data.frame")
expect_equivalent(nrow(erdf), 56)
# Contains all expected measures
expect_true(all(c("Observed", "Chao1", "ACE", "Shannon", "Simpson", "InvSimpson", "Fisher") %in% colnames(erdf)))
expect_true(all(c("Observed", "Chao1", "ACE", "Shannon", "Pielou", "Simpson", "InvSimpson", "SimpsonE", "Fisher") %in% colnames(erdf)))
# and certain standard errors:
expect_true(all(c("se.chao1", "se.ACE") %in% colnames(erdf)))
# Test some values.
Expand All @@ -353,7 +353,7 @@ test_that("plot_richness: Standard plots work", {
p = plot_richness(soilrep)
expect_is(p, "ggplot")
expect_equivalent(levels(p$data$variable),
c("Observed", "Chao1", "ACE", "Shannon", "Simpson", "InvSimpson", "Fisher"))
c("Observed", "Chao1", "ACE", "Shannon", "Pielou", "Simpson", "InvSimpson", "SimpsonE", "Fisher"))
expect_false(all(is.na(p$data$se)))
expect_true(any(is.na(p$data$se)))
p = plot_richness(soilrep, measures=c("Observed", "Chao1"))
Expand Down