###################################################
### chunk number 1: library
###################################################
#line 49 "EfficientR-lab.Rnw"
library(AdvancedR2011)


###################################################
### chunk number 2: scripts
###################################################
#line 53 "EfficientR-lab.Rnw"
fl <- system.file("script", "efficient.R", 
                  package="AdvancedR2011")
source(fl)


###################################################
### chunk number 3: fname
###################################################
#line 74 "EfficientR-lab.Rnw"
fname0
f0
system.time(gwas0 <- f0(fname0, nrows=3))
dim(gwas0)


###################################################
### chunk number 4: f12
###################################################
#line 91 "EfficientR-lab.Rnw"
f1
f2


###################################################
### chunk number 5: system.time-f12
###################################################
#line 100 "EfficientR-lab.Rnw"
system.time(gwas1 <- f1(fname0, nrows=3))
system.time(gwas2 <- f2(fname0, nrows=3))


###################################################
### chunk number 6: object.size
###################################################
#line 107 "EfficientR-lab.Rnw"
object.size(gwas0)
object.size(gwas1)


###################################################
### chunk number 7: identical-f12
###################################################
#line 115 "EfficientR-lab.Rnw"
identical(gwas1, gwas2)    


###################################################
### chunk number 8: gtype
###################################################
#line 131 "EfficientR-lab.Rnw"
gtype <- f2(fname0, keep=1:10000, nrows=-1)
head(gtype[,1:5])


###################################################
### chunk number 9: shuffle0
###################################################
#line 141 "EfficientR-lab.Rnw"
shuffle0


###################################################
### chunk number 10: system.time-shuffl0
###################################################
#line 152 "EfficientR-lab.Rnw"
system.time(s0 <- shuffle0(gtype))


###################################################
### chunk number 11: profile
###################################################
#line 158 "EfficientR-lab.Rnw"
profFile <- tempfile()
Rprof(profFile)      # start gathering profile information
s0 <- shuffle0(gtype)
Rprof(NULL)          # stop
head(summaryRprof(profFile)$by.self)


###################################################
### chunk number 12: unlist
###################################################
#line 168 "EfficientR-lab.Rnw"
gsubset <- gtype[1:2, 1:3]
unlist(gsubset)


###################################################
### chunk number 13: shuffle1
###################################################
#line 177 "EfficientR-lab.Rnw"
shuffle1


###################################################
### chunk number 14: system.time-shuffle1
###################################################
#line 183 "EfficientR-lab.Rnw"
system.time(s1 <- shuffle1(gtype))
identical(s0, s1)


###################################################
### chunk number 15: shuffle2
###################################################
#line 190 "EfficientR-lab.Rnw"
shuffle2
system.time(s2 <- shuffle2(gtype))


###################################################
### chunk number 16: identical-s12
###################################################
#line 197 "EfficientR-lab.Rnw"
identical(s1, s2)


###################################################
### chunk number 17: all.equal
###################################################
#line 203 "EfficientR-lab.Rnw"
all.equal(s1, s2)
all.equal(s1, s2, check.attributes=FALSE)


###################################################
### chunk number 18: fapply
###################################################
#line 224 "EfficientR-lab.Rnw"
.fapply


###################################################
### chunk number 19: fapply-FUN
###################################################
#line 234 "EfficientR-lab.Rnw"
heterozygosity <- function(chunk, ...)
    list(N=nrow(chunk), Het=colSums(chunk == 2))


###################################################
### chunk number 20: fapply-get
###################################################
#line 242 "EfficientR-lab.Rnw"
get <- function(con)
    f2(con, nrows=100, ncols=.ncols, keep=1:100)


###################################################
### chunk number 21: fapply-reduce
###################################################
#line 256 "EfficientR-lab.Rnw"
reduce <- function(lst)
{
    n <- sum(sapply(lst, "[[", "N"))
    het <- Reduce("+", lapply(lst, "[[", "Het")) 
    het / n
}


###################################################
### chunk number 22: fapply
###################################################
#line 265 "EfficientR-lab.Rnw"
con <- file(fname0, open="r")
res <- .fapply(con, heterozygosity, .get=get, .reduce=reduce)
close(con)
length(res)


###################################################
### chunk number 23: fapply-densityplot
###################################################
#line 271 "EfficientR-lab.Rnw"
library(lattice)
print(densityplot(res, plot.points=FALSE, xlab="Heterozygosity", 
                  main="Stream"))


