jokergoo / ComplexHeatmap

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

color coding issue with alter_fun() when presenting more than 2 types of mutations #1150

Open drhmoosavi opened 5 months ago

drhmoosavi commented 5 months ago

In my matrix I have combinations of values in the mutation_colors vector seperated using " ; ". For example, I have instances of "stopgain;frameshiftdeletion" and "stopgain;frameshiftinsertion". How do I make the alter function to produce correct color coding for this combinations? It currently procuded combinations that dose not exist, for example it has made a 2-color squre for "nonsynonymousSNV+frameshiftdeletion+frameshiftinsertion", which is not in the input matrix. Also, I was wondering if I could have the entire squre filled with designated color for any of the 4 simple mutation types in the mutation_color vector, instead of having a rectangle ?

mutation_colors = c(
  "nonsynonymousSNV" = "#1D907F",  
  "frameshiftdeletion" = "#3267A5", 
  "frameshiftinsertion" = "#DEB37C",
  "stopgain" = "#7D1D67"           
)

alter_fun = list(
   background = function(x, y, w, h) {
    grid.polygon(
      unit.c(x - 0.5*w, x - 0.5*w, x + 0.5*w), 
      unit.c(y - 0.5*h, y + 0.5*h, y - 0.5*h),
      gp = gpar(fill = "grey", col = "white"))
grid.polygon(
      unit.c(x + 0.5*w, x + 0.5*w, x - 0.5*w), 
      unit.c(y + 0.5*h, y - 0.5*h, y + 0.5*h),
      gp = gpar(fill = "grey", col = "white"))
  },
  nonsynonymousSNV = function(x, y, w, h) {
    grid.polygon(
      unit.c(x - 0.5*w, x - 0.5*w, x + 0.5*w), 
      unit.c(y - 0.5*h, y + 0.5*h, y - 0.5*h),
      gp = gpar(fill = mutation_colors["nonsynonymousSNV"], col = "white"))
  },
  frameshiftinsertion = function(x, y, w, h) {
    grid.polygon(
      unit.c(x + 0.5*w, x + 0.5*w, x - 0.5*w), 
      unit.c(y + 0.5*h, y - 0.5*h, y + 0.5*h),
      gp = gpar(fill = mutation_colors["frameshiftinsertion"], col = "white"))
  },
  frameshiftdeletion = function(x, y, w, h) {
    grid.polygon(
      unit.c(x + 0.5*w, x + 0.5*w, x - 0.5*w), 
      unit.c(y + 0.5*h, y - 0.5*h, y + 0.5*h),
      gp = gpar(fill = mutation_colors["frameshiftdeletion"], col = "white"))
  },
  stopgain = function(x, y, w, h) {
    grid.polygon(
      unit.c(x + 0.5*w, x + 0.5*w, x - 0.5*w), 
      unit.c(y + 0.5*h, y - 0.5*h, y + 0.5*h),
      gp = gpar(fill = mutation_colors["stopgain"], col = "white"))
  }
)

test_alter_fun(alter_fun)
drhmoosavi commented 5 months ago

I could fix the problem but the barplot for color combontions are not shown properly now.

Here is my revised code:

mutation_colors = c(
  nonsynonymousSNV = "#196F3D",
  frameshiftdeletion = "#283593",
  frameshiftinsertion = "#EF6C00",
  stopgain = "#7D1D67",
  stopgain_stopgain = "#7D1D67",
  frameshiftdeletion_frameshiftdeletion = "#283593"
)

alter_fun = list(
  nonsynonymousSNV = alter_graphic("rect", fill = mutation_colors["nonsynonymousSNV"], col = "white", horiz_margin = unit(0,"mm"), vertical_margin = unit(0,"mm")),
  frameshiftinsertion = alter_graphic("rect", fill = mutation_colors["frameshiftinsertion"], col = "white", horiz_margin = unit(0,"mm"), vertical_margin = unit(0,"mm")),
  frameshiftdeletion = alter_graphic("rect", fill = mutation_colors["frameshiftdeletion"],col = "white", horiz_margin = unit(0,"mm"), vertical_margin = unit(0,"mm")),
  frameshiftdeletion_frameshiftdeletion = alter_graphic("rect", fill = mutation_colors["frameshiftdeletion_frameshiftdeletion"],col = "white", horiz_margin = unit(0,"mm"), vertical_margin = unit(0,"mm")),
  stopgain = alter_graphic("rect", fill = mutation_colors["stopgain"], col = "white", horiz_margin = unit(0,"mm"), vertical_margin = unit(0,"mm")),
  stopgain_stopgain = alter_graphic("rect", fill = mutation_colors["stopgain_stopgain"], col = "white", horiz_margin = unit(0,"mm"), vertical_margin = unit(0,"mm")),
  stopgain_nonsynonymousSNV = function(x, y, w, h) {
    # First mutation type (triangle)
    grid.polygon(
      unit.c(x - 0.5*w, x - 0.5*w, x + 0.5*w), 
      unit.c(y - 0.5*h, y + 0.5*h, y - 0.5*h),
      gp = gpar(fill = mutation_colors["stopgain"], col = "white")) # fill
    # Second mutation type (triangle)
    grid.polygon(
      unit.c(x + 0.5*w, x + 0.5*w, x - 0.5*w), 
      unit.c(y + 0.5*h, y - 0.5*h, y + 0.5*h),
      gp = gpar(fill = mutation_colors["nonsynonymousSNV"], col= "white", lwd =1)) # fill
  },
  stopgain_frameshiftinsertion = function(x, y, w, h) {
    # First mutation type (triangle)
    grid.polygon(
      unit.c(x - 0.5*w, x - 0.5*w, x + 0.5*w), 
      unit.c(y - 0.5*h, y + 0.5*h, y - 0.5*h),
      gp = gpar(fill = mutation_colors["stopgain"], col = "white")) # fill
    # Second mutation type (triangle)
    grid.polygon(
      unit.c(x + 0.5*w, x + 0.5*w, x - 0.5*w), 
      unit.c(y + 0.5*h, y - 0.5*h, y + 0.5*h),
      gp = gpar(fill = mutation_colors["frameshiftinsertion"], col= "white", lwd =1)) # fill
  },
  stopgain_frameshiftdeletion = function(x, y, w, h) {
    # First mutation type (triangle)
    grid.polygon(
      unit.c(x - 0.5*w, x - 0.5*w, x + 0.5*w), 
      unit.c(y - 0.5*h, y + 0.5*h, y - 0.5*h),
      gp = gpar(fill = mutation_colors["stopgain"], col = "white")) # fill
    # Second mutation type (triangle)
    grid.polygon(
      unit.c(x + 0.5*w, x + 0.5*w, x - 0.5*w), 
      unit.c(y + 0.5*h, y - 0.5*h, y + 0.5*h),
      gp = gpar(fill = mutation_colors["frameshiftdeletion"], col= "white", lwd =1)) # fill
  },
  background = function(x, y, w, h) {
    grid.rect(x, y, w, h, gp = gpar(fill = "gray80", col= "white", lwd =1))
  }
)

image