Skip to content

Commit

Permalink
Merge pull request #160 from mojaveazure/fix/layer-slot
Browse files Browse the repository at this point in the history
Update use of `GetAssayData()`
  • Loading branch information
mojaveazure authored Nov 9, 2023
2 parents 78f71fe + 62f1584 commit d476a23
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 29 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: SeuratObject
Type: Package
Title: Data Structures for Single Cell Data
Version: 5.0.0
Version: 5.0.0.9001
Authors@R: c(
person(given = 'Rahul', family = 'Satija', email = 'rsatija@nygenome.org', role = 'aut', comment = c(ORCID = '0000-0001-9448-8833')),
person(given = 'Paul', family = 'Hoffman', email = 'seurat@nygenome.org', role = c('aut', 'cre'), comment = c(ORCID = '0000-0002-7693-8957')),
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Unreleased

## Changes:
- Update internal calls to `GetAssayData()` to use `layer` instead of `slot` (#160)

# SeuratObject 5.0.0
## Added
- New `Assay5` class with support for layers; layers provide support for:
Expand Down
34 changes: 17 additions & 17 deletions R/assay.R
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ Features.Assay <- function(
}
layer <- layer[1L] %||% 'data'
layer <- match.arg(arg = layer)
features <- rownames(x = GetAssayData(object = x, slot = layer))
features <- rownames(x = GetAssayData(object = x, layer = layer))
if (!length(x = features)) {
features <- NULL
}
Expand Down Expand Up @@ -370,7 +370,7 @@ FetchData.Assay <- function(
x = vars
)
# Pull expression information
mat <- GetAssayData(object = object, slot = layer)
mat <- GetAssayData(object = object, layer = layer)
if (IsMatrixEmpty(x = mat)) {
abort(message = paste("Layer", sQuote(x = layer), "is empty in this assay"))
}
Expand Down Expand Up @@ -736,7 +736,7 @@ RenameCells.Assay <- function(object, new.names = NULL, ...) {
CheckDots(...)
names(new.names) <- NULL
for (data.slot in c("counts", "data", "scale.data")) {
old.data <- GetAssayData(object = object, slot = data.slot)
old.data <- GetAssayData(object = object, layer = data.slot)
if (ncol(x = old.data) <= 1) {
next
}
Expand Down Expand Up @@ -1370,7 +1370,7 @@ merge.Assay <- function(
}
combined.assay <- SetAssayData(
object = combined.assay,
slot = "data",
layer = "data",
new.data = merged.data
)
}
Expand Down Expand Up @@ -1465,17 +1465,17 @@ subset.Assay <- function(x, cells = NULL, features = NULL, ...) {
if (length(x = features) == 0) {
abort(message = "Cannot find features provided")
}
if (ncol(x = GetAssayData(object = x, slot = 'counts')) == ncol(x = x)) {
slot(object = x, name = "counts") <- GetAssayData(object = x, slot = "counts")[features, cells, drop = FALSE]
if (ncol(x = GetAssayData(object = x, layer = 'counts')) == ncol(x = x)) {
slot(object = x, name = "counts") <- GetAssayData(object = x, layer = "counts")[features, cells, drop = FALSE]
}
slot(object = x, name = "data") <- GetAssayData(object = x, slot = "data")[features, cells, drop = FALSE]
cells.scaled <- colnames(x = GetAssayData(object = x, slot = "scale.data"))
slot(object = x, name = "data") <- GetAssayData(object = x, layer = "data")[features, cells, drop = FALSE]
cells.scaled <- colnames(x = GetAssayData(object = x, layer = "scale.data"))
cells.scaled <- cells.scaled[cells.scaled %in% cells]
cells.scaled <- cells.scaled[na.omit(object = match(x = colnames(x = x), table = cells.scaled))]
features.scaled <- rownames(x = GetAssayData(object = x, slot = 'scale.data'))
features.scaled <- rownames(x = GetAssayData(object = x, layer = 'scale.data'))
features.scaled <- intersect(x = features, y = features.scaled)
slot(object = x, name = "scale.data") <- if (length(x = cells.scaled) > 0 && length(x = features.scaled) > 0) {
GetAssayData(object = x, slot = "scale.data")[features.scaled, cells.scaled, drop = FALSE]
GetAssayData(object = x, layer = "scale.data")[features.scaled, cells.scaled, drop = FALSE]
} else {
new(Class = 'matrix')
}
Expand Down Expand Up @@ -1625,7 +1625,7 @@ setMethod(
signature = c(x = 'Assay'),
definition = function(x, na.rm = FALSE, dims = 1, ..., slot = 'data') {
return(Matrix::colMeans(
x = GetAssayData(object = x, slot = slot),
x = GetAssayData(object = x, layer = slot),
na.rm = na.rm,
dims = dims,
...
Expand All @@ -1649,7 +1649,7 @@ setMethod(
signature = c(x = 'Assay'),
definition = function(x, na.rm = FALSE, dims = 1, ..., slot = 'data') {
return(Matrix::colSums(
x = GetAssayData(object = x, slot = slot),
x = GetAssayData(object = x, layer = slot),
na.rm = na.rm,
dims = dims,
...
Expand All @@ -1673,7 +1673,7 @@ setMethod(
signature = c(x = 'Assay'),
definition = function(x, na.rm = FALSE, dims = 1, ..., slot = 'data') {
return(Matrix::rowMeans(
x = GetAssayData(object = x, slot = slot),
x = GetAssayData(object = x, layer = slot),
na.rm = na.rm,
dims = dims,
...
Expand All @@ -1697,7 +1697,7 @@ setMethod(
signature = c(x = 'Assay'),
definition = function(x, na.rm = FALSE, dims = 1, ..., slot = 'data') {
return(Matrix::rowSums(
x = GetAssayData(object = x, slot = slot),
x = GetAssayData(object = x, layer = slot),
na.rm = na.rm,
dims = dims,
...
Expand Down Expand Up @@ -1927,11 +1927,11 @@ SubsetVST <- function(sct.info, cells, features) {
#' @noRd
#'
ValidateDataForMerge <- function(assay, slot) {
mat <- GetAssayData(object = assay, slot = slot)
mat <- GetAssayData(object = assay, layer = slot)
if (any(dim(x = mat) == c(0, 0))) {
slots.to.check <- setdiff(x = c("counts", "data", "scale.data"), y = slot)
for (ss in slots.to.check) {
data.dims <- dim(x = GetAssayData(object = assay, slot = ss))
data.dims <- dim(x = GetAssayData(object = assay, layer = ss))
data.slot <- ss
if (!any(data.dims == c(0, 0))) {
break
Expand All @@ -1944,7 +1944,7 @@ ValidateDataForMerge <- function(assay, slot) {
data = 0,
nrow = data.dims[1],
ncol = data.dims[2],
dimnames = dimnames(x = GetAssayData(object = assay, slot = data.slot))
dimnames = dimnames(x = GetAssayData(object = assay, layer = data.slot))
)
mat <- as.sparse(x = mat)
}
Expand Down
2 changes: 1 addition & 1 deletion R/assay5.R
Original file line number Diff line number Diff line change
Expand Up @@ -2733,7 +2733,7 @@ setAs(
# browser()
# Add the expression matrices
for (i in c('counts', 'data', 'scale.data')) {
adata <- GetAssayData(object = from, slot = i)
adata <- GetAssayData(object = from, layer = i)
if (IsMatrixEmpty(x = adata)) {
next
}
Expand Down
27 changes: 18 additions & 9 deletions R/seurat.R
Original file line number Diff line number Diff line change
Expand Up @@ -1871,7 +1871,7 @@ FetchData.Seurat <- function(
#'
#' @examples
#' # Get assay data from the default assay in a Seurat object
#' GetAssayData(object = pbmc_small, slot = "data")[1:5,1:5]
#' GetAssayData(object = pbmc_small, layer = "data")[1:5,1:5]
#'
GetAssayData.Seurat <- function(
object,
Expand Down Expand Up @@ -2153,9 +2153,11 @@ LayerData.Seurat <- function(
...
) {
if (is_present(arg = slot)) {
deprecate_stop(when = "5.0.0",
what = "LayerData(slot = )",
with = "LayerData(layer = )")
deprecate_stop(
when = "5.0.0",
what = "LayerData(slot = )",
with = "LayerData(layer = )"
)
}
assay <- assay %||% DefaultAssay(object = object)
assay <- arg_match(arg = assay, values = Assays(object = object))
Expand Down Expand Up @@ -2465,13 +2467,20 @@ SetAssayData.Seurat <- function(
...
) {
CheckDots(...)
if (is_present(arg = slot)) {
.Deprecate(
when = '5.0.0',
what = 'GetAssayData(slot = )',
with = 'GetAssayData(layer = )'
)
layer <- slot
}
object <- UpdateSlots(object = object)
assay <- assay %||% DefaultAssay(object = object)
object[[assay]] <- SetAssayData(
object = object[[assay]],
layer = layer,
new.data = new.data,
slot = slot,
...
)
return(object)
Expand Down Expand Up @@ -2795,7 +2804,7 @@ WhichCells.Seurat <- function(
object = object,
vars = unique(x = expr.char[vars.use]),
cells = cells,
slot = slot
layer = slot
)
cells <- rownames(x = data.subset)[eval_tidy(expr = expr, data = data.subset)]
}
Expand Down Expand Up @@ -3911,7 +3920,7 @@ setMethod( # because R doesn't allow S3-style [[<- for S4 classes
# Ensure cell order stays the same
if (all(Cells(x = value) %in% Cells(x = x)) && !all(Cells(x = value) == Cells(x = x))) {
for (slot in c('counts', 'data', 'scale.data')) {
assay.data <- GetAssayData(object = value, slot = slot)
assay.data <- GetAssayData(object = value, layer = slot)
if (!IsMatrixEmpty(x = assay.data)) {
assay.data <- assay.data[, Cells(x = x), drop = FALSE]
}
Expand Down Expand Up @@ -4113,8 +4122,8 @@ setMethod( # because R doesn't allow S3-style [[<- for S4 classes
if (inherits(x = value, what = 'Assay')) {
if ((!i %in% Assays(object = x)) |
(i %in% Assays(object = x) && !identical(
x = GetAssayData(object = x, assay = i, slot = "counts"),
y = GetAssayData(object = value, slot = "counts"))
x = GetAssayData(object = x, assay = i, layer = "counts"),
y = GetAssayData(object = value, layer = "counts"))
)) {
n.calc <- CalcN(object = value)
if (!is.null(x = n.calc)) {
Expand Down
2 changes: 1 addition & 1 deletion man/AssayData.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/SeuratObject-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d476a23

Please sign in to comment.