Contents

This document gives an introduction to and overview of the functionality of the scater package.

The scater package is contains tools to help with the analysis of single-cell transcriptomic data, with the focus on RNA-seq data. The package features:

Future versions of scater may also incorporate:

To get up and running as quickly as possible, see the Quick Start section below. For see the various in-depth sections on various aspects of the functionality that follow.

1 Quick Start

Assuming you have a matrix containing expression count data summarised at the level of some features (gene, exon, region, etc.), then we first need to form an SCESet object containing the data. An SCESet object is the basic data container that we use in scater.

Here we use the example data provided with the package, which gives us two objects, a matrix of counts and a dataframe with information about the cells we are studying:

library(scater, quietly = TRUE)
data("sc_example_counts")
data("sc_example_cell_info")

We use these objects to form an SCESet object containing all of the necessary information for our analysis:

pd <- new("AnnotatedDataFrame", data = sc_example_cell_info)
rownames(pd) <- pd$Cell
example_sceset <- newSCESet(countData = sc_example_counts, phenoData = pd)

Subsetting is very convenient with this class. For example, we can filter out features (genes) that are not expressed in any cells:

keep_feature <- rowSums(exprs(example_sceset) > 0) > 0
example_sceset <- example_sceset[keep_feature,]

Now we have the expression data neatly stored in a structure that can be used for lots of exciting analyses.

It is straight-forward to compute many quality control metrics:

example_sceset <- calculateQCMetrics(example_sceset, feature_controls = 1:40)

Now you can play around with your data using the graphical user interface (GUI), which opens an interactive dashboard in your browser!

scater_gui(example_sceset)

Many plotting functions are available for visualising the data:

More detail on all plotting methods is given throughout the vignette below.

Visualisations can highlight features and cells to be filtered out, which can be done easily with the subsetting capabilities of scater.

The QC plotting functions also enable the identification of important experimental variables, which can be conditioned out in the data normalisation step.

After QC and data normalisation (methods are available in scater), the dataset is ready for downstream statistical modeling.

2 A note on terminology

The capabilities of scater are built on top of Bioconductor’s Biobase, which ties us to some specific terminology. Some of this terminology may be unfamiliar or seem peculiar for the single-cell RNA-seq setting scater is designed for.

In Bioconductor terminology we assay numerous “features” for a number of “samples”.

Features, in the context of scater, correspond most commonly to genes or transcripts, but could be any general genomic or transcriptomic regions (e.g. exon) of interest for which we take measurements. Thus, a command such as featureNames, returns the names of the features defined for an SCESet object, which in typical scater usage could correspond to gene IDs. In much of what follows, it may be more intuitive to mentally replace “feature” with “gene” or “transcript” (depending on the context of the study) wherever “feature” appears.

In the scater context, “samples” refer to individual cells that we have assayed. This differs from common usage of “sample” in other contexts, where we might usually use “sample” to refer to an individual subject, a biological replicate or similar. A “sample” in this sense in scater may be referred to as a “block” in the more classical statistical sense. Within a “block” (e.g. individual) we may have assayed numerous cells. Thus, the function sampleNames, when applied to an SCESet object returns the cell IDs.

3 The SCESet class and methods

In scater we organise single-cell expression data in objects of the SCESet class. The class inherits the Bioconductor ExpressionSet class, which provides a common interface familiar to those who have analyzed microarray experiments with Bioconductor. The class requires three input files:

  1. exprs, a numeric matrix of expression values, where rows are features, and columns are cells
  2. phenoData, an AnnotatedDataFrame object, where rows are cells, and columns are cell attributes (such as cell type, culture condition, day captured, etc.)
  3. featureData, an AnnotatedDataFrame object, where rows are features (e.g. genes), and columns are feature attributes, such as biotype, gc content, etc.

For more details about other features inherited from Bioconductor’s ExpressionSet class, type ?ExpressionSet at the R prompt.

The requirements for the SCESet class (as with other S4 classes in R and Bioconductor) are strict. The idea is that strictness with generating a valid class object ensures that downstream methods applied to the class will work reliably. Thus, the expression value matrix must have the same number of columns as the phenoData object has rows, and it must have the same number of rows as the featureData dataframe has rows. Row names of the phenoData object need to match the column names of the expression matrix. Row names of the featureData object need to match row names of the expression matrix.

You can create a new SCESet object using count data as follows. In this case, the exprs slot in the object will be generated as log2(counts-per-million) using the cpm function from edgeR, with a “prior count” value of 1:

pd <- new("AnnotatedDataFrame", data = sc_example_cell_info)
rownames(pd) <- pd$Cell
gene_df <- data.frame(Gene = rownames(sc_example_counts))
rownames(gene_df) <- gene_df$Gene
fd <- new("AnnotatedDataFrame", data = gene_df)
example_sceset <- newSCESet(countData = sc_example_counts, phenoData = pd,
                            featureData = fd)
example_sceset
## SCESet (storageMode: lockedEnvironment)
## assayData: 2000 features, 40 samples 
##   element names: counts, exprs 
## protocolData: none
## phenoData
##   sampleNames: Cell_001 Cell_002 ... Cell_040 (40 total)
##   varLabels: Cell Mutation_Status Cell_Cycle Treatment
##   varMetadata: labelDescription
## featureData
##   featureNames: Gene_0001 Gene_0002 ... Gene_2000 (2000 total)
##   fvarLabels: Gene
##   fvarMetadata: labelDescription
## experimentData: use 'experimentData(object)'
## Annotation:

We can also make an SCESet object with just a matrix of expression values (no count data required; here we compute counts-per-million from the example_sceset defined above and transform to the log2 scale) like this:

example2 <- newSCESet(exprsData = log2(calculateCPM(example_sceset) + 1))
## Warning in .local(object, ...): 'sizeFactors' have not been set
## Warning in calculateCPM(example_sceset): size factors requested but not
## specified, using library sizes instead
## Warning in newSCESet(exprsData = log2(calculateCPM(example_sceset) + 1)):
## 'logExprsOffset' should be set manually for non-count data
## Warning in newSCESet(exprsData = log2(calculateCPM(example_sceset) + 1)):
## 'lowerDetectionLimit' should be set manually for log-expression values
pData(example2)
## data frame with 0 columns and 40 rows
fData(example2)
## data frame with 0 columns and 2000 rows

We have accessor functions to access elements of the SCESet object:

counts(example2)[1:3, 1:6]
## NULL
exprs(example2)[1:3, 1:6]
##           Cell_001 Cell_002 Cell_003 Cell_004 Cell_005 Cell_006
## Gene_0001   0.0000 9.551765 2.918629  0.00000  0.00000 0.000000
## Gene_0002  10.3943 8.633332 3.438547 12.44072 11.46530 9.852595
## Gene_0003   0.0000 0.000000 0.000000  0.00000 10.53582 0.000000
get_exprs(example_sceset, "counts")[1:3, 1:6]
##           Cell_001 Cell_002 Cell_003 Cell_004 Cell_005 Cell_006
## Gene_0001        0      123        2        0        0        0
## Gene_0002      575       65        3     1561     2311      160
## Gene_0003        0        0        0        0     1213        0

Similarly we can assign a new (say, transformed) expression matrix to an SCESet object using set_exprs as follows:

set_exprs(example2, "counts") <- counts(example_sceset)

If you later find some count data that you want to add to the SCESet object, then you can add it easily with the counts assignment function directly (and the same goes for exprs, tpm, cpm, fpkm and versions of these with the prefix “norm_”):

counts(example2) <- sc_example_counts
example2
## SCESet (storageMode: lockedEnvironment)
## assayData: 2000 features, 40 samples 
##   element names: counts, exprs 
## protocolData: none
## phenoData: none
## featureData: none
## experimentData: use 'experimentData(object)'
## Annotation:
counts(example2)[1:3, 1:6]
##           Cell_001 Cell_002 Cell_003 Cell_004 Cell_005 Cell_006
## Gene_0001        0      123        2        0        0        0
## Gene_0002      575       65        3     1561     2311      160
## Gene_0003        0        0        0        0     1213        0

Handily, it is also easy to replace other data in slots of the SCESet object using generic accessor and replacement functions.

gene_df <- data.frame(Gene = rownames(sc_example_counts))
rownames(gene_df) <- gene_df$Gene
fd <- new("AnnotatedDataFrame", data = gene_df)
## replace featureData
fData(example_sceset) <- fd
## replace phenotype data
pData(example_sceset) <- pd
## replace expression data to be used
exprs(example_sceset) <- log2(calculateCPM(example_sceset) + 1)
## Warning in .local(object, ...): 'sizeFactors' have not been set
## Warning in calculateCPM(example_sceset): size factors requested but not
## specified, using library sizes instead

It is possible to get an overall view of the dataset by using the plot method available for SCESet objects. This method plots the cumulative proportion of each cell’s library that is accounted for by the top highest-expressed features (by default showing the cumulative proportion across the top 500 features).

This type of plot gives an overall idea of differences in expression distributions for different cells. It is used in the same way as per-sample boxplots are for microarray or bulk RNA-seq data. Due to the large numbers of zeroes in expression values for single-cell RNA-seq data, boxplots are not as useful, so instead we focus on the contributions from the most expressed features for each cell.

With this function, we can split up the cells based on phenoData variables to get a finer-grained look at differences between cells. By default, the plot method will try to use transcripts-per-million values for the plot, but if these are not present in the SCESet object, then the values in exprs(object) will be used. Other expression values can be used, specified with the exprs_values argument

plot(example_sceset, block1 = "Mutation_Status", block2 = "Treatment",
     colour_by = "Cell_Cycle", nfeatures = 300, exprs_values = "counts")

This sort of approach can help to pick up large differences in expression distributions across different experimental blocks (e.g. processing batches or similar.)

4 Plots of expression values

In scater, the plotExpression function makes it easy to plot expression values for a subset of genes or features. This can be particularly useful when investigating the some features identified as being of interest from differential expression testing or other means.

plotExpression(example_sceset, rownames(example_sceset)[1:6],
               x = "Mutation_Status", exprs_values = "exprs", colour = "Treatment")

This function uses ggplot2, making it easy to change the theme to whatever you prefer. We can also show the median expression level per group on the plot and show a violin plot to summarise the distribution of expression values:

plotExpression(example_sceset, rownames(example_sceset)[7:12],
               x = "Mutation_Status", exprs_values = "counts", colour = "Cell_Cycle",
               show_median = TRUE, show_violin = FALSE,  xlab = "Mutation Status",
               log = TRUE)

The package also contains the function plotExprsVsTxLength() for plotting expression values against transcript length, and the function plotPlatePosition() to plot expression values and cell metadata information for cells in their position on a plate.

5 Quality control

The scater package puts a focus on aiding with quality control (QC) and pre-processing of single-cell RNA-seq data before further downstream analysis.

We see QC as consisting of three distinct steps:

  1. QC and filtering of features (genes)
  2. QC and filtering of cells
  3. QC of experimental variables

Following QC, we can proceed with data normalisation before downstream analysis and modelling.

In the next few sections we discuss the QC and filtering capabilities available in scater.

5.1 Calculate QC metrics

To compute commonly-used QC metrics we have the function calculateQCMetrics():

example_sceset <- calculateQCMetrics(example_sceset, feature_controls = 1:20)
varLabels(example_sceset)
##  [1] "Cell"                                          
##  [2] "Mutation_Status"                               
##  [3] "Cell_Cycle"                                    
##  [4] "Treatment"                                     
##  [5] "total_counts"                                  
##  [6] "log10_total_counts"                            
##  [7] "filter_on_total_counts"                        
##  [8] "total_features"                                
##  [9] "log10_total_features"                          
## [10] "filter_on_total_features"                      
## [11] "pct_dropout"                                   
## [12] "exprs_feature_controls_unnamed1"               
## [13] "pct_exprs_feature_controls_unnamed1"           
## [14] "filter_on_pct_exprs_feature_controls_unnamed1" 
## [15] "counts_feature_controls_unnamed1"              
## [16] "pct_counts_feature_controls_unnamed1"          
## [17] "filter_on_pct_counts_feature_controls_unnamed1"
## [18] "n_detected_feature_controls_unnamed1"          
## [19] "n_detected_feature_controls"                   
## [20] "counts_feature_controls"                       
## [21] "pct_counts_feature_controls"                   
## [22] "filter_on_pct_counts_feature_controls"         
## [23] "pct_counts_top_50_features"                    
## [24] "pct_counts_top_100_features"                   
## [25] "pct_counts_top_200_features"                   
## [26] "pct_counts_top_500_features"                   
## [27] "pct_counts_top_50_endogenous_features"         
## [28] "pct_counts_top_100_endogenous_features"        
## [29] "pct_counts_top_200_endogenous_features"        
## [30] "pct_counts_top_500_endogenous_features"        
## [31] "counts_endogenous_features"                    
## [32] "log10_counts_feature_controls_unnamed1"        
## [33] "log10_counts_feature_controls"                 
## [34] "log10_counts_endogenous_features"              
## [35] "is_cell_control"

More than one set of feature controls can be defined if desired.

example_sceset <- calculateQCMetrics(
    example_sceset, feature_controls = list(controls1 = 1:20, controls2 = 500:1000),
    cell_controls = list(set_1 = 1:5, set_2 = 31:40))
varLabels(example_sceset)
##  [1] "Cell"                                           
##  [2] "Mutation_Status"                                
##  [3] "Cell_Cycle"                                     
##  [4] "Treatment"                                      
##  [5] "total_counts"                                   
##  [6] "log10_total_counts"                             
##  [7] "filter_on_total_counts"                         
##  [8] "total_features"                                 
##  [9] "log10_total_features"                           
## [10] "filter_on_total_features"                       
## [11] "pct_dropout"                                    
## [12] "exprs_feature_controls_unnamed1"                
## [13] "pct_exprs_feature_controls_unnamed1"            
## [14] "filter_on_pct_exprs_feature_controls_unnamed1"  
## [15] "counts_feature_controls_unnamed1"               
## [16] "pct_counts_feature_controls_unnamed1"           
## [17] "filter_on_pct_counts_feature_controls_unnamed1" 
## [18] "n_detected_feature_controls_unnamed1"           
## [19] "log10_counts_feature_controls_unnamed1"         
## [20] "exprs_feature_controls_controls1"               
## [21] "pct_exprs_feature_controls_controls1"           
## [22] "filter_on_pct_exprs_feature_controls_controls1" 
## [23] "counts_feature_controls_controls1"              
## [24] "pct_counts_feature_controls_controls1"          
## [25] "filter_on_pct_counts_feature_controls_controls1"
## [26] "n_detected_feature_controls_controls1"          
## [27] "exprs_feature_controls_controls2"               
## [28] "pct_exprs_feature_controls_controls2"           
## [29] "filter_on_pct_exprs_feature_controls_controls2" 
## [30] "counts_feature_controls_controls2"              
## [31] "pct_counts_feature_controls_controls2"          
## [32] "filter_on_pct_counts_feature_controls_controls2"
## [33] "n_detected_feature_controls_controls2"          
## [34] "n_detected_feature_controls"                    
## [35] "counts_feature_controls"                        
## [36] "pct_counts_feature_controls"                    
## [37] "filter_on_pct_counts_feature_controls"          
## [38] "pct_counts_top_50_features"                     
## [39] "pct_counts_top_100_features"                    
## [40] "pct_counts_top_200_features"                    
## [41] "pct_counts_top_500_features"                    
## [42] "pct_counts_top_50_endogenous_features"          
## [43] "pct_counts_top_100_endogenous_features"         
## [44] "pct_counts_top_200_endogenous_features"         
## [45] "pct_counts_top_500_endogenous_features"         
## [46] "counts_endogenous_features"                     
## [47] "log10_counts_feature_controls_controls1"        
## [48] "log10_counts_feature_controls_controls2"        
## [49] "log10_counts_feature_controls"                  
## [50] "log10_counts_endogenous_features"               
## [51] "is_cell_control_set_1"                          
## [52] "is_cell_control_set_2"                          
## [53] "is_cell_control"

5.1.1 Cell-level QC metrics

This function adds the following columns to pData(object):

  • total_counts: total number of counts for the cell (aka ‘library size’)
  • log10_total_counts: total_counts on the log10-scale
  • total_features: the number of features for the cell that have expression above the detection limit (default detection limit is zero)
  • filter_on_total_counts: would this cell be filtered out based on its log10-total_counts being (by default) more than 5 median absolute deviations from the median log10-total_counts for the dataset?
  • filter_on_total_features: would this cell be filtered out based on its total_features being (by default) more than 5 median absolute deviations from the median total_features for the dataset?
  • counts_feature_controls: total number of counts for the cell that come from (a set of user-defined) control features. Defaults to zero if no control features are indicated.
  • counts_endogenous_features: total number of counts for the cell that come from endogenous features (i.e. not control features). Defaults to total_counts if no control features are indicated.
  • log10_counts_feature_controls: total number of counts from control features on the log10-scale. Defaults to zero (i.e. log10(0 + 1), offset to avoid infinite values) if no control features are indicated.
  • log10_counts_endogenous_features: total number of counts from endogenous features on the log10-scale. Defaults to zero (i.e. log10(0 + 1), offset to avoid infinite values) if no control features are indicated.
  • n_detected_feature_controls: number of defined feature controls that have expression greater than the threshold defined in the object. *pct_counts_feature_controls: percentage of all counts that come from the defined control features. Defaults to zero if no control features are defined.

If we define multiple sets of feature controls, then the above will be supplied for all feature sets, plus the set of all feature controls combined, as appropriate.

Furthermore, where “counts” appear in the above, the same metrics will also be computed for “exprs”, “tpm” and “fpkm” (if tpm and fpkm are present in the SCESet object).

5.1.2 Feature-level QC metrics

The function further adds the following columns to fData(object):

  • mean_exprs: the mean expression level of the gene/feature.
  • exprs_rank: the rank of the feature’s expression level in the cell.
  • total_feature_counts: the total number of counts mapped to that feature across all cells.
  • log10_total_feature_counts: total feature counts on the log10-scale.
  • pct_total_counts: the percentage of all counts that are accounted for by the counts mapping to the feature.
  • is_feature_control: is the feature a control feature? Default is FALSE unless control features are defined by the user.
  • n_cells_exprs: the number of cells for which the expression level of the feature is above the detection limit (default detection limit is zero).
names(fData(example_sceset))
##  [1] "Gene"                         "mean_exprs"                  
##  [3] "exprs_rank"                   "n_cells_exprs"               
##  [5] "total_feature_exprs"          "pct_total_exprs"             
##  [7] "pct_dropout"                  "total_feature_counts"        
##  [9] "log10_total_feature_counts"   "pct_total_counts"            
## [11] "is_feature_control_unnamed1"  "is_feature_control_controls1"
## [13] "is_feature_control_controls2" "is_feature_control"

As above, where “counts” appear in the above, the same metrics will also be computed for “exprs”, “tpm” and “fpkm” (if tpm and fpkm are present in the SCESet object).

5.2 Produce diagnostic plots for QC

Visualising the data and metadata in various ways can be very helpful for QC. We have a suite of plotting functions to produce diagnostic plots for:

  1. Plotting the most expressed features across the dataset.
  2. Finding the most important principal components for a given cell phenotype or metadata variable (from pData(object)).
  3. Plotting a set of cell phenotype/metadata variables against each other and calculating the (marginal) percentage of feature expression variance that they explain.

These three QC plots can all be accessed through the function plotQC (we need to make sure there are no features with zero or constant expression).

5.3 QC and filtering of features

The first step in the QC process is filtering out unwanted features. We will typically filter out features with very low overall expression, and any others that plots or other metrics indicate may be problematic.

First we look at a plot that shows the top 50 (by default) most-expressed features. By default, “expression” is defined using the feature counts (if available), but \(tpm\), \(cpm\), \(fpkm\) or the exprs values can be used instead, if desired.

keep_feature <- rowSums(counts(example_sceset) > 0) > 4
example_sceset <- example_sceset[keep_feature,]
## Plot QC
plotQC(example_sceset, type = "highest-expression", exprs_values = "counts")

The multiplot function allows a very simple way to plot multiple ggplot2 plots on the same page. For more sophisticated possibilities for arranging multiple ggplot2 plots, check out the excellent cowplot package, available on CRAN. If you have cowplot installed (highly recommended), then scater will automatically use it to create particularly attractive plots.

It can also be particularly useful to inspect the most-expressed features in just the cell controls (for example blanks or bulk samples). Subsetting capabilities for SCESet objects allow us to do this easily. In the previous section, we defined two sets of cell controls in the call to calculateQCMetrics. That function added the is_cell_control column to the phenotype data of the SCESet object example_sceset, which indicates if a cell is defined as a cell control across any of the cell control sets.

The $ operator makes it easy to access the is_cell_control column and use it to subset the SCESet as below. We can compare the most-expressed features in the cell controls and in the cells of biological interest with this subsetting, as demonstrated in the code below (plot not shown).

p1 <- plotQC(example_sceset[, !example_sceset$is_cell_control],
             type = "highest-expression")
p2 <- plotQC(example_sceset[, example_sceset$is_cell_control],
       type = "highest-expression")
multiplot(p1, p2, cols = 2)

Another way to obtain an idea of the level of technical noise in the dataset is to plot the frequency of expression (that is, number of cells with expression for the gene above the defined threshold (default is zero)) against mean expression expression level . A set of specific features to plot can be defined, but need not be. By default, the function will look for defined feature controls (as supplied to calculateQCMetrics). If feature controls are found, then these will be plotted, if not then all features will be plotted.

plotQC(example_sceset, type = "exprs-freq-vs-mean")
## `geom_smooth()` using method = 'loess'

We can also plot just a subset of features with code like that below (plot not shown):

feature_set_1 <- fData(example_sceset)$is_feature_control_controls1
plotQC(example_sceset, type = "exprs-freq-vs-mean", feature_set = feature_set_1)
## `geom_smooth()` using method = 'loess'

Beyond these QC plots, we have a neat, general and flexible function for plotting two feature metadata variables:

plotFeatureData(example_sceset, aes(x = n_cells_exprs, y = pct_total_counts))

We can see that there is a small number of features that are ubiquitously expressed expressed in all cells (n_cells_exprs) and account for a large proportion of all counts observed (pct_total_counts; more than 0.5% of all counts).

The subsetting of rows of SCESet objects makes it easy to drop unwanted features.

5.4 QC and filtering of cells

See plotPhenoData and other QC plots below. The subsetting of columns (which correspond to cells) of SCESet objects makes it easy to drop unwanted cells.

5.4.1 Plotting cell metadata variables

We also have neat functions to plot two cell metadata variables:

plotPhenoData(example_sceset, aes(x = Mutation_Status, y = total_features,
                                  colour = log10_total_counts))

Note that ggplot aesthetics will work correctly (in general) for everything except colour (color) and fill, which must be either columns of pData or feature names (i.e. gene/transcript names).

These sorts of plots can be very useful for finding potentially problematic cells.

plotPhenoData(example_sceset, aes(x = total_counts, y = total_features,
                                  colour = Gene_1000))

plotPhenoData(example_sceset, aes(x = pct_counts_feature_controls,
                                  y = total_features, colour = Gene_0500))

plotPhenoData(example_sceset, aes(x = pct_counts_feature_controls,
                                  y = pct_counts_top_50_features,
                                  colour = Gene_0001))

The output of these functions is a ggplot object, which can be added to, amended and altered. For example, if we don’t like the legend position we can change it, and we could also add a trend line for each group (see below).

Tapping into the powerful capabilities of ggplot2, the possibilities are many.

A particularly useful plot for cell QC is plotting the percentage of expression accounted for by feature controls against total_features.

plotPhenoData(example_sceset, aes(x = total_features,
                                  y = pct_counts_feature_controls,
                                  colour = Mutation_Status)) +
    theme(legend.position = "top") +
    stat_smooth(method = "lm", se = FALSE, size = 2, fullrange = TRUE)

On real data, we expect to see well-behaved cells with relatively high total_features (number of features with detectable expression) and low percentage of expression from feature controls. High percentage expression from feature controls and low total_features are indicative of blank and failed cells.

The plotPhenoData function is useful for exploring the relationships between the many QC metrics computed by calculateQCMetrics above. Often, problematic cells can be identified from such plots.

5.4.2 PCA and dimensionality reduction

The plotPCA function makes it easy to produce a PCA plot directly from an SCESet object, which is useful for visualising cells.

The default plot shows the first two principal components and if any cell controls have been defined, plots these cells in a different colour.

plotPCA(example_sceset)

By default, the PCA plot is produced using the 500 features with the most variable expression across all cells. The number of most-variable features used can be changed with the ntop argument. Alternatively, a specific set of features to use for the PCA can be defined with the feature_set argument. This allows, for example, using only housekeeping features or control features to produce a PCA plot.

By default the PCA plot uses the expression values in the exprs slot of the SCESet object, but other expression values can be used by specifying the exprs_values argument (plot not shown):

plotPCA(example_sceset, exprs_values = "cpm")

A subset of features can be used to produce a PCA plot. In the code below only the features defined as “feature controls” are used for the PCA (plot not shown).

plotPCA(example_sceset, feature_set = fData(example_sceset)$is_feature_control)

The function allows more than just the first two components to be plotted, and also allows phenotype variables to be used to define the colour, shape and size of points in the scatter plot.

plotPCA(example_sceset, ncomponents = 4, colour_by = "Treatment",
        shape_by = "Mutation_Status")

When more than two components are plotted, the diagonal boxes in the scatter plot matrix show the density for each component.

We can also use the colour and size of point in the plot to reflect feature expression values.

plotPCA(example_sceset, colour_by = "Gene_0001", size_by = "Gene_1000")

Thus, expression levels of two marker genes or transcripts can be shown overlaid onto reduced-dimension representations of cells. This also works for plotTSNE plots.

plotTSNE(example_sceset, colour_by = "Gene_0001", size_by = "Gene_1000")

And for diffusion map plots.

plotDiffusionMap(example_sceset, colour_by = "Gene_0001", size_by = "Gene_1000")

The SCESet matrix has a reducedDimension slot, where coordinates for a reduced dimension representation of the cells can be stored. If we so wish, the top principal components can be added to the reducedDimension slot:

example_sceset <- plotPCA(example_sceset, ncomponents = 4,
                          colour_by = "Treatment", shape_by = "Mutation_Status",
                          return_SCESet = TRUE, theme_size = 12)

head(reducedDimension(example_sceset))
##                  PC1        PC2        PC3        PC4         PC5        PC6
## Cell_001 -11.7405184 -0.3209675  3.5990418 -2.9751060   1.5810125 -4.1999258
## Cell_002  -1.9466596 -4.7957353  1.3600393  0.8383979   0.9423988 10.7112143
## Cell_003   1.5947439 -1.5970910  0.8662887 -3.6980087   6.1636357  2.2419701
## Cell_004 -10.2066554 -1.4720576 -5.8766212  5.0231098  -8.1705614 -0.4751267
## Cell_005   6.9926111 -0.2796181 -5.5851141 -0.6082134 -10.6324267 -3.8372210
## Cell_006  -0.4625486  4.9714271  7.9721817 -6.2982593  -0.1959868 -2.6861381
##                 PC7       PC8        PC9      PC10
## Cell_001  0.2708609  2.363178 -2.3003535  1.694117
## Cell_002  1.4175784  1.510552 -7.1935431 -4.234476
## Cell_003  1.3487904 -9.071572 -0.7153641  6.275781
## Cell_004  7.3409573 -4.127103 -0.9559890  3.896330
## Cell_005 -6.2793325  1.180982 -4.4908847  3.796712
## Cell_006 -0.9166522  1.887895  0.9683000 -6.971136

As shown above, the function reducedDimension (as well as the shorthand redDim) can be used to access the reduced dimension coordinates.

This means that any other dimension reduction method can be applied and the coordinates stored. For example, we might wish to use t-distributed stochastic nearest neighbour embedding (t-SNE) or Gaussian process latent variable models or any other dimensionality reduction method. We can store these in the SCESet object and produce plots just as we did with PCA (plot not shown):

plotReducedDim(example_sceset, ncomponents = 4, colour_by = "Treatment",
               shape_by = "Mutation_Status")

As for the PCA plots we can also colour and size points by feature expression.

plotReducedDim(example_sceset, ncomponents = 4, colour_by = "Gene_1000",
               size_by = "Gene_0500")

(Here, the dimensionality reduction is just PCA, so we have the same plot as the PCA plot above.)

Based on PCA or dimensionality reduction plots we may identify outlier cells and, if we wish, filter them out of the analysis. There is also an outlier detection option available with the plotPCA function.

example_sceset <- plotPCA(example_sceset, pca_data_input = "pdata", 
                          detect_outliers = TRUE, return_SCESet = TRUE)
## sROC 0.1-2 loaded
## The following cells/samples are detected as outliers:
## 
## Variables with highest loadings for PC1 and PC2:
## 
##                                            PC1          PC2
## ---------------------------------  -----------  -----------
## pct_counts_top_100_features          0.4134957   -0.2473997
## pct_counts_feature_controls          0.1653627   -0.4157540
## log10_counts_feature_controls       -0.4030576   -0.5920277
## log10_counts_endogenous_features    -0.4271752   -0.4937645
## n_detected_feature_controls         -0.4755199    0.2595464
## total_features                      -0.4802325    0.3229203

The $outlier element of the pData (phenotype data) slot of the SCESet contains indicator values about whether or not each cell has been designated as an outlier based on the PCA. Here, these values can be accessed for filtering low quality cells with example_sceset$outlier. Automatic outlier detection can be informative, but a close inspection of QC metrics and tailored filtering for the specifics of the dataset at hand is strongly recommended.

5.4.3 Filtering cells

On this example dataset there are no cells that need filtering, but the subsetting capabilities of scater make it easy to filter out unwanted cells. Column subsetting selects cells, while row subsetting selects features (genes or transcripts). In particular, there is a function filter (inspired by the function of the same name in the dplyr package and operating in exactly the same) that can be used to very conviently subset (i.e. filter) the cells of an SCESet object based on pData variables of the object.

5.5 QC of experimental variables

See the plotQC options below. The various plotting functions enable visualisation of the relationship betwen experimental variables and the expression data.

We can look at the relative importance of different explanatory variables with some of the plotQC function options. We can compute the median marginal \(R^2\) for each variable in pData(example_sceset) when fitting a linear model regressing exprs values against just that variable.

The default approach looks at all variables in pData(object) and plots the top nvars_to_plot variables (default is 10).

plotQC(example_sceset, type = "expl")
## The variable filter_on_total_counts only has one unique value, so R^2 is not meaningful.
## This variable will not be plotted.
## The variable filter_on_total_features only has one unique value, so R^2 is not meaningful.
## This variable will not be plotted.
## The variable filter_on_pct_exprs_feature_controls_unnamed1 only has one unique value, so R^2 is not meaningful.
## This variable will not be plotted.
## The variable filter_on_pct_counts_feature_controls_unnamed1 only has one unique value, so R^2 is not meaningful.
## This variable will not be plotted.
## The variable filter_on_pct_exprs_feature_controls_controls1 only has one unique value, so R^2 is not meaningful.
## This variable will not be plotted.
## The variable filter_on_pct_counts_feature_controls_controls1 only has one unique value, so R^2 is not meaningful.
## This variable will not be plotted.
## The variable filter_on_pct_exprs_feature_controls_controls2 only has one unique value, so R^2 is not meaningful.
## This variable will not be plotted.
## The variable filter_on_pct_counts_feature_controls_controls2 only has one unique value, so R^2 is not meaningful.
## This variable will not be plotted.
## The variable filter_on_pct_counts_feature_controls only has one unique value, so R^2 is not meaningful.
## This variable will not be plotted.
## The variable outlier only has one unique value, so R^2 is not meaningful.
## This variable will not be plotted.

Alternatively, we can choose a subset of variables to plot in this manner.

plotQC(example_sceset, type = "expl",
       variables = c("total_features", "total_counts", "Mutation_Status", "Treatment",
                     "Cell_Cycle"))

We can also easily produce plots to identify PCs that correlate with experimental and QC variables of interest. The function ranks the principal components in decreasing order of \(R^2\) from a linear model regressing PC value against the variable of interest.

We can also produce a pairs plot of potential explanatory variables ranked by their median percentage of expression variance explained in a marginal (only one explanatory variable) linear model.

plotQC(example_sceset, type = "expl", method = "pairs", theme_size = 6)
## The variable filter_on_total_counts only has one unique value, so R^2 is not meaningful.
## This variable will not be plotted.
## The variable filter_on_total_features only has one unique value, so R^2 is not meaningful.
## This variable will not be plotted.
## The variable filter_on_pct_exprs_feature_controls_unnamed1 only has one unique value, so R^2 is not meaningful.
## This variable will not be plotted.
## The variable filter_on_pct_counts_feature_controls_unnamed1 only has one unique value, so R^2 is not meaningful.
## This variable will not be plotted.
## The variable filter_on_pct_exprs_feature_controls_controls1 only has one unique value, so R^2 is not meaningful.
## This variable will not be plotted.
## The variable filter_on_pct_counts_feature_controls_controls1 only has one unique value, so R^2 is not meaningful.
## This variable will not be plotted.
## The variable filter_on_pct_exprs_feature_controls_controls2 only has one unique value, so R^2 is not meaningful.
## This variable will not be plotted.
## The variable filter_on_pct_counts_feature_controls_controls2 only has one unique value, so R^2 is not meaningful.
## This variable will not be plotted.
## The variable filter_on_pct_counts_feature_controls only has one unique value, so R^2 is not meaningful.
## This variable will not be plotted.
## The variable outlier only has one unique value, so R^2 is not meaningful.
## This variable will not be plotted.

In this small dataset, total_counts and total_features explain a very large proportion of the variance in feature expression. The proportion of variance that they explain for a real dataset should be much smaller (say 1-5%).

The default is to plot six most-associated principal components against the variable of interest.

p1 <- plotQC(example_sceset, type = "find-pcs", variable = "total_features",
        plot_type = "pcs-vs-vars")
p2 <- plotQC(example_sceset, type = "find-pcs", variable = "Cell_Cycle",
       plot_type = "pcs-vs-vars")
multiplot(p1, p2, cols = 2)
## `stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.

An alternative is to produce a pairs plot of the top five PCs.

plotQC(example_sceset, type = "find-pcs", variable = "total_features",
       plot_type = "pairs-pcs")

plotQC(example_sceset, type = "find-pcs", variable = "Cell_Cycle",
       plot_type = "pairs-pcs")

Combined with the excellent subsetting capabilities of the SCESet class, we have convenient tools for conducting QC and pre-processing (e.g. filtering) data for downstream analysis.

6 Data normalisation

High levels of variability between cells characterise single-cell expression data. In almost all settings, many sources of unwanted variation should be accounted for before proceeding with more sophisticated analysis.

The size-factor normalisation method from the scran package is tightly integrated with scater and strongly recommended as a first normalization of the data before investigating other sources of variability.

Below, we show some of scater’s capabilities for normalising data for downstream analyses.

We can use feature controls to help address differences between cells arising from different sets of transcripts being expressed and differences in library composition.

Important experimental variables and latent factors (if used) can be regressed out, so that normalised data has these effects removed.

7 Feature and cell pairwise distance matrices

In many single-cell expression analyses we may want to generate and store pairwise distance matrices for both cells and features.

We can first look at a multidimensional scaling (MDS) plot using Euclidean distance between cells.

cell_dist <- dist(t(exprs(example_sceset)))
cellPairwiseDistances(example_sceset) <- cell_dist
plotMDS(example_sceset)

Second, we could also look at an MDS plot using the count data and the Canberra distance metric (plot not shown).

cell_dist <- dist(t(counts(example_sceset)), method = "canberra")
cellPairwiseDistances(example_sceset) <- cell_dist
plotMDS(example_sceset, colour_by = "Mutation_Status")

We could also look at an MDS plot for the features, here using Euclidean distance (plot not shown).

feature_dist <- dist(exprs(example_sceset))
featurePairwiseDistances(example_sceset) <- feature_dist
limma::plotMDS(featDist(example_sceset))

8 Using kallisto and Salmon to quantify transcript abundance from within R

Lior Pachter’s group at Berkeley has recently released a new software tool called kallisto for rapid quantification of transcript abundance from RNA-seq data. Similarly, Rob Patro at Stony Brook University and colleagues have released Salmon, a similarly fast and accurate RNA-seq quantification tool. In scater, a couple of wrapper functions to kallisto and Salmon enable easy and extremely fast quantification of transcript abundance from within R.

For simplicity, examples are shown below demonstrating the use of kallisto, but the same could be done with Salmon using almost identical interfaces (replacing runKallisto with runSalmon, readKallistoResults with readSalmonResults and so on).

################################################################################
### Tests and Examples

# Example if in the kallisto/test directory
setwd("/home/davis/kallisto/test")
kallisto_log <- runKallisto("targets.txt", "transcripts.idx", single_end=FALSE,
            output_prefix="output", verbose=TRUE, n_bootstrap_samples=10)

sce_test <- readKallistoResults(kallisto_log, read_h5=TRUE)
sce_test

An example using real data from a project investigating cell cycle. Note that this analysis is not ‘live’ (the raw data are not included in the package), but it demonstrates what can be done with scater and kallisto.

setwd("/home/davis/021_Cell_Cycle/data/fastq")
system("wc -l targets.txt")
ave_frag_len <- mean(c(855, 860, 810, 760, 600, 690, 770, 690))

kallisto_test <- runKallisto("targets.txt",
                             "Mus_musculus.GRCm38.rel79.cdna.all.ERCC.idx",
                             output_prefix="kallisto_output_Mmus", n_cores=12,
                             fragment_length=ave_frag_len, verbose=TRUE)
sce_kall_mmus <- readKallistoResults(kallisto_test, read_h5=TRUE)
sce_kall_mmus

sce_kall_mmus <- readKallistoResults(kallisto_test)

sce_kall_mmus <- getBMFeatureAnnos(sce_kall_mmus)

head(fData(sce_kall_mmus))
head(pData(sce_kall_mmus))
sce_kall_mmus[["start_time"]]

counts(sce_kall_mmus)[sample(nrow(sce_kall_mmus), size=15), 1:6]

## Summarise expression at the gene level
sce_kall_mmus_gene <- summariseExprsAcrossFeatures(
    sce_kall_mmus, exprs_values="tpm", summarise_by="feature_id")

datatable(fData(sce_kall_mmus_gene))

sce_kall_mmus_gene <- getBMFeatureAnnos(
    sce_kall_mmus_gene, filters="ensembl_gene_id",
    attributes=c("ensembl_gene_id", "mgi_symbol", "chromosome_name",
                 "gene_biotype", "start_position", "end_position",
                 "percentage_gc_content", "description"),
    feature_symbol="mgi_symbol", feature_id="ensembl_gene_id",
    biomart="ensembl", dataset="mmusculus_gene_ensembl")

datatable(fData(sce_kall_mmus_gene))

## Add gene symbols to featureNames to make them more intuitive
new_feature_names <- featureNames(sce_kall_mmus_gene)
notna_mgi_symb <- !is.na(fData(sce_kall_mmus_gene)$mgi_symbol)
new_feature_names[notna_mgi_symb] <- fData(sce_kall_mmus_gene)$mgi_symbol[notna_mgi_symb]
notna_ens_gid <- !is.na(fData(sce_kall_mmus_gene)$ensembl_gene_id)
new_feature_names[notna_ens_gid] <- paste(new_feature_names[notna_ens_gid],
          fData(sce_kall_mmus_gene)$ensembl_gene_id[notna_ens_gid], sep="_")
sum(duplicated(new_feature_names))
featureNames(sce_kall_mmus_gene) <- new_feature_names
head(featureNames(sce_kall_mmus_gene))
tail(featureNames(sce_kall_mmus_gene))
sum(duplicated(fData(sce_kall_mmus_gene)$mgi_symbol))