[Bioc-devel] How to use initializing functions correctly ?

Hervé Pagès hp@ge@ @end|ng |rom |redhutch@org
Tue May 12 19:18:24 CEST 2020


Hi Charles,


On 5/11/20 17:48, Charles Plessy wrote:
> Dear Bioconductor developers,
> 
> I am using some S4 classes in my package (CAGEr) for type saftety and
> functional polymorphism; in particular in some case I simply wrap on
> an existing class, like this:
> 
>      newGPos <- setClass("newGPos", contains = "UnstitchedGPos")
> 
> (For example to express the fact that the GPos is a transcription start
> site.)
> 
> I know I can create wrapped objects like this:
> 
>      newGPos(GPos("chr1:1:+", stitch = FALSE))
> 
> But I was surprised I can not do it like that:
> 
>      newGPos("chr1:1:+", stitch = FALSE)
>      
>      Error in as(as({ :
>        no method or default for coercing “CompressedCharacterList” to “Ranges”

newGPos() is the generator function returned by setClass(). It is a 
simple wrapper to new():

   > newGPos
   class generator function for class “newGPos” from package ‘.GlobalEnv’
   function (...)
   new("newGPos", ...)

so it has no knowledge of the 'stitch' argument, which is an argument 
implemented by the GPos() constructor.

Note that the GPos() constructor is NOT the generator function returned 
by the setClass() statement that defines the GPos class. It's a 
constructor function defined in the GenomicRanges package and it 
implements a sophisticated logic.

> 
> I am struggling to find some documentation explaining how to create a
> constructor for the wrapping class, that would function the same way as
> the constructor of the base class, except of course that it would return
> the object in the wrapping class.

You need to define your own newGPos() constructor function as a wrapper 
to the GPos() constructor. Something like this:

   newGPos <- function(...)
   {
     gpos <- GPos(...)
     new("newGPos", gpos)
   }

HOWEVER, please note that trying to support the 'stitch' argument does 
not make sense in a context where the newGPos class extends the 
UnstitchedGPos class. So you might want to do something like this instead:

## newGPos() has the same arguments as the GPos() constructor **except**
## the 'stitch' argument.
newGPos <- function (seqnames=NULL, pos=NULL, strand=NULL,
                      ..., seqinfo=NULL, seqlengths=NULL)
{
     gpos <- GPos(seqnames=seqnames, pos=pos, strand=strand,
                  ..., seqinfo=seqinfo, seqlengths=seqlengths,
                  stitch=FALSE)
     new("newGPos", gpos)
}

Another advantage of using explicit arguments (instead of the ellipsis) 
is that it's easier for the end user to discover them (tab completion 
works) and easier for you do document them.

Hope this helps,

H.

> 
> Have a nice day,
> 
> Charles Plessy
> 

-- 
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpages using fredhutch.org
Phone:  (206) 667-5791
Fax:    (206) 667-1319



More information about the Bioc-devel mailing list