Search code examples
rsubsetseurat

Conditional subsetting of Seurat object


I have a Seurat object that I have run through doubletFinder. I am trying to subset the object based on cells being classified as a 'Singlet' under [email protected][["DF.classifications_0.25_0.03_252"]] and can achieve this by doing the following:

seurat_object <- subset(seurat_object, subset = DF.classifications_0.25_0.03_252 == 'Singlet') #this approach works

I would like to automate this process but the _0.25_0.03_252 of DF.classifications_0.25_0.03_252 is based on values that are calculated and will not be known in advance. I can figure out what it is by doing the following:

meta_data = colnames([email protected])[grepl("DF.classification", colnames([email protected]))]

Where meta_data = 'DF.classifications_0.25_0.03_252' and is a character class.

However, when I try to do any of the following:

seurat_object <- subset(seurat_object, subset = meta_data == 'Singlet') #this approach does not recognize meta_data
seurat_object <- subset(seurat_object, subset = [email protected]$meta_data == 'Singlet') #this approach does not work
seurat_object <- subset(seurat_object, subset = [email protected][[meta_data]] == 'Singlet') #this approach does not work

I get an error that states:

Error in FetchData(object = object, vars = unique(x = expr.char[vars.use]),  :   
  None of the requested variables were found: 
Calls: remove_doublets ... subset.Seurat -> WhichCells -> WhichCells.Seurat -> FetchData
Execution halted

I am at loss for how to perform conditional matching with the meta_data variable. Does anyone have an idea how I can automate the subset process? Many thanks in advance.


Solution

  • This worked.

    seurat_object = seurat_object[, [email protected][, meta_data] == "Singlet"]
    

    But I especially don't get why this one did not work:

    seurat_object <- subset(seurat_object, subset = [email protected][[meta_data]] == 'Singlet') 
    

    If anyone can tell me why the latter did not function I would appreciate it.