From efeeb0e855743abc971faff39b06ba4ecc5b8ab3 Mon Sep 17 00:00:00 2001 From: mhkowalski Date: Fri, 1 Nov 2024 12:19:57 -0400 Subject: [PATCH 1/2] combine global options statements --- R/helpers.R | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/R/helpers.R b/R/helpers.R index 07deace..90cdabf 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -681,8 +681,13 @@ LoadH5ADobs <- function(path, cell.groups = NULL) { #' } #' LoadReference <- function(path, seconds = 10L) { - op <- options(Seurat.object.assay.calcn = FALSE) - on.exit(expr = options(op), add = TRUE) + # set global options appropriately + original_options <- options( + Seurat.object.assay.calcn = FALSE, + Seurat.object.assay.version = "v3" + ) + # restore all options at exit + on.exit(expr = options(original_options), add = TRUE) ref.names <- list( map = 'ref.Rds', ann = 'idx.annoy' @@ -763,8 +768,6 @@ LoadReference <- function(path, seconds = 10L) { i = 1, j = 1, x = 0, dims = c(1, nrow(x = plotref.dr)), dimnames = list("placeholder", Cells(x = plotref.dr)) ) - op <- options(Seurat.object.assay.version = "v3") - on.exit(expr = options(op), add = TRUE) plot <- CreateSeuratObject(counts = cm) plot[["refUMAP"]] <- plotref.dr DefaultAssay(plot[["refUMAP"]]) <- DefaultAssay(plot) @@ -812,8 +815,13 @@ LoadReference <- function(path, seconds = 10L) { #' } LoadBridgeReference<- function(path, seconds = 10L) { - op <- options(Seurat.object.assay.calcn = FALSE) - on.exit(expr = options(op), add = TRUE) + # set global options appropriately + original_options <- options( + Seurat.object.assay.calcn = FALSE, + Seurat.object.assay.version = "v3" + ) + # restore all options at exit + on.exit(expr = options(original_options), add = TRUE) ref.names <- list( map = 'ext.Rds' ) @@ -886,8 +894,6 @@ LoadBridgeReference<- function(path, seconds = 10L) { i = 1, j = 1, x = 0, dims = c(1, nrow(x = plotref.dr)), dimnames = list("placeholder", Cells(x = plotref.dr)) ) - op <- options(Seurat.object.assay.version = "v3") - on.exit(expr = options(op), add = TRUE) plot <- CreateSeuratObject(counts = cm) plot[["refUMAP"]] <- plotref.dr plot <- AddMetaData(object = plot, metadata = Misc(object = plotref.dr, slot = "plot.metadata")) From 5795fe96534a8bf6971d51dc586390386100cbd5 Mon Sep 17 00:00:00 2001 From: mhkowalski Date: Fri, 1 Nov 2024 12:46:02 -0400 Subject: [PATCH 2/2] prevent global options from being overwritten --- R/helpers.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/helpers.R b/R/helpers.R index 90cdabf..4304382 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -571,7 +571,8 @@ LoadH5AD <- function(path) { colnames <- rownames(metadata) rownames(x = counts) <- rownames colnames(x = counts) <- colnames - options(Seurat.object.assay.calcn = TRUE) + op <- options(Seurat.object.assay.calcn = TRUE) + on.exit(expr = options(op), add = TRUE) object <- CreateSeuratObject(counts = counts) if (ncol(x = metadata)) { object <- AddMetaData(object = object, metadata = metadata)