jokergoo / ComplexHeatmap

Make Complex Heatmaps
https://jokergoo.github.io/ComplexHeatmap-reference/book/
Other
1.31k stars 227 forks source link

Cannot Do Custom Textbox Annotations. #1191

Open pthom010 opened 4 months ago

pthom010 commented 4 months ago

I am able to make a Heatmap with the following data (attached in a csv file) AxillaryDESeq_OrthoPlagio_DEG_CPM.csv

I use the following script to make my Heatmap and subsequently get the assigned clusters:

phase_heatmap1 = Heatmap(axillary.mat, width = unit(12, "cm"),
                        column_names_side = "bottom",
                        column_title_side = "top",
                        column_names_centered = TRUE,
                        row_names_side = "right",
                        km = 12,
                        cluster_row_slices = FALSE,
                        row_title_side = "left",
                        row_names_gp = gpar(fontfamily = "sans", fontsize = 12),
                        row_title_gp = gpar(fontfamily = "sans", fontsize = 18),
                        cluster_rows = T, cluster_columns = F,
                        na_col = "#000000",
                        column_title_gp = gpar(fontfamily = "sans", fontsize = 18),
                        column_names_gp = gpar(fontfamily = "sans", fontsize = 18),
                        column_names_rot = 90,
                        row_gap = unit(4, "mm"),
                        show_row_names = F,
                        show_column_names = T,
                        row_title_rot = 0,
                        border = T,
                        heatmap_legend_param = list(title_gp = gpar(fontfamily = "sans", fontsize = 16),
                                                    labels_gp = gpar(fontfamily = "sans", fontsize = 14), 
                                                    border = "#000000",
                                                    direction = "vertical",
                                                    legend_height = unit(6, "cm"),
                                                    title = expression(log[10]~CPM)),
                        column_gap = unit(4, "cm"),
                        col = colorRamp2(c(0,  1.5,  3), colors = c("#0000FF", "white", "#FF0000")))

I then acquire my clusters using the following script:

r.dend <- row_dend(phase_heatmap)  #If needed, extract row dendrogram
rcl.list <- row_order(phase_heatmap)  #Extract clusters (output is a list)

lapply(rcl.list, function(x) length(x))  #check/confirm size gene clusters

library(magrittr) # needed to load the pipe function '%%'

clu_df <- lapply(names(rcl.list), function(i){
  out <- data.frame(GeneID = rownames(axillary.mat[rcl.list[[i]],]),
                    Cluster = paste0("cluster", i),
                    stringsAsFactors = FALSE)
  return(out)
}) %>%  #pipe (forward) the output 'out' to the function rbind to create 'clu_df'
  do.call(rbind, .)

With these clusters, I want to make a new Heatmap with annotations in text boxes corresponding to specific clusters (Clusters 1, 2, 4, 5, 6, 8, 9, and 10) with the following script:

phase_heatmap1 = Heatmap(axillary.mat, width = unit(12, "cm"),
                        column_names_side = "bottom",
                        column_title_side = "top",
                        column_names_centered = TRUE,
                        row_names_side = "right",
                        row_split = clu_df$Cluster,
                        cluster_row_slices = FALSE,
                        row_title_side = "left",
                        row_names_gp = gpar(fontfamily = "sans", fontsize = 12),
                        row_title_gp = gpar(fontfamily = "sans", fontsize = 18),
                        cluster_rows = T, cluster_columns = F,
                        na_col = "#000000",
                        column_title_gp = gpar(fontfamily = "sans", fontsize = 18),
                        column_names_gp = gpar(fontfamily = "sans", fontsize = 18),
                        column_names_rot = 90,
                        row_gap = unit(4, "mm"),
                        show_row_names = F,
                        right_annotation = rowAnnotation(
                          textbox = anno_textbox(
                            clusters_split1[c("cluster1","cluster10", "cluster2","cluster4","cluster5","cluster6","cluster8","cluster9")], 
                            go_split[c("cluster1","cluster10","cluster2","cluster4","cluster5","cluster6","cluster8","cluster9")], # names should match
                            word_wrap = TRUE, 
                            add_new_line = TRUE)),
                        show_column_names = T,
                        row_title_rot = 0,
                        border = T,
                        heatmap_legend_param = list(title_gp = gpar(fontfamily = "sans", fontsize = 16),
                                                    labels_gp = gpar(fontfamily = "sans", fontsize = 14), 
                                                    border = "#000000",
                                                    direction = "vertical",
                                                    legend_height = unit(6, "cm"),
                                                    title = expression(log[10]~CPM)),
                        column_gap = unit(4, "cm"),
                        col = colorRamp2(c(0,  1.5,  3), colors = c("#0000FF", "white", "#FF0000")))

I keep getting the following error:

Error: `at` should be numeric row index corresponding to the matrix.

Not sure where the error is coming in. Any help would be appreciated. Thanks.