Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
vertesy committed Jan 31, 2024
1 parent 884b4d6 commit 0d312e5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
27 changes: 13 additions & 14 deletions R/Seurat.Utils.Metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -555,14 +555,15 @@ set.all.genes <- function(obj = combined.obj) iprint("Use calc.q99.Expression.an
# Combine metadata ______________________________ ----
# _________________________________________________________________________________________________

#' @title Combine Metadata and Write to TSV

#' @title Combine Metadata from a list of Seurat objects and Write to TSV
#'
#' @description
#' `writeMetadataToTsv` takes a list of objects, extracts their `@meta.data` slots,
#' `writeMetadataToTsv` takes a list of ls.Obj, extracts their `@meta.data` slots,
#' removes specified columns, checks for column consistency, creates a barplot showing the number
#' of rows per object, and finally merges these into one large data frame.
#'
#' @param objects A list of objects, each containing a `@meta.data` slot.
#' @param ls.Obj A list of objects, each containing a `@meta.data` slot.
#' @param cols.remove A character vector of column names to be removed from each metadata data frame.
#' Default is an empty character vector, meaning no columns will be removed.
#'
Expand All @@ -583,30 +584,28 @@ set.all.genes <- function(obj = combined.obj) iprint("Use calc.q99.Expression.an
#' The function currently contains a `browser()` call for debugging purposes, which should be removed in production.
#'
#' @export

writeMetadataToTsv <- function(objects, cols.remove = character(), write_out = TRUE, ...) {
writeCombinedMetadataToTsvFromLsObj <- function(ls.Obj, cols.remove = character(), write_out = TRUE, ...) {
warning("writeMetadataToTsv is EXPERIMENTAL. It writes out subset of columns", immediate. = TRUE)
stopifnot(is.list(objects)) # Validate that input is a list
stopifnot(is.list(ls.Obj)) # Validate that input is a list

# Extract metadata from each object and remove specified columns
metadataList <- lapply(objects, function(obj) {
metadataList <- lapply(ls.Obj, function(obj) {
stopifnot("meta.data" %in% slotNames(obj)) # Check for meta.data slot
metaData <- obj@meta.data
metaData[, !(names(metaData) %in% cols.remove)]
})


# Find common columns and subset
commonCols <- CodeAndRoll2::intersect.ls(lapply(metadataList, names))
metadataList <- lapply(metadataList, function(df) df[, commonCols, drop = FALSE])

# Check if qbarplot is available and create a barplot showing the number of rows per object

# Check if qbarplot is available and create a barplot showing the number of rows per object
metadata.cells.per.obj <- sapply(metadataList, nrow)
print(metadata.cells.per.obj)
pobj <- ggExpress::qbarplot(metadata.cells.per.obj,
label = metadata.cells.per.obj, ylab = "cells",
save = FALSE
label = metadata.cells.per.obj, ylab = "cells",
save = FALSE
)
print(pobj)

Expand All @@ -616,10 +615,10 @@ writeMetadataToTsv <- function(objects, cols.remove = character(), write_out = T
# Print dimensions of the merged data frame
print(dim(mergedMetaData))

# Return the merged data frame
return(mergedMetaData)

if (write_out) ReadWriter::write.simple.tsv(mergedMetaData, ...)

# Return the merged data frame
invisible(mergedMetaData)
}


Expand Down
2 changes: 1 addition & 1 deletion R/Seurat.Utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -3573,7 +3573,7 @@ isave.RDS <- function(
xsave <- function(
obj, prefix = NULL,
suffix = NULL,
nthreads = 12,
nthreads = if(exists('CBE.params')) CBE.params$'cpus' else 12,
preset = "high",
project = getProject(),
out_dir = if (exists("OutDir")) OutDir else getwd(),
Expand Down

0 comments on commit 0d312e5

Please sign in to comment.