Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Issue176: fix filterFeatures() #177

Merged
merged 5 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## QFeatures 1.7.3

- Nothing yet.
- fix: fixed filterFeatures when selection contains environment variables

## QFeatures 1.7.2

Expand Down
2 changes: 2 additions & 0 deletions R/QFeatures-filter.R
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ filterFeaturesWithFormula <- function(object, filter, na.rm, keep, ...) {

## Internal function that
.checkFilterVariables <- function(object, vars) {
## Ignore variables from the user environment
vars <- vars[!vars %in% ls(envir = parent.frame(4))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this number 4 here? Is it always 4?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is always four because we only want to include variables from the environment the function was called in (may not always be .GlobalEnv). Here is a "traceback" counter:

0 in .checkFilterVariables()
1 in FilterFeaturesWithFormula()
2 in .local()
3 in filterFeatures()
4 in environment the function was called

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm improving the unit test to ensure this is checked.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add this information as a comment in the code.

## Get in which assays each variable comes from
out <- sapply(rowDataNames(object), function(rdn) vars %in% rdn)
if (!is.array(out)) out <- t(out)
Expand Down
41 changes: 21 additions & 20 deletions R/QFeatures-join.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,24 @@
res
}

##' @importFrom methods as
.merge_2_by_rows <- function(x, y) {
## Save class to coerce at the end
cl <- class(x)
res <- merge(x, y,
by = 0,
all.x = TRUE, all.y = TRUE,
sort = FALSE)
## Set and remove row names
rownames(res) <- res[[1]]
res <- res[, -1, drop = FALSE]
as(res, cl[1])
}

.merge_by_rows <- function(x, y, ...) {
Reduce(.merge_2_by_rows, list(x, y, ...))
.merge_assays_by_rows <- function(l) {
cn <- unlist(lapply(l, colnames))
rn <- unique(unlist(lapply(l, rownames)))

## Check for duplicate column (sample) names
if (any(duplicated(cn)))
stop("Merging assays with columns in common is not allowed.")

res <- matrix(NA, ncol = length(cn), nrow = length(rn),
dimnames = list(rn, cn))
for (i in seq_along(l)) {
x <- l[[i]]
res[rownames(x), colnames(x)] <- as.matrix(x)
## as.matrix in case x is an HDF5Array, note x (and res) are
## realized in memory.
}
res
}


Expand All @@ -54,7 +56,7 @@ mergeSElist <- function(x) {
if (length(x_classes) != 1)
stop("Can't join assays from different classes.", call. = FALSE)
joined_mcols <- Reduce(.merge_2_by_cols, lapply(x, rowData))
joined_assay <- Reduce(.merge_2_by_rows, lapply(x, assay))
joined_assay <- .merge_assays_by_rows(lapply(x, assay))
joined_coldata <- Reduce(.merge_2_by_cols, lapply(x, colData))
res <- SummarizedExperiment(joined_assay[rownames(joined_mcols), ],
joined_mcols,
Expand Down Expand Up @@ -152,10 +154,9 @@ joinAssays <- function(x,
"Need at least 2 assays to join" = length(i) >= 2)
if (name %in% names(x))
stop("Assay with name '", name, "' already exists.")
joined_se <- mergeSElist(as.list(experiments(x)[i]))
## Join assays and add to x
joined_se <- mergeSElist(experiments(x)[i])
x <- addAssay(x, joined_se, name = name)
## Add the multi-parent AssayLinks
if (is.numeric(i)) i <- names(x)[i]
al <- .create_assay_link(x, from = i, to = name)
.update_assay_links(x, al)
addAssayLink(x, from = i, to = name)
}
8 changes: 2 additions & 6 deletions R/QFeatures-missing-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,7 @@ setMethod("zeroIsNA", c("QFeatures", "integer"),
el <- experiments(object)
for (ii in i)
el[[ii]] <- zeroIsNA(el[[ii]])
BiocGenerics:::replaceSlots(object,
ExperimentList = el,
check = TRUE)
replaceAssay(object, el)
})

##' @rdname QFeatures-missing-data
Expand Down Expand Up @@ -206,9 +204,7 @@ setMethod("infIsNA", c("QFeatures", "integer"),
el <- experiments(object)
for (ii in i)
el[[ii]] <- infIsNA(el[[ii]])
BiocGenerics:::replaceSlots(object,
ExperimentList = el,
check = TRUE)
replaceAssay(object, el)
})

##' @rdname QFeatures-missing-data
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test_filterFeatures.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ test_that("filterFeatures", {
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))
expect_equal(filter1, filter8)

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