jokergoo / ComplexHeatmap

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

Cannot adjust the padding between the rows annotation and oncoprint #1197

Open xieduo7 opened 4 months ago

xieduo7 commented 4 months ago

Dear all,

This is my code:

library(maftools)

#path to TCGA LAML MAF file
laml.maf = system.file('extdata', 'tcga_laml.maf.gz', package = 'maftools')
#clinical information containing survival information and histology. This is optional
laml.clin = system.file('extdata', 'tcga_laml_annot.tsv', package = 'maftools')

laml = read.maf(maf = laml.maf, clinicalData = laml.clin)

oncoplot(maf = laml, top = 20,writeMatrix=TRUE)

data_matrix <- read.table('onco_matrix.txt', header=TRUE, sep="\t", check.names=FALSE,row.names=1)
data_matrix <- as.matrix(data_matrix)
print(data_matrix)
data_matrix <- data_matrix[,1:70]
unique_values <- unique(as.vector(data_matrix))
print(unique_values)
library(ComplexHeatmap)
alter_fun = list(
  background = function(x, y, w, h)
    grid.rect(x, y, w*0.9, h*0.9, gp = gpar(fill = "#eeeeee", col = NA)),
  #grid.rect(x, y, w*0.9, h*0.9, gp = gpar(fill = "white", col = NA)),

  # red rectangles
  Missense_Mutation = function(x, y, w, h)
    grid.rect(x, y, w*0.9, h*0.9, gp = gpar(fill = "#3a6fb0", col = NA)),
  # blue rectangles
  Frame_Shift_Ins = function(x, y, w, h)
    grid.rect(x, y, w*0.9, h*0.9, gp = gpar(fill = "#45894d", col = NA)),
  In_Frame_Ins = function(x, y, w, h)
    grid.rect(x, y, w*0.9, h*0.9, gp = gpar(fill = "#dc4b39", col = NA)),
  In_Frame_Del = function(x, y, w, h)
    grid.rect(x, y, w*0.9, h*0.9, gp = gpar(fill = "#932ce7", col = NA)),
  Frame_Shift_Del = function(x, y, w, h)
    grid.rect(x, y, w*0.9, h*0.9, gp = gpar(fill = "#7aacd2", col = NA)),
  Splice_Site = function(x, y, w, h)
    grid.rect(x, y, w*0.9, h*0.9, gp = gpar(fill = "#f2bea5", col = NA)),
  Nonsense_Mutation = function(x, y, w, h){
    grid.rect(x, y, w*0.9, h*0.9, gp = gpar(fill = "#add7a1", col = NA))
    grid.points(x, y, pch = 4,gp = gpar(fill = NA, col = "white"),size = unit(0.15, "char"))
    #grid.segments(x - w*0.4, y - h*0.4, x + w*0.4, y + h*0.4, gp = gpar(lwd = 2))
    #grid.segments(x + w*0.4, y - h*0.4, x - w*0.4, y + h*0.4, gp = gpar(lwd = 2))
    },

  Multi_Hit = function(x, y, w, h) {
    grid.rect(x, y, w*0.9, h*0.9, gp = gpar(fill = "black", col = NA))
    grid.points(x, y, pch = 16,gp = gpar(fill = NA, col = "white"),size = unit(0.2, "char"))
    }
)
columns_to_keep <- !apply(data_matrix, 2, function(x) all(x == "0"))
filtered_matrix <- data_matrix[, columns_to_keep]
pdf('oncoprint_test.pdf',height=2.5,width=4.5)
print(dim(filtered_matrix))
ht_opt
ht_opt$ROW_ANNO_PADDING <- -1*unit(0.1, "mm")
ht_opt$HEATMAP_LEGEND_PADDING <- unit(0.1, "mm")
ht_opt$ANNOTATION_LEGEND_PADDING <- unit(0.1, "mm")
ht_opt$COLUMN_ANNO_PADDING <- unit(0.9, "mm")
ht_opt
ht <- oncoPrint(filtered_matrix, alter_fun = alter_fun, col = c(Missense_Mutation = "#3a6fb0", Frame_Shift_Ins = "#45894d",In_Frame_Ins="#dc4b39",In_Frame_Del="#932ce7",Frame_Shift_Del="#7aacd2",Splice_Site="#f2bea5",Nonsense_Mutation="#add7a1",Multi_Hit="black"),
width = ncol(filtered_matrix)*unit(1.25, "mm"),
height = nrow(filtered_matrix)*unit(1.25, "mm"),
row_names_gp = gpar(fontface = "italic",fontsize =5),
pct_gp=gpar(fontsize =5),
#show_heatmap_legend=FALSE,
right_annotation=rowAnnotation(rbar = anno_oncoprint_barplot(width = 6*unit(1, "mm"),axis_param = list(gp= gpar(fontsize= 5,lwd=0.5),side = "top"))),
top_annotation=HeatmapAnnotation(cbar = anno_oncoprint_barplot(height = 6*unit(1, "mm"),axis_param = list(gp= gpar(fontsize= 5,lwd=0.5)))),
heatmap_legend_param= list(
    labels=c("Missense_Mutation","Frame_Shift_Ins","In_Frame_Ins","In_Frame_Del","Frame_Shift_Del","Splice_Site","Nonsense_Mutation","Multi_Hit"),
    labels_gp = gpar(fontsize = 5),
#   legend_height=unit(1, "mm")*8
    title = "",
    nrow=2,
    grid_height = 1*unit(1, "mm"),
    grid_width = 1*unit(1, "mm")
    )
)
draw(ht, heatmap_legend_side = "bottom", annotation_legend_side = "bottom")
decorate_annotation("cbar", { # the column name has the name "cbar"
    grid.text("Number\nof mutations", x = unit(-4, "mm"), rot = 90, just = "bottom",gp = gpar(fontsize=5))
})
decorate_annotation("rbar", { # the column name has the name "cbar"
    grid.text("Number\nof tumors", y = unit(1, "npc") + unit(4, "mm"), gp = gpar(fontsize=5),just = "bottom")
})

ht_opt
dev.off()

This is my output plot:

image

I wanted to set the space between the column/row annotations and the heatmap using

ht_opt$ROW_ANNO_PADDING <- -1*unit(0.1, "mm")
ht_opt$HEATMAP_LEGEND_PADDING <- unit(0.1, "mm")
ht_opt$ANNOTATION_LEGEND_PADDING <- unit(0.1, "mm")
ht_opt$COLUMN_ANNO_PADDING <- unit(0.9, "mm")

But it didn't change.

Thank you, Duo