Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Issue 89 - support for inverting filters #90

Merged
merged 3 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: romic
Type: Package
Title: R for High-Dimensional Omic Data
Version: 1.2.3
Version: 1.2.4
Authors@R: c(
person(
given = "Sean",
Expand Down
30 changes: 16 additions & 14 deletions R/data_classes.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@
#' )
#' @export
create_tidy_omic <- function(
df,
feature_pk,
feature_vars = NULL,
sample_pk,
sample_vars = NULL,
omic_type_tag = "general",
verbose = TRUE
) {
df,
feature_pk,
feature_vars = NULL,
sample_pk,
sample_vars = NULL,
omic_type_tag = "general",
verbose = TRUE
) {

checkmate::assertDataFrame(df)
checkmate::assertString(omic_type_tag)
Expand Down Expand Up @@ -367,12 +367,14 @@ check_tidy_omic <- function(tidy_omic, fast_check = TRUE) {
#' "feature_id", "sample_id"
#' )
#' @export
create_triple_omic <- function(measurement_df,
feature_df = NULL,
sample_df = NULL,
feature_pk,
sample_pk,
omic_type_tag = "general") {
create_triple_omic <- function(
measurement_df,
feature_df = NULL,
sample_df = NULL,
feature_pk,
sample_pk,
omic_type_tag = "general"
) {
# testing

checkmate::assertClass(measurement_df, "data.frame")
Expand Down
62 changes: 41 additions & 21 deletions R/filters.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
#' filter_value}
#' \item{range}{filter filter_variable to using the range (i.e., lower and
#' upper limit) provided in filter_value}
#' \item{apply}{a quosure as a \code{filter_value} to a table of interest}
#' \item{quo}{a quosure as a \code{filter_value} to a table of interest}
#' }
#' @param filter_table table where the filter should be applied
#' @param filter_variable variable to apply the filter to
#' @param filter_value values to filter based on
#' @param invert If FALSE (default) entities will be retained; if TRUE, they
#' will be removed.
#'
#' @returns A \code{tomic} object where a subset of features, samples or
#' measurmenets have been filtered.
Expand Down Expand Up @@ -51,33 +53,36 @@
#' filter_value = rlang::quo(BP == "biological process unknown")
#' )
#' @export
filter_tomic <- function(tomic,
filter_type,
filter_table,
filter_value,
filter_variable = NULL) {
filter_tomic <- function(
tomic,
filter_type,
filter_table,
filter_value,
filter_variable = NULL,
invert = FALSE
) {

checkmate::assertClass(tomic, "tomic")
checkmate::assertChoice(filter_type, c("category", "range", "quo"))
checkmate::assertString(filter_type)
checkmate::assertChoice(
filter_table,
c("features", "samples", "measurements")
)
checkmate::assertLogical(invert, len = 1)

# convert to triple_omic
triple_omic <- tomic_to(tomic, "triple_omic")

VALID_FILTER_TYPES <- c("category", "range", "quo")
if (filter_type %in% c("category", "range")) {
checkmate::assertString(filter_variable)

valid_variables <- colnames(triple_omic[[filter_table]])
if (!(filter_variable %in% valid_variables)) {
stop(
filter_variable,
" is not a valid value for \"filter_type\",
valid values are all variables within the \"",
filter_table,
"\" table: ",
paste(valid_variables, collapse = ", ")
cli::cli_abort(
"{.field {filter_variable}} is is not a valid value for {.arg filter_type},
valid values are all variables within the {filter_table} table:
{.field {valid_variables}}"
)
}

Expand All @@ -87,19 +92,19 @@ filter_tomic <- function(tomic,
filter_var_type <- filter_var_type$type[1]
} else if (filter_type == "quo") {
if (!("NULL" %in% class(filter_variable))) {
warning(
"filter_variable was provided when filter_type is quo
cli::cli_alert_warning(
"{.arg filter_variable} was provided when {.arg filter_type} is {.field quo}
only a filter_value should be passed. filter_variable will be ignored"
)
}
} else {
stop("invalid filter type")
cli::cli_abort("{filter_type} is not a valid {.arg filter_type}. Valid types are {.field {VALID_FILTER_TYPES}}")
}

if (filter_type == "category") {
checkmate::assertVector(filter_value)

triple_omic[[filter_table]] <- triple_omic[[filter_table]] %>%
updated_filtered_table <- triple_omic[[filter_table]] %>%
dplyr::filter(
!!rlang::sym(filter_variable) %in% !!rlang::quo(filter_value)
)
Expand All @@ -116,20 +121,35 @@ filter_tomic <- function(tomic,
)
}

triple_omic[[filter_table]] <- triple_omic[[filter_table]] %>%
updated_filtered_table <- triple_omic[[filter_table]] %>%
dplyr::filter(
!!rlang::sym(filter_variable) >= !!rlang::quo(filter_value[1]),
!!rlang::sym(filter_variable) <= !!rlang::quo(filter_value[2])
)
} else if (filter_type == "quo") {
checkmate::assertClass(filter_value, "quosure")

triple_omic[[filter_table]] <- triple_omic[[filter_table]] %>%
updated_filtered_table <- triple_omic[[filter_table]] %>%
dplyr::filter(!!filter_value)
} else {
stop("invalid filter_type")
stop("Unexpected behavior")
}

# invert filter if invert is TRUE
if (invert) {
join_keys <- triple_omic$design[[filter_table]] %>%
dplyr::filter(type %in% c("feature_primary_key", "sample_primary_key")) %>%
dplyr::pull(variable)

updated_filtered_table <- dplyr::anti_join(
triple_omic[[filter_table]],
updated_filtered_table,
by = join_keys
)
}

triple_omic[[filter_table]] <- updated_filtered_table

# clear out data impacted by filters
triple_omic <- reconcile_triple_omic(triple_omic)

Expand Down
8 changes: 6 additions & 2 deletions man/filter_tomic.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading