-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
89be7f6
commit cf351da
Showing
12 changed files
with
195 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
|
||
#' Batch Correction | ||
#' | ||
#' @description `PomaBatch` performs batch correction on a `SummarizedExperiment` object given a batch factor variable. | ||
#' | ||
#' @param data A `SummarizedExperiment` object. | ||
#' @param batch Character. The name of the column in `colData` that contains the batch information. | ||
#' @param mod Character vector. Indicates the names of `colData` columns to be included as covariates. Default is NULL (no covariates). | ||
#' | ||
#' @export | ||
#' | ||
#' @return A `SummarizedExperiment` object with batch-corrected data. | ||
#' @references Leek JT, Johnson WE, Parker HS, Fertig EJ, Jaffe AE, Zhang Y, Storey JD, Torres LC (2023). sva: Surrogate Variable Analysis. doi:10.18129/B9.bioc.sva <https://doi.org/10.18129/B9.bioc.sva> | ||
#' @author Pol Castellano-Escuder | ||
#' | ||
#' @importFrom magrittr %>% | ||
#' | ||
#' @examples | ||
#' data("st000284") | ||
#' | ||
#' st000284 %>% | ||
#' PomaImpute(method = "knn") %>% | ||
#' PomaBatch(batch = "gender") | ||
PomaBatch <- function(data, | ||
batch, | ||
mod = NULL) { | ||
|
||
if (!is(data, "SummarizedExperiment")) { | ||
stop("data is not a SummarizedExperiment object. \nSee POMA::PomaCreateObject or SummarizedExperiment::SummarizedExperiment") | ||
} | ||
if (!batch %in% names(SummarizedExperiment::colData(data))) { | ||
stop("Specified batch column not found in colData") | ||
} | ||
|
||
batch_info <- as.factor(SummarizedExperiment::colData(data)[, batch]) | ||
to_batch <- SummarizedExperiment::assay(data) | ||
|
||
if (!is.null(mod)) { | ||
covariates <- SummarizedExperiment::colData(data) %>% | ||
as.data.frame() %>% | ||
dplyr::select_at(dplyr::vars(dplyr::matches(mod))) | ||
|
||
mod_matrix <- stats::model.matrix(~ 0 + ., data = covariates) | ||
} else { | ||
mod_matrix <- NULL | ||
} | ||
|
||
corrected_data <- sva::ComBat(dat = to_batch, batch = batch_info, mod = mod_matrix) | ||
|
||
data <- SummarizedExperiment::SummarizedExperiment(assays = corrected_data, | ||
colData = SummarizedExperiment::colData(data)) | ||
|
||
if (validObject(data)) | ||
return(data) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
test_that("PomaBatch handles valid SummarizedExperiment objects", { | ||
data <- create_mock_summarized_experiment() | ||
SummarizedExperiment::colData(data)$batch <- factor(rep(c("Batch1", "Batch2"), each = ncol(data)/2)) | ||
corrected_data <- PomaBatch(data, batch = "batch") | ||
expect_is(corrected_data, "SummarizedExperiment") | ||
}) | ||
|
||
test_that("PomaBatch stops with non-SummarizedExperiment objects", { | ||
data <- data.frame(matrix(runif(100), ncol = 10)) | ||
expect_error(PomaBatch(data, batch = "batch")) | ||
}) | ||
|
||
test_that("PomaBatch stops if batch column is not found", { | ||
data <- create_mock_summarized_experiment() | ||
expect_error(PomaBatch(data, batch = "nonexistent_batch")) | ||
}) | ||
|
||
test_that("PomaBatch handles additional covariates", { | ||
data <- create_mock_summarized_experiment() | ||
SummarizedExperiment::colData(data)$batch <- factor(rep(c("Batch1", "Batch2"), each = ncol(data)/2)) | ||
SummarizedExperiment::colData(data)$covariate <- rnorm(nrow(SummarizedExperiment::colData(data))) | ||
corrected_data <- PomaBatch(data, batch = "batch", mod = c("covariate")) | ||
expect_is(corrected_data, "SummarizedExperiment") | ||
}) | ||
|
Oops, something went wrong.