diff --git a/DESCRIPTION b/DESCRIPTION index 3b9dc55..3bd1b8f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: MicEco Title: Various functions for microbial community data -Version: 0.9.12 +Version: 0.9.13 Authors@R: person("Jakob", "Russel", email = "russel2620@gmail.com", role = c("aut", "cre")) Description: Collection of functions for microbiome analyses. E.g. fitting neutral models and standardized effect sizes of phylogenetic beta diversities, and much more. Depends: R (>= 3.2.5) diff --git a/NAMESPACE b/NAMESPACE index 9d5697a..39c8a2e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,6 +3,7 @@ export(UniFrac.multi) export(WdS.test) export(adonis_OmegaSq) +export(clr) export(comdist.par) export(comdistnt.par) export(community_rrna) diff --git a/R/clr.R b/R/clr.R new file mode 100644 index 0000000..74ee0fb --- /dev/null +++ b/R/clr.R @@ -0,0 +1,33 @@ +#' CLR transformation of community matrix, with multiplicative zero replacement +#' +#' This function performs a CLR transformation on the input. +#' Prior to transformation it does a multiplicative zero replacement, which adds a pseudocount, but corrects the non-zero abundances such that log-ratios between non-zero elements are unchanged after correction. +#' @param mat A community matrix with features as rows +#' @param delta The pseudocount to exchange zeroes with. Zero-correction is multiplicative such that the log-ratios between any entirely non-zero features will not be affected by the pseudocount. +#' @keywords clr pseudocount +#' @return CLR transformed community matrix +#' @export + +clr <- function(mat, delta=1){ + + # Zero correction + mat_zc <- apply(mat, 2, function(y) sapply(y,function(x) ifelse(x==0,delta,(1-(sum(y==0)*delta)/sum(y))*x))) + + if(any(mat_zc <= 0)) stop("Community matrix should only contain positive values") + + # CLR transformation + gm_mean = function(x){ + if(any(x < 0, na.rm = TRUE)){ + stop("Negative values not allowed") + } + exp(mean(log(x))) + } + + mat_gm <- apply(mat_zc,2,function(y) gm_mean(y)) + mat_log <- t(log(t(mat_zc)/mat_gm)) + + rownames(mat_log) <- rownames(mat) + colnames(mat_log) <- colnames(mat) + + return(mat_log) +} diff --git a/README.md b/README.md index 74c6bf3..26455c4 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,10 @@ Rarefaction curve (theoretical and fast) from a phyloseq object. Output ready fo Clean tax_table such that NAs are replaced with names of the most specific known taxonomy prefixed with the rank. ## Miscellaneous functions +#### clr + +CLR transformation of community matrix, with multiplicative zero replacement + #### adonis_OmegaSq Calculate the unbiased effect size estimation (partial) omega-squared for adonis (PERMANOVA) models diff --git a/man/clr.Rd b/man/clr.Rd new file mode 100644 index 0000000..59610d4 --- /dev/null +++ b/man/clr.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/clr.R +\name{clr} +\alias{clr} +\title{CLR transformation of community matrix, with multiplicative zero replacement} +\usage{ +clr(mat, delta = 1) +} +\arguments{ +\item{mat}{A community matrix with features as rows} + +\item{delta}{The pseudocount to exchange zeroes with. Zero-correction is multiplicative such that the log-ratios between any entirely non-zero features will not be affected by the pseudocount.} +} +\value{ +CLR transformed community matrix +} +\description{ +This function performs a CLR transformation on the input. +Prior to transformation it does a multiplicative zero replacement, which adds a pseudocount, but corrects the non-zero abundances such that log-ratios between non-zero elements are unchanged after correction. +} +\keyword{clr} +\keyword{pseudocount}