发现了一个计算Hill很好的包:Hilldiv
安装
>install.packages("devtools")
>library(devtools)
>install_github("anttonalberdi/hilldiv")
>library(hilldiv)
#依赖的重要的包: ggplot2, RColorBrewer, data.table, ape, ade4, iNEXT, iNextPD.
#ggplot2:可视化
#RColorBrewer #调色板,用于可视化
#ape #系统发育分析
#ade4 #生态和环境领域广泛使用
#data.table:数据清洗
#iNEXT和iNextPD:计算Hill number
iNEXT之前说过,见前文:
导入数据
>data(bat.diet.otutable) #OTU
>data(bat.diet.hierarchy) #分组文件
>data(bat.diet.tree) #有根树
#Create simple objects
>otu.table <- bat.diet.otutable
>otu.vector <- bat.diet.otutable[,1]
>names(otu.vector) <- rownames(otu.table)
>hierarchy.table <- bat.diet.hierarchy
>tree <- bat.diet.tree
重要函数
hill.div:
输入OTU 或者向量,计算对应的Hill。如果有树的信息,则会计算基于系统发育的Hill。
#Usage
#true.div(abund,qvalue,tree,type)
#qvalue:hill的阶数。q>=0
#type:abundance或者incidence
>hill.div(otu.table,1)
Msc1 Msc2 Msc3 Msc4 Msc5 Mmy1 Mmy2 Mmy3 Mmy4
1.505545 3.122265 2.012664 1.000000 3.238900 2.394539 1.410046 2.276801 2.000301
Mmy5 Rme1 Rme2 Rme3 Rme4 Rme5 Mda1 Mda2 Mda3
1.006834 1.008112 1.000000 1.156695 1.081390 2.173392 1.009006 2.987138 4.576420
Mda4 Mda5 Mca1 Mca2 Mca3 Mca4 Mca5 Reu1 Reu2
3.528198 1.237994 2.503107 3.204414 1.118893 3.472679 1.010790 2.079217 1.905770
Reu3 Reu4 Reu5 Mem1 Mem2 Mem3 Mem4 Mem5 Rhi1
2.994990 1.435739 1.496800 4.265005 1.159175 7.853560 2.426102 3.156563 4.838018
Rhi2 Rhi3 Rhi4 Rhi5
2.331697 1.414016 3.972967 5.138890
>hill.div(otu.table,1,tree)
Msc1 Msc2 Msc3 Msc4 Msc5 Mmy1 Mmy2 Mmy3 Mmy4
1.152278 1.199440 1.071783 1.000000 1.120999 1.202859 1.011519 1.237840 1.223297
Mmy5 Rme1 Rme2 Rme3 Rme4 Rme5 Mda1 Mda2 Mda3
1.001831 1.000436 1.000000 1.017877 1.006787 1.058370 1.003925 1.265173 1.669337
Mda4 Mda5 Mca1 Mca2 Mca3 Mca4 Mca5 Reu1 Reu2
1.475675 1.077681 1.250052 1.401592 1.030458 1.174893 1.002285 1.098038 1.048106
Reu3 Reu4 Reu5 Mem1 Mem2 Mem3 Mem4 Mem5 Rhi1
1.111219 1.224831 1.050408 1.220784 1.013132 1.573986 1.265680 1.368388 1.253371
Rhi2 Rhi3 Rhi4 Rhi5
1.174519 1.077542 1.333563 1.390569
>hill.div(otu.table,1,tree,type="incidence")
[1] 2.994891
div.profile:
hill的阶数和其值的关系
#Usage
#div.profile(abund,qvalues,tree,values,hierarchy,level,colour,log)
#qvalue: default from 0 to 5. order=seq(from = 0, to = 5, by = (0.1))
#level: alpha或gamma。默认为gamma多样性。
#log:Hill是否取对数
# One sample
>div.profile(otu.vector)# Multiple individual samples (first 5 samples of the OTU table)
>div.profile(otu.table[,c(1:5)])
div.test:
多组之间的多样性比较
#Usage
#div.test(otutable,qvalue,hierarchy,tree)
# 先进行Shapiro and Barlett tests检验数据的正态性和均质性;再根据结果选择显著性检验的方法。
#两组:Student’s t-test or a Wilcoxon Rank Sum Test
#多组:ANOVA or a Kruskal-Wallis test
#Contrast based on Hill numbers
>div.test(otu.table,qvalue=0,hierarchy=hierarchy.table)
$normality.pvalue
[1] 0.06082876
$homogeneity.pvalue
[1] 0.5009248
$groups
[1] 8
$method
[1] "ANOVA"
$test
Df Sum Sq Mean Sq F value Pr(>F)
Group 7 598.7 85.53 2.55 0.0332 *
Residuals 32 1073.2 33.54
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
div.test.plot:
多组之间的多样性比较的可视化
#Usage
#div.test.plot(divtest,chart,colour)
#chart 可选择 boxplot 或 jitter 或 violin plot
>contrast.div.q0 <- div.test(otu.table,qvalue=0,hierarchy=hierarchy.table)
>colours <- c("#35a849","#9d1923","#f7ab1b","#ed7125","#cc4323","#b6d134","#fcee21","#085ba7")
#Box plot
>div.test.plot(contrast.div.q0,chart="box")
#Jitter plot
>div.test.plot(contrast.div.q0,chart="jitter",colour=colours)
#Violin plot
>div.test.plot(contrast.div.q0,chart="violin",colour=c("#35a849","#9d1923","#f7ab1b","#ed7125","#cc4323","#b6d134","#fcee21","#085ba7"))
div.part:
多样性分解。通过计算alpha和gamma多样性,用gamma/alpha得到beta多样性。
对于变量分解,前文提到过:
#Usage
#div.part(otutable,qvalue,hierarchy,tree,type)
#Abundance-based
>div.part(otu.table,qvalue=1)
$Hierarchical_levels
[1] 2
$Type
[1] "abundance"
$Order_diversity
[1] 1
$Sample_size
[1] 40
$L1_diversity
[1] 2.087489
$L2_diversity
[1] 61.96846
$Beta_diversity
[1] 29.68564
beta.dis:
基于beta多样性;样本大小及hill的阶数进行相似性或不相似性分析。 可计算 Sørensen-type overlap (CqN),
Jaccard-type overlap (UqN),
Sørensen-type turn over-complement (VqN),
Jaccard-type turn over-complement (SqN)
#Usage
#beta.dis(beta,qvalue,N,metric,type)
#N:样本数
#matrix: "C", "U", "V" or "S".
#type:"similarity" or "dissimilarity".
>beta.dis(beta=4.5,qvalue=1,N=8,metric="C",type="similarity")
[1] 0.2766937
pair.dis:
成对样本之间的多样性分解
#Usage
#pair.dis(otutable,qvalue,tree,hierarchy,level,measure)
#level:1 or 2.1表示在所有样本之间计算不相似性;2表示在组间计算不相似性。默认1
>pair.dis(otu.table,qvalue=0,hierarchy=hierarchy.table)
#结果会给出"C", "U", "V","S"这几种所有的不相似性。
pair.dis.plot:
可视化
#Usage
#pair.dis.plot(distance,hierarchy,type,level,colour,magnify)
#NMDS chart, a qgraph plot or a heatmap/correlogram.
#type:NMDS or qgraph
>pair.div.q0.L2 <- pair.dis(otu.table[,sort(colnames(otu.table))],qvalue=0,hierarchy=hierarchy.table,level="2")
#Plot NMDS
>pair.dis.plot(pair.div.q0.L2$L2_CqN,hierarchy=hierarchy.table,type="NMDS",level=2)
#Plot qgraph
>pair.dis.plot(pair.div.q0.L2$L2_CqN,hierarchy=hierarchy.table,type="qgraph",level=2,magnify=TRUE)
alpha.div :
计算alpha多样性指数
#Usage
#alpha.div(otutable,qvalue,weight)
>alpha.div(otutable=otu.table,qvalue=0)
[1] 10.45
Reference: https://github.com/anttonalberdi/hilldiv/
彩蛋
如果科研不能使我快乐,那将毫无意义~
Github上有大神用Javascript写了一个CXK 打篮球游戏,实在是太好玩了哈哈~
Github:
https://github.com/kasuganosoras/cxk-ball
游戏网址:
https://cxk.ssrr.one/
你看上面这堵墙它又长又宽,就像CXK顶的球它又大又圆~