r format(Sys.time(), "%d %B, %Y; %R")

Setup

knitr::opts_chunk$set(echo=TRUE, warning=FALSE, message=FALSE, 
                      error=TRUE)
suppressWarnings(library('tidyverse'))
suppressWarnings(library('magrittr'))
suppressWarnings(library('rtracklayer'))
suppressWarnings(library('microbenchmark'))

select regions and a bigwig file

bedfile <- system.file("extdata", "Chen_PROMPT_TSSs_liftedTohg38.bed", package = "RMetaTools")
regions <- rtracklayer::import(bedfile)

bw <- system.file("extdata", "GSM1573841_mNET_8WG16_siLuc_plus_hg38.bw", package = "RMetaTools")

benchmark import function for the 3 return types

microbenchmark(
  import(bw, which=regions, as = c("GRanges")),
  import(bw, which=regions, as = c("RleList")),
  import(bw, which=regions, as = c("NumericList")))

--> the import type almost does not matter, RleList is slightly slower than the others though.

benchmark import+sum function for the 3 return types

sums_via_GRanges <- function(bw, regions){
sapply(seq_along(regions), function(i) {
  hits <- import(bw, which=regions[i], as = c("GRanges"))
  if(length(hits) > 0){
    return(sum(hits$score*width(hits)))
  }else{
    return(0)
  }
})
}

microbenchmark(sums_via_GRanges(bw, regions), times=1)
sums_via_NumericList <- function(bw, regions){ 
  sapply(import(bw, which=regions, as = c("NumericList")), sum)
}

microbenchmark(sums_via_NumericList(bw, regions), times=1)
sessionInfo()


manschmi/RMetaTools documentation built on Dec. 14, 2021, 4:33 a.m.