Skip to content

Commit

Permalink
Fixes for Seurat new version
Browse files Browse the repository at this point in the history
  • Loading branch information
mvfki committed Dec 7, 2023
1 parent 5af313a commit cb45fcb
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 65 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Suggests:
rgl,
rmarkdown,
Seurat,
SeuratObject,
SingleCellExperiment,
SummarizedExperiment,
testthat (>= 3.0.0)
Expand Down
17 changes: 6 additions & 11 deletions R/binary.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ plotBinary.default <- function(
}
}

#' @param slot For "Seurat" method, choose from \code{"counts"},
#' \code{"data"} or \code{"scale.data"}. Default \code{"counts"}.
#' @param layer For "Seurat" method, which layer of the assay to be used.
#' Default \code{"counts"}.
#' @param assay For "Seurat" method, the specific assay to get data from.
#' Default \code{NULL} to the default assay.
#' @rdname plotBinary
Expand All @@ -140,25 +140,21 @@ plotBinary.default <- function(
#' if (FALSE) {
#' srt <- CreateSeuratObject(rnaRaw)
#' Idents(srt) <- rnaCluster
#' srt <- colNormalize(srt)
#' gene <- selectTopFeatures(srt, vertices = c("OS", "RE"))
#' srt <- colNormalize(srt, scaleFactor = 1e4, log = TRUE)
#' plotBinary(srt, features = gene, vertices = c("OS", "RE"))
#' }
plotBinary.Seurat <- function(
x,
slot = c("counts", "data", "scale.data"),
layer = "counts",
assay = NULL,
clusterVar = NULL,
processed = FALSE,
...
) {
slot <- match.arg(slot)
values <- .getSeuratData(x, slot = slot, assay = assay,
values <- .getSeuratData(x, layer = layer, assay = assay,
clusterVar = clusterVar)
if (missing(processed)) {
if (slot == "counts") processed <- FALSE
else processed <- TRUE
processed <- layer != "counts"
}
plotBinary(x = values[[1]], clusterVar = values[[2]], processed = processed,
...)
Expand Down Expand Up @@ -188,8 +184,7 @@ plotBinary.SingleCellExperiment <- function(
) {
values <- .getSCEData(x, assay.type = assay.type, clusterVar = clusterVar)
if (missing(processed)) {
if (assay.type == "counts") processed <- FALSE
else processed <- TRUE
processed <- assay.type != "counts"
}
plotBinary(values[[1]], clusterVar = values[[2]], processed = processed,
...)
Expand Down
8 changes: 4 additions & 4 deletions R/normalize.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ colNormalize.default <- function(x, scaleFactor = NULL, log = FALSE, ...) {
}

#' @rdname colNormalize
#' @param slot For "Seurat" method, choose from \code{"counts"},
#' \code{"data"} or \code{"scale.data"}. Default \code{"counts"}.
#' @param layer For "Seurat" method, which layer of the assay to be used.
#' Default \code{"counts"}.
#' @param assay For "Seurat" method, the specific assay to get data from.
#' Default \code{NULL} to the default assay.
#' @return A Seurat object with normalized data in the specified slot of the
Expand All @@ -55,10 +55,10 @@ colNormalize.Seurat <- function(
scaleFactor = NULL,
log = FALSE,
assay = NULL,
slot = "counts",
layer = "counts",
...
) {
value <- .getSeuratData(x, assay = assay, slot = slot, clusterVar = NULL)
value <- .getSeuratData(x, assay = assay, layer = layer, clusterVar = NULL)
mat <- value[[1]]
norm <- colNormalize.default(mat, scaleFactor = scaleFactor, log = log)
x <- Seurat::SetAssayData(x, slot = "data", assay = assay, new.data = norm)
Expand Down
15 changes: 6 additions & 9 deletions R/quaternary.R
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ plotQuaternary.default <- function(
}
}

#' @param slot For "Seurat" method, choose from \code{"counts"},
#' \code{"data"} or \code{"scale.data"}. Default \code{"counts"}.
#' @param layer For "Seurat" method, which layer of the assay to be used.
#' Default \code{"counts"}.
#' @param assay For "Seurat" method, the specific assay to get data from.
#' Default \code{NULL} to the default assay.
#' @rdname plotQuaternary
Expand All @@ -174,18 +174,16 @@ plotQuaternary.default <- function(
#' }
plotQuaternary.Seurat <- function(
x,
slot = c("counts", "data", "scale.data"),
layer = "counts",
assay = NULL,
clusterVar = NULL,
processed = FALSE,
...
) {
slot <- match.arg(slot)
values <- .getSeuratData(x, slot = slot, assay = assay,
values <- .getSeuratData(x, layer = layer, assay = assay,
clusterVar = clusterVar)
if (missing(processed)) {
if (slot == "counts") processed <- FALSE
else processed <- TRUE
processed <- layer != "counts"
}
plotQuaternary(values[[1]], clusterVar = values[[2]], processed = processed,
...)
Expand Down Expand Up @@ -216,8 +214,7 @@ plotQuaternary.SingleCellExperiment <- function(
) {
values <- .getSCEData(x, assay.type = assay.type, clusterVar = clusterVar)
if (missing(processed)) {
if (assay.type == "counts") processed <- FALSE
else processed <- TRUE
processed <- assay.type != "counts"
}
plotQuaternary(values[[1]], clusterVar = values[[2]], processed = processed,
...)
Expand Down
15 changes: 8 additions & 7 deletions R/selectTopFeatures.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ selectTopFeatures.default <- function(
#' @rdname selectTopFeatures
#' @export
#' @method selectTopFeatures Seurat
#' @param slot Slot name of the Seurat object to be used. Default
#' \code{"counts"}.
#' @param layer For "Seurat" method, which layer of the assay to be used.
#' Default \code{"counts"}.
#' @param assay Assay name of the Seurat object to be used. Default \code{NULL}.
#' @examples
#'
Expand All @@ -128,16 +128,16 @@ selectTopFeatures.Seurat <- function(
clusterVar = NULL,
vertices,
assay = NULL,
slot = c("counts", "data", "scale.data"),
layer = "counts",
processed = FALSE,
...
) {
slot <- match.arg(slot)
value <- .getSeuratData(x, assay = assay, slot = slot,
value <- .getSeuratData(x, assay = assay, layer = layer,
clusterVar = clusterVar)
mat <- value[[1]]
clusterVar <- value[[2]]

processed <- ifelse(slot != "counts", TRUE, FALSE)
if (missing(processed)) processed <- layer != "counts"
selectTopFeatures(mat, clusterVar, vertices, processed = processed, ...)
}

Expand All @@ -160,12 +160,13 @@ selectTopFeatures.SingleCellExperiment <- function(
clusterVar = NULL,
vertices,
assay.type = "counts",
processed = FALSE,
...
) {
value <- .getSCEData(x, assay.type = assay.type, clusterVar = clusterVar)
mat <- value[[1]]
clusterVar <- value[[2]]
processed <- ifelse(assay.type == "counts", FALSE, TRUE)
if (missing(processed)) processed <- assay.type != "counts"
selectTopFeatures(mat, clusterVar, vertices, processed = processed, ...)
}

Expand Down
20 changes: 9 additions & 11 deletions R/ternary.R
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ plotTernary.default <- function(
}
}

#' @param slot For "Seurat" method, choose from \code{"counts"},
#' \code{"data"} or \code{"scale.data"}. Default \code{"counts"}.
#' @param layer For "Seurat" method, which layer of the assay to be used.
#' Default \code{"counts"}.
#' @param assay For "Seurat" method, the specific assay to get data from.
#' Default \code{NULL} to the default assay.
#' @rdname plotTernary
Expand All @@ -187,18 +187,16 @@ plotTernary.default <- function(
#' }
plotTernary.Seurat <- function(
x,
slot = c("counts", "data", "scale.data"),
layer = "counts",
assay = NULL,
clusterVar = NULL,
processed = FALSE,
...
) {
slot <- match.arg(slot)
values <- .getSeuratData(x, slot = slot, assay = assay,
values <- .getSeuratData(x, layer = layer, assay = assay,
clusterVar = clusterVar)
if (missing(processed)) {
if (slot == "counts") processed <- FALSE
else processed <- TRUE
processed <- layer != "counts"
}
plotTernary(values[[1]], clusterVar = values[[2]], processed = processed,
...)
Expand Down Expand Up @@ -228,8 +226,7 @@ plotTernary.SingleCellExperiment <- function(
) {
values <- .getSCEData(x, assay.type = assay.type, clusterVar = clusterVar)
if (missing(processed)) {
if (assay.type == "counts") processed <- FALSE
else processed <- TRUE
processed <- assay.type != "counts"
}
plotTernary(values[[1]], clusterVar = values[[2]], processed = processed,
...)
Expand Down Expand Up @@ -395,8 +392,9 @@ plotTernary.simMat <- function(
p <- p +
annotate("segment", x = subcoords[,1], y = subcoords[,2],
xend = subcoords[,3], yend = subcoords[,4],
color = labelColors[i], size = arrowLinewidth,
arrow = arrow(angle = arrowAngle, length = unit(arrowLen, "cm"),
color = labelColors[i], linewidth = arrowLinewidth,
arrow = arrow(angle = arrowAngle,
length = unit(arrowLen, "cm"),
type = "closed"))
}
}
Expand Down
29 changes: 24 additions & 5 deletions R/util.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
`%||%` <- function(x, y) {
if (is.null(x)) y
else x
}

.scaleMinMax <- function(x) {
if (all(x == 0)) return(x)
else {
Expand Down Expand Up @@ -69,18 +74,25 @@ is.rawCounts <- function(x) {
.getSeuratData <- function(
object,
assay = NULL,
slot = "data",
layer = "counts",
clusterVar = NULL
) {
if (!requireNamespace("Seurat", quietly = TRUE)) {
stop("Please install package 'Seurat' before interacting with a ", # nocov
"Seurat object.\ninstall.packages(\"Seurat\")") # nocov
}
mat <- Seurat::GetAssayData(object, slot = slot, assay = assay)
if (is.null(clusterVar)) clusterVar <- Seurat::Idents(object)
else if (length(clusterVar) == 1) {
if (!requireNamespace("SeuratObject", quietly = TRUE)) {
stop("Please install package 'SeuratObject' before interacting with a ", # nocov
"Seurat object.\ninstall.packages(\"Seurat\")") # nocov
}
mat <- SeuratObject::LayerData(object, layer = layer, assay = assay)
clusterVar <- clusterVar %||% SeuratObject::Idents(object)
if (length(clusterVar) == 1) {
clusterVar <- object[[clusterVar]][[1]]
}
if (length(clusterVar) != ncol(object)) {
stop("Invalid `clusterVar`.")
}
return(list(mat, clusterVar))
}

Expand All @@ -100,13 +112,20 @@ is.rawCounts <- function(x) {
"\nBiocManager::install(\"SummarizedExperiment\")") # nocov
}
mat <- SummarizedExperiment::assay(object, assay.type)

if (is.null(clusterVar)) {
if (inherits(object, "SingleCellExperiment")) {
clusterVar <- SingleCellExperiment::colLabels(object)
} else {
stop("No default labels available for this SCE object.")
}
} else if (length(clusterVar) == 1) {
}
if (length(clusterVar) == 1) {
clusterVar <- SummarizedExperiment::colData(object)[[clusterVar]]
}
if (length(clusterVar) != ncol(object)) {
stop("Invalid `clusterVar`.")
}
return(list(mat, clusterVar))
}

Expand Down
6 changes: 3 additions & 3 deletions man/colNormalize.Rd

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

8 changes: 3 additions & 5 deletions man/plotBinary.Rd

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

6 changes: 3 additions & 3 deletions man/plotQuaternary.Rd

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

6 changes: 3 additions & 3 deletions man/plotTernary.Rd

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

Loading

0 comments on commit cb45fcb

Please sign in to comment.