This is a test version of Biostars. For the public version, visit https://www.biostars.org.
active.ident slot present but not recognized in seurat object

Hi. I am totally new to R and Seurat so please forgive me if this is a stupid question.

I have four samples (two mouse strains, treated and control). I created four individual Seuratobjects using the standard pipeline Read10X CreateSeuratObject Done QC (mito content ++)-->then filtered the cells

Then I run this on each object

SCTransform
RunPCA(npcs = 30, verbose = FALSE) %>%
RunUMAP(reduction = "pca", dims = 1:30, verbose = FALSE) %>%
FindNeighbors(reduction = "pca", dims = 1:30, verbose = FALSE) %>%
FindClusters(resolution = 0.7, verbose = FALSE)

before merging, and then again running:

merge.sct<-merge(x = object1, y = c(object2, object3, object4), add.cell.ids = c("object1", "object2", "object3", "object4"), merge.data = TRUE)
VariableFeatures(merge.sct[["SCT"]]) <- rownames(merge.sct[["SCT"]]@scale.data)
merge.sct <- PrepSCTFindMarkers((merge.sct), assay = "SCT", verbose = TRUE)
all.genes <- rownames(merge.sct)
merge.sct <- RunPCA(merge.sct, features = VariableFeatures(object = merge.sct))
merge.sct <- FindNeighbors(merge.sct, dims = 1:40)
merge.sct <- FindClusters(merge.sct, resolution = 0.5)
merge.sct <- RunUMAP(merge.sct, dims = 1:40)

In the merged object, the STRAINcontrol/STRAINtreated is stored in the orig.ident slot. I have also created individual slots called Strain and Treatment.

So to my question: All this worked nicely, I got nice clusters, and was able to use FindAllMarkers to determine cell types. However, when I wanted to look at single clusters and compare DEGs between conditions, something goes wrong.

I wanted to find DEGs in a given cluster in treated vs control:

`merged_markers<-FindMarkers(merge.sct, ident.1 = "3", group.by = "Treatment", test.use = "wilcox")`

then I get the error that it can't find the ident.1. I have tried typing "3", 3, just 3, I have tried renaming the clusters with cell type, nothing works.. Here's the error:

Error in WhichCells.Seurat(object = object, idents = ident.1) : Cannot find the following identities in the object: 3

I have trouble with accessing the active.ident slot in the merged object as well..or I have no trouble changing it using

`Idents(object=merge.sct) <- "orig.ident"`

or back to clusters

Idents(object=merge.sct) <- "seurat_clusters"

When I do this I can see that it changes in the object (view(merge.sct)) but when I type

merge.sct$active.ident

I get this error.

Error: Cannot find 'active.ident' in this Seurat object

I get the same error when I try to visualize the active.ident in the four initial seurat objects that I used to create the merged.sct, so my concern is that something has gone wrong when creating these initial objects.

Additional info: I have Seurat version 4.3.0. I have tried to restart R, re-install Seurat several times.

Let me know if I need to provide more information Thank you so much! E

seurat idents

2 answers

merged_markers<-FindMarkers(merge.sct, ident.1 = "3", group.by = "Treatment", test.use = "wilcox")

Your parameters are not telling FindMarkers to do what you say you are trying to do. From FindMakers description:

group.by Regroup cells into a different identity class prior to performing differential expression (see example)

So by setting group.by = "Treatment" and ident.1 = "3" you are telling FindMarkers to take all cells from Treatment == 3 and compare to all other cells.

Instead, if you want to compare treated versus untreated cells only within cluster 3, try the following

merged_markers<-FindMarkers(merge.sct, ident.1 = "treated", group.by = "Treatment", subset.ident = "3", test.use = "wilcox")

Where subset.ident = first subsets the data to look at cluster 3 cells.

Also see the example at https://satijalab.org/seurat/reference/findmarkers

To loop through multiple clusters, one option would be:

my_clusters <- unique(merge.sct$seurat_clusters)
cluster_markers <- purrr::map(my_clusters, function(cluster, sct){
    FindMarkers(sct, ident.1 = "treated", group.by = "Treatment", subset.ident = cluster, test.use = "wilcox")}, 
    sct = merged.sct)
names(cluster_markers) <- my_clusters

this will produce a list, cluster_markers with the FindMarkers results for each cluster as a separate "entry"/key-value pair in the list

Thank you so very very much jv! I've been struggling with this for a week, and it finally worked :)

Do you have a suggestion for how I can create a for loop to loop through all clusters? I have 19..

E

see the comment I've added to my answer. Use 'Add Reply' or 'Add Comment' button for follow up discussion

Log in to answer this question.