原文地址:https://mp.weixin.qq.com/s/wiTOWfrscK_dfDxk2vEFig
Circos可以用來畫基因組數據的環狀圖,也可以用來繪制其它數據的相關環狀圖,功能甚是強大,各種環狀的疊加,很是美觀。下面我們學習一下:
基本參數:
?circos.genomicTrackPlotRegion:? 創建軌道,添加圖形
?circos.genomicPoints:低水平函數,添加點
?circos.genomicLines: 低水平函數,添加線
?circos.genomicRect:? 低水平函數,添加矩形
?circos.genomicText: 低水平函數,添加文本
?circos.genomicLink:添加連線
1、數據
數據格式:數據框
數據框必須包含三列信息:第一列是因子變量(基因類別、染色體等),第二列和第三列是數值變量,表示第一列因子的起始和終止位置。
library(circlize)
set.seed(1)
#隨機產生一個數據
bed<-generateRandomBed(nr = 10,nc = 1)
bed
當然,你還可以增加很多變量(上述數據只有一個變量value1),需要設定nc=”變量數目“
2、初始化布局
默認:我們借用人類基因組信息進行布局:
par(mar = c(1, 1, 1, 1))
circos.initializeWithIdeogram()
當然了,circos.initializeWithIdeogram有很多參數可以選擇,你可以繪制其它物種的信息(species可以選擇物種)。
circos.initializeWithIdeogram(species = "hg18")
circos.initializeWithIdeogram(species = "mm10")
一般作為缺省值,該函數繪制出所有染色體的信息,當然你還可以自己調整繪制哪一條染色體。
circos.initializeWithIdeogram(species = 'hg19',chromosome.index = paste('chr',1:5,sep=""))
par(mar = c(1, 1, 1, 1))
par(mfrow=c(2,1))
#打亂順序
circos.initializeWithIdeogram(species = 'hg18',sort.chr = F)
circos.initializeWithIdeogram(species = 'hg18',chromosome.index = c('chr1','chr2','chr3','chr4'))
注意:如果你不設定chromosome.index, 染色體順序是可以打亂順序的,也就是說可以人為排序。當你設定后chromosome.index,sort.chr是不起作用的,染色體位置信息取決于你的因子水平。
當然了,很重要的一點就是,軌道上的信息,我們可以通過參數plotType調整。
#plotType函數:可以設定僅繪制軸和標簽
circos.initializeWithIdeogram(plotType = c("axis", "labels"))
#僅僅初始化布局(什么也看不到)
circos.initializeWithIdeogram(plotType = NULL)
上一節我們也談到圖形繪制起始角度,扇形之間的距離,軌道高度等參數,在這里我們演示一下。
circos.par("start.degree" = 90,gap.degree=c(rep(2,3),10),cell.padding=c(0,0,0,0))
circos.initializeWithIdeogram(chromosome.index = c('chr1','chr2','chr3','chr4'))我們也是可以自己定制化軌道:
#設定隨機數種子
set.seed(2)
#圓形布局
par(mar=c(1,1,1,1))
#初始化
circos.initializeWithIdeogram(species = 'hg18',plotType = NULL)
#繪制軌道
circos.trackPlotRegion(ylim=c(0,1),track.height = 0.2,panel.fun = function(x,y){
#獲取信息(x、y范圍,分類變量),這一步是必須的!
chr=get.cell.meta.data('sector.index')
xlim=get.cell.meta.data('xlim')
ylim=get.cell.meta.data('ylim')
#在軌道上繪制矩形
? circos.rect(xleft = xlim[1],ybottom = ylim[1],xright = xlim[2],ytop = ylim[2]-0.5,col=rand_color(1))
#添加文本信息
?
circos.text(x=mean(xlim),y =ylim[2]-0.3,labels = chr,font=1,facing =
'bending.outside', niceFacing = T,cex=0.6)},bg.border = NA)
#重置命令
circos.clear()
我們也是可以自己構造數據框,繪制圖形。
df = data.frame(
name = c("TP53", "TP63", "TP73"),
start = c(7565097, 189349205, 3569084),
end = c(7590856, 189615068, 3652765))
#下面三條命令繪制結果一樣
circos.genomicInitialize(df)
circos.genomicInitialize(df, sector.names = c("tp53", "tp63", "tp73"))
circos.genomicInitialize(df, plotType=c('axis','labels'))
#載入數據
load(paste0(system.file(package = "circlize"), "/extdata/tp_family.RData"))
names(tp_family)
#外顯子名字
names(tp_family[["TP53"]])
#位置信息
head(tp_family[['TP53']])[[1]]
#構造數據框
df<-data.frame(
gene=names(tp_family),
start=sapply(tp_family,function(x)min(unlist(x))),
end=sapply(tp_family,function(x)max(unlist(x))))
#初始化
circos.genomicInitialize(df)
#繪制軌道
circos.genomicTrackPlotRegion(ylim = c(0, 1),
bg.col = c("blue", "green", "grey"),
bg.border = NA, track.height = 0.05)
我們繪制出外顯子在基因上的位置信息:
#初始化
circos.genomicInitialize(df)
#繪制軌道
circos.genomicTrackPlotRegion(ylim = c(0, 1), bg.col = c("blue", "green", "grey"),
bg.border=NA,track.height = 0.05)
n = max(sapply(tp_family, length))
#創建新的軌道
circos.genomicTrackPlotRegion(ylim = c(0.5, n + 0.5),
panel.fun = function(region, value, ...) {
#類別變量信息(有三個:TP53,TP63,TP73)
gn = get.cell.meta.data("sector.index")
#從列表中獲取每個類別變量的信息
tr = tp_family[[gn]]
?#循環繪制線、矩形
for(i in seq_along(tr)) {
current_tr_start = min(tr[[i]]$start)
current_tr_end = max(tr[[i]]$end)
circos.lines(c(current_tr_start, current_tr_end),c(n - i, n - i), col = "#CCCCCC")
circos.genomicRect(tr[[i]], ytop = n - i + 0.4,? ybottom = n - i - 0.4, col = "orange", border = NA)? }
}, bg.border = NA, track.height = 0.3)
circos.clear()
3、構建作圖區域(添加點、線、矩形等)
一般格式如下:
#單個變量
circos.genomicTrackPlotRegion(data,ylim=c(0,1),
panel.fun=function(region,value,...) {
circos.genomicPoints(region, value, ...)
})
#多個變量
circos.genomicTrackPlotRegion(data, numeric.column,
panel.fun=function(region,value,...) {
circos.genomicPoints(region, value, ...)
})
點:circos.genomicPointscircos.genomicPoints(region, value, ...)
circos.genomicPoints(region, value,numeric.column=c(1,2))
circos.genomicPoints(region, value, cex, pch)
circos.genomicPoints(region, value, sector.index, track.index)這里numeric.column也可以用列的名字代替
circos.genomicPoints=function(region,value,numeric.column=1,...) {
x=(region[[2]]+region[[1]])/2
y=value[[numeric.column]]
circos.points(x, y, ...)
}這里circos.points也可以表示出circos.genomicPoints。
線:circos.genomicLines circos.genomicLines(region, value, ...)
circos.genomicLines(region, value,numeric.column=c(1,2))
circos.genomicLines(region, value, lwd,lty="segment")
circos.genomicLines(region, value, area, baseline, border)
circos.genomicLines(region, value, sector.index, track.index)
Text:文本circos.genomicText(region, value, ...)
circos.genomicText(region, value, y, labels)
circos.genomicText(region, value, numeric.column, labels.column)
circos.genomicText(region, value, facing, niceFacing, adj)
circos.genomicText(region, value, sector.index, track.index)
Rectangle:矩形circos.genomicRect(region, value,ytop=1,ybottom=0)
circos.genomicRect(region, value,ytop.column=2,ybottom=0)
circos.genomicRect(region, value, col, border)
在這里介紹一個函數,非常好用,可以分割你的數據:colorRamp2
colorRamp2(breaks=c(-1,0,1),colors=c("green","black","red"))分割斷點-1,0,1分別對應顏色green、black、red;此外-1到0對應green到black的漸變色,0到1對應black到red的漸變色。
注意:這篇文章整個放不下了,請到附文查看……