diff --git a/R/Seurat.Utils.Metadata.R b/R/Seurat.Utils.Metadata.R index 1955432..ffb6768 100644 --- a/R/Seurat.Utils.Metadata.R +++ b/R/Seurat.Utils.Metadata.R @@ -25,6 +25,8 @@ #' @param obj A Seurat object to be updated. Default: combined.obj. #' @param orig.ident A character string specifying the original metadata to be translated. Default: "RNA_snn_res.0.4". #' @param translation_as_named_vec A named vector where names are old values and values are new translations. Default: None. +#' @param new_col_name A character string specifying the name of the new metadata column. Default: "translation_as_named_vec". +# #' @param NA.as.character A logical indicating whether to convert NAs to character. Default: TRUE. #' @param suffix A character string specifying the suffix for the new metadata column name. Default: ".". #' @param plot A logical indicating whether to plot the UMAP for the new metadata. Default: FALSE. #' @@ -37,6 +39,7 @@ addTranslatedMetadata <- function(obj = combined.obj, orig.ident = "RNA_snn_res.0.4", translation_as_named_vec, new_col_name = substitute_deparse(translation_as_named_vec), + # NA.as.character = T, suffix = NULL, plot = F, ...) { @@ -49,11 +52,13 @@ addTranslatedMetadata <- function(obj = combined.obj, message("new_col_name: ", new_col_name) # Translate metadata - obj@meta.data[[new_col_name]] <- CodeAndRoll2::translate( + obj@meta.data[[new_col_name]] <- new <- CodeAndRoll2::translate( vec = as.character(obj@meta.data[[orig.ident]]), old = names(translation_as_named_vec), new = translation_as_named_vec ) + print(table(new, useNA = "ifany")) + if(plot) clUMAP(ident = new_col_name, obj = obj, caption = "New metadata column", ...) return(obj) @@ -213,7 +218,7 @@ calculateAverageMetaData <- function( length(unique(obj@meta.data[, ident])) < max.categ ) - # browser() + # Initialize list to store results results <- list() @@ -345,7 +350,7 @@ getMedianMetric.lsObj <- function(ls.obj = ls.Seurat, n.datasets = length(ls.Seu getCellIDs.from.meta <- function(ident = GetClusteringRuns()[1], ident_values = NA, obj = combined.obj, inverse = FALSE) { - # browser() + mdat <- obj@meta.data[, ident] cells.pass <- mdat %in% ident_values if (inverse) cells.pass <- !cells.pass @@ -377,7 +382,7 @@ getCellIDs.from.meta <- function(ident = GetClusteringRuns()[1], addMetaDataSafe <- function(obj, metadata, col.name, overwrite = FALSE, verbose = FALSE, strict = TRUE) { if (verbose) message("Running addMetaDataSafe...") - # browser() + stopifnot( is(obj, "Seurat"), is.vector(metadata), is.character(col.name), is.logical(overwrite), "Column already exists" = ((!col.name %in% colnames(obj@meta.data)) | overwrite) @@ -525,7 +530,7 @@ addMetaFraction <- function( message("Assay: ", assay) message("Layer: ", layer) - # browser() + stopif(condition = isFALSE(gene.set) && isFALSE(gene.symbol.pattern), "Either gene.set OR gene.symbol.pattern has to be defined (!= FALSE).") if (!isFALSE(gene.set) && !isFALSE(gene.symbol.pattern) && verbose) print("Both gene.set AND gene.symbol.pattern are defined. Only using gene.set.") @@ -1722,6 +1727,10 @@ transferLabelsSeurat <- function( #' @param new_ident_name A string. The name for the newly created identity column in `obj@meta.data`. #' Default is a concatenation: kpp(prefix, ident_to_rename, "match.to", reference_ident) . #' @param plot_suffix A string. The suffix to add to the final UMAP. +#' @param barplot_match Draw a barplot to show the % match between the original and new identities. +#' Default: TRUE +#' @param barplot_fractions Draw a barplot to show the % of each category in the original and new +#' identities, using `scBarplot.CellFractions()`. Default: TRUE #' @param h Height for the saved image. Default: 12 #' @param w Width for the saved image. Default: 9 #' @param ... Additional parameters to be passed to `.replace_by_most_frequent_categories` function. @@ -1740,12 +1749,14 @@ transferLabelsSeurat <- function( matchBestIdentity <- function( obj, ident_to_rename, reference_ident = GetOrderedClusteringRuns(obj)[1], - prefix = Reference, + prefix = reference_ident, suffix = gsub(prefix, "", x = reference_ident), # to_suffix = "matched", # to_suffix = FixPlotName(gsub(pattern = "[a-zA-Z_]", replacement = "", x = ident_to_rename)), new_ident_name = kpp(prefix, ident_to_rename, "match.to", suffix), plot_suffix = prefix, + barplot_match = TRUE, + barplot_fractions = TRUE, w = 12, h = 9, ...) { stopifnot("colname prefix undefined" = !is.null(prefix)) @@ -1753,7 +1764,7 @@ matchBestIdentity <- function( dictionary <- obj@meta.data[, c(ident_to_rename, reference_ident)] translation <- .replace_by_most_frequent_categories( - df = dictionary, show_plot = TRUE, suffix_barplot = ident_to_rename, ... + df = dictionary, show_plot = barplot_match, suffix_barplot = ident_to_rename, ... ) obj@meta.data[, new_ident_name] <- translation[, 1] @@ -1761,6 +1772,11 @@ matchBestIdentity <- function( imessage("new ident name:", new_ident_name) px <- clUMAP(ident = new_ident_name, obj = obj, suffix = plot_suffix, w = w, h = h, ...) print(px) + + if (barplot_fractions) { + scBarplot.CellFractions(fill.by = reference_ident , group.by = new_ident_name, obj = obj) + } + return(obj) } diff --git a/R/Seurat.Utils.R b/R/Seurat.Utils.R index 3e96509..0394865 100644 --- a/R/Seurat.Utils.R +++ b/R/Seurat.Utils.R @@ -363,7 +363,7 @@ runDGEA <- function(obj, message("Resolutions analyzed:") df.markers.all <- Idents.for.DEG <- list.fromNames(x = res.analyzed.DE) - # browser() + if (clean.misc.slot) { message("Erasing up the misc slot: df.markers and top.markers.resX") topMslots <- grepv("top.markers.res", names(obj@misc)) @@ -4093,7 +4093,7 @@ RenameGenesSeurat <- function(obj = ls.Seurat[[i]], nr1 <- nrow(matrix_n) if (all(dim(matrix_n)) > 0) { - # browser() + stopifnot(nrow(matrix_n) == length(newnames)) if ("dgCMatrix" %in% class(matrix_n)) { diff --git a/R/Seurat.Utils.Visualization.R b/R/Seurat.Utils.Visualization.R index eb4c071..66c0401 100644 --- a/R/Seurat.Utils.Visualization.R +++ b/R/Seurat.Utils.Visualization.R @@ -2237,7 +2237,7 @@ clUMAP <- function( nr.cols = NULL, plotname = ppp(toupper(reduction), ident), cols = NULL, - palette = c("alphabet", "alphabet2", "glasbey", "polychrome", "stepped")[3], + palette = c("alphabet", "alphabet2", "glasbey", "polychrome", "stepped")[4], max.cols.for.std.palette = 7, highlight.clusters = NULL, cells.highlight = NULL, cols.highlight = "red", diff --git a/man/addTranslatedMetadata.Rd b/man/addTranslatedMetadata.Rd index 7531f9e..89eac06 100644 --- a/man/addTranslatedMetadata.Rd +++ b/man/addTranslatedMetadata.Rd @@ -21,6 +21,8 @@ addTranslatedMetadata( \item{translation_as_named_vec}{A named vector where names are old values and values are new translations. Default: None.} +\item{new_col_name}{A character string specifying the name of the new metadata column. Default: "translation_as_named_vec".} + \item{suffix}{A character string specifying the suffix for the new metadata column name. Default: ".".} \item{plot}{A logical indicating whether to plot the UMAP for the new metadata. Default: FALSE.} diff --git a/man/clUMAP.Rd b/man/clUMAP.Rd index e3f1b34..5d832d6 100644 --- a/man/clUMAP.Rd +++ b/man/clUMAP.Rd @@ -20,7 +20,7 @@ clUMAP( nr.cols = NULL, plotname = ppp(toupper(reduction), ident), cols = NULL, - palette = c("alphabet", "alphabet2", "glasbey", "polychrome", "stepped")[3], + palette = c("alphabet", "alphabet2", "glasbey", "polychrome", "stepped")[4], max.cols.for.std.palette = 7, highlight.clusters = NULL, cells.highlight = NULL, diff --git a/man/filterExpressedGenes.Rd b/man/filterExpressedGenes.Rd index 24d8368..f33c153 100644 --- a/man/filterExpressedGenes.Rd +++ b/man/filterExpressedGenes.Rd @@ -4,7 +4,7 @@ \alias{filterExpressedGenes} \title{Filter and Sort Gene Expression List Based on Specified Genes and Expression Threshold} \usage{ -filterExpressedGenes(genes = NULL, gene_list = all.genes, threshold = 0) +filterExpressedGenes(genes = NULL, gene_list = all.genes, threshold = 0.1) } \arguments{ \item{genes}{Character vector of gene symbols to search for in the gene list. Default: NULL.} @@ -13,7 +13,7 @@ filterExpressedGenes(genes = NULL, gene_list = all.genes, threshold = 0) expression levels. Default: all.genes} \item{threshold}{Numeric value specifying the minimum expression level for filtering. Genes with -expression values below this threshold will be excluded. Default: 0.} +expression values below this threshold will be excluded. Default: 0.1.} } \value{ A character vector of gene symbols that match the specified list, meet the expression threshold, @@ -29,6 +29,6 @@ by expression in descending order. # Example usage: gene_list <- list(ROBO2 = 0.9982406, CDH18 = 0.9981755, DCC = 0.9981755, AL589740.1 = 0.9981103) genes <- c("ROBO2", "DCC", "AL589740.1", "UNKNOWN") -filter_and_sort_genes(gene_list, genes, threshold = 0.9981) +filterExpressedGenes(gene_list, genes, threshold = 0.9981) } diff --git a/man/matchBestIdentity.Rd b/man/matchBestIdentity.Rd index 57094ee..b165007 100644 --- a/man/matchBestIdentity.Rd +++ b/man/matchBestIdentity.Rd @@ -8,10 +8,12 @@ matchBestIdentity( obj, ident_to_rename, reference_ident = GetOrderedClusteringRuns(obj)[1], - prefix = Reference, + prefix = reference_ident, suffix = gsub(prefix, "", x = reference_ident), new_ident_name = kpp(prefix, ident_to_rename, "match.to", suffix), plot_suffix = prefix, + barplot_match = TRUE, + barplot_fractions = TRUE, w = 12, h = 9, ... @@ -36,6 +38,12 @@ Default is a concatenation: kpp(prefix, ident_to_rename, "match.to", reference_i \item{plot_suffix}{A string. The suffix to add to the final UMAP.} +\item{barplot_match}{Draw a barplot to show the \% match between the original and new identities. +Default: TRUE} + +\item{barplot_fractions}{Draw a barplot to show the \% of each category in the original and new +identities, using \code{scBarplot.CellFractions()}. Default: TRUE} + \item{w}{Width for the saved image. Default: 9} \item{h}{Height for the saved image. Default: 12}