R - example of mclapply() usage

03 Jun 2018

Example how to use parallel::mclapply:

binnedMean2 <- function(bins, numvar) {
  stopifnot(is(bins, "GRangesList"))
  stopifnot(is(numvar, "RleList"))
  mean.list <- mclapply(names(bins),
                        function(binname) {
                          print(binname)
                          seqname=unique(as.character(seqnames(bins[[binname]])))
                          views <- Views(numvar[[seqname]],ranges(bins[[binname]]))
                          sum(viewSums(views))/sum(as.numeric(width(bins[[binname]])))
                        },mc.cores=9)
  names(mean.list) <- names(bins)
  mean.list <- unlist(mean.list)
  mean.list
}
[ R  function  parallel  ]