Skip to content

Commit

Permalink
Add rel to abs abund matrices conversion
Browse files Browse the repository at this point in the history
Added function 'make_absolute()' to convert relative abundances matrix to absolute abundances matrix, corresponding tests and documentation. This function works with both regular and sparse matrices.
  • Loading branch information
Rekyt committed Oct 13, 2016
1 parent d992a67 commit d8e801b
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 2 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export(distinctiveness_com)
export(distinctiveness_stack)
export(funrar)
export(funrar_stack)
export(make_absolute)
export(make_relative)
export(matrix_to_stack)
export(restrictedness)
Expand Down
34 changes: 34 additions & 0 deletions R/make_relative.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#'
#' @param abund_matrix abundance matrix, with sites in rows and species in columns
#'
#' @seealso \code{\link[funrar]{make_absolute}} for the reverse operation
#'
#' @export
make_relative = function(abund_matrix) {

Expand All @@ -29,3 +31,35 @@ make_relative = function(abund_matrix) {

return(rel_abund_matrix)
}

#' Abundance matrix from relative abundances matrix
#'
#' Convert the input relative abundances matrix to an abundance matrix
#'
#' @param rel_abund_matrix a relative abundance matrix (proportion of individuals
#' of a given species at a given site)
#'
#' @seealso \code{\link[funrar]{make_relative}} for the reverse operation
#'
#' @export
make_absolute = function(rel_abund_matrix) {

# Compute absolute matrix
if (requireNamespace("Matrix", quietly = TRUE) &
is(rel_abund_matrix, "sparseMatrix")) {

sites_abund = Matrix::rowSums((rel_abund_matrix != 0) &
(!is.na(rel_abund_matrix)), na.rm = TRUE)

abs_matrix = rel_abund_matrix * sites_abund
} else {
# Compute total site abundances
sites_abund = rowSums((rel_abund_matrix != 0) &
(!is.na(rel_abund_matrix)), na.rm = TRUE)

# Divide each individual abundace by total site abundance
abs_matrix = sweep(rel_abund_matrix, 1, sites_abund, "*")
}

return(abs_matrix)
}
19 changes: 19 additions & 0 deletions man/make_absolute.Rd

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

3 changes: 3 additions & 0 deletions man/make_relative.Rd

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

11 changes: 9 additions & 2 deletions tests/testthat/test-rel_abund.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Test file for function to convert abundance matrix to relative abundances
# matrix

context("Abundances to Relative abundances Matrix")
context("Convert Absolute and Relative Abund Matrices")

# Packages ---------------------------------------------------------------------
library(Matrix)
Expand Down Expand Up @@ -35,3 +34,11 @@ test_that("Can convert from absolute to relative abundances matrices", {
# On sparse matrices
expect_equal(make_relative(sparse_abs_abund), sparse_rel_abund)
})

test_that("Convert relative abundances to absolute abundances matrices", {
# On regular matrices
expect_equal(make_absolute(rel_abund_mat), abs_abund_mat)

# On sparse matrices
expect_equal(make_absolute(sparse_rel_abund), sparse_abs_abund)
})

0 comments on commit d8e801b

Please sign in to comment.