Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix allowing duplicate keys #26

Merged
merged 3 commits into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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')),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
69 changes: 43 additions & 26 deletions R/seurat.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -2812,38 +2816,51 @@ 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
}
}
# 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"))
)) {
Expand Down