[Bioc-devel] How to define S4 method for class from another package

Martin Morgan mtmorg@n@b|oc @end|ng |rom gm@||@com
Wed Apr 3 17:19:52 CEST 2019

It's a little hard to follow the details of the conversation on your submission, but maybe I find myself disagreeing with the reviewer __ 

In some ways one would like to think 'I can find out everything I need to know about DNAStringSet by looking in Biostrings', but as your use case illustrates there are things that people want to do that are not defined in Biostrings. It would be wrong to create a new class MyDNAStringSet that has no new data representation just new methods, and it would be wrong to create an entirely new class with just your methods, and it would be wrong to say 'well, if you want to calculate the GC distribution of a DNAStringSet, just copy and paste these lines of code...', and it would be wrong (at least in general) to add all kinds of idiosyncratic code to Biostrings 'on request' of other package authors. So the only thing left is to write new methods in your package that use data structures from other packages.

On the technical side of things, if the existing class and methods are as strongly S4 based as DNAStringSet, I would not implement S3 methods. So

setGeneric("getGcDistn", function(x) standardGeneric("getGcDistn"))

setMethod("getGcDistn", "ANY", function(x) { ... })

setMethod("getGcDistn", "character", function(x) { ... })

setMethod("getGcDistn", "DNAStringSet", function(x) { ... })

S3 methods are relevant when there are S3 generics or S3 classes ('character' is a base type) that one wants to handle, so not relevant in the current circumstance; `sort()` and the GenomicRanges package would be an example of this. `sort()` is an S3 generic in base R, but is also an S4 generic in BiocGenerics. So we have

> library(GenomicRanges)
> methods(sort)
 [1] sort,ANY-method               sort,DataTable-method
 [3] sort,GenomicRanges-method     sort,GRangesList-method
 [5] sort,List-method              sort,Rle-method
 [7] sort,SortedByQueryHits-method sort,Vector-method
 [9] sort.bibentry*                sort.default
[11] sort.GenomicRanges            sort.GRangesList
[13] sort.POSIXlt                  sort.Vector
see '?methods' for accessing help and source code

with both S3 and S4 methods for, e.g., GenomicRanges and GRangesList.


On 4/3/19, 10:17 AM, "Stephen Martin Pederson" <stephen.pederson using adelaide.edu.au> wrote:

    Hi Martin,
    Thanks for the help. Everything was working, despite missing methods for signature = "ANY" (which is now fixed).
    It was just the
     feedback given when submitting & when I sought clarification to ease my confusion, that was confirmed as the interpretation. I've just re-implemented that particular one using S3 dispatch using the functions getGcDistn.default, getGcDistn.character, getGcDistn.DNAStringSet,
     based on a modification of your reply here: https://stackoverflow.com/questions/12100856/combining-s4-and-s3-methods-in-a-single-function.
    The original discussion which made me think it's not permissable is here: https://github.com/Bioconductor/Contributions/issues/985#issuecomment-468003572
    Thanks again,
    From: Martin Morgan <mtmorgan.bioc using gmail.com>
    Sent: Thursday, 4 April 2019 12:18 AM
    To: Stephen Martin Pederson; bioc-devel using r-project.org
    Subject: Re: [Bioc-devel] How to define S4 method for class from another package 
    Lots of package do this so probably you're missing a step, e.g., import(Biostrings) or importClassesFrom(Biostrings, DNAStringSet).
    What makes you think it's not permissible?
    On 4/3/19, 1:19 AM, "Bioc-devel on behalf of Stephen Martin Pederson" <bioc-devel-bounces using r-project.org on behalf of stephen.pederson using adelaide.edu.au> wrote:
        I'm really struggling with how to use an S4 class defined in another package, where I would like to define a method for it in my package. By way of simple example, I'd like to use a DNAStringSet to define an expected GC distribution based on a given fragment
     size. I had defined the method
        setMethod("getGcDistn", "DNAStringSet", function(myArgs){myBody})
        after using setGeneric() to define the generic getGcDistn.
        When submitting to BioC, I was advised that this is not permissable & I'm trying to figure out what is the best etiquette & best practice. I haven't really turned up much guidance through numerous searches. Do I just write a standard function which checks
     the class and executes the code as required without using S4 dispatch?
        (Disclaimer: I'm very easily confused by the simplest things)
        Thanks in advance,
             [[alternative HTML version deleted]]
        Bioc-devel using r-project.org mailing list

More information about the Bioc-devel mailing list