谁主沉浮😳 1谁主丶沉浮1
签名是一种态度,我想我可以更酷...
关注数: 15 粉丝数: 13 发帖数: 214 关注贴吧数: 31
已经下载Picante包却找不到comdistnt函数 我要做βNTI,代码如下: #本函数依赖picant、ape、doParrallel、foreach四个包 #threads:使用的线程数,可用detectCores()函数查看可使用的CPU数。 #使用的CPU数并不是越多越好,还要顾及自己的内存是否足够,因为每开一个线程都会占据相同的内存。 #源代码由刘洪提供,胡天龙进行并行修改。 #otu_niche表示行为样点,列为OTU的表;otu_tree表示OTU的遗传发育树; #reps表示构建的零模型的个数;threads表示使用的线程数。 beta_nti <- function(otu_niche,otu_tree,reps,threads){ library(picante) library(ape) #去除发育树中不存在于OTU表中的OTU prune_tree<-prune.sample(otu_niche,otu_tree) #获取发育树中OTU的名称 tip<-prune_tree$tip.label coln<-colnames(otu_niche) m<-NULL #打印OTU表中不存在于发育树中的OTU for(i in 1:length(coln)){ if(!coln[i]%in%tip){ #print(coln[i]) m<-cbind(m,coln[i]) } } m<-as.vector(m) #去除OTU表中不存在于发育树中的OTU otu_niche<-otu_niche[,!colnames(otu_niche)%in%m] #计算OTU两两间的遗传距离 otu_phydist <- cophenetic(prune_tree) #将OTU表和发育树存入一个对象中 match.phylo.otu = match.phylo.comm(prune_tree, otu_niche) #str(match.phylo.otu) ## calculate empirical betaMNTD #算beta-mntd beta.mntd.weighted = as.matrix(comdistnt(match.phylo.otu$comm,cophenetic(match.phylo.otu$phy),abundance.weighted=T)); #identical(rownames(match.phylo.otu$comm),colnames(beta.mntd.weighted)); # just a check, should be TRUE #identical(rownames(match.phylo.otu$comm),rownames(beta.mntd.weighted)); # just a check, should be TRUE # calculate randomized betaMNTD beta.reps = reps; # number of randomizations rand.weighted.bMNTD.comp = NULL dim(rand.weighted.bMNTD.comp) library(abind) arraybind <- function(...){ abind(...,along = 3,force.array=TRUE) } #并行化运行 library(foreach) library(doParallel) registerDoParallel(cores = threads) rand.weighted.bMNTD.comp <- foreach (rep = 1:beta.reps, .combine = "arraybind") %dopar%{ #计算零模型的beta-MNTD as.matrix(comdistnt(match.phylo.otu$comm,taxaShuffle(cophenetic(match.phylo.otu$phy)),abundance.weighted=T,exclude.conspecifics = F)) } weighted.bNTI = matrix(c(NA),nrow=nrow(match.phylo.otu$comm),ncol=nrow(match.phylo.otu$comm)); dim(weighted.bNTI); #根据零模型和观察数据的beta-MNTD计算beta-NTI for (columns in 1:(nrow(match.phylo.otu$comm)-1)) { for (rows in (columns+1):nrow(match.phylo.otu$comm)) { rand.vals = rand.weighted.bMNTD.comp[rows,columns,]; weighted.bNTI[rows,columns] = (beta.mntd.weighted[rows,columns] - mean(rand.vals)) / sd(rand.vals); rm("rand.vals") } } rownames(weighted.bNTI) = rownames(match.phylo.otu$comm) colnames(weighted.bNTI) = rownames(match.phylo.otu$comm) return(weighted.bNTI) } library(vegan) #读取OTU表 otu_biom2 <- read.delim("E:/otu table.txt", row.names=1) #查看样品序列条数 summary(colSums(otu_biom2)) otu_biom2 = otu_biom2[,colSums(otu_biom2) > 8500] #将OTU表转置为行名为样品名称,列名为OTU名称的表 otu<-t(otu_biom2) head(rowSums(otu)) #对OTU表进行抽平 otu = as.data.frame(rrarefy(otu, 8500)) head(rowSums(otu)) #去除序列条数较少的OTU otu_niche<-otu[,log(colSums(otu)/sum(otu),10)>-4.5] library(picante) #读取树文件 otu_tree<-read.tree("E:/tree") #以前500个OTU为例,度量十核计算所需的时间 system.time(beta_nti(otu_niche = otu_niche[1:200],otu_tree = otu_tree,1000,10)) #以前500个OTU为例,度量单核计算所需的时间 system.time(beta_nti(otu_niche = otu_niche[1:200],otu_tree = otu_tree,1000,1)) #计算beta-NTI beta_nti <- beta_nti(otu_niche = otu_niche,otu_tree = otu_tree,1000,10) #输出表格 write.table(beta_nti,"E:/beta-nti.txt",quote = FALSE, sep = "\t",row.names = TRUE,col.names = TRUE) 但是最后运行到“system.time(beta_nti(otu_niche = otu_niche[1:200],otu_tree = otu_tree,1000,10))”的时候就报错:“Error in { : task 1 failed - "could not find function "comdistnt" " Timing stopped at: 1.46 0.03 1.83” 请问哪位大佬知道报错的原因,就差这最后两行代码就能做出图了,好难
1 下一页