woodcorpse的个人博客分享 http://blog.sciencenet.cn/u/woodcorpse

博文

重现2篇Nature中GraPhlAn绘制的超高颜值物种树Cladogram

已有 4420 次阅读 2019-11-28 19:13 |个人分类:R|系统分类:科研笔记

GraphLan绘制教程

我们经常在文章中看到这样的图

image

Yang Bai, Daniel B. Müller, Girish Srinivas, Ruben Garrido-Oter, Eva Potthoff, Matthias Rott, Nina Dombrowski, Philipp C. Münch, Stijn Spaepen, Mitja Remus-Emsermann, Bruno Hüttel, Alice C. McHardy, Julia A. Vorholt & Paul Schulze-Lefert. Functional overlap of the Arabidopsis leaf and root microbiota. Nature. 2015, 528: 364-369. doi:10.1038/nature16192

还有这样的图

image

Jingying Zhang, Yong-Xin Liu, Na Zhang, Bin Hu, Tao Jin, Haoran Xu, Yuan Qin, Pengxu Yan, Xiaoning Zhang, Xiaoxuan Guo, Jing Hui, Shouyun Cao, Xin Wang, Chao Wang, Hui Wang, Baoyuan Qu, Guangyi Fan, Lixing Yuan, Ruben Garrido-Oter, Chengcai Chu & Yang Bai. NRT1.1B is associated with root microbiota composition and nitrogen use in field-grown rice. Nature Biotechnology. 2019, 37: 676-684. doi:10.1038/s41587-019-0104-4

是不是很漂亮

之前公众号已经为大家介绍了GraPhlAn进化树的绘制方法,如下文:

今天就带大家根据特征表(OTU table)、和物种注释(Taxonomy),绘制另一类高颜值的物种树(Cladogram,也称进化分支图)。并提供相关测试数据、代码,让你准备好输入文件,方便一步步生成绘图所需文件。并可按需求组合数据和样式,达到出版要求的图片。

代码和数据下载链接

https://github.com/YongxinLiu/Note/tree/master/R/format2graphlan

format2graphlan.Rmd # 完整代码文件,包括R和Bash两种语言,需要在Linux中运行

format2graphlan.html # 代码完整运行的报告,方便阅读,也确保代码有效和可重复

如果链接失效,“宏基因组”公众号后台回复“graphlan”关键字获取最新数据和代码下载链接。

输入文件

文件夹内要准备至少两个文件:OTU表和物种注释

# 从现在项目中复制文件,准备起始数据
cd ~/github/Note/R/format2graphlan
cp ~/ehbio/amplicon/22Pipeline/result/otutab.txt ./
cp ~/ehbio/amplicon/22Pipeline/result/taxonomy.txt ./

OTU表otutab.txt格式如下:行名为特征OTU/ASV,列名为样本名,可以为原始值或标准化的小数均可。

#OTUID  KO1     KO2     KO3   
ASV_1   1113    1968    816   
ASV_2   1922    1227    2355  
ASV_3   568     460     899   

物种注释taxonomy.txt:包括OTUID和7级注释,末知的补Unassigned

OTUID   Kingdom Phylum  Class   Order   Family  Genus   Species
ASV_1   Bacteria        Actinobacteria  Actinobacteria  Actinomycetales Thermomonosporaceae     Unassigned      Unassigned
ASV_2   Bacteria        Proteobacteria  Betaproteobacteria      Burkholderiales Comamonadaceae  Pelomonas       Pelomonas_puraquae
ASV_3   Bacteria        Proteobacteria  Gammaproteobacteria     Pseudomonadales Pseudomonadaceae        Rhizobacter     Rhizobacter_bergeniae

首选我们要对原始数据进行筛选,因为结果过少或过多都不美观。如根据丰度进行筛选Top 150的特征进行展示。

1. 特征表求均值并按丰度筛选

输入文件:OTU表+物种注释

可以指定丰度或数量筛选,两个条件选择共有部分

输出文件:OTU对应均值,筛选后的OTU表+物种注释

# 参数设置
# 按丰度筛选,如0.01即代表0.01%,即万分之一
abundance = 0.01
# 按数量筛选,如150即代表最高丰度的150个特征
number = 150

# 读取输入文件
otutab = read.table("otutab.txt", sep="\t", header = TRUE, row.names = 1, stringsAsFactors = F, comment.char = "")
taxonomy = read.table("taxonomy.txt", sep="\t", header = TRUE, row.names = 1, stringsAsFactors = F, comment.char = "")

# 数据筛选
# 标准化并求均值
norm = as.data.frame(t(t(otutab)/colSums(otutab,na=T)*100))
# 丰度由大到小排序
idx = order(rowMeans(norm), decreasing = T)
norm = norm[idx,]
# 按丰度筛选
idx = rowMeans(norm) > abundance
filtered_otutab = norm[idx,]
# 按数量筛选
filtered_otutab = head(norm, number)
# 添加均值并保留4位小数
filtered_otutab = round(cbind(rowMeans(filtered_otutab), filtered_otutab), digits = 4)
colnames(filtered_otutab)[1] = "Mean"
# 对应过滤物种注释
idx = rownames(filtered_otutab) %in% rownames(taxonomy)
filtered_otutab = filtered_otutab[idx,]
filtered_taxonomy = taxonomy[rownames(filtered_otutab),]

# 保存输出文件
# 过滤的OTU表
write.table("OTUID\t", file="filtered_otutab.txt", append = F, sep="\t", quote=F, eol = "", row.names=F, col.names=F)
suppressWarnings(write.table(filtered_otutab, file="filtered_otutab.txt", append = T, sep="\t", quote=F, row.names=T, col.names=T))
# 过滤的物种注释
write.table("OTUID\t", file="filtered_taxonomy.txt", append = F, sep="\t", quote=F, eol = "", row.names=F, col.names=F)
suppressWarnings(write.table(filtered_taxonomy, file="filtered_taxonomy.txt", append = T, sep="\t", quote=F, row.names=T, col.names=T))

2. 绘制树骨架文件

输入文件为筛选后的taxonomy文件:filtered_taxonomy.txt

本处主要筛选了门、纲、目、科、属和OTU作为树枝,按科添加标签,并对应门着色。由于Unassigned末分类的较多,重名会引着色混乱(每个标签是独立着色的,名称必须唯一,不唯一时后出现的名称会覆盖之前的颜色值。),本文去除了在科水平无注释的分类单元。

# 读取筛选后的文件,不设置行名
tax = read.table("filtered_taxonomy.txt", sep="\t", header = TRUE, stringsAsFactors = F)
# 筛选门-属5级+OTUID
tree = data.frame(tax[,c(3:7,1)], stringsAsFactors = F)
# head(tree)
## clarify taxonomy,解决不同级别重名问题,为可识别级别,且与Greengene格式保持一致
tree[,1] = paste("p__",tree[,1],sep = "")
tree[,2] = paste("c__",tree[,2],sep = "")
tree[,3] = paste("o__",tree[,3],sep = "")
# tree[,4] = paste("f__",tree[,4],sep = "")
tree[,5] = paste("g__",tree[,5],sep = "")
# save tree backbone, 按点分隔格式

# 解决科标签重名问题
idx = tree[,4] %in% "Unassigned"
# 方法1. 重名标签添加数字编号,但结果有太多Unassigned
# tree[idx,4] = paste0(tree[idx,4], 1:length(tree[idx,4]))
# 方法2. 过滤掉科末注释的条目,数量会减少,但图片更美观
tree = tree[!idx,]
# 简化一些代_的不规则科名
tree[,4] = gsub('_\\w*',"",tree[,4])
write.table (tree, file="tree1_backbone.txt", sep=".", col.names=F, row.names=F, quote=F)

# 列出现在有门、纲、目、科、属,用于设置与门对应的背景色
Phylum = unique(tree[,1]) 
Class = unique(tree[,2])
Order = unique(tree[,3])
Family = unique(tree[,4])
Genus = unique(tree[,5])

# 筛选四大菌门中的科并按门着色
# 修改为目,则将tree的4列改为3列,Family改为Order
pro = tree[tree[,1]=="p__Proteobacteria",4]
act = tree[tree[,1]=="p__Actinobacteria",4] 
bac = tree[tree[,1]=="p__Bacteroidetes",4]
fir = tree[tree[,1]=="p__Firmicutes",4]

# 对每个科进行标签、文字旋转、按门注释背景色
# 也可调整为其它级别,如Order, Class或Genus
label_color = data.frame(stringsAsFactors = F)
for (element in Family)
{
  # element
  anno = data.frame(stringsAsFactors = F)
  anno[1,1] = element
  anno[1,2] = "annotation"
  anno[1,3] = "*"
  # 设置文字旋转90度
  anno[2,1] = element
  anno[2,2] = "annotation_rotation"
  anno[2,3] = "90"
  # 设置背景色,四大门各指定一种色,其它为灰色
  anno[3,1] = element
  anno[3,2] = "annotation_background_color" 

  if (element %in% pro)
  {
      anno[3,3] = "#85F29B"
  } else if (element %in% act)
  {
      anno[3,3] = "#F58D8D"   
  } else if (element %in% fir)
  {
      anno[3,3] = "#F7C875"  
  } else if (element %in% bac)
  {
      anno[3,3] = "#91DBF6"   
  } else {
      anno[3,3] = "grey"   
  }
  label_color = rbind(label_color,anno)
}
write.table(label_color, "tree2_label_color.txt", sep = "\t", quote = F,col.names = F,row.names = F, na="")

此时生成了两个文件

树骨架

tree1_backbone.txt

是一点相连的各级物种分类名称,添加p, c等为减少不同级别的不规范重名引起颜色混乱

p__Actinobacteria.c__Actinobacteria.o__Actinomycetales.Thermomonosporaceae.g__Unassigned.ASV_1
p__Proteobacteria.c__Betaproteobacteria.o__Burkholderiales.Comamonadaceae.g__Pelomonas.ASV_2
p__Proteobacteria.c__Gammaproteobacteria.o__Pseudomonadales.Pseudomonadaceae.g__Rhizobacter.ASV_3

树颜色和标签

tree2_label_color.txt

科水平的标签、标签旋转角度和与门对应的颜色。

Thermomonosporaceae     annotation      *
Thermomonosporaceae     annotation_rotation     90
Thermomonosporaceae     annotation_background_color     #F58D8D
Comamonadaceae  annotation      *
Comamonadaceae  annotation_rotation     90
Comamonadaceae  annotation_background_color     #85F29B

基本树绘图

绘制树,还需要一些参数文件,见cfg目录,是我提前编写好的样本,可以调整更多样式。

cfg/global.cfg设置了图型的基本样式,配色等,

以下部分以bash中操作,需要在Linux上的Rstudio或Rstudio server中操作。或自己使用终端连接服务器执行。

一定要提前安装过graphlan这个软件,安装方法conda install graphlan

rm -rf track*
# 生成树的默认参数,可手动调整更多样式
cat cfg/global.cfg tree2_label_color.txt > track0
# 合并所有的注释,接下来会生成更多track,使树更复杂
cat track* > graphlan_annotate.txt
# 注释树
graphlan_annotate.py --annot graphlan_annotate.txt tree1_backbone.txt graphlan.xml
# 绘图,size决定图片大小,越大字越小
graphlan.py graphlan.xml graphlan0_tree.pdf --size 5

image

现在用以上代码为大家写出了一套注释方案,这要是手动编写和优化出这方案,也可能要花上几天至几周。

我们需要从树文件中获得节点名称,并添加注释数据。

如获得结点的丰度,在下面很多注释都会基于丰度信息

# 获得最终出图的结点ID
cut -f 6 -d '.' tree1_backbone.txt > tree1_backbone.id
# 注释结果丰度均值
awk 'BEGIN{OFS=FS="\t"} NR==FNR{a[$1]=$2} NR>FNR {print $1,a[$1]}' filtered_otutab.txt tree1_backbone.id > tree1_backbone.mean

形状标签有无

样式1. 如筛选丰度,用紫色方块标出大于千分之5的结点

# 环1,筛选千分之五的结果注释为方块,cfg/ring1.cfg中的m代表紫色,R代表方块
cat cfg/ring1.cfg <(awk '$2>0.5' tree1_backbone.mean | cut -f 1 | sed 's/$/\tring_shape\t1\tR/') > track1

# 绘图,加第一环矩形,展示丰度大于千万的特征
cat track* > graphlan_annotate.txt
graphlan_annotate.py --annot graphlan_annotate.txt tree1_backbone.txt graphlan.xml
graphlan.py graphlan.xml graphlan1_rectangle.pdf --size 5

image

样式2. 如筛选丰度,用第二环位置橙色倒三角标出小于千分之5的结点

注释:ring2.cfg为第二环,颜色y为yellow橙色,注释track中也为2

# 环1筛选千分之五的结果注释为方块,cfg/ring1.cfg中的m代表紫色,R代表方块
cat cfg/ring2.cfg <(awk '$2<=0.5' tree1_backbone.mean | cut -f 1 | sed 's/$/\tring_shape\t2\tv/') > track2

# 绘图,加第一环矩形,展示丰度大于千万的特征
cat track* > graphlan_annotate.txt
graphlan_annotate.py --annot graphlan_annotate.txt tree1_backbone.txt graphlan.xml
graphlan.py graphlan.xml graphlan2_triangle.pdf --size 5

image

热图展示丰度

添加所有样品均值作为热图,作为第3环。

本质上热图即环形条带的透明度

# 环3用绿色不同的透明度展示丰度
cat cfg/heat3.cfg <(sed 's/\t/\tring_alpha\t3\t/g' tree1_backbone.mean) > track3

# 绘图绿色不同的透明度的3号环
cat track* > graphlan_annotate.txt
graphlan_annotate.py --annot graphlan_annotate.txt tree1_backbone.txt graphlan.xml
graphlan.py graphlan.xml graphlan3_heatmap.pdf --size 5

image

我们可以用同样原理,添加每个组,或每个样品的丰度热图。

柱状图显示丰度

# 环4用蓝色柱状图展示丰度
cat cfg/bar4.cfg <(sed 's/\t/\tring_height\t4\t/g' tree1_backbone.mean) > track4

# 绘图,环4用蓝色柱状图展示丰度
cat track* > graphlan_annotate.txt
graphlan_annotate.py --annot graphlan_annotate.txt tree1_backbone.txt graphlan.xml
graphlan.py graphlan.xml graphlan4_bar.pdf --size 5

image

附录1. 颜色

颜色有三种设置方法

  1. 颜色英文名称

blue, green, red, cyan, magenta, yellow, black, white

  1. 单个字母的缩写

‘b’ (blue), ‘g’ (green), ‘r’ (red), ‘c’ (cyan), ‘m’ (magenta), ‘y’ (yellow), ‘k’ (black), ‘w’ (white)

  1. RGB模式颜色

    rrggbb, for example #FF0000 corresponds to (full) red

附录2. 形状选择

  • ‘.’ : 点 point marker
  • ‘,’ : pixel marker
  • ‘o’ : 圈 circle marker
  • ‘v’ : 下三角 triangle_down marker
  • ‘^’ : triangle_up marker
  • ‘<’ : triangle_left marker
  • ‘>’ : triangle_right marker
  • ‘1’ : tri_down marker
  • ‘2’ : tri_up marker
  • ‘3’ : tri_left marker
  • ‘4’ : tri_right marker
  • ‘s’ : square marker
  • ‘R’ : 矩阵 rectangle marker
  • ‘p’ : pentagon marker
  • ‘*’ : star marker
  • ‘h’ : hexagon1 marker
  • ‘H’ : hexagon2 marker
  • ‘+’ : plus marker
  • ‘x’ : x marker
  • ‘D’ : diamond marker
  • ‘d’ : thin_diamond marker
  • ‘|’ : vline marker
  • ‘_’ : hline marker

Reference

http://huttenhower.sph.harvard.edu/graphlan

Asnicar, Francesco, George Weingart, Timothy L. Tickle, Curtis Huttenhower, and Nicola Segata. 2015. ‘Compact graphical representation of phylogenetic data and metadata with GraPhlAn’, PeerJ, 3: e1029.

Jingying Zhang, Yong-Xin Liu, Na Zhang, Bin Hu, Tao Jin, Haoran Xu, Yuan Qin, Pengxu Yan, Xiaoning Zhang, Xiaoxuan Guo, Jing Hui, Shouyun Cao, Xin Wang, Chao Wang, Hui Wang, Baoyuan Qu, Guangyi Fan, Lixing Yuan, Ruben Garrido-Oter, Chengcai Chu & Yang Bai. NRT1.1B is associated with root microbiota composition and nitrogen use in field-grown rice. Nature Biotechnology. 2019, 37: 676-684. doi:10.1038/s41587-019-0104-4

注:本文的代码来自我之前发表的Nature Biotechnology中的图5,如果参考本文代码绘制类似图,请引用以上两篇文章,谢谢!

猜你喜欢

写在后面

为鼓励读者交流、快速解决科研困难,我们建立了“宏基因组”专业讨论群,目前己有国内外5000+ 一线科研人员加入。参与讨论,获得专业解答,欢迎分享此文至朋友圈,并扫码加主编好友带你入群,务必备注“姓名-单位-研究方向-职称/年级”。技术问题寻求帮助,首先阅读《如何优雅的提问》学习解决问题思路,仍末解决群内讨论,问题不私聊,帮助同行。
image

学习扩增子、宏基因组科研思路和分析实战,关注“宏基因组”
image

image

点击阅读原文,跳转最新文章目录阅读
https://mp.weixin.qq.com/s/5jQspEvH5_4Xmart22gjMA



https://wap.sciencenet.cn/blog-3334560-1207955.html

上一篇:16S扩增子分析专题研讨论会——背景介绍
下一篇:COM:下一代微生物组技术在作物生产中的应用——局限性以及基于知识的解决方案的需求
收藏 IP: 210.75.224.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-3-28 16:52

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部