Skip to content

Commit 39d16f8

Browse files
committed
Catch problems with enrichments early
* termIDs must be unique * extra columns are removed
1 parent 57e04f6 commit 39d16f8

7 files changed

Lines changed: 94 additions & 8 deletions

File tree

R/add.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,9 @@ addResults <- function(study, results, reset = FALSE) {
360360
#' of these elements should be a data frame with enrichment results. Each
361361
#' table must contain the following columns: "termID", "description",
362362
#' "nominal" (the nominal statistics), and "adjusted" (the statistics after
363-
#' adjusting for multiple testing). Any additional columns are ignored.
363+
#' adjusting for multiple testing). Any additional columns are ignored and
364+
#' removed. The first column should be "termID", and it should only contain
365+
#' unique values.
364366
#' @inherit shared-add
365367
#'
366368
#' @seealso \code{\link{getEnrichments}}

R/check.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,7 @@ checkEnrichments <- function(enrichments) {
396396
if ("adjusted" %in% colnames(test) && !is.numeric(test$adjusted)) {
397397
stop("Column 'adjusted' from enrichments must be numeric")
398398
}
399-
enrichments[[i]][[j]][[k]] <-
400-
test[, c("termID", "description", "nominal", "adjusted")]
399+
hasUniqueIdColumn(test)
401400
}
402401
}
403402
}

R/sanitize.R

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,19 @@ sanitizeEnrichments <- function(enrichments) {
5252
for (i in seq_along(enrichments)) {
5353
for (j in seq_along(enrichments[[i]])) {
5454
for (k in seq_along(enrichments[[i]][[j]])) {
55-
enrichments[[i]][[j]][[k]] <- as.data.frame(enrichments[[i]][[j]][[k]])
55+
theTable <- enrichments[[i]][[j]][[k]]
56+
theTable <- as.data.frame(theTable)
57+
columnsCurrent <- names(theTable)
58+
columnsToKeep <- c("termID", "description", "nominal", "adjusted")
59+
columnsToRemove <- !columnsCurrent %in% columnsToKeep
60+
if (any(columnsToRemove)) {
61+
warning(
62+
"The following columns were removed from the enrichments table: ",
63+
paste(columnsCurrent[columnsToRemove], collapse = ", ")
64+
)
65+
}
66+
theTable <- theTable[, columnsToKeep]
67+
enrichments[[i]][[j]][[k]] <- theTable
5668
}
5769
}
5870
}

inst/tinytest/testApp.R

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,20 @@ expect_identical_xl(
231231

232232
# getEnrichmentsTable ----------------------------------------------------------
233233

234-
enrichmentsTable <- getEnrichmentsTable(testStudyName, testModelName, testAnnotationName)
234+
enrichmentsTable <- getEnrichmentsTable(
235+
study = testStudyName,
236+
modelID = testModelName,
237+
annotationID = testAnnotationName
238+
)
235239

236240
expect_identical_xl(
237241
class(enrichmentsTable),
238242
"data.frame"
239243
)
240244

241-
expect_true_xl(all(names(getTests(testStudyName, testModelName)) %in% colnames(enrichmentsTable)))
245+
expect_true_xl(
246+
all(names(getTests(testStudyName, testModelName)) %in% colnames(enrichmentsTable))
247+
)
242248

243249
expect_error_xl(
244250
getEnrichmentsTable(1),
@@ -250,6 +256,30 @@ expect_equal_xl(
250256
getEnrichmentsTable(testStudyObj, testModelName, testAnnotationName)
251257
)
252258

259+
perTestenrichments <- getEnrichments(
260+
study = testStudyObj,
261+
modelID = testModelName,
262+
annotationID = testAnnotationName,
263+
testID = testTestName
264+
)
265+
266+
expect_equal_xl(
267+
enrichmentsTable[[testTestName]],
268+
perTestenrichments[["nominal"]]
269+
)
270+
271+
enrichmentsTableAdj <- getEnrichmentsTable(
272+
study = testStudyName,
273+
modelID = testModelName,
274+
annotationID = testAnnotationName,
275+
type = "adjusted"
276+
)
277+
278+
expect_equal_xl(
279+
enrichmentsTableAdj[[testTestName]],
280+
perTestenrichments[["adjusted"]]
281+
)
282+
253283
# getEnrichmentsNetwork --------------------------------------------------------
254284

255285
enrichmentsNetwork <- getEnrichmentsNetwork(

inst/tinytest/testCheck.R

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,45 @@ expect_error_xl(
527527
"Column 'adjusted' from enrichments must be numeric"
528528
)
529529

530+
# Results for each annotationID/termID/testID must be unique
531+
enrichmentswithDups <- OmicNavigator:::testEnrichments(
532+
nModels = 1,
533+
nAnnotations = 1,
534+
nTests = 1
535+
)
536+
enrichmentswithDups[["model_01"]][["annotation_01"]][["test_01"]] <- rbind(
537+
enrichmentswithDups[["model_01"]][["annotation_01"]][["test_01"]],
538+
enrichmentswithDups[["model_01"]][["annotation_01"]][["test_01"]][1, ]
539+
)
540+
541+
expect_error_xl(
542+
addEnrichments(study, enrichments = enrichmentswithDups),
543+
"The first column, \"termID\", must contain unique values"
544+
)
545+
546+
# Duplicate columns are removed with warning to user
547+
enrichmentswithExtraCols <- OmicNavigator:::testEnrichments(
548+
nModels = 1,
549+
nAnnotations = 1,
550+
nTests = 1
551+
)
552+
553+
enrichmentswithExtraCols[["model_01"]][["annotation_01"]][["test_01"]] <- cbind(
554+
enrichmentswithExtraCols[["model_01"]][["annotation_01"]][["test_01"]],
555+
extra1 = "remove me",
556+
extra2 = "remove me too"
557+
)
558+
559+
expect_warning_xl(
560+
studyExtraCols <- addEnrichments(study, enrichments = enrichmentswithExtraCols),
561+
"The following columns were removed from the enrichments table: extra1, extra2"
562+
)
563+
564+
expect_identical_xl(
565+
colnames(studyExtraCols[["enrichments"]][["model_01"]][["annotation_01"]][["test_01"]]),
566+
c("termID", "description", "nominal", "adjusted")
567+
)
568+
530569
# checkMetaFeatures ------------------------------------------------------------
531570

532571
expect_error_xl(

man/addEnrichments.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/createStudy.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)