原文地址:https://mp.weixin.qq.com/s/wiTOWfrscK_dfDxk2vEFig
Circos可以用來畫基因組數(shù)據(jù)的環(huán)狀圖悼吱,也可以用來繪制其它數(shù)據(jù)的相關(guān)環(huán)狀圖驼仪,功能甚是強(qiáng)大乙埃,各種環(huán)狀的疊加,很是美觀炊林。下面我們學(xué)習(xí)一下:
基本參數(shù):
?circos.genomicTrackPlotRegion:? 創(chuàng)建軌道米间,添加圖形
?circos.genomicPoints:低水平函數(shù),添加點(diǎn)
?circos.genomicLines: 低水平函數(shù)破衔,添加線
?circos.genomicRect:? 低水平函數(shù),添加矩形
?circos.genomicText: 低水平函數(shù)钱烟,添加文本
?circos.genomicLink:添加連線
1晰筛、數(shù)據(jù)
數(shù)據(jù)格式:數(shù)據(jù)框
數(shù)據(jù)框必須包含三列信息:第一列是因子變量(基因類別、染色體等)拴袭,第二列和第三列是數(shù)值變量读第,表示第一列因子的起始和終止位置。
library(circlize)
set.seed(1)
#隨機(jī)產(chǎn)生一個(gè)數(shù)據(jù)
bed<-generateRandomBed(nr = 10,nc = 1)
bed
當(dāng)然拥刻,你還可以增加很多變量(上述數(shù)據(jù)只有一個(gè)變量value1)怜瞒,需要設(shè)定nc=”變量數(shù)目“
2、初始化布局
默認(rèn):我們借用人類基因組信息進(jìn)行布局:
par(mar = c(1, 1, 1, 1))
circos.initializeWithIdeogram()
當(dāng)然了般哼,circos.initializeWithIdeogram有很多參數(shù)可以選擇吴汪,你可以繪制其它物種的信息(species可以選擇物種)。
circos.initializeWithIdeogram(species = "hg18")
circos.initializeWithIdeogram(species = "mm10")
一般作為缺省值逝她,該函數(shù)繪制出所有染色體的信息浇坐,當(dāng)然你還可以自己調(diào)整繪制哪一條染色體。
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'))
注意:如果你不設(shè)定chromosome.index, 染色體順序是可以打亂順序的黔宛,也就是說可以人為排序近刘。當(dāng)你設(shè)定后chromosome.index,sort.chr是不起作用的臀晃,染色體位置信息取決于你的因子水平觉渴。
當(dāng)然了,很重要的一點(diǎn)就是徽惋,軌道上的信息案淋,我們可以通過參數(shù)plotType調(diào)整。
#plotType函數(shù):可以設(shè)定僅繪制軸和標(biāo)簽
circos.initializeWithIdeogram(plotType = c("axis", "labels"))
#僅僅初始化布局(什么也看不到)
circos.initializeWithIdeogram(plotType = NULL)
上一節(jié)我們也談到圖形繪制起始角度险绘,扇形之間的距離踢京,軌道高度等參數(shù)誉碴,在這里我們演示一下。
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'))我們也是可以自己定制化軌道:
#設(shè)定隨機(jī)數(shù)種子
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()
我們也是可以自己構(gòu)造數(shù)據(jù)框成黄,繪制圖形。
df = data.frame(
name = c("TP53", "TP63", "TP73"),
start = c(7565097, 189349205, 3569084),
end = c(7590856, 189615068, 3652765))
#下面三條命令繪制結(jié)果一樣
circos.genomicInitialize(df)
circos.genomicInitialize(df, sector.names = c("tp53", "tp63", "tp73"))
circos.genomicInitialize(df, plotType=c('axis','labels'))
#載入數(shù)據(jù)
load(paste0(system.file(package = "circlize"), "/extdata/tp_family.RData"))
names(tp_family)
#外顯子名字
names(tp_family[["TP53"]])
#位置信息
head(tp_family[['TP53']])[[1]]
#構(gòu)造數(shù)據(jù)框
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))
#創(chuàng)建新的軌道
circos.genomicTrackPlotRegion(ylim = c(0.5, n + 0.5),
panel.fun = function(region, value, ...) {
#類別變量信息(有三個(gè):TP53,TP63,TP73)
gn = get.cell.meta.data("sector.index")
#從列表中獲取每個(gè)類別變量的信息
tr = tp_family[[gn]]
?#循環(huán)繪制線逻杖、矩形
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奋岁、構(gòu)建作圖區(qū)域(添加點(diǎn)、線荸百、矩形等)
一般格式如下:
#單個(gè)變量
circos.genomicTrackPlotRegion(data,ylim=c(0,1),
panel.fun=function(region,value,...) {
circos.genomicPoints(region, value, ...)
})
#多個(gè)變量
circos.genomicTrackPlotRegion(data, numeric.column,
panel.fun=function(region,value,...) {
circos.genomicPoints(region, value, ...)
})
點(diǎn):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)
在這里介紹一個(gè)函數(shù),非常好用够话,可以分割你的數(shù)據(jù):colorRamp2
colorRamp2(breaks=c(-1,0,1),colors=c("green","black","red"))分割斷點(diǎn)-1虾攻,0,1分別對(duì)應(yīng)顏色green更鲁、black、red奇钞;此外-1到0對(duì)應(yīng)green到black的漸變色澡为,0到1對(duì)應(yīng)black到red的漸變色。
注意:這篇文章整個(gè)放不下了景埃,請(qǐng)到附文查看……