diff --git a/bin/workflow_glue_r/R/bambu.R b/bin/workflow_glue_r/R/bambu.R index 5e4d3f2..a4666c3 100644 --- a/bin/workflow_glue_r/R/bambu.R +++ b/bin/workflow_glue_r/R/bambu.R @@ -168,14 +168,22 @@ bambu_resolve_chunk_dirs <- function(args) { chunk_dirs } +bambu_read_sample_sheet <- function(path) { + header <- names(utils::read.csv(path, nrows = 0, check.names = FALSE)) + char_cols <- intersect(c("alias", "sample_id"), header) + col_classes <- stats::setNames(rep("character", length(char_cols)), char_cols) + utils::read.csv( + path, + check.names = FALSE, + stringsAsFactors = FALSE, + colClasses = col_classes + ) +} + bambu_resolve_inputs <- function(args, bamfile_list_ctor = Rsamtools::BamFileList) { sample_df <- NULL if (!bambu_missing(args$sample_sheet)) { - sample_df <- utils::read.csv( - args$sample_sheet, - check.names = FALSE, - stringsAsFactors = FALSE - ) + sample_df <- bambu_read_sample_sheet(args$sample_sheet) if (!"alias" %in% names(sample_df)) { stop("Sample sheet must contain an 'alias' column.", call. = FALSE) } @@ -1028,11 +1036,7 @@ bambu_collate_chunk_outputs <- function( }) raw_se <- bambu_combine_transcript_chunks(tx_ses) - sample_df <- utils::read.csv( - file.path(chunk_dirs[[1]], "samples.csv"), - check.names = FALSE, - stringsAsFactors = FALSE - ) + sample_df <- bambu_read_sample_sheet(file.path(chunk_dirs[[1]], "samples.csv")) gene_se <- gene_expression_fn(raw_se) filtered <- bambu_filter_transcripts(raw_se) diff --git a/bin/workflow_glue_r/tests/testthat/test_bambu.R b/bin/workflow_glue_r/tests/testthat/test_bambu.R index 6006ef4..b86ea21 100644 --- a/bin/workflow_glue_r/tests/testthat/test_bambu.R +++ b/bin/workflow_glue_r/tests/testthat/test_bambu.R @@ -223,6 +223,35 @@ testthat::test_that("sample sheet reordered to match BAMs", { ) }) +testthat::test_that("numeric alias and sample_id values are preserved as strings", { + sample_sheet <- tempfile(fileext = ".csv") + writeLines( + paste( + "barcode,sample_id,alias,condition", + "barcode01,01,01,control", + "barcode02,02,02,treated", + sep = "\n" + ), + sample_sheet + ) + + args <- list( + bams = "sample1.bam,sample2.bam", + aliases = "01,02", + sample_sheet = sample_sheet + ) + resolved <- bambu_resolve_inputs( + args, + bamfile_list_ctor = function(paths, yieldSize) paths + ) + + testthat::expect_equal(resolved$aliases, c("01", "02")) + testthat::expect_equal(resolved$sample_df$alias, c("01", "02")) + testthat::expect_equal(resolved$sample_df$sample_id, c("01", "02")) + testthat::expect_type(resolved$sample_df$alias, "character") + testthat::expect_type(resolved$sample_df$sample_id, "character") +}) + # Explicit discovery/quant flags are passed through to bambu consistently. # NDR is only passed during discovery and omitted when automatic selection is wanted. testthat::test_that("bambu args include requested discovery and quant flags", {