Skip to content

Commit

Permalink
add custom dispersion for tibble input
Browse files Browse the repository at this point in the history
  • Loading branch information
stemangiola committed Aug 29, 2023
1 parent d56afc0 commit cb96f32
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
6 changes: 4 additions & 2 deletions R/functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -685,13 +685,15 @@ get_differential_transcript_abundance_glmmSeq <- function(.data,
omit_contrast_in_colnames = FALSE,
prefix = "",
.sample_total_read_count = NULL,
.dispersion = NULL,
...) {
# Get column names
.sample = enquo(.sample)
.transcript = enquo(.transcript)
.abundance = enquo(.abundance)
.sample_total_read_count = enquo(.sample_total_read_count)

.dispersion = enquo(.dispersion)

# Check if omit_contrast_in_colnames is correctly setup
if(omit_contrast_in_colnames & length(.contrasts) > 1){
warning("tidybulk says: you can omit contrasts in column names only when maximum one contrast is present")
Expand Down Expand Up @@ -757,7 +759,7 @@ get_differential_transcript_abundance_glmmSeq <- function(.data,
counts = counts[,rownames(metadata),drop=FALSE]

if(quo_is_symbolic(.dispersion))
dispersion = .data |> pivot_transcript() |> select(!!feature__$symbol, !!.dispersion) |> deframe()
dispersion = .data |> pivot_transcript(!!.transcript) |> select(!!.transcript, !!.dispersion) |> deframe()
else
dispersion = setNames(edgeR::estimateDisp(counts)$tagwise.dispersion, rownames(counts))

Expand Down
38 changes: 34 additions & 4 deletions tests/testthat/test-bulk_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -835,18 +835,22 @@ test_that("DESeq2 differential trancript abundance - no object",{

test_that("differential trancript abundance - random effects",{

input_df |>
my_input =
input_df |>
identify_abundant(a, b, c, factor_of_interest = condition) |>
mutate(time = time |> stringr::str_replace_all(" ", "_")) |>

filter(b %in% c("ABCB4" , "ABCB9" , "ACAP1", "ACHE", "ACP5" , "ADAM28"))|>

filter(b %in% c("ABCB4" , "ABCB9" , "ACAP1", "ACHE", "ACP5" , "ADAM28"))

my_input |>
test_differential_abundance(
~ condition + (1 + condition | time),
.sample = a,
.transcript = b,
.abundance = c,
method = "glmmseq_lme4",
action="only"
action="only",
cores = 1
) |>
pull(P_condition_adjusted) |>
head(4) |>
Expand All @@ -855,6 +859,32 @@ test_that("differential trancript abundance - random effects",{
tolerance=1e-3
)

# Custom dispersion
my_input =
my_input |>
left_join(
my_input |> pivot_transcript(b) |> mutate(disp_ = 2 ),
by = join_by(b, entrez, .abundant)
)


my_input |>
test_differential_abundance(
~ condition + (1 + condition | time),
.sample = a,
.transcript = b,
.abundance = c,
method = "glmmseq_lme4",
action="only",
cores = 1,
.dispersion = disp_
) |>
pull(P_condition_adjusted) |>
head(4) |>
expect_equal(
c(0.1081176, 0.1303558, 0.1303558, 0.1693276),
tolerance=1e-3
)

})

Expand Down
13 changes: 7 additions & 6 deletions tests/testthat/test-bulk_methods_SummarizedExperiment.R
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,8 @@ test_that("differential trancript abundance - random effects SE",{
#mutate(time = time |> stringr::str_replace_all(" ", "_")) |>
test_differential_abundance(
~ condition + (1 + condition | time),
method = "glmmseq_lme4"
method = "glmmseq_lme4",
cores = 1
)

rowData(res)[,"P_condition_adjusted"] |>
Expand All @@ -480,22 +481,22 @@ test_that("differential trancript abundance - random effects SE",{
se_mini |>
identify_abundant(factor_of_interest = condition)

rowData(se_mini)$disp_ = rnorm(nrow(se_mini))
rowData(se_mini)$disp_ = rep(2,nrow(se_mini))

res =
se_mini |>
identify_abundant(factor_of_interest = condition) |>
se_mini[1:10,] |>
#mutate(time = time |> stringr::str_replace_all(" ", "_")) |>
test_differential_abundance(
~ condition + (1 + condition | time),
method = "glmmseq_lme4",
dispersion = disp_
.dispersion = disp_,
cores = 1
)

rowData(res)[,"P_condition_adjusted"] |>
head(4) |>
expect_equal(
c(0.1441371, 0.1066183, 0.1370748, NA),
c(0.1153254, 0.1668555, 0.1668555 , NA),
tolerance=1e-3
)

Expand Down

0 comments on commit cb96f32

Please sign in to comment.