From 17654c9280b9be03eee626dfc1f813961e8ecb95 Mon Sep 17 00:00:00 2001 From: Keith Hughitt Date: Sat, 28 May 2016 08:17:07 -0400 Subject: [PATCH 1/2] Added check for rows in input dat with no variance --- R/ComBat.R | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/R/ComBat.R b/R/ComBat.R index 116816b..b28fac5 100644 --- a/R/ComBat.R +++ b/R/ComBat.R @@ -38,6 +38,10 @@ #' ComBat <- function(dat, batch, mod=NULL, par.prior=TRUE,prior.plots=FALSE,mean.only=FALSE,ref.batch=NULL) { + # check to make sure there are no rows in the data with zero variance + if (sum(apply(dat, 1, var) == 0) > 0) { + stop("One or more rows in the data found to have zero variance.") + } # make batch a factor and make a set of indicators for batch if(mean.only==TRUE){cat("Using the 'mean only' version of ComBat\n")} if(length(dim(batch))>1){stop("This version of ComBat only allows one batch variable")} ## to be updated soon! From ca185e0600b49551f570956a92f2912f4d1b3253 Mon Sep 17 00:00:00 2001 From: Keith Hughitt Date: Sat, 28 May 2016 15:56:22 -0400 Subject: [PATCH 2/2] Updated ComBat function to ignore genes with zero variance when adjusting for batch --- R/ComBat.R | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/R/ComBat.R b/R/ComBat.R index b28fac5..aefd5ce 100644 --- a/R/ComBat.R +++ b/R/ComBat.R @@ -38,10 +38,19 @@ #' ComBat <- function(dat, batch, mod=NULL, par.prior=TRUE,prior.plots=FALSE,mean.only=FALSE,ref.batch=NULL) { - # check to make sure there are no rows in the data with zero variance - if (sum(apply(dat, 1, var) == 0) > 0) { - stop("One or more rows in the data found to have zero variance.") + # check for genes with uniform expression; if such genes exist, they will + # be temporarily removed from the data matrix before performing batch + # adjustment and add back in before returning the final result. + zero.var.rows <- apply(dat, 1, var) == 0 + + if (sum(zero.var.rows) > 0) { + cat(sprintf("Found %d genes with uniform expression; these will not be adjusted for batch.\n", sum(zero.var.rows))) + + # keep a copy of the original data matrix and remove zero var rows + dat.orig <- dat + dat <- dat[!zero.var.rows,] } + # make batch a factor and make a set of indicators for batch if(mean.only==TRUE){cat("Using the 'mean only' version of ComBat\n")} if(length(dim(batch))>1){stop("This version of ComBat only allows one batch variable")} ## to be updated soon! @@ -216,6 +225,13 @@ ComBat <- function(dat, batch, mod=NULL, par.prior=TRUE,prior.plots=FALSE,mean.o if(!is.null(ref.batch)){ bayesdata[, batches[[ref]]] <- dat[, batches[[ref]]] } + + # if genes genes with uniform expression were held out, add them back in now + if (sum(zero.var.rows) > 0) { + # keep a copy of the original data matrix and remove zero var rows + dat.orig[!zero.var.rows,] <- bayesdata + bayesdata <- dat.orig + } return(bayesdata)