This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Remove cells from Seurat based on a list

Hi guys, is there a way to remove from Seurat3 object (all slots) a set of cells listed in a vector?

I tried:

my_object_without_cells = subset(object = my_dataset, cells = cells_to_be_removed)

but the cells are still there. I checked the n of cells and the n of genes and it is unchanged.

Thank you in advance

scrna-seq seurat

So, basically subsetting on colnames(obj) removes the cells from all slots of the SeuratObject? Meaning @data,@sct @meta.data etc?

Thank you very much

2 answers

## Example data:
pbmc_raw <- read.table(
  file = system.file('extdata', 'pbmc_raw.txt', package = 'Seurat'),
  as.is = TRUE
)
pbmc_small <- CreateSeuratObject(counts = pbmc_raw)

length(colnames(pbmc_small)) ## 80

## list the cells (=colnames) that you want to remove:
toRemove <- c("ATGCCAGAACGACT", "CATGGCCTGTGCAT")

## filter them out:
pbmc_filtered <- pbmc_small[,!colnames(pbmc_small) %in% toRemove]

length(colnames(pbmc_filtered)) ## 78

Thank you very much! It works!

Thank you!!

Looks to me like you should be providing the function with the reverse -- the cells you want to keep.

cells = colnames(my_dataset)[!(colnames(my_dataset) %in% cells_to_be_removed)]

should do the trick.

Log in to answer this question.