Skip to content

Commit

Permalink
Allow specification which cells are test cells
Browse files Browse the repository at this point in the history
  • Loading branch information
const-ae committed Sep 29, 2023
1 parent 22066a7 commit 52ecfef
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion R/find_de_neighborhoods.R
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ neighborhood_count_test <- function(de_regions, counts, group_by, contrast, desi
}

if(add_diff_in_diff){
if(verbose) message("Fit diff-in-diff effect")
mm <- if(method == "glmGamPoi") glm_regions$model_matrix else glm_regions$design
mat <- assay(region_psce, "masked_counts")
complement_mat <- assay(region_psce, "counts") - assay(region_psce, "masked_counts")
Expand All @@ -530,7 +531,6 @@ neighborhood_count_test <- function(de_regions, counts, group_by, contrast, desi
mod_size_factor_matrix <- pseudobulk_size_factors_for_neighborhoods(cbind(counts, counts), mask = cbind(mask, 1-mask),
col_data = mod_col_data, group_by = c({{group_by}}, vars(..did_indicator)),
method = size_factor_method, verbose = verbose)
if(verbose) message("Fit diff-in-diff effect")
if(method == "glmGamPoi"){
did_fit <- glmGamPoi::glm_gp(comb_mat, design = comb_design_mat, verbose = FALSE,
offset = log(mod_size_factor_matrix + 1e-10),
Expand Down
10 changes: 8 additions & 2 deletions R/lemur.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
#' @param use_assay if `data` is a `SummarizedExperiment` / `SingleCellExperiment` object,
#' which assay should be used.
#' @param test_fraction the fraction of cells that are split of before the model fit to keep an
#' independent set of test observations. Default: 20% (`0.2`).
#' independent set of test observations. Alternatively, a logical vector of length `ncol(data)`.
#' Default: 20% (`0.2`).
#' @param ... additional parameters that are passed on to the internal function `lemur_impl`.
#' @param verbose Should the method print information during the fitting. Default: `TRUE`.
#'
Expand Down Expand Up @@ -61,7 +62,12 @@ lemur <- function(data, design = ~ 1, col_data = NULL,

# Create indicator vector which cells are used for training and which for testing
is_test_data <- rep(FALSE, ncol(data))
if(test_fraction < 0 || test_fraction >= 1){
if(is.logical(test_fraction) && length(ncol(data))){
if(any(is.na(test_fraction))) stop("test_fraction must not contain 'NA's.")
is_test_data <- test_fraction
}else if(length(test_fraction) != 1){
stop("'test_fraction' must be a boolean vector of length 'ncol(data)' or a single number between 0 and 1.")
}else if(test_fraction < 0 || test_fraction >= 1){
stop("'test_fraction' must be at least 0 and smaller than 1.")
}else{
if(verbose) message("Storing ", round(test_fraction, 2) * 100, "% of the data (", round(ncol(data) * test_fraction), " cells)",
Expand Down
3 changes: 2 additions & 1 deletion man/lemur.Rd

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

0 comments on commit 52ecfef

Please sign in to comment.