Skip to content

Commit

Permalink
Improve consistency among message, warnings, and errors
Browse files Browse the repository at this point in the history
Now message() is uniformly used instead of cat(), warning() is used
for warnings instead of cat(), correct spacing is added between
arguments where appropriate, and error messages do not have trailing
newlines, since stop() already adds one.
  • Loading branch information
DarwinAwardWinner committed May 14, 2018
1 parent e1d30f4 commit 8ca4fb9
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 32 deletions.
13 changes: 7 additions & 6 deletions R/ComBat.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ ComBat <- function (dat, batch, mod = NULL, par.prior = TRUE, prior.plots = FALS
if (!(ref.batch%in%levels(batch))) {
stop("reference level ref.batch is not one of the levels of the batch variable")
}
message("Using batch =",ref.batch, "as a reference batch (this batch won't change)")
message("Using batch = ",ref.batch, " as a reference batch (this batch won't change)")
ref <- which(levels(as.factor(batch))==ref.batch) # find the reference
batchmod[,ref] <- 1
} else {
ref <- NULL
}
message("Found", nlevels(batch), "batches")
message("Found ", nlevels(batch), " batches")

## A few other characteristics on the batches
n.batch <- nlevels(batch)
Expand All @@ -90,7 +90,7 @@ ComBat <- function (dat, batch, mod = NULL, par.prior = TRUE, prior.plots = FALS
design <- as.matrix(design[,!check])

## Number of covariates or covariate levels
message("Adjusting for", ncol(design)-ncol(batchmod), 'covariate(s) or covariate level(s)')
message("Adjusting for ", ncol(design)-ncol(batchmod), " covariate(s) or covariate level(s)")

## Check if the design is confounded
if(qr(design)$rank < ncol(design)) {
Expand All @@ -100,7 +100,7 @@ ComBat <- function (dat, batch, mod = NULL, par.prior = TRUE, prior.plots = FALS
}
if(ncol(design)>(n.batch+1)) {
if((qr(design[,-c(1:n.batch)])$rank<ncol(design[,-c(1:n.batch)]))){
stop('The covariates are confounded! Please remove one or more of the covariates so the design is not confounded')
stop("The covariates are confounded! Please remove one or more of the covariates so the design is not confounded")
} else {
stop("At least one covariate is confounded with batch! Please remove confounded covariates and rerun ComBat")
}
Expand All @@ -110,7 +110,8 @@ ComBat <- function (dat, batch, mod = NULL, par.prior = TRUE, prior.plots = FALS
## Check for missing values
NAs <- any(is.na(dat))
if(NAs){
message(c('Found',sum(is.na(dat)),'Missing Data Values'), sep=' ')}
message('Found ', sum(is.na(dat)), ' missing data values')
}
## print(dat[1:2,])

##Standardize Data across genes
Expand Down Expand Up @@ -254,7 +255,7 @@ ComBat <- function (dat, batch, mod = NULL, par.prior = TRUE, prior.plots = FALS
}

## Normalize the Data ###
message("Adjusting the Data\n")
message("Adjusting the data")

bayesdata <- s.data
j <- 1
Expand Down
4 changes: 2 additions & 2 deletions R/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ it.sol <- function(sdat,g.hat,d.hat,g.bar,t2,a,b,conv=.0001){
d.old <- d.new
count <- count+1
}
## cat("This batch took", count, "iterations until convergence\n")
## message("This batch took ", count, " iterations until convergence")
adjust <- rbind(g.new, d.new)
rownames(adjust) <- c("g.star","d.star")
adjust
Expand Down Expand Up @@ -138,7 +138,7 @@ int.eprior <- function(sdat, g.hat, d.hat){
LH[LH=="NaN"]=0
g.star <- c(g.star, sum(g*LH)/sum(LH))
d.star <- c(d.star, sum(d*LH)/sum(LH))
## if(i%%1000==0){cat(i,'\n')}
## if(i%%1000==0){message(i)}
}
adjust <- rbind(g.star,d.star)
rownames(adjust) <- c("g.star","d.star")
Expand Down
6 changes: 4 additions & 2 deletions R/irwsva.build.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ irwsva.build <- function(dat, mod, mod0 = NULL,n.sv,B=5) {

rm(resid)

cat(paste("Iteration (out of", B,"):"))
message("Iteration (out of ", B,"):", appendLF=FALSE)
for(i in 1:B){
mod.b <- cbind(mod,uu$vectors[,1:n.sv])
mod0.b <- cbind(mod0,uu$vectors[,1:n.sv])
Expand All @@ -65,8 +65,10 @@ irwsva.build <- function(dat, mod, mod0 = NULL,n.sv,B=5) {
dats <- dat*pprob
dats <- dats - rowMeans(dats)
uu <- eigen(t(dats)%*%dats)
cat(paste(i," "))
message(" ", i, appendLF=FALSE)
}
# Newline at the end of the message
message("")

sv = svd(dats)$v[,1:n.sv, drop=FALSE]
retval <- list(sv=sv,pprob.gam = pprob.gam, pprob.b=pprob.b,n.sv=n.sv)
Expand Down
2 changes: 1 addition & 1 deletion R/num.sv.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
num.sv <- function(dat, mod,method=c("be","leek"),vfilter=NULL,B=20,seed=NULL) {
if(!is.null(vfilter)){
if(vfilter < 100 | vfilter > dim(dat)[1]){
stop(paste("The number of genes used in the analysis must be between 100 and",dim(dat)[1],"\n"))
stop(paste("sva error: the number of genes used in the analysis must be between 100 and",dim(dat)[1]))
}
tmpv = rowVars(dat)
ind = which(rank(-tmpv) < vfilter)
Expand Down
14 changes: 10 additions & 4 deletions R/ssva.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,18 @@
#'

ssva <- function(dat,controls,n.sv){
if(is.null(n.sv)){stop("ssva error: You must specify the number of surrogate variables")}
if(dim(dat)[1] != length(controls)){stop("ssva error: You must specify a control vector the same length as the number of genes.")}
if(any(controls > 1) | any(controls < 0)){stop("ssva error: Control probabilities must be between 0 and 1.")}
if(is.null(n.sv)){
stop("ssva error: You must specify the number of surrogate variables.")
}
if(dim(dat)[1] != length(controls)){
stop("ssva error: You must specify a control vector the same length as the number of genes.")
}
if(any(controls > 1) | any(controls < 0)){
stop("ssva error: Control probabilities must be between 0 and 1.")
}

if (n.sv == 0) {
warning("Returning zero surrogate variables as requested")
warning("Returning zero surrogate variables as requested.")
return(list(sv=matrix(nrow=ncol(dat), ncol=0),
pprob.gam = controls, pprob.b=NULL, n.sv=0))
}
Expand Down
23 changes: 15 additions & 8 deletions R/sva.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,28 @@
sva <- function(dat, mod, mod0 = NULL,n.sv=NULL,controls=NULL,method=c("irw","two-step","supervised"),
vfilter=NULL,B=5, numSVmethod = "be") {
method <- match.arg(method)
if(!is.null(controls) & !is.null(vfilter)){stop("sva error: if controls is provided vfilter must be NULL.\n")}
if((method=="supervised") & is.null(controls)){stop("sva error: for a supervised analysis you must provide a vector of controls.\n")}
if(!is.null(controls) & (method!="supervised")){method = "supervised"; cat("sva warning: controls provided so supervised sva is being performed.\n")}
if(!is.null(controls) & !is.null(vfilter)){
stop("sva error: if controls is provided vfilter must be NULL.")
}
if((method=="supervised") & is.null(controls)){
stop("sva error: for a supervised analysis you must provide a vector of controls.")
}
if(!is.null(controls) & (method!="supervised")){
method = "supervised";
cat("sva warning: controls provided so supervised sva is being performed.")
}

if(!is.null(vfilter)){
if(vfilter < 100 | vfilter > dim(dat)[1]){
stop(paste("sva error: the number of genes used in the analysis must be between 100 and",dim(dat)[1],"\n"))
stop(paste("sva error: the number of genes used in the analysis must be between 100 and",dim(dat)[1]))
}
tmpv = rowVars(dat)
ind = which(rank(-tmpv) <= vfilter)
dat = dat[ind,]
}

if (!is.null(n.sv) && n.sv == 0) {
warning("Returning zero surrogate variables as requested")
warning("Returning zero surrogate variables as requested.")
return(list(sv=matrix(nrow=ncol(dat), ncol=0),
pprob.gam = rep(0, nrow(dat)), pprob.b=NULL, n.sv=0))
}
Expand All @@ -63,8 +70,8 @@ sva <- function(dat, mod, mod0 = NULL,n.sv=NULL,controls=NULL,method=c("irw","tw
}

if(n.sv > 0){
cat(paste("Number of significant surrogate variables is: ",n.sv,"\n"))
message("Number of significant surrogate variables is: ", n.sv, ".")

if(method=="two-step"){
return(twostepsva.build(dat=dat, mod=mod,n.sv=n.sv))
}
Expand All @@ -75,7 +82,7 @@ sva <- function(dat, mod, mod0 = NULL,n.sv=NULL,controls=NULL,method=c("irw","tw
return(ssva(dat,controls,n.sv))
}
}else{
cat("No significant surrogate variables\n");
message("No significant surrogate variables.");
return(list(sv=matrix(nrow=ncol(dat), ncol=0),
pprob.gam = rep(0, nrow(dat)), pprob.b=NULL, n.sv=0))
}
Expand Down
28 changes: 19 additions & 9 deletions R/svaseq.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,28 @@
#'

svaseq <- function(dat, mod, mod0 = NULL,n.sv=NULL,controls=NULL,method=c("irw","two-step","supervised"),
vfilter=NULL,B=5, numSVmethod = "be",constant = 1) {
vfilter=NULL,B=5, numSVmethod = "be",constant = 1) {
method <- match.arg(method)
if(!is.null(controls) & !is.null(vfilter)){stop("sva error: if controls is provided vfilter must be NULL.\n")}
if((method=="supervised") & is.null(controls)){stop("sva error: for a supervised analysis you must provide a vector of controls.\n")}
if(!is.null(controls) & (method!="supervised")){method = "supervised"; cat("sva warning: controls provided so supervised sva is being performed.\n")}
if(!is.null(controls) & !is.null(vfilter)){
stop("sva error: if controls is provided vfilter must be NULL.")
}
if((method=="supervised") & is.null(controls)){
stop("sva error: for a supervised analysis you must provide a vector of controls.")
}
if(!is.null(controls) & (method!="supervised")){
method = "supervised";
warning("sva warning: controls provided so supervised sva is being performed.")
}

if(any(dat < 0)){stop("svaseq error: counts must be zero or greater")}
if(any(dat < 0)){
stop("svaseq error: counts must be zero or greater.")
}
dat = log(dat + constant)


if(!is.null(vfilter)){
if(vfilter < 100 | vfilter > dim(dat)[1]){
stop(paste("sva error: the number of genes used in the analysis must be between 100 and",dim(dat)[1],"\n"))
stop(paste("sva error: the number of genes used in the analysis must be between 100 and",dim(dat)[1]))
}
tmpv = rowVars(dat)
ind = which(rank(-tmpv) < vfilter)
Expand All @@ -65,8 +74,8 @@ svaseq <- function(dat, mod, mod0 = NULL,n.sv=NULL,controls=NULL,method=c("irw",
}

if(n.sv > 0){
cat(paste("Number of significant surrogate variables is: ",n.sv,"\n"))
message("Number of significant surrogate variables is: ", n.sv, ".")

if(method=="two-step"){
return(twostepsva.build(dat=dat, mod=mod,n.sv=n.sv))
}
Expand All @@ -77,6 +86,7 @@ svaseq <- function(dat, mod, mod0 = NULL,n.sv=NULL,controls=NULL,method=c("irw",
return(ssva(dat,controls,n.sv=n.sv))
}
}else{
cat("No significant surrogate variables\n"); return(list(sv=0,pprob.gam=0,pprob.b=0,n.sv=0))
message("No significant surrogate variables.")
return(list(sv=0,pprob.gam=0,pprob.b=0,n.sv=0))
}
}

0 comments on commit 8ca4fb9

Please sign in to comment.