autoplot是ggbio包中的一个function,能够更好的对生物学中基因组数据画图。
参考网页:
https://www.rdocumentation.org/packages/ggbio/versions/1.20.1/topics/autoplot
有一个博主使用autoplot画基因的图,其中参数具有一定的参考价值:
跟着Nature Genetics学画图:R语言ggbio包画基因结构图 - 简书 (jianshu.com)
autoplot可以针对各种不同的类型的对象。
S3 method for class 'GRanges':
autoplot(object, ..., chr, xlab, ylab, main, truncate.gaps = FALSE,
truncate.fun = NULL, ratio = 0.0025, space.skip = 0.1,
legend = TRUE, geom = NULL, stat = NULL,
chr.weight = NULL,
coord = c("default", "genome", "truncate_gaps"),
layout = c("linear", "karyogram", "circle"))
文中给的栗子:
set.seed(1)
N <- 1000
library(GenomicRanges)
#构建Granges对象
gr <- GRanges(seqnames = sample(c("chr1", "chr2", "chr3"),
size = N, replace = TRUE),
IRanges(start = sample(1:300, size = N, replace = TRUE),
width = sample(70:75, size = N,replace = TRUE)),
strand = sample(c("+", "-", "*"), size = N, replace = TRUE),
value = rnorm(N, 10, 3), score = rnorm(N, 100, 30),
sample = sample(c("Normal", "Tumor"),
size = N, replace = TRUE),
pair = sample(letters, size = N,
replace = TRUE))
idx <- sample(1:length(gr), size = 50) #取50个随机数
###################################################
### code chunk number 3: default
###################################################
autoplot(gr[idx]) #画图
#随机取出granges对象中的50个exon画图,会默认按照染色体分开画图
image.png
栗子二:
set.seed(123)
# 构建两个Granges对象
gr.b <- GRanges(seqnames = "chr1", IRanges(start = seq(1, 100, by = 10),
width = sample(4:9, size = 10, replace = TRUE)),
score = rnorm(10, 10, 3), value = runif(10, 1, 100))
gr.b2 <- GRanges(seqnames = "chr2", IRanges(start = seq(1, 100, by = 10),
width = sample(4:9, size = 10, replace = TRUE)),
score = rnorm(10, 10, 3), value = runif(10, 1, 100))
gr.b <- c(gr.b, gr.b2) #把两个Granges对象拼接在一起
head(gr.b)
###################################################
### code chunk number 5: bar-default
###################################################
p1 <- autoplot(gr.b, geom = "bar")
## use value to fill the bar
p2 <- autoplot(gr.b, geom = "bar", aes(fill = value))
tracks(default = p1, fill = p2) #把p1和p2两张图整合在一起
#ru'guo
image.png
autoplot(gr[idx], geom = "arch", aes(color = value), facets = sample ~ seqnames)
#应该是把exon的位置用曲线标注出来,将图形按照不同的samples以及染色体分开。
image.png
栗子 again:
gra <- GRanges("chr1", IRanges(c(1,7,20), end = c(4,9,30)), group = c("a", "a", "b"))
#构建granges对象
## if you doesn't specify group, then group based on stepping levels, and gaps are computed without
## considering extra group method
p1 <- autoplot(gra, aes(fill = group), geom = "alignment")
#这里使用 geom = alignment 我猜是画基因结构的参数
## when use group method, gaps only computed for grouped intervals.
## default is group.selfish = TRUE, each group keep one row.
## in this way, group labels could be shown as y axis.
## group.selfish = FALSE, save space
# group.selfish = TRUE 这个参数可以在侧边增加title
p2 <- autoplot(gra, aes(fill = group, group = group), geom = "alignment")
#这里和p1相比增加了映射里面的group = group,就会按照group的分类,把属于两个不同的group的exon分开画,p1中三个exon画在了一条线上。
p3 <- autoplot(gra, aes(fill = group, group = group), geom = "alignment", group.selfish = FALSE) #这里p2和p3相比,加了group.selfish = FALSE。其实这里是默认值,group.selfish = TRUE可以在侧边加title。
tracks('non-group' = p1,'group.selfish = TRUE' = p2 , 'group.selfish = FALSE' = p3)
#整合图片加title
image.png
autoplot(gr, stat = "coverage", geom = "area",
facets = strand ~ seqnames, aes(fill = strand))
# stat ggplot参数
image.png
autoplot(gr[idx], layout = 'circle')
image.png
seqlengths(gr) <- c(400, 500, 700)
values(gr)$to.gr <- gr[sample(1:length(gr), size = length(gr))]
idx <- sample(1:length(gr), size = 50)
gr <- gr[idx]
ggplot() + layout_circle(gr, geom = "ideo", fill = "gray70", radius = 7, trackWidth = 3) +
layout_circle(gr, geom = "bar", radius = 10, trackWidth = 4,
aes(fill = score, y = score)) +
layout_circle(gr, geom = "point", color = "red", radius = 14,
trackWidth = 3, grid = TRUE, aes(y = score)) +
layout_circle(gr, geom = "link", linked.to = "to.gr", radius = 6, trackWidth = 1)
#这个图有点复杂,没怎么看懂
image.png
[图片上传中...(image.png-aa0e73-1626595812631-0)]
set.seed(1)
N <- 100
ir <- IRanges(start = sample(1:300, size = N, replace = TRUE),
width = sample(70:75, size = N,replace = TRUE))
## add meta data
df <- DataFrame(value = rnorm(N, 10, 3), score = rnorm(N, 100, 30),
sample = sample(c("Normal", "Tumor"),
size = N, replace = TRUE),
pair = sample(letters, size = N,
replace = TRUE))
values(ir) <- df
ir
#以上是构建了一个Granges对象,gr
###################################################
### code chunk number 14: ir-exp
###################################################
p1 <- autoplot(ir)
p2 <- autoplot(ir, aes(fill = pair)) + theme(legend.position = "none")
p3 <- autoplot(ir, stat = "coverage", geom = "line", facets = sample ~. )
p4 <- autoplot(ir, stat = "reduce")
tracks(p1, p2, p3, p4)
#p1画exon图
#p2按照pair画图
#p3按照sample分别画coverage的图
#p4不明白呢
image.png
set.seed(1)
N <- 100
## ======================================================================
## simmulated GRanges
## ======================================================================
gr <- GRanges(seqnames = sample(c("chr1", "chr2", "chr3"),
size = N, replace = TRUE),
IRanges(
start = sample(1:300, size = N, replace = TRUE),
width = sample(30:40, size = N,replace = TRUE)),
strand = sample(c("+", "-", "*"), size = N,
replace = TRUE),
value = rnorm(N, 10, 3), score = rnorm(N, 100, 30),
sample = sample(c("Normal", "Tumor"),
size = N, replace = TRUE),
pair = sample(letters, size = N,
replace = TRUE))
grl <- split(gr, values(gr)$pair)
#把刚才构建的Granges对象按照pair切割成Granges list
###################################################
### code chunk number 16: grl-exp
###################################################
## default gap.geom is 'chevron'
p1 <- autoplot(grl, group.selfish = TRUE)
p2 <- autoplot(grl, group.selfish = TRUE, main.geom = "arrowrect", gap.geom = "segment")
tracks(p1, p2)
image.png
autoplot(grl, aes(fill = ..grl_name..))
## equal to
## autoplot(grl, aes(fill = grl_name))
image.png
写不动了,大家看网页吧。
以上是针对Granges对象画图
将Granges对象转化为dataframe,可以使用ggtranscript 画转录本
git地址:https://github.com/dzhang32/ggtranscript
参考手册:https://dzhang32.github.io/ggtranscript/articles/ggtranscript.html