之前有一次,我導說“能不能把這些基因按順序畫在果蠅的染色體上”,因此,我尋找了一些實現這個需求的方法。經過一番奇奇怪怪的操作之后,最終用ggplot2畫了。。。其它方法簡單地記錄在這里(反正也是學了的,萬一以后有用呢)
一、畫基因和染色體的R包
- RIdeogram:畫染色體很好看
- gggenes:畫基因的結構
- ggbio:可以把基因畫在染色體上
-
Gviz:把基因組坐標和其它數據用相同的坐標放在一起展示
用處不大,隨便看看
二、安裝R包
options("repos" = c(CRAN="https://mirror.lzu.edu.cn/CRAN/"))
options(BioC_mirror="http://mirrors.tuna.tsinghua.edu.cn/bioconductor")
install.packages("RIdeogram")
install.packages("gggenes")
BiocManager::install("ggbio")
BiocManager::install("Gviz")
library(RIdeogram)
library(RColorBrewer)
library(ggbio)
library(gggenes)
library(Gviz)
library(ggplot2)
三、RIdeogram
(1)每條染色體的長度
需要至少有Chr/ Start/ End三列,如果還有著絲粒位點可以加上CE_start/ CE_end兩列。可以從UCSC查到所需物種的每條染色體長度,這里的染色體編號要和后面的一致。
> DmChromosome <- read.table("DmChromosome.txt", header=TRUE, sep="\t", row.names=1, stringsAsFactors=F)
> DmChromosome
Chr Start End
1 2L 0 23513712
2 2R 0 25286936
3 3L 0 28110227
4 3R 0 32079331
5 4 0 1348131
6 X 0 23542271
7 Y 0 3667352
(2)基因密度信息
可以通過gffex函數從gff文件中提取熱圖信息(基因密度)。多次嘗試選擇一個合適的window大小,這里的feature也可以是CDS、exon、ncRNA等。
> Dmdensity <- GFFex(input="Drosophila_melanogaster.BDGP6.28.99.chr.gff3.gz",
karyotype="DmChromosome.txt",
feature="gene",window=200000)
> head(Dmdensity)
Chr Start End Value
1 2L 1 200000 25
2 2L 200001 400000 29
3 2L 400001 600000 31
4 2L 600001 800000 14
5 2L 800001 1000000 23
(3)marker型繪圖
有marker/line/polygon/heatmap四種繪圖方法。marker型需要Type/ Shape/ Chr/ Start/ End/ color。它僅對type一欄繪制圖例,marker型只適合展示少量基因。
> head(gene_list)
Type Shape Chr Start End color name
1 mRNA box 2L 18683720 18688800 FFFFB3 gene-1
2 mRNA box 2L 18684984 18688800 FFFFB3 gene-1
3 mRNA box 3L 7123213 7125533 FFFFB3 gene-3
4 mRNA box 2L 3462218 3466112 FFFFB3 gene-4
5 mRNA box 2L 3462218 3466112 FFFFB3 gene-4
6 mRNA box X 5873840 5876853 FB8072 gene-2
> ideogram(karyotype=DmChromosome, #染色體信息
+ overlaid=Dmdensity, #基因密度
+ label=gene_list, #目標基因標簽
+ label_type="marker",width=135)
> convertSVG("chromosome.svg",device="png") #非常耗時
(4)line/polygon/heatmap型繪圖
需要制作一個類似于基因密度信息的表,包含Chr/ Start/ End/ Value_1/ Color_1/ Value_2/ Color_2。
> head(gene_list_v)
Chr Start End Value Color
1 2L 1 200000 1966.7804 fc8d62
2 2L 200001 400000 4279.0219 fc8d62
3 2L 400001 600000 3022.2958 fc8d62
4 2L 600001 800000 182.4522 fc8d62
5 2L 800001 1000000 14683.3203 fc8d62
6 2L 1000001 1200000 1023.5985 fc8d62
> ideogram(karyotype=DmChromosome, #染色體信息
+ overlaid=Dmdensity, #基因密度
+ label=gene_list_v, #目標基因標簽
+ label_type="line",width=135)
> convertSVG("chromosome.svg",device="png") #可以手動修改文件名
(5)heatmap型繪圖
需要制作一個類似于基因密度信息的表,包含Chr/ Start/ End/ Value。
ideogram(karyotype=DmChromosome,
overlaid=Dmdensity,
label=gene_list_v,
label_type="heatmap",width=135,
colorset1 = c("#f7f7f7", "#2c7fb8"),
colorset2 = c("#f7f7f7", "#e34a33"))
convertSVG("chromosome.svg",device="png")
(6)數據集
如果是人類的染色體,可以通過直接獲得核型信息(不需要這里的前兩步)。
data(human_karyotype,package="RIdeogram")
data(gene_density,package="RIdeogram")
head(human_karyotype) #24行 Chr/ Start/ End/ CE_start/ CE_end
head(gene_density) #3102行 Chr/ Start/ End/ Value
四、gggenes
(1)示例數據
> head(example_genes); dim(example_genes)
molecule gene start end strand direction
1 Genome5 genA 405113 407035 forward 1
2 Genome5 genB 407035 407916 forward 1
3 Genome5 genC 407927 408394 forward 1
4 Genome5 genD 408387 408737 reverse -1
5 Genome5 genE 408751 409830 forward 1
6 Genome5 genF 409836 410315 forward 1
[1] 72 6
(2)繪圖
> p1 <- ggplot(example_genes,aes(xmin=start,xmax=end,y=molecule,fill=gene)) +
+ geom_gene_arrow() +
+ facet_wrap(~ molecule,scales="free",ncol=1) +
+ scale_fill_brewer(palette="Set3") +
+ theme_genes()
> p1
(3)對齊某一基因
> dummies <- make_alignment_dummies(example_genes,
+ aes(xmin=start,xmax=end,y=molecule,id=gene),
+ on="genE")
> p1 + geom_blank(data=dummies)
(4)寫上基因名稱
> p2 <- ggplot(example_genes,aes(xmin=start,xmax=end,y=molecule,fill=gene)) +
+ geom_gene_arrow(arrowhead_height=unit(3,"mm"),arrowhead_width=unit(1,"mm")) +
+ facet_wrap(~ molecule,scales="free",ncol=1) +
+ scale_fill_brewer(palette="Set3") +
+ theme_genes()
> p2
> p2 + geom_gene_label(aes(label=gene),align="left")
(5)區分正負鏈
> p3 <- ggplot(example_genes,aes(xmin=start,xmax=end,y=molecule,
+ fill=gene,forward=direction)) +
+ geom_gene_arrow() +
+ facet_wrap(~ molecule,scales="free",ncol=1) +
+ scale_fill_brewer(palette="Set3") +
+ theme_genes()
> p3
(6)基因亞結構
> head(example_subgenes);dim(example_subgenes)
molecule gene start end strand subgene from to
1 Genome5 genA 405113 407035 forward genA-1 405774 406538
2 Genome5 genB 407035 407916 forward genB-1 407458 407897
3 Genome5 genC 407927 408394 forward genC-1 407942 408158
4 Genome5 genC 407927 408394 forward genC-2 408186 408209
5 Genome5 genC 407927 408394 forward genC-3 408233 408257
6 Genome5 genF 409836 410315 forward genF-1 409938 410016
[1] 143 8
> p4 <- ggplot(example_genes,aes(xmin=start,xmax=end,y=molecule)) +
+ facet_wrap(~ molecule, scales="free",ncol = 1) +
+ geom_gene_arrow(fill="white") +
+ geom_subgene_arrow(data=example_subgenes,
+ aes(xmin=start,xmax=end,y=molecule,fill=gene,
+ xsubmin=from,xsubmax=to),color="black",alpha=.7) +
+ theme_genes()
> p4
五、ggbio
只用到了一個功能,把一些基因(比如上調或下調的基因)畫在染色體上。
(1)構建GRanges對象
> mygr #GRanges object
GRanges object with 127 ranges and 4 metadata columns:
seqnames ranges strand | gene_name logFC pvalue regulation
<Rle> <IRanges> <Rle> | <factor> <numeric> <numeric> <factor>
[1] chrX 3720007-3788818 + | Tlk -6.982227392 0.394290308 down
[2] chrX 3858940-3862697 - | roX1 11.48670351 0.00280582 up
[3] chrX 3932156-3936469 - | CG2938 -4.634468751 0.373950298 down
(2)繪圖
> autoplot(mygr,layout="karyogram",aes(color=regulation,fill=regulation))
> mygr$levels <- as.numeric(factor(mytx$regulation))
> autoplot(mygr,layout="karyogram",aes(color=regulation,fill=regulation,
+ ymin=(levels-1)*10/2,ymax=levels*10/2))
六、Gviz
Gviz能展示的內容非常多。特點是把基因組坐標和其它數據(如logFC)用相同的坐標放在一起展示。每一部分內容作為一個track,可以有GenomeAxisTrack、IdeogramTrack、DataTrack、AnnotationTrack、GeneRegionTrack、 SequenceTrack、 AlignmentsTrack等tracks。
(感覺過于復雜,我又不太能用到,所以只是粗略地看了看。官網的文檔很詳細)
(1)構造GRanges對象
> mygrsub[1:3]
GRanges object with 3 ranges and 6 metadata columns:
seqnames ranges strand | symbol CF CM logFC
<Rle> <IRanges> <Rle> | <factor> <numeric> <numeric> <numeric>
[1] chr2L 6911090-6914138 - | Wee1 11.99432931 1.329478214 -3.173420446
[2] chr2L 6911343-6914138 - | Wee1 4.959662983 68.0307017 3.777871974
[3] chr2L 6914301-6920210 - | x16 3.455682714 2.584117733 -0.41929896
pvalue group
<numeric> <character>
[1] 0.066684944 Wee1
[2] 0.000146948 Wee1
[3] 0.644895204 x16
-------
seqinfo: 2 sequences from an unspecified genome; no seqlengths
(2)繪圖
> g <- GenomeAxisTrack()
> a <- AnnotationTrack(mygrsub,name="gene ranges",feature=updown)
> d <- DataTrack(mygrsub, data="logFC", baseline=0,
+ type="h", name="log2 fold change", strand="+") #h,histogram lines
> displayPars(g) <- list(col="darkgray",fontcolor="lightblue4")
> displayPars(a) <- list(background.panel="#FFFEDB",background.title="brown",col=NULL)
> displayPars(d) <- list(background.panel="azure",background.title="dodgerblue4",col=NULL)
> plotTracks(list(g,d,a),groupAnnotation="group",
+ down="#40B3D9", up="#FE8C8C")
最上面的數軸代表GenomeAxisTrack,log2FC就放在了DataTrack,基因區間放在了AnnotationTrack。其中DataTrack就是想要展示的數據了。還可以有很多種繪圖方法,像dot and lines plot、 boxplot、 heatmap等。