「微生物组」基于LorMe包进行微生物组数据分析—Beta多样性分析!!!

文摘   科学   2024-10-14 09:05   宁夏  





为了避免各位错过最新的推文教程,强烈建议大家将“科研后花园”设置为“星标”!


1、安装并加载LorMe包:
###安装LorMe包install.packages("LorMe") ##加载MicrobiomeStat包library(LorMe) # Lightening One-Code Resolving Microbial Ecology Solutionlibrary(ggplot2) # Create Elegant Data Visualisations Using the Grammar of Graphicslibrary(dplyr) # A Grammar of Data Manipulation
2、加载数据(这里使用LorMe包自带数据集)并对分析所用的数据进行封装:
##加载示例数据集data(testotu) # Load the standard Qiime output feature tablehead(testotu) # First column -ID, last column -taxonomic annotation, others - the feature table.feature_table<- testotu[, -c(1,22)]tax_anno<- testotu[, c(1,22)]
#创建分组数据groupinformation <- data.frame( group = c(rep("a", 10), rep("b", 10)), factor1 = rnorm(10), factor2 = rnorm(mean = 100, 10),   subject = factor(c(1:10, 1:10)),  group2 = c(rep("e", 5), rep("f", 5), rep("e", 5), rep("f", 5)))
###对数据进行封装test_object <- tax_summary( groupfile = groupinformation, inputtable = feature_table, reads = TRUE, taxonomytable = tax_anno)
###对一些默认设置进行配置my_col=color_scheme("Plan1")my_order=c("b","a")my_facet_order=c("e","f")test_object_plan2 <- object_config( taxobj = test_object, treat_location = 1, rep_location = 4, facet_location = 5,   subject_location = NULL,  treat_col = my_col, treat_order = my_order,  facet_order = my_facet_order)
# Taxonomy tidying...# Genarating tables...# Done# # Contained elements:#   [1] "Groupfile"       "Base"            "Base_percent"   # [4] "Base_taxonomy"   "Phylum"          "Phylum_percent" # [7] "Phylum_taxonomy" "Genus"           "Genus_percent"  # [10] "Genus_taxonomy"  "parameters"     # Warning message:#   Expected 7 pieces. Missing pieces filled with `NA` in 261 rows [4,#                                                                   6, 9, 12, 18, 20, 24, 32, 37, 43, 44, 45, 46, 58, 59, 68, 70, 71,#                                                                   72, 76, ...].
3、Beta多样性分析(由于如上示例数据的分组聚类效果并不是很好,所以这里使用另一组示例数据Two_group):

1)进行Beta多样性分析:

community_structure<- structure_plot(taxobj = Two_group,                                     taxlevel = "Base",                                      diagram = "stick",                                     ellipse.level = 0.95)
###PERMANOVA has done######PCA has done######PCoA has done###Run 0 stress 0.08827856 Run 1 stress 0.09328546 Run 2 stress 0.1106891 Run 3 stress 0.08984926 Run 4 stress 0.09519836 Run 5 stress 0.08827854 ... New best solution... Procrustes: rmse 0.0001176953  max resid 0.0002718019 ... Similar to previous bestRun 6 stress 0.08827854 ... New best solution... Procrustes: rmse 5.594382e-05  max resid 0.0001283387 ... Similar to previous bestRun 7 stress 0.1140033 Run 8 stress 0.1067692 Run 9 stress 0.09216223 Run 10 stress 0.08827854 ... New best solution... Procrustes: rmse 3.776737e-06  max resid 6.700617e-06 ... Similar to previous bestRun 11 stress 0.08984929 Run 12 stress 0.110387 Run 13 stress 0.1091767 Run 14 stress 0.08827855 ... Procrustes: rmse 1.143503e-05  max resid 2.70159e-05 ... Similar to previous bestRun 15 stress 0.1091765 Run 16 stress 0.09171475 Run 17 stress 0.09359075 Run 18 stress 0.09459429 Run 19 stress 0.09328543 Run 20 stress 0.08827854 ... New best solution... Procrustes: rmse 3.025526e-05  max resid 6.993105e-05 ... Similar to previous best*** Best solution repeated 1 times###NMDS has done#####Output list###PERMANOVA#PERMANOVA:named as('PERMANOVA_statistics')##Plot#PCAplot:named as('PCA_Plot')(1/3)PCoAplot:named as('PCoA_Plot')(2/3)NMDSplot:named as('NMDS_Plot')(3/3)#Analysis object#PCA object:named as('PCA_object')PCoA object:named as('PCoA_object')NMDS object:named as ('NMDS_object')#Coordinates dataframe#PCA Coordinates dataframe:named as('PCA_coordinates')PCoA Coordinates dataframe:named as('PCoA_coordinates')NMDS Coordinates dataframe:named as('NMDS_coordinates')##Done##

2)查看PERMANOVA结果:

print(community_structure$PERMANOVA_statistics)
Permutation test for adonis under reduced modelTerms added sequentially (first to last)Permutation: freeNumber of permutations: 999
adonis2 Df SumOfSqs R2 F Pr(>F) Group 1 0.6802 0.2045 3.5991 0.001 ***Residual 14 2.6460 0.7955 Total 15 3.3263 1.0000 ---Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

3)数据查看及可视化(LorMe包Beta多样性分析提供PCoA、PCA以及NMDS三种方法供大家选择):

##PCoA#数据community_structure$PCoA_coordinates#图形community_structure$PCoA_Plot
 PCOA1(22.5%)  PCOA2(10.7%) SampleID     Group Rep      cent1       cent21   -0.20083569  1.920869e-02  Sample1 Treatment   1 -0.2023718  0.014930652   -0.32562289  1.252941e-01  Sample2 Treatment   2 -0.2023718  0.014930653    0.21837293  3.294172e-02  Sample3   Control   1  0.2023718 -0.014930654    0.22946699  4.762851e-02  Sample4   Control   2  0.2023718 -0.014930655    0.19816585 -3.728299e-05  Sample5   Control   3  0.2023718 -0.014930656    0.31838249  1.962647e-01  Sample6   Control   4  0.2023718 -0.014930657    0.32406834  2.008773e-01  Sample7   Control   5  0.2023718 -0.014930658    0.13987165 -2.460515e-01  Sample8   Control   6  0.2023718 -0.014930659    0.06210269 -9.246651e-02  Sample9   Control   7  0.2023718 -0.0149306510   0.12854385 -2.586021e-01 Sample10   Control   8  0.2023718 -0.0149306511  -0.18395269  7.715084e-02 Sample11 Treatment   3 -0.2023718  0.0149306512  -0.20509628 -7.482498e-02 Sample12 Treatment   4 -0.2023718  0.0149306513  -0.19444461 -1.052167e-01 Sample13 Treatment   5 -0.2023718  0.0149306514  -0.22643263  7.214771e-02 Sample14 Treatment   6 -0.2023718  0.0149306515  -0.06648091 -2.118060e-01 Sample15 Treatment   7 -0.2023718  0.0149306516  -0.21610909  2.174914e-01 Sample16 Treatment   8 -0.2023718  0.01493065

##PCAcommunity_structure$PCA_coordinatescommunity_structure$PCA_Plot
         PC1        PC2 SampleID     Group Rep     cent1     cent21  -23.580643 -24.365888  Sample1 Treatment   1 -15.56312 -18.496382  -13.599862 -11.109099  Sample2 Treatment   2 -15.56312 -18.496383    4.124383  18.151454  Sample3   Control   1  15.56312  18.496384    2.665482  20.852532  Sample4   Control   2  15.56312  18.496385    3.174064  83.615681  Sample5   Control   3  15.56312  18.496386  123.236966 -29.287876  Sample6   Control   4  15.56312  18.496387   14.644622  18.705873  Sample7   Control   5  15.56312  18.496388   -6.802447  13.787313  Sample8   Control   6  15.56312  18.496389   -8.220349  11.556028  Sample9   Control   7  15.56312  18.4963810  -8.317801  10.590007 Sample10   Control   8  15.56312  18.4963811  -8.653012  -5.414103 Sample11 Treatment   3 -15.56312 -18.4963812 -11.907421  -8.518922 Sample12 Treatment   4 -15.56312 -18.4963813 -27.574772 -69.790647 Sample13 Treatment   5 -15.56312 -18.4963814 -15.355070 -13.360412 Sample14 Treatment   6 -15.56312 -18.4963815 -10.237921   0.994979 Sample15 Treatment   7 -15.56312 -18.4963816 -13.596220 -16.406920 Sample16 Treatment   8 -15.56312 -18.49638

##NMDScommunity_structure$NMDS_coordinatescommunity_structure$NMDS_Plot
         MDS1        MDS2 SampleID     Group Rep      cent1       cent21  -0.2957033 -0.07279166  Sample1 Treatment   1 -0.3300792 -0.021048522  -0.5727358 -0.10099619  Sample2 Treatment   2 -0.3300792 -0.021048523   0.3567468 -0.13451069  Sample3   Control   1  0.3300792  0.021048524   0.4533141  0.11348936  Sample4   Control   2  0.3300792  0.021048525   0.3040678  0.02462535  Sample5   Control   3  0.3300792  0.021048526   0.5454692 -0.26255489  Sample6   Control   4  0.3300792  0.021048527   0.5723497 -0.13970404  Sample7   Control   5  0.3300792  0.021048528   0.1785089  0.22568280  Sample8   Control   6  0.3300792  0.021048529   0.0744541  0.04720751  Sample9   Control   7  0.3300792  0.0210485210  0.1557234  0.29415275 Sample10   Control   8  0.3300792  0.0210485211 -0.2637770 -0.18606664 Sample11 Treatment   3 -0.3300792 -0.0210485212 -0.2351927  0.05343645 Sample12 Treatment   4 -0.3300792 -0.0210485213 -0.3856578  0.17168116 Sample13 Treatment   5 -0.3300792 -0.0210485214 -0.3575036 -0.02875466 Sample14 Treatment   6 -0.3300792 -0.0210485215 -0.1262922  0.21872544 Sample15 Treatment   7 -0.3300792 -0.0210485216 -0.4037715 -0.22362206 Sample16 Treatment   8 -0.3300792 -0.02104852

4)由于可视化图形比较固定,所以小编这里也是提取计算结果并自定义可视化:

##以PCoA结果为例PCoA_data <- community_structure$PCoA_coordinatesPCoA_data %>%   ggplot(aes(`PCOA1(22.5%)`,`PCOA2(10.7%)`))+  #质心连线  geom_segment(aes(x =cent1, y = cent2,                   xend =`PCOA1(22.5%)`, yend = `PCOA2(10.7%)`,                   color = Group),               alpha = 0.6,linewidth = 1, show.legend = FALSE)+  geom_point(aes(fill = Group), shape =21, color = "black",size =3)+  stat_ellipse(aes(fill=Group,color=Group),               geom = "polygon",                level = 0.9,#置信度95%               linetype=1,linewidth=0.6,alpha=0.2)+  #主题设置  theme_bw()+  theme(panel.grid = element_blank(),        axis.text = element_text(size = 10),        axis.title = element_text(color = "black", size = 12))+  scale_color_manual(values = c("#f68d42","#5ec6f2"))+  scale_fill_manual(values = c("#f68d42","#5ec6f2"))

##同理绘制NMDS图形NMDS_data <- community_structure$NMDS_coordinatesNMDS_data %>%   ggplot(aes(MDS1,MDS2))+  #质心连线  geom_segment(aes(x =cent1, y = cent2,                   xend =MDS1, yend = MDS2,                   color = Group),               alpha = 0.6,linewidth = 1, show.legend = FALSE)+  geom_point(aes(fill = Group), shape =21, color = "black",size =3)+  stat_ellipse(aes(fill=Group,color=Group),               geom = "polygon",                level = 0.9,#置信度95%               linetype=1,linewidth=0.6,alpha=0.2)+  #主题设置  theme_bw()+  theme(panel.grid = element_blank(),        axis.text = element_text(size = 10),        axis.title = element_text(color = "black", size = 12))+  scale_color_manual(values = c("#f68d42","#5ec6f2"))+  scale_fill_manual(values = c("#f68d42","#5ec6f2"))

更多内容大家请阅读LorMe包的帮助文档!

参考:LorMe包帮助文档


微生物组系列推文合集:

扩增子测序数据分析还不会?小编整理的全套R语言代码助您轻松解决问题!(更新版


「微生物组」基于LorMe包进行微生物组数据分析—Alpha多样性分析!!!


微生物组」基于microeco包进行共现性网络分析!!!


「微生物组」基于microeco包进行lefsef分析及随机森林分析!!!


「微生物组」Stamp结果图复现——分组差异图的绘制!!!


「微生物组」基于microeco包进行环境数据关联分析—以RDA分析为例!!!


「微生物组」基于microeco包进行共享/独特OTU计算及可视化!!!


微生物组」基于microeco包进行Alpha多样性分析!!!


「微生物组」基于microeco包进行微生物群落结构组成分析!!!


R可视化——Mantel test分析及可视化


基于R语言的微生物群落组成多样性分析——α多样性及其可视化


基于R语言的微生物群落组成多样性分析—β多样性之PCoA分析


基于R语言的微生物群落组成多样性分析—β多样性之组间差异性检验


基于R语言的微生物群落组成多样性分析——种丰度计算及可视化


基于R语言的微生物群落组成多样性分析——种丰度可视化之热图(Heatmap)


基于R语言的微生物群落组成多样性分析——种丰度可视化之弦图(Chord Diagram)


基于R语言的微生物群落组成多样性分析——共线性网络分析


基于R语言的微生物群落组成多样性分析——RDA分析


基于R语言的微生物群落组成多样性分析——CCA分析


基于R语言的微生物群落组成多样性分析——PCA分析


基于R语言的微生物群落组成多样性分析——聚类分析


基于LEfSe分析进行微生物物种差异及关联分析


基于R语言的微生物群落组成分析—差异OTU筛选及展示


基于R语言的微生物群落组成多样性分析——NMDS分析


基于R语言的微生物群落组成分析—α多样性指数的正态分布检验及差异性标注()


R可视化——基于MicrobiomeStat包进行微生物组数据分析(更新版)!!!


基于R语言进行扩增子数据的UMAP分析!


R可视化——共现性网络分析第二弹


R可视化——基于MicrobiotaProcess包进行物种群落结构组成分析!


R可视化——基于MicrobiotaProcess包进行物种差异分析!


基于microeco包进行PCoA分析!!!


基于MicrobiotaProcess包进行PCoA分析!


R可视化——基于MicrobiotaProcess包进行Alpha多样性分析!

PS: 以上内容是小编个人学习代码笔记分享,仅供参考学习,欢迎大家一起交流学习。

R绘图模板合集(包括附带注释的源码、测试数据及绘制效果图)可在公众号后台菜单栏查看具体获取方式!绘图模板合集效果图展示:


科研后花园
专注于R语言绘图及数据分析!
 最新文章