@@ -90,15 +90,16 @@ RunVelocity <- function(sce, mode = c("deterministic", "stochastic", "dynamical"
9090# '
9191# ' @param sce A SingleCellExperiment object with velocity calculated.
9292# ' @param reduction String specifying the reduction to plot on. Defaults to "UMAP".
93- # ' @param group.by String specifying the colData column for cell grouping.
93+ # ' @param group.by String specifying the colData column for cell grouping. If NULL,
94+ # ' uses `ActiveIdent(sce)`, including the special `"colLabels"` identity source.
9495# ' @param ... Additional arguments passed to plotting function.
9596# '
9697# ' @return A ggplot object.
9798# ' @importFrom rlang .data
9899# ' @importFrom S4Vectors metadata
99100# ' @importFrom SummarizedExperiment colData
100101# ' @export
101- VelocityPlot <- function (sce , reduction = " UMAP" , group.by = ActiveIdent( sce ) , ... ) {
102+ VelocityPlot <- function (sce , reduction = " UMAP" , group.by = NULL , ... ) {
102103 if (! requireNamespace(" velociraptor" , quietly = TRUE )) {
103104 stop(" Please install 'velociraptor' to plot RNA Velocity." )
104105 }
@@ -133,12 +134,38 @@ VelocityPlot <- function(sce, reduction = "UMAP", group.by = ActiveIdent(sce), .
133134 # Let's extract velocity vectors for the specific reduction
134135 vel_emb <- velociraptor :: embedVelocity(emb , vel_res )
135136 grid_df <- velociraptor :: gridVectors(emb , vel_emb )
137+
138+ if (is.null(group.by )) {
139+ group.by <- ActiveIdent(sce )
140+ }
141+ if (is.null(group.by )) {
142+ stop(" No active identity found. Please provide 'group.by'." )
143+ }
144+
145+ if (identical(group.by , " colLabels" )) {
146+ groups <- SingleCellExperiment :: colLabels(sce )
147+ if (is.null(groups )) {
148+ stop(" Active identity is 'colLabels' but `colLabels(sce)` is empty." )
149+ }
150+ } else {
151+ meta <- as.data.frame(SummarizedExperiment :: colData(sce ))
152+ if (! group.by %in% colnames(meta )) {
153+ stop(" Column '" , group.by , " ' not found in colData(sce)." )
154+ }
155+ groups <- meta [[group.by ]]
156+ }
157+ if (length(groups ) != nrow(emb )) {
158+ stop(
159+ " Grouping vector derived from '" , group.by , " ' has length " , length(groups ),
160+ " , but reduction '" , reduction , " ' has " , nrow(emb ), " cells."
161+ )
162+ }
136163
137164 # Prepare base plot
138165 df <- data.frame (
139166 x = emb [, 1 ],
140167 y = emb [, 2 ],
141- group = colData( sce )[[ group.by ]]
168+ group = groups
142169 )
143170
144171 p <- ggplot2 :: ggplot() +
0 commit comments