From 37a30b845497a7903963303fb9f37a3d4754a090 Mon Sep 17 00:00:00 2001 From: micahpf Date: Wed, 2 Aug 2023 15:39:31 -0400 Subject: [PATCH 1/2] init convertCLID2Name() to convert cl ids in SingleR output and seurat objects into human-readable names --- NAMESPACE | 1 + R/runSingleR.R | 56 ++++++++++++++++++++++++++++++++++++ man/convertClID2Name.Rd | 64 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 man/convertClID2Name.Rd diff --git a/NAMESPACE b/NAMESPACE index 6c9b294..7033494 100755 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,6 +6,7 @@ export(IdentName) export(NormFindVarFeatScaleData) export(addQCfilter) export(addQCmetrics) +export(convertClID2Name) export(createAzimuthReference) export(demuxAntibodyData) export(extractGeneCorrectionsSoupX) diff --git a/R/runSingleR.R b/R/runSingleR.R index 144bd52..8d95895 100644 --- a/R/runSingleR.R +++ b/R/runSingleR.R @@ -61,3 +61,59 @@ runSingleR <- function(obj.seurat, return(list(obj.seurat, singleR.out)) } + +#' Convert cell ontology IDs to names in SingleR output +#' +#' Convert bare cell ontology IDs used in SingleR to human-readable names +#' +#' @param metadata data.frame with labels, pruned.labels and delta.next columns. Typically either: +#' - Seurat object metadata (after running SingleR), or +#' - SingleR output object +#' @param cl Cell ontology database +#' - Can be btained via: `cl <- ontoProc::getOnto('cellOnto')` +#' @param old.prefix The prefix used for the original runSingleR call (e.g. "ImmGen.ont") +#' @param new.prefix The new prefix to use for the human readable column (e.g. "ImmGen.ont.name") +#' +#' @returns data.frame with three new columns: `{new.prefix}.labels`, `{new.prefix}.pruned.labels`, `{new.prefix}.delta.next`. +#' +#'@examples +#' \dontrun{ +#' ## Run on bare ontology IDs, then convert to human readable cell type labels +#' # Get cell ontology names and other info +#' cl <- ontoProc::getOnto('cellOnto') +#' +#' # ImmGen.ont +#' c(obj.seurat, SingleR.ImmGen.ont) %<-% obj.seurat %>% +#' Run.SingleR(obj.seurat=., +#' ref=ref.ImmGen, +#' labels=ref.ImmGen$label.ont, +#' de.method="classic", +#' meta.prefix = "ImmGen.ont") +#' +#' # These labels aren't very informative: +#' SingleR::plotScoreHeatmap(SingleR.ImmGen.ont, show.pruned = TRUE, fontsize=4) +#' +#' # Convert bare cell ontology IDs to informative names in seurat object +#' obj.seurat@meta.data <- clID2clNames(metadata = obj.seurat@meta.data, cl = cl, +#' old.prefix = "ImmGen.ont", new.prefix = "ImmGen.ont.name") +#' +#' # Convert bare cell ontology IDs to informative names in SingleR output and visualize +#' SingleR.ImmGen.ont.names <- clID2clNames(metadata = SingleR.ImmGen.ont, cl = cl, +#' old.prefix = "ImmGen.ont", new.prefix = "ImmGen.ont.name") +#' +#' SingleR::plotScoreHeatmap(SingleR.ImmGen.ont.names, show.pruned = TRUE, fontsize=4) +#' } +#' +#' @export +# Run SingleR +# Convert bare cell ontology IDs to informative names +convertClID2Name <- function(metadata, cl, old.prefix = "ImmGen.ont", new.prefix = "ImmGen.ont.name") { + op <- old.prefix + np <- new.prefix + md <- metadata + md[[paste0(np, ".labels")]] <- unname(cl$name[unname(md[[paste0(op, ".labels")]])]) + md[[paste0(np, ".pruned.labels")]] <- unname(cl$name[unname(md[[paste0(op, ".pruned.labels")]])]) + md[[paste0(np, ".delta.next")]] <- md[[paste0(op, ".delta.next")]] + metadata <- md + return(metadata) +} diff --git a/man/convertClID2Name.Rd b/man/convertClID2Name.Rd new file mode 100644 index 0000000..7981e84 --- /dev/null +++ b/man/convertClID2Name.Rd @@ -0,0 +1,64 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/runSingleR.R +\name{convertClID2Name} +\alias{convertClID2Name} +\title{Convert cell ontology IDs to names in SingleR output} +\usage{ +convertClID2Name( + metadata, + cl, + old.prefix = "ImmGen.ont", + new.prefix = "ImmGen.ont.name" +) +} +\arguments{ +\item{metadata}{data.frame with labels, pruned.labels and delta.next columns. Typically either: +\itemize{ +\item Seurat object metadata (after running SingleR), or +\item SingleR output object +}} + +\item{cl}{Cell ontology database +\itemize{ +\item Can be btained via: \code{cl <- ontoProc::getOnto('cellOnto')} +}} + +\item{old.prefix}{The prefix used for the original runSingleR call (e.g. "ImmGen.ont")} + +\item{new.prefix}{The new prefix to use for the human readable column (e.g. "ImmGen.ont.name")} +} +\value{ +data.frame with three new columns: \verb{\{new.prefix\}.labels}, \verb{\{new.prefix\}.pruned.labels}, \verb{\{new.prefix\}.delta.next}. +} +\description{ +Convert bare cell ontology IDs used in SingleR to human-readable names +} +\examples{ +\dontrun{ +## Run on bare ontology IDs, then convert to human readable cell type labels +# Get cell ontology names and other info +cl <- ontoProc::getOnto('cellOnto') + +# ImmGen.ont +c(obj.seurat, SingleR.ImmGen.ont) \%<-\% obj.seurat \%>\% + Run.SingleR(obj.seurat=., + ref=ref.ImmGen, + labels=ref.ImmGen$label.ont, + de.method="classic", + meta.prefix = "ImmGen.ont") + +# These labels aren't very informative: +SingleR::plotScoreHeatmap(SingleR.ImmGen.ont, show.pruned = TRUE, fontsize=4) + +# Convert bare cell ontology IDs to informative names in seurat object +obj.seurat@meta.data <- clID2clNames(metadata = obj.seurat@meta.data, cl = cl, + old.prefix = "ImmGen.ont", new.prefix = "ImmGen.ont.name") + +# Convert bare cell ontology IDs to informative names in SingleR output and visualize +SingleR.ImmGen.ont.names <- clID2clNames(metadata = SingleR.ImmGen.ont, cl = cl, + old.prefix = "ImmGen.ont", new.prefix = "ImmGen.ont.name") + +SingleR::plotScoreHeatmap(SingleR.ImmGen.ont.names, show.pruned = TRUE, fontsize=4) +} + +} From 0b3612f2c0a7c10adc61ece56a29fbb11db5161a Mon Sep 17 00:00:00 2001 From: Derrik Gratz <46037149+derrik-gratz@users.noreply.github.com> Date: Wed, 9 Aug 2023 13:44:54 -0400 Subject: [PATCH 2/2] Update runSingleR.R fix example function calls to proper function name --- R/runSingleR.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/runSingleR.R b/R/runSingleR.R index 8d95895..7776f33 100644 --- a/R/runSingleR.R +++ b/R/runSingleR.R @@ -94,11 +94,11 @@ runSingleR <- function(obj.seurat, #' SingleR::plotScoreHeatmap(SingleR.ImmGen.ont, show.pruned = TRUE, fontsize=4) #' #' # Convert bare cell ontology IDs to informative names in seurat object -#' obj.seurat@meta.data <- clID2clNames(metadata = obj.seurat@meta.data, cl = cl, +#' obj.seurat@meta.data <- convertClID2Name(metadata = obj.seurat@meta.data, cl = cl, #' old.prefix = "ImmGen.ont", new.prefix = "ImmGen.ont.name") #' #' # Convert bare cell ontology IDs to informative names in SingleR output and visualize -#' SingleR.ImmGen.ont.names <- clID2clNames(metadata = SingleR.ImmGen.ont, cl = cl, +#' SingleR.ImmGen.ont.names <- convertClID2Name(metadata = SingleR.ImmGen.ont, cl = cl, #' old.prefix = "ImmGen.ont", new.prefix = "ImmGen.ont.name") #' #' SingleR::plotScoreHeatmap(SingleR.ImmGen.ont.names, show.pruned = TRUE, fontsize=4)