Conditional clustering arguments in R (complexheatmap)
1
0
Entering edit mode
20 months ago
MaxF ▴ 100

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).

R heatmap • 1.1k views
ADD COMMENT
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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 REPLY
1
Entering edit mode

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 REPLY
3
Entering edit mode
20 months ago
ATpoint 62k

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 COMMENT
0
Entering edit mode

Oh man that's so simple! Thank you!

ADD REPLY

Login before adding your answer.

Traffic: 1944 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6