- phyloseq: Explore microbiome pro
- phyloseq: Explore microbiome pro
- phyloseq: Explore microbiome pro
- phyloseq: Explore microbiome pro
- phyloseq: Explore microbiome pro
- phyloseq: Explore microbiome pro
- phyloseq: Explore microbiome pro
- phyloseq: Explore microbiome pro
- phyloseq: Explore microbiome pro
- phyloseq: Explore microbiome pro
本节主要是在PhyloseqTutorials学习distance的函数功能.
加载包,包括phyloseq
library("phyloseq"); packageVersion("phyloseq")
library("ggplot2"); packageVersion("ggplot2")
#(d)plyr提供了一种灵活的数据操作语法
library("plyr"); packageVersion("plyr")
#ggplot2包主题设置
theme_set(theme_bw())
The distance function
distance
函数接受一个phyloseq-class
对象和方法选项,并返回一个适合某些排序方法和其他基于距离的分析的dist-class距离对象,目前在phyloseq包中有44个明确支持的方法选项,以及用户通过与vegetarian::designdist
的接口提供的任意方法.对于方法参数的当前支持的选项/参数的完整列表,在R会话的命令行中输入distanceMethodList
,有关详细信息,请参阅“distance”的包内文档
?distance
Usage
distance(phyloseq,method="unifrac",type="samples",...)
Example: “Enterotypes” dataset using many different methods
因为distance()
函数将距离计算组织成一个函数,计算所有支持的距离方法并研究结果是比较简单的,下面的代码将对“Enterotypes”数据集执行这样的循环,执行多维缩放(也称为主坐标分析),并绘制前两个轴,根据测序技术对每个图中的点进行着色和塑形,并分配“Enterotype”标签。
Load Data
加载肠型数据
data(enterotype)
一些初步的筛选。建议使用更高级的preprocessing。移除包含所有未分配序列的otu"-1"
enterotype <- subset_taxa(enterotype, Genus != "-1")
可用的距离方法编码distance
#将列表方式的distanceMethodList转换成非列表的展现方式
dist_methods <- unlist(distanceMethodList)
print(dist_methods)
删除需要树的两个距离方法和需要用户定义的距离参数的通用自定义方法
#这些函数需要tree
dist_methods[(1:3)]
#移除这些函数
dist_methods<-dist_methods[-(1:3)]
#用户定义的方法
dist_methods["designdist"]
#移除用户定义的距离.which函数给出逻辑对象的真实下标,允许数组下标。eg:which(LETTERS == "R")
dist_methods<-dist_methods[-which(dist_methods=="ANY")]
依次通过每个距离方法,将每个图保存在列表中,称为plist
#vector函数向量产生一个给定length和Mode的向量
plist<-vector("list",length(dist_methods))
names(plist)<-dist_methods
for(i in dist_methods) {
#计算距离矩阵
iDist<-distance(enterotype,method=i)
#Calculate ordination,对phyloseq数据进行排序
iMDS<-ordinate(enterotype,"MDS",distance=iDist)
##Make plot
#不能继续存在先前的图片(如果错误,p将会是blank)
p<-NULL
#创建plot,存储为临时变量,p
p<-plot_ordination(enterotype,iMDS,color="SeqTech",shape="Enterotype")
#Add title to each plot
p<-p+ggtitle(paste("MDS using distance method",i,sep="")
#Save the graphic to file.
plist[[i]]=p
}
#上述其实先是将利用了for循环,然后将利用distance函数各种距离进行了计算,随后利用ordinate函数利用distance的结果进行了排序的分析,最后利用plot_ordination函数进行了图片的绘制,利用ggtitle添加了标题。
Combine results
根据测序技术着色
#ldply拆分列表,应用函数,并在数据框中返回结果
df = ldply(plist, function(x) x$data)
names(df)[1] <- "distance"
p = ggplot(df, aes(Axis.1, Axis.2, color=SeqTech, shape=Enterotype))
p = p + geom_point(size=3, alpha=0.5)
p = p + facet_wrap(~distance, scales="free")
p = p + ggtitle("MDS on various distance metrics for Enterotype dataset")
p
按照肠型着色
df = ldply(plist, function(x) x$data)
names(df)[1] <- "distance"
p = ggplot(df, aes(Axis.1, Axis.2, color=Enterotype, shape=SeqTech))
p = p + geom_point(size=3, alpha=0.5)
p = p + facet_wrap(~distance, scales="free")
p = p + ggtitle("MDS on various distance metrics for Enterotype dataset")
p
网友评论