-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add fct to make relative abunds matrices
Add function to transform abundances matrices to relative abundances matrices. It also works with sparse matrices. It has the corresponding tests.
- Loading branch information
Rekyt
committed
Oct 13, 2016
1 parent
4f37ae7
commit d992a67
Showing
4 changed files
with
86 additions
and
0 deletions.
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
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,31 @@ | ||
#' Relative abundance matrix from absolute abundance matrix | ||
#' | ||
#' From an abundance matrix (numbers of individuals of a given species at a site) | ||
#' returns a relative abundance matrix (proportion of individuals of a given species | ||
#' at a given site). This function works also with sparse matrices. | ||
#' | ||
#' @param abund_matrix abundance matrix, with sites in rows and species in columns | ||
#' | ||
#' @export | ||
make_relative = function(abund_matrix) { | ||
|
||
# Check input type using internal function | ||
check_matrix(abund_matrix, "abundance") | ||
|
||
# Compute relative abundances matrix | ||
if (requireNamespace("Matrix", quietly = TRUE) & | ||
is(abund_matrix, "sparseMatrix")) { | ||
|
||
sites_abund = Matrix::rowSums(abund_matrix, na.rm = TRUE) | ||
|
||
rel_abund_matrix = abund_matrix / sites_abund | ||
} else { | ||
# Compute total site abundances | ||
sites_abund = rowSums(abund_matrix, na.rm = TRUE) | ||
|
||
# Divide each individual abundace by total site abundance | ||
rel_abund_matrix = sweep(abund_matrix, 1, sites_abund, "/") | ||
} | ||
|
||
return(rel_abund_matrix) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,37 @@ | ||
# Test file for function to convert abundance matrix to relative abundances | ||
# matrix | ||
|
||
context("Abundances to Relative abundances Matrix") | ||
|
||
# Packages --------------------------------------------------------------------- | ||
library(Matrix) | ||
|
||
# Common objects --------------------------------------------------------------- | ||
|
||
# Abundance Matrix to test | ||
abs_abund_mat = matrix(c(1, 1, 1, NA, | ||
NA, rep(1, 3), | ||
NA, 1, 1, NA, | ||
NA, NA, 1, 1), | ||
ncol = 4) | ||
|
||
sparse_abs_abund = as(abs_abund_mat, "sparseMatrix") | ||
|
||
# Corresponding relative abundances matrix | ||
rel_abund_mat = matrix(c(1, 1/3, 1/4, NA, | ||
NA, 1/3, 1/4, 1/2, | ||
NA, 1/3, 1/4, NA, | ||
NA, NA, 1/4, 1/2), | ||
ncol = 4) | ||
|
||
sparse_rel_abund = as(rel_abund_mat, "sparseMatrix") | ||
|
||
# Tests ------------------------------------------------------------------------ | ||
|
||
test_that("Can convert from absolute to relative abundances matrices", { | ||
# On regular matrices | ||
expect_equal(make_relative(abs_abund_mat), rel_abund_mat) | ||
|
||
# On sparse matrices | ||
expect_equal(make_relative(sparse_abs_abund), sparse_rel_abund) | ||
}) |