Question: Retrieve Clusters From ComplexHeatmap
0
gravatar for pthom010
3 months ago by
pthom0100
pthom0100 wrote:

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.

complexheatmap R • 230 views
ADD COMMENTlink modified 3 months ago by ATpoint44k • written 3 months ago by pthom0100
2
gravatar for ATpoint
3 months ago by
ATpoint44k
ATpoint44k wrote:

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 COMMENTlink modified 3 months ago • written 3 months ago by ATpoint44k

This worked. Thanks!

ADD REPLYlink written 3 months ago by pthom0100
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: 2175 users visited in the last hour
_