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

add attributes #80

Merged
merged 3 commits into from
Mar 1, 2023
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
19 changes: 3 additions & 16 deletions R/functions_multi_beta_binomial.R
Original file line number Diff line number Diff line change
Expand Up @@ -496,27 +496,14 @@ multi_beta_binomial_glm = function(.data,
add_attr(.cell_group, ".cell_group") |>
add_attr(.count, ".count") |>
add_attr(check_outliers, "check_outliers") |>

add_attr(formula_composition, "formula_composition") |>
add_attr(formula_variability, "formula_variability") |>

test_contrasts(
contrasts = contrasts,
percent_false_positive = percent_false_positive,
test_composition_above_logit_fold_change = test_composition_above_logit_fold_change
) |>

# Add osme attributes
add_attr(get_mean_precision_association(estimates_list$fit), "mean_concentration_association") %>%
when(pass_fit ~ add_attr(., estimates_list$fit, "fit"), ~ (.)) |>

# Attach association mean concentration
add_attr(estimates_list$data_for_model, "model_input") |>
add_attr(estimates_list$truncation_df2, "truncation_df2") |>
add_attr(.sample, ".sample") |>
add_attr(.cell_group, ".cell_group") |>
add_attr(.count, ".count") |>
add_attr(check_outliers, "check_outliers") |>
add_attr(formula_composition, "formula_composition") |>
add_attr(formula_variability, "formula_variability")
)


}
Expand Down
25 changes: 21 additions & 4 deletions R/methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ sccomp_glm_data_frame_counts = function(.data,
#' @param contrasts A vector of character strings. For example if your formula is `~ 0 + treatment` and the factor treatment has values `yes` and `no`, your contrast could be "constrasts = c(treatmentyes - treatmentno)".
#' @param percent_false_positive A real between 0 and 100 non included. This used to identify outliers with a specific false positive rate.
#' @param test_composition_above_logit_fold_change A positive integer. It is the effect threshold used for the hypothesis test. A value of 0.2 correspond to a change in cell proportion of 10% for a cell type with baseline proportion of 50%. That is, a cell type goes from 45% to 50%. When the baseline proportion is closer to 0 or 1 this effect thrshold has consistent value in the logit uncontrained scale.
#' @param pass_fit A boolean. Whether to pass the Stan fit as attribute in the output. Because the Stan fit can be very large, setting this to FALSE can be used to lower the memory imprint to save the output.
#'
#' @return A nested tibble `tbl` with cell_group-wise statistics
#'
Expand All @@ -616,7 +617,8 @@ sccomp_glm_data_frame_counts = function(.data,
test_contrasts <- function(.data,
contrasts = NULL,
percent_false_positive = 5,
test_composition_above_logit_fold_change = 0.2) {
test_composition_above_logit_fold_change = 0.2,
pass_fit = TRUE) {
UseMethod("test_contrasts", .data)
}

Expand All @@ -626,7 +628,8 @@ test_contrasts <- function(.data,
test_contrasts.data.frame = function(.data,
contrasts = NULL,
percent_false_positive = 5,
test_composition_above_logit_fold_change = 0.2){
test_composition_above_logit_fold_change = 0.2,
pass_fit = TRUE){


.sample = .data |> attr(".sample")
Expand All @@ -640,7 +643,7 @@ test_contrasts.data.frame = function(.data,
abundance_CI =
get_abundance_contrast_draws(.data, contrasts) %>%

# If my constrasts do not match my model. I have to do something more elegant.
# If my contrasts do not match my model. I have to do something more elegant.
when(
"parameter" %in% colnames(.) ~ draws_to_statistics(
.,
Expand Down Expand Up @@ -702,7 +705,21 @@ test_contrasts.data.frame = function(.data,
nest(count_data = -!!.cell_group),
by = quo_name(.cell_group)
)
)
) |>

# Add back attributes
add_attr(.data |> attr("fit") |> get_mean_precision_association(), "mean_concentration_association") %>%
when(pass_fit ~ add_attr(., .data |> attr("fit") , "fit"), ~ (.)) |>

# Attach association mean concentration
add_attr(.data |> attr("model_input") , "model_input") |>
add_attr(.data |> attr("truncation_df2"), "truncation_df2") |>
add_attr(.sample, ".sample") |>
add_attr(.cell_group, ".cell_group") |>
add_attr(.count, ".count") |>
add_attr(check_outliers, "check_outliers") |>
add_attr(.data |> attr("formula_composition"), "formula_composition") |>
add_attr(.data |> attr("formula_variability"), "formula_variability")
}

#' sccomp_replicate
Expand Down
5 changes: 4 additions & 1 deletion man/test_contrasts.Rd

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

22 changes: 22 additions & 0 deletions tests/testthat/test-sccomp_.R
Original file line number Diff line number Diff line change
Expand Up @@ -427,3 +427,25 @@ test_that("plot test variability",{


})

test_that("test constrasts",{


estimate =
seurat_obj |>
sccomp_glm(
formula_composition = ~ type ,
formula_variability = ~ 1,
sample, cell_group,
check_outliers = FALSE,
approximate_posterior_inference = FALSE,
cores = 1,
mcmc_seed = 42
)

new_test =
estimate |>
test_contrasts() |>
test_contrasts()

})