What is the proper way to modify mcols of a GRanges inside a GRangesList?
1
1
Entering edit mode
@ryan-c-thompson-5618
Last seen 8 months ago
Scripps Research, La Jolla, CA

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.)

granges metadata • 4.7k views
ADD COMMENT
3
Entering edit mode
@michael-lawrence-3846
Last seen 2.4 years ago
United States

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:

grl[[,"newColumn"]]

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 COMMENT
0
Entering edit mode

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 REPLY
0
Entering edit mode

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:
$gr1
GRanges object with 1 range and 1 metadata column:
      seqnames    ranges strand |        GC
         <Rle> <IRanges>  <Rle> | <numeric>
  [1]   Chrom2    [3, 6]      + |      0.45

$gr2
(etc.)

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

> 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/libblas.so.3.7.0
LAPACK: /usr/lib/lapack/liblapack.so.3.7.0

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C               LC_TIME=en_GB.UTF-8       
 [4] LC_COLLATE=en_GB.UTF-8     LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
 [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=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 REPLY

Login before adding your answer.

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