Extracting values from out-of-bounds Rle Views
0
0
Entering edit mode
@pierre-luc-germain-21191
Last seen 2.3 years ago
Univeristy & ETH Zürich, Switzerland

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

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

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

as(v, "IntegerList")
v[[1]]

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_){
  stopifnot(is.integer(padVal))
  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)
  v
}

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 • 865 views
ADD COMMENT

Login before adding your answer.

Traffic: 410 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

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

Powered by the version 2.3.6