subsetting out cells from seurat object based on expression of 1 gene
1
1
Entering edit mode
5.2 years ago
cook.675 ▴ 230

I have a seurat object, with raw counts stored in the RNA assay at object@assays[["RNA"]]@counts

Lets say that the count matrix is simple and looks like this, where letters are genes and numbers are cells:

      [1] [2]  [3] 
[A]    2    4    3 
[B]    1    5    7 
[C]    2    3    4

I want to subset out all the cells that have expression of gene A >= 3. So that if I did that the resulting matrix would look like this:

      [2]  [3] 
[A]    4    3 
[B]    5    7 
[C]    3    4

Im having trouble getting the right syntax for this, specifically how to call only that one gene within a Subset function and still retain all the other genes in the list

seurat R • 23k views
ADD COMMENT
0
Entering edit mode

I tried subsetting out the count matrix like this:

data.frame = as.data.frame(Object[["RNA"]]@counts)

then running: Subset <- data.frame[, "A" >= 4]

and it returned all values

I also tried Subset <- data.frame[, data.frame$"A" >= 4]

ADD REPLY
0
Entering edit mode

I found a solution if anyone has interest. Take the data out of the object first as a data frame (df) then:

new_df <- df[, df["A",] >= 4]
ADD REPLY
3
Entering edit mode
5.1 years ago

For the future, the correct way to do this is:

subset(x = my.seurat.object, subset = my.gene >= 4)

As stated in the Seurat cheat sheet.

ADD COMMENT
0
Entering edit mode

Thanks thats much simpler and more elegant. I have a follow up if you wouldn't mind:

How would I subset a list of genes based on the same criteria? I tried:

subset(object, features = gene.list > 0)

And got an error here:

Error: Under current subsetting parameters, the default assay will be removed. Please adjust subsetting parameters or change default assay.
ADD REPLY
0
Entering edit mode

What is gene.list? Just a vector?

ADD REPLY
0
Entering edit mode

sorry for the late reply, yes a vector, I created it by entering the following:

gene.list <- c("GeneX", "GeneY", "GeneZ")

It works if I type out each gene as follows:

Object.subset <- subset(Object, GeneX > 0 & GeneY > 0 & GeneZ > 0)

Anytime I try to pass a gene name or a list to features it gives me that same error, and I cant seem to pass gene.list to subset either

ADD REPLY
0
Entering edit mode

Old post, but I came across it today since I'm trying to subset an object in a bunch of different ways. My "solution" isn't elegant or robust at all, but it'll do in a pinch:

### create list w/ genes of interest
genelist <- c("Gene1", "Gene2", "Gene3", "Gene4", "Gene5", "Gene6", "Gene7", "Gene8")

### init empty list
genelist_subset_query <- ""

### for loop to construct query
for(i in 1:length(genelist)){
   if(i != length(genelist)){
      genelist_subset_query <- paste(genelist_subset_query, genelist[i], "> 0 &", sep = " ") #### can change & to | (logical "or") if needed
   }
   if(i == length(genelist)){
      genelist_subset_query <- paste(genelist_subset_query, genelist[i], "> 0", sep = " ")
   }
}

### copy and paste this output into the subset command
genelist_subset_query

### subset w/ pasted output
subset_SeuratObj <- subset(SeuratObj, Gene1 > 0 & Gene2 > 0 & Gene3 > 0 & Gene4 > 0 & Gene5 > 0 & Gene6 > 0 & Gene7 > 0 & Gene8 > 0)

If you try to pass genelist_subset_query to the subset function it will fail since it is interpreted as one long character, hence the copy & paste step.

ADD REPLY
0
Entering edit mode

I don't know if it's normal bu when I filter my seurat object based on this way of doing it, I have the following results:

My data goes like this based on the quantiles:

`0%    25%    50%    75%   100%  : 0.0   10.0   61.5  300.0 4029.0`  So the maximum is 4029 and the minimum is 0.

I can't use counts_svep1 <- subset(x = scrna_m, my_gene > 60) because the following error appears Error in CellsByIdentities(object = object, cells = cells) : Cannot find cells provided.

But if I do this counts_svep1 <- subset(x = scrna_m, my_gene > 2)

It cuts the data like this:

`0%  25%  50%  75% 100% : 529  860 1087 1733 4029` 

How does this work? What is the logic behind it?

ADD REPLY
1
Entering edit mode

Open another question with your full example. Piggybacking on questions like this makes it hard for others to find your question (and any answers to it).

ADD REPLY
0
Entering edit mode

Okay, I will do that :)

ADD REPLY

Login before adding your answer.

Traffic: 1665 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6