1

I have a GRANGES object similar to below:

GRanges object with 3 ranges and 2 metadata columns:
      seqnames    ranges strand |   txid txname
         <Rle> <IRanges>  <Rle> | <list> <list>
  [1]     chr1       1-3      + |      1      a
  [2]     chr1       3-5      - |      2      b
  [3]     chr1       5-7      + |    1,2    a,b
  -------
  seqinfo: 1 sequence from an unspecified genome; no seqlengths

which can be created using

gr <- GRanges(seqnames = "chr1", strand = c("+", "-", "+"),
              ranges = IRanges(start = c(1,3,5), width = 3)
              )
mcols(gr)$txid <- list(1, 2, list(1, 2))
mcols(gr)$txname <- list("a", "b", list("a", "b"))

and I would like to expand the object so each txid/txname is on a new row, such that the result looks like this:

      seqnames    ranges strand |      txid txname
         <Rle> <IRanges>  <Rle> | <numeric> <character>
  [1]     chr1       1-3      + |         1      a
  [2]     chr1       3-5      - |         2      b
  [3]     chr1       5-7      + |         1      a
  [4]     chr1       5-7      + |         2      b

I have tried using S4Vectors::expand() but cannot achieve what I am after. S4Vectors::expand(gr) expands without consideration for the order of elements in the list. e.g. txid 1 and txname "a" should be on the same row as they are the first elements in their respective lists, and the same goes for txid 2 and txname "b". The results of S4Vectors::expand(gr) look like this:


      seqnames    ranges strand |      txid      txname
         <Rle> <IRanges>  <Rle> | <numeric> <character>
  [1]     chr1       1-3      + |         1           a
  [2]     chr1       3-5      - |         2           b
  [3]     chr1       5-7      + |         1           a
  [4]     chr1       5-7      + |         1           b
  [5]     chr1       5-7      + |         2           a
  [6]     chr1       5-7      + |         2           b

Expanding just one of the two columns (i.e. S4Vectors::expand(gr, "txid")) works as intended but is not what I'm after:

      seqnames    ranges strand |      txid txname
         <Rle> <IRanges>  <Rle> | <numeric> <list>
  [1]     chr1       1-3      + |         1      a
  [2]     chr1       3-5      - |         2      b
  [3]     chr1       5-7      + |         1    a,b
  [4]     chr1       5-7      + |         2    a,b

I feel like I'm close but cannot figure this out on my own so any pointers are welcome. Thank you.

0 Answers0