library(igraph) NC <- function(x) { N <- vcount(x) log(sum(exp(eigen(get.adjacency(x))$values)) / N) / (N-log(N)) } # ig -> igraph object robustnessNC <- function(ig, nrand=0) { ## Order by betweenness btw <- centralization.betweenness(ig, directed=FALSE)$res btw <- order(btw, decreasing=TRUE) nv <- vcount(ig) btw_ncvec <- vector('numeric', nv-1) #tmpgr <- ig for (i in 1:(nv-1)) { # tmpgr <- delete_vertices(ig, which(btw > i)) tmpgr <- delete_edges(ig, E(ig)[from(V(ig)[which(btw > i)])]) btw_ncvec[i] <- NC(tmpgr) } ## Order by degree deg <- degree(ig) deg <- order(deg, decreasing=TRUE) #tmpgr <- ig deg_ncvec <- vector('numeric', nv-1) for (i in 1:(nv-1)) { # tmpgr <- delete_vertices(ig, which(deg > i)) tmpgr <- delete_edges(ig, E(ig)[from(V(ig)[which(deg > i)])]) deg_ncvec[i] <- NC(tmpgr) } if (nrand > 0) { randmat <- replicate(nrand, { rand_ncvec <- vector('numeric', nv-1) rand <- sample(1:(nv-1)) for (i in 1:(nv-1)) { tmpgr <- delete_edges(ig, E(ig)[from(V(ig)[which(rand > i)])]) rand_ncvec[i] <- NC(tmpgr) } return(rand_ncvec) }, simplify='matrix') } else randmat <- NULL list(vert=(nv-1):1, btw=btw_ncvec, deg=deg_ncvec, rand=randmat) } number_to_sign <- function(number,signal=c("-",".","+")){ number[number<0] = "-" number[number==0] = "." number[number>0] = "+" return(number) } get_Oxf_interaction <- function(biom_to_use,minimum_prevalence,lambda,type="signal"){ # type can be mean, summary, sign biom_to_use <- prune_taxa(rowSums(biom_to_use@otu_table@.Data)!=0, biom_to_use) tax_table(biom_to_use) <- t(sapply(taxa_names(biom_to_use), parse_taxonomy_qiime)) biom_to_use.filt <- prune_taxa(rowSums(sign(biom_to_use@otu_table@.Data)) > nsamples(biom_to_use)*minimum_prevalence, biom_to_use) if(length(grep("formigenes",taxa_names(biom_to_use.filt)))==0){ print("O. formigenes is not present in 20% of the samples" ) } else{ temp = spiec.easi(biom_to_use.filt, method='mb', sel.criterion='bstars', lambda.min.ratio=lambda, nlambda=100, pulsar.params=list(rep.num=20, ncores=2)) Of_index = grep("formigenes",taxa_names(biom_to_use.filt)) taxa_itrt = taxa_names(biom_to_use.filt)[as(temp$refit,"matrix")[Of_index,]==1] #return(taxa_itrt) taxa_itrt_index = which(taxa_names(biom_to_use.filt)%in%taxa_itrt) taxa_itrt_beta = as.data.frame(lapply(temp$est$beta,function(x) as(x,"matrix")[Of_index,taxa_itrt_index])) rownames(taxa_itrt_beta) = taxa_itrt if(type=="signal"){return(number_to_sign(apply(taxa_itrt_beta,1,mean)))} else if(type=="mean"){return(apply(taxa_itrt_beta,1,mean))} else if(type=="summary"){return(apply(taxa_itrt_beta,1,summary))} } }