Skip to content

Commit

Permalink
nf bf create.metadata.vector etc
Browse files Browse the repository at this point in the history
  • Loading branch information
vertesy committed Sep 20, 2024
1 parent 18337c6 commit 099c63c
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 85 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
64 changes: 24 additions & 40 deletions R/Seurat.Utils.Metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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))) {
Expand All @@ -404,64 +404,48 @@ 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)
}



# _________________________________________________________________________________________________
#' @title Create a Metadata Vector
#'
#' @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)
}
Expand Down
31 changes: 31 additions & 0 deletions R/Seurat.utils.less.used.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
#' [email protected][, 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)
Expand Down
23 changes: 17 additions & 6 deletions man/create.metadata.vector.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/plotMetadataMedianFractionBarplot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 0 additions & 37 deletions man/seu.add.meta.from.vector.Rd

This file was deleted.

0 comments on commit 099c63c

Please sign in to comment.