@@ -499,6 +499,52 @@ FetchData <- function(object, vars, cells = NULL, slot = 'data') {
499499 return (data.fetched )
500500}
501501
502+ # ' Find Sub-objects of a Certain Class
503+ # '
504+ # ' Get the names of objects within a \code{Seurat} object that are of a
505+ # ' certain class
506+ # '
507+ # ' @param object A \code{\link{Seurat}} object
508+ # ' @param classes.keep A vector of names of classes to get
509+ # '
510+ # ' @return A vector with the names of objects within the \code{Seurat} object
511+ # ' that are of class \code{classes.keep}
512+ # '
513+ # ' @export
514+ # '
515+ # ' @examples
516+ # ' FilterObjects(pbmc_small)
517+ # '
518+ FilterObjects <- function (object , classes.keep = c(' Assay' , ' DimReduc' )) {
519+ object <- UpdateSlots(object = object )
520+ slots <- na.omit(object = Filter(
521+ f = function (x ) {
522+ sobj <- slot(object = object , name = x )
523+ return (is.list(x = sobj ) && ! is.data.frame(x = sobj ) && ! is.package_version(x = sobj ))
524+ },
525+ x = slotNames(x = object )
526+ ))
527+ slots <- grep(pattern = ' tools' , x = slots , value = TRUE , invert = TRUE )
528+ slots <- grep(pattern = ' misc' , x = slots , value = TRUE , invert = TRUE )
529+ slots.objects <- unlist(
530+ x = lapply(
531+ X = slots ,
532+ FUN = function (x ) {
533+ return (names(x = slot(object = object , name = x )))
534+ }
535+ ),
536+ use.names = FALSE
537+ )
538+ object.classes <- sapply(
539+ X = slots.objects ,
540+ FUN = function (i ) {
541+ return (inherits(x = object [[i ]], what = classes.keep ))
542+ }
543+ )
544+ object.classes <- which(x = object.classes , useNames = TRUE )
545+ return (names(x = object.classes ))
546+ }
547+
502548# ' @rdname ObjectAccess
503549# ' @export
504550# '
@@ -1313,12 +1359,16 @@ Key.Seurat <- function(object, ...) {
13131359 object = object ,
13141360 classes.keep = c(' Assay' , ' DimReduc' , ' SpatialImage' )
13151361 )
1316- return (sapply (
1362+ keys <- vapply (
13171363 X = keyed.objects ,
13181364 FUN = function (x ) {
13191365 return (Key(object = object [[x ]]))
1320- }
1321- ))
1366+ },
1367+ FUN.VALUE = character (length = 1L ),
1368+ USE.NAMES = FALSE
1369+ )
1370+ names(x = keys ) <- keyed.objects
1371+ return (keys )
13221372}
13231373
13241374# ' @param reduction Name of reduction to pull feature loadings for
@@ -2766,38 +2816,51 @@ setMethod( # because R doesn't allow S3-style [[<- for S4 classes
27662816 }
27672817 Key(object = value ) <- UpdateKey(key = Key(object = value ))
27682818 # Check for duplicate keys
2769- object.keys <- sapply(
2770- X = FilterObjects(object = x ),
2771- FUN = function (i ) {
2772- return (Key(object = x [[i ]]))
2773- }
2774- )
2775- if (Key(object = value ) %in% object.keys && is.null(x = FindObject(object = x , name = i ))) {
2776- # Attempt to create a duplicate key based off the name of the object being added
2777- new.keys <- c(paste0(tolower(x = i ), c(' _' , paste0(RandomName(length = 2L ), ' _' ))))
2778- # Select new key to use
2779- key.use <- min(which(x = ! new.keys %in% object.keys ))
2780- new.key <- if (is.infinite(x = key.use )) {
2781- RandomName(length = 17L )
2819+ object.keys <- Key(object = x )
2820+ vkey <- Key(object = value )
2821+ if (vkey %in% object.keys && ! isTRUE(x = object.keys [i ] == vkey )) {
2822+ new.key <- if (is.na(x = object.keys [i ])) {
2823+ # Attempt to create a duplicate key based off the name of the object being added
2824+ new.keys <- paste0(
2825+ paste0(tolower(x = i ), c(' ' , RandomName(length = 2L ))),
2826+ ' _'
2827+ )
2828+ # Select new key to use
2829+ key.use <- min(which(x = ! new.keys %in% object.keys ))
2830+ new.key <- if (is.infinite(x = key.use )) {
2831+ RandomName(length = 17L )
2832+ } else {
2833+ new.keys [key.use ]
2834+ }
2835+ warning(
2836+ " Cannot add objects with duplicate keys (offending key: " ,
2837+ Key(object = value ),
2838+ " ), setting key to '" ,
2839+ new.key ,
2840+ " '" ,
2841+ call. = FALSE
2842+ )
2843+ new.key
27822844 } else {
2783- new.keys [key.use ]
2845+ # Use existing key
2846+ warning(
2847+ " Cannot add objects with duplicate keys (offending key: " ,
2848+ Key(object = value ),
2849+ " ) setting key to original value '" ,
2850+ object.keys [i ],
2851+ " '" ,
2852+ call. = FALSE
2853+ )
2854+ object.keys [i ]
27842855 }
2785- warning(
2786- " Cannot add objects with duplicate keys (offending key: " ,
2787- Key(object = value ),
2788- " ), setting key to '" ,
2789- new.key ,
2790- " '" ,
2791- call. = FALSE
2792- )
27932856 # Set new key
27942857 Key(object = value ) <- new.key
27952858 }
27962859 }
27972860 # For Assays, run CalcN
27982861 if (inherits(x = value , what = ' Assay' )) {
27992862 if ((! i %in% Assays(object = x )) |
2800- (i %in% Assays(object = x ) && ! identical(
2863+ (i %in% Assays(object = x ) && ! identical(
28012864 x = GetAssayData(object = x , assay = i , slot = " counts" ),
28022865 y = GetAssayData(object = value , slot = " counts" ))
28032866 )) {
@@ -3082,48 +3145,6 @@ DefaultImage <- function(object) {
30823145 return (images [[1 ]])
30833146}
30843147
3085- # ' Get the names of objects within a Seurat object that are of a certain class
3086- # '
3087- # ' @param object A \code{\link{Seurat}} object
3088- # ' @param classes.keep A vector of names of classes to get
3089- # '
3090- # ' @return A vector with the names of objects within the Seurat object that are
3091- # ' of class \code{classes.keep}
3092- # '
3093- # ' @keywords internal
3094- # '
3095- # ' @noRd
3096- # '
3097- FilterObjects <- function (object , classes.keep = c(' Assay' , ' DimReduc' )) {
3098- object <- UpdateSlots(object = object )
3099- slots <- na.omit(object = Filter(
3100- f = function (x ) {
3101- sobj <- slot(object = object , name = x )
3102- return (is.list(x = sobj ) && ! is.data.frame(x = sobj ) && ! is.package_version(x = sobj ))
3103- },
3104- x = slotNames(x = object )
3105- ))
3106- slots <- grep(pattern = ' tools' , x = slots , value = TRUE , invert = TRUE )
3107- slots <- grep(pattern = ' misc' , x = slots , value = TRUE , invert = TRUE )
3108- slots.objects <- unlist(
3109- x = lapply(
3110- X = slots ,
3111- FUN = function (x ) {
3112- return (names(x = slot(object = object , name = x )))
3113- }
3114- ),
3115- use.names = FALSE
3116- )
3117- object.classes <- sapply(
3118- X = slots.objects ,
3119- FUN = function (i ) {
3120- return (inherits(x = object [[i ]], what = classes.keep ))
3121- }
3122- )
3123- object.classes <- which(x = object.classes , useNames = TRUE )
3124- return (names(x = object.classes ))
3125- }
3126-
31273148# ' Find the collection of an object within a Seurat object
31283149# '
31293150# ' @param object A \code{\link{Seurat}} object
0 commit comments