satijalab / seurat

R toolkit for single cell genomics
http://www.satijalab.org/seurat
Other
2.3k stars 917 forks source link

Cluster IDs in UMAP not in numerical order #9134

Closed zgb963 closed 3 months ago

zgb963 commented 4 months ago

Hi Seurat team,

I have some questions about the order of my cluster id's when making a UMAP. I first was renaming the clusters in the below UMAP for a first pass of labeling cell types.

DimPlot(seurat.obj_combined_filtered, reduction = "umap", group.by = "RNA_snn_res.0.4", label = TRUE)
rhemac10_snRNAseq_combined_UMAP_PC1-20_res 0 4_7_12_24

So I renamed them first by just the cell type in each cluster using this code

seurat.obj_combined_filtered<-RenameIdents(seurat.obj_combined_filtered, `0` = "Glutamatergic Neurons-1")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, `1` = "Glutamatergic Neurons-2")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, `2` = "Mature Oligodendrocytes")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, `3` = "Glutamatergic Neurons-3")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, `4` = "Glutematergic Neurons-4 L4")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, `5` = "Glutematergic Neurons-5")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, `6` = "Glutematergic Neurons-6")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, `7` = "PVALB Interneurons")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, `8` = "Glutematergic Neurons-7")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, `9` = "Astrocytes")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, `10` = "SST Interneurons")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, `11` = "Glutematergic Neuron-8")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, `12` = "VIP Interneurons")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, `13` = "Immature Oligodendrocytes (OPCs)")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, `14` = "Glutematergic Neurons-9")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, `15` = "Glutematergic Neuron-10")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, `16` = "Glutematergic Neuron-11")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, `17` = "Vascular cells")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, `18` = "Glutematergic Neuron-12")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, `19` = "Glutematergic Neuron-13")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, `20` = "Glutematergic Neuron-14")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, `21` = "TBD") 

But then when I made the UMAP with the labeled cluster id's, the ordering of the clusters in the legend seems to be in a weird order and I'm not sure why it's ordering it this way? I'm also not sure why the colors changed for each cluster?

DimPlot(seurat.obj_combined_filtered, reduction = "umap", group.by = "ident", label = TRUE)
rhemac10_snRNAseq_combined_UMAP_PC1-20_res 0 4_1st_pass_label_7_12_24

Next thing I did was rename them again except this time I kept the original cluster id first before the cell type

seurat.obj_combined_filtered<-RenameIdents(seurat.obj_combined_filtered, "Glutamatergic Neurons-1" = "0 Glutamatergic Neurons 1st")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, "Glutamatergic Neurons-2" = "1 Glutamatergic Neurons 2nd")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, "Mature Oligodendrocytes" = " 2 Mature Oligodendrocytes")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, "Glutamatergic Neurons-3" = "3 Glutamatergic Neurons 3rd")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, "Glutematergic Neurons-4 L4" = "4 Glutamatergic Neurons 4th L4")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, "Glutematergic Neurons-5" = "5 Glutamatergic Neurons 5th")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, "Glutematergic Neurons-6" = "6 Glutamatergic Neurons 6th")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, "PVALB Interneurons" = "7 PVALB Interneurons")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, "Glutematergic Neurons-7" = "8 Glutamatergic Neurons 7th")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, "Astrocytes" = "9 Astrocytes")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, "SST Interneurons" = "10 SST Interneurons")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, "Glutematergic Neuron-8" = "11 Glutamatergic Neuron 8th")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, "VIP Interneurons" = "12 VIP Interneurons")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, "Immature Oligodendrocytes (OPCs)" = "13 Immature Oligodendrocytes (OPCs)")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, "Glutematergic Neurons-9" = "14 Glutamatergic Neurons 9th")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, "Glutematergic Neuron-10" = "15 Glutamatergic Neurons 10th")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, "Glutematergic Neuron-11" = "16 Glutamatergic Neurons 11th")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, "Vascular cells" = "17 Vascular cells")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, "Glutematergic Neuron-12" = "18 Glutamatergic Neurons 12th")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, "Glutematergic Neuron-13" = "19 Glutamatergic Neurons 13th")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, "Glutematergic Neuron-14" = "20 Glutamatergic Neurons 14th")
seurat.obj_combined_filtered <- RenameIdents(seurat.obj_combined_filtered, "TBD" = "21 TBD")
print(table(Idents(seurat.obj_combined_filtered)))

                             21 TBD       20 Glutamatergic Neurons 14th       19 Glutamatergic Neurons 13th       18 Glutamatergic Neurons 12th 
                                137                                 199                                 214                                 286 
                  17 Vascular cells       16 Glutamatergic Neurons 11th       15 Glutamatergic Neurons 10th        14 Glutamatergic Neurons 9th 
                                422                                 431                                 810                                 861 
13 Immature Oligodendrocytes (OPCs)                 12 VIP Interneurons         11 Glutamatergic Neuron 8th                 10 SST Interneurons 
                               1301                                1551                                2078                                2100 
                       9 Astrocytes         8 Glutamatergic Neurons 7th                7 PVALB Interneurons         6 Glutamatergic Neurons 6th 
                               2150                                2563                                2634                                3501 
        5 Glutamatergic Neurons 5th      4 Glutamatergic Neurons 4th L4         3 Glutamatergic Neurons 3rd           2 Mature Oligodendrocytes 
                               3604                                4128                                4988                                5737 
        1 Glutamatergic Neurons 2nd         0 Glutamatergic Neurons 1st 
                               7848                                8477 

I then created the UMAP again. But I wasn't sure why the legend is in the reverse order.

rhemac10_snRNAseq_combined_UMAP_PC1-20_res 0 4_1st_pass_label_redo_7_19_24

I tried using the order argument to manually change the order of the clusters so that cluster 0 would be listed first and cluster 21 would be listed last, but then it orders the cluster ids in the legend randomly again

DimPlot(seurat.obj_combined_filtered, reduction = "umap", group.by = "ident", label = TRUE, label.size = 2.5, order = c("21 TBD", "20 Glutamatergic Neurons 14th", "19 Glutamatergic Neurons 13th", "18 Glutamatergic Neurons 12th", "17 Vascular cells", "16 Glutamatergic Neurons 11th", "15 Glutamatergic Neurons 10th", "14 Glutamatergic Neurons 9th", "13 Immature Oligodendrocytes (OPCs)", "12 VIP Interneurons ", "11 Glutamatergic Neuron 8th", "10 SST Interneurons", "9 Astrocytes ", "8 Glutamatergic Neurons 7th", "7 PVALB Interneurons", "6 Glutamatergic Neurons 6th", "5 Glutamatergic Neurons 5th", "4 Glutamatergic Neurons 4th L4 ", "3 Glutamatergic Neurons 3rd", "2 Mature Oligodendrocytes", "1 Glutamatergic Neurons 2nd", "0 Glutamatergic Neurons 1st"))

I also made a subcluster UMAP of clusters 0, 1, 3,, 4, 5, 6, 8, 11, 14, 15, 16, 19, 20

seurat.obj_combined_filtered_excitatiory <- subset(seurat.obj_combined_filtered, idents = c(0, 1, 3, 4, 5, 6, 8, 11, 14, 15, 16, 19, 20))
ElbowPlot(seurat.obj_combined_filtered_excitatiory, ndims = 45)
seurat.obj_combined_filtered_excitatiory <- FindNeighbors(seurat.obj_combined_filtered_excitatiory, dims = 1:25)
seurat.obj_combined_filtered_excitatiory <- FindClusters(seurat.obj_combined_filtered_excitatiory, resolution = c(0.5, 0.7, 0.9, 1, 1.5))
seurat.obj_combined_filtered_excitatiory <- RunUMAP(seurat.obj_combined_filtered_excitatiory, dims = 1:25)
DimPlot(seurat.obj_combined_filtered_excitatiory, reduction = "umap", group.by = "RNA_snn_res.0.5", label = TRUE)
DimPlot(seurat.obj_combined_filtered_excitatiory, reduction = "umap", group.by = "RNA_snn_res.0.7", label = TRUE)
DimPlot(seurat.obj_combined_filtered_excitatiory, reduction = "umap", group.by = "RNA_snn_res.0.9", label = TRUE)
DimPlot(seurat.obj_combined_filtered_excitatiory, reduction = "umap", group.by = "RNA_snn_res.1", label = TRUE)
DimPlot(seurat.obj_combined_filtered_excitatiory, reduction = "umap", group.by = "RNA_snn_res.1.5", label = TRUE)

But it's for some reason ordering them lexicographically, not in numerical order like before?

Screenshot 2024-07-22 at 1 30 40 PM
igrabski commented 3 months ago

Hi, you should be able to force whatever order you want by setting the factor levels of Idents as desired, e.g. something like:

Idents(obj) <- factor(Idents(obj),levels=('a','b','c'))

I will close this issue for now, but please re-open this issue if you are still running into problems!