ropensci / iheatmapr

Complex, interactive heatmaps in R
https://docs.ropensci.org/iheatmapr
Other
267 stars 35 forks source link

How to extract cluster assignment from Iheatmap-class object? #2

Open mschilli87 opened 7 years ago

mschilli87 commented 7 years ago

Dear Alicia,

First of all, thanks for this nice package.


I have a question on how to extract cluster assignment from an Iheatmap-class object.

Lets consider the following example:

require(iheatmapr)

nclust <- 3

mtcars %>%
  as.matrix %>%
  main_heatmap %>%
  add_row_clustering(k = nclust) -> hm

Printing hm, I get a nice interactive heatmap with the rows clustered into three groups.

My question is how can I extract the information which row (car is this case) belongs to which cluster.

The best I came up with so far ist the following:

shapes(hm)$row_dendro@data %>%
  cutree(k = nclust) -> clusters

This way, clusters is assigned an integer vector with the cluster assignment named by the corresponding row labels.

However, I see two issues with this approach:

  1. The number of clusters needs to be saved for cutree.
  2. The need to explicitely access a slot using @ tells me there ~must~ should be a better way to achieve what I want.

The first point could be resolved by recovering the number of row clusters from the Iheatmap-class object itself.

The best way to do so I found is the following:

colorbars(hm)$`Row<br>Clusters`@ticktext %>%
  length -> nclust

Unfortunately, this emphasizes the second point I made above by seaming even more 'hacky'.

I would highly appreciate any feedback feedback on how to get from hm to clusters without an ugly wrapper function based on the above hacks.

Best regards, Marcel

AliciaSchep commented 7 years ago

Hi Marcel,

Thanks for bringing up this issue! It seems like extracting the dendrogram and / or the cluster assignments is likely a general feature that would be useful to include in the package with a dedicated method.

In the meantime, a simpler (but still hacky!) solution for your example:

plots(hm)[["Row<br>Clusters"]]@data

will return what you're looking for.

Currently there is a get_data method that is not exported (only available using iheatmapr:::get_data, although using ::: isn't really better than using @), that would help with not having to use @. I think it may be a good idea to export that method, although I think an additional method specifically for pulling out cluster information would also be a useful addition to the package, as I imagine that is the type of data that one would be most interested in pulling out of the object. I will hopefully get a chance to add such a method in the next few days.

Cheers, Alicia

mschilli87 commented 7 years ago

Thanks a lot for your fast feedback.

Another main concern with any solution so far is the need to know the name/label of the row cluster colorbar ("Row<br>Clusters" in this case). An ideal solution would also work when name = "gene cluster" was passed to add_row_clustering.