Skip to content

Commit 2db253d

Browse files
committed
feat: implement default dimension reduction functions for Seurat objects
1 parent ab8bcd7 commit 2db253d

6 files changed

Lines changed: 104 additions & 6 deletions

File tree

NAMESPACE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ export(top)
6060
export(uniq)
6161
import(R6)
6262
importFrom(SeuratObject,"Idents<-")
63-
importFrom(SeuratObject,DefaultDimReduc)
6463
importFrom(SeuratObject,Embeddings)
6564
importFrom(SeuratObject,GetAssayData)
6665
importFrom(SeuratObject,Graphs)

R/celldimplot.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#' @return A ggplot object or a list if `combine` is FALSE
2222
#' @export
2323
#' @seealso [scplotter::CellStatPlot()] [scplotter::CellVelocityPlot()]
24-
#' @importFrom SeuratObject DefaultDimReduc Embeddings Graphs Reductions Idents
24+
#' @importFrom SeuratObject Embeddings Graphs Reductions Idents
2525
#' @importFrom plotthis DimPlot
2626
#' @details
2727
#' See
@@ -268,7 +268,7 @@ CellDimPlot.Seurat <- function(
268268
is.null(group_by) || is.null(ident) || identical(group_by, ident))
269269
group_by <- group_by %||% ident
270270

271-
reduction <- reduction %||% DefaultDimReduc(object)
271+
reduction <- reduction %||% default_dimreduc(object)
272272

273273
if (!reduction %in% Reductions(object)) {
274274
stop("The object does not have reduction:", reduction)
@@ -397,7 +397,7 @@ CellDimPlot.H5File <- function(
397397
#' @return A ggplot object
398398
#' @export
399399
#' @seealso [scplotter::CellDimPlot()]
400-
#' @importFrom SeuratObject DefaultDimReduc Embeddings Reductions
400+
#' @importFrom SeuratObject Embeddings Reductions
401401
#' @importFrom plotthis VelocityPlot
402402
#' @details See:
403403
#' * <https://pwwang.github.io/scplotter/articles/Working_with_anndata_h5ad_files.html>

R/featurestatplot.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@
212212
#' @return A ggplot object or a list if `combine` is FALSE
213213
#' @export
214214
#' @importFrom rlang %||%
215-
#' @importFrom SeuratObject GetAssayData Embeddings DefaultDimReduc Graphs Reductions Idents
215+
#' @importFrom SeuratObject GetAssayData Embeddings Graphs Reductions Idents
216216
#' @importFrom plotthis ViolinPlot BoxPlot BarPlot DotPlot RidgePlot FeatureDimPlot Heatmap CorPlot CorPairsPlot
217217
#' @details
218218
#' See:
@@ -567,7 +567,7 @@ FeatureStatPlot.Seurat <- function(
567567
stopifnot("[FeatureStatPlot] 'spat_unit' and 'feat_type' should not be used for Seurat object." = is.null(spat_unit) && is.null(feat_type))
568568

569569
reduction <- reduction %||% (
570-
if(is.null(Reductions(object))) NULL else DefaultDimReduc(object)
570+
if(is.null(Reductions(object))) NULL else default_dimreduc(object)
571571
)
572572
# dim plot may use expression for highlighting cells
573573
# Heatmap may use other variables as annotations, but shrinking only includes minimal columns

R/utils.R

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,51 @@ do_call <- function(fn, args, quote = FALSE, envir = parent.frame()) {
220220
enclos = envir
221221
)
222222
}
223+
224+
#' Set default dimension reduction for Seurat object
225+
#'
226+
#' @details
227+
#' `DefaultDimReduc<-` was just introduced in Seurat v5.4.0
228+
#' See: https://github.com/satijalab/seurat-object/pull/268
229+
#' and https://github.com/satijalab/seurat-object/releases/tag/v5.4.0
230+
#' This function is a fallback for older Seurat versions that do not have this setter function.
231+
#' It will simply save the default dimension reduction name in the Seurat object's misc slot under `DefaultDimReduc`.
232+
#' When the setter function is available, it will use that instead.
233+
#'
234+
#' @param object Seurat object to modify
235+
#' @param value Name of the default dimension reduction to set
236+
#' @return Modified Seurat object with updated default dimension reduction
237+
#' @keywords internal
238+
`default_dimreduc<-` <- function(object, value) {
239+
if (exists("DefaultDimReduc<-", where = asNamespace("SeuratObject"), mode = "function")) {
240+
# Use the official setter if available
241+
get("DefaultDimReduc<-", envir = asNamespace("SeuratObject"))(object, value)
242+
} else {
243+
# Fallback: store in misc slot
244+
object@misc$DefaultDimReduc <- value
245+
}
246+
return(object)
247+
}
248+
249+
#' Get default dimension reduction for Seurat object
250+
#'
251+
#' @details
252+
#' `DefaultDimReduc` was just introduced in Seurat v5.4.0.
253+
#' This function will first check if we have `DefaultDimReduc` value in the misc slot (set by our fallback setter),
254+
#' and if not, it will try to call the official `DefaultDimReduc` getter function. If that also fails, it will return NULL.
255+
#'
256+
#' @param object Seurat object to query
257+
#' @return Name of the default dimension reduction, or NULL if not set
258+
#' @keywords internal
259+
`default_dimreduc` <- function(object) {
260+
# If we have SeuratObject >= 5.4.0, we can use the official getter
261+
if (exists("DefaultDimReduc<-", where = asNamespace("SeuratObject"), mode = "function")) {
262+
return(get("DefaultDimReduc", envir = asNamespace("SeuratObject"))(object))
263+
}
264+
# Fallback: check misc slot
265+
if (!is.null(object@misc$DefaultDimReduc)) {
266+
return(object@misc$DefaultDimReduc)
267+
}
268+
# Try to call official getter, may return NULL if not set
269+
return(SeuratObject::DefaultDimReduc(object))
270+
}

man/default_dimreduc-set.Rd

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/default_dimreduc.Rd

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)