Views on a 10000000-length Rle subject views: start end width [1] 1 1000 1000 [100 100 100 100 100 100 100 100 100 100 ...] [2] 1001 2000 1000 [190 190 190 190 190 190 190 190 190 190 ...] [3] 2001 3000 1000 [280 280 280 280 280 280 280 280 280 280 ...] [4] 3001 4000 1000 [370 370 370 370 370 370 370 370 370 370 ...] [5] 4001 5000 1000 [460 460 460 460 460 460 460 460 460 460 ...] ... ... ... ... ... [9996] 995001 9996000 9001000 [89650 89650 89650 89650 89650 89650 ...] [9997] 996001 9997000 9001000 [89740 89740 89740 89740 89740 89740 ...] [9998] 997001 9998000 9001000 [89830 89830 89830 89830 89830 89830 ...] [9999] 998001 9999000 9001000 [89920 89920 89920 89920 89920 89920 ...] [10000] 999001 10000000 9001000 [90010 90010 90010 90010 90010 90010 ...] [1] 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 library('GenomicRanges') # generating data frame df=data.frame(chrom=rep('Chr1',100000),start=seq(1,1000000,by=1000),end=seq(1000,10000000,by=1000),strand=rep("+",100000)) # making GRanges object gr=GRanges(seqnames=as.vector(df[,1]),IRanges(start=df[,2],end=df[,3]),strand=df[,4]) # obtaining coverage using function coverage in the form of RLE object gr.cov=coverage(gr) # generating views for specific start and end gr.views=Views(gr.cov[[1]],start=seq(1,1000000,by=1000),end=seq(1000,10000000,by=1000)) # putting in temp variable d=gr.views # this following code calculates the matrix (where each line is 20 points) for 10 lines # reduce or increase the number in the outermost sapply loop to increase/decrease the lines to be calculated sapply(1:10,function(j) sapply(1:20, function(i)as.numeric( format( mean( as(d[[j]][( seq(0,length(d[[j]]),floor(length(d[[j]])/20))+1)[i]: c((seq(0,length(d[[j]]),floor(length(d[[j]])/20)))[ -length((seq(0,length(d[[j]]),floor(length(d[[j]])/20)))) ],length(d[[j]]))[i+1]], "RangedData")$score), digits=2) ) ) ) genes <- GRanges(seqnames, IRanges(geneStarts, geneEnds)) n <- 50L starts0 <- Map(function(...) floor(seq(...)), start(genes), end(genes), MoreArgs=list(length.out=n + 1L)) ends <- lapply(starts0, function(x) floor(x)[-1]) starts <- lapply(starts0, function(x) x[-length(x)]) v <- Views(gr.cov[[1]], start=unlist(starts), end=unlist(ends)) split(viewMeans(v), rep(seq_along(genes), each=n)) > (x <- Rle(c(rep(100, 10), rep(200, 10)))) numeric-Rle of length 20 with 2 runs Lengths: 10 10 Values : 100 200 > runValue(x) [1] 100 200 > runLength(x) [1] 10 10