Skip to content

Commit

Permalink
edit dispersion for SE methods
Browse files Browse the repository at this point in the history
  • Loading branch information
stemangiola committed Dec 7, 2023
1 parent 33c5dcd commit 0693ee1
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
9 changes: 8 additions & 1 deletion R/functions_SE.R
Original file line number Diff line number Diff line change
Expand Up @@ -1133,10 +1133,17 @@ get_differential_transcript_abundance_glmmSeq_SE <- function(.data,
.data %>%
assay(my_assay)

# Create design matrix for dispersion, removing random effects
design =
model.matrix(
object = .formula |> eliminate_random_effects(),
data = metadata
)

if(quo_is_symbolic(.dispersion))
dispersion = rowData(.data)[,quo_name(.dispersion),drop=FALSE] |> as_tibble(rownames = feature__$name) |> deframe()
else
dispersion = setNames(edgeR::estimateDisp(counts)$tagwise.dispersion, rownames(counts))
dispersion = counts |> edgeR::estimateDisp(design = design) %$% tagwise.dispersion |> setNames(rownames(counts))

# # Check dispersion
# if(!names(dispersion) |> sort() |> identical(
Expand Down
29 changes: 29 additions & 0 deletions R/methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -2600,6 +2600,7 @@ setMethod("ensembl_to_symbol", "tidybulk", .ensembl_to_symbol)
#' All methods use raw counts, irrespective of if scale_abundance or adjust_abundance have been calculated, therefore it is essential to add covariates such as batch effects (if applicable) in the formula.
#'
#' Underlying method for edgeR framework:
#'
#' .data |>
#'
#' # Filter
Expand All @@ -2623,7 +2624,10 @@ setMethod("ensembl_to_symbol", "tidybulk", .ensembl_to_symbol)
#' edgeR::glmQLFit(design) |> // or glmFit according to choice
#' edgeR::glmQLFTest(coef = 2, contrast = my_contrasts) // or glmLRT according to choice
#'
#'
#'
#' Underlying method for DESeq2 framework:
#'
#' keep_abundant(
#' factor_of_interest = !!as.symbol(parse_formula(.formula)[[1]]),
#' minimum_counts = minimum_counts,
Expand All @@ -2636,6 +2640,31 @@ setMethod("ensembl_to_symbol", "tidybulk", .ensembl_to_symbol)
#' DESeq2::results()
#'
#'
#'
#' Underlying method for glmmSeq framework:
#'
#' counts =
#' .data %>%
#' assay(my_assay)
#'
#' # Create design matrix for dispersion, removing random effects
#' design =
#' model.matrix(
#' object = .formula |> eliminate_random_effects(),
#' data = metadata
#' )
#'
#' dispersion = counts |> edgeR::estimateDisp(design = design) %$% tagwise.dispersion |> setNames(rownames(counts))
#'
#' glmmSeq( .formula,
#' countdata = counts ,
#' metadata = metadata |> as.data.frame(),
#' dispersion = dispersion,
#' progress = TRUE,
#' method = method |> str_remove("(?i)^glmmSeq_" ),
#' )
#'
#'
#' @return A consistent object (to the input) with additional columns for the statistics from the test (e.g., log fold change, p-value and false discovery rate).
#'
#'
Expand Down
29 changes: 29 additions & 0 deletions man/test_differential_abundance-methods.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/test-bulk_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ test_that("differential trancript abundance - random effects",{
pull(P_condition_adjusted) |>
head(4) |>
expect_equal(
c(0.02381167, 0.01097209, 0.01056741, 0.02381167),
c(0.03643414, 0.02938584, 0.02938584, 0.03643414),
tolerance=1e-3
)

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-bulk_methods_SummarizedExperiment.R
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ test_that("differential trancript abundance - random effects SE",{
rowData(res)[,"P_condition_adjusted"] |>
head(4) |>
expect_equal(
c(0.1065866, 0.1109067, 0.1116562 , NA),
c(0.03394914, 0.03394914, 0.03394914, NA),
tolerance=1e-2
)

Expand Down

0 comments on commit 0693ee1

Please sign in to comment.