###################################################
### chunk number 1: options
###################################################
#line 54 "E:/biocbld/bbs-2.7-bioc/tmpdir/RtmpaV3OLd/R.INSTALL1b5d6089/beadarray/inst/doc/beadlevel.rnw"

options(width = 70)



###################################################
### chunk number 2: prelim
###################################################
#line 60 "E:/biocbld/bbs-2.7-bioc/tmpdir/RtmpaV3OLd/R.INSTALL1b5d6089/beadarray/inst/doc/beadlevel.rnw"
library(beadarray)
data(BLData)


###################################################
### chunk number 3: readIllumina eval=FALSE
###################################################
## #line 75 "E:/biocbld/bbs-2.7-bioc/tmpdir/RtmpaV3OLd/R.INSTALL1b5d6089/beadarray/inst/doc/beadlevel.rnw"
## 
## BLData = readIllumina(useImages=FALSE, illuminaAnnotation = "Humanv3")


###################################################
### chunk number 4: BLData
###################################################
#line 103 "E:/biocbld/bbs-2.7-bioc/tmpdir/RtmpaV3OLd/R.INSTALL1b5d6089/beadarray/inst/doc/beadlevel.rnw"

data(BLData)


is(BLData)
class(BLData)
slotNames(BLData)

##Get the beadData for array-section 1

BLData[[1]][1:10,]

##Alternative using accessor function

getBeadData(BLData, array=1, what="Grn")[1:10]

##Get unique ProbeIDs. These are the ArrayAddressIDs

uIDs = unique(getBeadData(BLData, array=1, what="ProbeID"))

uIDs[1:10]



###################################################
### chunk number 5: transform
###################################################
#line 135 "E:/biocbld/bbs-2.7-bioc/tmpdir/RtmpaV3OLd/R.INSTALL1b5d6089/beadarray/inst/doc/beadlevel.rnw"

log2(BLData[[1]][1:10,2])

logGreenChannelTransform

logGreenChannelTransform(BLData, array=1)[1:10]

logRedChannelTransform



###################################################
### chunk number 6: backgroundCorrection
###################################################
#line 149 "E:/biocbld/bbs-2.7-bioc/tmpdir/RtmpaV3OLd/R.INSTALL1b5d6089/beadarray/inst/doc/beadlevel.rnw"

for(i in 1:10){
 	BLData = backgroundCorrectSingleSection(BLData, array=i)
}

head(BLData[[1]])




###################################################
### chunk number 7: BeadLevelBoxplots
###################################################
#line 164 "E:/biocbld/bbs-2.7-bioc/tmpdir/RtmpaV3OLd/R.INSTALL1b5d6089/beadarray/inst/doc/beadlevel.rnw"

getBackgroundCorrectionIntensities = function(BLData, array){

	log2(getBeadData(BLData, array=array, what="Grn.bc"))

}

par(mfrow=c(1,2))
boxplot(BLData,las=2, outline=FALSE, ylim=c(4,12), main="Green Foreground")
boxplot(BLData,las=2, transFun =getBackgroundCorrectionIntensities, outline=FALSE, ylim=c(4,12), main="Green Foreground")



###################################################
### chunk number 8: Imageplots
###################################################
#line 182 "E:/biocbld/bbs-2.7-bioc/tmpdir/RtmpaV3OLd/R.INSTALL1b5d6089/beadarray/inst/doc/beadlevel.rnw"

par(mfrow=c(1,3), mar = c(2,2,2,2))

imageplot(BLData, array=1, low="lightgreen", high="darkgreen", horizontal = FALSE ,squareSize=25)

imageplot(BLData, array=2, low="lightgreen", high="darkgreen", horizontal = FALSE ,squareSize=25)

imageplot(BLData, array=3, low="lightgreen", high="darkgreen", horizontal = FALSE ,squareSize=25)




###################################################
### chunk number 9: BASH
###################################################
#line 213 "E:/biocbld/bbs-2.7-bioc/tmpdir/RtmpaV3OLd/R.INSTALL1b5d6089/beadarray/inst/doc/beadlevel.rnw"


bsh = BASH(BLData, array=1:10)



###################################################
### chunk number 10: bashResults
###################################################
#line 222 "E:/biocbld/bbs-2.7-bioc/tmpdir/RtmpaV3OLd/R.INSTALL1b5d6089/beadarray/inst/doc/beadlevel.rnw"

bsh$QC

par(mfrow=c(1,2))
barplot(bsh$QC[,1], main="Number of beads masked")

barplot(bsh$QC[,2], main="Extended Score")



###################################################
### chunk number 11: savingBASH
###################################################
#line 236 "E:/biocbld/bbs-2.7-bioc/tmpdir/RtmpaV3OLd/R.INSTALL1b5d6089/beadarray/inst/doc/beadlevel.rnw"

for(i in 1:10){

	BLData = setWeights(BLData, wts=bsh$wts[[i]], array=i)

}

BLData = insertSectionData(BLData, what="BASHQC", data = bsh$QC)



###################################################
### chunk number 12: Controls
###################################################
#line 253 "E:/biocbld/bbs-2.7-bioc/tmpdir/RtmpaV3OLd/R.INSTALL1b5d6089/beadarray/inst/doc/beadlevel.rnw"

data(controlProfile)

head(controlProfile)
table(controlProfile[,2])



###################################################
### chunk number 13: positiveControls
###################################################
#line 265 "E:/biocbld/bbs-2.7-bioc/tmpdir/RtmpaV3OLd/R.INSTALL1b5d6089/beadarray/inst/doc/beadlevel.rnw"

par(mfrow=c(2,5), mar = c(2,2,2,2))

for(i in 1:10){
	poscontPlot(BLData, array=i, controlProfile=controlProfile, ylim = c(10,16))
}


###################################################
### chunk number 14: positiveNegativeControls
###################################################
#line 274 "E:/biocbld/bbs-2.7-bioc/tmpdir/RtmpaV3OLd/R.INSTALL1b5d6089/beadarray/inst/doc/beadlevel.rnw"
par(mfrow=c(2,5), mar = c(2,2,2,2))

for(i in 1:10){
	poscontPlot(BLData, array=i, controlProfile=controlProfile,positiveControlTags = c("housekeeping", "negative"),  ylim = c(10,16))
}


###################################################
### chunk number 15: qaSummary
###################################################
#line 288 "E:/biocbld/bbs-2.7-bioc/tmpdir/RtmpaV3OLd/R.INSTALL1b5d6089/beadarray/inst/doc/beadlevel.rnw"

quickSummary(BLData, array=1, reporterIDs=controlProfile[,1], reporterTags=controlProfile[,2])


qcReport = makeQCTable(BLData, controlProfile=controlProfile)

head(qcReport)[,1:5]

BLData = insertSectionData(BLData, what="BeadLevelQC", data=qcReport)

names(BLData@sectionData)

for(i in 1:10){
print(controlProbeDetection(BLData, array = i, controlProfile=controlProfile, tagsToDetect=c("housekeeping", "biotin"), negativeTag="negative"))
}




###################################################
### chunk number 16: qcPipeline eval=FALSE
###################################################
## #line 313 "E:/biocbld/bbs-2.7-bioc/tmpdir/RtmpaV3OLd/R.INSTALL1b5d6089/beadarray/inst/doc/beadlevel.rnw"
## 
## expressionQCPipeline(BLData, qcDir="QC")
## 
## expressionQCPipeline(BLData, controlProfile=controlProfile, positiveControlTags=c("housekeeping", "biotin"), hybridisationTags = c("cy3_hyb", "high_stringency_hyb"), zlim=c(10,12), horizontal=F, negativeTag = "negative", tagsToDetect=list(Housekeeping = "housekeeping", Biotin = "biotin", hybridisation = "cy3_hyb"))
## 
## 


###################################################
### chunk number 17: outlierPlots
###################################################
#line 329 "E:/biocbld/bbs-2.7-bioc/tmpdir/RtmpaV3OLd/R.INSTALL1b5d6089/beadarray/inst/doc/beadlevel.rnw"

par(mfrow=c(1,3), mar = c(2,2,2,2))

outlierplot(BLData, array=1, horizontal = FALSE)

outlierplot(BLData, array=2,  horizontal = FALSE)

outlierplot(BLData, array=3, horizontal = FALSE)



###################################################
### chunk number 18: createBeadSummaryData
###################################################
#line 347 "E:/biocbld/bbs-2.7-bioc/tmpdir/RtmpaV3OLd/R.INSTALL1b5d6089/beadarray/inst/doc/beadlevel.rnw"

myMean = function(x) mean(x,na.rm=TRUE)
mySd = function(x) sd(x,na.rm=TRUE)

greenChannel = new("illuminaChannel", logGreenChannelTransform, illuminaOutlierMethod, myMean, mySd,"G")

BSData <- summarize(BLData, list(greenChannel))


myMedian = function(x) median(x, na.rm=TRUE)
mySe = function(x) sd(x, na.rm=TRUE)/sqrt(length(x))


greenChannel2 = new("illuminaChannel", logGreenChannelTransform, illuminaOutlierMethod, myMedian, mySe,"G")

BSData2 <- summarize(BLData, list(greenChannel2))



###################################################
### chunk number 19: showBSData
###################################################
#line 367 "E:/biocbld/bbs-2.7-bioc/tmpdir/RtmpaV3OLd/R.INSTALL1b5d6089/beadarray/inst/doc/beadlevel.rnw"
BSData

head(exprs(BSData)[,1:4])
head(se.exprs(BSData)[,1:4])

BSData2

head(exprs(BSData2)[,1:4])
head(se.exprs(BSData2)[,1:4])





###################################################
### chunk number 20: multipleChannel
###################################################
#line 387 "E:/biocbld/bbs-2.7-bioc/tmpdir/RtmpaV3OLd/R.INSTALL1b5d6089/beadarray/inst/doc/beadlevel.rnw"

greenBackgroundCorrected = new("illuminaChannel", getBackgroundCorrectionIntensities, illuminaOutlierMethod, myMean, mySd, "G.bc")

BSData.multChannel = summarize(BLData, channelList = list(greenChannel, greenBackgroundCorrected))

channelNames(BSData.multChannel)

G = channel(BSData.multChannel, "G")

G.bc = channel(BSData.multChannel, "G.bc")




###################################################
### chunk number 21: calculateDetection
###################################################
#line 405 "E:/biocbld/bbs-2.7-bioc/tmpdir/RtmpaV3OLd/R.INSTALL1b5d6089/beadarray/inst/doc/beadlevel.rnw"


status = rep("regular", as.numeric(dim(BSData.multChannel)[1]))

negIDs = controlProfile[which(controlProfile[,2] == "negative"),1]

status[match(negIDs, featureNames(BSData.multChannel))] = "negative"

det = calculateDetection(G, status=status)

head(det)

Detection(G) = det




###################################################
### chunk number 22: sessionInfo
###################################################
#line 424 "E:/biocbld/bbs-2.7-bioc/tmpdir/RtmpaV3OLd/R.INSTALL1b5d6089/beadarray/inst/doc/beadlevel.rnw"

sessionInfo()



