diff --git a/tests/testthat/test-5transformCounts.R b/tests/testthat/test-5transformCounts.R index bb9587179..83145d965 100644 --- a/tests/testthat/test-5transformCounts.R +++ b/tests/testthat/test-5transformCounts.R @@ -19,12 +19,8 @@ test_that("transformAssay", { check.attributes = FALSE) ############################# RELATIVE ABUNDANCE ####################### - # Calculates relative abundances. Should be equal. - expect_equal(as.matrix(assays(mia::transformAssay(tse, method = "relabundance"))$relabundance), - apply(as.matrix(assay(tse,"counts")), 2, FUN=function(x){ - x/sum(x) - }), check.attributes = FALSE) - + # Calculation tests against vegan at later section + # Tests validity of assay name mat <- matrix(1:60, nrow = 6) df <- DataFrame(n = c(1:6)) expect_error(transformAssay( @@ -119,56 +115,21 @@ test_that("transformAssay", { expect_true(all.equal(as.matrix(ass[19:20, 17:18]), normalized_counts, check.attributes = FALSE)) ########################## PA ########################################## - # Calculates pa transformation. Should be equal. + # Calculates pa transformation. Validate format. actual <- assay(mia::transformAssay(tse, method = "pa"),"pa") - expect_equal(as.vector(actual), - as.integer(as.matrix(assay(tse, "counts")) > 0), - check.attributes = FALSE) expect_equal(typeof(actual),"double") expect_true(all(actual == 1 | actual == 0)) - # Tests transformAssay(MARGIN = "features"), calculates pa transformation. Should be equal. + # .. feature wise actual <- assay(mia::transformAssay(tse, MARGIN = "features", method = "pa"),"pa") - expect_equal(as.vector(actual), - as.integer(t(as.matrix(t(assay(tse, "counts"))) > 0))) expect_equal(typeof(actual),"double") expect_true(all(actual == 1 | actual == 0)) - ######################## HELLINGER ##################################### - # Calculates Hellinger transformation. Should be equal. - # Calculates relative abundance table - relative <- assays(mia::transformAssay(tse, method = "relabundance", name = "relative"))$relative - - expect_equal(as.matrix(assays(mia::transformAssay(tse, method = "hellinger", name = "test_123"))$test_123), - apply(as.matrix(relative), 2, FUN=function(x){ - sqrt(x) - }), check.attributes = FALSE) - ############################### CLR #################################### - # Calculates clr-transformation. Should be equal. - # Random pseudocount pseudonumber <- runif(1, 1, 100) tse <- transformAssay(tse, method = "relabundance") - # Calculates relative abundance table - relative <- assay(tse, "relabundance") - relative <- relative + pseudonumber - # Tests clr - # Calc CLRs - mat <- assays(mia::transformAssay(tse, assay.type = "relabundance", - method = "clr", pseudocount = pseudonumber))$clr - mat_comp <- apply(as.matrix(relative), 2, FUN=function(x){ - log(x) - mean(log(x)) - }) - # Remove attributes since vegan adds additional ones - attributes(mat) <- NULL - attributes(mat_comp) <- NULL - # Compare - expect_equal(mat, mat_comp) - - # Expect that error occurs - expect_error(mia::transformAssay(tse, method = "clr")) # Expect that error does not occur tse <- mia::transformAssay(tse, method = "rclr") @@ -176,16 +137,10 @@ test_that("transformAssay", { # Tests transformAssay, tries to calculate clr. Should be an error, because of zeros. expect_error(mia::transformAssay(tse, method = "clr")) - # Tests that clr robust gives values that are approximately same if only - # one value per sample are changed to zero tse <- transformAssay(tse, method = "relabundance") - # Adds pseudocount - assay(tse, "test") <- assay(tse, "relabundance") + 1 - assay(tse, "test2") <- assay(tse, "test") + # Adds pos/neg pseudocount assay(tse, "neg_values") <- assay(tse, "counts") - 2 assay(tse, "na_values") <- assay(tse, "counts") + 2 - # First row is zeroes - assay(tse, "test2")[1, ] <- 0 # One missing value assay(tse, "na_values")[4, 5] <- NA @@ -204,11 +159,11 @@ test_that("transformAssay", { assay.type = "neg_values", pseudocount = TRUE)) # Expect pseudocount to be half of min value when NA values present - test3 <- tmp - assay(test3, "na_values") <- assay(test3, "counts") - assay(test3, "na_values")[4, 5] <- NA + test2 <- tmp + assay(test2, "na_values") <- assay(test2, "counts") + assay(test2, "na_values")[4, 5] <- NA expect_warning( - actual <- transformAssay(test3, method = "relabundance", + actual <- transformAssay(test2, method = "relabundance", assay.type = "na_values", pseudocount = TRUE) ) value <- attr(assay(actual, "relabundance"), "parameters")[["pseudocount"]] @@ -272,18 +227,6 @@ test_that("transformAssay", { # Expect that assay contains count and rank table expect_true(all(c("counts", "rank") %in% assayNames(tse_rank))) - for(i in c(1:10)){ - # Gets columns from 'rank' table - ranks <- assay(tse_rank, "rank")[,i] - # Gets columns from 'counts' table, and calculates ranks - counts_compare <- assay(tse_rank, "counts")[,i] - counts_compare[counts_compare == 0] <- NA - ranks_compare <- rank(counts_compare, na.last = "keep") - ranks_compare[is.na(ranks_compare)] <- 0 - # Expect that they are equal - expect_equal(ranks, ranks_compare, check.attributes = FALSE) - } - # Calculates rank with pseudocount tse_rank_pseudo <- transformAssay(tse, method = "rank", pseudocount = runif(1, 0, 1000)) # Pseudocount should not change the rank @@ -295,15 +238,13 @@ test_that("transformAssay", { ############################## Z TRANSFORMATION ######################## # Calculates Z-transformation for features - xx <- t(scale(t(as.matrix(assay(tse, "counts"))))) expect_warning(z_assay <- assay( mia::transformAssay( tse, method = "standardize", MARGIN = "features", pseudocount = 1), "standardize")) - expect_equal(max(abs(z_assay - xx), na.rm=TRUE), 0, - tolerance = 1e-14, check.attributes = FALSE) + ####################### Test equality to vegan ######################### # Test that transformations are equal to ones directly from vegan # clr tse <- transformAssay(tse, method = "relabundance") @@ -313,9 +254,9 @@ test_that("transformAssay", { compare <- vegan::decostand(assay(tse, "relabundance"), method = "clr", pseudocount = 4, MARGIN = 2) expect_equal(actual, compare) + # rclr tse <- transformAssay(tse, assay.type = "relabundance", method = "rclr") - actual <- assay(tse, "rclr") # mia has additional pseudocount parameter for all methods attr(actual, "parameters")$pseudocount <- NULL @@ -351,6 +292,35 @@ test_that("transformAssay", { MARGIN = 2) compare <- t(compare) expect_equal(na.omit(actual), na.omit(compare)) + + # rank + tse <- transformAssay(tse, assay.type = "counts", method = "rank") + actual <- assay(tse, "rank") + attr(actual, "parameters")$pseudocount <- NULL + compare <- vegan::decostand(assay(tse, "counts"), method = "rank", + MARGIN = 2) + expect_equal(actual, compare) + + # pa + tse <- transformAssay(tse, assay.type = "counts", method = "pa") + actual <- assay(tse, "pa") + compare <- vegan::decostand(assay(tse, "counts"), method = "pa", + MARGIN = 2) + expect_equal(actual, compare, check.attributes = FALSE) + # .. feature wise + tse <- transformAssay(tse, MARGIN = "features", method = "pa") + actual <- assay(tse, "pa") + compare <- vegan::decostand(assay(tse, "counts"), method = "pa", + MARGIN = 1) + expect_equal(actual, compare, check.attributes = FALSE) + + # Standardize + tse <- transformAssay(tse, method = "standardize") + actual <- assay(tse, "standardize") + compare <- vegan::decostand(assay(tse, "counts"), method = "standardize", + MARGIN = 2) + expect_equal(actual, compare, + check.attributes = FALSE) # Check that transformation is applied to altExps expect_error(transformAssay(tse, altexp = "Phylum")) @@ -372,7 +342,7 @@ test_that("transformAssay", { test <- altExp(test, "Genus") expect_equal(assay(test, "relabundance"), assay(ref, "relabundance")) - # Check that philt transformation works + # Check that philr transformation works skip_if_not_installed("philr") data("GlobalPatterns") tse <- GlobalPatterns