Hi Steve,

Thanks for your explanation. I'm just learning about the S4 class and methods so I suspected I'd missed something. I ran your example on my machine and it returned the same output.

I've now found the real problem in my code but don't understand why is causing inheritance problems for Rle. Basically, there's a line in my class definitions to define a class union, namely: setClassUnion('vectorOrNULL', c("vector", "NULL"). Depending on whether that line is included before I try to construct the GRanges object determines whether the object is successfully created. Can anyone please explain this to me?

Here is a minimal reproducible example:
## This version works as intended
> library(GenomicRanges)
Loading required package: BiocGenerics

Attaching package: ‘BiocGenerics’

The following object(s) are masked from ‘package:stats’:

    xtabs

The following object(s) are masked from ‘package:base’:

    anyDuplicated, cbind, colnames, duplicated, eval, Filter, Find,
    get, intersect, lapply, Map, mapply, mget, order, paste, pmax,
    pmax.int, pmin, pmin.int, Position, rbind, Reduce, rep.int,
    rownames, sapply, setdiff, table, tapply, union, unique

Loading required package: IRanges
> out <- list(chr = rep('chr21', 10), 1:10, start = 1:10, end = 2:11)
> showMethods('Rle')
Function: Rle (package IRanges)
values="missing", lengths="missing"
values="vectorORfactor", lengths="integer"
values="vectorORfactor", lengths="missing"
values="vectorORfactor", lengths="numeric"

> gr <- GRanges(seqnames = out[['chr']], ranges = IRanges(start = out[['start']], end = out[['end']]))
> gr
GRanges with 10 ranges and 0 metadata columns:
       seqnames    ranges strand
          <Rle> <IRanges>  <Rle>
   [1]    chr21  [ 1,  2]      *
   [2]    chr21  [ 2,  3]      *
   [3]    chr21  [ 3,  4]      *
   [4]    chr21  [ 4,  5]      *
   [5]    chr21  [ 5,  6]      *
   [6]    chr21  [ 6,  7]      *
   [7]    chr21  [ 7,  8]      *
   [8]    chr21  [ 8,  9]      *
   [9]    chr21  [ 9, 10]      *
  [10]    chr21  [10, 11]      *
  ---
  seqlengths:
   chr21
      NA
> showMethods('Rle')
Function: Rle (package IRanges)
values="character", lengths="integer"
    (inherited from: values="vectorORfactor", lengths="integer")
values="character", lengths="missing"
    (inherited from: values="vectorORfactor", lengths="missing")
values="factor", lengths="integer"
    (inherited from: values="vectorORfactor", lengths="integer")
values="missing", lengths="missing"
values="vectorORfactor", lengths="integer"
values="vectorORfactor", lengths="missing"
values="vectorORfactor", lengths="numeric"

## We have proper inheritance for Rle

> sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=C                 LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] GenomicRanges_1.10.6 IRanges_1.16.4       BiocGenerics_0.4.0  

loaded via a namespace (and not attached):
[1] parallel_2.15.2 stats4_2.15.2

## But this version does not work as intended
## Firstly, start a fresh R session
> library(GenomicRanges)
Loading required package: BiocGenerics

Attaching package: ‘BiocGenerics’

The following object(s) are masked from ‘package:stats’:

    xtabs

The following object(s) are masked from ‘package:base’:

    anyDuplicated, cbind, colnames, duplicated, eval, Filter, Find,
    get, intersect, lapply, Map, mapply, mget, order, paste, pmax,
    pmax.int, pmin, pmin.int, Position, rbind, Reduce, rep.int,
    rownames, sapply, setdiff, table, tapply, union, unique

Loading required package: IRanges
> setClassUnion("vectorOrNULL", c("vector", "NULL")) ## This line is the culprit 
> out <- list(chr = rep('chr21', 10), 1:10, start = 1:10, end = 2:11)
> showMethods('Rle')
Function: Rle (package IRanges)
values="missing", lengths="missing"
values="vectorORfactor", lengths="integer"
values="vectorORfactor", lengths="missing"
values="vectorORfactor", lengths="numeric"

> gr <- GRanges(seqnames = out[['chr']], ranges = IRanges(start = out[['start']], end = out[['end']]))
Error in function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘Rle’ for signature ‘"character", "missing"’
> showMethods('Rle')
Function: Rle (package IRanges)
values="missing", lengths="missing"
values="vectorORfactor", lengths="integer"
values="vectorORfactor", lengths="missing"
values="vectorORfactor", lengths="numeric"

## Inheritance problems for Rle

> sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=C                 LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] GenomicRanges_1.10.6 IRanges_1.16.4       BiocGenerics_0.4.0  

loaded via a namespace (and not attached):
[1] parallel_2.15.2 stats4_2.15.2 

Cheers,
Pete

On 30/01/2013, at 10:39 PM, Steve Lianoglou wrote:

> Hi,
> 
> On Wed, Jan 30, 2013 at 4:57 AM,  <hickey@wehi.edu.au> wrote:
>> There appear to be different methods available for 'Rle' when loaded via the GenomicRanges package depending on whether a GRanges object has been created. Specifically, prior to a GRanges object being created there are no 'values = character' methods for 'Rle'. This doesn't make sense to me and is causing me problems in code I am developing.
>> 
>> The following code highlights the cause of my confusion:
>> 
>> | > library(GenomicRanges)
>> | Loading required package: BiocGenerics
>> |
>> | Attaching package: ‘BiocGenerics’
>> |
>> | The following object(s) are masked from ‘package:stats’:
>> |
>> |     xtabs
>> |
>> | The following object(s) are masked from ‘package:base’:
>> |
>> |    anyDuplicated, cbind, colnames, duplicated, eval, Filter, Find,
>> |    get, intersect, lapply, Map, mapply, mget, order, paste, pmax,
>> |    pmax.int, pmin, pmin.int, Position, rbind, Reduce, rep.int,
>> |    rownames, sapply, setdiff, table, tapply, union, unique
>> 
>> | Loading required package: IRanges
>> | > showMethods('Rle')
>> | Function: Rle (package IRanges)
>> | values="missing", lengths="missing"
>> | values="vectorORfactor", lengths="integer"
>> | values="vectorORfactor", lengths="missing"
>> | values="vectorORfactor", lengths="numeric"
>> 
>> ## Only 4 methods are available for Rle
>> 
>> | > seqinfo <- Seqinfo(paste0("chr", 1:3), c(1000, 2000, 1500), NA, "mock1")
>> | > gr <- GRanges(seqnames = Rle(c("chr1", "chr2", "chr1", "chr3"), c(1, 3, 2, 4)),
>> | +               ranges = IRanges(1:10, width = 10:1, names = head(letters,10)),
>> | +               strand = Rle(strand(c("-", "+", "*", "+", "-")),c(1, 2, 2, 3, 2)),
>> | +               score = 1:10, GC = seq(1, 0, length=10),
>> | +               seqinfo = seqinfo)
>> | > gr
>> | GRanges with 10 ranges and 2 metadata columns:
>> |     seqnames    ranges strand |     score                GC
>> |        <Rle> <IRanges>  <Rle> | <integer>         <numeric>
>> |   a     chr1  [ 1, 10]      - |         1                 1
>> |   b     chr2  [ 2, 10]      + |         2 0.888888888888889
>> |   c     chr2  [ 3, 10]      + |         3 0.777777777777778
>> |   d     chr2  [ 4, 10]      * |         4 0.666666666666667
>> |   e     chr1  [ 5, 10]      * |         5 0.555555555555556
>> |   f     chr1  [ 6, 10]      + |         6 0.444444444444444
>> |   g     chr3  [ 7, 10]      + |         7 0.333333333333333
>> |   h     chr3  [ 8, 10]      + |         8 0.222222222222222
>> |   i     chr3  [ 9, 10]      - |         9 0.111111111111111
>> |   j     chr3  [10, 10]      - |        10                 0
>> |   ---
>> |   seqlengths:
>> |   chr1 chr2 chr3
>> |   1000 2000 1500
>> | > showMethods('Rle')
>> | Function: Rle (package IRanges)
>> | values="character", lengths="integer"
>> |     (inherited from: values="vectorORfactor", lengths="integer")
>> | values="character", lengths="numeric"
>> |     (inherited from: values="vectorORfactor", lengths="numeric")
>> | values="factor", lengths="integer"
>> |     (inherited from: values="vectorORfactor", lengths="integer")
>> | values="factor", lengths="numeric"
>> |     (inherited from: values="vectorORfactor", lengths="numeric")
>> | values="missing", lengths="missing"
>> | values="vectorORfactor", lengths="integer"
>> | values="vectorORfactor", lengths="missing"
>> | values="vectorORfactor", lengths="numeric"
>> 
>> ## Now, there are 8 methods available for Rle
>> 
>> Is this a bug or am I missing something? If I'm just missing something, can someone please explain how I can ensure that the methods involving 'values = character' are available to me upon loading of the GenomicRanges package?
> 
> It's not a bug, it just means that Rle was used on a character vector,
> which doesn't have it's own signature and you are being told that it
> used the one defined from values="vectorOrFactor", as it is the one
> most closely related to the inputs that have been provided given the
> functions already defined and the class hierarchy.
> 
> For instance, let's get on the same initial page:
> 
> R> library(GenomicRanges)
> R> showMethods("Rle")
> Function: Rle (package IRanges)
> values="missing", lengths="missing"
> values="vectorORfactor", lengths="integer"
> values="vectorORfactor", lengths="missing"
> values="vectorORfactor", lengths="numeric"
> 
> Ok -- now I try to create an Rle from a character vector:
> 
> R> set.seed(123)
> R> x <- Rle(sample(letters[1:5], 100, replace=TRUE))
> R> showMethods("Rle")
> Function: Rle (package IRanges)
> values="character", lengths="integer"
>    (inherited from: values="vectorORfactor", lengths="integer")
> values="character", lengths="missing"
>    (inherited from: values="vectorORfactor", lengths="missing")
> values="missing", lengths="missing"
> values="vectorORfactor", lengths="integer"
> values="vectorORfactor", lengths="missing"
> values="vectorORfactor", lengths="numeric"
> 
> 
> 
> Looks like the initial call to Rle(x) triggers the
> c(values="character", lengths="missing") "version" of the function.
> There is no "direct/specific" implementation of this function, so R
> grabs the next closest thing (inherited from
> c(values="vectorORfactor", values="missing")). That function likely
> internally will call a version of the function with a signature like
> so c(values="character", lengths="integer"), which R wants to tell you
> has no direct implementation, and is using makes the second "inherited
> version" defined w/ vectorOrFactor and integer inputs.
> 
> The question is -- what makes you think there is no version of Rle
> that accepts just a character vector as its first argument when you
> load GenomicRanges from the get go? Does the above example not work
> for you in a clean R session?
> 
> I'm guessing something else is going wrong with your code, but we'll
> need some sort of minimal reproducible example to help sort that out.
> 
> HTH,
> -steve
> 
> -- 
> Steve Lianoglou
> Graduate Student: Computational Systems Biology
> | Memorial Sloan-Kettering Cancer Center
> | Weill Medical College of Cornell University
> Contact Info: http://cbio.mskcc.org/~lianos/contact

--------------------------------
Peter Hickey,
PhD Student/Research Assistant,
Bioinformatics Division,
Walter and Eliza Hall Institute of Medical Research,
1G Royal Parade, Parkville, Vic 3052, Australia.
Ph: +613 9345 2324

hickey@wehi.edu.au
http://www.wehi.edu.au


______________________________________________________________________
The information in this email is confidential and intend...{{dropped:8}}

