Extracting values from out-of-bounds Rle Views
Entering edit mode
Last seen 9 weeks ago
Univeristy & ETH Z├╝rich, Switzerland

Suppose I create an out-of-range view of a Rle:

co <- Rle(values=c(0,1,0), lengths=c(100,50,20))
v <- Views(co, 125,175)

As indicated in the documentation, trying to extract the values leads to an out-of-bounds error:

as(v, "IntegerList")

These give Error in getListElement(x, i, ...) : view is out of limits and Error: subscript contains out-of-bounds ranges, respectively.

I can see arguments for this design choice, although I'd personally have preferred NA values for the out of range locations (along with a warning)...

My use case is pretty common I think: I have lots of Views of the same width, and I want to compare the signal across those Views. Because we enlarge around the actual sites to get an idea of the surroundings, we can get out-of-bounds views. We can't simply trim because that would disrupt the positional alignment with the other views. So in order to extract signals, I need to trim the views (to be able to get the values), and then add back the padding on both sides, which I currently do this way:

view2paddedIL <- function(v, padVal=NA_integer_){
  v2 <- trim(v)
  if(any(w <- width(v2)==0)){
    v <- v[which(!w)]
    v2 <- v2[which(!w)]
    warning(sum(w), " views were excluded as completely out of range.")
  # figure out how much is trimmed on either side
  pleft <- start(v2)-start(v)
  pright <- end(v)-end(v2)
  # concatenate the list elements with their padding
  n <- seq_along(v2)
  v <- splitAsList(c( rep(padVal, sum(pleft)),
                      unlist(IntegerList(v2), use.names=FALSE),
                      rep(padVal, sum(pright))),
                   c(rep(n, pleft), rep(n, width(v2)), rep(n, pright)))
  names(v) <- names(v2)

This gives me what I want, but the whole thing sounds rather convoluted.

I wonder therefore whether there's some obvious method I'm missing, or if someone has a better idea?

IRanges XVector AtomicList • 64 views

Login before adding your answer.

Traffic: 292 users visited in the last hour
Help About
Access RSS

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6