Skip to content

Commit

Permalink
update to handle group.by containing missing meta.data and update num…
Browse files Browse the repository at this point in the history
…eric conversion warning
  • Loading branch information
mhkowalski committed Nov 13, 2023
1 parent c17879f commit 8e3a22d
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -1425,6 +1425,8 @@ PseudobulkExpression.Seurat <- function(
stop("Number of layers provided does not match number of assays")
}
data <- FetchData(object = object, vars = rev(x = group.by))
#only keep meta-data columns that are in object
group.by <- intersect(group.by, colnames(data))
data <- data[which(rowSums(x = is.na(x = data)) == 0), , drop = F]
if (nrow(x = data) < ncol(x = object)) {
inform("Removing cells with NA for 1 or more grouping variables")
Expand Down Expand Up @@ -1460,7 +1462,9 @@ PseudobulkExpression.Seurat <- function(
)
colnames(category.matrix) <- col.names
inform(
message = "Pseudobulk group.by variables are numeric, appending `g` to column names/cells of pseudobulked output.",
message = paste0("group.by variable `", group.by[1],
"` starts with a number, appending `g` to `", group.by[1],
"` variable to ensure\nvalid variable names"),
.frequency = "regularly",
.frequency_id = "PseudobulkExpression"
)
Expand Down Expand Up @@ -1600,7 +1604,24 @@ PseudobulkExpression.Seurat <- function(
}
#set idents to pseudobulk variables
Idents(toRet) <- cells
toRet$orig.ident <- cells

#make orig.ident variable
#orig.ident = ident if group.by includes `ident`
#if not, orig.ident is equal to pseudobulk cell names
if(any(group.by == "ident")) {
i = which(group.by == "ident")
v <- sapply(
strsplit(cells, "_"),
function(x) {return(x[i])}
)
names(v) <- cells
toRet <- AddMetaData(toRet,
metadata = v,
col.name = "orig.ident"
)
} else {
toRet$orig.ident <- cells
}
return(toRet)
} else {
return(data.return)
Expand Down

0 comments on commit 8e3a22d

Please sign in to comment.