-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jakob Russel
committed
Mar 4, 2021
1 parent
901f63e
commit 85b43ba
Showing
5 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.