Skip to content
Open
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 22 additions & 46 deletions tests/testthat/test-5transformCounts.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,50 +134,18 @@ test_that("transformAssay", {
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")

# 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
Expand All @@ -188,6 +156,15 @@ test_that("transformAssay", {
assay(tse, "test2")[1, ] <- 0
# One missing value
assay(tse, "na_values")[4, 5] <- NA

# Tests that clr robust gives values that are approximately same if only
# one value per sample is changed to zero
expect_equal(assays(mia::transformAssay(tse, assay.type = "test",
method = "rclr")),
assays(mia::transformAssay(tse, assay.type = "test2",
method = "rclr")),
check.attributes = FALSE)

Comment thread
raivo-otus marked this conversation as resolved.
Outdated

tse <- transformAssay(tse, method = "relabundance")
# Expect error when counts and zeroes
Expand Down Expand Up @@ -272,18 +249,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
Expand All @@ -304,6 +269,7 @@ test_that("transformAssay", {
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")
Expand All @@ -313,9 +279,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
Expand Down Expand Up @@ -351,6 +317,16 @@ 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)



# Check that transformation is applied to altExps
expect_error(transformAssay(tse, altexp = "Phylum"))
Expand All @@ -372,7 +348,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
Expand Down
Loading