Skip to content
Open
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
16 changes: 11 additions & 5 deletions R/merge_ml_results.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions man/parse_ml_filename.Rd

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

61 changes: 61 additions & 0 deletions tests/testthat/test-merge-ml-results.R
Original file line number Diff line number Diff line change
@@ -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.
# "<species>_drug_year_<drug>_<year_range>_...".
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)
})