python绘制基因结构图_从 gff 到 gggenes 绘制基因结构图
gffutils 是一個用來解析 gff 文件的 Python 包,可以十分方便地獲取 gff 文件中的相關信息。gggenes 是 ggplot2 的擴展包,用于繪制基因結構圖、多物種基因比較圖的很好玩的工具。兩個工具聯用可以實現從 gff 數據獲取到基因結構圖繪制的全過程。
對 gff 原始數據進行處理
安裝 gffutils
使用 conda 或者 pip 進行安裝。
conda install gffutils
pip install gffutils
gff 文件預處理
對 gff 文件進行預處理,截取包含所需基因的 gff 內容。建議在 Linux 中使用 sed 命令完成。如果進行比較基因組工作,需要將各基因組數據合并到一個 gff 文件中。
程序調用
下載 gff2gggenes.py 到本地。
Windows 中利用 powershell 調用程序:
python .\gff2gggenes.py example.gff
python .\gff2gggenes.py example.gff sub
Linux 中利用 console 調用程序:
python ./gff2gggenes.py example.gff
python ./gff2gggenes.py example.gff sub
根據安裝 Python 的版本不同,可能需要將“python”替換為“python3”。
不添加 sub 參數,表示只將各基因的情況進行輸出;添加 sub 參數,表示同時輸出各基因子區域(例如:mRNA、CDS等,與 gff 文件內容有關)。
結果輸出
界面顯示“完成”表示程序運行成功。csv 文件輸出到工作路徑中,文件名結尾是“_Gene.csv”或者“_SubGene.csv”。
利用 R 包 gggenes 進行可視化
gggene 安裝
直接從 CRAN 安裝:
install.packages("gggenes")
如果使用 rstudio 進行 R 工作,也可以在 package 中安裝。
啟用 ggplot2 和 gggene
可以在 rstudio 的包管理工具中開啟,也可以使用以下代碼:
library(ggplot2)
library(gggenes)
導入 python 程序生成的 csv 數據
geneData = read.csv('gene.csv')
用geom_gene_arrow()畫基因箭頭
ggplot(geneData, 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 美化圖形
默認的圖形不太美觀,可以使用自帶的 theme_genes() 進行美化。
ggplot(geneData, 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()
使用 make_alignment_dummies() 跨面對齊基因
可以選擇一個基因將所有數據進行對齊,在比較基因組時會用到。
dummies
geneData,
aes(xmin = start, xmax = end, y = molecule, id = gene),
on = "geneX"
)
ggplot(geneData, aes(xmin = start, xmax = end, y = molecule, fill = gene)) +
geom_gene_arrow() +
geom_blank(data = dummies) +
facet_wrap(~ molecule, scales = "free", ncol = 1) +
scale_fill_brewer(palette = "Set3") +
theme_genes()
用 geom_gene_label() 標記基因
geom_gene_label() 可以將標簽文本放入基因箭頭內,需要把基因名字所在的列名字映射到 label 屬性。依賴于 ggfittext 包。
ggplot(
geneData,
aes(xmin = start, xmax = end, y = molecule, fill = gene, label = gene)
) +
geom_gene_arrow(arrowhead_height = unit(3, "mm"), arrowhead_width = unit(1, "mm")) +
geom_gene_label(align = "left") +
geom_blank(data = dummies) +
facet_wrap(~ molecule, scales = "free", ncol = 1) +
scale_fill_brewer(palette = "Set3") +
theme_genes()
查看基因子片段(subgene)
可以使用 geom_subgene_arrow() 突出顯示子基因片段。此時,需要多調用另一個 csv 文件。
subGeneData = read.csv('subGene.csv')
ggplot(geneData, aes(xmin = start, xmax = end, y = molecule)) +
facet_wrap(~ molecule, scales = "free", ncol = 1) +
geom_gene_arrow(fill = "white") +
geom_subgene_arrow(data = subGeneData,
aes(xmin = start, xmax = end, y = molecule, fill = gene,
xsubmin = from, xsubmax = to), color="black", alpha=.7) +
theme_genes()
同時,也可以為基因子片段添加標簽。
ggplot(geneData, aes(xmin = start, xmax = end, y = strand)
) +
geom_gene_arrow() +
geom_gene_label(aes(label = gene)) +
geom_subgene_arrow(
data = subGeneData, aes(xsubmin = from, xsubmax = to, fill = subgene)
) +
geom_subgene_label(
data = subGeneData, aes(xsubmin = from, xsubmax = to, label = subgene),
min.size = 0
)
總結
以上是生活随笔為你收集整理的python绘制基因结构图_从 gff 到 gggenes 绘制基因结构图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jsonhandle主界面没有显示格式_
- 下一篇: python 编程该看那些书籍_初学者自