作者:Zuguang Gu8
    翻译:Steven Shen
    原文:https://jokergoo.github.io/ComplexHeatmap-reference/book/oncoprint.html#define-the-alter-fun


    alter_fun is a list of functons which add graphics layer by layer (i.e. first draw for snv, then for indel). Graphics can also be added in a grid-by-grid style by specifying alter_fun as a single function. The difference from the function list is now alter_fun should accept a fifth argument which is a logical vector. This logical vector shows whether different alterations exist for current gene in current sample.

    Let’s assume in a grid there is only snv event, then v for this grid is:

    1. ## snv indel
    2. ## TRUE FALSE
    3. oncoPrint(mat,
    4. alter_fun = function(x, y, w, h, v) {
    5. if(v["snv"]) grid.rect(x, y, w*0.9, h*0.9, # v["snv"] is a logical value
    6. gp = gpar(fill = col["snv"], col = NA))
    7. if(v["indel"]) grid.rect(x, y, w*0.9, h*0.4, # v["indel"] is a logical value
    8. gp = gpar(fill = col["indel"], col = NA))
    9. }, col = col)

    7.1.2 定义 alter_fun() - 图1

    If alter_fun is set as a single function, customization can be more flexible. In following example, the blue rectangles can have different height in different grid.

    1. oncoPrint(mat,
    2. alter_fun = function(x, y, w, h, v) {
    3. n = sum(v) # how many alterations for current gene in current sample
    4. h = h*0.9
    5. # use `names(which(v))` to correctly map between `v` and `col`
    6. if(n) grid.rect(x, y - h*0.5 + 1:n/n*h, w*0.9, 1/n*h,
    7. gp = gpar(fill = col[names(which(v))], col = NA), just = "top")
    8. }, col = col)

    7.1.2 定义 alter_fun() - 图2

    Following is a complicated example for alter_fun where triangles are used:

    1. oncoPrint(mat,
    2. alter_fun = list(
    3. background = function(x, y, w, h) {
    4. grid.polygon(
    5. unit.c(x - 0.5*w, x - 0.5*w, x + 0.5*w),
    6. unit.c(y - 0.5*h, y + 0.5*h, y - 0.5*h),
    7. gp = gpar(fill = "grey", col = "white"))
    8. grid.polygon(
    9. unit.c(x + 0.5*w, x + 0.5*w, x - 0.5*w),
    10. unit.c(y + 0.5*h, y - 0.5*h, y + 0.5*h),
    11. gp = gpar(fill = "grey", col = "white"))
    12. },
    13. snv = function(x, y, w, h) {
    14. grid.polygon(
    15. unit.c(x - 0.5*w, x - 0.5*w, x + 0.5*w),
    16. unit.c(y - 0.5*h, y + 0.5*h, y - 0.5*h),
    17. gp = gpar(fill = col["snv"], col = "white"))
    18. },
    19. indel = function(x, y, w, h) {
    20. grid.polygon(
    21. unit.c(x + 0.5*w, x + 0.5*w, x - 0.5*w),
    22. unit.c(y + 0.5*h, y - 0.5*h, y + 0.5*h),
    23. gp = gpar(fill = col["indel"], col = "white"))
    24. }
    25. ), col = col)

    7.1.2 定义 alter_fun() - 图3

    In some cases, you might need to define alter_fun for many alteration types. If you are not sure about the visual effect of your alter_fun, you can use test_alter_fun() to test your alter_fun. In following example, we defined seven alteration functions:

    1. alter_fun = list(
    2. mut1 = function(x, y, w, h)
    3. grid.rect(x, y, w, h, gp = gpar(fill = "red", col = NA)),
    4. mut2 = function(x, y, w, h)
    5. grid.rect(x, y, w, h, gp = gpar(fill = "blue", col = NA)),
    6. mut3 = function(x, y, w, h)
    7. grid.rect(x, y, w, h, gp = gpar(fill = "yellow", col = NA)),
    8. mut4 = function(x, y, w, h)
    9. grid.rect(x, y, w, h, gp = gpar(fill = "purple", col = NA)),
    10. mut5 = function(x, y, w, h)
    11. grid.rect(x, y, w, h, gp = gpar(fill = NA, lwd = 2)),
    12. mut6 = function(x, y, w, h)
    13. grid.points(x, y, pch = 16),
    14. mut7 = function(x, y, w, h)
    15. grid.segments(x - w*0.5, y - h*0.5, x + w*0.5, y + h*0.5, gp = gpar(lwd = 2))
    16. )
    17. test_alter_fun(alter_fun)
    18. ## `alter_fun` is defined as a list of functions.
    19. ## Functions are defined for following alteration types:
    20. ## mut1, mut2, mut3, mut4, mut5, mut6, mut7

    7.1.2 定义 alter_fun() - 图4

    For the combination of alteration types, test_alter_fun() randomly samples some of them.

    test_alter_fun() works both for alter_fun as a list and as a single function.