require("doMC")
require("doNWS")

# Numeric Iterator
i <- iter(1:3)
nextElem(i)

# Long sequences
i <- icount(1e9)
nextElem(i)

# Matrix dimensions
M <- matrix(1:25,ncol=5)
r <- iter(M,by="row")
nextElem(r)

# Data File
# write.table(MSFT,"MSFT.csv",sep=",",row.names=F)
rec <- iread.table("MSFT.csv",sep=",", header=T, row.names=NULL)
nextElem(rec)

# Irregular sequences
require(gmp)
iprime <- function() {
 lastPrime <- 1

 nextEl <- function() {
  lastPrime <<- as.numeric(nextprime(lastPrime))
  lastPrime
 }
 it <- list(nextElem=nextEl)
 class(it) <- c('abstractiter','iter')
 it
}

p <- iprime()
nextElem(p)

# Database
m <- dbDriver('SQLite')
con <- dbConnect(m, dbname="arrests")
it <- iquery(con, 'select * from USArrests', n=10)
nextElem(it)

## simple foreach examples
foreach (j=1:4) %dopar% sqrt (j)
foreach (j=1:4, .combine=c) %dopar% sqrt(j)
foreach (j=1:4, .combine="+", .inorder=FALSE) %dopar% sqrt(j)

#referencing global variables
z <- 2
f <- function (x) sqrt (x + z)
foreach (j=1:4, .combine="+") %dopar% f(j)

#nested loops
foreach(i=1:3, .combine=cbind) %:% 
  foreach(j=1:3, .combine=c) %dopar% 
    (i + j)

## Birthday function
birthday <- function(n) {
  ntests <- 10000
  pop <- 1:365
  anydup <- function(i) 
      any(duplicated(
		sample(pop, n, replace=TRUE)))
  sum(sapply(seq(ntests), anydup)) / ntests
}

registerDoSEQ()
system.time(
x <- foreach (j=1:100) %dopar% birthday (j)
)
# 41s
plot(unlist(x),type="l")

## Warning: Do not run from R.app GUI on MacOS
registerDoMC()
system.time(
x <- foreach (j=1:100) %dopar% birthday (j)
)
# 28s

s <- sleigh(workerCount=2)
registerDoNWS(s)
system.time(
x <- foreach (j=1:100) %dopar% birthday (j)
)
# 26s

### BIOCONDUCTOR EXAMPLES

# Example from ppiData package vignettes:
# http://www.bioconductor.org/packages/release/data/experiment/vignettes/ppiData/inst/doc/ppiStats.pdf

## Get specified data-sets
dataList <- collectIntactPPIData(c("EBI-531419","EBI-698096","EBI-762635"))

## Create sparse matrix representation of data
bpList <- createBPList(dataList[["indexSetAll"]], 
   dataList[["baitsSystematic"]],  
   dataList[["preysSystematic"]])


## sequential version
bpMats <- lapply(bpList, function(x) {
  bpMatrix(x, symMat = FALSE, homodimer = FALSE, baitAsPrey = FALSE,
          unweighted = TRUE, onlyRecip = FALSE, baitsOnly = FALSE)
})

## parallel version
bpMats <- foreach(x=iter(bpList), .packages = "ppiStats") %dopar% {
   bpMatrix(x, sysMat = FALSE, homodimer = FALSE, baitAsPrey = FALSE,
            unWeighted = TRUE, onlyRecip = FALSE, baitsOnly = FALSE)
}

# Create graph object from adjacency matrix
# sequential
bpMats1 <- lapply(bpList, function(x) {
   bpMatrix(x, symMat = TRUE, homodimer = FALSE, baitAsPrey = FALSE,
            unWeighted = FALSE, onlyRecip = FALSE, baitsOnly = FALSE)
})
bpGraphs <- lapply(bpMats1, function(x) {
   genBPGraph(x, directed = TRUE, bp = FALSE)
})

# parallel
bpMats1 <- foreach(x=iter(bpList), .packages = "ppiStats") %dopar% {
   bpMatrix(x, sysMat = TRUE, homodimer = FALSE, baitAsPrey = FALSE,
            unWeighted = FALSE, onlyRecip = FALSE, baitsOnly = FALSE)
}
bpGraph <- foreach(x=iter(bpMat1), .packages = "ppiStats") %dopar% {
   genBPGraph(x, directed = TRUE, bp = FALSE)
}
