@@ -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+ }
0 commit comments