diff --git a/NAMESPACE b/NAMESPACE index 777fa855..7e7e018c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -72,6 +72,8 @@ importFrom(graphics,barplot) importFrom(graphics,par) importFrom(methods,as) importFrom(methods,is) +importFrom(methods,setGeneric) +importFrom(methods,setMethod) importFrom(parallel,mclapply) importFrom(rlang,.data) importFrom(stats,cor) diff --git a/R/fixCompartments.R b/R/fixCompartments.R index 8485a952..867ff354 100644 --- a/R/fixCompartments.R +++ b/R/fixCompartments.R @@ -2,7 +2,7 @@ #' #' @name fixCompartments #' -#' @param obj Input RaggedExperiment or output of condenseSE +#' @param x Input RaggedExperiment or output of condenseSE #' @param min.conf Minimum confidence score to use #' @param parallel Whether to run in parallel #' @param cores How many cores to use if running in parallel @@ -10,24 +10,28 @@ #' @return A "fixed" set of compartments #' @import RaggedExperiment #' @import SummarizedExperiment +#' @importFrom methods setGeneric setMethod standardGeneric #' @export #' #' @examples -fixCompartments <- function(obj, min.conf = 0.8, parallel = FALSE, cores = 1) { - # this function will invert or "fix" compartments based on bootstrapping results - if (is(obj, "RaggedExperiment")) obj <- condenseSE(obj, sample.name = colnames(assay(obj))) +setGeneric("fixCompartments", function(x, min.conf = 0.8, parallel = FALSE, cores = 1) { + standardGeneric("fixCompartments") +}) - # if we somehow only have 1 sample - if (is(obj, "GRanges")) { - return(flipper(obj, min.conf)) - } +setMethod("fixCompartments", "GRanges", function(x, min.conf = 0.8, parallel = FALSE, cores = 1) { + message("Assuming we only have a single sample to process") + message("Fixing compartments using a minimum confidence score of ", min.conf * 100, "%") + flipper(x, min.conf) +}) +setMethod("fixCompartments", "RaggedExperiment", function(x, min.conf = 0.8, parallel = FALSE, cores = 1) { + obj <- condenseSE(x, sample.name = colnames(assay(x))) message("Fixing compartments using a minimum confidence score of ", min.conf * 100, "%") # go through and invert compartments based on the min.conf flip_compartments_lst <- mclapply(obj, flipper, min.conf, mc.cores = ifelse(parallel, cores, 1)) names(flip_compartments_lst) <- names(obj) - return(flip_compartments_lst) -} + RaggedExperiment(flip_compartments_lst) +}) #' Helper to invert, or "fix", compartments that have a minimum confidence score (1-min.conf) #' @@ -41,8 +45,6 @@ flipper <- function(input_obj, min.conf) { stop("Bootstrapping was not performed. Cannot fix compartments.") } - message("Assuming we only have a single sample to process.") - message("Fixing compartments using a minimum confidence score of ", min.conf * 100, "%") invert_compartments <- apply(mcols(input_obj), 1, .inverter, min.conf) mcols(input_obj)$flip.compartment <- invert_compartments diff --git a/man/fixCompartments.Rd b/man/fixCompartments.Rd index abaaf6e2..71d322f6 100644 --- a/man/fixCompartments.Rd +++ b/man/fixCompartments.Rd @@ -4,10 +4,10 @@ \alias{fixCompartments} \title{Invert, or "fix", compartments that have a minimum confidence score (1-min.conf)} \usage{ -fixCompartments(obj, min.conf = 0.8, parallel = FALSE, cores = 1) +fixCompartments(x, min.conf = 0.8, parallel = FALSE, cores = 1) } \arguments{ -\item{obj}{Input RaggedExperiment or output of condenseSE} +\item{x}{Input RaggedExperiment or output of condenseSE} \item{min.conf}{Minimum confidence score to use} diff --git a/tests/testthat/test-fixCompartments.R b/tests/testthat/test-fixCompartments.R index e4b7cc28..f647a331 100644 --- a/tests/testthat/test-fixCompartments.R +++ b/tests/testthat/test-fixCompartments.R @@ -90,6 +90,6 @@ test_that("fixCompartments", { re <- RaggedExperiment(A = gr1, B = gr2, C = gr3) fix_result <- fixCompartments(re) - expect_length(fix_result, length(colnames(re))) - expect_equal(names(fix_result), c("A", "B", "C")) + expect_equal(ncol(fix_result), ncol(re)) + expect_equal(colnames(fix_result), c("A", "B", "C")) }) diff --git a/vignettes/compartmap.Rmd b/vignettes/compartmap.Rmd index 86324010..3d805f05 100644 --- a/vignettes/compartmap.Rmd +++ b/vignettes/compartmap.Rmd @@ -87,7 +87,8 @@ k562_compartments.boot.fix <- fixCompartments( ) # Look at the first cell in the GRangesList object -k562_compartments.boot.fix[[1]] +k562_compartments.grlist <- condenseSE(k562_compartments.boot.fix) +k562_compartments.grlist[[1]] ``` ### Visualization of inferred chromatin domains @@ -107,7 +108,7 @@ intervals and median confidence estimate: ```{r postprocessPlot, warning=FALSE} plotAB( - k562_compartments.boot.fix[[1]], + k562_compartments.grlist[[1]], chr = "chr14", with.ci = TRUE, median.conf = TRUE @@ -140,7 +141,7 @@ extract single-cell domain inflections: ```{r postprocessInflections, message=FALSE, warning=FALSE} k562_cell_1_inflections <- getDomainInflections( - k562_compartments.boot.fix[[1]], + k562_compartments.grlist[[1]], what = "flip.score", res = 1e6, chrs = "chr14",