### R code from vignette source 'ChIC-Vignette.Rnw'

###################################################
### code chunk number 1: ChIC-Vignette.Rnw:134-138
###################################################
options(width=60, str=strOptions(vec.len=3))
library(ChIC)
chipName <- "ENCFF000BFX"
inputName <- "ENCFF000BDQ"


###################################################
### code chunk number 2: ChIC-Vignette.Rnw:142-153 (eval = FALSE)
###################################################
## library(ChIC)
## 
## chipName <- "ENCFF000BFX"
## inputName <- "ENCFF000BDQ"
## 
## system(paste("wget https://www.encodeproject.org/files/",
##     chipName, "/@@download/",
##     chipName, ".bam", sep=""))
## system(paste("wget https://www.encodeproject.org/files/",
##     inputName, "/@@download/",
##     inputName, ".bam", sep=""))


###################################################
### code chunk number 3: ChIC-Vignette.Rnw:163-171 (eval = FALSE)
###################################################
## ChIC_RFscore<-chicWrapper(
##     chipName=chipName,
##     inputName=inputName, 
##     savePlotPath=getwd(),
##     target= "H3K4me3",
##     read_length=36,
##     annotationID="hg19"
##     )


###################################################
### code chunk number 4: ChIC-Vignette.Rnw:225-226
###################################################
listAvailableElements("sharp")


###################################################
### code chunk number 5: ChIC-Vignette.Rnw:230-231
###################################################
listAvailableElements("broad")


###################################################
### code chunk number 6: ChIC-Vignette.Rnw:295-297 (eval = FALSE)
###################################################
## chipBam <- readBamFile(chipName)
## inputBam <- readBamFile(inputName)


###################################################
### code chunk number 7: ChIC-Vignette.Rnw:303-308 (eval = FALSE)
###################################################
## subset_chromosomes<-c("chr17","chr18","chr19")
## chipSubset<-lapply(chipBam,
##     FUN=function(x) {x[subset_chromosomes]})
## inputSubset<-lapply(inputBam,
##     FUN=function(x) {x[subset_chromosomes]})


###################################################
### code chunk number 8: ChIC-Vignette.Rnw:313-319
###################################################
data("chipSubset", package = "ChIC.data", 
    envir = environment())
str(chipSubset)
data("inputSubset", package = "ChIC.data", 
 envir = environment())
str(inputSubset)


###################################################
### code chunk number 9: ChIC-Vignette.Rnw:331-338 (eval = FALSE)
###################################################
## EM_Results <- qualityScores_EM(
##     chipName=chipName, 
##     inputName=inputName,
##     chip.data=chipSubset, 
##     input.data=inputSubset,
##     annotationID="hg19",
##     read_length=36)


###################################################
### code chunk number 10: ChIC-Vignette.Rnw:341-343
###################################################
data("EM_Results", package = "ChIC.data", 
    envir = environment())


###################################################
### code chunk number 11: ChIC-Vignette.Rnw:356-364 (eval = FALSE)
###################################################
## EM_Results <- qualityScores_EM(
##     chipName=chipName, 
##     inputName=inputName,
##     chip.data=chipSubset, 
##     input.data=inputSubset,
##     annotationID="hg19",
##     read_length=36,
##     mc=20)


###################################################
### code chunk number 12: ChIC-Vignette.Rnw:385-386
###################################################
   finalTagshift<-EM_Results$QCscores_ChIP$tag.shift


###################################################
### code chunk number 13: ChIC-Vignette.Rnw:393-395
###################################################
selectedTagsChip<-EM_Results$SelectedTagsChip
selectedTagsInput<-EM_Results$SelectedTagsInput


###################################################
### code chunk number 14: Fingerprint (eval = FALSE)
###################################################
## GM_Results<-qualityScores_GM(
##         selectedTagsChip=selectedTagsChip,
##         selectedTagsInput=selectedTagsInput,
##         tag.shift=finalTagshift,
##         annotationID="hg19")


###################################################
### code chunk number 15: ChIC-Vignette.Rnw:425-426
###################################################
GM_Results<-qualityScores_GM(
        selectedTagsChip=selectedTagsChip,
        selectedTagsInput=selectedTagsInput,
        tag.shift=finalTagshift,
        annotationID="hg19")


###################################################
### code chunk number 16: ChIC-Vignette.Rnw:433-434
###################################################
str(GM_Results)


###################################################
### code chunk number 17: ChIC-Vignette.Rnw:444-449
###################################################
Meta_Results<-createMetageneProfile(
        selectedTagsChip=selectedTagsChip,
        selectedTagsInput=selectedTagsInput,
        tag.shift=finalTagshift,
        annotationID="hg19")


###################################################
### code chunk number 18: ChIC-Vignette.Rnw:452-457 (eval = FALSE)
###################################################
## Meta_Results<-createMetageneProfile(
##         selectedTagsChip=selectedTagsChip,
##         selectedTagsInput=selectedTagsInput,
##         tag.shift=finalTagshift,
##         annotationID="hg19")


###################################################
### code chunk number 19: TSSplot (eval = FALSE)
###################################################
## TSSProfile<-qualityScores_LM(
##         data=Meta_Results,
##         tag="TSS", plot="split")


###################################################
### code chunk number 20: ChIC-Vignette.Rnw:492-493
###################################################
TSSProfile<-qualityScores_LM(
        data=Meta_Results,
        tag="TSS", plot="split")


###################################################
### code chunk number 21: geneBodyplot (eval = FALSE)
###################################################
## geneBodyProfile<-qualityScores_LM(
##         data=Meta_Results,
##         tag="geneBody", plot="split")


###################################################
### code chunk number 22: ChIC-Vignette.Rnw:519-520
###################################################
geneBodyProfile<-qualityScores_LM(
        data=Meta_Results,
        tag="geneBody", plot="split")


###################################################
### code chunk number 23: geneBodyplotNorm (eval = FALSE)
###################################################
## geneBodyProfile<-qualityScores_LM(
##         data=Meta_Results,
##         tag="geneBody", plot="norm")


###################################################
### code chunk number 24: ChIC-Vignette.Rnw:541-542
###################################################
geneBodyProfile<-qualityScores_LM(
        data=Meta_Results,
        tag="geneBody", plot="norm")


###################################################
### code chunk number 25: ChIC-Vignette.Rnw:575-576
###################################################
head(listDatasets(dataset="ENCODE"))


###################################################
### code chunk number 26: geneBodyplotComparison (eval = FALSE)
###################################################
## metagenePlotsForComparison(
##     data = Meta_Results,
##     target = "H3K4me3", 
##     tag = "geneBody",
##     plot="chip")


###################################################
### code chunk number 27: ChIC-Vignette.Rnw:603-604
###################################################
metagenePlotsForComparison(
    data = Meta_Results,
    target = "H3K4me3", 
    tag = "geneBody",
    plot="chip")


###################################################
### code chunk number 28: geneBodyplotComparisonNorm (eval = FALSE)
###################################################
## metagenePlotsForComparison(
##     data = Meta_Results,
##     target = "H3K4me3", 
##     tag = "geneBody",
##     plot="norm")


###################################################
### code chunk number 29: ChIC-Vignette.Rnw:627-628
###################################################
metagenePlotsForComparison(
    data = Meta_Results,
    target = "H3K4me3", 
    tag = "geneBody",
    plot="norm")


###################################################
### code chunk number 30: TSSplotComparison (eval = FALSE)
###################################################
## metagenePlotsForComparison(
##     data = Meta_Results,
##     target = "H3K4me3", 
##     tag = "TSS",
##     plot="norm")


###################################################
### code chunk number 31: ChIC-Vignette.Rnw:650-651
###################################################
metagenePlotsForComparison(
    data = Meta_Results,
    target = "H3K4me3", 
    tag = "TSS",
    plot="norm")


###################################################
### code chunk number 32: ChIC-Vignette.Rnw:666-667
###################################################
listAvailableElements(target="mark")


###################################################
### code chunk number 33: ValueComparison (eval = FALSE)
###################################################
## plotReferenceDistribution(
##     target = "H3K4me3", 
##     metricToBePlotted = "RSC", 
##     currentValue = EM_Results$QCscores_ChIP$CC_RSC
##     )


###################################################
### code chunk number 34: ChIC-Vignette.Rnw:694-695
###################################################
plotReferenceDistribution(
    target = "H3K4me3", 
    metricToBePlotted = "RSC", 
    currentValue = EM_Results$QCscores_ChIP$CC_RSC
    )


###################################################
### code chunk number 35: ChIC-Vignette.Rnw:708-709
###################################################
head(listMetrics("EM"))


###################################################
### code chunk number 36: ChIC-Vignette.Rnw:717-721
###################################################
# compute the missing part
TESProfile<-qualityScores_LM(
        data=Meta_Results,
        tag="TES", plot="none")


###################################################
### code chunk number 37: ChIC-Vignette.Rnw:725-733
###################################################
ChIC_RFscore <-predictionScore(
target="H3K4me3",
features_cc=EM_Results,
features_global=GM_Results,
features_TSS=TSSProfile,
features_TES=TESProfile,
features_scaled=geneBodyProfile
)


###################################################
### code chunk number 38: ChIC-Vignette.Rnw:738-739
###################################################
ChIC_RFscore["values", "P"]