jokergoo / ComplexHeatmap

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

row_km split membership access #622

Closed vkartha closed 3 years ago

vkartha commented 4 years ago

Hi, thanks for making such a wonderful package. I was wondering if there is a way to obtain the km split membership (similar to how you can fetch this in pheatmap), using the main Heatmap function. For example, if I have used parameter row_km = 5, I'd like to be able to get a vector of memberships (row_km groupings) for each row feature, saying it belongs to clusters 1-5, for downstream annotation. Is this possible using the stored Heatmap output?

jokergoo commented 4 years ago

Do as follows:

ht = Heatmap(..., row_km = 5)
ht = draw(ht)
row_order(ht) # this returns a list of 5
honghh2018 commented 3 years ago

Hi @jokergoo , the heatmap drawing with param row_km showing different cluster when runing two time. the heatmap like below showing : image image The cluster including genes was different but the row_order get the genes assigned into the clusters was unchange,like below. image cluster 1 alway own 137 genes. it was paradox. The code i plot the heatmap lying below:

htkm <- Heatmap( pt.matrix, name = "z-score", col = colorRamp2(seq(from=-2,to=2,length=11),rev(brewer.pal(11, "Spectral"))), show_row_names = TRUE, show_column_names = FALSE, row_names_gp = gpar(fontsize = 8), row_km = 4, row_title_rot = 0, cluster_rows = TRUE, cluster_row_slices = FALSE, cluster_columns = FALSE )

The session R: R version 4.0.2 (2020-06-22) Platform: x86_64-pc-linux-gnu (64-bit) Running under: CentOS Linux 7 (Core)

Matrix products: default BLAS: /share/nas1/Data/software/R/R-4.0.2/lib64/R/lib/libRblas.so LAPACK: /share/nas1/Data/software/R/R-4.0.2/lib64/R/lib/libRlapack.so

locale: [1] LC_CTYPE=zh_CN.UTF-8 LC_NUMERIC=C LC_TIME=zh_CN.UTF-8
[4] LC_COLLATE=zh_CN.UTF-8 LC_MONETARY=zh_CN.UTF-8 LC_MESSAGES=zh_CN.UTF-8
[7] LC_PAPER=zh_CN.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=zh_CN.UTF-8 LC_IDENTIFICATION=C

attached base packages: [1] splines grid stats4 parallel stats graphics grDevices utils datasets methods
[11] base

other attached packages: [1] harmony_1.0 Rcpp_1.0.7 clusterProfiler_4.0.5
[4] DOSE_3.18.2 enrichplot_1.12.2 STRINGdb_2.2.2
[7] cowplot_1.1.1 readxl_1.3.1 ggrepel_0.9.1
[10] DT_0.17 doParallel_1.0.16 iterators_1.0.13
[13] foreach_1.5.1 ggalluvial_0.12.3 NMF_0.23.0
[16] rngtools_1.5 pkgmaker_0.32.2 registry_0.5-1
[19] gdtools_0.2.3 CellChat_1.1.3 igraph_1.2.6
[22] ggforce_0.3.2 MASS_7.3-51.6 sfsmisc_1.1-11
[25] hopach_2.50.0 cluster_2.1.0 org.Mm.eg.db_3.12.0
[28] RpipeLineFun_0.2.0 infercnv_1.6.0 org.Hs.eg.db_3.13.0
[31] data.table_1.13.6 hash_2.2.6.1 clustree_0.4.3
[34] ggraph_2.0.4 monocle_2.18.0 BiocParallel_1.24.1
[37] SCENIC_1.2.4 loomR_0.2.1.9000 hdf5r_1.3.3
[40] R6_2.5.0 RcisTarget_1.10.0 GENIE3_1.12.0
[43] AUCell_1.12.0 SCopeLoomR_0.10.4 qusage_2.24.0
[46] limma_3.46.0 msigdbr_7.4.1 reticulate_1.18
[49] DDRTree_0.1.5 irlba_2.3.3 VGAM_1.1-5
[52] Matrix_1.3-4 magrittr_2.0.1 RColorBrewer_1.1-2
[55] circlize_0.4.12 ComplexHeatmap_2.6.2 Cairo_1.5-12.2
[58] patchwork_1.1.1 SeuratWrappers_0.3.0 shiny_1.6.0
[61] monocle3_1.0.0 SingleCellExperiment_1.12.0 SummarizedExperiment_1.20.0 [64] GenomicRanges_1.42.0 GenomeInfoDb_1.26.2 MatrixGenerics_1.2.1
[67] matrixStats_0.58.0 AnnotationDbi_1.54.0 IRanges_2.24.1
[70] S4Vectors_0.28.1 SeuratDisk_0.0.0.9018 scales_1.1.1
[73] ggplot2_3.3.3 plyr_1.8.6 dplyr_1.0.4
[76] stringr_1.4.0 Seurat_4.0.4 SeuratObject_4.0.2
[79] Biobase_2.50.0 BiocGenerics_0.36.0

loaded via a namespace (and not attached): [1] statnet.common_4.4.1 rsvd_1.0.3 ica_1.0-2 svglite_1.2.3.2
[5] lmtest_0.9-38 crayon_1.4.1 spatstat.core_2.1-2 nlme_3.1-148
[9] qlcMatrix_0.9.7 argparse_2.0.3 GOSemSim_2.16.1 rlang_0.4.10
[13] XVector_0.30.0 ROCR_1.0-11 proto_1.0.0 rjson_0.2.20
[17] bit64_4.0.5 glue_1.4.2 pheatmap_1.0.12 sctransform_0.3.2
[21] spatstat.sparse_2.0-0 spatstat.geom_2.1-0 tidyselect_1.1.0 fitdistrplus_1.1-3
[25] XML_3.99-0.5 tidyr_1.1.2 zoo_1.8-8 chron_2.3-56
[29] xtable_1.8-4 cli_2.3.1 zlibbioc_1.36.0 rstudioapi_0.13
[33] miniUI_0.1.1.1 rjags_4-10 rpart_4.1-15 fastmatch_1.1-0
[37] lambda.r_1.2.4 treeio_1.14.4 clue_0.3-58 caTools_1.18.1
[41] tidygraph_1.2.0 KEGGREST_1.30.1 tibble_3.0.6 ape_5.4-1
[45] listenv_0.8.0 reshape_0.8.8 Biostrings_2.58.0 png_0.1-7
[49] future_1.21.0 withr_2.4.1 rle_0.9.2 bitops_1.0-6
[53] slam_0.1-48 cellranger_1.1.0 GSEABase_1.52.1 sparsesvd_0.2
[57] coda_0.19-4 pillar_1.4.7 gplots_3.1.1 GlobalOptions_0.1.2
[61] cachem_1.0.3 multcomp_1.4-16 GetoptLong_1.0.5 vctrs_0.3.6
[65] ellipsis_0.3.1 generics_0.1.0 gsubfn_0.7 tools_4.0.2
[69] feather_0.3.5 munsell_0.5.0 tweenr_1.0.1 fgsea_1.16.0
[73] emmeans_1.6.1 DelayedArray_0.16.1 fastmap_1.1.0 HSMMSingleCell_1.10.0 [77] compiler_4.0.2 abind_1.4-5 httpuv_1.5.5 plotly_4.9.3
[81] GenomeInfoDbData_1.2.4 gridExtra_2.3 edgeR_3.32.1 lattice_0.20-41
[85] deldir_0.2-9 fftw_1.0-6 later_1.1.0.1 jsonlite_1.7.2
[89] docopt_0.7.1 graph_1.68.0 tidytree_0.3.5 pbapply_1.4-3
[93] estimability_1.3 lazyeval_0.2.2 promises_1.2.0.1 R.utils_2.10.1
[97] goftest_1.2-2 spatstat.utils_2.1-0 sna_2.6 sandwich_3.0-0
[101] Rtsne_0.15 downloader_0.4 uwot_0.1.10 yaml_2.2.1
[105] survival_3.1-12 plotrix_3.8-2 systemfonts_1.0.1 htmltools_0.5.1.1
[109] memoise_2.0.0 modeltools_0.2-23 locfit_1.5-9.4 graphlayouts_0.7.1
[113] viridisLite_0.3.0 digest_0.6.27 assertthat_0.2.1 mime_0.9
[117] densityClust_0.3 futile.options_1.0.1 RSQLite_2.2.3 sqldf_0.4-11
[121] yulab.utils_0.0.2 future.apply_1.7.0 remotes_2.2.0 blob_1.2.1
[125] R.oo_1.24.0 futile.logger_1.4.3 labeling_0.4.2 fastICA_1.2-2
[129] RCurl_1.98-1.2 hms_1.0.0 colorspace_2.0-0 BiocManager_1.30.16
[133] shape_1.4.5 libcoin_1.0-8 aplot_0.1.1 coin_1.4-1
[137] RANN_2.6.1 mvtnorm_1.1-1 parallelly_1.23.0 ggridges_0.5.3
[141] lifecycle_0.2.0 formatR_1.7 leiden_0.3.7 fastcluster_1.1.25
[145] DO.db_2.9 qvalue_2.22.0 RcppAnnoy_0.0.18 TH.data_1.0-10
[149] htmlwidgets_1.5.3 polyclip_1.10-0 purrr_0.3.4 network_1.16.1
[153] shadowtext_0.0.7 gridGraphics_0.5-1 mgcv_1.8-31 globals_0.14.0
[157] codetools_0.2-16 GO.db_3.12.1 FNN_1.1.3 gtools_3.8.2
[161] gridBase_0.4-7 RSpectra_0.16-0 R.methodsS3_1.8.1 gtable_0.3.0
[165] DBI_1.1.1 ggfun_0.0.4 tensor_1.5 httr_1.4.2
[169] KernSmooth_2.23-17 stringi_1.5.3 reshape2_1.4.4 farver_2.0.3
[173] annotate_1.68.0 viridis_0.5.1 ggtree_2.4.2 combinat_0.0-8
[177] rvcheck_0.1.8 ggplotify_0.0.5 scattermore_0.7 bit_4.0.4
[181] scatterpie_0.1.5 spatstat.data_2.1-0 pkgconfig_2.0.3 babelgene_21.4
Best, Hanhuihong

jokergoo commented 3 years ago

The clustering from k-means has randomness. You need to either set the same random seed:

set.seed(123)
Heatmap(...)

or first generate the clustering result from k-means:

km = kmeans(...)$cluster
Heatmap(..., row_split = km)