Question: How to initialize a SummarizedExperiment?
gravatar for Thomas Sandmann
17 months ago by
Thomas Sandmann60 wrote:

Dear Bioconductor users,

I would like to establish a new S4 class, based on the SummarizedExperiment class from the package of the same name.

I am wondering that the correct way of initializing a SummarizedExperiment is. The SummarizedExperiment method accepts e.g. a matrix and constructs the missing colData and rowData objects.

m <- matrix(1:24, ncol = 3, dimnames = list(letters[1:8], letters[9:11]))
se <- SummarizedExperiment(m)

But when I try to obtain the same result using the `new` method, using the following lines of code:

    assays = Assays(SimpleList(counts = m)),
    colData = DataFrame(row.names = colnames(m)),
    rowData = DataFrame(row.names = row.names(m)))
I get this error:
Error in initialize(value, ...) : 
  invalid name for slot of class “SummarizedExperiment”: rowData

To provide some context, I would like to define a simple subclass of SummarizedExperiment. But I don't understand how to create an object of my new class with the  new method, e.g. without using the SummarizedExperiment constructor method - and copying the slots into the new object. (This is just an example, I am aware that similar classes already exist, e.g. in the DESeq2 package.)


# define new class, inheritng from SummarizedExperiment
setClass("CountSet", contains = "SummarizedExperiment", 
         representation(filtered = "logical"), 
         prototype = prototype(
           filtered = FALSE), 
         validity = function(object) {
           if (!("raw" %in% assayNames(object)))
             return( "The assays slot must contain a matrix named 'counts'" )
           if (any( counts(object) < 0 ))
             return( "The count data contains negative values" )

CountSet <- function(counts, colData, rowData, ...) {
  se <- SummarizedExperiment(assays = SimpleList(counts = counts), ...)
  cs <- new("CountSet")
  for(sl in slotNames(se)) {
    slot(cs, sl) <- slot(se, sl)

Any hints?

Thanks a lot,



ADD COMMENTlink modified 17 months ago by Martin Morgan ♦♦ 20k • written 17 months ago by Thomas Sandmann60
gravatar for Martin Morgan
17 months ago by
Martin Morgan ♦♦ 20k
United States
Martin Morgan ♦♦ 20k wrote:

Really a question for the bioc-devel (i.e., software development, rather than use) email list. But `new()` uses unnamed arguments to populate classes that it inherits from. So

> setClass("A", representation(x="numeric"), contains="SummarizedExperiment")
> new("A", SummarizedExperiment(), x=1:5)

Think of the constructor SummarizedExperiment() as part of the class interface that separates what the 'user' sees from the details of the class implementation; it is the right thing to use. I like the use of the simple wrapper returned by setClass()

> .A = setClass("A", representation(x="numeric"), contains="SummarizedExperiment")
> .A(SummarizedExperiment(), x=1:5)

I notice your validity method tests for an object named 'raw' but complains about a missing 'counts'. Also the contract of the validity method is to return a character string describing how the class is invalid, or TRUE; yours does not return TRUE. A typical pattern is

.validity_A = function(object) {
    msg = character()
    if (...)
        msg = c(msg, "some problem")
   if (length(msg)) msg else TRUE
ADD COMMENTlink modified 17 months ago • written 17 months ago by Martin Morgan ♦♦ 20k

Thanks a lot, Martin - both for the great explanation and for pointing out the bioc-devel list.

ADD REPLYlink written 17 months ago by Thomas Sandmann60

Hi Thomas,

FWIW the vignette in the SummarizedExperiment package has a section dedicated to extending the RangedSummarizedExperiment class. It's the last section of the vignette.



ADD REPLYlink written 17 months ago by Hervé Pagès ♦♦ 13k
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.2.0
Traffic: 166 users visited in the last hour