Skip to content

Commit

Permalink
Fix failure of coocurrence_matrix() when names missing
Browse files Browse the repository at this point in the history
Problem solved by creating default sample and taxa names when these are
missing. Fixes #5.
  • Loading branch information
mikemc committed Aug 18, 2019
1 parent f0f8fef commit 4e207a8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
7 changes: 7 additions & 0 deletions R/bias-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ NULL
#' @describeIn cooccurrence Adjacency matrix of the co-occurrence network
#' @export
cooccurrence_matrix <- function(mat, all = TRUE) {
# To compute the number of times each pair of taxa cooccur, we leverage the
# `compute_ratios()` function. Doing so requires that the samples (rows of
# `mat`) and the taxa (cols of `mat`) have names.
if (is.null(rownames(mat)))
rownames(mat) <- paste0("S", seq(nrow(mat)))
if (is.null(colnames(mat)))
colnames(mat) <- paste0("T", seq(ncol(mat)))
ratios <- mat %>%
tibble::as_tibble(rownames = "Sample") %>%
tidyr::gather("Taxon", "Value", -Sample) %>%
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-center.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ test_that("sampling rows or using `weights` gives an equal estimate", {
center(d2, weights = w.rows, method = "rss"))
})

test_that("the center is correctly computed when sample and taxa names are missing", {
d2.no_names <- d2
rownames(d2.no_names) <- colnames(d2.no_names) <- NULL
c1 <- center(d2)
names(c1) <- NULL
c2 <- center(d2.no_names)
expect_equal(c1, c2)
})

# Additional tests to add
# - Check various in scale and out scale combinations
Expand Down

0 comments on commit 4e207a8

Please sign in to comment.