satijalab / seurat

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

group order in Heatmap in Seurat v3.0.1 #1589

Closed stevenhuakui closed 5 years ago

stevenhuakui commented 5 years ago

Dear Seurat Developers,

I upgraded to seurat v3.0.1 recently and found that the groups in the heatmap are ordered according to the alphabetical order of the group names rather than the factor levels. I checked the code and guess that the group.use needs to be re-factorized after the line 'groups.use <- object[[group.by]][cells, , drop = FALSE]'. You have defined something that is inaccessible outside Seurat so I cannot check where exactly the problem is.

Hope you can fix this soon.

Best, Steven

MikuGene commented 5 years ago

image

Me too

Dragonmasterx87 commented 5 years ago

This is what I do, (works in v3.0.0.9150) it's a bit manual but it gets the job done:

# cluster re-assignment occurs, which re-assigns clustering in my_levels (assuming you have 12 clusters in total)
my_levels <- c(7, 6, 4, 3, 1, 2, 5, 10, 8, 0, 9, 11, 12)

# Re-level object@ident
yourseuratobject@active.ident <- factor(x = yourseuratobject@active.ident, levels = my_levels)

Hope that helps!

andrewwbutler commented 5 years ago

Hi,

The groups in the heatmap should be ordered by the factor levels so @Dragonmasterx87 suggestion should work. If you're running the latest development version, you could also use the levels function directly on the Seurat object to achieve the same effect. E.g.

levels(yourseuratobject) <- my_levels
cemalley commented 5 years ago

Hi all, both the methods here are not working to relevel the active ident in my Seurat 3.0.1. Order of idents in heatmap is still sorted in default alpha not my levels. What happened to the group order option of DoHeatmap?

timoast commented 5 years ago

@cemalley are you using the current version on the develop branch? This was addressed in #1632

cemalley commented 5 years ago

@timoast I'm using Seurat_3.0.1.9015

andrewwbutler commented 5 years ago
data(pbmc_small)
DoHeatmap(pbmc_small)

Screen Shot 2019-06-05 at 3 37 08 PM

> levels(pbmc_small)
[1] "0" "1" "2"
> levels(pbmc_small) <- c("2", "1", "0")
> levels(pbmc_small)
[1] "2" "1" "0"
DoHeatmap(pbmc_small)

Screen Shot 2019-06-05 at 3 36 51 PM

Can you provide an example where it doesn't work?

cemalley commented 5 years ago

@andrewwbutler

aggr4 An object of class Seurat 13212 features across 960 samples within 1 assay Active assay: RNA (13212 features) 2 dimensional reductions calculated: pca, tsne

levels(aggr4) <- c('LAday0','LAday7','Lday7','Aday7')

levels(aggr4) [1] "LAday0" "LAday7" "Lday7" "Aday7"

DoHeatmap(aggr4, features = c('POU5F1','ESRG','NANOG','POU5F1B','FOXD3-AS1'), raster=F) + scale_fill_gradientn(colors = rev(RColorBrewer::brewer.pal(n = 10, name = "RdBu")) ) + guides(color=FALSE)

wrong_levels
andrewwbutler commented 5 years ago

Hmm, can you share aggr4 or a similar object that reproduces the problem?

cemalley commented 5 years ago

I cannot, it is not published yet. Only workaround I have is to rename idents with numbers preceding like "1_LAday0", "2_LAday7".

sidlawrence commented 5 years ago

Hi, I am also having the identical problem - it seems to default to alphabetical order so yes the only solution is a prefix as per @cemalley a fix would be greatly appreciated! thanks sid lawrence

andrewwbutler commented 5 years ago

I'm not able to reproduce the issue on several objects that I've tried and it seems to have been resolved for others (#1632) as well. Are you able to recreate the issue with any of the public datasets we use in any of the tutorials?

erosix commented 5 years ago

Hi, I am also having this problem at the moment on an integrated assay object. I have the develop version now. I just followed all steps in the "Tutorial: Integrating stimulated vs. control PBMC datasets to learn cell-type specific responses" and before running the "FindAllMarkers" function I added: levels(immune.combined)=factor(0:18) #because I have 18 clusters. Then I run the DoHeatmap and it still orders the clusters as =,1,10,11,12 etc.

aubreyghoward commented 5 years ago

I have a similar indexing issue where the clusters are being grouped 1, 10, 11, 12, etc, as shown in the snippette attached below. I have 22 clusters in my graph from DoHeatmap. When I reassign levels, using levels(object) <- factor(1:20), I still get the incorrect indexing.

Capture

Here is a snapshot of my code: levels(SeuratObj) <- c("1","0","2","3","4","5","6","7","8","9","10","11","12","13", "14","15","16","17","18","19","20","21","22") top10 <- SeuratObj.tsne_markers %>% group_by(cluster) %>% top_n(n = 10, wt = avg_logFC) top10HM <- DoHeatmap(SeuratObj, features = top10$gene,group.by = "levels") + NoLegend()

I wanted to confirm that I have also encountered this index in issue.

ph329 commented 4 years ago

Hi,

I was having the same issue in which setting levels to be my desired order didn't change the order plotted with DoHeatmap - but after playing around with it I realized that removing the "group.by" variable from the DoHeatmap input solved the problem. Just make sure to set Idents(seurat_obj) to whatever you want to group by before running DoHeatmap, then no "group.by" input is required. Hopefully this helps others having the same issue.

strawberry789 commented 3 years ago

I was able to reorder the clusters as above, but now the gene order remains the same. I want to have the top genes for the first cluster at the top, and the top genes for the last cluster at the bottom. How should I go about it? I'm using Seurat 4.0

SCQUchenyang commented 3 years ago

Hi,

I was having the same issue in which setting levels to be my desired order didn't change the order plotted with DoHeatmap - but after playing around with it I realized that removing the "group.by" variable from the DoHeatmap input solved the problem. Just make sure to set Idents(seurat_obj) to whatever you want to group by before running DoHeatmap, then no "group.by" input is required. Hopefully this helps others having the same issue.

Could you provide a example for this?Thanks, my seurat don't work for this.

rahulnutron commented 2 years ago

Not sure if this comment is still relevant, but I tried this and it worked

seurat_obj$celltype <- factor( seurat_obj$celltype, levels = c("Epidermal cells", "Mesophyll cells", "Xylem cells" , "Phloem cells" ,"Companion cells", "Tracheary element") )

DoHeatmap(seurat_obj,features = marker_genes,group.by = 'celltype ',size=8)

quaxxo commented 11 months ago

I was able to reorder the clusters as above, but now the gene order remains the same. I want to have the top genes for the first cluster at the top, and the top genes for the last cluster at the bottom. How should I go about it? I'm using Seurat 4.0

Could you solve this issue?

FFriis commented 11 months ago

Hi all

I can confirm the workaround from @rahulnutron

My regular cell group level order:

cell.groups %>% levels()

"Astrocytes" "diff" "Endothelial cells" "ID2" "Immune cells" "L2_3_CUX2" "L2_CUX2" "L4_RORB" "L5_6" "Microglia" "Neu_dev_THY1" "Oligodendrocytes" "OPCs" "Pericytes" "PVALB" "SST" "TBD" "VIP"

Giving this order on the heatmap:

image

I instead want this cell group order:

my_levels <- c("Astrocytes","OPCs","diff","Oligodendrocytes","Microglia","Endothelial cells","Pericytes","Immune cells","L2_CUX2","L2_3_CUX2","L4_RORB","L5_6","ID2","VIP","PVALB","SST","Neu_dev_THY1","TBD")

So I make a cell group factor from the old changing the levels:

seurat$cell.groups<- factor(seurat$cell.groups, levels = my_levels)

Changing heatmap markers to have the right order:

heatmap_markers <- c("AQP4","VCAN","MOG","CSF1R","CLDN5","CUX2","RORB","FEZF2","THEMIS","ID2","VIP","PVALB","SST")

And plot the heatmap:

DoHeatmap(subset(seurat, downsample = 2000), features = heatmap_markers, group.by = "cell.groups", size=3)

image

And afterwards you can rid of small cell groups, line sizes or edit the text position.