How to convert a GRangesList object into a GRanges object?
1
4
Entering edit mode
richierocks ▴ 40
@richierocks-9994
Last seen 5.0 years ago

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.

genomicranges grangeslist granges s4 • 6.3k views
ADD COMMENT
0
Entering edit mode

Using do.call and c works, but you have to help R find the correct method method of c to use.

do.call(getMethod(c, "GenomicRanges"), grl)
## GRanges object with 8 ranges and 0 metadata columns:
##       seqnames    ranges strand
##          <Rle> <IRanges>  <Rle>
##   [1]     chr1   [1, 16]      +
##   [2]     chr1   [5, 12]      +
##   [3]     chr1   [8,  9]      -
##   [4]     chr2   [2, 15]      -
##   [5]     chr2   [6, 11]      -
##   [6]     chr3   [3, 14]      +
##   [7]     chr3   [7, 10]      +
##   [8]     chr4   [4, 13]      -
##   -------
##   seqinfo: 4 sequences from an unspecified genome; no seqlengths
ADD REPLY
10
Entering edit mode
@martin-morgan-1513
Last seen 9 weeks ago
United States

Unlist the list

> unlist(grl)
GRanges object with 8 ranges and 0 metadata columns:
       seqnames    ranges strand
          <Rle> <IRanges>  <Rle>
  chr1     chr1   [1, 16]      +
  chr1     chr1   [5, 12]      +
  chr1     chr1   [8,  9]      -
  chr2     chr2   [2, 15]      -
  chr2     chr2   [6, 11]      -
  chr3     chr3   [3, 14]      +
  chr3     chr3   [7, 10]      +
  chr4     chr4   [4, 13]      -
  -------
  seqinfo: 4 sequences from an unspecified genome; no seqlengths

In the S4Vectors world, this unlist operation is very fast, because the 'list' is actually a single instance with a 'partitioning' vector describing how the elements are supposed to be grouped; split() adds the partitioning vector, unlist() removes it. relist() takes the partitioning of the second argument and adds it to the first.

 

ADD COMMENT

Login before adding your answer.

Traffic: 225 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