diff --git a/R/MsExperiment.R b/R/MsExperiment.R index 9ecba6c..2ffff28 100644 --- a/R/MsExperiment.R +++ b/R/MsExperiment.R @@ -300,16 +300,16 @@ setMethod("readMsObject", .clean_merged <- function(x, keepProtocol, keepOntology, simplify) { # remove ontology if (!keepOntology) - x <- x[, -which(grepl("Term", names(x)))] + x <- x[, -which(grepl("Term", names(x))), drop = FALSE] # remove protocol if (!keepProtocol) - x <- x[, -which(grepl("Protocol|Parameter", names(x)))] + x <- x[, -which(grepl("Protocol|Parameter", names(x))), drop = FALSE] # remove duplicated columns contents and NAs if (simplify) { - x <- x[, !duplicated(as.list(x))] - x <- x[, colSums(is.na(x)) != nrow(x)] + x <- x[, !duplicated(as.list(x)), drop = FALSE] + x <- x[, colSums(is.na(x)) != nrow(x), drop = FALSE] } return(x) } diff --git a/tests/testthat/test_MetaboLightsParam.R b/tests/testthat/test_MetaboLightsParam.R index d42b378..fcc16c0 100644 --- a/tests/testthat/test_MetaboLightsParam.R +++ b/tests/testthat/test_MetaboLightsParam.R @@ -33,3 +33,42 @@ test_that("interactive session works", { expect_true(ncol(result@sampleData) == 30) }) +test_that(".clean_merged function works correctly", { + tbc <- data.frame( + Protocol_A = c(1, 2, 3), + Term_B = c("ontology1", "ontology2", "ontology3"), + Parameter_C = c(10, 20, 30), + Term_D = c("ontology1", "ontology2", "ontology3"), + Data_E = c(NA, NA, NA), + Duplicate_F = c(1, 2, 3), + stringsAsFactors = FALSE + ) + result <- .clean_merged(tbc, keepProtocol = TRUE, + keepOntology = TRUE, + simplify = FALSE) + expect_equal(names(result), names(tbc)) + + result <- .clean_merged(tbc, keepProtocol = TRUE, + keepOntology = FALSE, simplify = FALSE) + expect_equal(names(result), c("Protocol_A", "Parameter_C", "Data_E", + "Duplicate_F")) + + result <- .clean_merged(tbc, keepProtocol = FALSE, + keepOntology = TRUE, simplify = FALSE) + expect_equal(names(result), c("Term_B", "Term_D", "Data_E", "Duplicate_F")) + + result <- .clean_merged(tbc, keepProtocol = FALSE, keepOntology = FALSE, + simplify = FALSE) + expect_equal(names(result), c("Data_E", "Duplicate_F")) + + result <- .clean_merged(tbc, keepProtocol = TRUE, keepOntology = TRUE, + simplify = TRUE) + expect_equal(names(result), c("Protocol_A", "Term_B", "Parameter_C")) + + + result <- .clean_merged(tbc, keepProtocol = FALSE, keepOntology = FALSE, + simplify = TRUE) + expect_equal(names(result), "Duplicate_F") +}) + +