The [
method is not working as expected for my simple class that extends the SummarizedExperiment
class.
This example shows the unexpected (to me) behaviour.
MyClass
definitionDefine MyClass
, basically a SummarizedExperiment
with an extraSlot
, which is a numeric vector of the same length as the number of rows in the rowData
:
require(GenomicRanges) .MyClass <- setClass("MyClass", representation(extraSlot = "vector"), contains = "SummarizedExperiment")
MyClass
constructorMyClass <- function(assays, gr, extraSlot){ se <- SummarizedExperiment(assays = assays, rowData = gr) mc <- .MyClass(se, extraSlot = extraSlot) mc }
[
method definition for MyClass
Define the [
method for MyClass
objects. Should basically do the same thing as [
for a SummarizedExperiment
but also subset the extraSlot
:
setMethod("[", c("MyClass", "ANY", "missing"), function(x, i, j, ..., drop = FALSE) { if (missing(i) && missing(j)){ return(x) } if (missing(i)){ i <- seq_len(nrow(x)) } if (missing(j)){ j <- seq_len(ncol(x)) } initialize(x, as(x, "SummarizedExperiment")[i, j, ..., drop = drop], extraSlot = x@extraSlot[i]) })
nrows <- 20 ncols <- 6 counts <- matrix(runif(nrows * ncols, 1, 1e4), nrows) colnames(counts) <- letters[1:6] gr <- GRanges('chr1', IRanges(floor(runif(nrows, 1e5, 1e6)), width=100)) extraSlot <- runif(n = length(gr)) ex <- MyClass(assays = SimpleList(counts), gr = gr, extraSlot = extraSlot) ex slotNames(ex) ex@extraSlot
[
method for MyClass
objectsAs expected, the following contains 10 elements:
ex[1:10, ]@extraSlot
As unexpected, the following contains 20 elements. Why?
ex[1:10, 2]@extraSlot
As unexpected, the following contains 10 elements. Why? This is the exact same code as the final line of the [
method definition:
initialize(ex, as(ex, "SummarizedExperiment")[1:10, 2, drop = FALSE], extraSlot = ex@extraSlot[1:10])@extraSlot
I never got to the bottom of this. I didn't post a question to the Bioconductor mailing lists. MyClass
was a simplified version of an earlier incarnation the CoMeth
class. When I changed the definition of the CoMeth
class[^cometh_class_definition], I got the intended subsetting behaviour of the [
method "for free" via inheritance. Therefore, this was no longer a problem for the cometh
package, although it would be good to understand the behaviour in the example.
[^cometh_class_definition]: The CoMeth
class definition changed to use the MTuples
class, which extends the GRanges
class, as the rowData
, rather than the GRanges
class itself. Effectively, the extraPos
information is stored in the MTuples
object and preserved in subsetting by use of the GenomicRanges:::extraColumnSlotNames
function (see class definition of MTuples
in R/AllClasses.R
and the vignette Creating MTuples and CoMeth classes).
sessionInfo()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.