Skip to content

Commit

Permalink
add unit test to track local vars/values issue
Browse files Browse the repository at this point in the history
  • Loading branch information
lgatto committed Apr 11, 2024
1 parent 21e8374 commit b812997
Showing 1 changed file with 44 additions and 23 deletions.
67 changes: 44 additions & 23 deletions tests/testthat/test_filterFeatures.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
test_that("filterFeatures", {
## Prepare data
data(feat1)
data(feat1)
feat1 <- aggregateFeatures(feat1, 1, fcol = "Sequence", name = "peptides", fun = colMedians)
feat1 <- aggregateFeatures(feat1, 2, fcol = "Protein", name = "proteins", fun = colMedians)

## Test character filters
filter1 <- expect_message(filterFeatures(feat1, ~ location == "Mitochondrion"))
filter2 <- expect_message(filterFeatures(feat1, ~startsWith(location, "Mito")))
Expand All @@ -19,7 +19,7 @@ test_that("filterFeatures", {
expect_equal(filter1, filter6)
expect_equal(filter1, filter7)
expect_identical(lengths(filter1), c(6L, 2L, 1L))

## Test filter stored in variable
target <- "Mitochondrion"
filter8 <- expect_message(filterFeatures(feat1, ~ location == target))
Expand All @@ -32,35 +32,35 @@ test_that("filterFeatures", {
expect_message(filterFeatures(feat1, ~ location == target2))
}
expect_equal(filter8, runfilter())

## Test numerical filters
filter1 <- expect_message(filterFeatures(feat1, VariableFilter("pval", 0.03, "<=")))
filter2 <- expect_message(filterFeatures(feat1, ~ pval <= 0.03))
expect_equal(filter1, filter2)

## Test no match filters
expect_true(all(dims(filterFeatures(feat1, VariableFilter("location", "not")))[1, ] == 0))
expect_true(all(dims(filterFeatures(feat1, ~ location == "not"))[1, ] == 0))
expect_true(all(dims(filterFeatures(feat1, ~ is.na(pval)))[1, ] == 0))

## Test fraud filters
expect_error(VariableFilter("pval", TRUE, "<="))
expect_error(VariableFilter("location", TRUE, "!="))
})
})


test_that("filterFeatures with NAs", {
## Prepare data
data(feat1)
rowData(feat1[[1]])[1, "location"] <- NA
flt <- VariableFilter("location", "Mitochondrion")

## Default is na.rm = FALSE
res1 <- expect_message(filterFeatures(feat1, flt))
res2 <- expect_message(filterFeatures(feat1, flt, na.rm = FALSE))
expect_identical(res1, res2)
expect_true(is.na(rowData(res1[[1]])[1, "location"]))

## Removing the row with the NA
res3 <- expect_message(filterFeatures(feat1, flt, na.rm = TRUE))
expect_identical(nrow(assay(res1, 1)), nrow(assay(res3, 1)) + 1L)
Expand All @@ -80,17 +80,17 @@ test_that("filterFeatures with missing filter variables", {
data(feat1)
feat1 <- aggregateFeatures(feat1, 1, fcol = "Sequence", name = "peptides", fun = colMedians)
feat1 <- aggregateFeatures(feat1, 2, fcol = "Protein", name = "proteins", fun = colMedians)

## Using Variable filter
## Test: 1 variable, missing in some assays, remove features for

## Test: 1 variable, missing in some assays, remove features for
## missing variables
expect_message(
filt <- filterFeatures(feat1, VariableFilter("pval", 0.03, "<=")),
regexp = "pval.*1 out of 3.*keep['] argument"
)
expect_identical(lengths(filt), c(3L, 0L, 0L))
## Test: 1 variable, missing in some assays, keep features for
## Test: 1 variable, missing in some assays, keep features for
## missing variables
expect_message(
filt <- filterFeatures(feat1, VariableFilter("pval", 0.03, "<="), keep = TRUE),
Expand All @@ -101,17 +101,17 @@ test_that("filterFeatures with missing filter variables", {
expect_error(filterFeatures(feat1, ~ foo),
regexp = "foo.*absent")
## Variable filter only allows filtering 1 variable at a time

## Using formula filter
## Test: 1 variable, missing in some assays, remove features for

## Test: 1 variable, missing in some assays, remove features for
## missing variables
expect_message(
filt <- filterFeatures(feat1, ~ pval <= 0.03),
regexp = "pval.*1 out of 3.*keep['] argument"
)
expect_identical(lengths(filt), c(3L, 0L, 0L))
## Test: 1 variable, missing in some assays, keep features for
## Test: 1 variable, missing in some assays, keep features for
## missing variables
expect_message(
filt <- filterFeatures(feat1, ~ pval <= 0.03, keep = TRUE),
Expand All @@ -121,7 +121,7 @@ test_that("filterFeatures with missing filter variables", {
## Test: 1 variable, missing in all assays
expect_error(filterFeatures(feat1, ~ foo),
regexp = "foo.*absent")

## Test: 2 variables, 1 present in all assays and 1 in some, remove
## features for missing variables
expect_message(
Expand All @@ -142,7 +142,7 @@ test_that("filterFeatures with missing filter variables", {
## Test: 2 variable, 2 missing in all assays
expect_error(filterFeatures(feat1, ~ foo & bar),
regexp = "foo['][,] [']bar.*absent")
})
})

test_that("filterFeatures on selected assays", {
data("feat2")
Expand All @@ -169,7 +169,7 @@ test_that("filterFeatures on selected assays", {
## --- One doesn't contain variable
## ---- keep = FALSE
expect_message(
filt <- filterFeatures(feat2, VariableFilter("y", 0, "<"),
filt <- filterFeatures(feat2, VariableFilter("y", 0, "<"),
i = 1:2),
regexp = "2 out of 3"
)
Expand All @@ -179,14 +179,14 @@ test_that("filterFeatures on selected assays", {
expect_identical(exp, dims(filt))
## ---- keep = TRUE
expect_message(
filt <- filterFeatures(feat2, VariableFilter("y", 0, "<"),
filt <- filterFeatures(feat2, VariableFilter("y", 0, "<"),
i = 1:2, keep = TRUE),
regexp = "2 out of 3"
)
exp <- init
exp[1, 2] <- 1L
expect_identical(exp, dims(filt))

## - With formulat
## -- Filter on 1 selected assay
expect_message(
Expand Down Expand Up @@ -225,4 +225,25 @@ test_that("filterFeatures on selected assays", {
exp <- init
exp[1, 2] <- 1L
expect_identical(exp, dims(filt))
})
})


test_that("test filterFeatures with value and vars in GlobalEnv", {
data(feat1)
feat1 <- aggregateFeatures(feat1, 1, fcol = "Sequence", name = "peptides", fun = colMedians)
feat1 <- aggregateFeatures(feat1, 2, fcol = "Protein", name = "proteins", fun = colMedians)
##
expect_error(filterFeatures(feat1, ~ location == target),
"'target' is/are absent from all rowData.")
target <- "Mitochondrion"
expect_type(filterFeatures(feat1, ~ location == target), "S4")
location <- 1
expect_error(filterFeatures(feat1, ~ location == target),
"No vars left.")
##
rm(location)
res1 <- filterFeatures(feat1, ~ pval <= 0.03 & grepl("Mito", location))
res2 <- filterFeatures(feat1, ~ pval <= 0.03) |>
filterFeatures(~ location == "Mitochondrion")
expect_identical(res1, res2)
})

0 comments on commit b812997

Please sign in to comment.