diff --git a/R/data_type.R b/R/data_type.R index d93e6ef8..43e62595 100644 --- a/R/data_type.R +++ b/R/data_type.R @@ -408,12 +408,14 @@ process_perturbations <- function(dt, drug_order <- gsub(".*_(\\d+)$|.*", "\\1", drug_col) drug_cols <- - get_relevant_ids(paste0( + unique(c( + get_relevant_ids(paste0( c("drug", "drug_name", "drug_moa", "concentration"), drug_order), - dt) + dt), + drug_col, conc_col)) # Remove the current drug and concentration columns - dt[, (drug_cols, drug_col, conc_col) := NULL] + dt[, (drug_cols) := NULL] } } } diff --git a/tests/testthat/test-data_type.R b/tests/testthat/test-data_type.R index 17aff176..1e939537 100644 --- a/tests/testthat/test-data_type.R +++ b/tests/testthat/test-data_type.R @@ -72,14 +72,14 @@ test_that("collapse drugs works as expected", { test_that("process_perturbations works as expected", { dt <- data.table::data.table( - drug1 = c("vehicle", "drugA", "drugA"), - conc1 = c(0, 10, 0), - drug2 = c("vehicle", "drugB", "drugB"), - conc2 = c(0, 20, 0) + DrugName = c("vehicle", "drugA", "drugA"), + Concentration = c(0, 10, 0), + DrugName_2 = c("vehicle", "drugB", "drugB"), + Concentration_2 = c(0, 20, 0) ) - drugs_cotrt_ids <- c("drug1", "drug2") - conc_cotrt_ids <- c("conc1", "conc2") + drugs_cotrt_ids <- c("DrugName", "DrugName_2") + conc_cotrt_ids <- c("Concentration", "Concentration_2") result <- process_perturbations(dt, drugs_cotrt_ids, conc_cotrt_ids) @@ -134,5 +134,27 @@ test_that("process_perturbations works as expected", { drugC = c(0, 30, 0) ) expect_equal(result, expected) + + + dt4 <- data.table::data.table( + Gnumber = c("vehicle", "drugA", "drugB"), + Concentration = c(0, 10, 2), + Gnumber_2 = c("vehicle", "drugB", "drugB"), + Concentration_2 = c(0, 20, 0), + drug_moa_2 = c("vehicle", "moa_A", "moa_A"), + DrugName_2 = c("vehicle", "drugB", "drugB") + ) + + drugs_cotrt_ids <- "Gnumber_2" + conc_cotrt_ids <- "Concentration_2" + + result <- process_perturbations(dt4, drugs_cotrt_ids, conc_cotrt_ids) + + expected <- data.table::data.table( + Gnumber = c("vehicle", "drugA", "drugB"), + Concentration = c(0, 10, 2), + drugB = c(0, 20, 0) + ) + expect_equal(result, expected) })