Retrieve Clusters From ComplexHeatmap
1
0
Entering edit mode
7 months ago
pthom010 ▴ 10

I made a heatmap with the following data (the actual table has 240 rows):

tf.log


                              0-dpi      1-dpi     7-dpi    14-dpi      22-dpi
TRINITY_DN0_c0_g1_i2      1.27584408  0.5872404  1.780178  1.414362  1.53341851
TRINITY_DN10214_c0_g1_i2 -2.34774378 -2.9194079 -3.211677 -2.885869 -2.04617227
TRINITY_DN10214_c0_g1_i6 -2.14867876 -1.5794957 -1.577811 -2.485095 -1.44172768
TRINITY_DN1038_c0_g1_i4   0.03163921  0.7375222  2.037936  2.462830  0.04559793
TRINITY_DN10462_c0_g1_i2 -2.63973533 -2.8039350 -2.481144 -2.698932 -1.76284020
TRINITY_DN1052_c0_g1_i3  -3.32767605 -2.2006082 -1.887869 -1.211592 -1.43292669

I then made the following heatmap:

heat.gen.k = Heatmap(tf.log, width = unit(10, "cm"),
                     km = 12, 
                     cluster_columns = F, 
                     show_row_names = F, 
                     row_title_rot = 0, 
                     row_gap = unit(3, "mm"), 
                     name = "Log2FC", 
                     column_title = "Resistant - Susceptible",
                     column_title_gp = gpar(fontfamily = "sans", fontsize = 28), 
                     column_names_gp = gpar(fontfamily = "sans", fontsize = 20),
                     col = heat.col,
                     right_annotation = ha)
print(heat.gen.k)

I would like to pull the clusters out for downstream analysis but I cannot do so with the following code:

clusterlist = row_order(heat.gen.k)
clu_df <- lapply(names(clusterlist), function(i){
  out <- data.frame(GeneID = rownames(tf.log[clusterlist[[i]],]),
                                             Cluster = paste0("cluster", i),
                                             stringsAsFactors = FALSE)
     return(out)
   }) %>%  
     do.call(rbind, .)

I keep getting the following error:

Error in data.frame(GeneID = rownames(tf.log[clusterlist[[i]], ]), Cluster = paste0("cluster",  :
  arguments imply differing number of rows: 0, 1

Not sure how to resolve this issue.

R complexheatmap • 467 views
ADD COMMENT
2
Entering edit mode
7 months ago
ATpoint 49k

It happens when a cluster consists of only one gene. In that case you are extracting a data.frame with a single row and this is then coerced into a vector which has no rownames. You can easily avoid that by changing:

rownames(tf.log[clusterlist[[i]],]) into rownames(tf.log)[clusterlist[[i]]] so you don't extract the data.frame and then the rownames but extract the rownames first and then subset it to those genes you have per cluster.

ADD COMMENT
0
Entering edit mode

This worked. Thanks!

ADD REPLY

Login before adding your answer.

Traffic: 1644 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