1.安装和载入包
install.packages("circlize")
library(circlize)
2.开始设置
circos.clear()
# 结束上次绘图绘图,否则会提示警告信息
circos.par("start.degree" = 80)
#设置1号染色体起始角度,然后顺时针
circos.par("gap.degree" = rep(c(2, 4), 12))
# 每个gap的度数大小,重复12次,24条染色体共有24个gap
circos.par("gap.degree" = c(rep(2, 23),20))
#Y染色体和1号染色体间的gap为20°,其他相邻染色体之间为2°
3.选择要画物种的染色体
3.1 人的染色体
circos.initializeWithIdeogram()#初始化画板,默认画的是人的染色体数据,不是hg38
circos.initializeWithIdeogram(species = "hg18")#OK,16,17,18,19,38,不同版本的人染色体
circos.initializeWithIdeogram(species = "hg38", chromosome.index = paste0("chr", c(1:22, "X", "Y")))#除去contig
circos.initializeWithIdeogram(chromosome.index = 'chr1')#只画一号染色体
circos.initializeWithIdeogram(chromosome.index = c('chr1','chr3'))#只画1号和3号染色体
circos.initializeWithIdeogram(plotType = NULL)#什么都不画
circos.initializeWithIdeogram(plotType = c("axis", "labels"))只画染色体坐标和染色体号
circos.info()
3.2 鼠的染色体
circos.initializeWithIdeogram(species = "mm10")#初始化画板,mm7,mm8,mm9,mm10
3.3 猪的染色体
circos.initializeWithIdeogram(species = "susScr11")#初始化画板,susScr2,susScr3出错,只画坐标,原因是没有cytoband data文件
circos.initializeWithIdeogram(species = "susScr11",plotType = c("axis", "labels"))#初始化画板
3.4 大鼠的染色体
circos.initializeWithIdeogram(species = "rn3")#3,4,5都OK
circos.initializeWithIdeogram(species = "rn6", chromosome.index = paste0("chr", c(1:20, "X", "Y")))#去除contig
3.5 牛羊的染色体
circos.initializeWithIdeogram(species = " oviAri4")#bosTau8报错
4 创建BED文件(一般要自己创建)
set.seed(123)#123只是一个编号,方便下次重复,详见https://blog.csdn.net/vencent_cy/article/details/50350020
bed = generateRandomBed(nc = 2)#nc 表示随机指定产生value的列数,nc = 2,会产生value1和value2
#head(bed, n = 2)
bed = generateRandomBed(500)#生成507行数据,默认生成一列value
#nrow(bed)
5 创建track
5.1 散点图
bed = generateRandomBed(nr = 1000)#默认生成一列value
circos.genomicTrack(bed, panel.fun = function(region, value, ...) {
circos.genomicPoints(region, value, pch = 16, cex = 0.5, ...)})#在这个轨道里画散点,散点图只需要一列value
circos.genomicTrack(bed, bg.col =rep(c("#FF000040", "#00FF0040", "#0000FF40") ,8), panel.fun = function(region, value, ...) {
circos.genomicPoints(region, value, pch = 16, cex = 0.5, ...)})#在这个轨道里画散点
5.2 折线图
bed = generateRandomBed(nc = 2, nr = 1000)#nc 表示随机指定产生value的列数,nc = 2,会产生value1和value2
bed = generateRandomBed(nr = 100)#也OK
circos.genomicTrack(bed,,bg.col =rep(c("#FF000040", "#00FF0040", "#0000FF40") ,8), panel.fun = function(region, value, ...) {
circos.genomicLines(region, value,cex = 0.5, ...)})#
5.3 和弦图
circos.genomicIdeogram()#再画一遍染色体
bed1 = generateRandomBed(nr = 100)#nr 表示指定产生数据的行数,实际为107行
bed1 = bed1[sample(nrow(bed1), 20), ]#sample取样,dplyr包,从bed1的100行里取20行
bed2 = generateRandomBed(nr = 100) # 实际为107
bed2 = bed2[sample(nrow(bed2), 20), ]#从bed2的100行里取20行
circos.genomicLink(bed1, bed2, col = rand_color(nrow(bed1), transparency = 0.5),
border = NA)#和弦图
5.4 热力图
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")
5.5 直方图
bed = generateRandomBed(nr = 10000)
circos.initialize(factors = bed$chr, x = bed$value1)
circos.trackHist(bed$chr, x=bed$value1, col = "#999999", border = "#999999")
5.6 基因组密度图
bed = generateRandomBed(nr = 10000, species = "hg18")[,1:3]#取1到3列
circos.genomicDensity(bed, col = c("#FF000080"), track.height = 0.1)
circos.clear() # 结束绘图,否则会继续叠加图层
5.7 柱状图
circos.genomicTrack(bed,,bg.col =rep(c("#FF000040", "#00FF0040", "#0000FF40") ,8), panel.fun = function(region, value, ...) {
circos.genomicRect(region, value,ytop = 1, ybottom = 0)})#
6.添加标签
text(0, 0.9, "a", cex = 1)
7.范例
circos.initializeWithIdeogram(chromosome.index = 'chr1')
bed = generateRandomBed(nr = 1000)
bed=data.frame(chr=bed$chr, start=bed$start, start=bed$start+1, value1
=1000*bed$value1)
circos.genomicTrack(bed, panel.fun = function(region, value, ...) {
circos.genomicLines(region, value,cex = 0.5,col = c("#FF000080"))})#
circos.genomicTrack(bed, bg.col =rep(c("#FF000040", "#00FF0040", "#0000FF40") ,8), panel.fun = function(region, value, ...) {
circos.genomicPoints(region, value, pch = 16, cex = 0.5, ...)})
circos.genomicTrack(bed,
panel.fun = function(region, value, ...) {
circos.genomicLines(region, value, area = TRUE, col = 'blue')#填充为蓝色
})