Question: What is the proper way to modify mcols of a GRanges inside a GRangesList?
gravatar for Ryan C. Thompson
19 months ago by
The Scripps Research Institute, La Jolla, CA
Ryan C. Thompson7.1k wrote:

Sometimes I have a GRangeList object and I want to modify the mcols of the underlying GRanges object. The only way I know to do this is to do something like:

grl@unlistData$newColumn <- newValue

But that unfortunately breaks the GRangesList abstraction by directly accessing the slot. Is there any way to do this without resorting to slot access?

(To be clear, I'm aware that the GRangesList also has its own separate set of mcols, and those are not what I'm trying to access.)

ADD COMMENTlink modified 19 months ago by Michael Lawrence10k • written 19 months ago by Ryan C. Thompson7.1k
gravatar for Michael Lawrence
19 months ago by
United States
Michael Lawrence10k wrote:

This should work (as long as the column is not actually "new"):

grl[,"newColumn"] <- newValue

Someone should probably document that (I only found it by reading the code). But maybe it's better to do this:

mcols(grl, level="within")$newColumn <- newValue

Because it's more explicit, can add new columns, and is consistent with extracting a column:

mcols(grl, level="within")$newColumn

I'm wondering whether it would make sense to support this syntax for extraction:


It shares the problem with the first example, in that it's not clear whether "newColumn" is coming from the inside or outside. But in interactive code, those concerns are often secondary to convenience... the biggest practical problem is that there is no way to tell it to add an inner column. I guess there could be a heuristic based on the value being a list of the right shape. Opinions?

ADD COMMENTlink written 19 months ago by Michael Lawrence10k

The level="within" trick is exactly what I was looking for. I don't know if we really need a shortcut for this pattern, since it doesn't come up that often. As long as there's a way to do it at all without breaking abstraction, I'm satisfied.

ADD REPLYlink written 19 months ago by Ryan C. Thompson7.1k

As of today, I do not manage to reproduce the examples above.  After running example("GRangesList") to create a grl object with "score" and "GC" mcols, here is what I get:

> grl[,"newColumn"] <- "new value"
Error in `[<-`(`*tmp*`, , "newColumn", value = "new value") :
  replacement value must be a GRangesList object

> grl[,"GC"]
GRangesList object of length 3:
GRanges object with 1 range and 1 metadata column:
      seqnames    ranges strand |        GC
         <Rle> <IRanges>  <Rle> | <numeric>
  [1]   Chrom2    [3, 6]      + |      0.45


> mcols(grl, level="within")$GC

> sessionInfo()
R version 3.4.3 (2017-11-30)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 9 (stretch)

Matrix products: default
BLAS: /usr/lib/libblas/
LAPACK: /usr/lib/lapack/

 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C               LC_TIME=en_GB.UTF-8       
 [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                  LC_ADDRESS=C              

attached base packages:
[1] parallel  stats4    stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] GenomicRanges_1.31.3 GenomeInfoDb_1.14.0  IRanges_2.13.4       S4Vectors_0.17.12   
[5] BiocGenerics_0.24.0

loaded via a namespace (and not attached):
[1] zlibbioc_1.24.0         compiler_3.4.3          XVector_0.19.1          tools_3.4.3            
[5] GenomeInfoDbData_0.99.1 RCurl_1.95-4.8          yaml_2.1.15             bitops_1.0-6  
ADD REPLYlink written 11 months ago by Charles Plessy90
Please log in to add an answer.


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