[BioC] Subscripting GenomicRanges objects with [[ or $

Tim Yates tyates at picr.man.ac.uk
Fri Aug 27 16:14:37 CEST 2010


Had a quick look at how IRanges does it, and found I can add this method to
GRanges objects:

setMethod("[[", "GRanges",
          function(x, i, j, ...)
          {
            dotArgs <- list(...)
            if (length(dotArgs) > 0)
                dotArgs <- dotArgs[names(dotArgs) != "exact"]
            if (!missing(j) || length(dotArgs) > 0)
              stop("invalid subsetting")
            if (missing(i))
              stop("subscript is missing")
            if (!is.character(i) && !is.numeric(i))
              stop("invalid subscript type")
            if (length(i) < 1L)
              stop("attempt to select less than one element")
            if (length(i) > 1L)
              stop("attempt to select more than one element")
            if (is.numeric(i) && !is.na(i) && (i < 1L || i > ncol(x)))
              stop("subscript out of bounds")
            if (is.na(i) || (is.character(i) &&
                !(i %in% c("seqnames", "ranges", "strand",
colnames(values(x))))))
              NULL
            else if (i == "seqnames")
              seqnames(x)
            else if (i == "strand")
              strand(x)
            else if (i == "ranges")
              ranges(x)
            else
              values(x)[[i]]
          })

And then the [[ accessing works

Tim

On 27/08/2010 11:03, "tim" <tyates at picr.man.ac.uk> wrote:

> Hi Richard,
> 
> Ahhh..cool, yeah that works. Shame it's not a unified interface across all
> three datatypes though.
> 
> Thanks for pointing me in the right direction though :-)
> 
> Tim
> 
> On 27/08/2010 10:31, "Richard Pearson" <richard.pearson at well.ox.ac.uk>
> wrote:
> 
>> Hi Tim
>> 
>> I think you need the values accessor method here:
>> 
>> print( values(my.gr)[[ 'name' ]] )
>> 
>> Cheers
>> 
>> Richard
>> 
>> 
>> Tim Yates wrote:
>>> Hi all,
>>> 
>>> I'm trying to move to using GRanges objects for storing my genomic features
>>> rather than IRanges objects that I use currently.
>>> 
>>> However, I cannot seem to subscript the Genomic Ranges object to extract a
>>> single column from the meta-data of the object.
>>> 
>>> Hopefully this code explains what I am trying to do, and someone can point
>>> me in the right direction?
>>> 
>>> Cheers,
>>> 
>>> Tim
>>> 
>>>> library(GenomicRanges)
>>> Loading required package: IRanges
>>> 
>>> Attaching package: 'IRanges'
>>> 
>>> 
>>>     The following object(s) are masked from package:base :
>>> 
>>>      cbind,
>>>      Map,
>>>      mapply,
>>>      order,
>>>      paste,
>>>      pmax,
>>>      pmax.int,
>>>      pmin,
>>>      pmin.int,
>>>      rbind,
>>>      rep.int,
>>>      table 
>>> 
>>>> library(GenomicRanges)
>>>> my.starts  = c(     10,    100,   1000 )
>>>> my.ends    = c(     20,    200,   2000 )
>>>> my.spaces  = c(    '1',    '2',    '3' )
>>>> my.strands = c(    '+',    '+',    '-' )
>>>> my.names   = c( 'seq1', 'seq2', 'seq3' )
>>>> my.delta   = c(   1.23,   2.34,   3.45 )
>>>> 
>>>> my.df = data.frame( start=my.starts, end=my.ends, space=my.spaces,
>>> strand=my.strands, name=my.names, delta=my.delta )
>>>> my.rd = as( my.df, 'RangedData' )
>>>> my.gr = as( my.rd, 'GRanges' )
>>>> 
>>> 
>>> # Extract the name field from each of these objects using [[
>>> 
>>>> print( my.df[[ 'name' ]] )
>>> [1] seq1 seq2 seq3
>>> Levels: seq1 seq2 seq3
>>>> print( my.rd[[ 'name' ]] )
>>> [1] seq1 seq2 seq3
>>> Levels: seq1 seq2 seq3
>>>> print( my.gr[[ 'name' ]] )
>>> Error in my.gr[["name"]] : missing '[[' method for Sequence class GRanges
>>> 
>>> # Extract the name field from each of these objects using $
>>> 
>>>> print( my.df$'name' )
>>> [1] seq1 seq2 seq3
>>> Levels: seq1 seq2 seq3
>>>> print( my.rd$'name' )
>>> [1] seq1 seq2 seq3
>>> Levels: seq1 seq2 seq3
>>>> print( my.gr$'name' )
>>> Error in x[[name, exact = FALSE]] :
>>>   missing '[[' method for Sequence class GRanges
>>>> sessionInfo()
>>> R version 2.10.1 (2009-12-14)
>>> x86_64-apple-darwin9.8.0
>>> 
>>> locale:
>>> [1] en_GB.UTF-8/en_GB.UTF-8/C/C/en_GB.UTF-8/en_GB.UTF-8
>>> 
>>> attached base packages:
>>> [1] stats     graphics  grDevices utils     datasets  methods   base
>>> 
>>> other attached packages:
>>> [1] GenomicRanges_1.0.8 IRanges_1.6.15
>>> --------------------------------------------------------
>>> This email is confidential and intended solely for the u...{{dropped:15}}
>>> 
>>> _______________________________________________
>>> Bioconductor mailing list
>>> Bioconductor at stat.math.ethz.ch
>>> https://stat.ethz.ch/mailman/listinfo/bioconductor
>>> Search the archives:
>>> http://news.gmane.org/gmane.science.biology.informatics.conductor
>>> 
> --------------------------------------------------------
> This email is confidential and intended solely for the...{{dropped:22}}



More information about the Bioconductor mailing list