### R code from vignette source 'QDNAseq.Rnw'

###################################################
### code chunk number 1: style-Sweave
###################################################
BiocStyle::latex()


###################################################
### code chunk number 2: QDNAseq.Rnw:23-24
###################################################
library(QDNAseq)


###################################################
### code chunk number 3: QDNAseq.Rnw:27-29
###################################################
options("QDNAseq::verbose"=NA)
options(width=40)


###################################################
### code chunk number 4: QDNAseq.Rnw:66-78 (eval = FALSE)
###################################################
## readCounts <- binReadCounts(bins)
## # all files ending in .bam from the current working directory
## 
## # or
## 
## readCounts <- binReadCounts(bins, bamfiles='tumor.bam')
## # file 'tumor.bam' from the current working directory
## 
## # or
## 
## readCounts <- binReadCounts(bins, path='tumors')
## # all files ending in .bam from the subdirectory 'tumors'


###################################################
### code chunk number 5: QDNAseq.Rnw:96-99
###################################################
data(LGG150)
readCounts <- LGG150
readCounts


###################################################
### code chunk number 6: QDNAseq.Rnw:105-106
###################################################
png("rawprofile.png")


###################################################
### code chunk number 7: rawprofile
###################################################
plot(readCounts, logTransform=FALSE, ylim=c(-50, 200))
highlightFilters(readCounts, logTransform=FALSE,
  residual=TRUE, blacklist=TRUE)


###################################################
### code chunk number 8: QDNAseq.Rnw:113-114
###################################################
dev.off()


###################################################
### code chunk number 9: QDNAseq.Rnw:130-132
###################################################
readCountsFiltered <- applyFilters(readCounts,
  residual=TRUE, blacklist=TRUE)


###################################################
### code chunk number 10: QDNAseq.Rnw:134-135
###################################################
png("isobar.png")


###################################################
### code chunk number 11: isobar
###################################################
isobarPlot(readCountsFiltered)


###################################################
### code chunk number 12: QDNAseq.Rnw:140-141
###################################################
dev.off()


###################################################
### code chunk number 13: QDNAseq.Rnw:159-160
###################################################
readCountsFiltered <- estimateCorrection(readCountsFiltered)


###################################################
### code chunk number 14: QDNAseq.Rnw:162-163
###################################################
png("noise.png")


###################################################
### code chunk number 15: noise
###################################################
noisePlot(readCountsFiltered)


###################################################
### code chunk number 16: QDNAseq.Rnw:168-169
###################################################
dev.off()


###################################################
### code chunk number 17: QDNAseq.Rnw:183-187
###################################################
copyNumbers <- correctBins(readCountsFiltered)
copyNumbers
copyNumbersNormalized <- normalizeBins(copyNumbers)
copyNumbersSmooth <- smoothOutlierBins(copyNumbersNormalized)


###################################################
### code chunk number 18: QDNAseq.Rnw:189-190
###################################################
png("profile.png")


###################################################
### code chunk number 19: profile
###################################################
plot(copyNumbersSmooth)


###################################################
### code chunk number 20: QDNAseq.Rnw:195-196
###################################################
dev.off()


###################################################
### code chunk number 21: QDNAseq.Rnw:210-213 (eval = FALSE)
###################################################
## exportBins(copyNumbersSmooth, file="LGG150.txt")
## exportBins(copyNumbersSmooth, file="LGG150.igv", format="igv")
## exportBins(copyNumbersSmooth, file="LGG150.bed", format="bed")


###################################################
### code chunk number 22: QDNAseq.Rnw:226-228
###################################################
copyNumbersSegmented <- segmentBins(copyNumbersSmooth, transformFun="sqrt")
copyNumbersSegmented <- normalizeSegmentedBins(copyNumbersSegmented)


###################################################
### code chunk number 23: QDNAseq.Rnw:230-231
###################################################
png("segments.png")


###################################################
### code chunk number 24: segments
###################################################
plot(copyNumbersSegmented)


###################################################
### code chunk number 25: QDNAseq.Rnw:236-237
###################################################
dev.off()


###################################################
### code chunk number 26: QDNAseq.Rnw:250-251
###################################################
copyNumbersCalled <- callBins(copyNumbersSegmented)


###################################################
### code chunk number 27: QDNAseq.Rnw:253-254
###################################################
png("calls.png")


###################################################
### code chunk number 28: calls
###################################################
plot(copyNumbersCalled)


###################################################
### code chunk number 29: QDNAseq.Rnw:259-260
###################################################
dev.off()


###################################################
### code chunk number 30: QDNAseq.Rnw:272-274 (eval = FALSE)
###################################################
## exportBins(copyNumbersCalled, format="vcf")
## exportBins(copyNumbersCalled, format="seg")


###################################################
### code chunk number 31: QDNAseq.Rnw:292-294
###################################################
cgh <- makeCgh(copyNumbersCalled)
cgh


###################################################
### code chunk number 32: QDNAseq.Rnw:334-335 (eval = FALSE)
###################################################
## copyNumbers <- callBins(..., ncpus=4L)


###################################################
### code chunk number 33: QDNAseq.Rnw:343-344 (eval = FALSE)
###################################################
## future::plan("eager")


###################################################
### code chunk number 34: QDNAseq.Rnw:352-353 (eval = FALSE)
###################################################
## future::plan("multiprocess")


###################################################
### code chunk number 35: QDNAseq.Rnw:367-368 (eval = FALSE)
###################################################
## options(mc.cores=4L)


###################################################
### code chunk number 36: QDNAseq.Rnw:376-378 (eval = FALSE)
###################################################
## cl <- parallel::makeCluster(...)
## future::plan("cluster", cluster=cl)


###################################################
### code chunk number 37: QDNAseq.Rnw:398-403 (eval = FALSE)
###################################################
## readCounts <- binReadCounts(getBinAnnotations(15))
## readCounts <- applyFilters(readCounts)
## readCounts <- estimateCorrection(readCounts)
## readCounts <- applyFilters(readCounts, chromosomes=NA)
## copyNumbers <- correctBins(readCounts)


###################################################
### code chunk number 38: QDNAseq.Rnw:418-420 (eval = FALSE)
###################################################
## readCounts <- estimateCorrection(readCounts,
##   control=loess.control(surface="direct"))


###################################################
### code chunk number 39: QDNAseq.Rnw:434-444 (eval = FALSE)
###################################################
## # load required packages for human reference genome build hg19
## library(QDNAseq)
## library(Biobase)
## library(BSgenome.Hsapiens.UCSC.hg19)
## 
## # set the bin size
## binSize <- 15
## 
## # create bins from the reference genome
## bins <- createBins(bsgenome=BSgenome.Hsapiens.UCSC.hg19, binSize=binSize)


###################################################
### code chunk number 40: QDNAseq.Rnw:461-465 (eval = FALSE)
###################################################
## # calculate mappabilites per bin from ENCODE mapability tracks
## bins$mappability <- calculateMappability(bins,
##   bigWigFile='/path/to/wgEncodeCrgMapabilityAlign50mer.bigWig',
##   bigWigAverageOverBed='/path/to/bigWigAverageOverBed')


###################################################
### code chunk number 41: QDNAseq.Rnw:477-481 (eval = FALSE)
###################################################
## # calculate overlap with ENCODE blacklisted regions
## bins$blacklist <- calculateBlacklist(bins,
##   bedFiles=c('/path/to/wgEncodeDacMapabilityConsensusExcludable.bed',
##   '/path/to/wgEncodeDukeMapabilityRegionsExcludable.bed'))


###################################################
### code chunk number 42: QDNAseq.Rnw:487-490 (eval = FALSE)
###################################################
## # generic calculation of overlap with blacklisted regions
## bins$blacklist <- calculateBlacklistByRegions(bins, 
##   cbind(chromosome, bpStart, bpEnd))


###################################################
### code chunk number 43: QDNAseq.Rnw:498-504 (eval = FALSE)
###################################################
## # load data for the 1000 Genomes (or similar) data set, and generate residuals
## ctrl <- binReadCounts(bins,
##   path='/path/to/control-set/bam/files')
## ctrl <- applyFilters(ctrl, residual=FALSE, blacklist=FALSE,
##   mappability=FALSE, bases=FALSE)
## bins$residual <- iterateResiduals(ctrl)


###################################################
### code chunk number 44: QDNAseq.Rnw:513-516 (eval = FALSE)
###################################################
## # by default, use all autosomal bins that have a reference sequence
## # (i.e. not only N's)
## bins$use <- bins$chromosome %in% as.character(1:22) & bins$bases > 0


###################################################
### code chunk number 45: QDNAseq.Rnw:522-535 (eval = FALSE)
###################################################
## # convert to AnnotatedDataFrame and add metadata
## bins <- AnnotatedDataFrame(bins,
##   varMetadata=data.frame(labelDescription=c(
##   'Chromosome name',
##   'Base pair start position',
##   'Base pair end position',
##   'Percentage of non-N nucleotides (of full bin size)',
##   'Percentage of C and G nucleotides (of non-N nucleotides)',
##   'Average mappability of 50mers with a maximum of 2 mismatches',
##   'Percent overlap with ENCODE blacklisted regions',
##   'Median loess residual from 1000 Genomes (50mers)',
##   'Whether the bin should be used in subsequent analysis steps'),
##   row.names=colnames(bins)))


###################################################
### code chunk number 46: QDNAseq.Rnw:541-549 (eval = FALSE)
###################################################
## attr(bins, "QDNAseq") <- list(
##   author="Ilari Scheinin",
##   date=Sys.time(),
##   organism="Hsapiens",
##   build="hg19",
##   version=packageVersion("QDNAseq"),
##   md5=digest::digest(bins@data),
##   sessionInfo=sessionInfo())


###################################################
### code chunk number 47: QDNAseq.Rnw:559-592 (eval = FALSE)
###################################################
## # download table of samples
## server <- "ftp://ftp.1000genomes.ebi.ac.uk/vol1/ftp/"
## g1k <- read.table(paste0(server, "sequence.index"),
##   header=TRUE, sep="\t", as.is=TRUE, fill=TRUE)
## 
## # keep cases that are Illumina, low coverage, single-read, and not withdrawn
## g1k <- g1k[g1k$INSTRUMENT_PLATFORM == 'ILLUMINA', ]
## g1k <- g1k[g1k$ANALYSIS_GROUP == 'low coverage', ]
## g1k <- g1k[g1k$LIBRARY_LAYOUT == 'SINGLE', ]
## g1k <- g1k[g1k$WITHDRAWN == 0, ]
## 
## # keep cases with read lengths of at least 50 bp
## g1k <- g1k[!g1k$BASE_COUNT %in% c("not available", ""), ]
## g1k$BASE_COUNT <- as.numeric(g1k$BASE_COUNT)
## g1k$READ_COUNT <- as.integer(g1k$READ_COUNT)
## g1k$readLength <- g1k$BASE_COUNT / g1k$READ_COUNT
## g1k <- g1k[g1k$readLength > 50, ]
## 
## # keep samples with a minimum of one million reads
## readCountPerSample <- aggregate(g1k$READ_COUNT,
##   by=list(sample=g1k$SAMPLE_NAME), sum)
## g1k <- g1k[g1k$SAMPLE_NAME %in%
##   readCountPerSample$sample[readCountPerSample$x >= 1e6], ]
## 
## g1k$fileName <- basename(g1k$FASTQ_FILE)
## 
## # download fastq files
## for (i in rownames(g1k)) {
##   sourceFile <- paste0(server, g1k[i, "FASTQ_FILE"])
##   destFile <- g1k[i, "fileName"]
##   if (!file.exists(destFile))
##     download.file(sourceFile, destFile)
## }


###################################################
### code chunk number 48: QDNAseq.Rnw:607-608
###################################################
toLatex(sessionInfo())