From b39123283df047124c49ce60b5630a37de729237 Mon Sep 17 00:00:00 2001 From: Emily Boyer Date: Wed, 12 Aug 2026 15:20:30 -0600 Subject: [PATCH] The stratification token appears right after drug in teh actual result filenames, not after the drug value so there was mislabeling. Corrected the token order to match was regex expects, and added regression tests for the stratified and unstratified cases. --- R/merge_ml_results.R | 16 ++++--- man/parse_ml_filename.Rd | 1 + tests/testthat/test-merge-ml-results.R | 61 ++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 tests/testthat/test-merge-ml-results.R diff --git a/R/merge_ml_results.R b/R/merge_ml_results.R index ce32baf..6494f4f 100644 --- a/R/merge_ml_results.R +++ b/R/merge_ml_results.R @@ -21,6 +21,7 @@ #' #' @examples #' parse_ml_filename("Csp_drug_AMX_genes_binary_42_top_features.tsv") +#' parse_ml_filename("Csp_drug_year_AMX_2010-2015_genes_binary_year_42_performance.tsv") #' #' @export parse_ml_filename <- function(filename) { @@ -63,25 +64,30 @@ parse_ml_filename <- function(filename) { # Case A: drug_class if (xs[i + 1] == "class") { out$drug_label <- "drug_class" - out$drug_or_class <- xs[i + 2] - i <- i + 3 + i <- i + 2 } # Case B: simple drug else { out$drug_label <- "drug" - out$drug_or_class <- xs[i + 1] - i <- i + 2 + i <- i + 1 } } else { stop("ERROR: expected 'drug' token after species") } # --------------------------- - # 4. Stratified? + # 4. Stratified? (the strat label, if present, comes before the + # drug/drug_class value, e.g. "..._drug_year_AMX_2010-2015_...") # --------------------------- if (i <= length(xs) && xs[i] %in% c("year", "country")) { out$strat_label <- xs[i] i <- i + 1 + } + + out$drug_or_class <- xs[i] + i <- i + 1 + + if (!is.na(out$strat_label)) { out$strat_value <- xs[i] i <- i + 1 } diff --git a/man/parse_ml_filename.Rd b/man/parse_ml_filename.Rd index 8bd6ec2..7bd48e9 100644 --- a/man/parse_ml_filename.Rd +++ b/man/parse_ml_filename.Rd @@ -30,5 +30,6 @@ feature types, and seed information. } \examples{ parse_ml_filename("Csp_drug_AMX_genes_binary_42_top_features.tsv") +parse_ml_filename("Csp_drug_year_AMX_2010-2015_genes_binary_year_42_performance.tsv") } diff --git a/tests/testthat/test-merge-ml-results.R b/tests/testthat/test-merge-ml-results.R new file mode 100644 index 0000000..58cdde8 --- /dev/null +++ b/tests/testthat/test-merge-ml-results.R @@ -0,0 +1,61 @@ +# Unit tests for parse_ml_filename() in merge_ml_results.R. + +test_that("parse_ml_filename parses an unstratified drug filename", { + out <- parse_ml_filename("Csp_drug_AMX_genes_binary_42_top_features.tsv") + + expect_false(out$shuffled) + expect_equal(out$species, "Csp") + expect_equal(out$drug_label, "drug") + expect_equal(out$drug_or_class, "AMX") + expect_true(is.na(out$strat_label)) + expect_true(is.na(out$strat_value)) + expect_equal(out$feature_type, "genes") + expect_equal(out$feature_subtype, "binary") + expect_equal(out$seed, 42L) +}) + +test_that("parse_ml_filename parses an unstratified drug_class filename", { + out <- parse_ml_filename("Csp_drug_class_AMINOGLYCOSIDES_genes_binary_42_performance.tsv") + + expect_equal(out$drug_label, "drug_class") + expect_equal(out$drug_or_class, "AMINOGLYCOSIDES") + expect_true(is.na(out$strat_label)) + expect_equal(out$seed, 42L) +}) + +test_that("parse_ml_filename detects a shuffled run", { + out <- parse_ml_filename("shuffled_Csp_drug_AMX_genes_binary_42_top_features.tsv") + + expect_true(out$shuffled) + expect_equal(out$drug_or_class, "AMX") +}) + +test_that("parse_ml_filename parses a year-stratified drug filename", { + # The strat label sits between "drug" and the drug value in the actual + # filenames written by the matrix-generation code, e.g. + # "_drug_year___...". + out <- parse_ml_filename( + "Csp_drug_year_AMX_2010-2015_genes_binary_year_42_performance.tsv" + ) + + expect_equal(out$species, "Csp") + expect_equal(out$drug_label, "drug") + expect_equal(out$drug_or_class, "AMX") + expect_equal(out$strat_label, "year") + expect_equal(out$strat_value, "2010-2015") + expect_equal(out$feature_type, "genes") + expect_equal(out$feature_subtype, "binary") + expect_equal(out$seed, 42L) +}) + +test_that("parse_ml_filename parses a country-stratified drug_class filename", { + out <- parse_ml_filename( + "Csp_drug_class_country_AMINOGLYCOSIDES_USA_genes_binary_country_7_top_features.tsv" + ) + + expect_equal(out$drug_label, "drug_class") + expect_equal(out$drug_or_class, "AMINOGLYCOSIDES") + expect_equal(out$strat_label, "country") + expect_equal(out$strat_value, "USA") + expect_equal(out$seed, 7L) +})