I can go from a GRanges object to a GRangesList object by splitting it. For example:
(gr <- GRanges(
seqnames = paste0("chr", c(1, 2, 3, 4, 1, 2, 3, 1)),
ranges = IRanges(c(1:8), c(16:9)),
strand = rep.int(c("+", "-"), 4)
))
## GRanges object with 8 ranges and 0 metadata columns:
## seqnames ranges strand
## <Rle> <IRanges> <Rle>
## [1] chr1 [1, 16] +
## [2] chr2 [2, 15] -
## [3] chr3 [3, 14] +
## [4] chr4 [4, 13] -
## [5] chr1 [5, 12] +
## [6] chr2 [6, 11] -
## [7] chr3 [7, 10] +
## [8] chr1 [8, 9] -
## -------
## seqinfo: 4 sequences from an unspecified genome; no seqlengths
(grl <- split(gr, seqnames(gr)))
## GRangesList object of length 4:
## $chr1
## GRanges object with 3 ranges and 0 metadata columns:
## seqnames ranges strand
## <Rle> <IRanges> <Rle>
## [1] chr1 [1, 16] +
## [2] chr1 [5, 12] +
## [3] chr1 [8, 9] -
##
## $chr2
## GRanges object with 2 ranges and 0 metadata columns:
## seqnames ranges strand
## [1] chr2 [2, 15] -
## [2] chr2 [6, 11] -
##
## $chr3
## GRanges object with 2 ranges and 0 metadata columns:
## seqnames ranges strand
## [1] chr3 [3, 14] +
## [2] chr3 [7, 10] +
##
## ...
## <1 more element>
## -------
## seqinfo: 4 sequences from an unspecified genome; no seqlengths
(See also Converting single GRanges object as per chromosome GRangeList.)
How do I go concatenate the elements of a GRangesList object into a GRanges object?
I want to do:
do.call(rbind, grl)
But this throws an error
## Error in rbind2(..1) : no method for coercing this S4 class to a vector
Or possibly:
do.call(c, grl)
But this leaves grl unchanged.

Using
do.callandcworks, but you have to help R find the correct method method ofcto use.I get
Error in h(simpleError(msg, call)) : error in evaluating the argument 'what' in selecting a method for function 'do.call': no method found for function 'c' and signature GenomicRangesWhen I run this
That's because there's no
c()method defined for GenomicRanges objects (maybe there was one 6 years ago when richierocks posted their comment):However there's one defined for Vector objects:
And because the method dispatch mechanism in R uses inheritance to find the appropriate method, there's no need to use
getMethod()orselectMethod():You should in fact _never_ use
getMethod()orselectMethod()in your code. Just call the generic function (c()in this case) and let the method dispatch mechanism find the right method for you.This is with GenomicRanges 1.46.1 which is the most current version of GenomicRanges in BioC 3.14.
Anyways, why are you trying to do this when Martin Morgan provided the best way to do this below? Using
unlist()is _the_ recommended way and is much more efficient thando.call(c, ...).H.