Skip to content

Commit

Permalink
GetNamedClusteringRuns
Browse files Browse the repository at this point in the history
  • Loading branch information
vertesy committed Jun 22, 2024
1 parent a529649 commit c0bdbda
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 19 deletions.
28 changes: 22 additions & 6 deletions R/Seurat.Utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -1165,20 +1165,32 @@ getClusterNames <- function(obj = combined.obj, ident = GetClusteringRuns(obj)[2
#' @param obj Seurat object, Default: combined.obj
#' @param res Clustering resoluton to use, Default: FALSE
#' @param pat Pattern to match, Default: `*snn_res.*[0-9]$`
#' @param v
#'
#' @return Prints and returns the sorted unique cluster names as a character vector.
#' @examples
#' \dontrun{
#' if (interactive()) {
#' GetClusteringRuns(obj = combined.obj, pat = "*snn_res.*[0-9]$")
#' }
#' }
#' @export
GetClusteringRuns <- function(obj = combined.obj, res = FALSE, pat = "*snn_res.[0-9].[0-9]+$") { # OLD: '*snn_res.*[0-9]$'
GetClusteringRuns <- function(obj = combined.obj,
res = FALSE, pat = "*snn_res.[0-9].[0-9]+$",
v = TRUE) {

if (!isFALSE(res)) pat <- gsub(x = pat, pattern = "\\[.*\\]", replacement = res)

clustering.results <- sort(CodeAndRoll2::grepv(x = colnames(obj@meta.data), pattern = pat))

if (identical(clustering.results, character(0))) warning("No matching (simple) clustering column found!", immediate. = TRUE)
message("Clustering runs found:")
dput(clustering.results)

if(v) {
message("Clustering runs found:")
dput(clustering.results)
}


return(clustering.results)
}

Expand All @@ -1193,6 +1205,8 @@ GetClusteringRuns <- function(obj = combined.obj, res = FALSE, pat = "*snn_res.[
#' @param topgene Match clustering named after top expressed gene (see vertesy/Seurat.pipeline/~Diff gene expr.), Default: FALSE
#' @param pat Pattern to match, Default: '^cl.names.Known.*[0,1]\.[0-9]$'
#' @param find.alternatives If TRUE, tries to find alternative clustering runs with
#' the same resolution, Default: TRUE
#' @param v Verbose output, Default: TRUE
#' simple GetClusteringRuns(), Default: TRUE
#' @examples
#' \dontrun{
Expand All @@ -1205,7 +1219,8 @@ GetNamedClusteringRuns <- function(
obj = combined.obj,
res = list(FALSE, 0.5)[[1]], topgene = FALSE,
pat = c("^cl.names.*[0-9]\\.[0-9]", "Name|name")[2],
find.alternatives = TRUE) {
find.alternatives = TRUE,
v = TRUE) {

if (res) pat <- gsub(x = pat, pattern = "\\[.*\\]", replacement = res)
if (topgene) pat <- gsub(x = pat, pattern = "Known", replacement = "top")
Expand All @@ -1215,10 +1230,11 @@ GetNamedClusteringRuns <- function(
warning("No matching (named) clustering column found! Trying GetClusteringRuns(..., pat = '*_res.*[0,1]\\.[0-9]$)",
immediate. = TRUE )
if (find.alternatives) clustering.results <-
GetClusteringRuns(obj = obj, res = FALSE, pat = "*_res.*[0,1]\\.[0-9]$")
GetClusteringRuns(obj = obj, res = FALSE, pat = "*_res.*[0,1]\\.[0-9]$", v = F)
}

dput(clustering.results)
if(v) dput(clustering.results)

return(clustering.results)
}

Expand Down
12 changes: 6 additions & 6 deletions R/Seurat.Utils.Visualization.R
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ plotGeneExprHistAcrossCells <- function(

PctCellsAboveX <- function(obj = combined.obj,
feature = "TOP2A",
ident = GetNamedClusteringRuns(obj)[1],
ident = GetNamedClusteringRuns(obj = obj, v = F)[1],
threshold = 2,
suffix = ppp(substitute(obj), ncol(obj), "thr", threshold),
box = FALSE,
Expand Down Expand Up @@ -1866,7 +1866,7 @@ qFeatureScatter <- function(
qSeuViolin <- function(
obj,
feature = "nFeature_RNA",
ident = GetNamedClusteringRuns(obj)[1],
ident = GetNamedClusteringRuns(obj = obj, v = F)[1],
split.by = NULL,
colors = NULL,
replace.na = FALSE,
Expand Down Expand Up @@ -2144,7 +2144,7 @@ clUMAP <- function(
#

if (is.null(ident)) {
ident <- GetNamedClusteringRuns(obj = obj)[1]
ident <- GetNamedClusteringRuns(obj = obj, v = F)[1]
message("Identity not provided. Plotting: ", ident)
}

Expand Down Expand Up @@ -2311,7 +2311,7 @@ umapHiLightSel <- function(obj = combined.obj,
#' @export
DimPlot.ClusterNames <- function(
obj = combined.obj,
ident = GetNamedClusteringRuns(obj)[1],
ident = GetNamedClusteringRuns(obj = obj, v = F)[1],
reduction = "umap",
title = ident,
...) {
Expand Down Expand Up @@ -3853,7 +3853,7 @@ ww.check.quantile.cutoff.and.clip.outliers <- function(expr.vec = plotting.data[
plot3D.umap.gene <- function(
gene = "TOP2A",
obj = combined.obj,
annotate.by = GetNamedClusteringRuns(obj)[1],
annotate.by = GetNamedClusteringRuns(obj = obj, v = F)[1],
quantileCutoff = .99,
def.assay = c("integrated", "RNA")[2],
suffix = NULL,
Expand Down Expand Up @@ -3951,7 +3951,7 @@ plot3D.umap.gene <- function(

plot3D.umap <- function(
obj = combined.obj,
category = GetNamedClusteringRuns(obj)[1],
category = GetNamedClusteringRuns(obj = obj, v = F)[1],
annotate.by = category,
suffix = NULL,
dotsize = 1.25,
Expand Down
2 changes: 1 addition & 1 deletion man/DimPlot.ClusterNames.Rd

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

8 changes: 7 additions & 1 deletion man/GetClusteringRuns.Rd

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

6 changes: 5 additions & 1 deletion man/GetNamedClusteringRuns.Rd

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

2 changes: 1 addition & 1 deletion man/PctCellsAboveX.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/clUMAP.Rd

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

2 changes: 1 addition & 1 deletion man/plot3D.umap.Rd

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

2 changes: 1 addition & 1 deletion man/plot3D.umap.gene.Rd

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

2 changes: 1 addition & 1 deletion man/qSeuViolin.Rd

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

0 comments on commit c0bdbda

Please sign in to comment.