How to initialize a SummarizedExperiment?
1
1
Entering edit mode
@thomas-sandmann-6817
Last seen 15 months ago
USA

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)
colData(se)
rowData(se)

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

new("SummarizedExperiment",
    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.)

library(SummarizedExperiment)

# 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)
  }
  return(cs)
}

Any hints?

Thanks a lot,

Thomas

 

summarizedexperiment SummarizedExperiment • 2.0k views
ADD COMMENT
2
Entering edit mode
@martin-morgan-1513
Last seen 4 months ago
United States

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 COMMENT
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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.

Cheers,

H.

ADD REPLY

Login before adding your answer.

Traffic: 848 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6