XuegongLab / HGC

fast hierarchical clustering for large-scale single-cell data
8 stars 5 forks source link

Subsetting tree and custom color palette #3

Closed kaizen89 closed 1 year ago

kaizen89 commented 1 year ago

Hi, I got a tree using seurat graph, now I would like to plot only a subset of it so it does not take a lot of time. Is it possible to do so? Also, how can I use a custom color palette instead of the default one which is not really convenient? Thanks!

zouzh14 commented 1 year ago

Hello! Thanks for the reply in the utilization of HGC. And here is the response for the two problems:

  1. Do you mean you have built a tree and then want just to plot a subtree structure of it? I will look for solutions later.
  2. Of course custom color palette is supported in the dendextend package and I will show some examples.
kaizen89 commented 1 year ago

@zouzh14

  1. Yes, exactly, otherwise it takes really long to plot.
  2. Thanks
Fan-iX commented 1 year ago

@zouzh14 I had the same problem once; I did in this way:

hc <- HGC.dendrogram(G)
# First 2000 nodes from root
dend1 <- cut(as.dendrogram(hc), rev(hc$height)[2000])$upper # may take some time for large hclust
# Subtree
dend2 <- as.dendrogram(hc)[[1]][[2]][[1]]

Here dend1 and dend2 are dendrogram objects, you can plot them directly with plot() To set custom branch color, you can use color_branches from the dendextend package before plotting

plot(color_branches(dend, k=3, col=c("#FF0000","green","#0000FF")))

Otherwise, you can use the ggtree package, which provides highly customizable styles for your tree.

zouzh14 commented 1 year ago

@Fan-iX, Thanks for sharing you experience!

HGC ploting function is based on the dendextend package, so for the two requirements, I will show the solutions from dendextend.

  1. Custom color palette

We could save the tree to a dendextend object and setting colours, and in fact it is the way we do in PlotDendrogram function. Here is an instance.

Pollen.ClusteringTree <- HGC.dendrogram(G = Pollen.SNN)
Pollen.ClusteringTree$height = log(Pollen.ClusteringTree$height + 1)
Pollen.ClusteringTree$height = log(Pollen.ClusteringTree$height + 1)

library(dplyr)
library(dendextend)

clus.dendrogram <- stats::as.dendrogram(Pollen.ClusteringTree)
dend <- clus.dendrogram %>%
    dendextend::set("branches_k_color", k=5, value = c("skyblue", "orange", "grey", "pink", "firebrick")) %>%
    dendextend::set("branches_lwd", 1.2) %>%
    dendextend::set("labels_colors", "white")
plot(dend)

Here we set branches colours in "value" of dendextend::set("branches_k_color") sentence. You can find more example in https://cran.r-project.org/web/packages/dendextend/vignettes/dendextend.html.

  1. Get a sub-tree

With the function get_subdendrograms from dendextend package, we can get the subtree with specific clustering numbers, and then choose the one wanted.

dend_list <- get_subdendrograms(dend, 5)
dend1 = dend_list[[1]]
plot(dend1)

Here the dend1 is the subtree in correspondence with "cluster 1" in dendextend::cutree(dend, k=5). More details could check https://www.rdocumentation.org/packages/dendextend/versions/1.15.2/topics/get_subdendrograms.

kaizen89 commented 1 year ago

Thanks all!