Bioconductor/IRanges

Reverse IRangesList

Closed this issue · 3 comments

Is there a vectorized alternative for endoapply(IRangesList, rev)?

x <- IRanges(start = 1:10, width = 3)
group <- c(1,1,2,2,2,3,3,4,4,4)
split_x <- split(x, group)
rev_split_x <- endoapply(split_x, rev)
# works

reverse does not work in this case:

reverse(split_x)
> Error in (function (classes, fdef, mtable)  : 
> unable to find an inherited method for function ‘reverse’ for signature ‘"CompressedIRangesList"’
hpages commented

Hi,

You can use revElements() for that:

> revElements(split_x)
IRangesList object of length 4:
$`1`
IRanges object with 2 ranges and 0 metadata columns:
          start       end     width
      <integer> <integer> <integer>
  [1]         2         4         3
  [2]         1         3         3

$`2`
IRanges object with 3 ranges and 0 metadata columns:
          start       end     width
      <integer> <integer> <integer>
  [1]         5         7         3
  [2]         4         6         3
  [3]         3         5         3

$`3`
IRanges object with 2 ranges and 0 metadata columns:
          start       end     width
      <integer> <integer> <integer>
  [1]         7         9         3
  [2]         6         8         3

$`4`
IRanges object with 3 ranges and 0 metadata columns:
          start       end     width
      <integer> <integer> <integer>
  [1]        10        12         3
  [2]         9        11         3
  [3]         8        10         3

Note that revElements() has a 2nd argument that lets you specify which list-elements to reverse (by default all elements are reversed). According to the man page (?revElements):

  ‘revElements(x, i)’ reverses the list elements in ‘x’ specified
  by ‘i’. It's equivalent to, but faster than, doing ‘x[i] <-
  endoapply(x[i], rev)’.

We should probably unify reverse() and revElements(), or at least define a reverse() method for list-like objects that calls revElements().

Also note that a good place to find help and ask questions about Bioconductor packages is on our support site.

Best,
H.

Hi Hervé,

Sorry, I'd missedrevElements in the docs. And thank you for your prompt response, this is what I needed.

Best,
Thiago

hpages commented

No worries, it's easy to miss. I just added a link in the man page for reverse() (see caa88ee).