Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quantile normalise custom target #313

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions R/methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -586,19 +586,25 @@ setMethod("scale_abundance", "tidybulk", .scale_abundance)
#' @param .sample The name of the sample column
#' @param .transcript The name of the transcript/gene column
#' @param .abundance The name of the transcript/gene abundance column
#' @param method A character string. Either "limma_normalize_quantiles" for limma::normalizeQuantiles or "preprocesscore_normalize_quantiles_use_target" for preprocessCore::normalize.quantiles.use.target for large-scale dataset, where limmma could not be compatible.
#' @param method A character string. Either "limma_normalize_quantiles" for limma::normalizeQuantiles or "preprocesscore_normalize_quantiles_use_target" for preprocessCore::normalize.quantiles.use.target for large-scale datasets.
#' @param target_distribution A numeric vector. If NULL the target distribution will be calculated by preprocessCore. This argument only affects the "preprocesscore_normalize_quantiles_use_target" method.
#' @param action A character string between "add" (default) and "only". "add" joins the new information to the input tbl (default), "only" return a non-redundant tbl with the just new information.
#'
#'
#' @details Scales transcript abundance compensating for sequencing depth
#' (e.g., with TMM algorithm, Robinson and Oshlack doi.org/10.1186/gb-2010-11-3-r25).
#' Lowly transcribed transcripts/genes (defined with minimum_counts and minimum_proportion parameters)
#' are filtered out from the scaling procedure.
#' The scaling inference is then applied back to all unfiltered data.
#' @details Tranform the feature abundance across samples so to have the same quantile distribution (using preprocessCore).
#'
#' Underlying method
#' edgeR::calcNormFactors(.data, method = c("TMM","TMMwsp","RLE","upperquartile"))
#'
#'
#' If `limma_normalize_quantiles` is chosen
#'
#' .data |>limma::normalizeQuantiles()
#'
#' If `preprocesscore_normalize_quantiles_use_target` is chosen
#'
#' .data |>
#' preprocessCore::normalize.quantiles.use.target(
#' target = preprocessCore::normalize.quantiles.determine.target(.data)
#' )
#'
#'
#' @return A tbl object with additional columns with scaled data as `<NAME OF COUNT COLUMN>_scaled`
Expand All @@ -621,6 +627,7 @@ setGeneric("quantile_normalise_abundance", function(.data,
.transcript = NULL,
.abundance = NULL,
method = "limma_normalize_quantiles",
target_distribution = NULL,
action = "add")
standardGeneric("quantile_normalise_abundance"))

Expand All @@ -630,6 +637,8 @@ setGeneric("quantile_normalise_abundance", function(.data,
.transcript = NULL,
.abundance = NULL,
method = "limma_normalize_quantiles",
target_distribution = NULL,

action = "add")
{

Expand Down Expand Up @@ -685,10 +694,12 @@ setGeneric("quantile_normalise_abundance", function(.data,
BiocManager::install("preprocessCore", ask = FALSE)
}

if(is.null(target_distribution)) target_distribution = preprocessCore::normalize.quantiles.determine.target(.data_norm)

.data_norm_quant =
.data_norm |>
preprocessCore::normalize.quantiles.use.target(
target = preprocessCore::normalize.quantiles.determine.target(.data_norm)
target = target_distribution
)

colnames(.data_norm_quant) = .data_norm |> colnames()
Expand Down
5 changes: 4 additions & 1 deletion R/methods_SE.R
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ setMethod("scale_abundance",
.transcript = NULL,
.abundance = NULL,
method = "limma_normalize_quantiles",
target_distribution = NULL,
action = NULL) {


Expand Down Expand Up @@ -311,10 +312,12 @@ setMethod("scale_abundance",
assay(my_assay) |>
as.matrix()

if(is.null(target_distribution)) target_distribution = preprocessCore::normalize.quantiles.determine.target(.data_norm)

.data_norm =
.data_norm |>
preprocessCore::normalize.quantiles.use.target(
target = preprocessCore::normalize.quantiles.determine.target(.data_norm)
target = target_distribution
)

colnames(.data_norm) = .data |> assay(my_assay) |> colnames()
Expand Down
28 changes: 21 additions & 7 deletions man/quantile_normalise_abundance-methods.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion tests/testthat/test-bulk_methods_SummarizedExperiment.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,21 @@ test_that("quantile normalisation",{
filter(a=="SRR1740035" & b=="ABCB9") |>
dplyr::pull(c_scaled)
)


target_distribution =
se_mini |>
assay( "count") |>
as.matrix() |>
preprocessCore::normalize.quantiles.determine.target()

se_mini |>
quantile_normalise_abundance(
method = "preprocesscore_normalize_quantiles_use_target",
target_distribution = target_distribution
) |>
expect_no_error()


})

test_that("tidybulk SummarizedExperiment normalisation subset",{
Expand Down
Loading