Skip to content

Commit

Permalink
Fixing the misassumptions on Seurat v5 and end of cycle of v4.
Browse files Browse the repository at this point in the history
  • Loading branch information
enblacar committed Oct 4, 2023
1 parent da053bf commit 6d64ae7
Show file tree
Hide file tree
Showing 15 changed files with 86 additions and 93 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## General.
- Fixed a bug that prevented error messages stating the dependencies missing per function to show up properly.
- Fixed assumptions on Seurat v4 and v5 and reverted to the use of cannonical `GetAssayData` functions.
- Fixed assumptions on Seurat v4 and v5 and reverted to the use of cannonical `GetAssayData` and `SetAssayData` functions.

## do_BeeSwarmPlot()
- Changed default continuous palette to `YlGnBu`.
Expand Down
10 changes: 6 additions & 4 deletions R/do_AffinityAnalysisPlot.R
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,11 @@ do_AffinityAnalysisPlot <- function(sample,
dplyr::filter(.data$target != "deleteme")

# Get expression data.
suppressWarnings({
mat <- SeuratObject::GetAssayData(sample,
assay = assay,
slot = slot)

assay = assay,
slot = slot)
})
# Compute activities.
if(isTRUE(verbose)){message(paste0(add_info(), crayon_body("Computing "),
crayon_key("activities"),
Expand Down Expand Up @@ -256,6 +257,7 @@ do_AffinityAnalysisPlot <- function(sample,
counter <- 0
for (group in group.by){
counter <- counter + 1
suppressWarnings({
data.use <- SeuratObject::GetAssayData(sample,
assay = "affinity",
slot = "scale.data") %>%
Expand All @@ -269,7 +271,7 @@ do_AffinityAnalysisPlot <- function(sample,
tidyr::pivot_longer(cols = -dplyr::all_of(c("cell", group)),
names_to = "source",
values_to = "score")

})
# Clustering based on the median across all cells.
data.cluster <- data.use %>%
tidyr::pivot_wider(id_cols = dplyr::all_of(c("cell", group)),
Expand Down
2 changes: 2 additions & 0 deletions R/do_BeeSwarmPlot.R
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,9 @@ do_BeeSwarmPlot <- function(sample,
sample@meta.data$rank_me <- sample@meta.data[, feature_to_rank]
sample@meta.data$rank <- rank(sample@meta.data$rank_me)
} else if (feature_to_rank %in% rownames(sample)){
suppressWarnings({
sample@meta.data$rank_me <- SeuratObject::GetAssayData(object = sample, slot = slot, assay = assay)[feature_to_rank, ]
})
sample@meta.data$rank <- rank(sample@meta.data$rank_me)
} else if (feature_to_rank %in% dim_colnames){
for(red in Seurat::Reductions(object = sample)){
Expand Down
2 changes: 2 additions & 0 deletions R/do_CopyNumberVariantPlot.R
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ do_CopyNumberVariantPlot <- function(sample,

list.data <- list()
for (group in group.by){
suppressWarnings({
data.use <- SeuratObject::GetAssayData(object = sample,
assay = "CNV_scores",
slot = "data") %>%
Expand All @@ -266,6 +267,7 @@ do_CopyNumberVariantPlot <- function(sample,

dplyr::group_by(.data[[group]], .data$Event) %>%
dplyr::summarise("mean" = mean(.data$CNV_score, na.rm = TRUE))
})

events <- c(as.character(seq(1, 22)), vapply(seq(1, 22), function(x){return(c(paste0(x, "p"), paste0(x, "q")))}, FUN.VALUE = character(2)))
if (base::isFALSE(flip)){
Expand Down
6 changes: 4 additions & 2 deletions R/do_CorrelationPlot.R
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ do_CorrelationPlot <- function(sample = NULL,
variable_genes <- Seurat::VariableFeatures(sample)

# Sort them in order (for ATAC experiments).
suppressWarnings({
genes <- rownames(SeuratObject::GetAssayData(object = sample,
assay = assay,
slot = "data"))
Expand All @@ -165,14 +166,15 @@ do_CorrelationPlot <- function(sample = NULL,
dplyr::filter(.data$Genes %in% variable_genes) %>%
dplyr::arrange(.data$Position) %>%
dplyr::pull(.data$Genes)

})

# Subset sample according to the variable genes.
sample <- sample[genes, ]
# Scale the data
sample <- Seurat::ScaleData(sample, features = genes, verbose = FALSE)

# Retrieve correlation matrix.
suppressWarnings({
out <- sample@meta.data %>%
dplyr::select(dplyr::all_of(c(group.by))) %>%
tibble::rownames_to_column(var = "cell") %>%
Expand All @@ -197,7 +199,7 @@ do_CorrelationPlot <- function(sample = NULL,
as.matrix() %>%
stats::cor() %>%
round(digits = 2)

})
# Compute hclust.
if (isTRUE(cluster)){
order <- rownames(out)[stats::hclust(stats::dist(out, method = "euclidean"), method = "ward.D")$order]
Expand Down
3 changes: 2 additions & 1 deletion R/do_ExpressionHeatmap.R
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ do_ExpressionHeatmap <- function(sample,
# Extract activities from object as a long dataframe
suppressMessages({
sample$group.by <- sample@meta.data[, group]

suppressWarnings({
df <- SeuratObject::GetAssayData(sample,
assay = assay,
slot = slot)[features, , drop = FALSE] %>%
Expand All @@ -211,6 +211,7 @@ do_ExpressionHeatmap <- function(sample,
dplyr::group_by(.data$group.by, .data$gene) %>%
dplyr::summarise(mean = mean(.data$expression, na.rm = TRUE))
df.order <- df
})
})
matrix.list[[group]][["df"]] <- df
matrix.list[[group]][["df.order"]] <- df.order
Expand Down
2 changes: 2 additions & 0 deletions R/do_FeaturePlot.R
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,9 @@ do_FeaturePlot <- function(sample,
sample$dummy <- sample@meta.data[, feature]
## Or is a gene in the object.
} else if (feature %in% rownames(sample)){
suppressWarnings({
sample$dummy <- SeuratObject::GetAssayData(object = sample, slot = slot, assay = assay)[feature, ]
})
## Or is a dimensional reduction component.
} else if (feature %in% dim_colnames){
# Iterate over each dimensional reduction in the object.
Expand Down
2 changes: 2 additions & 0 deletions R/do_GeyserPlot.R
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ do_GeyserPlot <- function(sample,
tibble::rownames_to_column(var = "cell") %>%
tibble::as_tibble()
} else if (isTRUE(feature %in% rownames(sample))){
suppressWarnings({
data <- SeuratObject::GetAssayData(object = sample,
assay = assay,
slot = slot)[feature, , drop = FALSE] %>%
Expand All @@ -300,6 +301,7 @@ do_GeyserPlot <- function(sample,
dplyr::select(dplyr::all_of(c(group.by))) %>%
tibble::rownames_to_column(var = "cell")},
by = "cell")
})
} else if (isTRUE(feature %in% dim_colnames)){
data <- sample@reductions[[reduction]][[]][, feature, drop = FALSE] %>%
as.data.frame() %>%
Expand Down
2 changes: 2 additions & 0 deletions R/do_GroupwiseDEPlot.R
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ do_GroupwiseDEPlot <- function(sample,
list.exp <- list()
for (group in group.by){
order.use <- if (is.factor(sample@meta.data[, group])){levels(sample@meta.data[, group])} else {sort(unique(sample@meta.data[, group]))}
suppressWarnings({
data <- SeuratObject::GetAssayData(sample,
assay = assay,
slot = slot)[genes.use, ] %>%
Expand All @@ -301,6 +302,7 @@ do_GroupwiseDEPlot <- function(sample,
"Group" = factor(.data[[group]], levels = rev(order.use)))

list.exp[[group]] <- data
})
}


Expand Down
4 changes: 2 additions & 2 deletions R/do_LoadingsPlot.R
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ do_LoadingsPlot <- function(sample,
dplyr::left_join(y = loadings,
by = "PC",
relationship = "many-to-many")

suppressWarnings({
data.use <- data.use %>%
dplyr::left_join(y = {SeuratObject::GetAssayData(sample,
assay = assay,
Expand All @@ -215,7 +215,7 @@ do_LoadingsPlot <- function(sample,
values_to = "Expression")},
by = c("Gene", "Cell")) %>%
dplyr::mutate("Gene" = factor(.data$Gene, levels = genes.use))

})
data.loading <- data.use %>%
dplyr::group_by(.data$Gene, .data$PC) %>%
dplyr::reframe("mean_Loading_Score" = mean(.data$Loading_Score, na.rm = TRUE))
Expand Down
8 changes: 5 additions & 3 deletions R/do_PathwayActivityPlot.R
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ do_PathwayActivityPlot <- function(sample,
# Extract activities from object as a long dataframe
suppressMessages({
sample$group.by <- sample@meta.data[, group]


suppressWarnings({
df <- t(as.matrix(SeuratObject::GetAssayData(object = sample,
assay = "progeny",
slot = slot))) %>%
Expand All @@ -196,7 +197,7 @@ do_PathwayActivityPlot <- function(sample,
values_to = "score") %>%
dplyr::group_by(.data$group.by, .data$source) %>%
dplyr::summarise(mean = mean(.data$score, na.rm = TRUE))

})
df.order <- df
df.order[is.na(df.order)] <- 0

Expand All @@ -206,7 +207,7 @@ do_PathwayActivityPlot <- function(sample,

if (!is.null(split.by)){
sample$split.by <- sample@meta.data[, split.by]

suppressWarnings({
df.split <- t(as.matrix(SeuratObject::GetAssayData(object = sample,
assay = "progeny",
slot = slot))) %>%
Expand All @@ -222,6 +223,7 @@ do_PathwayActivityPlot <- function(sample,
dplyr::group_by(.data$split.by, .data$group.by, .data$source) %>%
dplyr::summarise(mean = mean(.data$score, na.rm = TRUE))
matrix.list[[group]][["df.split"]] <- df.split
})
}
})
}
Expand Down
9 changes: 6 additions & 3 deletions R/do_SCExpressionHeatmap.R
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,11 @@ do_SCExpressionHeatmap <- function(sample,
}

`%>%` <- magrittr::`%>%`

suppressWarnings({
genes.avail <- rownames(SeuratObject::GetAssayData(sample, slot = slot, assay = assay))

})

assertthat::assert_that(sum(features %in% genes.avail) > 0,
msg = paste0(add_cross(), crayon_body("None of the features are present in the row names of the assay "),
crayon_key(assay),
Expand Down Expand Up @@ -212,12 +215,12 @@ do_SCExpressionHeatmap <- function(sample,
crayon_body(".")))
}


suppressWarnings({
matrix <- SeuratObject::GetAssayData(sample,
assay = assay,
slot = slot)[features, , drop = FALSE] %>%
as.matrix()

})
# Check group.by.
out <- check_group_by(sample = sample,
group.by = group.by,
Expand Down
7 changes: 4 additions & 3 deletions R/do_TFActivityPlot.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ do_TFActivityPlot <- function(sample,
# Extract activities from object as a long dataframe
suppressMessages({
sample$group.by <- sample@meta.data[, group]

suppressWarnings({
df <- t(as.matrix(SeuratObject::GetAssayData(object = sample,
assay = "dorothea",
slot = slot))) %>%
Expand All @@ -202,10 +202,10 @@ do_TFActivityPlot <- function(sample,
dplyr::group_by(.data$group.by, .data$source) %>%
dplyr::summarise(mean = mean(.data$score, na.rm = TRUE))
df.order <- df

})
if (!is.null(split.by)){
sample$split.by <- sample@meta.data[, split.by]

suppressWarnings({
df.split <- t(as.matrix(SeuratObject::GetAssayData(object = sample,
assay = "dorothea",
slot = slot))) %>%
Expand All @@ -221,6 +221,7 @@ do_TFActivityPlot <- function(sample,
dplyr::group_by(.data$split.by, .data$group.by, .data$source) %>%
dplyr::summarise(mean = mean(.data$score, na.rm = TRUE))
matrix.list[[group]][["df.split"]] <- df.split
})
}


Expand Down
50 changes: 6 additions & 44 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -1026,9 +1026,11 @@ compute_scale_limits <- function(sample, feature, assay = NULL, reduction = NULL
}

if (feature %in% rownames(sample)){
suppressWarnings({
data.check <- SeuratObject::GetAssayData(sample,
assay = assay,
slot = slot)[feature, ]
})
scale.begin <- min(data.check, na.rm = TRUE)
scale.end <- max(data.check, na.rm = TRUE)
} else if (feature %in% colnames(sample@meta.data)){
Expand Down Expand Up @@ -1950,8 +1952,10 @@ compute_enrichment_scores <- function(sample,
if (is.null(slot)){
slot <- "data"
}
suppressWarnings({
scores <- AUCell::AUCell_run(exprMat = SeuratObject::GetAssayData(sample, assay = assay, slot = slot),
geneSets = input_gene_list)
})
scores <- scores@assays@data$AUC %>%
as.matrix() %>%
t() %>%
Expand Down Expand Up @@ -2096,6 +2100,7 @@ get_data_column <- function(sample,
tibble::rownames_to_column(var = "cell") %>%
dplyr::rename("feature" = dplyr::all_of(c(feature)))
} else if (isTRUE(feature %in% rownames(sample))){
suppressWarnings({
feature_column <- SeuratObject::GetAssayData(object = sample,
assay = assay,
slot = slot)[feature, , drop = FALSE] %>%
Expand All @@ -2104,6 +2109,7 @@ get_data_column <- function(sample,
as.data.frame() %>%
tibble::rownames_to_column(var = "cell") %>%
dplyr::rename("feature" = dplyr::all_of(c(feature)))
})
} else if (isTRUE(feature %in% dim_colnames)){
feature_column <- sample@reductions[[reduction]][[]][, feature, drop = FALSE] %>%
as.data.frame() %>%
Expand Down Expand Up @@ -3259,50 +3265,6 @@ get_SCpubr_colors <- function(){
return(colors)
}

#' Set assay data depending on the version of SeuratObject
#'
#' @param sample Seurat object.
#' @param assay Assay name.
#' @param slot slot name.
#' @param data data to set.
#'
#' @return The assay data.
#' @noRd
#' @examples
#' \donttest{
#' TBD
#' }
.SetAssayData <- function(sample,
assay,
slot,
data){

# Check version of SeuratObject.
version <- utils::packageVersion("SeuratObject")
# nocov start
if (version > "4.1.3"){
if (slot == "counts"){
sample@assays[[assay]]$counts <- data
} else if (slot == "data"){
sample@assays[[assay]]$data <- data
} else if (slot == "scale.data"){
sample@assays[[assay]]$scale.data <- data
}

# Uncomment once the version is on CRAN.
# SeuratObject::LayerData(object = sample,
# assay = assay,
# layer = slot) <- data

# nocov end
} else {
sample <- SeuratObject::SetAssayData(object = sample,
assay = assay,
slot = slot,
new.data = data)
}
return(sample)
}

#' Check the group.by parameter
#'
Expand Down
Loading

0 comments on commit 6d64ae7

Please sign in to comment.