From 5adfab893edb73d8f27c0c73a0feb964bc65f618 Mon Sep 17 00:00:00 2001 From: vertesy Date: Mon, 18 Nov 2024 12:52:31 +0100 Subject: [PATCH] bf qSeuViolin --- R/Seurat.Utils.R | 5 ++++- R/Seurat.Utils.Visualization.R | 20 ++++++++++++++++--- ...dot-replace_by_most_frequent_categories.Rd | 1 + man/matchBestIdentity.Rd | 1 + man/qSeuViolin.Rd | 2 ++ 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/R/Seurat.Utils.R b/R/Seurat.Utils.R index 2f23582..e9aeea2 100644 --- a/R/Seurat.Utils.R +++ b/R/Seurat.Utils.R @@ -1247,7 +1247,7 @@ filterExpressedGenes <- function(genes, gene_list = all.genes # browser() # Step 1: Intersect the gene symbols with the names in the list and report statistics - matching_genes <- CodeAndRoll2::intersect.w.Names(x = genes, y = names(gene_list), names = "x") + matching_genes <- CodeAndRoll2::intersect.wNames(x = genes, y = names(gene_list), names = "x") message("Number of matching genes: ", length(matching_genes), " from ", length(genes) , ". Missing: ", head(setdiff(genes, names(gene_list))), " ...") @@ -1268,6 +1268,9 @@ filterExpressedGenes <- function(genes, gene_list = all.genes return(filtered_genes) } +# r$CodeAndRoll2() +# filterExpressedGenes(AstrocyteMarkers) + diff --git a/R/Seurat.Utils.Visualization.R b/R/Seurat.Utils.Visualization.R index 76f1aba..96d8068 100644 --- a/R/Seurat.Utils.Visualization.R +++ b/R/Seurat.Utils.Visualization.R @@ -1971,6 +1971,8 @@ qSeuViolin <- function( obj, feature = "nFeature_RNA", ident = GetNamedClusteringRuns(obj = obj, v = FALSE)[1], + assay = "RNA", + slot = "data", split.by = NULL, colors = NULL, clip.outliers = TRUE, @@ -2006,6 +2008,17 @@ qSeuViolin <- function( feature %in% colnames(obj@meta.data) || feature %in% rownames(obj) ) + is_meta_feature <- feature %in% colnames(obj@meta.data) # check if feature is in meta.data, otherwise it is a gene name + + value_vector <- + if(is_meta_feature){ + obj@meta.data[[feature]] + } else { + # Extract normalized expression values ("data" slot) from the RNA assay + GetAssayData(obj, assay = assay, slot = slot)[feature, ] + } + + if (exists("idents")) warning("Use arg. ident instead of idents!\n", immediate. = TRUE) if (exists("features")) warning("Use arg. feature instead of features!\n", immediate. = TRUE) @@ -2024,14 +2037,14 @@ qSeuViolin <- function( if (replace.na) { warning("NA's are not, but zeros are displayed on the plot. Avoid replace.na when possible", immediate. = TRUE) - obj@meta.data[[feature]] <- na.replace(x = obj@meta.data[[feature]], replace = 0) + value_vector <- na.replace(x = value_vector, replace = 0) } - # browser() + if (clip.outliers) { warning("Outliers are clipped at percentiles 0.5% and 99.5%", immediate. = TRUE) obj@meta.data[[feature]] <- CodeAndRoll2::clip.outliers.at.percentile( - x = obj@meta.data[[feature]], percentiles = c(.005, .995) + x = value_vector, percentiles = c(.005, .995) ) } @@ -2041,6 +2054,7 @@ qSeuViolin <- function( colors <- as.factor.numeric(sapply(split(col_long, split_col), unique)) stopifnot("colors cannot be uniquely split by ident. Set colors = NULL!" = length(colors) == length(unique(split_col))) } + # browser() p.obj <- Seurat::VlnPlot( object = obj, diff --git a/man/dot-replace_by_most_frequent_categories.Rd b/man/dot-replace_by_most_frequent_categories.Rd index 9acf16f..77ef01a 100644 --- a/man/dot-replace_by_most_frequent_categories.Rd +++ b/man/dot-replace_by_most_frequent_categories.Rd @@ -11,6 +11,7 @@ show_plot = TRUE, suffix_barplot = NULL, ext = "png", + min.thr = 0.5, ... ) } diff --git a/man/matchBestIdentity.Rd b/man/matchBestIdentity.Rd index b165007..7413964 100644 --- a/man/matchBestIdentity.Rd +++ b/man/matchBestIdentity.Rd @@ -14,6 +14,7 @@ matchBestIdentity( plot_suffix = prefix, barplot_match = TRUE, barplot_fractions = TRUE, + rnd_colors = T, w = 12, h = 9, ... diff --git a/man/qSeuViolin.Rd b/man/qSeuViolin.Rd index ab4bb91..cde4097 100644 --- a/man/qSeuViolin.Rd +++ b/man/qSeuViolin.Rd @@ -8,6 +8,8 @@ qSeuViolin( obj, feature = "nFeature_RNA", ident = GetNamedClusteringRuns(obj = obj, v = FALSE)[1], + assay = "RNA", + slot = "data", split.by = NULL, colors = NULL, clip.outliers = TRUE,