R 数据可视化 —— circlize 基因组绘图函数

绘图函数

创建基因组数据的绘图区域的函数是 circos.genomicTrack(),或者 circos.genomicTrackPlotRegions()

其实用方式类似于 circos.track() 函数,可以使用 panel.fun 添加自定义的绘图函数

circos.genomicTrackPlotRegion(
  data, panel.fun = function(region, value, ...) {
    circos.genomicPoints(region, value, ...)
})

panel.fun 函数中,可以基础图形函数来添加图形,函数接收两个参数 regionvalue

  • region:包含两列起止位置的数据框
  • value:其他列信息的数据框,一般从第四列开始的数据

其中 region 的数据用于标识 x 轴,value 标识的是 y 轴。

panel.fun 函数还强制要求传入第三个参数 ...,用于传递用户不可见的变量,并交由其内部的基础绘图函数进行解析,如 circos.genomicPoints

例如,我们创建包含两列额外值的数据

> bed <- generateRandomBed(nc = 2)
> head(bed, n = 2)
   chr  start    end     value1     value2
1 chr1 121306 127516 -0.5083810 -0.7065109
2 chr1 140866 680287  0.4426682 -1.0817683

我们可以在 panel.fun 函数中,将 regionvalue 打印出来

circos.initializeWithIdeogram(plotType = NULL)
circos.genomicTrackPlotRegion(
  bed, panel.fun = function(region, value, ...) {
    if(CELL_META$sector.index == "chr1") {
      print(head(region, n = 2))
      print(head(value, n = 2))
    }
})

可以看到,region 为数据的 23 两列,value45 两列

   start    end
1 121306 127516
2 140866 680287
      value1     value2
1 -0.5083810 -0.7065109
2  0.4426682 -1.0817683

numeric.column 参数用于指定 y 轴数据,可以传递对应的列名或列索引,默认为所有数值列(从第四列开始),这些数据拥有相同的 x 轴坐标,可以使用 ylim 来设置数据范围,例如

circos.genomicTrackPlotRegion(
  data, ylim = c(0, 1),
  panel.fun = function(region, value, ...) {
    circos.genomicPoints(region, value, ...)
})
circos.genomicTrackPlotRegion(
  data, numeric.column = c("value1", "value2"), 
  panel.fun = function(region, value, ...) {
     circos.genomicPoints(region, value, ...)
})

如果输入数据是数据框列表,则 numeric.column 为长度与列表一致的向量或一个标量

1. 基础图形函数

circos.genomicPoints() 用于绘制点图,是使用 circos.points() 函数来实现的

circos.genomicPoints <- function(region, value, numeric.column = 1, ...) {
    x = (region[[2]] + region[[1]])/2
    for(i in numeric.column) {
        y = value[[i]]
        circos.points(x, y, ...)
    }
}

如果你不想使用 circos.genomic*() 类型的函数,可以使用 circos.*() 来实现。

使用方式包括

circos.genomicPoints(region, value, numeric.column = c(1, 2))
circos.genomicPoints(region, value, cex, pch)
circos.genomicPoints(region, value, sector.index, track.index)
circos.genomicTrack(data, numeric.column = 4, 
    panel.fun = function(region, value, ...) {
        circos.genomicPoints(region, value, ...)
})

其他基因组数据绘图函数也是使用对应的 circos.*() 来实现的

  • circos.genomicLines
circos.genomicLines(region, value, ...)
circos.genomicLines(region, value, numeric.column = c(1, 2))
circos.genomicLines(region, value, area, baseline, border)
circos.genomicLines(region, value, sector.index, track.index)
  • circos.genomicText
circos.genomicText(region, value, ...)
circos.genomicText(region, value, y = 1, labels)
circos.genomicText(region, value, numeric.column, labels.column)
circos.genomicText(region, value, facing, niceFacing, adj)
circos.genomicText(region, value, sector.index, track.index)
  • circos.genomicRect

因为矩形框的左右边界由 x 轴固定了,只需要设置上下边界即可,参数可以是 ytop, ybottomytop.columnybottom.column 指定对应的数据列

circos.genomicRect(region, value, ytop = 1, ybottom = 0)
circos.genomicRect(region, value, ytop.column = 2, ybottom = 0)
circos.genomicRect(region, value, col, border)
  • circos.genomicLink

需要两个数据框来确定连接区域,其他参数都由 circos.link() 解析,例如

bed1 <- generateRandomBed(nr = 100)
bed1 <- bed1[sample(nrow(bed1), 20), ]
bed2 <- generateRandomBed(nr = 100)
bed2 <- bed2[sample(nrow(bed2), 20), ]

circos.initializeWithIdeogram()
circos.genomicLink(
  bed1, bed2, border = NA,
  col = rand_color(nrow(bed1), transparency = 0.5)
)
circos.clear()

2. 绘图模式

circos.genomicTrack() 函数和 panel.fun 参数对不同的输入数据或不同的模式,会有不同的表现形式

2.1 正常模式

2.1.1 数据框

如果输入数据是数据框,绘制方式与前面一样

circos.initializeWithIdeogram()
circos.genomicTrack(
  data, numeric.column = 4, 
  panel.fun = function(region, value, ...) {
    circos.genomicPoints(region, value, col = "blue")
    # 这里的 numeric.column = 1 表示 value 的第一列,即 data 的第 4 列
    circos.genomicPoints(region, value, numeric.column = 1, col = "red")
  }
)
circos.clear()
2.1.2 数据框列表

对于数据框列表的输入数据,panel.fun 将按照当前染色体的各不同数据框进行绘制,regionvalue 表示的是当前染色体,当前数据框所对应的值。

需要在 panel.fun 函数内部使用 getI(...) 来获取当前数据框的索引。例如

circos.initializeWithIdeogram()
circos.genomicTrack(
  bed_list,
  panel.fun = function(region, value, ...) {
    i = getI(...)
    circos.genomicPoints(region, value, col = rand_color(1), ...)
  })

# column 4 in the first bed and column 5 in the second bed
circos.genomicTrack(
  bed_list,
  numeric.column = c(4, 5),
  panel.fun = function(region, value, ...) {
    i = getI(...)
    circos.genomicPoints(region, value, col = rand_color(1), ...)
  }
)
circos.clear()

2.2 堆叠模式

circos.genomicTrack() 函数中设置 stack = TRUE,开启堆叠模式。

在堆叠模式下,ylim 将会被重新定义,y 轴将会被分割为一些高度相同的 biny = 1, 2, ...),每个 bin 内放置对应的图形

2.2.1 数据框

如果在堆叠模式下输入数据是包含多列数值列的数据框,则 numeric.column 所指定的每个数值列都会作为一个单元,ylim 被设置为 (0.5,0.5+n)n 为数值列的数目。y 轴的值 value 将会被替换为 y=i

例如

data <- generateRandomBed(nr = 100, nc = 2)
circos.initializeWithIdeogram()
circos.genomicTrack(
  data, stack = TRUE,
  panel.fun = function(region, value, ...) {
    i = getI(...)
    circos.genomicPoints(region, value, col = rand_color(1), ...)
  }
)
circos.clear()
2.2.2 数据框列表

如果输入的是数据框列表,则每个数据框被认为是一个单元,ylim 被重定义为 (0.5,0.5+n)n 为数据框列表的长度。

panel.fun 将会应用在每个数据框中,

circos.initializeWithIdeogram()
circos.genomicTrack(
  bed_list, stack = TRUE,
  panel.fun = function(region, value, ...) {
    i = getI(...)
    circos.genomicPoints(region, value, col = rand_color(1), ...)
  })

circos.clear()

3 应用

3.1. 点图

为了更容易看出图形的区别,我们只显示一条染色体,并将其绘制成 1/4

circos.par(
  "track.height" = 0.1, start.degree = 90,
  canvas.xlim = c(0, 1), canvas.ylim = c(0, 1), 
  gap.degree = 270
)
circos.initializeWithIdeogram(
  chromosome.index = "chr1", plotType = NULL
)

添加轨迹 A,只绘制点

bed <- generateRandomBed(nr = 300)

circos.genomicTrack(
  bed, panel.fun = function(region, value, ...) {
  circos.genomicPoints(region, value, pch = 16, cex = 0.5, ...)
  circos.text(
      CELL_META$cell.xlim[1], 
      mean(CELL_META$cell.ylim), 
      'A', adj = c(1.05, 0.5)
    )
  }
)

添加轨迹 B,将点以 stack 模式排列,并添加一条虚线

circos.genomicTrack(
  bed, stack = TRUE, 
  panel.fun = function(region, value, ...) {
    circos.genomicPoints(region, value, pch = 16, cex = 0.5, ...)
    i = getI(...)
    circos.lines(CELL_META$cell.xlim, c(i, i), 
                 lty = 2, col = "#00000040")
    circos.text(
      CELL_META$cell.xlim[1], 
      mean(CELL_META$cell.ylim), 
      'B', adj = c(1.05, 0.5)
    )
  }
)

添加轨迹 C,使用数据框列表,两个数据框的点设置不同的颜色

bed1 <- generateRandomBed(nr = 300)
bed2 <- generateRandomBed(nr = 300)
bed_list <- list(bed1, bed2)
circos.genomicTrack(
  bed_list, 
  panel.fun = function(region, value, ...) {
    i = getI(...)
    circos.genomicPoints(
      region, value, pch = 16,
      cex = 0.5, col = rand_color(1), ...
    )
    circos.text(
      CELL_META$cell.xlim[1], 
      mean(CELL_META$cell.ylim), 
      'C', adj = c(1.05, 0.5)
    )
  }
)

添加轨迹 D,为数据框列表使用堆积的方式

circos.genomicTrack(
  bed_list, stack = TRUE, 
  panel.fun = function(region, value, ...) {
    i = getI(...)
    circos.genomicPoints(
      region, value, pch = 16,
      cex = 0.5, col = rand_color(1), ...
    )
    circos.lines(
      CELL_META$cell.xlim, c(i, i), lty = 2, 
      col = "grey50"
    )
    circos.text(
      CELL_META$cell.xlim[1], 
      mean(CELL_META$cell.ylim), 
      'D', adj = c(1.05, 0.5)
    )
  }
)

添加轨迹 E,数据框包含 4 列数值数据,每列数据拥有相同的 x 轴坐标,并设置不同的颜色

bed <- generateRandomBed(nr = 300, nc = 4)
circos.genomicTrack(
  bed, panel.fun = function(region, value, ...) {
    circos.genomicPoints(
      region, value, pch = 16,
      cex = 0.5, col = 1:4, ...
    )
    circos.text(
      CELL_META$cell.xlim[1], 
      mean(CELL_META$cell.ylim), 
      'E', adj = c(1.05, 0.5)
    )
  }
)

添加轨迹 F,堆叠方式显示 4 列数据

bed <- generateRandomBed(nr = 300, nc = 4)
circos.genomicTrack(
  bed, stack = TRUE, 
  panel.fun = function(region, value, ...) {
    i = getI(...)
    circos.genomicPoints(
      region, value, pch = 16,
      cex = 0.5, col = i, ...
    )
    circos.lines(
      CELL_META$cell.xlim, c(i, i), 
      lty = 2, col = "grey50"
    )
    circos.text(
      CELL_META$cell.xlim[1], 
      mean(CELL_META$cell.ylim), 
      'F', adj = c(1.05, 0.5)
    )
  })
circos.clear()

3.2 线

类似于上面的点图,我们也只用 1/4 圆来绘制 chr1

circos.par(
  "track.height" = 0.08, start.degree = 90,
  canvas.xlim = c(0, 1), canvas.ylim = c(0, 1), 
  gap.degree = 270,
  cell.padding = c(0, 0, 0, 0)
)

circos.initializeWithIdeogram(
  chromosome.index = "chr1", plotType = NULL
)

轨迹 A,绘制简单折线,折线的点为区间中点

bed <- generateRandomBed(nr = 500)
circos.genomicTrack(
  bed, panel.fun = function(region, value, ...) {
    circos.genomicLines(region, value)
    circos.text(
      CELL_META$cell.xlim[1], 
      mean(CELL_META$cell.ylim), 
      'A', adj = c(1.05, 0.5)
    )
  }
)

轨迹 B,面积折线图,轨迹 Ch 类型

circos.genomicTrack(
  bed,  panel.fun = function(region, value, ...) {
    circos.genomicLines(region, value, area = TRUE)
    circos.text(
      CELL_META$cell.xlim[1], 
      mean(CELL_META$cell.ylim), 
      'B', adj = c(1.05, 0.5)
    )
  }
)
circos.genomicTrack(
  bed, panel.fun = function(region, value, ...) {
    circos.genomicLines(region, value, type = "h")
    circos.text(
      CELL_META$cell.xlim[1], 
      mean(CELL_META$cell.ylim), 
      'C', adj = c(1.05, 0.5)
    )
  }
)

轨迹 D,用数据框列表绘制分组折线图,每组为一个数据框

bed1 <- generateRandomBed(nr = 500)
bed2 <- generateRandomBed(nr = 500)
bed_list <- list(bed1, bed2)
circos.genomicTrack(
  bed_list, 
  panel.fun = function(region, value, ...) {
    i = getI(...)
    circos.genomicLines(
      region, value, col = rand_color(1), ...
    )
    circos.text(
      CELL_META$cell.xlim[1], 
      mean(CELL_META$cell.ylim), 
      'D', adj = c(1.05, 0.5)
    )
  }
)

轨迹 E,使用堆叠的方式绘制数据框列表

circos.genomicTrack(
  bed_list, stack = TRUE, 
  panel.fun = function(region, value, ...) {
    i = getI(...)
    circos.genomicLines(region, value, col = i, ...)
    circos.text(
      CELL_META$cell.xlim[1], 
      mean(CELL_META$cell.ylim), 
      'E', adj = c(1.05, 0.5)
    )
  }
)

轨迹 F,包含 4 列的数据框,绘制分组折线图,每组表示一列

bed <- generateRandomBed(nr = 500, nc = 4)
circos.genomicTrack(
  bed, 
  panel.fun = function(region, value, ...) {
    circos.genomicLines(region, value, col = 1:4, ...)
    circos.text(
      CELL_META$cell.xlim[1], 
      mean(CELL_META$cell.ylim), 
      'F', adj = c(1.05, 0.5)
    )
  }
)

轨迹 G,堆叠的数据框

circos.genomicTrack(
  bed, stack = TRUE, 
  panel.fun = function(region, value, ...) {
    i = getI(...)
    circos.genomicLines(region, value, col = i, ...)
    circos.text(
      CELL_META$cell.xlim[1], 
      mean(CELL_META$cell.ylim), 
      'G', adj = c(1.05, 0.5)
    )
  }
)

轨迹 H,绘制 segment 类型的线

bed <- generateRandomBed(nr = 200)
circos.genomicTrack(
  bed, 
  panel.fun = function(region, value, ...) {
    circos.genomicLines(
      region, value, type = "segment",
      lwd = 2, col = rand_color(nrow(region)),
      ...
    )
    circos.text(
      CELL_META$cell.xlim[1], 
      mean(CELL_META$cell.ylim), 
      'H', adj = c(1.05, 0.5)
    )
  })
circos.clear()

3.3 矩形

由于矩阵的颜色表示的值大小,我们定义连续型颜色映射

circos.par(
  "track.height" = 0.15, start.degree = 90,
  canvas.xlim = c(0, 1), canvas.ylim = c(0, 1), 
  gap.degree = 270
)
circos.initializeWithIdeogram(
  chromosome.index = "chr1", plotType = NULL
)
col_fun <- colorRamp2(
  breaks = c(-1, 0, 1), 
  colors = c("#ef8a62", "#f7f7f7", "#67a9cf")
)

如果要绘制热图,可以设置 stack 模式

bed <- generateRandomBed(nr = 100, nc = 4)
circos.genomicTrack(
  bed, stack = TRUE, 
  panel.fun = function(region, value, ...) {
    circos.genomicRect(
      region, value,
      col = col_fun(value[[1]]),
      border = NA, ...
    )
    circos.text(
      CELL_META$cell.xlim[1], 
      mean(CELL_META$cell.ylim), 
      'A', adj = c(1.05, 0.5)
    )
  }
)

在轨迹 B 中,使用数据框列表的堆叠模式

bed1 <- generateRandomBed(nr = 100)
bed2 <- generateRandomBed(nr = 100)
bed_list <- list(bed1, bed2)
circos.genomicTrack(
  bed_list, stack = TRUE, 
  panel.fun = function(region, value, ...) {
    i = getI(...)
    circos.genomicRect(
      region, value, ytop = i + 0.3,
      ybottom = i - 0.3,
      col = col_fun(value[[1]]),
      ...
    )
    circos.text(
      CELL_META$cell.xlim[1], 
      mean(CELL_META$cell.ylim), 
      'B', adj = c(1.05, 0.5)
    )
  }
)

在轨迹 C 中,我们使用正常的模式实现类似的功能

circos.genomicTrack(
  bed_list, ylim = c(0.5, 2.5), 
  panel.fun = function(region, value, ...) {
    i = getI(...)
    circos.genomicRect(
      region, value, ytop = i + 0.3,
      ybottom = i - 0.3,
      col = col_fun(value[[1]]),
      ...
    )
    circos.text(
      CELL_META$cell.xlim[1], 
      mean(CELL_META$cell.ylim), 
      'C', adj = c(1.05, 0.5)
    )
  }
)

轨迹 D,我们可以设置条形的高度,ytop.column = 1 表示 value 的第一列

bed <- generateRandomBed(nr = 200)
circos.genomicTrack(
  bed, 
  panel.fun = function(region, value, ...) {
    circos.genomicRect(
      region, value, ytop.column = 1,
      ybottom = 0,
      col = ifelse(value[[1]] > 0, "#ef8a62", "#67a9cf"),
      ...
    )
    circos.lines(CELL_META$cell.xlim, c(0, 0), lty = 2, col = "grey50")
    circos.text(
      CELL_META$cell.xlim[1], 
      mean(CELL_META$cell.ylim), 
      'D', adj = c(1.05, 0.5)
    )
  }
)
circos.clear()
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容