[Bioc-devel] error when combining objects inheriting from GRangesList

Hervé Pagès hpages at fredhutch.org
Fri Dec 4 19:41:46 CET 2015


Hi Leonard,

On 12/04/2015 09:43 AM, Leonard Goldstein wrote:
> Hi all,
>
> In the latest Bioc release (and devel) I encountered problems with
> classes inheriting from GRangesList. Combining two or more objects
> that belong to such classes can result in an error, as the combined
> object is apparently not recognized as a valid instance of the class.
> I included an example below. Thanks in advance for your help.
>
> Leonard
>
> --
>> library(GenomicRanges)
>>
>> validNewClass <- function(object) {
> +
> +   if (!"ID" %in% names(mcols(object))) {
> +
> +     return("missing metadata column ID")
> +
> +   }
> +
> + }
>>
>> setClass(
> +   Class = "newClass",
> +   contains = "GRangesList",
> +   validity = validNewClass)
>>
>> gr <- GRanges(1, IRanges(1, 100))
>> grl <- split(gr, 1)
>> mcols(grl)$ID <- 1
>> x <- new("newClass", grl)
>>
>> ## combining two instances of newClass results in an error
>> c(x, x)
> Error in validObject(.Object) :
>    invalid class “newClass” object: missing metadata column ID

A lot of code in the S4Vectors/IRanges/GenomicRanges infrastructure
needs to create temporarily invalid objects for various reasons. At
the lowest level we use S4Vectors::new2(..., check=FALSE) for that.
Unlike methods::new(), which always validates the new object, new2()
only validates it if 'check' is set to TRUE.
However new2() is only able to to this if the validity method for the
object was defined using setValidity2(), also defined in the S4Vectors
package. If you build on top of the S4Vectors/IRanges/GenomicRanges
infrastructure, please always use setValidity2():

   library(GenomicRanges)

   setClass("newClass", "GRangesList")

   setValidity2("newClass",
     function(object) {
       if (!("ID" %in% names(mcols(object))))
         return("missing metadata column ID")
       NULL
     }
   )

   gr <- GRanges("1:1-100")
   grl <- split(gr, 1)
   mcols(grl)$ID <- 1
   x <- new("newClass", grl)

Then:

   > c(x, x)
   newClass object of length 2:
   $1
   GRanges object with 1 range and 0 metadata columns:
         seqnames    ranges strand
            <Rle> <IRanges>  <Rle>
     [1]     1  [   1, 100]      *

   $1
   GRanges object with 1 range and 0 metadata columns:
         seqnames    ranges strand
     [1]        1 [ 1, 100]      *

   -------
   seqinfo: 1 sequence from an unspecified genome; no seqlengths

>>
>> ## but can create an instance after combining as GRangesLists
>> new("newClass", c(as(x, "GRangesList"), as(x, "GRangesList")))
> newClass object of length 2:
> $1
> GRanges object with 1 range and 0 metadata columns:
>        seqnames    ranges strand
>           <Rle> <IRanges>  <Rle>
>    [1]        1  [1, 100]      *
>
> $1
> GRanges object with 1 range and 0 metadata columns:
>        seqnames   ranges strand
>    [1]        1 [1, 100]      *
>
> -------
> seqinfo: 1 sequence from an unspecified genome; no seqlengths

This works because the validity method for GRangesList objects is set
with setValidity2().

I wish new() had the extra 'check' argument so we wouldn't need to use
the new2/setValidity2 hack.

Hope this helps,
H.

>>
>> sessionInfo()
> R version 3.2.2 (2015-08-14)
> Platform: x86_64-pc-linux-gnu (64-bit)
> Running under: Red Hat Enterprise Linux Server release 6.6 (Santiago)
>
> 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=en_US.UTF-8       LC_NAME=C
>   [9] LC_ADDRESS=C               LC_TELEPHONE=C
> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
>
> attached base packages:
> [1] stats4    parallel  stats     graphics  grDevices utils     datasets
> [8] methods   base
>
> other attached packages:
> [1] GenomicRanges_1.22.1 GenomeInfoDb_1.6.1   IRanges_2.4.4
> [4] S4Vectors_0.8.3      BiocGenerics_0.16.1
>
> loaded via a namespace (and not attached):
> [1] zlibbioc_1.16.0 XVector_0.10.0
>>
>
> _______________________________________________
> Bioc-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/bioc-devel
>

-- 
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 at fredhutch.org
Phone:  (206) 667-5791
Fax:    (206) 667-1319



More information about the Bioc-devel mailing list