Skip to content

Commit

Permalink
nf addMetaFraction
Browse files Browse the repository at this point in the history
  • Loading branch information
vertesy committed Jun 20, 2024
1 parent 1632904 commit 071b14d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
8 changes: 6 additions & 2 deletions R/Seurat.Utils.Metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,8 @@ create.metadata.vector <- function(vec = All.UVI, obj = combined.obj, min.inters
#' @param obj Seurat object to which the new metadata column will be added. Default: ls.Seurat[[1]]
#' @param col.name Name of the new metadata column to be added. Default: 'percent.mito'
#' @param gene.symbol.pattern Regular expression pattern to match gene symbols. Default: c("^MT\\.|^MT-", FALSE)[1]
#' @param assay Name of the assay to be used. Default: 'RNA'
#' @param layer Name of the layer to be used. Default: 'data'
#' @param gene.set A set of gene symbols. If specified, it will be used instead of gene.symbol.pattern. Default: FALSE
#' @param verbose Logical indicating whether to display detailed messages (TRUE) or not (FALSE). Default: TRUE
#' @examples
Expand Down Expand Up @@ -512,10 +514,12 @@ addMetaFraction <- function(
col.name = "percent.mito",
gene.symbol.pattern = c("^MT\\.|^MT-", FALSE)[1],
assay = "RNA",
layer = "data",
gene.set = FALSE,
verbose = TRUE) {
message("Should rather use the default `Seurat::PercentageFeatureSet`")
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).")
Expand All @@ -531,10 +535,10 @@ addMetaFraction <- function(
CodeAndRoll2::grepv(pattern = gene.symbol.pattern, x = all.genes)
}

genes.expr <- GetAssayData(object = obj, assay = assay)[genes.matching, ]
genes.expr <- GetAssayData(object = obj, assay = assay, layer = layer)[genes.matching, ]
target_expr <- if (length(genes.matching) > 1) Matrix::colSums(genes.expr) else genes.expr

iprint(length(genes.matching), "genes found, :", head(genes.matching))
iprint(length(genes.matching), "genes found, eg:", sample(genes.matching, 10))

obj <- AddMetaData(object = obj, metadata = target_expr / total_expr, col.name = col.name)
colnames(obj@meta.data)
Expand Down
5 changes: 5 additions & 0 deletions man/addMetaFraction.Rd

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

0 comments on commit 071b14d

Please sign in to comment.