摘抄于:https://www.jianshu.com/p/2773231dfc0f

    1. #### A minimal example on how to use EnrichedHeatmap together with rtracklayer:
    2. require(EnrichedHeatmap)
    3. require(rtracklayer)
    4. require(circlize)
    5. require(data.table)
    6. ## We start from a BED file with coordinates and load as GRanges:
    7. tmp.targets <- makeGRangesFromDataFrame(
    8. df = fread("~/your.bed", header = F),
    9. seqnames.field = "V1", start.field = "V2", end.field = "V3")
    10. ## Say we want to take the peak center and extend it by 5kb in each direction:
    11. tmp.extension <- 5000
    12. ## Extend center of the peaks by tmp.extension in each direction:
    13. tmp.targets_extended <- resize(tmp.targets, fix = "center", width = tmp.extension*2)
    14. ## Now load the content of the bigwig limited to the regions we are interested in.
    15. ## This is much quicker than loading the entire bigwig and does not consume so much memory:
    16. tmp.bigwig <- rtracklayer::import("~/your.bigwig" ,
    17. format = "BigWig",
    18. selection = BigWigSelection(tmp.targets_extended))
    19. ## create the normalizedMatrix that EnrichedHeatmap accepts as input.
    20. ## We use the tmp.targets center (width=1) because from what I understand normalizeMatrix
    21. ## does not allow to turn off its extend= option. Therefore we trick it by simply
    22. ## providing the peak centers and then let the function extend it by our predefined window size.
    23. normMatrix <- normalizeToMatrix(signal = tmp.bigwig,
    24. target = resize(tmp.targets, fix = "center", width = 1),
    25. background = 0,
    26. keep = c(0, 0.99), ## minimal value to the 99th percentile
    27. target_ratio = 0,
    28. mean_mode = "w0", ## see ?EnrichedHeatmap on other options
    29. value_column = "score", ## = the name of the 4th column of the bigwig
    30. extend = tmp.extension)
    31. ## a color gradient that I personally find visually appealing, which will cover
    32. ## the range from the lowest value of normMatrix to the 99th percentile
    33. ## (99th perc. avoids extreme values skewing the heatmap):
    34. col_fun = circlize::colorRamp2(quantile(normMatrix, c(0, .99)), c("darkblue", "darkgoldenrod1"))
    35. ## heatmap function:
    36. enrHtmp <- EnrichedHeatmap( mat = normMatrix,
    37. pos_line = FALSE, ## no dashed lines around the start
    38. border = FALSE, ## no box around heatmap
    39. col = col_fun, ## color gradients from above
    40. column_title = "Nice Heatmap", ## column title
    41. column_title_gp = gpar(fontsize = 15, fontfamily = "sans"),
    42. ## these three options produce a high-quality pdf
    43. ## while keeping the file size small so that it easily fits
    44. ## nto any powerpoint presentation without crashing it
    45. use_raster = TRUE, raster_quality = 10, raster_device = "png",
    46. ## turn off background colors
    47. rect_gp = gpar(col = "transparent"),
    48. ## legend:
    49. heatmap_legend_param = list(
    50. legend_direction = "horizontal", ## legend horizontal
    51. title = "legend_title"),
    52. ## options for the profile plot on top
    53. top_annotation = HeatmapAnnotation(
    54. enriched = anno_enriched(
    55. gp = gpar(col = "black", lty = 1, lwd=2),
    56. col="black")
    57. )
    58. ) ## end of EnrichedHeatmap function
    59. ## Instead of plotting to the Rstudio device save as pdf,
    60. ## with width-2 and height-6 I personally find the heatmap visually most appealing,
    61. ## it looks good while not being too "fat":
    62. pdf("~/EnrichedHeatmap.pdf", width = 2, height = 6)
    63. ## Plot it:
    64. draw(enrHtmp, ## plot the heatmap from above
    65. heatmap_legend_side = "bottom", ## we want the legend below the heatmap
    66. annotation_legend_side = "bottom", ##
    67. padding = unit(c(4, 4, 4, 4), "mm") ## some padding to avoid labels beyond plot borders
    68. )
    69. dev.off() ## close the pdf

    出图示例:
    使用 edgerTMM 算法对 bw 文件的均一化并且根据 bigwig 文件和候选区域 bed 文件用 R 绘制热图 - 图1