From c56ce286e67da1158de57532079596ffc4a04c0b Mon Sep 17 00:00:00 2001 From: marouenbg Date: Thu, 17 Feb 2022 18:55:31 -0500 Subject: [PATCH] ALPACA fix to NAs in community assignement --- R/ALPACA.R | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/R/ALPACA.R b/R/ALPACA.R index 6b0d4e6e..f0e51007 100644 --- a/R/ALPACA.R +++ b/R/ALPACA.R @@ -68,32 +68,29 @@ alpaca <- function(net.table,file.stem,verbose=FALSE) print("Computing node scores...") louv.Ascores <- NULL louv.Bscores <- NULL + louv.Anames <- NULL + louv.Bnames <- NULL for (i in seq_len(max(louv.memb))) { - print(i) + print(i) this.comm <- names(louv.memb)[louv.memb==i] this.tfs <- this.comm[grep("_A$",this.comm)] this.genes <- this.comm[grep("_B$",this.comm)] - if (length(this.tfs)>=1){ - if (length(this.tfs)>1){ - if (length(this.genes)==1){ - gene.sums <- sum(dwbm[this.tfs,this.genes]) - tf.sums <- dwbm[this.tfs,this.genes] - }else if (length(this.genes)>1){ - tf.sums <- apply(dwbm[this.tfs,this.genes],1,sum) - gene.sums <- apply(dwbm[this.tfs,this.genes],2,sum) - } - } else if (length(this.tfs)==1) { - tf.sums <- sum(dwbm[this.tfs,this.genes]) - gene.sums <- dwbm[this.tfs,this.genes] - } - this.denom <- sum(dwbm[this.tfs,this.genes]) - louv.Ascores <- c(louv.Ascores,tf.sums/this.denom) - louv.Bscores <- c(louv.Bscores,gene.sums/this.denom) + if (length(this.tfs)>0 && length(this.genes)>0) + { + dwbm.submat <- array(dwbm[this.tfs,this.genes],dim=c(length(this.tfs),length(this.genes))) + tf.sums <- apply(dwbm.submat,1,sum) + gene.sums <- apply(dwbm.submat,2,sum) + this.denom <- sum(dwbm[this.tfs,this.genes]) + louv.Ascores <- c(louv.Ascores,tf.sums/this.denom) + louv.Bscores <- c(louv.Bscores,gene.sums/this.denom) + louv.Anames <- c(louv.Anames,this.tfs) + louv.Bnames <- c(louv.Bnames,this.genes) } } louv.scores <- c(louv.Ascores,louv.Bscores) + names(louv.scores) <- c(louv.Anames,louv.Bnames) if (!is.null(file.stem)) { write.table(cbind(names(louv.memb), as.vector(louv.memb)),paste(c(file.stem,"_ALPACA_final_memb.txt"),collapse=""),col.names=FALSE,row.names=FALSE,quote=FALSE,sep="\t")