Skip to content

Commit

Permalink
add test for .clean_merge
Browse files Browse the repository at this point in the history
  • Loading branch information
philouail committed Sep 19, 2024
1 parent c2c055c commit 0a27d69
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
8 changes: 4 additions & 4 deletions R/MsExperiment.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
39 changes: 39 additions & 0 deletions tests/testthat/test_MetaboLightsParam.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})


0 comments on commit 0a27d69

Please sign in to comment.