Skip to content

Commit

Permalink
substitute_deparse
Browse files Browse the repository at this point in the history
  • Loading branch information
vertesy committed Oct 31, 2024
1 parent c6b5953 commit d7567e4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 25 deletions.
4 changes: 2 additions & 2 deletions R/Seurat.Utils.Metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
addTranslatedMetadata <- function(obj = combined.obj,
orig.ident = "RNA_snn_res.0.4",
translation_as_named_vec,
new_col_name = substitute(translation_as_named_vec),
new_col_name = substitute_deparse(translation_as_named_vec),
suffix = NULL,
plot = F,
...) {
Expand Down Expand Up @@ -683,7 +683,7 @@ seu.add.meta.from.table <- function(obj = combined.obj, meta, suffix = ".fromMet
seu.map.and.add.new.ident.to.meta <- function(
obj = combined.obj, ident.table = clusterIDs.GO.process,
orig.ident = Idents(obj),
metaD.colname = substitute(ident.table)) {
metaD.colname = substitute_deparse(ident.table)) {
# identities should match
{
Idents(obj) <- orig.ident
Expand Down
39 changes: 25 additions & 14 deletions R/Seurat.Utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ showMiscSlots <- function(obj = combined.obj, max.level = 1, subslot = NULL,
str(slotX, max.level = max.level, ...)

# Path to slot
msg <- paste0(substitute(obj), "@misc")
msg <- paste0(substitute_deparse(obj), "@misc")
if (!is.null(subslot)) msg <- paste0(msg, "$", substitute_deparse(subslot))
message(msg)
}
Expand Down Expand Up @@ -1015,7 +1015,7 @@ calc.q99.Expression.and.set.all.genes <- function(
assay = c("RNA", "integrated", "SCT")[1],
set.misc = TRUE,
assign_to_global_env = TRUE,
suffix = substitute(obj),
suffix = substitute_deparse(obj),
plot = TRUE,
show = TRUE,
obj.version = obj@version
Expand Down Expand Up @@ -1494,7 +1494,7 @@ calc.cluster.averages <- function(
)
},
fname = ppp(col_name, split_by, "cluster.average.barplot.pdf", ...)) { # calc.cluster.averages of a m
iprint(substitute(obj), "split by", split_by)
iprint(substitute_deparse(obj), "split by", split_by)
if (absolute.thr) iprint("In case of the absolute threshold, only the returned values are correct, the plot annotations are not!")

if (plot.UMAP.too) qUMAP(obj = obj, feature = col_name)
Expand Down Expand Up @@ -2266,7 +2266,8 @@ downsampleSeuObjByIdentAndMaxcells <- function(obj,
#' v = TRUE
#' )
#'
RelabelSmallCategories <- function(obj, col_in, backup_col_name = ppp(col_in, "orig"), min_count = 100, small_label = "Other", v = TRUE) {
RelabelSmallCategories <- function(obj, col_in, backup_col_name = ppp(col_in, "orig")
, min_count = 100, small_label = "Other", v = TRUE) {
# Input assertions
stopifnot(
inherits(obj, "Seurat"), # Check if obj is a Seurat object
Expand All @@ -2280,12 +2281,22 @@ RelabelSmallCategories <- function(obj, col_in, backup_col_name = ppp(col_in, "o

message("backup_col_name: ", backup_col_name)

categories <- obj@meta.data[[backup_col_name]] <- obj@meta.data[[col_in]] # Extract the specified metadata column
category_counts <- table(categories) # Count occurrences of each category
small_categories <- names(category_counts[category_counts < min_count]) # Identify small categories
# Extract the specified metadata column
categories <- obj@meta.data[[backup_col_name]] <- obj@meta.data[[col_in]]

# Count occurrences of each category
category_counts <- table(categories)

# Identify small categories
small_categories <- names(category_counts[category_counts < min_count])

new_categories <- as.character(categories) # Copy original categories
new_categories[new_categories %in% small_categories] <- small_label # Relabel small categories
obj@meta.data[[col_in]] <- new_categories # Add new column to metadata

# Relabel small categories
new_categories[new_categories %in% small_categories] <- small_label

# Add new column to metadata
obj@meta.data[[col_in]] <- new_categories

if (v) { # Verbose output
total_cells <- length(categories)
Expand Down Expand Up @@ -3249,7 +3260,7 @@ plot.Gene.Cor.Heatmap <- function(
)
iprint(length(corgene.names), "genes are more (anti-)correlated than +/-:", min.g.cor)

pname <- paste0("Pearson correlations of ", substitute(genes), "\n min.cor:", min.g.cor, " | ", assay.use, ".", slot.use)
pname <- paste0("Pearson correlations of ", substitute_deparse(genes), "\n min.cor:", min.g.cor, " | ", assay.use, ".", slot.use)
o.heatmap <- pheatmap::pheatmap(cor.mat[corgene.names, corgene.names], main = pname, cutree_rows = cutRows, cutree_cols = cutCols, ...)
MarkdownReports::wplot_save_pheatmap(o.heatmap, plotname = make.names(pname))

Expand Down Expand Up @@ -4678,7 +4689,7 @@ isave.RDS <- function(
try(obj@misc$p <- p, silent = TRUE)
try(obj@misc$all.genes <- all.genes, silent = TRUE)
}
fnameBase <- kppu(prefix, substitute(obj), project, suffix, idate(Format = "%Y.%m.%d_%H.%M"))
fnameBase <- kppu(prefix, substitute_deparse(obj), project, suffix, idate(Format = "%Y.%m.%d_%H.%M"))
fnameBase <- trimws(fnameBase, whitespace = "_")
FNN <- paste0(path_rdata, fnameBase, ".Rds")
FNN <- gsub(pattern = "~/", replacement = homepath, x = FNN)
Expand Down Expand Up @@ -4752,19 +4763,19 @@ xsave <- function(
annot.suffix <- if (is.list(obj)) kppd("ls", length(obj)) else NULL
}

if (!isFALSE(saveParams)) message("paramList: ", if (exists("paramList")) paste(substitute(paramList), length(paramList), " elements.") else " not provided.")
if (!isFALSE(saveParams)) message("paramList: ", if (exists("paramList")) paste(substitute_deparse(paramList), length(paramList), " elements.") else " not provided.")
if (!isFALSE(saveParams)) message("allGenes: ", if (exists("allGenes")) " found as global variable." else " not provided.")

try(tictoc::tic("xsave"), silent = TRUE)
if (showMemObject & v) try(memory.biggest.objects(), silent = TRUE)

fnameBase <- trimws(kppu(
prefix, substitute(obj), annot.suffix, suffix, project,
prefix, substitute_deparse(obj), annot.suffix, suffix, project,
idate(Format = "%Y.%m.%d_%H.%M")
), whitespace = "_")

FNN <- paste0(dir, fnameBase, ".qs")
CMND <- paste0(substitute(obj), " <- xread('", FNN, "')")
CMND <- paste0(substitute_deparse(obj), " <- xread('", FNN, "')")
if (v) message(CMND)

if ("Seurat" %in% is(obj)) {
Expand Down
22 changes: 13 additions & 9 deletions R/Seurat.Utils.Visualization.R
Original file line number Diff line number Diff line change
Expand Up @@ -3601,7 +3601,7 @@ scBarplotEnrichr <- function(df.enrichment,
tag = "...",
universe = NULL,
title = paste("GO Enriched Terms", tag),
subtitle = kppws("Input: ", substitute(df.enrichment)),
subtitle = kppws("Input: ", substitute_deparse(df.enrichment)),
caption = paste0(
"Input genes: ", length(df.enrichment@"gene"),
" | Enriched terms: ", nrow(df.enrichment),
Expand Down Expand Up @@ -3876,7 +3876,7 @@ save2plots.A4 <- function(
plot_list, pname = FALSE, suffix = NULL, scale = 1,
nrow = 2, ncol = 1,
h = 11.69 * scale, w = 8.27 * scale, ...) {
if (pname == FALSE) pname <- sppp(substitute(plot_list), suffix)
if (pname == FALSE) pname <- sppp(substitute_deparse(plot_list), suffix)
p1 <- cowplot::plot_grid(
plotlist = plot_list, nrow = nrow, ncol = ncol,
labels = LETTERS[1:length(plot_list)], ...
Expand Down Expand Up @@ -3926,7 +3926,7 @@ save4plots.A4 <- function(
nrow = 2, ncol = 2,
h = 8.27 * scale, w = 11.69 * scale,
...) {
if (pname == FALSE) pname <- sppp(substitute(plot_list), suffix)
if (pname == FALSE) pname <- sppp(substitute_deparse(plot_list), suffix)
p1 <- cowplot::plot_grid(
plotlist = plot_list, nrow = nrow, ncol = ncol,
labels = LETTERS[1:length(plot_list)], ...
Expand Down Expand Up @@ -4436,7 +4436,7 @@ Plot3D.ListOfGenes <- function(
obj = combined.obj # Plot and save list of 3D UMAP ot tSNE plots using plotly.
, annotate.by = "integrated_snn_res.0.7", opacity = 0.5, cex = 1.25, default.assay = c("integrated", "RNA")[2],
ListOfGenes = c("BCL11B", "FEZF2", "EOMES", "DLX6-AS1", "HOPX", "DDIT4"),
SubFolderName = ppp("plot3D", substitute(ListOfGenes))) {
SubFolderName = ppp("plot3D", substitute_deparse(ListOfGenes))) {
try(create_set_SubDir(SubFolderName))
obj. <- obj
rm("obj")
Expand All @@ -4462,11 +4462,15 @@ Plot3D.ListOfGenes <- function(
#'
#' @description This function plots and saves a list of 3D UMAP or tSNE plots using plotly.
#' @param obj A Seurat object for which the plot is to be created. Default: 'combined.obj'.
#' @param annotate.by Character vector specifying the metadata column to be used for annotating the plot. Default: 'integrated_snn_res.0.7'.
#' @param annotate.by Character vector specifying the metadata column to be used for annotating
#' the plot. Default: 'integrated_snn_res.0.7'.
#' @param cex Numeric value specifying the point size on the plot. Default: 1.25.
#' @param default.assay Character vector specifying the assay to be used. Default: 'RNA' (second element in the vector c("integrated", "RNA")).
#' @param ListOfCategories Character vector specifying the categories to be included in the plot. Default categories are "v.project", "experiment", "Phase", "integrated_snn_res.0.7".
#' @param SubFolderName String specifying the name of the subfolder where the plots will be saved. By default, it's created using the function ppp("plot3D", substitute(ListOfCategories)).
#' @param default.assay Character vector specifying the assay to be used. Default: 'RNA'
#' (second element in the vector c("integrated", "RNA")).
#' @param ListOfCategories Character vector specifying the categories to be included in the plot.
#' Default categories are "v.project", "experiment", "Phase", "integrated_snn_res.0.7".
#' @param SubFolderName String specifying the name of the subfolder where the plots will be saved.
#' By default, it's created using the function ppp("plot3D", substitute_deparse(ListOfCategories)).
#' @examples
#' \dontrun{
#' if (interactive()) {
Expand All @@ -4479,7 +4483,7 @@ Plot3D.ListOfCategories <- function(
obj = combined.obj # Plot and save list of 3D UMAP ot tSNE plots using plotly.
, annotate.by = "integrated_snn_res.0.7", cex = 1.25, default.assay = c("integrated", "RNA")[2],
ListOfCategories = c("v.project", "experiment", "Phase", "integrated_snn_res.0.7"),
SubFolderName = ppp("plot3D", substitute(ListOfCategories))) {
SubFolderName = ppp("plot3D", substitute_deparse(ListOfCategories))) {
try(create_set_SubDir(SubFolderName))
obj. <- obj
rm("obj")
Expand Down

0 comments on commit d7567e4

Please sign in to comment.