####################################################################################################### # Y is the number of times to re-sample the data to account for differences in sequencing depths between samples # X is the number of traits to sample - number in the smallest sample. # trait - a matrix. Samples are listed in rows. Traits listed in columns. Aka, each column lists the number of times a # specificy trait (or gene category) appeared in each sample. # requires the R packages: # ipsolve # sampling # vegan # # # ####################################################################################################### RarefyFT=function(trait,X,Y) { df.list<-vector("list",Y) for( i in 1:Y){ print(c("itteration #",i),quote=FALSE) traitR<-rrarefy(trait,X) df.list[[i]]<-Ses.Kegg(traitR) } # close for loop Res<-Reduce("+",df.list)/length(df.list) return(Res) } # close program ####### Ses.Kegg=function (trait){ Z<-999 sig<-ceiling(.025*Z) ind<-rowSums(trait) abund<-c(as.numeric(colSums(trait))) #print(sig) #create a list of traits that repeates each trait the number of times it has occured in all the samples trait.list<-rep(c(colnames(trait)),colSums(trait)) #print(trait.list) l.trait.list<-length(trait.list) resmatrix<-matrix(NA,ncol=6,nrow=dim(trait)[1]) row.names(resmatrix)<-row.names(trait) for (a in 1:dim(trait)[1]){ #print(a) res<-c() ind1<-ind[a] for(b in 1:Z){ #print(c(a,b)) gg<-srswor(ind1,l.trait.list) subtrait<-as.vector(trait.list[gg==1]) rich<-length(unique(subtrait)) res<-append(res,rich) } #end Y loop #res<-sort.ind(res) res<-sort(res) res_mean<-mean(res) res_sd<-sd(res) resmatrix[a,1]<-sum(trait[a,]!=0) resmatrix[a,2]<-res[sig] resmatrix[a,3]<-res[length(res)-sig] resmatrix[a,4]<-(sum(trait[a,]!=0)-res_mean)/res_sd resmatrix[a,5]<-rank(c(sum(trait[a,]!=0),res))[1]-1 resmatrix[a,6]<-res_mean colnames(resmatrix)<-c("total traits","lower bound","upper bound","obs.z","rank","mean of rand") } # end for loop return(resmatrix) }#close program ###################################################################################################################