1 Introduction

By design, the scope of this package is limited to defining the SingleCellExperiment class and some minimal getter and setter methods. For this reason, we leave it to developers of specialized packages to provide more advanced methods for the SingleCellExperiment class. For example, scater defines a number of specific methods such as normalize and dplyr-like verbs. We also leave it to the developers of existing packages to provide coercion methods from their classes to SingleCellExperiment.

The use of SingleCellExperiment objects inside package functions is mostly the same as the use of instances of the base SummarizedExperiment class. The only exceptions involve direct access to the internal fields of the SingleCellExperiment definition. Manipulation of these internal fields in other packages is possible but requires some caution, as we shall discuss below.

2 Using the internal fields

2.1 Rationale

We use an internal storage mechanism to protect the spike-in and size factor fields from direct manipulation by the user. This ensures that, e.g., only a call to sizeFactors<- can change the size factors. The same effect could be achieved by reserving a subset of columns (or column names) as “private” in colData() and rowData(), though this is not easily implemented.

The internal storage avoids situations where users or functions can silently overwrite these important metadata fields during manipulations of rowData or colData. This can result in bugs that are difficult to track down, particularly in long workflows involving many functions. It also allows us to add new methods and metadata types to SingleCellExperiment without worrying about overwriting user-supplied metadata in existing objects.

Methods to get or set the internal fields are exported for use by developers of packages that depend on SingleCellExperiment. This allows dependent packages to store their own custom fields that are not meant to be directly accessible by the user. However, this requires some care to avoid conflicts between packages.

2.2 Conflicts between packages

The concern is that package A and B both define methods that get/set an internal field X in a SingleCellExperiment instance. Consider the following example object:

counts <- matrix(rpois(100, lambda = 10), ncol=10, nrow=10)
sce <- SingleCellExperiment(assays = list(counts = counts))
## class: SingleCellExperiment 
## dim: 10 10 
## metadata(0):
## assays(1): counts
## rownames: NULL
## rowData names(0):
## colnames: NULL
## colData names(0):
## reducedDimNames(0):
## spikeNames(0):

Assume that we have functions that set an internal field X in packages A and B.

# Function in package A:
AsetX <- function(sce) {
    int_colData(sce)$X <- runif(ncol(sce))

# Function in package B:
BsetX <- function(sce) {
    int_colData(sce)$X <- sample(LETTERS, ncol(sce), replace=TRUE)

If both of these functions are called, one will clobber the output of the other. This may lead to nonsensical results in downstream procedures.

sce2 <- AsetX(sce)
##  [1] 0.94167987 0.49162278 0.92048480 0.06565014 0.19818330 0.08893972
##  [7] 0.23583047 0.21399852 0.61864668 0.94686724
sce2 <- BsetX(sce2)
##  [1] "H" "L" "M" "Y" "A" "P" "I" "C" "L" "R"

2.3 Using “Inception-style” nesting

We recommend using nested DataFrames to store internal fields in the column-level metadata. The name of the nested element should be set to the package name, thus avoiding clashes between fields with the same name from different packages.

AsetX_better <- function(sce) {
    int_colData(sce)$A <- DataFrame(X=runif(ncol(sce)))

BsetX_better <- function(sce) {
    choice <- sample(LETTERS, ncol(sce), replace=TRUE)
    int_colData(sce)$B <- DataFrame(X=choice)

sce2 <- AsetX_better(sce)
sce2 <- BsetX_better(sce2)
##  [1] 0.60766257 0.03990473 0.05046718 0.53451318 0.41778314 0.14232815
##  [7] 0.28916514 0.43064611 0.36698068 0.40355397
##  [1] "G" "U" "G" "Q" "E" "W" "Y" "O" "E" "S"

The same approach can be applied to the row-level metadata, e.g., for some per-row field Y.

AsetY_better <- function(sce) {
    int_elementMetadata(sce)$A <- DataFrame(Y=runif(nrow(sce)))

BsetY_better <- function(sce) {
    choice <- sample(LETTERS, nrow(sce), replace=TRUE)
    int_elementMetadata(sce)$B <- DataFrame(Y=choice)

sce2 <- AsetY_better(sce)
sce2 <- BsetY_better(sce2)
##  [1] 0.04795335 0.28775565 0.92400703 0.57103457 0.44817363 0.92048536
##  [7] 0.20006383 0.73969169 0.10147236 0.70968649
##  [1] "X" "F" "K" "O" "T" "M" "W" "W" "T" "S"

For the object-wide metadata, a nested list is usually sufficient.

AsetZ_better <- function(sce) {
    int_metadata(sce)$A <- list(Z = "Aaron")

BsetZ_better <- function(sce) {
    int_metadata(sce)$B <- list(Z = "Davide")

sce2 <- AsetZ_better(sce)
sce2 <- BsetZ_better(sce2)
## [1] "Aaron"
## [1] "Davide"

In this manner, both A and B can set their internal X, Y and Z without interfering with each other. Of course, this strategy assumes that packages do not have the same names as some of the in-built internal fields (which would be very unfortunate).

3 Contacting us

If your package accesses the internal fields of the SingleCellExperiment class, we suggest you get into contact with us on GitHub. This will help us in planning changes to the internal organization of the class. It will also allow us to contact you with respect to changes or to get feedback.

We are particularly interested in scenarios where multiple packages are defining internal fields with the same scientific meaning. In such cases, it may be valuable to provide getters and setters for this field in SingleCellExperiment directly. This reduces redundancy in the definitions across packages and promotes interoperability. For example, methods from one package can set the field, which can then be used by methods of another package.

4 Other design decisions

4.1 What’s up with reducedDims?

We use a SimpleList as the reducedDims slot to allow for multiple dimensionality reduction results. One can imagine that different dimensionality reduction techniques will be useful for different aspects of the analysis, e.g., t-SNE for visualization, PCA for pseudo-time inference. We see reducedDims as a similar slot to assays() in that multiple matrices can be stored, though the dimensionality reduction results need not have the same number of dimensions.

4.2 Why derive from a RangedSummarizedExperiment?

We decided to extend RangedSummarizedExperiment rather than SummarizedExperiment because for certain assays it will be essential to have rowRanges(). Even for RNA-seq, it is sometimes useful to have rowRanges() and other classes to define the genomic coordinates, e.g., DESeqDataSet in the DESeq2 package. An alternative would have been to have two classes, SingleCellExperiment and RangedSingleCellExperiment. However, this seems like an unnecessary duplication as having a class with default empty rowRanges seems good enough when one does not need rowRanges.

5 Session information

## R Under development (unstable) (2018-11-17 r75624)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 18.04.1 LTS
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.9-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.9-bioc/R/lib/libRlapack.so
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=C              
##  [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            
## attached base packages:
## [1] parallel  stats4    stats     graphics  grDevices utils     datasets 
## [8] methods   base     
## other attached packages:
##  [1] SingleCellExperiment_1.5.2  SummarizedExperiment_1.13.0
##  [3] DelayedArray_0.9.5          BiocParallel_1.17.5        
##  [5] matrixStats_0.54.0          Biobase_2.43.0             
##  [7] GenomicRanges_1.35.1        GenomeInfoDb_1.19.1        
##  [9] IRanges_2.17.4              S4Vectors_0.21.9           
## [11] BiocGenerics_0.29.1         BiocStyle_2.11.0           
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.0             knitr_1.21             XVector_0.23.0        
##  [4] magrittr_1.5           zlibbioc_1.29.0        lattice_0.20-38       
##  [7] stringr_1.3.1          tools_3.6.0            grid_3.6.0            
## [10] xfun_0.4               htmltools_0.3.6        yaml_2.2.0            
## [13] digest_0.6.18          bookdown_0.9           Matrix_1.2-15         
## [16] GenomeInfoDbData_1.2.0 BiocManager_1.30.4     bitops_1.0-6          
## [19] RCurl_1.95-4.11        evaluate_0.12          rmarkdown_1.11        
## [22] stringi_1.2.4          compiler_3.6.0