jokergoo / ComplexHeatmap

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

Error with labels_mat #451

Closed ronfinn closed 4 years ago

ronfinn commented 4 years ago

Hi there,

I am new to using R and I was just trying to run a R script that someone lese has provided and I have run across the following error when using ComplexHeatmap:

> library(ComplexHeatmap)
Loading required package: grid
========================================
ComplexHeatmap version 2.2.0
Bioconductor page: http://bioconductor.org/packages/ComplexHeatmap/
Github page: https://github.com/jokergoo/ComplexHeatmap
Documentation: http://jokergoo.github.io/ComplexHeatmap-reference

> If you use it in published research, please cite:
> Gu, Z. Complex heatmaps reveal patterns and correlations in multidimensional 
>   genomic data. Bioinformatics 2016.

========================================

> library(circlize)
========================================

> circlize version 0.4.8
> CRAN page: https://cran.r-project.org/package=circlize
> Github page: https://github.com/jokergoo/circlize
> Documentation: http://jokergoo.github.io/circlize_book/book/
> 
> If you use it in published research, please cite:
> Gu, Z. circlize implements and enhances circular visualization 
>   in R. Bioinformatics 2014.

========================================

> > pal_atac <- colorRampPalette(c('#3361A5',
>                             '#248AF3',
>                              '#14B3FF',
>                              '#88CEEF',
>                              '#C1D5DC',
>                              '#EAD397',
>                               '#FDB31A',
>                               '#E42A2A',
>                               '#A31D1D'))(100)
> > ha = HeatmapAnnotation(df = data.frame("Group" = esca.rse$primary_diagnosis, 
>                                "Replicate" = stringr::str_match(colnames(esca.rse),"rep[0-9]?")),
>                         show_annotation_name = T,
>                         col = list(Group = c("Squamous cell carcinoma, NOS" =  "red", 
>                                              "Adenocarcinoma, NOS" = "blue")),
>                         show_legend = T,
>                         annotation_name_side = "left",
>                      annotation_name_gp = gpar(fontsize = 6))
> > 
> > plot.atac <- assay(esca.rse)[result$FDR < fdr.cut.off & abs(result$ESCC_minus_ESAD) > diff.cut.off,]
> 
> col <- colorRamp2(seq(min(plot.atac), max(plot.atac), 
>                        by = (max(plot.atac) - min(plot.atac))/99), pal_atac)
> 
> rows.annot <- rowAnnotation(foo = anno_mark(at = c(1,18), labels = rownames(plot.atac)[c(1,18)]))
> 
> ht_list <- 
>      Heatmap(plot.atac,
>              name = "ATAC-seq log2(counts)", 
>              col = col,
>              column_names_gp = gpar(fontsize = 8),
>              show_column_names = F,
>              heatmap_legend_param = list(legend_direction = "horizontal",
>                                          labels_gp = gpar(fontsize = 12), 
>                                          title_gp = gpar(fontsize = 12)),
>              show_row_names = FALSE,
>              cluster_columns = TRUE,
>              use_raster = TRUE,
>              raster_device = c("png"),
>              raster_quality = 2,
>              cluster_rows = T,
>              right_annotation = rows.annot,
>              row_title = paste0(sum(result$FDR < fdr.cut.off & 
>                                         abs(result$ESCC_minus_ESAD) > diff.cut.off),
>                                 " ATAC-seq peaks"),
>              row_names_gp = gpar(fontsize = 4),
>              top_annotation = ha,
>              column_title_gp = gpar(fontsize = 12), 
>              row_title_gp = gpar(fontsize = 12)) 
> > 
> 
> options(repr.plot.width = 15, repr.plot.height=8)
> 
> draw(ht_list,newpage = TRUE, 
>       column_title = paste0("ATAC-seq ESCC vs ESAD (FDR < ", fdr.cut.off,
>                             ",  Diff mean log2 Count > ",diff.cut.off,")"),
>       column_title_gp = gpar(fontsize = 12, fontface = "bold"),
>       heatmap_legend_side = "bottom",
>      annotation_legend_side = "right")
> 

>Error in labels_mat[, i] : subscript out of bounds
> In addition: Warning message:
> In matrix(1:(nrow * ncol), nrow = nrow, ncol = ncol, byrow = by_row) :
>   data length exceeds size of matrix
> 

I was wonder if you can tell me where I am going wrong?

Kind Regards,

R.

jokergoo commented 4 years ago

for this line:

plot.atac <- assay(esca.rse)[result$FDR < fdr.cut.off & abs(result$ESCC_minus_ESAD) > diff.cut.off,]

Is plot.atac a matrix? How big is it?

jokergoo commented 4 years ago

And can you copy the message from trackback() after the error?

ronfinn commented 4 years ago

Thanks for getting back to me:

Here is the output from the trackback()

> traceback()
22: lapply(labels_mat[, i], function(x) {
        g = grobWidth(textGrob(x, gp = labels_gp))
        if (i < ncol) {
            g = g + gap
        }
        g
    })
21: do.call("unit.c", lapply(labels_mat[, i], function(x) {
        g = grobWidth(textGrob(x, gp = labels_gp))
        if (i < ncol) {
            g = g + gap
        }
        g
    }))
20: discrete_legend_body(at = at, labels = labels, nrow = nrow, ncol = ncol, 
        grid_height = grid_height, grid_width = grid_width, gap = gap, 
        labels_gp = labels_gp, border = border, background = background, 
        type = type, legend_gp = legend_gp, pch = pch, size = size, 
        by_row = by_row)
19: Legend(at = at, labels = labels, title = title, title_gp = title_gp, 
        grid_height = grid_height, grid_width = grid_width, border = border, 
        labels_gp = labels_gp, direction = legend_direction, nrow = nrow, 
        ncol = ncol, legend_gp = gpar(fill = map_to_colors(object, 
            at)), title_position = title_position, by_row = by_row)
18: .local(object, ...)
17: color_mapping_legend(ColorMappingList2[[i]], param = ColorMappingParamList2[[i]], 
        plot = FALSE, ...)
16: color_mapping_legend(ColorMappingList2[[i]], param = ColorMappingParamList2[[i]], 
        plot = FALSE, ...)
15: FUN(X[[i]], ...)
14: lapply(seq_along(ColorMappingList2), function(i) color_mapping_legend(ColorMappingList2[[i]], 
        param = ColorMappingParamList2[[i]], plot = FALSE, ...))
13: draw_legend(ColorMappingList, ColorMappingParamList, side = side, 
        plot = FALSE, legend_list = legend_list, padding = padding, 
        ...)
12: .local(object, ...)
11: annotation_legend_size(object, legend_list = annotation_legend_list, 
        max_height = calc_legends_max_height(object))
10: annotation_legend_size(object, legend_list = annotation_legend_list, 
        max_height = calc_legends_max_height(object))
9: .local(object, ...)
8: make_layout(object, row_title = row_title, row_title_side = row_title_side, 
       row_title_gp = row_title_gp, column_title = column_title, 
       column_title_side = column_title_side, column_title_gp = column_title_gp, 
       heatmap_legend_side = heatmap_legend_side, merge_legends = merge_legends, 
       show_heatmap_legend = show_heatmap_legend, heatmap_legend_list = heatmap_legend_list, 
       annotation_legend_side = annotation_legend_side, show_annotation_legend = show_annotation_legend, 
       annotation_legend_list = annotation_legend_list, ht_gap = ht_gap, 
       main_heatmap = main_heatmap, padding = padding, auto_adjust = auto_adjust, 
       row_dend_side = row_dend_side, row_sub_title_side = row_sub_title_side, 
       column_dend_side = column_dend_side, column_sub_title_side = column_sub_title_side, 
       row_gap = row_gap, cluster_rows = cluster_rows, cluster_row_slices = cluster_row_slices, 
       clustering_distance_rows = clustering_distance_rows, clustering_method_rows = clustering_method_rows, 
       row_dend_width = row_dend_width, show_row_dend = show_row_dend, 
       row_dend_reorder = row_dend_reorder, row_dend_gp = row_dend_gp, 
       row_order = row_order, row_km = row_km, row_km_repeats = row_km_repeats, 
       row_split = row_split, height = height, heatmap_height = heatmap_height, 
       column_gap = column_gap, cluster_columns = cluster_columns, 
       cluster_column_slices = cluster_column_slices, clustering_distance_columns = clustering_distance_columns, 
       clustering_method_columns = clustering_method_columns, column_dend_width = column_dend_width, 
       show_column_dend = show_column_dend, column_dend_reorder = column_dend_reorder, 
       column_dend_gp = column_dend_gp, column_order = column_order, 
       column_km = column_km, column_km_repeats = column_km_repeats, 
       column_split = column_split, width = width, heatmap_width = heatmap_width)
7: make_layout(object, row_title = row_title, row_title_side = row_title_side, 
       row_title_gp = row_title_gp, column_title = column_title, 
       column_title_side = column_title_side, column_title_gp = column_title_gp, 
       heatmap_legend_side = heatmap_legend_side, merge_legends = merge_legends, 
       show_heatmap_legend = show_heatmap_legend, heatmap_legend_list = heatmap_legend_list, 
       annotation_legend_side = annotation_legend_side, show_annotation_legend = show_annotation_legend, 
       annotation_legend_list = annotation_legend_list, ht_gap = ht_gap, 
       main_heatmap = main_heatmap, padding = padding, auto_adjust = auto_adjust, 
       row_dend_side = row_dend_side, row_sub_title_side = row_sub_title_side, 
       column_dend_side = column_dend_side, column_sub_title_side = column_sub_title_side, 
       row_gap = row_gap, cluster_rows = cluster_rows, cluster_row_slices = cluster_row_slices, 
       clustering_distance_rows = clustering_distance_rows, clustering_method_rows = clustering_method_rows, 
       row_dend_width = row_dend_width, show_row_dend = show_row_dend, 
       row_dend_reorder = row_dend_reorder, row_dend_gp = row_dend_gp, 
       row_order = row_order, row_km = row_km, row_km_repeats = row_km_repeats, 
       row_split = row_split, height = height, heatmap_height = heatmap_height, 
       column_gap = column_gap, cluster_columns = cluster_columns, 
       cluster_column_slices = cluster_column_slices, clustering_distance_columns = clustering_distance_columns, 
       clustering_method_columns = clustering_method_columns, column_dend_width = column_dend_width, 
       show_column_dend = show_column_dend, column_dend_reorder = column_dend_reorder, 
       column_dend_gp = column_dend_gp, column_order = column_order, 
       column_km = column_km, column_km_repeats = column_km_repeats, 
       column_split = column_split, width = width, heatmap_width = heatmap_width)
6: .local(object, ...)
5: draw(ht_list, ...)
4: draw(ht_list, ...)
3: .local(object, ...)
2: draw(ht_list, newpage = TRUE, column_title = paste0("ATAC-seq ESCC vs ESAD (FDR < ", 
       fdr.cut.off, ",  Diff mean log2 Count > ", diff.cut.off, 
       ")"), column_title_gp = gpar(fontsize = 12, fontface = "bold"), 
       heatmap_legend_side = "bottom", annotation_legend_side = "right")
1: draw(ht_list, newpage = TRUE, column_title = paste0("ATAC-seq ESCC vs ESAD (FDR < ", 
       fdr.cut.off, ",  Diff mean log2 Count > ", diff.cut.off, 
       ")"), column_title_gp = gpar(fontsize = 12, fontface = "bold"), 
       heatmap_legend_side = "bottom", annotation_legend_side = "right")
jokergoo commented 4 years ago

I think it might due to the format of plot.atac is not correct. Can you paste the output from str(plot.atac)?

ronfinn commented 4 years ago

Thank you, here you go:

> str(plot.atac)
 num [1:4067, 1:33] 1.59 1.74 1.29 1.54 1.29 ...
- attr(*, "dimnames")=List of 2
  ..$ : chr [1:4067] "ESCA_61_chr1_1124660_1125159" "ESCA_62_chr1_1125161_1125660" "ESCA_66_chr1_1136429_1136928" "ESCA_364_chr1_2048227_2048726" ...
  ..$ : chr [1:33] "ESCC-TCGA-IG-A51D-01A" "ESCC-TCGA-IG-A51D-01A" "ESCC-TCGA-LN-A9FQ-01A" "ESCC-TCGA-LN-A9FQ-01A" ...

 

jokergoo commented 4 years ago

Ah, sorry it has nothing to be with the matrix. This is a problem of the annotation:

> > ha = HeatmapAnnotation(df = data.frame("Group" = esca.rse$primary_diagnosis, 
>                                "Replicate" = stringr::str_match(colnames(esca.rse),"rep[0-9]?")),
>                         show_annotation_name = T,
>                         col = list(Group = c("Squamous cell carcinoma, NOS" =  "red", 
>                                              "Adenocarcinoma, NOS" = "blue")),
>                         show_legend = T,
>                         annotation_name_side = "left",
>                      annotation_name_gp = gpar(fontsize = 6))

What are the values of

data.frame("Group" = esca.rse$primary_diagnosis, 
>                                "Replicate" = stringr::str_match(colnames(esca.rse),"rep[0-9]?"))

?

ronfinn commented 4 years ago
>data.frame("Group" = esca.rse$primary_diagnosis,  "Replicate" = stringr::str_match(colnames(esca.rse),"rep[0-9]?"))

                         Group Replicate
1  Squamous cell carcinoma, NOS      <NA>
2  Squamous cell carcinoma, NOS      <NA>
3  Squamous cell carcinoma, NOS      <NA>
4  Squamous cell carcinoma, NOS      <NA>
5           Adenocarcinoma, NOS      <NA>
6           Adenocarcinoma, NOS      <NA>
7           Adenocarcinoma, NOS      <NA>
8           Adenocarcinoma, NOS      <NA>
9  Squamous cell carcinoma, NOS      <NA>
10 Squamous cell carcinoma, NOS      <NA>
11          Adenocarcinoma, NOS      <NA>
12          Adenocarcinoma, NOS      <NA>
13 Squamous cell carcinoma, NOS      <NA>
14 Squamous cell carcinoma, NOS      <NA>
15 Squamous cell carcinoma, NOS      <NA>
16 Squamous cell carcinoma, NOS      <NA>
17 Squamous cell carcinoma, NOS      <NA>
18          Adenocarcinoma, NOS      <NA>
19          Adenocarcinoma, NOS      <NA>
20 Squamous cell carcinoma, NOS      <NA>
21 Squamous cell carcinoma, NOS      <NA>
22 Squamous cell carcinoma, NOS      <NA>
23 Squamous cell carcinoma, NOS      <NA>
24 Squamous cell carcinoma, NOS      <NA>
25 Squamous cell carcinoma, NOS      <NA>
26 Squamous cell carcinoma, NOS      <NA>
27 Squamous cell carcinoma, NOS      <NA>
28 Squamous cell carcinoma, NOS      <NA>
29 Squamous cell carcinoma, NOS      <NA>
30          Adenocarcinoma, NOS      <NA>
31          Adenocarcinoma, NOS      <NA>
32          Adenocarcinoma, NOS      <NA>
33          Adenocarcinoma, NOS      <NA>
jokergoo commented 4 years ago

The second column (Replicate) are all NA. I think that causes the error.

ronfinn commented 4 years ago

Thanks for your reply, Any suggestions for how I fix this? Sorry, new to R and I am having trouble trying to authour of this script to respond. BW, R.

jokergoo commented 4 years ago

Can you just remove it? It is all NAs and provides no information.

And by the way, what does this line do:

"Replicate" = stringr::str_match(colnames(esca.rse),"rep[0-9]?"))

I think you might have done something wrong with using str_match(). Maybe nothing in the column names matches rep[0-9]?? Maybe you can change it to rep[0-9]* to match any number of digits?