Skip to content

Commit

Permalink
Add fct to make relative abunds matrices
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 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_relative)
export(matrix_to_stack)
export(restrictedness)
export(restrictedness_stack)
Expand Down
31 changes: 31 additions & 0 deletions R/make_relative.R
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)
}
17 changes: 17 additions & 0 deletions man/make_relative.Rd

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

37 changes: 37 additions & 0 deletions tests/testthat/test-rel_abund.R
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)
})

0 comments on commit d992a67

Please sign in to comment.