diff --git a/NAMESPACE b/NAMESPACE index b4b0b60..69c4729 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -172,7 +172,6 @@ export(set.mm) export(seu.Make.Cl.Label.per.cell) export(seu.RemoveMetadata) export(seu.add.meta.from.table) -export(seu.add.meta.from.vector) export(seu.map.and.add.new.ident.to.meta) export(shorten_clustering_names) export(showMiscSlots) diff --git a/R/Seurat.Utils.Metadata.R b/R/Seurat.Utils.Metadata.R index 360105c..3d825ab 100644 --- a/R/Seurat.Utils.Metadata.R +++ b/R/Seurat.Utils.Metadata.R @@ -2,6 +2,8 @@ # Seurat.Utils.Metadata ---- # ____________________________________________________________________ # source("~/GitHub/Packages/Seurat.utils/R/Seurat.Utils.Metadata.R") +# source("~/GitHub/Packages/Seurat.utils/R/Seurat.utils.less.used.R") + # devtools::load_all(path = '~/GitHub/Packages/Seurat.utils'); # devtools::document("~/GitHub/Packages/Seurat.utils"); devtools::load_all("~/GitHub/Packages/Seurat.utils") # source("~/GitHub/Packages/Seurat.utils/R/Seurat.Utils.R") @@ -372,12 +374,10 @@ addMetaDataSafe <- function(obj, metadata, col.name, overwrite = FALSE) { message("Running addMetaDataSafe...") # browser() stopifnot( - is(obj, "Seurat"), - is.vector(metadata), - is.character(col.name), - is.logical(overwrite), + is(obj, "Seurat"), is.vector(metadata), is.character(col.name), is.logical(overwrite), "Column already exists" = ((!col.name %in% colnames(obj@meta.data)) | overwrite), - "Check length" = (length(metadata) == ncol(obj)) + "Metadata shorter than object" = (length(metadata) < ncol(obj)), + "Metadata longer than object" = (length(metadata) > ncol(obj)) ) if (!is.null(names(metadata))) { @@ -404,32 +404,6 @@ addMetaDataSafe <- function(obj, metadata, col.name, overwrite = FALSE) { } -# _________________________________________________________________________________________________ -#' @title seu.add.meta.from.vector -#' -#' @description Adds a new metadata column to a Seurat object. -#' @param obj A Seurat object to which the new metadata column will be added. Default: combined.obj. -#' @param metaD.colname A string specifying the name of the new metadata column. Default: metaD.colname.labeled. -#' @param Label.per.cell A vector of labels for each cell, to be added as new metadata. Default: Cl.Label.per.cell. -#' @return A Seurat object with the new metadata column added. -#' @examples -#' \dontrun{ -#' if (interactive()) { -#' # Example usage: -#' combined.obj <- seu.add.meta.from.vector( -#' obj = combined.obj, -#' metaD.colname = metaD.colname.labeled, -#' Label.per.cell = Cl.Label.per.cell -#' ) -#' } -#' } -#' @export -seu.add.meta.from.vector <- function(obj = combined.obj, metaD.colname = metaD.colname.labeled, Label.per.cell = Cl.Label.per.cell) { # Add a new metadata column to a Seurat object - obj@meta.data[, metaD.colname] <- Label.per.cell - iprint(metaD.colname, "contains the named identitites. Use Idents(combined.obj) = '...'. The names are:", unique(Label.per.cell)) - return(obj) -} - # _________________________________________________________________________________________________ @@ -437,31 +411,41 @@ seu.add.meta.from.vector <- function(obj = combined.obj, metaD.colname = metaD.c #' #' @description This function creates a metadata vector from an input vector and a Seurat object. #' The resulting vector contains values from 'vec' for the intersecting cell names between 'vec' and 'obj'. -#' It also checks if the intersection between the cell names in 'vec' and 'obj' is more than a minimum intersection size. -#' @param vec A named vector where the names represent cell IDs. This vector should have partial overlap with the cells in a Seurat object. Default is 'All.UVI'. -#' @param obj A Seurat object that contains cell IDs which partially overlap with 'vec'. Default is 'combined.obj'. -#' @param min.intersect The minimum number of cells to find in both 'vec' and 'obj'. The function will stop if the intersection is less than this number. Default is 100. -#' @return A named vector of length equal to the number of cells in 'obj', with names from 'obj' and values from 'vec' for intersecting cell names. +#' It also checks if the intersection between the cell names in 'vec' and 'obj' is more than a +#' minimum intersection size. +#' @param vec A named vector where the names represent cell IDs. This vector should have partial +#' overlap with the cells in a Seurat object. +#' @param obj A Seurat object that contains cell IDs which partially overlap with 'vec'. +#' @param fill The value to fill for non-intersecting cell names in 'obj'. Default is NA. +#' @param min.intersect The minimum number of cells to find in both 'vec' and 'obj'. +#' The function will stop if the intersection is less than this number. Default is 100. +#' @return A named vector of length equal to the number of cells in 'obj', with names from 'obj' and +#' values from 'vec' for intersecting cell names. #' @examples #' \dontrun{ #' create.metadata.vector(vec = my_vector, obj = my_seurat_object, min.intersect = 50) #' } #' @export -create.metadata.vector <- function(vec = All.UVI, obj = combined.obj, min.intersect = 100) { +create.metadata.vector <- function(vec, obj = combined.obj, fill = NA, + min.intersect = min(length(vec), ncol(obj), 100)) { + + stopifnot(is.vector(vec), is(obj, "Seurat"), + is.character(names(vec)), is.character(colnames(obj)), + length(intersect(names(vec), colnames(obj))) > min.intersect + ) + cells.vec <- names(vec) cells.obj <- colnames(obj) cells.in.both <- intersect(cells.vec, cells.obj) - # iprint("intersect:", length(cells.in.both), head(cells.in.both)) iprint( length(cells.in.both), "cells in both;", length(cells.vec), "cells in vec;", length(cells.obj), "cells in obj", "intersect, e.g.:", head(cells.in.both, 5) ) - stopifnot(length(cells.in.both) > min.intersect) - new_assignment <- vec.fromNames(cells.obj) + new_assignment <- CodeAndRoll2::vec.fromNames(cells.obj, fill = fill) new_assignment[cells.in.both] <- vec[cells.in.both] return(new_assignment) } diff --git a/R/Seurat.utils.less.used.R b/R/Seurat.utils.less.used.R index 72cfb08..62fe50d 100644 --- a/R/Seurat.utils.less.used.R +++ b/R/Seurat.utils.less.used.R @@ -563,6 +563,7 @@ multiFeatureHeatmap.A4 <- function(...) .Deprecated("No longer provided.") Annotate4Plotly3D <- function(...) .Deprecated(".Annotate4Plotly3D() - with dot/invisible.") Percent.in.Trome <- function(...) .Deprecated("PercentInTranscriptome()") .parseRegressionVariablesForScaleData <- function(...) .Deprecated(".getRegressionVariablesForScaleData()") +seu.add.meta.from.vector <- function(...) .Deprecated("addMetaDataSafe()") # _________________________________________________________________________________________________ # Main script / functions @@ -794,6 +795,36 @@ ww.calc_helper <- function(obj, genes, slot = "RNA") { } + +#' # _________________________________________________________________________________________________ +#' #' @title seu.add.meta.from.vector +#' #' +#' #' @description Adds a new metadata column to a Seurat object. +#' #' @param obj A Seurat object to which the new metadata column will be added. Default: combined.obj. +#' #' @param metaD.colname A string specifying the name of the new metadata column. Default: metaD.colname.labeled. +#' #' @param Label.per.cell A vector of labels for each cell, to be added as new metadata. Default: Cl.Label.per.cell. +#' #' @return A Seurat object with the new metadata column added. +#' #' @examples +#' #' \dontrun{ +#' #' if (interactive()) { +#' #' # Example usage: +#' #' combined.obj <- seu.add.meta.from.vector( +#' #' obj = combined.obj, +#' #' metaD.colname = metaD.colname.labeled, +#' #' Label.per.cell = Cl.Label.per.cell +#' #' ) +#' #' } +#' #' } +#' #' @export +#' seu.add.meta.from.vector <- function(obj = combined.obj, metaD.colname, Label.per.cell = Cl.Label.per.cell) { +#' .Deprecated("addMetaDataSafe") +#' obj@meta.data[, metaD.colname] <- Label.per.cell +#' iprint(metaD.colname, "contains the named identitites. Use Idents(combined.obj) = '...'. The names are:", unique(Label.per.cell)) +#' return(obj) +#' } +#' +#' + # # _________________________________________________________________________________________________ # sparse.cor4 <- function(x){ # n <- nrow(x) diff --git a/man/create.metadata.vector.Rd b/man/create.metadata.vector.Rd index d0b8bff..88a36ae 100644 --- a/man/create.metadata.vector.Rd +++ b/man/create.metadata.vector.Rd @@ -4,22 +4,33 @@ \alias{create.metadata.vector} \title{Create a Metadata Vector} \usage{ -create.metadata.vector(vec = All.UVI, obj = combined.obj, min.intersect = 100) +create.metadata.vector( + vec, + obj = combined.obj, + fill = NA, + min.intersect = min(length(vec), ncol(obj), 100) +) } \arguments{ -\item{vec}{A named vector where the names represent cell IDs. This vector should have partial overlap with the cells in a Seurat object. Default is 'All.UVI'.} +\item{vec}{A named vector where the names represent cell IDs. This vector should have partial +overlap with the cells in a Seurat object.} -\item{obj}{A Seurat object that contains cell IDs which partially overlap with 'vec'. Default is 'combined.obj'.} +\item{obj}{A Seurat object that contains cell IDs which partially overlap with 'vec'.} -\item{min.intersect}{The minimum number of cells to find in both 'vec' and 'obj'. The function will stop if the intersection is less than this number. Default is 100.} +\item{fill}{The value to fill for non-intersecting cell names in 'obj'. Default is NA.} + +\item{min.intersect}{The minimum number of cells to find in both 'vec' and 'obj'. +The function will stop if the intersection is less than this number. Default is 100.} } \value{ -A named vector of length equal to the number of cells in 'obj', with names from 'obj' and values from 'vec' for intersecting cell names. +A named vector of length equal to the number of cells in 'obj', with names from 'obj' and +values from 'vec' for intersecting cell names. } \description{ This function creates a metadata vector from an input vector and a Seurat object. The resulting vector contains values from 'vec' for the intersecting cell names between 'vec' and 'obj'. -It also checks if the intersection between the cell names in 'vec' and 'obj' is more than a minimum intersection size. +It also checks if the intersection between the cell names in 'vec' and 'obj' is more than a +minimum intersection size. } \examples{ \dontrun{ diff --git a/man/plotMetadataMedianFractionBarplot.Rd b/man/plotMetadataMedianFractionBarplot.Rd index 7853464..7a72c99 100644 --- a/man/plotMetadataMedianFractionBarplot.Rd +++ b/man/plotMetadataMedianFractionBarplot.Rd @@ -39,7 +39,7 @@ plotMetadataMedianFractionBarplot( \item{ylab}{Label for the y-axis. Default: "Fraction of transcriptome (\%)".} -\item{percentify}{Logical; if TRUE, multiplies the fraction by 100. Default: TRUE.} +\item{percentify}{Logical. If TRUE, multiplies the fraction by 100. Default: TRUE.} \item{subt}{Subtitle for the plot. Default: NULL.} diff --git a/man/seu.add.meta.from.vector.Rd b/man/seu.add.meta.from.vector.Rd deleted file mode 100644 index 31764ce..0000000 --- a/man/seu.add.meta.from.vector.Rd +++ /dev/null @@ -1,37 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/Seurat.Utils.Metadata.R -\name{seu.add.meta.from.vector} -\alias{seu.add.meta.from.vector} -\title{seu.add.meta.from.vector} -\usage{ -seu.add.meta.from.vector( - obj = combined.obj, - metaD.colname = metaD.colname.labeled, - Label.per.cell = Cl.Label.per.cell -) -} -\arguments{ -\item{obj}{A Seurat object to which the new metadata column will be added. Default: combined.obj.} - -\item{metaD.colname}{A string specifying the name of the new metadata column. Default: metaD.colname.labeled.} - -\item{Label.per.cell}{A vector of labels for each cell, to be added as new metadata. Default: Cl.Label.per.cell.} -} -\value{ -A Seurat object with the new metadata column added. -} -\description{ -Adds a new metadata column to a Seurat object. -} -\examples{ -\dontrun{ -if (interactive()) { - # Example usage: - combined.obj <- seu.add.meta.from.vector( - obj = combined.obj, - metaD.colname = metaD.colname.labeled, - Label.per.cell = Cl.Label.per.cell - ) -} -} -}