
make.expr.summaries.alchemy <- function(filenames=character(0)) {

  require(affy)
  require(gcrma)
  require(vsn)

  data <- ReadAffy(filenames=filenames)
  save(data,file="data.orig")

##   calculate MAS5 epression values
  load("data.orig")
  expression <- expresso(data,bg.correct=TRUE,bgcorrect.method="mas",
      normalize=TRUE,normalize.method="loess", normalize.param = list(span=1/10,subset = sample(1:(dim(pm(data))[1]), min(c(50000, nrow(pm(data))))) ),
      pmcorrect.method="mas",
      summary.method="medianpolish",
      verbose=TRUE)
  save(expression,file="mas5_loess.dat")
  
  load("data.orig")
  expression <- expresso(data,bg.correct=TRUE,bgcorrect.method="mas",
      normalize=TRUE,normalize.method="quantiles",
      pmcorrect.method="mas",
      summary.method="medianpolish",
      verbose=TRUE)
  save(expression,file="mas5_quantiles.dat")
  


#   GC-RMA transform the PM probe values using the MM probes
#   as the negative controls for NSB correction
load("data.orig")
data.gcrma <- bg.adjust.gcrma(data, affinity.source="local", type="fullmodel", fast=FALSE)
save(data.gcrma, file = "data_gcrma.dat")
      
  
  load("data_gcrma.dat")
  expression <- expresso(data.gcrma,bg.correct=FALSE,
      normalize=TRUE,normalize.method="loess", normalize.param = list(span=1/10,subset = sample(1:(dim(pm(data))[1]), min(c(50000, nrow(pm(data))))) ),
      pmcorrect.method="pmonly",
      summary.method="medianpolish",
      verbose=TRUE)
  save(expression,file="gc_loess.dat")
  
  load("data_gcrma.dat")
  expression <- expresso(data.gcrma,bg.correct=FALSE,
      normalize=TRUE,normalize.method="quantiles",
      pmcorrect.method="pmonly",
      summary.method="medianpolish",
      verbose=TRUE)
  save(expression,file="gc_quantiles.dat")

  # now perform a second loess on these datasets.
  list.nodats <- c("mas5_loess","mas5_quantiles",
  "gc_loess","gc_quantiles")
  for(i in list.nodats) {
    filename <- paste(i,".dat",sep="")
    load(filename)
    expr <- prepare.expr(expression@exprs,log.it=TRUE,cutoff.value=-3)
    gene.names <- rownames(expr)
    expr.norm <- normalize.loesssubset(expr, subset = 1:length(gene.names),log.it=FALSE,span=1/10,sample.length=nrow(expr))
    expr.norm <- 2^expr.norm
    s <- 500/mean(expr.norm, trim=0.02)
    expr.norm <- s*expr.norm
    expr.norm <- prepare.expr(expr.norm,log.it=TRUE,cutoff.value=-3)
    filename <- paste(i,"_loess.dat",sep="")
    save(expr.norm, file=filename)
    rm(expr.norm)    
    filename <- paste(i,".dat",sep="")
    load(filename)
    expr <- prepare.expr(expression@exprs,log.it=TRUE,cutoff.value=-3)
    expr <- 2^expr
    vsn(expr, lts.quantile = 0.85) -> expr.norm
    exprs(expr.norm) -> expr.norm
    expr.norm <- exp(expr.norm)
    s <- 500/mean(expr.norm, trim=0.02)
    expr.norm <- s*expr.norm
    expr.norm <- prepare.expr(expr.norm,log.it=TRUE,cutoff.value=-3)
    filename <- paste(i,"_vsn.dat",sep="")
    save(expr.norm, file=filename)
    rm(expr.norm)
  }
}


do.paired.comparisons.alchemy <- function(expt.cols=NULL,control.cols=NULL,out.dat=NULL,out.tbl=NULL,out.eps=NULL) {
  # collect all the data into a big dataframe
#  list.nodats <- c("1a","2a","3a","4a","5a","6a","7a","8a")
  load(paste(list.nodats[1], ".dat", sep=""))
  if(is.null(expt.cols) || is.null(control.cols)) {
    cat("Here are the numbers assigned to your CEL files:\n")
    print(cbind(1:ncol(expr.norm),colnames(expr.norm)))
    num.rep <- as.numeric(readline(prompt="How many replicates? "))
    control.cols <- c()
    expt.cols <- c()
    for(i in 1:num.rep) {
      j <- as.numeric(readline(prompt=paste("Please type the number for control sample in replicate ",i,": ",sep="")))
      k <- as.numeric(readline(prompt=paste("Please type the number for experimental sample in replicate ",i,": ",sep="")))
      control.cols <- c(control.cols,j)
      expt.cols <- c(expt.cols,k)
    }
  }
  if(is.null(out.dat)) {
    out.dat <- readline(prompt="Please type the filename for the result R data file: ")
  }
  if(is.null(out.tbl)) {
    out.tbl <- readline(prompt="Please type the filename for the result text file: ")
  }
  if(is.null(out.eps)) {
    out.eps <- readline(prompt="Please type the filename for the result image file: ")
  }

  cat("Performing a comparison for paired samples:\n")
  tmp.show <- cbind(Expt=colnames(expr.norm)[expt.cols],Control=colnames(expr.norm)[control.cols])
  print(tmp.show)
  cat("Writing results to ",out.dat,", ",out.tbl,", and ",out.eps,".\n",sep="")

  num.pseudorep <- length(list.nodats)
  tmp.conf <- floor(10 * (num.pseudorep * num.rep - 1) / (num.rep - 1))
  res <- paired.composite(list.nodats,control.cols,expt.cols,conf=tmp.conf)
  save(res,file=out.dat)
  write.table(cbind(res$d.orig,res$mean.sig,res$all.signal),file=out.tbl,sep="\t",quote=FALSE)
  postscript(file=out.eps,horizontal=FALSE,onefile=FALSE,height=5,width=5,points=12)
  par(mar=c(4,4,0.5,0.5))
  q.cutoffs <- c(0.00001,0.0001,0.001,0.002,0.005,0.01,0.02,0.05,0.1)
  num.positive <- c()
  for (i in q.cutoffs) {
    num.positive <- c(num.positive,sum(res$d.orig$q <= i))
  }
  plot(log10(q.cutoffs),num.positive,pch=19,xlab="Log(10) Q-value cutoff",ylab="Number called differentially expressed")
  lines(log10(q.cutoffs),num.positive)
  dev.off()
}




do.unpaired.comparisons.alchemy <- function(expt.cols=NULL,control.cols=NULL,out.dat=NULL,out.tbl=NULL,out.eps=NULL) {
  # collect all the data into a big dataframe
  #list.nodats <- c("1a","2a","3a","4a","5a","6a","7a","8a")
  load(paste(list.nodats[1], ".dat", sep="") )
  if(is.null(expt.cols) || is.null(control.cols)) {
    cat("Here are the numbers assigned to your CEL files:\n")
    print(cbind(1:ncol(expr.norm),colnames(expr.norm)))
    control.cols <- as.numeric(unlist(strsplit(readline(prompt="Please type the numbers for the control samples, separated by spaces: "),'[[:space:]]+',extended=TRUE)))
    expt.cols <- as.numeric(unlist(strsplit(readline(prompt="Please type the numbers for the experimental samples, separated by spaces: "),'[[:space:]]+',extended=TRUE)))
  }
  if(is.null(out.dat)) {
    out.dat <- readline(prompt="Please type the filename for the result R data file: ")
  }
  if(is.null(out.tbl)) {
    out.tbl <- readline(prompt="Please type the filename for the result text file: ")
  }
  if(is.null(out.eps)) {
    out.eps <- readline(prompt="Please type the filename for the result image file: ")
  }
  
  cat("Performing a comparison for unpaired samples:\n")
  cat("Experimental sample names:\n")
  print(colnames(expr.norm)[expt.cols])
  cat("\nControl sample names:\n")
  print(colnames(expr.norm)[control.cols])
  cat("Writing results to ",out.dat,".1, ",out.dat,".2, ",out.tbl,", and ",out.eps,".\n",sep="")

  all.expr <- c()
  gene.names <- rownames(expr.norm)
  order.template <- order(gene.names)
  for(i in 1:length(list.nodats)) {
    filename <- paste(list.nodats[i],".dat",sep="")
    load(filename)
    expr <- prepare.expr(expr.norm,log.it=TRUE,cutoff.value=-3)
    rm(expr.norm)
    gene.names <- rownames(expr)
    if(length(which(order(gene.names) != order.template)) > 0) {
      cat("Error: gene names are not in same order for the individual datasets\n")
    }
    all.expr <- c(all.expr,list(i=expr))
  }

  tmp.conf <- 2 * (length(control.cols) + length(expt.cols))
  res <- pierre.unpair.combine(all.expr,controls=control.cols,expts=expt.cols,winsize=100,conf=tmp.conf,minrep=3,p0=1,num.permutations=100)
  save(res,file=paste(out.dat,".1",sep=""))

  # get the mean signal level
  all.sig <- c()
  for(i in 1:length(list.nodats)) {
    tmp.sig <- apply(all.expr[[i]],1,mean.na)
    all.sig <- cbind(all.sig,tmp.sig)
  }
  all.sig <- apply(all.sig,1,mean.na)

  res.summary <- cbind(res$d.orig$combined.mean,res$d.orig$var.a,res$d.orig$var.b,res$d.orig$stat,res$d.orig$q,all.sig)
  colnames(res.summary) <- c("mean.fc","var.a","var.b","stat","q","mean.sig")

  save(res.summary,file=paste(out.dat,".2",sep=""))
  write.table(res.summary,file=out.tbl,quote=FALSE,sep="\t")

  postscript(file=out.eps,horizontal=FALSE,onefile=FALSE,height=5,width=5,points=12)
  par(mar=c(4,4,0.5,0.5))
  q.cutoffs <- c(0.00001,0.0001,0.001,0.002,0.005,0.01,0.02,0.05,0.1)
  num.positive <- c()
  for (i in q.cutoffs) {
    num.positive <- c(num.positive,sum(res.summary[,"q"] <= i))
  }
  plot(log10(q.cutoffs),num.positive,pch=19,xlab="Log(10) Q-value cutoff",ylab="Number called differentially expressed")
  lines(log10(q.cutoffs),num.positive)
  dev.off()
}

