From bb468e774b73b77fa5063d2e47412086f469e4d0 Mon Sep 17 00:00:00 2001 From: Paul Hoffman Date: Tue, 19 Oct 2021 14:35:14 -0400 Subject: [PATCH 1/3] Fix allowing duplicate keys Duplicate keys were being allowed when re-adding an object to a Seurat object Now, do better checking of duplicate keys --- R/seurat.R | 69 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 26 deletions(-) diff --git a/R/seurat.R b/R/seurat.R index 6d31c34b..e4e16add 100644 --- a/R/seurat.R +++ b/R/seurat.R @@ -1359,12 +1359,16 @@ Key.Seurat <- function(object, ...) { object = object, classes.keep = c('Assay', 'DimReduc', 'SpatialImage') ) - return(sapply( + keys <- vapply( X = keyed.objects, FUN = function(x) { return(Key(object = object[[x]])) - } - )) + }, + FUN.VALUE = character(length = 1L), + USE.NAMES = FALSE + ) + names(x = keys) <- keyed.objects + return(keys) } #' @param reduction Name of reduction to pull feature loadings for @@ -2812,30 +2816,43 @@ setMethod( # because R doesn't allow S3-style [[<- for S4 classes } Key(object = value) <- UpdateKey(key = Key(object = value)) # Check for duplicate keys - object.keys <- sapply( - X = FilterObjects(object = x), - FUN = function(i) { - return(Key(object = x[[i]])) - } - ) - if (Key(object = value) %in% object.keys && is.null(x = FindObject(object = x, name = i))) { - # Attempt to create a duplicate key based off the name of the object being added - new.keys <- c(paste0(tolower(x = i), c('_', paste0(RandomName(length = 2L), '_')))) - # Select new key to use - key.use <- min(which(x = !new.keys %in% object.keys)) - new.key <- if (is.infinite(x = key.use)) { - RandomName(length = 17L) + object.keys <- Key(object = x) + vkey <- Key(object = value) + if (vkey %in% object.keys && !isTRUE(x = object.keys[i] == vkey)) { + new.key <- if (is.na(x = object.keys[i])) { + # Attempt to create a duplicate key based off the name of the object being added + new.keys <- paste0( + paste0(tolower(x = i), c('', RandomName(length = 2L))), + '_' + ) + # Select new key to use + key.use <- min(which(x = !new.keys %in% object.keys)) + new.key <- if (is.infinite(x = key.use)) { + RandomName(length = 17L) + } else { + new.keys[key.use] + } + warning( + "Cannot add objects with duplicate keys (offending key: ", + Key(object = value), + "), setting key to '", + new.key, + "'", + call. = FALSE + ) + new.key } else { - new.keys[key.use] + # Use existing key + warning( + "Cannot add objects with duplicate keys (offending key: ", + Key(object = value), + ") setting key to original value '", + object.keys[i], + "'", + call. = FALSE + ) + object.keys[i] } - warning( - "Cannot add objects with duplicate keys (offending key: ", - Key(object = value), - "), setting key to '", - new.key, - "'", - call. = FALSE - ) # Set new key Key(object = value) <- new.key } @@ -2843,7 +2860,7 @@ setMethod( # because R doesn't allow S3-style [[<- for S4 classes # For Assays, run CalcN if (inherits(x = value, what = 'Assay')) { if ((!i %in% Assays(object = x)) | - (i %in% Assays(object = x) && ! identical( + (i %in% Assays(object = x) && !identical( x = GetAssayData(object = x, assay = i, slot = "counts"), y = GetAssayData(object = value, slot = "counts")) )) { From bc71384ee97560239b59e8deb17c4ca4c8519598 Mon Sep 17 00:00:00 2001 From: Paul Hoffman Date: Tue, 19 Oct 2021 14:40:10 -0400 Subject: [PATCH 2/3] Bump develop version --- DESCRIPTION | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 3bd369f1..652e2d83 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: SeuratObject Type: Package Title: Data Structures for Single Cell Data -Version: 4.0.2.9000 -Date: 2021-09-07 +Version: 4.0.2.9001 +Date: 2021-10-19 Authors@R: c( person(given = 'Rahul', family = 'Satija', email = 'rsatija@nygenome.org', role = 'aut', comment = c(ORCID = '0000-0001-9448-8833')), person(given = 'Andrew', family = 'Butler', email = 'abutler@nygenome.org', role = 'aut', comment = c(ORCID = '0000-0003-3608-0463')), From 8c04519d4f0b66c4fa0dae08df60e4e2d377cd6e Mon Sep 17 00:00:00 2001 From: Paul Hoffman Date: Tue, 19 Oct 2021 14:40:16 -0400 Subject: [PATCH 3/3] Update changelog --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 5a0db1ae..98800e08 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,8 @@ ## Changed - Export utility functions (#22) +- Bug fix in names with `Key.Seurat` (#26) +- Improved duplicate key checking and resolution # SeuratObject 4.0.2