#####################################################
###
### Using forking with RAM objects
###
#####################################################
### Note to windows users: under Windows, this will
### result in sequential execution, as forking is not
### available.
## Get example input data and create data objects
data(inputEx)
## (this is not necessary, but is convenient;
## you could do the subsetting in the call themselves)
cgh.dat <- inputEx[, -c(1, 2, 3)]
chrom.dat <- as.integer(inputEx[, 2])
pos.dat <- inputEx[, 3]
## Segment with HaarSeg
haar.RAM.fork <- pSegmentHaarSeg(cgh.dat, chrom.dat,
merging = "MAD")
## What does the output look like?
lapply(haar.RAM.fork, head)
## Where and what length are segments in first sample?
rle(haar.RAM.fork$outSmoothed[, 1])
## Repeat, without load-balancing
haar.RAM.fork.nlb <- pSegmentHaarSeg(cgh.dat, chrom.dat,
merging = "MAD",
loadBalance = FALSE)
if(.Platform$OS.type != "windows") {
## We do not want this to run in Windows the automated tests since
## issues with I/O. It should work, though, in interactive usage
#####################################################
###
### Using forking with ff objects
###
#####################################################
### Note to windows users: under Windows, this will
### result in sequential execution, as forking is not
### available.
## Create a temp dir for storing output and ff objects.
## (Not needed, but cleaner).
dir.create("ADaCGH2_example_tmp_dir")
originalDir <- getwd()
setwd("ADaCGH2_example_tmp_dir")
## Get input data in ff format
## (we loaded the RData above, but we need to find the full path
## to use it in the call to inputToADaCGH)
fname <- list.files(path = system.file("data", package = "ADaCGH2"),
full.names = TRUE, pattern = "inputEx.RData")
inputToADaCGH(ff.or.RAM = "ff",
RDatafilename = fname)
## Segment with HaarSeg
haar.ff.fork <- pSegmentHaarSeg("cghData.RData",
"chromData.RData",
merging = "MAD")
## What does the output look like?
haar.ff.fork
## Note the warnings; we will be gentler in next example.
#####################################################
###
### Using a cluster with ff objects
###
#####################################################
## Start a socket cluster. Change the appropriate number of CPUs
## for your hardware and use other types of clusters (e.g., MPI)
## if you want.
cl2 <- parallel::makeCluster(4,"PSOCK")
parallel::clusterSetRNGStream(cl2)
parallel::setDefaultCluster(cl2)
parallel::clusterEvalQ(NULL, library("ADaCGH2"))
## The following is not really needed if you create the cluster AFTER
## changing directories. But better to be explicit.
wdir <- getwd()
parallel::clusterExport(NULL, "wdir")
parallel::clusterEvalQ(NULL, setwd(wdir))
## Segment with HaarSeg
haar.ff.cluster <- pSegmentHaarSeg("cghData.RData",
"chromData.RData",
merging = "MAD",
typeParall= "cluster")
## Avoid warnings by opening the objects
names(haar.ff.cluster)
open(haar.ff.cluster$outSmoothed)
open(haar.ff.cluster$outState)
## Alternatively, we can open the two ffdfs with lapply
## lapply(haar.ff.cluster, open)
##########################################
###
### Compare output (should be identical)
###
##########################################
all.equal(haar.ff.cluster$outSmoothed[ , ],
haar.ff.fork$outSmoothed[ , ])
all.equal(haar.ff.cluster$outSmoothed[ , ],
haar.RAM.fork$outSmoothed[ , ])
identical(haar.ff.cluster$outState[ , ],
haar.ff.fork$outState[ , ])
identical(haar.ff.cluster$outState[ , ],
haar.RAM.fork$outState[ , ])
#####################################################################
####
#### Clean up actions
####
#### (These are not needed. They are convenient here, to prevent
#### leaving garbage in your hard drive. In "real life" you will
#### have to decide what to delete and what to store).
#####################################################################
### Explicitly stop cluster
parallel::stopCluster(cl2)
### All objects (RData and ff) are left in this directory
getwd()
### We will clean it up, and do it step-by-step
### BEWARE: DO NOT do this with objects you want to keep!!!
## Remove ff and RData for the data
load("chromData.RData")
load("posData.RData")
load("cghData.RData")
delete(cghData); rm(cghData)
delete(posData); rm(posData)
delete(chromData); rm(chromData)
unlink("chromData.RData")
unlink("posData.RData")
unlink("cghData.RData")
unlink("probeNames.RData")
## Remove ff and R objects with segmentation results
lapply(haar.ff.fork, delete)
rm(haar.ff.fork)
lapply(haar.ff.cluster, delete)
rm(haar.ff.cluster)
### Try to prevent problems in R CMD check
## Sys.sleep(2)
### Delete temp dir
setwd(originalDir)
## Sys.sleep(2)
unlink("ADaCGH2_example_tmp_dir", recursive = TRUE)
## Sys.sleep(2)
}
Run the code above in your browser using DataLab