Question: Conditional clustering arguments in R (complexheatmap)
0
gravatar for MaxF
3 months ago by
MaxF80
MaxF80 wrote:

I have a nice script to generate heatmaps from gene expression data. I am making a lot of permutations of these heatmaps and am trying to make this as smooth as possible.

The problem I'm running into is with splitting rows into clusters. Complexheatmap and pheatmap both have take an argument that allows you to split rows into clusters based on the dendrogram (in Complexheatmap it's row_split =). Here's an example:

library(ComplexHeatmap)

test_mat <- matrix(sample(1:10000, 240), nrow = 40, ncol = 6)
rownames(test_mat) <- paste0("Gene", 1:40)
colnames(test_mat) <- c(paste0("A", 1:3), paste0("B", 1:3))

clust_num    <- 4

Heatmap(test_mat, 
        show_row_names=T, 
        show_column_names = T, 
        row_split = clust_num,
        cluster_rows=TRUE)

This works fine, but I'd like to make the row_split argument conditional in the event that I don't cluster the rows. Currently I am just manually commenting out the row_split line every time I don't want clusters. I'm hoping to do something like:

cluster_switch <- FALSE

Heatmap(test_mat, 
        show_row_names=T, 
        show_column_names = T, 
        if (cluster_switch) {row_split = clust_num},
        cluster_rows=TRUE)

But this gives an error. I know I can get around this by having a conditional statement at the top of the heatmap call, like this:

cluster_switch <- FALSE

if(cluster_switch){
    Heatmap(test_mat, 
            show_row_names=T, 
            show_column_names = T, 
            row_split = clust_num,
            cluster_rows=TRUE)
} else{
    Heatmap(test_mat, 
             show_row_names=T, 
             show_column_names = T, 
             cluster_rows=TRUE)
}

But this seems pretty clunky since I make the call to the heatmap multiple times (so I can extract the order of the clustered rows).

heatmap R • 215 views
ADD COMMENTlink modified 3 months ago by ATpoint46k • written 3 months ago by MaxF80

Only one of if or else will be executed, so you are not calling Heatmap multiple times.

ADD REPLYlink written 3 months ago by MatthewP880

Thanks -- I get that, in the above solution, I'm only calling Heatmap once. In my actual code I do call it twice: once to get the row order of the clusters (then I have some other code to label them) and then call it again to draw the annotated heatmap. When using the if - else solution above, I end up with a large unwieldy chunk of duplicated code just to change one line in the heatmap call.

ADD REPLYlink written 3 months ago by MaxF80
1

Ok, but you can get row order by clustering your data.

dist <- dist(matrix)
cls <- hclust(dist)
row_order <- cls.order

But you have to use same cluster method as Heatmap. So answer from ATpoint is better.

ADD REPLYlink written 3 months ago by MatthewP880
3
gravatar for ATpoint
3 months ago by
ATpoint46k
ATpoint46k wrote:

You can set it to NULL to turn the argument off. ?Heatmap shows you the help and row_split defaults to split which defaults to NULL, therefore set to NULL to turn off.

if(cluster_switch) {
  clust_num <- whateverthevaluemustbe
} else clust_num <- NULL

Heatmap(test_mat, 
        show_row_names=TRUE, 
        show_column_names = TRUE, 
        cluster_rows=TRUE,
        row_split = clust_num)
ADD COMMENTlink written 3 months ago by ATpoint46k

Oh man that's so simple! Thank you!

ADD REPLYlink written 3 months ago by MaxF80
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1609 users visited in the last hour
_