Skip to content

Commit

Permalink
minor changes for v0.7.8
Browse files Browse the repository at this point in the history
  • Loading branch information
jr-leary7 committed Nov 17, 2023
1 parent 9e7ed17 commit 24e7775
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 86 deletions.
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Changes in version 0.7.8

+ Added progress bar to `testDynamic()`.
+ Changed parallel backend in `testDynamic()` from `doParallel` to `doSNOW` in order to make this possible.
+ Updated documentation with more runnable examples.
+ Passing `BiocCheck` with no errors.
+ Reduced set of exported functions to just what's necessary for model fitting & downstream analysis.

# Changes in version 0.7.7

+ Added DOI badge to README.
Expand Down
8 changes: 0 additions & 8 deletions R/ModelLRT.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@
#' @param mod.0 The model corresponding to the null hypothesis. Defaults to NULL.
#' @param is.glmm Are the models being compared GLMMs? Defaults to FALSE.
#' @return A list containing the LRT test statistic, degrees freedom, and the \emph{p}-value computed using the Chi-squared assumption.
#' @export
#' @examples
#' \dontrun{
#' modelLRT(mod.1 = marge_mod, mod.0 = null_mod)
#' modelLRT(mod.1 = marge_mod,
#' mod.0 = null_mod,
#' is.glmm = TRUE)
#' }

modelLRT <- function(mod.1 = NULL,
mod.0 = NULL,
Expand Down
3 changes: 3 additions & 0 deletions R/embedGenes.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,8 @@ embedGenes <- function(smoothed.counts = NULL,
umap1 = smoothed_counts_umap[, 1],
umap2 = smoothed_counts_umap[, 2])
gene_df <- dplyr::bind_cols(gene_df, pca_df)
if (!cluster.genes) {
gene_df <- dplyr::select(gene_df, -leiden)
}
return(gene_df)
}
9 changes: 0 additions & 9 deletions R/stripGLM.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,7 @@
#' @description This function removes a \emph{lot} of components from the default GLM object in order to make it take up less memory. It does however retain enough pieces for \code{predict()} to still work. No promises beyond that.
#' @param glm.obj An object of class GLM from which you'd like to strip out unnecessary components. Defaults to NULL.
#' @return A slimmed-down \code{glm} object.
#' @export
#' @seealso \code{\link{glm}}
#' @examples
#' data(sim_counts)
#' data(sim_pseudotime)
#' cell_offset <- createCellOffset(sim_counts)
#' marge_model <- marge2(sim_pseudotime,
#' Y = BiocGenerics::counts(sim_counts)[4, ],
#' Y.offset = cell_offset)
#' smaller_model <- stripGLM(marge_model$final_mod)

stripGLM <- function(glm.obj = NULL) {
# check inputs
Expand Down
53 changes: 31 additions & 22 deletions R/testDynamic.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
#' @import magrittr
#' @importFrom Matrix t
#' @importFrom bigstatsr as_FBM
#' @importFrom utils txtProgressBar setTxtProgressBar
#' @importFrom foreach foreach %dopar% registerDoSEQ
#' @importFrom doParallel registerDoParallel
#' @importFrom doSNOW registerDoSNOW
#' @importFrom parallel makeCluster stopCluster clusterEvalQ clusterExport clusterSetRNGStream
#' @importFrom withr with_output_sink
#' @importFrom MASS glm.nb negative.binomial theta.mm
Expand All @@ -30,7 +31,7 @@
#' @param n.cores (Optional) If running in parallel, how many cores should be used? Defaults to 2.
#' @param approx.knot (Optional) Should the knot space be reduced in order to improve computation time? Defaults to TRUE.
#' @param glmm.adaptive (Optional) Should the basis functions for the GLMM be chosen adaptively? If not, uses 4 evenly spaced knots. Defaults to FALSE.
#' @param verbose (Optional) A boolean indicating whether the amount of time the function takes to run should be tracked and printed to the console. Defaults to TRUE.
#' @param verbose (Optional) A boolean indicating whether a progress bar should be printed to the console. Defaults to TRUE.
#' @param random.seed (Optional) The random seed used to initialize RNG streams in parallel. Defaults to 312.
#' @details
#' \itemize{
Expand Down Expand Up @@ -110,14 +111,21 @@ testDynamic <- function(expr.mat = NULL,
if (is.gee && !(cor.structure %in% c("ar1", "independence", "exchangeable"))) { stop("GEE models require a specified correlation structure.") }

# set up time tracking
start_time <- Sys.time()

# set up progress bar
if (verbose) {
start_time <- Sys.time()
pb <- utils::txtProgressBar(0, length(genes), style = 3)
progress_fun <- function(n) utils::setTxtProgressBar(pb, n)
snow_opts <- list(progress = progress_fun)
} else {
snow_opts <- list()
}

# set up parallel processing
if (parallel.exec) {
cl <- parallel::makeCluster(n.cores)
doParallel::registerDoParallel(cl)
doSNOW::registerDoSNOW(cl)
parallel::clusterSetRNGStream(cl, iseed = random.seed)
} else {
cl <- foreach::registerDoSEQ()
Expand Down Expand Up @@ -155,7 +163,8 @@ testDynamic <- function(expr.mat = NULL,
.noexport = no_export,
.errorhandling = "pass",
.inorder = TRUE,
.verbose = FALSE) %dopar% {
.verbose = FALSE,
.options.snow = snow_opts) %dopar% {
lineage_list <- vector("list", n_lineages)
for (j in seq(n_lineages)) {
# pull cells assigned to lineage j
Expand Down Expand Up @@ -367,24 +376,24 @@ testDynamic <- function(expr.mat = NULL,
}
})

# finalize time tracking
end_time <- Sys.time()
total_time <- end_time - start_time
total_time_units <- attributes(total_time)$units
total_time_numeric <- as.numeric(total_time)
time_message <- paste0("\nscLANE testing completed for ",
length(genes),
" genes across ",
n_lineages,
" ",
ifelse(n_lineages == 1, "lineage ", "lineages "),
"in ",
round(total_time_numeric, 3),
" ",
total_time_units)
message(time_message)

# return results
if (verbose) {
end_time <- Sys.time()
total_time <- end_time - start_time
total_time_units <- attributes(total_time)$units
total_time_numeric <- as.numeric(total_time)
time_message <- paste0("scLANE testing completed for ",
length(genes),
" genes across ",
n_lineages,
" ",
ifelse(n_lineages == 1, "lineage ", "lineages "),
"in ",
round(total_time_numeric, 3),
" ",
total_time_units)
message(time_message)
}
class(test_stats) <- "scLANE"
return(test_stats)
}
12 changes: 6 additions & 6 deletions R/theme_scLANE.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
#' data(scLANE_models)
#' data(sim_pseudotime)
#' cell_offset <- createCellOffset(sim_counts)
#' plotModels(scLANE_models,
#' gene = names(scLANE_models)[1],
#' pt = sim_pseudotime,
#' expr.mat = sim_counts,
#' size.factor.offset = cell_offset) +
#' theme_scLANE()
#' model_plot <- plotModels(scLANE_models,
#' gene = names(scLANE_models)[1],
#' pt = sim_pseudotime,
#' expr.mat = sim_counts,
#' size.factor.offset = cell_offset) +
#' theme_scLANE()

theme_scLANE <- function(base.size = 12,
base.lwd = 0.75,
Expand Down
7 changes: 1 addition & 6 deletions R/waldTestGEE.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@
#' @seealso \code{\link[aod]{wald.test}}
#' @seealso \code{\link[geeM]{geem}}
#' @seealso \code{\link{modelLRT}}
#' @export
#' @examples
#' \dontrun{
#' waldTestGEE(mod.1 = full_model, mod.0 = null_model)
#' }

waldTestGEE <- function(mod.1 = NULL, mod.0 = NULL) {
# check inputs
Expand All @@ -32,7 +27,7 @@ waldTestGEE <- function(mod.1 = NULL, mod.0 = NULL) {
Notes = "No test performed due to model failure.")
return(res)
}

mod.1 <- mod.1$final_mod
if (is.null(mod.1) || is.null(mod.0) || !(inherits(mod.1, "geem") && inherits(mod.0, "geem"))) { stop("You must provide two geeM models to wald_test_gee().") }
if (length(coef(mod.1)) <= length(coef(mod.0))) {
Expand Down
8 changes: 0 additions & 8 deletions man/modelLRT.Rd

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

9 changes: 0 additions & 9 deletions man/stripGLM.Rd

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

4 changes: 2 additions & 2 deletions man/testDynamic.Rd

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

12 changes: 6 additions & 6 deletions man/theme_scLANE.Rd

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

5 changes: 0 additions & 5 deletions man/waldTestGEE.Rd

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

9 changes: 4 additions & 5 deletions tests/testthat/test_scLANE.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ withr::with_output_sink(tempfile(), {
is.gee = TRUE,
cor.structure = "ar1",
id.vec = sim_data$subject,
n.cores = 2,
track.time = FALSE)
n.cores = 2)
glmm_gene_stats <- testDynamic(sim_data,
pt = pt_test,
genes = genes_to_test,
Expand All @@ -54,7 +53,7 @@ withr::with_output_sink(tempfile(), {
glmm.adaptive = TRUE,
id.vec = sim_data$subject,
n.cores = 2,
track.time = FALSE)
verbose = FALSE)
# get results tables overall
glm_test_results <- getResultsDE(glm_gene_stats)
gee_test_results <- getResultsDE(gee_gene_stats)
Expand Down Expand Up @@ -122,15 +121,15 @@ withr::with_output_sink(tempfile(), {
return.WIC = TRUE)
# run GLMM model -- no offset
glmm_mod <- fitGLMM(X_pred = pt_test,
Y = counts_test[, 3],
Y = counts_test[, 4],
id.vec = sim_data$subject,
adaptive = TRUE,
M.glm = 3,
return.basis = TRUE,
return.GCV = TRUE)
# run GLMM model -- with offset
glmm_mod_offset <- fitGLMM(X_pred = pt_test,
Y = counts_test[, 3],
Y = counts_test[, 4],
Y.offset = cell_offset,
id.vec = sim_data$subject,
adaptive = TRUE,
Expand Down

0 comments on commit 24e7775

Please sign in to comment.