karyoploteR绘图
2021-07-08 本文已影响0人
所以suoyi
karyoploteR
1、安装
>if (!requireNamespace("BiocManager", quiet = TRUE))
+ install.packages("BiocManager")
>BiocManager::install("IdeoViz")
>library(karyoploteR)
2、绘图
(1)画基因组
> kp <- plotKaryotype(genome='hg19')

画基因组的指定染色体
> kp <- plotKaryotype(genome = "hg19", chromosomes=c("chr10", "chr12", "chr2"))

(2)在染色体上映射标记
先伪造一个Marker的文件
> marker <- read.table("marker.txt",header = T) # txt文件最后必须有一空行,否则报错
> print(marker)
chr pos label
1 chr1 11111111 pos1
2 chr2 22222222 pos2
3 chr3 33333333 pos3
再画好基因组/染色体
> kp <- plotKaryotype(genome='hg19', chromosomes=c("chr1", "ch2", "chr3"))
也可以染色体标个尺度
> kpAddBaseNumbers(kp)

把marker标到它该去的地方
> kpPlotMarkers(kp, chr=marker$chr, x=marker$pos, labels=marker$label)

试试不标上labels
> kpPlotMarkers(kp, chr=marker$chr, x=marker$pos, labels=' ')

给这线上个色
> kpPlotMarkers(kp, chr=marker$chr, x=marker$pos, labels=' ', line.color = "#FFAA22")

还可以给label上个色儿
> kpPlotMarkers(kp, chr=marker$chr, x=marker$pos, labels=marker$label, label.color = "#22AAFF")

竖着看好挤呀,给label文本转个方向,水平方向
> kpPlotMarkers(kp, chr=marker$chr, x=marker$pos, labels=marker$label, text.orientation = "horizontal")

https://bernatgel.github.io/karyoploter_tutorial//Tutorial/PlotMarkers/PlotMarkers.html
(3)在染色体上映射区域
一样先伪造一个region的文件
> region <- read.table("region.txt",header = T) # txt文件最后必须有一空行,否则报错
> print(region)
chr start end
1 chr1 11111111 22222222
2 chr1 55555555 66666666
3 chr2 12345678 22345678
4 chr3 61325321 61325321
区域画到染色体上
> kp <- plotKaryotype(genome='hg19', chromosomes=c("chr1", "chr2", "chr3"))
> kpPlotRegions(kp, data=region)

给区域上色
> kpPlotRegions(kp, data=region, col="#FFAA22")

给区域的边框上色
> kpPlotRegions(kp, data=region, col="#FFEECC", border="#FFCCAA")

这个的实际作用不是把长条条变小,而是分割成两份用白色填充的
> kpPlotRegions(kp, region, col="blue",border = "white",r0 = 0, r1 = 1,num.layers = 5)

(4)在染色体上画个点吧
继续伪造数据,,,
> point <- read.table("point.txt", header=T)
> print(point)
chr pos value
1 chr1 11111111 0.3
2 chr1 55555555 0.4
3 chr2 12345678 0.5
4 chr3 61325321 0.2
画上去画上去
> kp <- plotKaryotype(genome='hg19', chromosomes=c("chr1", "chr2", "chr3"))
> kpPoints(kp, chr=point$chr, x=point$pos, y=point$value)

加个色儿,再加个色儿
> kpPoints(kp, chr=point$chr, x=point$pos, y=point$value, col="#FFCCAA")

变成小圈圈~
> kpPoints(kp, chr=point$chr, x=point$pos, y=point$value, pch=1)

pch=不同的数字图案都不一样哦,极限是多少我也不知道,自己试试嘛
> kpPoints(kp, chr=point$chr, x=point$pos, y=point$value, pch=1:23)

21-25可以指定边界颜色(col=)和填充色(bg=)
变成大黑圈
> kpPoints(kp, chr=point$chr, x=point$pos, y=point$value, cex=2)

呀达,组合起来这是什么神奇的反应
> kpPoints(kp, chr=point$chr, x=point$pos, y=point$value, pch=1, cex=2)

(5)画个折线图
老样子
> line <- read.table("line.txt", header=T)
> print(line)
chr pos value
1 chr1 11111111 0.3
2 chr1 22222222 0.4
3 chr1 33333333 0.1
4 chr1 55555555 0.6
5 chr1 66666666 0.2
6 chr1 77777777 0.3
画上去!!!
> kp <- plotKaryotype(chromosomes="chr1")
> kpLines(kp, chr=line$chr, x=line$pos, y=line$value) # col="#440000" 还是一样能搞颜色

折线下面是阴影
> kp <- plotKaryotype(chromosomes="chr1")
> kpArea(kp, chr=line$chr, x=line$pos, y=line$value)

还要有框框
kpAxis(kp, ymin = 0, ymax=1)

搞个标准线,base.y
> kp <- plotKaryotype(chromosomes="chr1")
> kpArea(kp, chr=line$chr, x=line$pos, y=line$value, base.y=0.3)
> kpAxis(kp, ymin = 0, ymax=1)

col="gold", border="red" 都可以安排上
https://bernatgel.github.io/karyoploter_tutorial//Tutorial/Area/Area.html
(5)某点写文本
就用上面的数据line
> kp <- plotKaryotype(chromosomes="chr1")
> kpText(kp, chr=line$chr, x=line$pos, y=line$value, labels="p")

(6)超级组合技
> line <- read.table("line.txt", header=T)
> print(line)
chr pos value
1 chr1 11111111 0.3
2 chr1 22222222 0.4
3 chr1 33333333 0.1
4 chr1 55555555 0.6
5 chr1 66666666 0.2
6 chr1 77777777 0.3
> kp <- plotKaryotype(chromosomes="chr1")
> kpPoints(kp, chr=line$chr, x=line$pos, y=line$value)
> kpText(kp, chr=line$chr, x=line$pos+0.1, y=line$value+0.1, labels="p")

看看看看,
点
与文本
的完美融合,要是是有 labels
这一项的数据就好了
> kp <- plotKaryotype(chromosomes="chr1")
> kpLines(kp, chr=line$chr, x=line$pos, y=line$value)
> kpPoints(kp, chr=line$chr, x=line$pos, y=line$value)
> kpText(kp, chr=line$chr, x=line$pos, y=line$value+0.2, labels="p")

靠谱
(7)添加图例
# 添加图例
kp <- plotKaryotype(genome="hg19", plot.type=2, labels.plotter = NULL)
legend("bottomright",
legend=c("a","b","c","d","e","f","g"),
col=c("black","black","#999999","blue","yellow","orange","red"),
pch=c(NA,19,NA,NA,NA,NA,NA),
lty=1,lwd=1,cex=0.6)

(8)别的格式plot.type=4
pp <- getDefaultPlotParams(plot.type=4) # 查看默认参数设置
pp$ideogramlateralmargin <- 0 # 修改某一个参数 两个染色体中间就没有了缝隙
kp <- plotKaryotype(genome="hg19", plot.type=4, cytobands = GRanges(), labels.plotter = NULL, plot.params=pp)
kpDataBackground(kp, col=c("#E1EAF2", NA), r0=-0.05, r1=0.95) # 先加背景色,否则会压住轴线
kpAxis(kp, ymax = 5, ymin=0, numticks=6, r0=-0.05, r1=0.95, cex=0.5) # 添加轴
kpAbline(kp, h=c(0.2, 0.4, 0.6, 0.8, 1), col="black", lwd=0.5, r0=-0.05, r1=0.95) #添加水平线
kpAddChromosomeNames(kp, chr.names=c(1:22, 'X', 'Y'), cex=0.5) # 加染色体名

为什么r0=-0.05呢? 因为某些我也不知道的默认设置的原因,上面面板和下面染色体中间就是0.05,为使中间没有缝隙,只能让它降0.05,起始要去改那个默认设置也行,就是没去试

(9)plot.type=1、2、3、4、5、6、7
都可以去试试
(10)初学R,做个笔记
> getwd() 当前所在目录
> setwd("D:\\Rwork") 切换到别的目录
>dev.new() 新建图形窗口
>dev.set(3) 切换到之前编号3画图窗口
剩下的先不写了
官方教程在这里