###################################################
### chunk number 1: input-packages
###################################################
library(affy)
library(affydata)


###################################################
### chunk number 2: input-bioc eval=FALSE
###################################################
## source("http://bioconductor.org/biocLite.R")
## biocLite("affydata")


###################################################
### chunk number 3: inpu-file-path
###################################################
celPath <- system.file("celfiles", package="affydata")


###################################################
### chunk number 4: input-ReadAffy
###################################################
affyBatch <- ReadAffy(celfile.path=celPath)


###################################################
### chunk number 5: input-ReadAffy-help eval=FALSE
###################################################
## help("ReadAffy")


###################################################
### chunk number 6: input-vignette eval=FALSE
###################################################
## openVignette()


###################################################
### chunk number 7: input-view
###################################################
affyBatch


###################################################
### chunk number 8: preprocess-justRMA
###################################################
currentDirectory <- getwd()
setwd(celPath)
celFiles <- list.files(celPath)
expressionSet <- justRMA(filenames=celFiles)
setwd(currentDirectory)


###################################################
### chunk number 9: preprocess-ExpressionSet
###################################################
expressionSet


###################################################
### chunk number 10: preprocess-vsn
###################################################
library(vsn)
data(lymphoma)
vsnData <- justvsn(lymphoma)


###################################################
### chunk number 11: visualization-rg
###################################################
exprVals <- exprs(vsnData)
green <- exprVals[,7]
red <- exprVals[,8]


###################################################
### chunk number 12: visualizeation-MA
###################################################
M <- green - red
A <- (green + red) / 2


###################################################
### chunk number 13: visualizeation-check-MA
###################################################
head(green, n=5)
head(red, n=5)
head(M, n=5)


###################################################
### chunk number 14: geneplotter
###################################################
library(geneplotter)
smoothScatter(A, M, pch=20, xlab="A", ylab="M")
abline(h=0, col="red")


###################################################
### chunk number 15: Interrogation-Biobase
###################################################
library(Biobase)
data(sample.ExpressionSet)
obj <- sample.ExpressionSet


###################################################
### chunk number 16: interrogation-annotation
###################################################
annotation(obj)
feature <- featureNames(obj)[100]
feature


###################################################
### chunk number 17: interrogation-GO-pkgs
###################################################
library(annotate)
library(hgu95av2)
library(GO)
ontologies <- hgu95av2GO[[ feature ]]
length(ontologies)


###################################################
### chunk number 18: interrogation-ontologies
###################################################
ontologies[[1]]


###################################################
### chunk number 19: biomart-load
###################################################
library(biomaRt)
ensembl <- useMart("ensembl", dataset="hsapiens_gene_ensembl")


###################################################
### chunk number 20: biomart-gene-info
###################################################
feature <- featureNames(obj)[100]
gene <- getGene(id=feature, type="affy_hg_u95av2", mart=ensembl)
names(gene)
gene$description


###################################################
### chunk number 21: limma-exprs-setup
###################################################
library(limma)
sd <- 0.3 * sqrt(4/rchisq(100, df=4))
ourExprs <- matrix(rnorm(100*6, sd=sd), nrow=100, ncol=6)
rownames(ourExprs) <- paste("Gene", 1:100)
colnames(ourExprs) <- paste("Sample", 1:6)
ourExprs[1:2, 4:6] <- ourExprs[1:2, 4:6] + 2


###################################################
### chunk number 22: limma-expressionSet
###################################################
ourPData <- data.frame(Group=c(rep("A", 3), rep("B", 3)))
rownames(ourPData) <- colnames(ourExprs)
exSet <- new("ExpressionSet", 
    phenoData = new("AnnotatedDataFrame", data=ourPData),
    exprs=ourExprs)


###################################################
### chunk number 23: limma-model.matrix
###################################################
modelMatrix <- model.matrix(~exSet$Group, data=pData(exSet))
modelMatrix


###################################################
### chunk number 24: limma-fit
###################################################
fit <- lmFit(exSet, modelMatrix)
names(fit)


###################################################
### chunk number 25: limma-eBayes
###################################################
moderated <- eBayes(fit)


###################################################
### chunk number 26: limma-topTable
###################################################
topTable(moderated)[1:5,-2]


###################################################
### chunk number 27: volcano
###################################################
volcanoplot(moderated, coef=2, highlight=3)


###################################################
### chunk number 28: biocLite eval=FALSE
###################################################
## source("http://bioconductor.org/biocLite.R")
## biocLite()


###################################################
### chunk number 29: biocLite2 eval=FALSE
###################################################
## biocLite(c("Biobase", "biglm"))


###################################################
### chunk number 30: update.packages eval=FALSE
###################################################
## library(Biobase)
## update.packages(repos=biocReposList())


###################################################
### chunk number 31: Biobase-explore
###################################################
library(Biobase)


###################################################
### chunk number 32: ExpressionSet-getClass eval=FALSE
###################################################
## getClass("ExpressionSet")


###################################################
### chunk number 33: showMethods-ExpressionSet eval=FALSE
###################################################
## showMethods(classes="ExpressionSet")


###################################################
### chunk number 34: showMethods-exprs eval=FALSE
###################################################
## showMethods("exprs")


###################################################
### chunk number 35: getNamespace
###################################################
nmSpace <- getNamespaceExports("Biobase")


###################################################
### chunk number 36: Sweave-info
###################################################
toLatex(sessionInfo())