采用circlize包繪制Circos圖
本文采用circlize包自帶的示例數(shù)據(jù)繪制Circos圖,簡要描述了circlize包的用法,以及幾個常用參數(shù)的作用。
# 本計算機的R的版本:
.libPaths()## [1] "C:/Program Files/R/R-4.0.4/library"
## [2] "C:/Users/HP/Documents/R/win-library/4.0"# R-4.0.3
# [1] "C:/Users/ACER/Documents/R/win-library/4.0"
# [2] "C:/Program Files/R/R-4.0.3/library"
# 安裝新的R包只會默認(rèn)安裝到上述路徑中。
# 安裝circlize包
options("repos" = c(CRAN="http://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
if (! require(circlize)) {
install.packages('circlize')
}## Loading required package: circlize## Warning: package 'circlize' was built under R version 4.0.5## ========================================
## circlize version 0.4.12
## CRAN page: https://cran.r-project.org/package=circlize
## Github page: https://github.com/jokergoo/circlize
## Documentation: https://jokergoo.github.io/circlize_book/book/
##
## If you use it in published research, please cite:## Gu, Z. circlize implements and enhances circular visualization
## in R. Bioinformatics 2014.
##
## This message can be suppressed by:
## suppressPackageStartupMessages(library(circlize))
## ========================================
library(circlize)1 | 使用測試數(shù)據(jù)繪圖
獲取測試?yán)L圖的數(shù)據(jù):
一般以bed格式給出,前三列一般是坐標(biāo),后面可以增加任意列作為注釋。
函數(shù)generateRandomBed用于獲取示例的bed數(shù)據(jù)
bed = generateRandomBed(nr = 200)
head(bed)## chr start end value1
## 1 chr1 1265305 23689474 -0.3692290
## 2 chr1 33693147 34684697 0.1979085
## 3 chr1 46407775 50257809 -0.2200499
## 4 chr1 50933154 50945647 -0.1620185
## 5 chr1 53258978 54441031 1.2335410
## 6 chr1 59225767 66683215 0.2868235繪制基因組(人類),版本為hg38 :
使用circos.initializeWithIdeogram函數(shù),其參數(shù)species的幫助:
Abbreviations of species. e.g. hg19 for human, mm10 for mouse. If this value is specified, the function will download cytoBand.txt.gz from UCSC website automatically. If there is no cytoband for user’s species, it will keep on trying to download chromInfo file. Pass to read.cytoband or read.chromInfo.
circos.clear() # 繪圖初始化
circos.initializeWithIdeogram(species = "hg38")

注意:后續(xù)的繪圖函數(shù)都依賴于circos.initializeWithIdeogram
添加SNV、INDEL的信息,以點圖展示,基于上面生成的bed數(shù)據(jù):
circos.clear() # 繪圖初始化
circos.initializeWithIdeogram(species = "hg38")circos.genomicTrack(bed,
panel.fun = function(region, value, ...) {
circos.genomicPoints(region, value, pch = 16, cex = 0.5, col = 1)})

添加CNV
circos.clear() # 繪圖初始化
circos.initializeWithIdeogram(species = "hg38")circos.genomicTrack(bed,
panel.fun = function(region, value, ...) {
circos.genomicPoints(region, value, pch = 16, cex = 0.5, col = 1)})circos.genomicTrack(bed,
panel.fun = function(region, value, ...) {
circos.genomicRect(region, value, ytop.column = 1, ybottom = 0,
col = ifelse(value[[1]] > 0, "red", "green"), ...)
circos.lines(CELL_META$cell.xlim, c(0, 0), lty = 2, col = "#00000040")})
# 大于閾值(例如0)即為紅色柱子,否則為綠色柱子

示例數(shù)據(jù)繪圖——高級
circos.clear()
circos.par("start.degree" = 90)
circos.par("gap.degree" = rep(c(2, 2), 12), ADD = TRUE)
circos.initializeWithIdeogram(species = "hg38", plotType = c("axis", "labels"))
text(0, 0, "Sample1", cex = 1)
# 添加SNV、INDEL的信息:
circos.genomicTrack(bed,
panel.fun = function(region, value, ...) {
circos.genomicPoints(region, value, pch = 16, cex = 0.5, col = 1)})
# 添加CNV:
circos.genomicTrack(bed,
panel.fun = function(region, value, ...) {
circos.genomicRect(region, value, ytop.column = 1, ybottom = 0,
col = ifelse(value[[1]] > 0, "red", "green"), ...)
circos.lines(CELL_META$cell.xlim, c(0, 0), lty = 2, col = "#00000040")})
# 大于閾值(例如0)即為紅色柱子,否則為綠色柱子

set.seed(50)
bed = generateRandomBed(nr = 200)
circos.clear()
circos.par("start.degree" = 90)
circos.par("gap.degree" = rep(c(2, 2), 12), ADD = TRUE)
circos.initializeWithIdeogram(species = "hg38", plotType = c("axis"))
text(0, 0, "Sample1", cex = 1)
# 染色體使用不同顏色的方框表示:
circos.track(ylim = c(0, 1), panel.fun = function(x, y) {
chr = CELL_META$sector.index
xlim = CELL_META$xlim
ylim = CELL_META$ylim
circos.rect(xlim[1], 0, xlim[2], 1, col = rand_color(24))
circos.text(mean(xlim), mean(ylim), chr, cex = 0.6, col = "white",
facing = "inside", niceFacing = TRUE)
}, track.height = 0.1, bg.border = NA)
# 添加SNV、INDEL的信息:
circos.genomicTrack(bed,
panel.fun = function(region, value, ...) {
circos.genomicPoints(region, value, pch = 16, cex = 0.5, col = 1)})
# 添加CNV:
circos.genomicTrack(bed,
panel.fun = function(region, value, ...) {
circos.genomicRect(region, value, ytop.column = 1, ybottom = 0,
col = ifelse(value[[1]] > 0, "red", "green"), ...)
circos.lines(CELL_META$cell.xlim, c(0, 0), lty = 2, col = "#00000040")
})
# 大于閾值(例如0)即為紅色柱子,否則為綠色柱子

# bed(文件多樣本value的合并)熱圖在內(nèi)層:
circos.clear()
circos.initializeWithIdeogram()
bed = generateRandomBed(nr = 100, nc = 4)
col_fun = colorRamp2(c(-1, 0, 1), c("green", "black", "red"))
circos.genomicHeatmap(bed, col = col_fun, side = "inside", border = "white")
# bed(文件多樣本value的合并)熱圖在外層:
circos.clear()
circos.initializeWithIdeogram(plotType = NULL)
circos.genomicHeatmap(bed, col = col_fun, side = "outside",
line_col = as.numeric(factor(bed[[1]])))
circos.genomicIdeogram()
# 加Label:
circos.clear()
circos.initializeWithIdeogram()
bed.1 = generateRandomBed(nr = 100, nc = 4)
circos.genomicHeatmap(bed.1, col = col_fun, side = "outside",
line_col = as.numeric(factor(bed[[1]])))
bed.2 = generateRandomBed(nr = 50, fun = function(k) sample(letters, k, replace = TRUE))
bed.2[1, 4] = "aaaaa"
circos.genomicLabels(bed.2, labels.column = 4, side = "inside")
# circos.genomicIdeogram()

2 | 示例代碼匯總
rm(list = ls())
set.seed(100)
bed = generateRandomBed(nr = 100)
col_fun = colorRamp2(c(-1, 0, 1), c("green", "black", "red"))
# col_fun = colorRamp2(c(-1, 0, 1), c("navy", "grey", "firebrick3"))
circos.clear()
#pdf("./Circos.hg38.pdf")
circos.par("start.degree" = 90)
circos.par("gap.degree" = rep(c(2, 2), 12), ADD = TRUE)
circos.initializeWithIdeogram(species = "hg38", plotType = c("axis"))
text(0, 0, "Sample1", cex = 1)
####### 染色體使用不同顏色的方框表示:
circos.track(ylim = c(0, 1), panel.fun = function(x, y) {
chr = CELL_META$sector.index
xlim = CELL_META$xlim
ylim = CELL_META$ylim
circos.rect(xlim[1], 0, xlim[2], 1, col = rand_color(24))
circos.text(mean(xlim), mean(ylim), chr, cex = 0.6, col = "white",
facing = "inside", niceFacing = TRUE)
}, track.height = 0.1, bg.border = NA)
####### 添加SNV、INDEL的信息:
circos.genomicTrack(bed,
panel.fun = function(region, value, ...) {
circos.genomicPoints(region, value, pch = 16, cex = 0.5, col = 1)},
stack = F, track.height = 0.1)
####### 添加CNV:
circos.genomicTrack(bed,
panel.fun = function(region, value, ...) {
circos.genomicRect(region, value, ytop.column = 1, ybottom = 0,
col = ifelse(value[[1]] > 0, "red", "green"), ...)
circos.lines(CELL_META$cell.xlim, c(0, 0), lty = 2, col = "#00000040")
}, stack = F, track.height = 0.1)
# 大于閾值(例如0)即為紅色柱子,否則為綠色柱子
####### 熱圖:
bed.1 = generateRandomBed(nr = 70, nc = 4)
# bed.1 = bed.1[1:10, ]
circos.genomicHeatmap(bed.1, col = col_fun, side = "inside", border = "white",
connection_height = mm_h(2),heatmap_height = 0.1,
line_col = as.numeric(factor(bed[[1]])))####### Label
bed.2 = generateRandomBed(nr = 10, fun = function(k) sample(letters, k, replace = TRUE))
bed.2[1, 4] = "aaa"
bed.2$value1 = paste0("Gene_",bed.2$value1)
circos.genomicLabels(bed.2, labels.column = 4,connection_height = mm_h(2),labels_height =cm_h(1.0), side = "inside")# circos.genomicIdeogram()
# dev.off()

如果您在繪制圖中出現(xiàn)如下報錯:

是由于畫布空間不足,可以將整塊代碼復(fù)制到Console中運行:

撰寫:葉明皓 校對:宋紅衛(wèi)
往期精品(點擊圖片直達文字對應(yīng)教程)
后臺回復(fù)“生信寶典福利第一波”或點擊閱讀原文獲取教程合集




























