查看原文
其他

transPlotR 优雅的绘制基因转录本结构

JunJunLab 老俊俊的生信笔记 2023-06-15


无能为力

1引言

这个工作大概陆陆续续花费了一周多的时间基本完善的差不多了。你会想不会有这么多轮子了吗?(R 包),比如 ggtranscript, gggenes, wiggleplotr, Gviz 等等, 有些包用起来太复杂,有些又或者达不到自己的可视化要求,功能太少等等的不足或者限制,说白了也许就是自己屁事多, 我觉得比较好看的是 IGV 里面的转录本结构比较漂亮,也符合生物学意义。

有时候需要逼自己造个轮子出来方便自己使用,于是写了这个 transPlotR package, 非常简单,就只有一个函数,前前后后也增加了很多功能,硬扣细节,最终搞了个差不多的样子出来。也分享给大家使用。

2介绍

只有一个函数 trancriptVis 来可视化基因结构,需要输入你的 GTF 文件就行了(你可以使用 rtracklayer 进行读取),操作方便简洁。

主要功能:

  • 绘制编码或者非编码基因的转录本结构。
  • 支持多个基因。
  • 折叠多个转录本。
  • 支持跨距离,跨染色体基因可视化。
  • 极坐标展示基因结构。
  • 添加新 style 的箭头表示转录方向。
  • 绘制相对距离的位置坐标(以转录起始位置/终止位置为起点)
  • 反转负链基因的结构位置。
  • ...

觉得有用的小伙伴欢迎在我的 github 上面留下你们的小星星哦!

3安装

# install.packages("devtools")
devtools::install_github("junjunlab/transPlotR")

github 地址:

https://github.com/junjunlab/transPlotR

参考手册链接:

https://github.com/junjunlab/transPlotR/wiki/TransPlot-documentation

4单个基因绘制

非编码基因:

# load test data
data(gtf)

# non-coding gene
trancriptVis(gtfFile = gtf,
             gene = 'Xist')

编码基因:

粗的代表 CDS 区域,较粗的为 UTR,细线为内含子,箭头方向为基因转录方向

# coding gene
trancriptVis(gtfFile = gtf,
             gene = 'Nanog')

修改填充颜色:

# change fill color
trancriptVis(gtfFile = gtf,
             gene = 'Nanog',
             exonFill = '#CCFF00')

修改标签大小,颜色及位置:

# change label size,color and position
trancriptVis(gtfFile = gtf,
             gene = 'Nanog',
             textLabelSize = 4,
             textLabelColor = 'red',
             relTextDist = 0)

标注基因名:

# aes by gene name
trancriptVis(gtfFile = gtf,
             gene = 'Nanog',
             textLabel = 'gene_name')

用转录本映射颜色:

# color aes by transcript
trancriptVis(gtfFile = gtf,
             gene = 'Tpx2',
             exonColorBy = 'transcript_id')

修改箭头颜色及类型:

# change arrow color and type
trancriptVis(gtfFile = gtf,
             gene = 'Nanog',
             arrowCol = 'orange',
             arrowType = 'closed')

如果没有内含子,可以改变箭头颜色,更加容易分辨:

# no intron gene and add arrow color
# change arrow color and type
trancriptVis(gtfFile = gtf,
             gene = 'Jun',
             textLabel = 'gene_name',
             arrowCol = 'white',
             arrowType = 'closed') +
  theme_void()

添加箭头数量:

# add arrow breaks
trancriptVis(gtfFile = gtf,
             gene = 'Nanog',
             arrowCol = 'orange',
             arrowType = 'closed',
             arrowBreak = 0.1)

指定特定的转录本进行可视化:

# draw specific transcript
p1 <- trancriptVis(gtfFile = gtf,
                   gene = 'Commd7')

p2 <- trancriptVis(gtfFile = gtf,
                   gene = 'Commd7',
                   myTranscript = c('ENSMUST00000071852','ENSMUST00000109782'))

# combine
cowplot::plot_grid(p1,p2,ncol = 2,align = 'hv')

5新的箭头类型

这种在一些文献里可以见到,标注出来代表基因的转录方向,我这里也添加了这个功能,可以实现这种效果。

对比:

# add specific arrow
pneg <- trancriptVis(gtfFile = gtf,
                     gene = 'Gucy2e',
                     newStyleArrow = T)

ppos <- trancriptVis(gtfFile = gtf,
                     gene = 'Tex15',
                     newStyleArrow = T)

# combine
cowplot::plot_grid(pneg,ppos,ncol = 2,align = 'hv')

我们可以去除正常的箭头:

# remove normal arrow
trancriptVis(gtfFile = gtf,
             gene = 'Fat1',
             newStyleArrow = T,
             addNormalArrow = F)

如你所见,每个转录本的新箭头是 与其转录本的长度成比例 的,是一种相对长度, 对于较短的转录本来说, 同样的相对长度可能会带来不一样的效果,比如一些短的转录本结构箭头太短。

这里我们可以设置为绝对长度,使每个转录本的箭头保持一样长:

# draw absolute specific arrow
trancriptVis(gtfFile = gtf,
             gene = 'Fat1',
             newStyleArrow = T,
             addNormalArrow = F,
             absSpecArrowLen = T)

我们还可以控制箭头的颜色,大小及位置:

# change position size color and height
trancriptVis(gtfFile = gtf,
             gene = 'Fat1',
             newStyleArrow = T,
             addNormalArrow = F,
             speArrowRelPos = 0.5,
             speArrowLineSize = 1,
             speArrowCol = 'red',
             speArrowRelHigh = 3)

绘制极坐标下的转录本:

# circle plot with specific arrow
trancriptVis(gtfFile = gtf,
             gene = 'F11',
             newStyleArrow = T,
             addNormalArrow = F,
             circle = T,
             ylimLow = -2)

极坐标下的绝对箭头长度因为弧度不一样,所以长度也会相应改变,也是比较好理解的:

# circle plot with absolute specific arrow
trancriptVis(gtfFile = gtf,
             gene = 'F11',
             newStyleArrow = T,
             addNormalArrow = F,
             circle = T,
             ylimLow = -2,
             absSpecArrowLen = T)

6多基因转录本结构绘制

支持多个基因多个转录本结构的绘制,但是首先记住,我们是在基因组坐标里绘图的,要想绘制多基因呈现一个较好的效果图,首先确定你的基因们是 相互靠近的,且是 位于同一染色体上面的,这很合理。

# support multiple gene
# should on same chromosome and close to each other
trancriptVis(gtfFile = gtf,
             gene = c('Trmt6','Mcm8','Crls1','Lrrn4','Fermt1'),
             textLabel = 'gene_name')

下面展示的是 IGV 的结果,有一些差异,因为使用的注释文件不太一致:

对基因进行映射,改变箭头长度:

# color by gene and change arrow length
trancriptVis(gtfFile = gtf,
             gene = c('Crls1','Fermt1'),
             textLabel = 'gene_name',
             exonColorBy = 'gene_name',
             newStyleArrow = T,
             speArrowRelLen = 1)

我们可以将多个转录本折叠为一个:

# collapse gene
trancriptVis(gtfFile = gtf,
             gene = c('Trmt6','Mcm8','Crls1','Lrrn4','Fermt1'),
             textLabel = 'gene_name',
             collapse = T,
             relTextDist = 0.2)

7给定区域绘图

你可以指定染色体,起始位置和终止位置来进行绘图:

# support plot at a given region
trancriptVis(gtfFile = gtf,
             Chr = 11,
             posStart = 69609973,
             posEnd = 69624790)

8环形图

我们可以在极坐标系里对基因结构进行展示:

# draw circle structure
trancriptVis(gtfFile = gtf,
             gene = 'Gucy2e',
             textLabelSize = 4,
             circle = T)

让圆圈变小点:

# change circle small
trancriptVis(gtfFile = gtf,
             gene = 'Gucy2e',
             textLabelSize = 4,
             circle = T,
             ylimLow = 0)

改变圆圈的夹角:

# change circle angle
c1 <- trancriptVis(gtfFile = gtf,
             gene = 'F11',
             textLabelSize = 4,
             circle = T,
             ylimLow = 0,
             openAngle = 0)

c2 <- trancriptVis(gtfFile = gtf,
             gene = 'F11',
             textLabelSize = 4,
             circle = T,
             ylimLow = 0,
             openAngle = 0.2)

# combine
cowplot::plot_grid(c1,c2,ncol = 2,align = 'hv')

对转录本进行映射填充颜色:

# chenge aes fill
trancriptVis(gtfFile = gtf,
             gene = 'Gucy2e',
             textLabelSize = 4,
             circle = T,
             ylimLow = 0,
             exonColorByTrans = T)

改变线颜色:

# change segment color
trancriptVis(gtfFile = gtf,
             gene = 'Gucy2e',
             textLabelSize = 4,
             circle = T,
             ylimLow = 0,
             exonColorByTrans = T,
             circSegCol = 'black')

添加基因名:

# add gene name
trancriptVis(gtfFile = gtf,
             gene = 'Gucy2e',
             textLabel = 'gene_name',
             textLabelSize = 5,
             circle = T,
             ylimLow = 0,
             exonColorByTrans = T)

移除连线:

# remove line
trancriptVis(gtfFile = gtf,
             gene = 'Gucy2e',
             textLabel = 'gene_name',
             textLabelSize = 5,
             circle = T,
             ylimLow = 0,
             exonColorByTrans = T,
             text_only = T)

绘制多个基因:

# multiple gene
trancriptVis(gtfFile = gtf,
             gene = c('Pfn1','Eno3','Spag7'),
             textLabel = 'gene_name',
             textLabelSize = 2,
             circle = T,
             ylimLow = -5,
             text_only = T,
             circSegCol = 'grey80',
             exonColorByTrans = T)

标记转录本名称:

# textlabel with transcript_name
trancriptVis(gtfFile = gtf,
             gene = 'Gucy2e',
             textLabelSize = 4,
             circle = T,
             ylimLow = 0,
             textLabel = 'transcript_name',
             addNormalArrow = F,
             newStyleArrow = T)

9跨距离/染色体绘图

想象你有多个基因,它们可能距离很远或者不在同一个染色体上面,你想绘制它们的转录本结构可以使用 单基因循环批量绘图, 这是一种解决方法。但是非常的不人性化,这里提供了解决这个问题的方案, 分面绘图 来实现。

批量循环绘图:

这里选了三个基因分别位于1号染色体首/中间/末尾的位置,跨度很大来作为示范:

# single plot
lapply(c('Camk1g','Daw1','Oprk1'), function(x){
  trancriptVis(gtfFile = gtf,
               gene = x,
               textLabel = 'gene_name')
}) -> plist

# combine
cowplot::plot_grid(plotlist = plist,ncol = 3,align = 'hv')

按照前面一起绘图将会得到下面结果:

# plot tegether
trancriptVis(gtfFile = gtf,
             gene = c('Camk1g','Daw1','Oprk1'),
             textLabel = 'gene_name')

对基因进行分面展示:

# facet by gene
trancriptVis(gtfFile = gtf,
             gene = c('Camk1g','Daw1','Oprk1'),
             facetByGene = T)

去除正常箭头,添加新样式箭头,以绝对距离展示:

# add new arrow and remove normal arrow
trancriptVis(gtfFile = gtf,
             gene = c('Camk1g','Daw1','Oprk1'),
             facetByGene = T,
             newStyleArrow = T,
             absSpecArrowLen = T,
             speArrowRelLen = 0.1,
             addNormalArrow = F)

绘制不同染色体的基因:

这里的三个基因分别位于染色体1/2/3上作为示范:

# for different chromosome genes
# chr1:Camk1g chr2:Duox2 chr3:Ttll7
trancriptVis(gtfFile = gtf,
             gene = c('Camk1g','Duox2','Ttll7'),
             facetByGene = T)

老铁没毛病!

10相对距离

如你所见,上面的绘图我们都是基因 基因组坐标系统下 进行绘制的,如果我们想在同一坐标下比较不同基因不同转录本将会变得困难。这里提供了可以 将坐标转换为相对坐标 来进行展示比较。

只需要设置 forcePosRel = T:

# transform relative position
trancriptVis(gtfFile = gtf,
             gene = c('Camk1g','Daw1','Oprk1'),
             facetByGene = T,
             newStyleArrow = T,
             absSpecArrowLen = T,
             speArrowRelLen = 0.1,
             addNormalArrow = F,
             forcePosRel = T)

我们可以看到都是以左边为 0 起始位点来绘图的,横坐标的长度代表了整个转录本的长度。

美化一下:

# ajusted with facet parameters
trancriptVis(gtfFile = gtf,
             gene = c('Camk1g','Daw1','Oprk1'),
             facetByGene = T,
             newStyleArrow = T,
             absSpecArrowLen = T,
             speArrowRelLen = 0.1,
             addNormalArrow = F,
             forcePosRel = T,
             ncolGene = 1,
             scales = 'free_y',
             strip.position = 'left',
             textLabelSize = 2,
             exonColorBy = 'gene_name',
             textLabel = 'transcript_name',
             panel.spacing = 0)

环形图展示:

# cicular plot with relative position
trancriptVis(gtfFile = gtf,
             gene = 'Nanog',
             textLabelSize = 4,
             circle = T,
             ylimLow = 0,
             textLabel = 'transcript_name',
             addNormalArrow = F,
             newStyleArrow = T,
             exonColorBy = 'transcript_name',
             forcePosRel = T)

11反转负链

在上面的图中可以看到,将坐标系转为相对以后, 负链上的基因还是以从右往左进行的,要 比较似乎还需要反转一下负链基因的位置。这里提供了反转负链的revNegStrand = T 参数来让比较更加方便和易于理解。

# reverse negtive strand
trancriptVis(gtfFile = gtf,
             gene = c('Camk1g','Daw1','Oprk1'),
             facetByGene = T,
             newStyleArrow = T,
             absSpecArrowLen = T,
             speArrowRelLen = 0.1,
             addNormalArrow = F,
             forcePosRel = T,
             revNegStrand = T)

可以看到 Camk1g 这个位于负链的基因所有位置都被反转了,此外,对应的新样式的箭头也一起改变了注意这里的箭头方向不再有意义了

美化调整一下:

# ajusted with facet parameters
p1 <- trancriptVis(gtfFile = gtf,
                   gene = c('Camk1g','Daw1','Oprk1'),
                   facetByGene = T,
                   newStyleArrow = T,
                   absSpecArrowLen = T,
                   speArrowRelLen = 0.1,
                   addNormalArrow = F,
                   forcePosRel = T,
                   ncolGene = 1,
                   scales = 'free_y',
                   strip.position = 'left',
                   textLabelSize = 2,
                   exonColorBy = 'gene_name',
                   textLabel = 'transcript_name',
                   panel.spacing = 0)

# reverse negtive strand
p2 <- trancriptVis(gtfFile = gtf,
                   gene = c('Camk1g','Daw1','Oprk1'),
                   facetByGene = T,
                   newStyleArrow = T,
                   absSpecArrowLen = T,
                   speArrowRelLen = 0.1,
                   addNormalArrow = F,
                   forcePosRel = T,
                   ncolGene = 1,
                   scales = 'free_y',
                   strip.position = 'left',
                   textLabelSize = 2,
                   exonColorBy = 'gene_name',
                   textLabel = 'transcript_name',
                   panel.spacing = 0,
                   revNegStrand = T)

# combine
cowplot::plot_grid(plotlist = list(p1,p2),ncol = 2,align = 'hv')

左边是没有反转的,右边是反转的,对比一下,看起来更加好比较了。

更多的参数见:

?trancriptVis

12结尾

有其它建议或者疑问请在 github 提问。





  老俊俊生信交流群 (微信交流群需收取20元入群费用(防止骗子和便于管理))



老俊俊微信:


知识星球:



今天的分享就到这里了,敬请期待下一篇!

最后欢迎大家分享转发,您的点赞是对我的鼓励肯定

如果觉得对您帮助很大,赏杯快乐水喝喝吧!




  





ggarchery 添加个性化箭头

scRNAtoolVis 0.0.3 版本更新

customize 你的 GSEA 图

GseaVis 优雅的可视化 GSEA 富集结果

GSEA 图是如何画出来的? (源码解析)

clusterProfiler 的可视化大全

clusterProfiler 做 GSEA 富集分析

深度神经网络学习对单细胞数据进行清洗去噪

ggpie 解决你的所有饼图绘制

Bigwig 可视化用 tackPlotR 试试看?

◀...

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存