一、引言
microeco包 是由福建农林大学姚敏杰教授团队开发的一个强大工具,专注于扩增子测序数据的集成分析 。该包集成了扩增子测序下游分析的多种功能,包括群落组成、多样性分析、网络分析、零模型等多方面内容。通过几行简单的代码,研究者就能实现复杂的分析需求。因此,自microeco包发布以来便受到了学术界的广泛关注,截至推文发稿日期,引用次数已达718次,是该领域的高被引文章。

之前一期我们介绍了microeco包中微生物网络分析,有需要的可跳转至扩增子分析|基于R语言microeco包进行微生物群落网络分析(network网络、Zi-Pi和subnetwork子网络图)交流与学习。这一期我们带来microeco包用于零模型计算。
在过去的几十年里,系统发育分析和零模型的结合 ,为更深入地探究生态群落组装过程中的生态位与中性过程提供了强有力的支持。通过引入系统发育维度,这种方法可以更全面地分析群落组装机制。Microeco包中trans_nullmodel模块提供了一种封装方案,包括系统发育信号、β平均成对系统发育距离(betaMPD)、β平均最近分类单元距离(betaMNTD)、β最近分类单元指数(betaNTI)、β净相关性指数(betaNRI) 以及基于Raup-Crick(RCbray)的计算。RCbray与betaNTI的组合应用可以在特定假设下推断出主导群落组装的生态过程强度。
二、示例数据和R代码
这套代码基于microeco包自带的数据进行分析,同时我们也提供了读取自己数据的方法,可私信小编获取代码。
2.1 数据基本操作之建立微表
R
#安装microeco包
if(!require("microeco")){install.packages("microeco")}
library(microeco)
#安装并加载ape包
if(!require("ape")){install.packages("ape")}
library(ape)
##################数据基本操作#######################
# 样本信息表;data.frame
data(sample_info_16S)
# 特征表;data.frame
data(otu_table_16S)
# 分类分配表;data.frame
data(taxonomy_table_16S)
# 系统发育树;不是必需的;用于系统发育分析
# Newick格式;使用ape包的read.tree函数读取树文件
data(phylo_tree_16S)
# 加载环境数据表(如果它不在样本表中)
data(env_data_16S)
# 使用magrittr包中的管道操作符
if(!require("magrittr")){install.packages("mgrittr")}
library(magrittr)
# 固定随机数生成,以使结果可重复
set.seed(123)
# 使绘图背景与教程相同
if(!require("ggplot2")){install.packages("ggplot2")}
library(ggplot2)
theme_set(theme_bw())
# 统一分类信息,自动统一分类前缀
taxonomy_table_16S %<>% tidy_taxonomy
# 在R6类中,'$new' 是用于创建类的新对象的原始方法
# 如果你只提供丰度表,该类可以帮助你创建一个样本信息表
mt <- microtable$new(otu_table = otu_table_16S)
# 通常情况下添加元数据
mt <- microtable$new(otu_table = otu_table_16S, sample_table = sample_info_16S)
mt
# 让我们创建一个包含更多信息的 microtable 对象
mt <- microtable$new(sample_table = sample_info_16S, otu_table = otu_table_16S, tax_table = taxonomy_table_16S, phylo_tree = phylo_tree_16S)
mt
#################数据清洗###########################
#删除非细菌和古菌的OTU
# 使用 R 的 subset 函数过滤 tax_table 中的分类单元
mt$tax_table %<>% base::subset(Kingdom == "k__Archaea" | Kingdom == "k__Bacteria")
mt
# 使用 grepl 函数的另一种方式
#mt$tax_table %<>% .[grepl("Bacteria|Archaea", .$Kingdom), ]
#删除线粒体或叶绿体OTU
# 这将删除 tax_table 中包含这些词的行,忽略分类级别并忽略大小写。
# 因此,如果你想过滤不被认为是污染的某些分类单元,请像之前那样使用 subset 来过滤 tax_table。
mt$filter_pollution(taxa = c("mitochondria", "chloroplast"))
#使所有文件中OTU和样本信息一致,使用tidy_dataset修剪函数
mt$tidy_dataset()
mt
#使用 sample_sums() 来检查每个样本中的序列号
mt$sample_sums() %>% range
#样本稀疏化,每个样本的序列数标准化为相同的数量
mt$rarefy_samples(sample.size = 10000)
mt
#save_table可以将微表对象中的所有基础数据保存到本地文件中,包括特征丰度、元数据、分类表、系统发育树和代表序列
mt$save_table(dirpath = "basic_files", sep = ",")
# 生成 trans_nullmodel 对象
# 作为一个示例,我们只使用平均相对丰度 > 0.0005 的高丰度 OTU
t1 <- trans_nullmodel$new(mt, filter_thres = 0.0005, add_data = env_data_16S)
2.2 NST模型分析加绘图
对于NST方法的理论和实操我们团队张鼎言同学出了两期详尽的教程,感兴趣的可以跳转至以下链接学习。
NST 包 | 从原理到应用,教你理解群落组装机制中随机性过程、确定性过程的相对贡献(数学公式、表现性能详细介绍)
NST 包 | 从原理到应用,教你理解群落组装机制中随机性过程、确定性过程的相对贡献之应用篇( NST 变式 MST 和 pNST 及代码实例)
microeco提供了一种方便的计算和绘图方法,详细代码如下:
R
# 需要安装 NST 包
if(!require("NST")){install.packages("NST")} #判断安装NST包
t1$cal_NST(method = "tNST", group = "Group", dist.method = "bray", abundance.weighted = TRUE, output.rand = TRUE, SES = TRUE)
# 计算每个 group 的平均 NST 值和标准差
nst_stats <- nst_data %>%
group_by(group) %>%
summarize(
avg_NST = mean(NST, na.rm = TRUE),
sd_NST = sd(NST, na.rm = TRUE)
)
# 将 index.pair.grp 导出为 CSV 文件
write.csv(t1$res_NST$index.pair.grp, file = "NST_index_pair_grp.csv", row.names = FALSE)
🌟 输出结果
保存为NST_index_pair_grp.csv

R
######################绘制NST柱状图#####################
# 安装 rstatix 包(如未安装)
if (!requireNamespace("rstatix", quietly = TRUE)) {
install.packages("rstatix")
}
# 加载所需的包
library(ggplot2)
library(dplyr)
library(ggsci)
library(ggpubr)
library(rstatix)
# 计算每个组的平均 NST 值和标准差
nst_stats <- t1$res_NST$index.pair.grp %>%
group_by(group) %>%
summarise(
avg_NST = mean(NST.ij.bray, na.rm = TRUE),
sd_NST = sd(NST.ij.bray, na.rm = TRUE)
)
# 计算各组之间的t检验
t_test_results <- t1$res_NST$index.pair.grp %>%
pairwise_t_test(NST.ij.bray ~ group, p.adjust.method = "bonferroni")
# 绘制柱状图
p <- ggplot(nst_stats, aes(x = group, y = avg_NST, fill = group)) +
geom_col(width = 0.7) +
geom_errorbar(aes(ymin = avg_NST - sd_NST, ymax = avg_NST + sd_NST), width = 0.2) +
theme_bw() +
ylab("Normalized stochasticity ratio(NST)") +
xlab("") +
scale_y_continuous(limits = c(0, 1), labels = scales::percent_format()) + # 设置 y 轴范围为 0 到 100
scale_color_npg() + # 使用 ggsci 中的颜色
theme(legend.position = "none")
# 显示图表
print(p)
# 保存柱状图
ggsave("NST_Barplot.png", plot = p, width = 4, height = 6, dpi = 300)
🌟输出结果

2.3 pNST模型分析
引入系统发育树计算pNST:
R
###########################pNST#############################
# 对于 pNST 方法,需要系统发育树文件
t1$cal_NST(method = "pNST", group = "Group", output.rand = TRUE, SES = TRUE)
t1$cal_NST_test(method = "nst.boot")
# 将 index.pair.grp 导出为 CSV 文件
write.csv(t1$res_NST$index.pair.grp, file = "pNST_index_pair_grp.csv", row.names = FALSE)
🌟输出结果
保存为pNST_index_pair_grp.csv

R
# 绘制带显著性标记的箱线图
p <- ggboxplot(data = t1$res_NST$index.pair.grp,
x = 'group',
y = 'MST.ij.bMNTD',
color = 'group') +
stat_compare_means(method = 'wilcox.test',
comparisons = list(c('IW', 'CW'), c('IW', 'TW'), c('CW', 'TW')),
label = "p.signif") + # 显示显著性标记
labs(y = 'Phylogenetic Normalized Stochasticity Ratio (pNST)') +
labs(x='')+
ylim(0, 1.4) + # 设置 y 轴范围
theme_bw() + # 设置主题为简洁风格
theme(axis.text.x = element_text(angle = 45, hjust = 1)) # x 轴标签倾斜
# 显示图形
print(p)
# 保存柱状图
ggsave("pNST_boxplot.png", plot = p, width = 4, height = 5, dpi = 300)
2.4 βNRI模型分析
R
##################βNRI模型计算##########################
# 查看 null.model 参数以了解其他零模型
# 本示例中,零模型运行 500 次
t1$cal_ses_betampd(runs = 500, abundance.weighted = TRUE)
# 将 betaNRI 矩阵添加到 beta_diversity 列表中
mt$beta_diversity[["betaNRI"]] <- t1$res_ses_betampd
# 创建 trans_beta 类,使用的测量方法为 "betaNRI"
t2 <- trans_beta$new(dataset = mt, group = "Group", measure = "betaNRI")
# 计算每个组的距离
t2$cal_group_distance()
# 将 index.pair.grp 导出为 CSV 文件
write.csv(t2$res_group_distance, file = "betaNRI.csv", row.names = FALSE)
# 查看帮助文档,了解更多方法,如 "anova" 和 "KW_dunn"
# 计算组间距离差异,使用 Wilcoxon 检验方法
t2$cal_group_distance_diff(method = "wilcox")
# 绘制结果
g1 <- t2$plot_group_distance(add = "mean")
p<- g1 + geom_hline(yintercept = -2, linetype = 2) +
geom_hline(yintercept = 2, linetype = 2)+
theme_bw()+
ylim(-3.4,6.5)
# 显示图形
print(p)
# 保存柱状图
ggsave("pNST_boxplot.png", plot = p, width = 4, height = 4.5, dpi = 300)

2.5 基于βNTI和RCbray的生态过程分析
βNTI和RCbray原理和计算以及绘图我们之前也出了两期进行阐述,感兴趣的可以跳转以下链接学习与交流:
(文末附代码)基于零模型的群落确定性和随机性构建过程 ------R 中实例计算
零模型 2------ 基于 βNTI 的微生物随机性和确定性装配过程(箱线图和柱状图 R 中实现)
microeco提供一行代码可实现生态过程计算,具体代码如下:
R
######################基于βNTI和RCbray的模型######################
# 零模型运行500次
t1$cal_ses_betamntd(runs = 500, abundance.weighted = TRUE, null.model = "taxa.labels")
# 返回 t1$res_ses_betamntd 结果
# 结果存储在 t1$res_rcbray 中
t1$cal_rcbray(runs = 1000)
# 返回 t1$res_rcbray 结果
# 使用 betaNTI 和 rcbray 评估组装过程
t1$cal_process(use_betamntd = TRUE, group = "Group")
# 使用ggplot2绘制柱状图
p<- ggplot(t1$res_process, aes(x = Group, y = percentage, fill = process)) +
geom_bar(stat = "identity", position = "stack") + # 使用堆叠柱状图
labs(y = 'Ecological Processes (%)') +
theme_bw()+
scale_fill_d3() # 使用 ggsci 中的颜色
p
# 保存柱状图
ggsave("EP.png", plot = p, width = 4, height = 4, dpi = 300)

三、参考文献
[1] Chi Liu, Yaoming Cui, Xiangzhen Li, Minjie Yao, microeco: an R package for data mining in microbial community ecology, FEMS Microbiology Ecology, Volume 97, Issue 2, February 2021, fiaa255
[2] microeco_tutorial/model-based-class.html#trans_nullmodel-class
四、相关信息
!!!本文内容由小编总结互联网和文献内容总结整理,如若侵权,联系立即删除!
📌示例代码中提供了数据和代码,小编已经测试,可直接运行。
以上就是本节所有内容。