###################################################
### chunk number 1: caching
###################################################
cache <- function(name, expr) {
  cachefile <- paste("tmp-", name, ".RData", sep="")
  if(file.exists(cachefile)) {
    load(cachefile)
  } else {
    assign(name, expr)
    save(list=name, file=cachefile)
  }
  get(name)
}


###################################################
### chunk number 2: readaffy eval=FALSE
###################################################
## library("affy")
## Data <- ReadAffy()


###################################################
### chunk number 3: loading
###################################################
library("affydata")
data(Dilution)


###################################################
### chunk number 4: pm
###################################################
pm(Dilution,"1001_at")[1:3,]


###################################################
### chunk number 5: matplot-show eval=FALSE
###################################################
## matplot(pm(Dilution,"1001_at"),type="l", xlab="Probe No.",
##         ylab="PM Probe intensity")
## matplot(t(pm(Dilution,"1001_at")),type="l",xlab="Array No.",
##         ylab="PM Probe intensity")


###################################################
### chunk number 6: matplot1
###################################################
matplot(pm(Dilution,"1001_at"),type="l",ylab="PM Probe
intensity",xlab="Probe No.")


###################################################
### chunk number 7: matplot2
###################################################
matplot(t(pm(Dilution,"1001_at")),type="l",ylab="PM Probe
intensity",xlab="Array No.",xaxt="n")
axis(1,1:4,1:4)


###################################################
### chunk number 8: pdata
###################################################
pData(Dilution)


###################################################
### chunk number 9: showH eval=FALSE
###################################################
## hist(Dilution)
## boxplot(Dilution)


###################################################
### chunk number 10: hist
###################################################
hist(Dilution)



###################################################
### chunk number 11: boxplot
###################################################

boxplot(Dilution)



###################################################
### chunk number 12: rma.bg.show eval=FALSE
###################################################
## Dilution.bg.rma <- bg.correct(Dilution, method="rma")


###################################################
### chunk number 13: mas5.bg.show eval=FALSE
###################################################
## Dilution.bg.mas <- bg.correct(Dilution,method="mas")


###################################################
### chunk number 14: mas5.norm eval=FALSE
###################################################
## Dilution.norm.scale <- normalize(Dilution,
##                                  method="constant")


###################################################
### chunk number 15: invariantset.norm.show eval=FALSE
###################################################
## Dilution.norm.nonlinear <- normalize(Dilution,
##                                      method="invariantset")


###################################################
### chunk number 16: quantile.norm eval=FALSE
###################################################
## Dilution.norm.quantile <- normalize(Dilution,
##                                     method="quantiles")


###################################################
### chunk number 17: loess.norm.show eval=FALSE
###################################################
## Dilution.norm.loess <- normalize(Dilution,
##                                  method="loess")


###################################################
### chunk number 18: vsn.norm eval=FALSE
###################################################
## library("vsn")
## Dil.vsn <- normalize(Dilution, method = "vsn")


###################################################
### chunk number 19: normalizemethods
###################################################
normalize.methods(Dilution)


###################################################
### chunk number 20: <setopts
###################################################
options(width=50)


###################################################
### chunk number 21: expresso1.show eval=FALSE
###################################################
## eset <- expresso(Dilution,
##                  bgcorrect.method="rma",
##                  normalize.method="constant",
##                  pmcorrect.method="pmonly",
##                  summary.method="avgdiff")


###################################################
### chunk number 22: expresso2.show eval=FALSE
###################################################
## eset <- expresso(Dilution,
##                  normalize.method="invariantset",
##                  bg.correct=FALSE,
##                  pmcorrect.method="pmonly",
##                  summary.method="liwong")


###################################################
### chunk number 23: mas5 eval=FALSE
###################################################
## eset <- mas5(Dilution)


###################################################
### chunk number 24: threestep eval=FALSE
###################################################
## library("affyPLM")
## eset <- threestep(Dilution,
##   background.method="IdealMM",
##   normalize.method="quantile",
##   summary.method="tukey.biweight")


###################################################
### chunk number 25: rma eval=FALSE
###################################################
## eset <- rma(Dilution)


###################################################
### chunk number 26: gcrma1.show eval=FALSE
###################################################
## library("gcrma")
## Dil.expr <- gcrma(Dilution)


###################################################
### chunk number 27: gcrma2.show eval=FALSE
###################################################
## ai <- compute.affinities(cdfName(Dilution))
## Dil.expr<-gcrma(Dilution,affinity.info=ai)


###################################################
### chunk number 28: gcrma3.show eval=FALSE
###################################################
## Dil.expr2 <- gcrma(Dilution,affinity.info=ai,type="affinities")


###################################################
### chunk number 29: setup
###################################################
library("hgu133bcdf")
library("affyPLM")


###################################################
### chunk number 30: loadData
###################################################
library("ALLMLL")
data(MLL.B)
Data <- MLL.B[,c(2,1,3:5,14,6,13)] ##subset for some examples
sampleNames(Data) <- letters[1:8]


###################################################
### chunk number 31: rawlogImageDo
###################################################
palette.gray <- c(rep(gray(0:10/10),times=seq(1,41,by=4)))
bitmap("AffyQuality-image1b.png",height=3,width=3,pointsize=10,res=300)
par(mar=c(2.0,2.1,1.6,1.1),oma=c(1,1,0,0))
#image(Data[,1]) ##the default transform is log
image(Data[,1],col=palette.gray)
dev.off()


###################################################
### chunk number 32: simpleAffyQC
###################################################
library("simpleaffy")
Data.qc <- qc(Data)


###################################################
### chunk number 33: avgbg
###################################################
avbg(Data.qc)


###################################################
### chunk number 34: scalefactor
###################################################
sfs(Data.qc)


###################################################
### chunk number 35: percentpresent
###################################################
percent.present(Data.qc)


###################################################
### chunk number 36: ratios35
###################################################
ratios(Data.qc)[,1:2]


###################################################
### chunk number 37: RNAdegDo
###################################################
library("AmpAffyExample")
data(AmpData)
## for illustrative purposes
sampleNames(AmpData) <- c("N1","N2","N3","A1","A2","A3") 
RNAdeg<-AffyRNAdeg(AmpData)
pdf(file="RNAdeg.pdf")
plotAffyRNAdeg(RNAdeg,col=c(2,2,2,3,3,3))
dev.off()


###################################################
### chunk number 38: affyPLM
###################################################
Data.plm <- cache("Data.plm", fitPLM(Data))


###################################################
### chunk number 39: plmimages
###################################################
jpeg(file="plmimages1.jpg", width=1000, height=1000)
par(mar=rep(0.5,4))
image(Data[, 1], col = palette.gray )
dev.off()

jpeg(file="plmimages2.jpg", width=1000, height=1000)
par(mar=rep(0.5,4))
image(Data.plm, type = "weights", which = 1)
dev.off()

jpeg(file="plmimages3.jpg", width=1000, height=1000)
par(mar=rep(0.5,4))
image(Data.plm, type = "resids", which = 1)
dev.off()

jpeg(file="plmimages4.jpg", width=1000, height=1000)
par(mar=rep(0.5,4))
image(Data.plm, type = "sign.resids", which = 1)
dev.off()


###################################################
### chunk number 40: plmRLE
###################################################
par(mar=c(2, 2, 0.5, 0.5), mgp=c(2.5,1,0))
Mbox(Data.plm, ylim = c(-1, 1), names = NULL, col="lightblue",
whisklty=0, staplelty=0)


###################################################
### chunk number 41: plmNUSE
###################################################
par(mar=c(2, 2, 0.5, 0.5), mgp=c(2.5,1,0))
boxplot(Data.plm, ylim = c(0.95, 1.5), names = NULL,
        outline = FALSE, col="lightblue")


