Question: subsetting out cells from seurat object based on expression of 1 gene
0
gravatar for cook.675
15 months ago by
cook.67560
cook.67560 wrote:

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 • 4.1k views
ADD COMMENTlink modified 14 months ago by jared.andrews078.4k • written 15 months ago by cook.67560

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 REPLYlink modified 15 months ago • written 15 months ago by cook.67560

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 REPLYlink written 14 months ago by cook.67560
2
gravatar for jared.andrews07
14 months ago by
jared.andrews078.4k
Memphis, TN
jared.andrews078.4k wrote:

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 COMMENTlink written 14 months ago by jared.andrews078.4k

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 REPLYlink written 14 months ago by cook.67560

What is gene.list? Just a vector?

ADD REPLYlink written 14 months ago by jared.andrews078.4k

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 REPLYlink modified 14 months ago • written 14 months ago by cook.67560

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 REPLYlink modified 4 weeks ago • written 4 weeks ago by angelo.pelonero0
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 2339 users visited in the last hour
_