Search
Question: How to convert a GRangesList object into a GRanges object?
1
gravatar for richierocks
2.3 years ago by
richierocks10
richierocks10 wrote:

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.

ADD COMMENTlink modified 2.3 years ago • written 2.3 years ago by richierocks10

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 REPLYlink written 2.3 years ago by richierocks10
7
gravatar for Martin Morgan
2.3 years ago by
Martin Morgan ♦♦ 22k
United States
Martin Morgan ♦♦ 22k wrote:

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 COMMENTlink written 2.3 years ago by Martin Morgan ♦♦ 22k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.2.0
Traffic: 123 users visited in the last hour