Skip to content

Commit

Permalink
add clr function
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakob Russel committed Mar 4, 2021
1 parent 901f63e commit 85b43ba
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]", 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)
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export(UniFrac.multi)
export(WdS.test)
export(adonis_OmegaSq)
export(clr)
export(comdist.par)
export(comdistnt.par)
export(community_rrna)
Expand Down
33 changes: 33 additions & 0 deletions R/clr.R
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions man/clr.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 85b43ba

Please sign in to comment.