轨迹函数
1. Ideograms
在前面的例子中,我们使用 circos.initializeWithIdeogram()
函数来初始化圆形绘图并添加染色体图形(ideogram
)。
事实上,ideogram
图形是由 circos.genomicIdeogram()
函数来绘制的,我们可以使用该函数在任意位置添加 ideogram
。例如
circos.initializeWithIdeogram(plotType = c("labels", "axis"))
circos.track(ylim = c(0, 1))
circos.genomicIdeogram() # put ideogram as the third track
circos.genomicIdeogram(track.height = 0.2)
circos.clear()
默认绘制人类 hg19 基因组
2. 热图
circos.genomicHeatmap()
函数可以绘制基因组区域热图,使用连接线来连接基因组区域和对应的热图
默认会使用所有的数值列来绘制热图,可以使用 numeric.column
参数来指定绘制热图的列。例如
circos.initializeWithIdeogram()
bed <- generateRandomBed(nr = 100, nc = 4)
col_fun <- colorRamp2(
c(-1, 0, 1),
c("#ef8a62", "#f7f7f7", "#67a9cf")
)
circos.genomicHeatmap(
bed, col = col_fun, side = "inside",
border = "white"
)
circos.clear()
将热图放置在外部,connection_height
和 heatmap_height
分别用于设置连接线和热图的高度(相对于单位圆)
circos.initializeWithIdeogram(plotType = NULL)
circos.genomicHeatmap(
bed, col = col_fun, side = "outside",
connection_height = 0.2,
heatmap_height = 0.2,
line_col = as.numeric(factor(bed[[1]]))
)
circos.genomicIdeogram()
circos.clear()
3. 标签
circos.genomicLabels()
函数用于设置基因组区域的文本标签,标签的位置会自动调整,避免重叠
与 circos.genomicHeatmap()
类似,也会添加两个轨迹,并使用连接线来连接基因组区域对应的标签
circos.initializeWithIdeogram()
bed <- generateRandomBed(
nr = 50, fun = function(k) sample(letters, k, replace = TRUE)
)
bed[1, 4] <- "aaaaa"
circos.genomicLabels(
bed, labels.column = 4, side = "inside",
col = "#ef8a62"
)
circos.clear()
也可以使标签朝外,padding
参数设置标签之间的间距
circos.initializeWithIdeogram(plotType = NULL)
circos.genomicLabels(
bed, labels.column = 4, side = "outside",
col = as.numeric(factor(bed[[1]])),
line_col = as.numeric(factor(bed[[1]])),
labels_height = max(strwidth(bed$value1)),
padding = 0.1
)
circos.genomicIdeogram()
circos.clear()
4. 轴
circos.genomicAxis()
函数可以在任意位置添加基因组轴线,例如
circos.initializeWithIdeogram(plotType = NULL)
circos.genomicIdeogram()
circos.track(
track.index = get.current.track.index(),
panel.fun = function(x, y) {
circos.genomicAxis(h = "top")
}
)
circos.track(ylim = c(0, 1), track.height = 0.1)
circos.track(
track.index = get.current.track.index(),
panel.fun = function(x, y) {
circos.genomicAxis(h = "bottom", direction = "inside")
}
)
circos.clear()
5. 密度图和雨量图
雨量图可以用于可视化基因组的区域分布,有助于鉴别不同类型的区域。在雨量图中,点代表区域,x
轴为区间在基因组中的位置,y
轴为该区域与相邻两个区域之间的最小距离(log10
转换)
可以使用 rainfallTransform
来计算相邻区域距离
> head(rainfallTransform(DMR_hyper))
chr start end dist
70 chr1 933445 934443 35323
104 chr1 969766 970362 4909
105 chr1 975271 976767 4909
154 chr1 1108819 1109923 31522
155 chr1 1141445 1142405 31522
157 chr1 1181550 1182782 39145
基因组区域密度图,可以展示一些相同大小的交叠窗口内包含的 bed
区域的比例,可以使用 genomicDensity
计算密度值,例如大小为 1000000
的窗口
> head(genomicDensity(DMR_hyper, window.size = 1e6))
chr start end value
1 chr1 1 1000000 0.003093
2 chr1 500001 1500000 0.007592
3 chr1 1000001 2000000 0.008848
4 chr1 1500001 2500000 0.010155
5 chr1 2000001 3000000 0.011674
6 chr1 2500001 3500000 0.007783
设置 count_by = "number"
,计算的是区域的数目
> head(genomicDensity(
> DMR_hyper, window.size = 1e6,
> count_by = "number")
> )
chr start end value
1 chr1 1 1000000 3
2 chr1 500001 1500000 7
3 chr1 1000001 2000000 7
4 chr1 1500001 2500000 7
5 chr1 2000001 3000000 9
6 chr1 2500001 3500000 7
我们可以使用 genomicRainfall
来绘制差异甲基化区域(DMR
)雨量图(超甲基化和低甲基化两类),并使用 genomicDensity
绘制这些区域的密度
load(system.file(package = "circlize", "extdata", "DMR.RData"))
circos.initializeWithIdeogram(chromosome.index = paste0("chr", 1:22))
bed_list <- list(DMR_hyper, DMR_hypo)
circos.genomicRainfall(
bed_list, pch = 16, cex = 0.4,
col = c("#ef8a6280", "#67a9cf80")
)
circos.genomicDensity(
DMR_hyper, col = c("#ef8a6280"), track.height = 0.1
)
circos.genomicDensity(
DMR_hypo, col = c("#67a9cf80"), track.height = 0.1
)
circos.clear()
嵌套缩放
在之前的章节中,我们介绍了如何在同一个轨迹中缩放某一区域,这种方式只适用于缩放区域较少的情况下,如果缩放的区域较多,那就需要另一种方式了,将缩放区域放置在一个新的轨迹中
首先,我们先构建一份随机数据
df <- data.frame(
cate = sample(letters[1:8], 400, replace = TRUE),
x = runif(400),
y = runif(400),
stringsAsFactors = FALSE
)
df <- df[order(df[[1]], df[[2]]), ]
rownames(df) <- NULL
df$interval_x <- as.character(cut(df$x, c(0, 0.2, 0.4, 0.6, 0.8, 1.0)))
df$name <- paste(df$cate, df$interval_x, sep = ":")
df$start <- as.numeric(
gsub("^\\((\\d(\\.\\d)?).*(\\d(\\.\\d)?)]", "\\1", df$interval_x)
)
df$end <- as.numeric(
gsub("^\\((\\d(\\.\\d)?),(\\d(\\.\\d)?)]$", "\\3",
df$interval_x)
)
nm <- sample(unique(df$name), 20)
df2 <- df[df$name %in% nm, ]
correspondance <- unique(df2[, c("cate", "start", "end", "name", "start", "end")])
zoom_sector <- unique(df2[, c("name", "start", "end", "cate")])
zoom_data <- df2[, c("name", "x", "y")]
data <- df[, 1:3]
sector <- data.frame(
cate = letters[1:8], start = 0, end = 1,
stringsAsFactors = FALSE
)
# 配置颜色
sector_col <- structure(
rand_color(8, transparency = 0.5),
names = letters[1:8]
)
其中,sector
包含所有扇形的名称和坐标
> head(sector, n = 4)
cate start end
1 a 0 1
2 b 0 1
3 c 0 1
4 d 0 1
data
包含数据点
> head(data, n = 4)
cate x y
1 a 0.009543345 0.7056003
2 a 0.042504896 0.2901097
3 a 0.066839552 0.9832001
4 a 0.084935793 0.7265302
在每个扇形中,随机抽取某些区间用于缩放,存储在 zoom_sector
中
> head(zoom_sector, n = 4)
name start end cate
8 a:(0.2,0.4] 0.2 0.4 a
61 b:(0.4,0.6] 0.4 0.6 b
96 c:(0.2,0.4] 0.2 0.4 c
109 c:(0.4,0.6] 0.4 0.6 c
每个缩放区域需要唯一的名称,可以组合区间和扇形名称,zoom_data
为对应的数据
> head(zoom_data, n = 4)
name x y
8 a:(0.2,0.4] 0.2175062 0.7969979
9 a:(0.2,0.4] 0.2331386 0.3014023
10 a:(0.2,0.4] 0.3334241 0.4342164
11 a:(0.2,0.4] 0.3400818 0.2409061
原始数据与缩放区间的映射关系为 correspondance
> head(correspondance, n = 4)
cate start end name start.1 end.1
8 a 0.2 0.4 a:(0.2,0.4] 0.2 0.4
61 b 0.4 0.6 b:(0.4,0.6] 0.4 0.6
96 c 0.2 0.4 c:(0.2,0.4] 0.2 0.4
109 c 0.4 0.6 c:(0.4,0.6] 0.4 0.6
要绘制缩放图,需要使用 circos.nested()
函数,该函数接受两个绘图函数,其中一个用于绘制原始数据圆形图,另一个用于绘制缩放区域圆形图,circos.nested()
会自动调整位置并添加连接线
我们只需将绘图代码块封装成函数即可,例如
# 绘制原始数据的圆形图
f1 <- function() {
circos.par(gap.degree = 10)
circos.initialize(sector[, 1], xlim = sector[, 2:3])
circos.track(
data[[1]], x = data[[2]], y = data[[3]],
ylim = c(0, 1), panel.fun = function(x, y) {
circos.points(x, y, pch = 16, cex = 0.5, col = "red")
}
)
}
# 绘制缩放数据的圆形图
f2 <- function() {
circos.par(gap.degree = 2, cell.padding = c(0, 0, 0, 0))
circos.initialize(
zoom_sector[[1]], xlim = as.matrix(zoom_sector[, 2:3])
)
circos.track(
zoom_data[[1]], x = zoom_data[[2]], y = zoom_data[[3]],
panel.fun = function(x, y) {
circos.points(x, y, pch = 16, cex = 0.5, col = "blue")
}
)
}
最后,将两个函数及映射关系传递给 circos.nested()
来绘制图片
circos.nested(f1, f2, correspondance)
在图中,缩放数据绘制在原始数据的内侧,通过调换 f1
和 f2
的顺序,同时调整映射关系的顺序,可以让缩放数据放置在外侧
circos.nested(f2, f1, correspondance[, c(4:6, 1:3)])
circos.nested
函数只是绘制两个圆形图,并根据映射关系添加连接线,而不关心哪个是原始数据。
在使用缩放时,有以下几点需要注意:
- 它只能应用于整个圆
- 如果在第一幅图中设置了
canvas.xlim
和canvas.ylim
,,在第二幅图中也要设置同样的值 - 默认情况下,第二幅图的起始位置会自动调整,也可以使用
circos.par("start.degree" = ...)
来指定,但是同时要在circos.nested()
函数中设置adjust_start_degree = TRUE
- 不要在绘图函数末尾添加
circos.clear()
我们可以在两个绘图函数中添加更加复杂的图形,例如,为缩放区域添加背景色
sector_col <- structure(paste0(brewer.pal(8, "Set2"), "80"), names = letters[1:8])
f1 <- function() {
circos.par(gap.degree = 10)
circos.initialize(sector[, 1], xlim = sector[, 2:3])
circos.track(
data[[1]], x = data[[2]], y = data[[3]], ylim = c(0, 1),
panel.fun = function(x, y) {
l = correspondance[[1]] == CELL_META$sector.index
# 添加缩放区域背景色
if (sum(l)) {
for (i in which(l)) {
circos.rect(
correspondance[i, 2],
CELL_META$cell.ylim[1],
correspondance[i, 3],
CELL_META$cell.ylim[2],
col = sector_col[CELL_META$sector.index],
border = sector_col[CELL_META$sector.index]
)
}
}
# 添加点
circos.points(x, y, pch = 16, cex = 0.5)
# 添加扇形标签
circos.text(
CELL_META$xcenter,
CELL_META$ylim[2] + mm_y(2),
CELL_META$sector.index,
niceFacing = TRUE,
adj = c(0.5, 0)
)
})
}
f2 <- function() {
circos.par(gap.degree = 2, cell.padding = c(0, 0, 0, 0))
circos.initialize(zoom_sector[[1]], xlim = as.matrix(zoom_sector[, 2:3]))
circos.track(
zoom_data[[1]], x = zoom_data[[2]], y = zoom_data[[3]],
panel.fun = function(x, y) {
circos.points(x, y, pch = 16, cex = 0.5)
},
# 设置背景色
bg.col = sector_col[zoom_sector$cate],
track.margin = c(0, 0)
)
}
# 设置连接颜色
circos.nested(f1, f2, correspondance, connection_col = sector_col[correspondance[[1]]])
差异甲基化区域可视化
Tagmentation-based whole-genome bisulfite sequencing
(T-WGBS
) 技术可以检测小范围的甲基化,我们使用来 circlize
来展示从 T-WGBS
数据中检测的 DMR
首先,导入数据
load(system.file(
package = "circlize", "extdata",
"tagments_WGBS_DMR.RData")
)
其中,tagments
表示检测的区域,DMR1
表示检测区域中的 DMR
,correspondance
表示的是映射关系
> head(tagments, n = 4)
tagments start end chr
1 chr1-44876009-45016546 44876009 45016546 chr1
2 chr1-90460304-90761641 90460304 90761641 chr1
3 chr1-211666507-211692757 211666507 211692757 chr1
4 chr2-46387184-46477385 46387184 46477385 chr2
> head(DMR1, n = 4)
chr start end methDiff
1 chr1-44876009-45016546 44894352 44894643 -0.2812889
2 chr1-44876009-45016546 44902069 44902966 -0.3331170
3 chr1-90460304-90761641 90535428 90536046 -0.3550701
4 chr1-90460304-90761641 90546991 90547262 -0.4310808
> head(correspondance, n = 4)
chr start end tagments start.1 end.1
1 chr1 44876009 45016546 chr1-44876009-45016546 44876009 45016546
2 chr1 90460304 90761641 chr1-90460304-90761641 90460304 90761641
3 chr1 211666507 211692757 chr1-211666507-211692757 211666507 211692757
4 chr2 46387184 46477385 chr2-46387184-46477385 46387184 46477385
我们需要展示检测区域在整个基因组中的位置,并绘制检测区域中的 DMR
信息
# 设置颜色映射
chr_bg_color <- paste0(sample(
c(brewer.pal(9, "Set1"),
brewer.pal(8, "Set2"),
brewer.pal(12, "Set3")),
size = 22), "80")
names(chr_bg_color) <- paste0("chr", 1:22)
# 绘制整个基因组
f1 <- function() {
circos.par(gap.after = 2, start.degree = 90)
circos.initializeWithIdeogram(chromosome.index = paste0("chr", 1:22),
plotType = c("ideogram", "labels"), ideogram.height = 0.03)
}
f2 <- function() {
circos.par(cell.padding = c(0, 0, 0, 0), gap.after = c(rep(1, nrow(tagments)-1), 10))
circos.genomicInitialize(tagments, plotType = NULL)
# 绘制检测区域中的 DMR 的数据
circos.genomicTrack(
DMR1, ylim = c(-0.6, 0.6),
panel.fun = function(region, value, ...) {
# 添加虚线
for (h in seq(-0.6, 0.6, by = 0.2)) {
circos.lines(
CELL_META$cell.xlim, c(h, h),
lty = 3, col = "#AAAAAA"
)
}
circos.lines(
CELL_META$cell.xlim, c(0, 0), lty = 3,
col = "#888888"
)
# 根据 DMR 值的正负设置不同颜色的点
circos.genomicPoints(
region, value, pch = 16, cex = 0.5,
col = ifelse(value[[1]] > 0, "#E41A1C", "#377EB8")
)
},
# 设置背景色
bg.col = chr_bg_color[tagments$chr],
track.margin = c(0.02, 0)
)
# 添加 y 轴刻度和标签
circos.yaxis(
side = "left",
at = seq(-0.6, 0.6, by = 0.3),
sector.index = get.all.sector.index()[1],
labels.cex = 0.4
)
# 添加最内层颜色圆环,标注检测区域所属染色体
circos.track(
ylim = c(0, 1),
track.height = mm_h(2),
bg.col = add_transparency(chr_bg_color[tagments$chr], 0)
)
}
circos.nested(
f1, f2, correspondance,
connection_col = chr_bg_color[correspondance[[1]]]
)
完整代码:
https://github.com/dxsbiocc/learn/blob/main/R/plot/DMRs_zoom.R