採用circlize包繪製Circos圖

生信寶典 發佈 2022-07-13T01:02:38.475803+00:00

本文採用circlize包自帶的示例數據繪製Circos圖,簡要描述了circlize包的用法,以及幾個常用參數的作用。


本文採用circlize包自帶的示例數據繪製circos圖,簡要描述了circlize包的用法,以及幾個常用參數的作用。



Circos圖(圈圖)功能

可展示的數據:基因密度、基因功能注釋、CG含量、突變頻譜、CNV分布、組蛋白修飾、甲基化密度、轉座子與順式調控元件分布等等;

組學可涉及:基因組、外顯子組、甲基化組、ATAC-Seq和ChiP-Seq等組學等等;

複雜度可包括:多個樣本、多個組織,多個時期的組學數據;

圖的類型可涵蓋:(複雜)熱圖、散點圖、密度圖、折線圖、(堆積)柱狀圖、弦圖等等,同時展現多種圖形及其變化關係。



# 本計算機的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包只會默認安裝到上述路徑中。
# 安裝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 | 使用測試數據繪圖


獲取測試繪圖的數據:

一般以bed格式給出,前三列一般是坐標,後面可以增加任意列作為注釋

函數generaterandomBed用於獲取示例的bed數據

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函數,其參數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")



注意:後續的繪圖函數都依賴於circos.initializeWithIdeogram


添加SNV、INDEL的信息,以點圖展示,基於上面生成的bed數據:

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)即為紅色柱子,否則為綠色柱子


示例數據繪圖——高級


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的合併)熱圖在內層:
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()


如果您在繪製圖中出現如下報錯:

是由於畫布空間不足,可以將整塊代碼複製到Console中運行:



相關連結:

https://jokergoo.github.io/circlize_book/book/

https://academic.oup.com/bioinformatics/article/30/19/2811/2422259

http://www.sci666.net/59536.html


撰寫:葉明皓 校對:宋紅衛

關鍵字: