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).
Only one of
if
orelse
will be executed, so you are not callingHeatmap
multiple times.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.
Ok, but you can get row order by clustering your data.
But you have to use same cluster method as
Heatmap
. So answer from ATpoint is better.