##############
##
##	Alchemy Method for used in R (v2.3.1) with Bioconductor (v1.8)
##
##	Full method requires use of Perfect Match (http://odin.mdacc.tmc.edu/~zhangli/PerfectMatch/)	
##	to calculate expression values with the PDNN model	
##
##	There currently is a affypdnn wrapper for use in (affypdnn),
##	but PDNN expression values in R do not match values calculated in Perfect Match
##
##	If users are unable to access Perfect Match, we offer
##	an alternative method to only uses the MAS5 PM-MM and GC-RMA method.
##	See details below
##
##############


library(affy)
library(gcrma)
library(goldenspike)
library(vsn)


#  set working directory to folder that contains cel files
#setwd("/dir/for/cel/files/")

#   If Perfect Match has been used, then remove "binCel" files from your working directory



#   Correct for NSB using the MAS5 PM-MM and GC-RMA method and
#   calculate 8 different probeset expression values
#   MAS5/loess probe-level normalization/medianpolish probe summary/loess probeset-level normalization
#   MAS5/quantiles probe-level normalization/medianpolish probe summary/vsn probeset-level normalization
#   MAS5/loess probe-level normalization/medianpolish probe summary/loess probeset-level normalization
#   MAS5/quantiles probe-level normalization/medianpolish probe summary/vsn probeset-level normalization
#   GC-NSB/loess probe-level normalization/medianpolish probe summary/loess probeset-level normalization
#   GC-NSB/quantiles probe-level normalization/medianpolish probe summary/vsn probeset-level normalization
#   GC-NSB/loess probe-level normalization/medianpolish probe summary/loess probeset-level normalization
#   GC-NSB/quantiles probe-level normalization/medianpolish probe summary/vsn probeset-level normalization
#   method is modified from make.expr.summaries function in the goldenspike package


#   Requires the modified scripts to be read into R
#   save alchemy_scripts.R into the same directory as Cel Files

source("modified_goldenspike_functions.txt")

#   If Perfect Match has been used, then remove "binCel" files from your working directory
make.expr.summaries.alchemy()


##################
#
#   To calculate q values without Perfect Match values
#   paired data requires do.paired.comparisons.alchemy()
#   unpaired data requires do.unpaired.comparisons.alchemy()
#
##################

list.nodats <- c("mas5_loess_loess","mas5_loess_vsn","mas5_quantiles_loess","mas5_quantiles_vsn",
"gc_loess_loess","gc_loess_vsn","gc_quantiles_loess","gc_quantiles_vsn")

do.unpaired.comparisons.alchemy()

#    If the data is paired
#do.unpaired.comparisons.alchemy()



##########################
#
#   To calculate q values with Perfect Match values
#   paired data requires do.paired.comparisons.alchemy()
#   unpaired data requires do.unpaired.comparisons.alchemy()
#
##########################


#   Read Perfect Match data into R
#   Requires estimated gene expression values generated in Perfect Match
#   to be saved into folder that contains the Cel files
#   files should be called "pdnn_exp.txt"
#   The file need to be changed to have the same order of Affymetrix IDs and sample names as other files

read.table(file="pdnn_exp.txt", sep="\t", header=TRUE) -> pdnn

#   To place Affymetrix ID as the rownames
rownames(pdnn) <- pdnn[,1]
pdnn <- pdnn[,2:ncol(pdnn)]
pdnn <- as.matrix(pdnn)

#   To have the same order of Affymetrix IDs as other files
load("data.orig")
gn <- geneNames(data)
pdnn <- pdnn[gn,]

#   Make sure pdnn matrix has the same column names and order of column names as other files
load("gc_loess_vsn.dat")
colnames(expr.norm)
colnames(pdnn) 
#    Change order of pdnn colnames to match expr.norm order
#pdnn <- pdnn,c(new.order)]
#    Change colnames to match expr.norm column names
#colnames(pdnn) <- colnames(expr.norm)


#   Loess probeset level normalization of pdnn expression values
pdnn <- exp(pdnn)
s <- 500/mean(pdnn, trim=0.02)
pdnn <- s*pdnn
pdnn <- log2(pdnn)
save(pdnn, file="pdnn.dat")
pdnn <- prepare.expr(pdnn,log.it=TRUE,cutoff.value=-3)
gene.names <- rownames(pdnn)
expr.norm <- normalize.loesssubset(pdnn, subset = 1:length(gene.names),log.it=FALSE,span=1/10,sample.length=5000)
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)
save(expr.norm, file="pdnn_loess.dat")
rm(expr.norm)


#   VSN probeset level normalization of pdnn expression values

load("pdnn.dat")
expr <- 2^pdnn
vsn(expr, lts.quantile = 0.85) -> expr.norm
expr.norm <- exprs(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)
save(expr.norm, file="pdnn_vsn.dat")
rm(expr.norm)


################
#
#   Calculate q values
#   paired data requires do.paired.comparisons.alchemy()
#   unpaired data requires do.unpaired.comparisons.alchemy()
#
##################


list.nodats <- c("mas5_loess_loess","mas5_loess_vsn","mas5_quantiles_loess","mas5_quantiles_vsn",
"gc_loess_loess","gc_loess_vsn","gc_quantiles_loess","gc_quantiles_vsn", "pdnn_loess", "pdnn_vsn")


do.unpaired.comparisons.alchemy()

#    If the data is paired
#do.unpaired.comparisons.alchemy()
