1 Introduction

In this section we are going to cover the basics of feature selection and dimensionality reduction. These methods allow us to represent our multi-dimensional data (with thousands of cells and thousands of genes) in a reduced set of dimensions for visualisation and more efficient downstream analysis.

In feature selection the principle is to remove those genes which are uninteresting or uninformative to both improve computation and because this ‘noise’ reduction will hopefully enable us to more clearly see the true biology in our data. We make the assumption that most of the low level variance is not caused by real biology and is due to stochastic sampling in the single cell protocol and various other technical effects. The genes which have the most variance are therefore the ones that reflect the real biological difference and are what we want to focus on. This is obviously not a perfect assumption but it is a good way to make informed interpretations from your data that you can hopefully have a little more confidence in.

In dimensionality reduction we attempt to find a way to represent all the the information we have in expression space in a way that we can easily interpret. High dimensional data has several issues. There is a high computational requirement to performing analysis on 30,000 genes and 48,000 cells (in the Caron dataset). Humans live in a 3D world; we can’t easily visualise all the dimensions. And then there is sparsity. As the number of dimensions increases the distance between two data points increases and becomes more invariant. This invariance causes us problems when we try to cluster the data into biologically similar groupings. By applying some dimensionality reducing methods we can solve these issues to a level where we can make interpretations.

2 Setup

Before we start, let’s load our packages and read our data in.

library(scater) 
library(scran)
library(PCAtools)
library(tidyverse)
library(patchwork)

We will load the SingleCellExperiment object generated in the Normalisation section, which contains normalised counts for 500 cells per sample. For demonstration purposes we are not using the full dataset but you would in your analyses.

sce <- readRDS("R_objects/Caron_normalized.500.rds")
sce
## class: SingleCellExperiment 
## dim: 29346 5500 
## metadata(1): Samples
## assays(2): counts logcounts
## rownames(29346): ENSG00000243485 ENSG00000238009 ... ENSG00000275063
##   ENSG00000278817
## rowData names(4): ID Symbol Type Chromosome
## colnames(5500): 1_CGACTTCGTCCAGTTA-1 1_AGAATAGCATACGCTA-1 ...
##   8_AGCGTCGAGATGTAAC-1 8_CATGACAAGATAGGAG-1
## colData names(11): Sample Barcode ... total sizeFactor
## reducedDimNames(0):
## mainExpName: NULL
## altExpNames(0):

To make some of our plots later on easier to interpret, we will replace the rownames of the object (containing Ensembl gene IDs) with the gene symbol. Sometimes it happens that there is no gene symbol and in some cases there are multiple genes with the same symbol (see e.g. RGS5). A safe way to handle these cases is to use the uniquifyFeatureNames() function, which will add the Ensembl gene id to symbols where necessary to distinguish between different genes.

rownames(sce) <- uniquifyFeatureNames(rownames(sce), rowData(sce)$Symbol)

3 Feature Selection

We often use scRNA-seq data in exploratory analyses to characterize heterogeneity across cells. Procedures like clustering and dimensionality reduction compare cells based on their gene expression profiles, which involves aggregating per-gene differences into a single (dis)similarity metric between every pair of cells. The choice of genes to use in this calculation has a major impact on the behavior of the metric and the performance of downstream methods. We want to select genes that contain useful information about the biology of the system while removing genes that contain random noise. This aims to preserve interesting biological structure without the variance that obscures that structure, and to reduce the size of the data to improve computational efficiency of later steps.

The simplest approach to feature selection is to select the most variable genes based on their expression across the population. This assumes that genuine biological differences will manifest as increased variation in the affected genes, compared to other genes that are only affected by technical noise or a baseline level of “uninteresting” biological variation (e.g., from transcriptional bursting). Several methods are available to quantify the variation per gene and to select an appropriate set of highly variable genes (HVGs).

3.1 Quantifying per-gene variation

Some assays allow the inclusion of known molecules in a known amount covering a wide range, from low to high abundance: spike-ins. The technical noise is assessed based on the amount of spike-ins used, the corresponding read counts obtained and their variation across cells. The variance in expression can then be decomposed into the biological and technical components. There is discussion over whether this step is necessary but like all of the decisions you make in your analyses it would be wise to optimise your parameters and steps over several iterations with a view to existing biological knowlegde and controls.

The commonly used UMI-based assays do not (yet?) allow spike-ins. But one can still identify highly variable genes (HVGs), which likely capture biological variation. Assuming that, for most genes, the observed variance across cells is due to technical noise, we can assess technical variation by fitting a trend line between the mean-variance relationship across all genes. Genes that substantially deviate from this trend may then be considered as highly-variable, i.e. capturing biologically interesting variation. The scran function modelGeneVar with carry out this estimation for us.

The resulting output gives us a dataframe with all our genes and several columns. It has modeled the mean-variance relationship and from there it has estimated our total variance along with what it considers is the biological and technical variance.

gene_var <- modelGeneVar(sce)

gene_var
## DataFrame with 29346 rows and 6 columns
##                        mean       total        tech          bio    p.value
##                   <numeric>   <numeric>   <numeric>    <numeric>  <numeric>
## MIR1302-2HG     0.000296568 0.000483740 0.000359017  1.24723e-04 0.00218142
## ENSG00000238009 0.001885572 0.002130691 0.002282621 -1.51930e-04 0.70752449
## ENSG00000239945 0.000148617 0.000121478 0.000179911 -5.84331e-05 0.99615196
## ENSG00000241860 0.005441182 0.006111014 0.006586943 -4.75929e-04 0.72337243
## LINC01409       0.037155913 0.046918261 0.044979122  1.93914e-03 0.36175780
## ...                     ...         ...         ...          ...        ...
## ENSG00000271254 0.051700861 0.057205459 0.062585099 -5.37964e-03 0.75970043
## ENSG00000276345 0.027056121 0.028648084 0.032753127 -4.10504e-03 0.84812953
## ENSG00000277856 0.000352207 0.000371456 0.000426372 -5.49152e-05 0.85471363
## ENSG00000275063 0.002842271 0.004530285 0.003440774  1.08951e-03 0.00468417
## ENSG00000278817 0.004954633 0.007312049 0.005997940  1.31411e-03 0.03610314
##                       FDR
##                 <numeric>
## MIR1302-2HG     0.0212537
## ENSG00000238009 1.0000000
## ENSG00000239945 1.0000000
## ENSG00000241860 1.0000000
## LINC01409       0.9185522
## ...                   ...
## ENSG00000271254 1.0000000
## ENSG00000276345 1.0000000
## ENSG00000277856 1.0000000
## ENSG00000275063 0.0406691
## ENSG00000278817 0.2082726

We can then plot variance vs mean for each gene and visualise the limit of technical variance according to the fitted trend line. We would regard those features below the trend line as technical noise.

gene_var %>% 
  as.data.frame() %>% 
  ggplot(aes(mean, total)) +
  geom_point() +
  geom_line(aes(y = tech), colour = "dodgerblue", size = 1) +
  labs(x = "Mean of log-expression", y = "Variance of log-expression")

3.2 Selecting highly variable genes

Once we have quantified the per-gene variation, the next step is to select the subset of HVGs to use in downstream analyses. A larger subset will reduce the risk of discarding interesting biological signal by retaining more potentially relevant genes, at the cost of increasing noise from irrelevant genes that might obscure said signal. It is difficult to determine the optimal trade-off for any given application as noise in one context may be useful signal in another. Commonly applied strategies are:

  • take top X genes with largest (biological) variation

Top 1000 genes: getTopHVGs(gene_var, n=1000)

Top 10% genes: getTopHVGs(gene_var, prop=0.1)

  • based on significance

getTopHVGs(gene_var, fdr.threshold = 0.05)

  • keeping all genes above the trend

getTopHVGs(gene_var, var.threshold = 0)

  • selecting a priori genes of interest

In our example, we will define ‘HVGs’ as the top 10% of genes with the highest biological component. This is a fairly arbitrary choice. A common practice is to pick an arbitrary threshold (either based on number of proportion) and proceed with the rest of the analysis, with the intention of testing other choices later, rather than spending much time worrying about obtaining the “optimal” value.

hvgs <- getTopHVGs(gene_var, prop=0.1)
length(hvgs)
## [1] 1449
hvgs[1:10]
##  [1] "HBB"      "HBA2"     "HBA1"     "HBD"      "HBM"      "CD74"    
##  [7] "AHSP"     "HLA-DRA"  "CA1"      "SLC25A37"

The result is a vector of gene IDs ordered by their biological variance (i.e. highest deviation from the trend line shown above). We can use this with functions that accept a list of genes as option to restrict their analysis to that subset of genes (e.g. when we do PCA later on).

We can visualise the expression of the top most-variable genes with a violin plot for each gene using the plotExpression() function:

plotExpression(sce, features = hvgs[1:20], point_alpha = 0.05)

4 Dimensionality Reduction

Many scRNA-seq analysis procedures involve comparing cells based on their expression values across thousands of genes. Thus, each individual gene represents a dimension of the data (and the total number of genes represents the “dimensionality” of the data). More intuitively, if we had a scRNA-seq data set with only two genes, we could visualise our data in a two-dimensional scatterplot, with each axis representing the expression of a gene and each point in the plot representing a cell. Intuitively, we can imagine the same for 3 genes, represented as a 3D plot. Although it becomes harder to imagine, this concept can be extended to data sets with thousands of genes (dimensions), where each cell’s expression profile defines its location in the high-dimensional expression space.

As the name suggests, dimensionality reduction aims to reduce the number of separate dimensions in the data. This is possible because different genes are correlated if they are affected by the same biological process. Thus, we do not need to store separate information for individual genes, but can instead compress multiple features into a single dimension, e.g., an “eigengene” (Langfelder and Horvath 2007). This reduces computational work in downstream analyses like clustering, as calculations only need to be performed for a few dimensions, rather than thousands. It also reduces noise by averaging across multiple genes to obtain a more precise representation of the patterns in the data. And finally it enables effective visualisation of the data, for those of us who are not capable of visualizing more than 2 or 3 dimensions.

Here, we will cover three methods that are most commonly used in scRNA-seq analysis:

Before we go into the details of each method, it is important to mention that while the first method (PCA) can be used for downstream analysis of the data (such as cell clustering), the latter two methods (t-SNE and UMAP) should only be used for visualisation and not for any other kind of analysis.

4.1 Principal Components Analysis

One of the most used and well-known methods of dimensionality reduction is principal components analysis (PCA). This method performs a linear transformation of the data, such that a set of variables (genes) are turned into new variables called Principal Components (PCs). These principal components combine information across several genes in a way that best captures the variability observed across samples (cells).

Watch the video linked below for more details of how PCA works:

StatQuest: PCA

After performing a PCA, there is no data loss, i.e. the total number of variables does not change. Only the fraction of variance captured by each variable differs.

Each PC represents a dimension in the new expression space. The first PC explains the highest proportion of variance possible. The second PC explains the highest proportion of variance not explained by the first PC. And so on: successive PCs each explain a decreasing amount of variance not captured by the previous ones.

The advantage of using PCA is that the total amount of variance explained by the first few PCs is usually enough to capture most of the signal in the data. Therefore, we can exclude the remaining PCs without much loss of information. The stronger the correlation between the initial variables, the stronger the reduction in dimensionality. We will see below how we can choose how many PCs to retain for our downstream analysis.

4.1.1 Running PCA

As discussed in the Preprocessing and QC section, SingleCellExperiment objects contain a slot that can store representations of our data in reduced dimensions. This is useful as we can keep all the information about our single-cell data within a single object.

The runPCA() function can be used to run PCA on a SCE object, and returns an updated version of the single cell object with the PCA result added to the reducedDim slot.

Importantly, we can also restrict the PCA to use only some of the features (rows) of the object, which in this case we do by using the highly variable genes we identified earlier.

Running the PCA returns a new SCE object with a “PCA” reducedDim slot.

sce <- runPCA(sce, subset_row = hvgs)
sce
## class: SingleCellExperiment 
## dim: 29346 5500 
## metadata(1): Samples
## assays(2): counts logcounts
## rownames(29346): MIR1302-2HG ENSG00000238009 ... ENSG00000275063
##   ENSG00000278817
## rowData names(4): ID Symbol Type Chromosome
## colnames(5500): 1_CGACTTCGTCCAGTTA-1 1_AGAATAGCATACGCTA-1 ...
##   8_AGCGTCGAGATGTAAC-1 8_CATGACAAGATAGGAG-1
## colData names(11): Sample Barcode ... total sizeFactor
## reducedDimNames(1): PCA
## mainExpName: NULL
## altExpNames(0):

We can see that the output shows a new reducedDimNames value called “PCA”. We can access it by using the reducedDim() function:

reducedDim(sce, "PCA")[1:10, 1:5]
##                           PC1       PC2        PC3       PC4        PC5
## 1_CGACTTCGTCCAGTTA-1 4.857944 -8.678251 -1.9769310  7.763731 -1.8736044
## 1_AGAATAGCATACGCTA-1 6.975464 -6.821056  0.8777444  7.226113 -2.0904117
## 1_TGACTAGAGAACTCGG-1 5.418403 -6.046118 -1.4431368  8.104965 -1.8340643
## 1_CTTAACTGTTATGCGT-1 6.710139 -8.502934 11.4443355 -8.604738  0.5148573
## 1_CCCAGTTTCAAGCCTA-1 6.394137 -9.307770  0.5732515  6.068439 -2.0552334
## 1_TACAGTGCACGACGAA-1 4.711232 -7.563851  3.0215653  7.903445 -0.9057246
## 1_CTTTGCGTCCCATTTA-1 7.314671 -9.222312  1.8459769  3.971201 -2.3524221
## 1_GCGCAGTCACGGTGTC-1 5.489230 -9.750849 -1.4120589  7.026349 -2.8158097
## 1_AGCTTGAAGGAGTACC-1 5.283623 -8.215514 -1.1146285  7.258839 -2.8973718
## 1_AGGTCATGTAAGCACG-1 5.451176 -7.573401  0.6106959  6.623330 -1.6791321

By default, runPCA() returns the first 50 PCs, but you can change this number by specifying the ncomponents option.

One of the first things to investigate after doing a PCA is how much variance is explained by each PC. This information is stored as an “attribute” (think of it as additional information) attached to the PCA matrix above. The typical way to view this information is using what is known as a “scree plot”.

percent.var <- attr(reducedDim(sce), "percentVar")

plot(percent.var, log = "y", xlab = "PC", ylab = "Variance explained (%)")

We can see how the two first PCs explain a substantial amount of the variance, and very little variation is explained beyond 10-15 PCs.

To visualise our cells in the reduced dimension space defined by PC1 and PC2, we can use the plotReducedDim() function.

plotReducedDim(sce, dimred = "PCA", colour_by = "SampleName")

The proximity of cells in this plot reflects the similarity of their expression profiles.

We can also plot several PCs at once, using the ncomponents option:

plotReducedDim(sce, dimred = "PCA", ncomponents = 3, colour_by = "SampleName")

Although these plotting functions are useful for quickly visualising our data, more customised visualisations can be used by using the ggcells() function, which extends the regular ggplot() function, but to work directly from the SCE object. We can use it in a similar manner as we use the regular ggplot() function, except we can define aesthetics both from our reducedDim slot as well as colData and even assays (to plot particular gene’s expression). Here is an example, where we facet our plot:

ggcells(sce, aes(x = PCA.1, y = PCA.2, colour = SampleName)) +
  geom_point(size = 0.5) +
  facet_wrap(~ SampleName) +
  labs(x = "PC1", y = "PC2", colour = "Sample")

4.1.2 PCA Diagnostics

There are a large number of potential confounders, artifacts and biases in scRNA-seq data. One of the main challenges stems from the fact that it is difficult to carry out true technical replication to distinguish biological and technical variability. Here we will continue to explore how experimental artifacts can be identified and removed.

One of the ways to achieve this is to calculate the association between our PC scores and different variables associated with our cells such as sample groups, number of detected genes, total reads per cell, percentage of mitochondrial genes, etc. We can achieve this using the getExplanatoryPCs() function (and associated plotting function), which calculates the variance in each PC explained by those variables we choose:

explain_pcs <- getExplanatoryPCs(sce,
                                variables = c("sum",
                                              "detected",
                                              "SampleGroup",
                                              "SampleName",
                                              "subsets_Mito_percent")
                                )

plotExplanatoryPCs(explain_pcs/100)

We can see that PC1 can be explained mostly by individual samples (SampleName), mitochondrial expression and mutation group (SampleGroup).

We can also compute the marginal R2 for each variable when fitting a linear model regressing expression values for each gene against just that variable, and display a density plot of the gene-wise marginal R2 values for the variables.

plotExplanatoryVariables(sce,
                         variables = c(
                           "sum",
                           "detected",
                           "SampleGroup",
                           "SampleName",
                           "subsets_Mito_percent"
                         ))

This analysis indicates that individual and subtype have the highest explanatory power for many genes, and we don’t see technical covariates having as high correlations. If that were the case, we might need to repeat the normalization step while conditioning out for these covariates, or we would include them in downstream analysis.

4.1.3 Chosing the number of PCs

The choice of the number of PCs we retain for downstream analyses is a decision that is analogous to the choice of the number of HVGs to use. Using more PCs will retain more biological signal at the cost of including more noise that might mask said signal. Much like the choice of the number of HVGs, it is hard to determine whether an “optimal” choice exists for the number of PCs. Even if we put aside the technical variation that is almost always uninteresting, there is no straightforward way to automatically determine which aspects of biological variation are relevant; one analyst’s biological signal may be irrelevant noise to another analyst with a different scientific question.

Most practitioners will simply set the number of PCs to a “reasonable” but arbitrary value, typically ranging from 10 to 50. This is often satisfactory as the later PCs explain so little variance that their inclusion or omission has no major effect. For example, in our dataset, few PCs explain more than 1% of the variance in the entire dataset.

table(percent.var > 1)
## 
## FALSE  TRUE 
##    40    10

The most commonly used strategies to choose PCs for downstream analysis include:

  • selecting the top X PCs (with X typically ranging from 10 to 50)
  • using the elbow point in the scree plot
  • using technical noise
  • using permutation

4.1.3.1 Elbow point

To choose the elbow point in our scree plot, we can use the following:

chosen_elbow <- findElbowPoint(percent.var)
chosen_elbow
## [1] 8

Here is our scree plot again, but this time with a vertical line indicating the elbow point:

plot(percent.var)
abline(v=chosen_elbow, col="dodgerblue")

4.1.3.2 Denoising PCA

The assumption of this method is that the biology drives most of the variance and hence should be captured by the first few PCs, while technical noise affects each gene independently, hence it should be captured by later PCs. Therefore, our aim in this approach is to find the minimum number of PCs that explains more variance than the total technical variance across genes (estimated from our mean-variance trend).

This method is implemented in the denoisePCA() function:

sce.denoised <- denoisePCA(sce, technical = gene_var, subset.row = hvgs)

If we look at our PCA result, we can see that it has 6 columns.

ncol(reducedDim(sce.denoised, "PCA"))
## [1] 6

4.1.3.3 Permutation

We do not demonstrate this method, as it is more code intensive. The idea is to permute (or “shuffle”) a subset of the data, rerun the PCA and calculate the percentage of variance explained by each PC on this “random” dataset. We can then compare the observed variance explained in the original data with this null or random expectation and determine a cutoff where the observed variance explained drops to similar values as the variance explained in the shuffled data.

In this example (which is for illustration only) we may define a threshold at PC8, since after that the variance explained in the observed data drops below the levels in the permuted (randomised) data.

4.2 t-SNE: t-Distributed Stochastic Neighbor Embedding

The t-Distributed Stochastic Neighbor Embedding (t-SNE) approach addresses the main shortcoming of PCA, which is that it can only capture linear transformations of the original variables (genes). Instead, t-SNE allows for non-linear transformations, while preserving the local structure of the data. This means that neighbourhoods of similar samples (cells, in our case) will appear together in a t-SNE projection, with distinct cell clusters separating from each other.

As you can see below, compared to PCA we get much tighter clusters of samples, with more structure in the data captured by two t-SNE axis, compared to the two first principal components.

We will not go into the details of the algorithm here, but briefly it involves two main steps:

  • Calculating a similarity matrix between every pair of samples. This similarity is scaled by a Normal distribution, such that points that are far away from each other are “penalised” with a very low similarity score. The variance of this normal distribution can be thought of as a “neighbourhood” size when computing similarities between cells, and is parameterised by a term called perplexity.

  • Then, samples are projected on a low-dimensional space (usually two dimensions) such that the similarities between the points in this new space are as close as possible to the similarities in the original high-dimensional space. This step involves a stochastic algorithm that “moves” the points around until it converges on a stable solution. In this case, the similarity between samples is scaled by a t-distribution (that’s where the “t” in “t-SNE” comes from), which is used instead of the Normal to guarantee that points within a cluster are still distinguishable from each other in the 2D-plane (the t-distribution has “fatter” tails than the Normal distribution).

Watch this video if you want to learn more about how t-SNE works:

StatQuest: t-SNE

There are two important points to remember:

  • the perplexity parameter, which indicates the relative importance of the local and global patterns in the structure of the data set. The default value in the functions we will use is 50, but different values should be tested to ensure consistency in the results.
  • stochasticity: because the t-SNE algorithm is stochastic, running the analysis multiple times will produce slightly different results each time (unless we set a “seed” for the random number generator).

See this interactive article on “How to Use t-SNE Effectively”, which illustrates how changing these parameters can lead to widely different results.

Importantly, because of the non-linear nature of this algorithm, strong interpretations based on how distant different groups of cells are from each other on a t-SNE plot are discouraged, as they are not necessarily meaningful. This is why it is often the case that the x- and y-axis scales are omitted from these plots (as in the example above), as they are largely uninterpretable. Therefore, the results of a t-SNE projection should be used for visualisation only and not for downstream analysis (such as cell clustering).

4.2.1 Running t-SNE

Similarly to how we did with PCA, there are functions that can run a t-SNE directly on our SingleCellExperiment object. We will leave this exploration for you to do in the following exercises, but the basic code is very similar to that used with PCA. For example, the following would run t-SNE with default options:

sce <- runTSNE(sce)
plotTSNE(sce, colour_by = "SampleName")

One thing to note here is that, because of the computational demands of this algorithm, the general practice is to run it on the PCA results rather than on the full matrix of normalised counts. The reason is that, as we’ve seen earlier, the first few PCs should capture most of the biologically meaningful variance in the data, thus reducing the influence of technical noise in our analysis, as well as substantially reducing the computational time to run the analysis.

Let’s explore more about t-SNE in the following exercise.

See the accompanying worksheet for this session, which includes the code below for the exercise. We want to achieve the following:

  • Add a t-SNE projection of the data to our SCE object
  • Explore how the stochasticity of the algorithm affects the results
  • Customise our visualisation to display gene expression on top of the t-SNE
  • Explore how the main tuneable parameter of the algorithm - perplexity - affects the results
Run t-SNE
  1. Add the t-SNE result to the reducedDim slot of the SCE object.
  2. Name this reducedDim “TSNE_perplex50”.
  3. Set perplexity = 50 (which is the default if we don’t specify it).
  4. Run t-SNE based on the PCA we ran previously using the first 10 principal components.
  5. Plot the PCA results ussing ggcells and colour the points by SampleName.
set.seed(123) # set a random seed to ensure reproducibility
sce <- runTSNE(sce, 
               name = "TSNE_perplex50",
               perplexity = 50, 
               dimred = "PCA",
               n_dimred = 10)

ggcells(sce, aes(x = TSNE_perplex50.1, 
                 y = TSNE_perplex50.2, 
                 colour = SampleName)) +
  geom_point()
Part A
  • Re-run the algorithm but change the random seed number and generate a plot of the new reduction.
  • Do the results change dramatically between runs?
YOUR CODE HERE
Hint

Look at the set.seed() function

Answer
set.seed(321)
sce <- runTSNE(sce, 
               name = "TSNE_perplex50_seed321",
               perplexity = 50, 
               dimred = "PCA",
               n_dimred = 10)

ggcells(sce, aes(x = TSNE_perplex50_seed321.1, y = TSNE_perplex50_seed321.2, 
                 colour = SampleName)) +
  geom_point()
Part B

Instead of colouring by SampleName colour by expression of known cell markers:

  • CD79A (B cells)
  • CST3 (monocytes)
  • CD3D (T cells)
  • HBA1 (erythrocytes)
YOUR CODE HERE
Hint

You can replace what we colour by with any of the gene names in our dataset as they are stored as the rownames in our object.

Answer

e.g for CD79A:

ggcells(sce, aes(x = TSNE_perplex50_seed321.1, 
                 y = TSNE_perplex50_seed321.2, 
                 colour = CD79A)) +
  geom_point()
Part C

Facet these plots by SampleName to better understand where each marker is mostly expressed

YOUR CODE HERE
Hint

The function facet_wrap() can be used to modify ggplots as we did earlier.

Answer
ggcells(sce, aes(x = TSNE_perplex50_seed321.1, y = TSNE_perplex50_seed321.2, 
                 colour = CD79A)) +
  geom_point() +
  facet_wrap(~ SampleName)
Part D

Rerun the t-SNE using different perplexity values (for example 5 and 500)

  • Do you get tighter or looser clusters?
YOUR CODE HERE
Hint

You can replace values in the perplexity argument of the runTSNE() function.

Answer

First re-run the t-SNE with different perplexity levels

set.seed(321)
sce <- runTSNE(sce, 
               name = "TSNE_perplex5",
               perplexity = 5, 
               dimred = "PCA",
               n_dimred = 10)
sce <- runTSNE(sce, 
               name = "TSNE_perplex500",
               perplexity = 500, 
               dimred = "PCA",
               n_dimred = 10)

Then visualise using ggcells

tsne_5 <- ggcells(sce, aes(x = TSNE_perplex5.1, 
                           y = TSNE_perplex5.2, 
                           colour = SampleName)) +
              geom_point() +
              labs(title = "Perplexity 5")

tsne_50 <- ggcells(sce, aes(x = TSNE_perplex50.1, 
                            y = TSNE_perplex50.2, 
                            colour = SampleName)) +
              geom_point() +
              labs(title = "Perplexity 50")

tsne_500 <- ggcells(sce, aes(x = TSNE_perplex500.1,
                             y = TSNE_perplex500.2, 
                             colour = SampleName)) +
              geom_point() +
              labs(title = "Perplexity 500")

Some things to note from our data exploration:

  • Changing the random seed doesn’t seem to qualitatively change the results dramatically. The same groups of cells are still seen. However, their relative position on the axis might change from run to run.
  • Low perplexities will favour resolution of finer structure, possibly to the point that the visualization is compromised by random noise. Thus, it is advisable to test different perplexity values to ensure that the choice of perplexity does not drive the interpretation of the plot.
  • Exploring the expression of known marker genes (from literature or previous experiments we might have done) may help us interpret these plots and judge whether a particular choice of perplexity is adequate to represent the expected cell types in our data.

4.3 UMAP: Uniform Manifold Approximation and Projection

Simiarly to t-SNE, UMAP performs a non-linear transformation of the data to project it down to lower dimensions. One difference to t-SNE is that this method claims to preserve both local and global structures (i.e. the relative positions of clusters are, most of the times, meaningful). However, it is worth mentioning that there is some debate as to whether this is always the case, as is explored in this recent paper by Chari, Banerjee and Pachter (2021).

Compared to t-SNE, the UMAP visualization tends to have more compact visual clusters with more empty space between them. It also attempts to preserve more of the global structure than t -SNE. From a practical perspective, UMAP is much faster than t-SNE, which may be an important consideration for large datasets.

Similarly to t-SNE, since this is a non-linear method of dimensionality reduction, the results of a UMAP projection should be used for visualisation only and not for downstream analysis (such as cell clustering).

4.3.1 Running UMAP

Running UMAP is very similar to what we’ve seen already for PCA and t-SNE, only the function name changes:

set.seed(123)
sce <- runUMAP(sce)

plotUMAP(sce, colour_by = "SampleName")

Because this UMAP also involves a series of randomization steps, setting the random-generator seed (as we did above) is critical if we want to obtain reproducible results after each run.

Like t-SNE, UMAP has its own suite of hyperparameters that affect the visualization. Of these, the number of neighbors (n_neighbors) and the minimum distance between embedded points (min_dist) have the greatest effect on the granularity of the output. If these values are too low, random noise will be incorrectly treated as high-resolution structure, while values that are too high will discard fine structure altogether in favor of obtaining an accurate overview of the entire dataset. Again, it is a good idea to test a range of values for these parameters to ensure that they do not compromise any conclusions drawn from a UMAP plot.

See this interactive article that goes into more depth about the underlying methods, and explores the impacts of changing the n_neighbours and min_dist parameters: Understanding UMAP.

Similarly to what we did with t-SNE, we will explore this further in the following exercise.

The following code can be found in the accompanying worksheet for this section. Our main objectives are:

  • Add a UMAP projection of the data to our SCE object
  • Explore how the main tuneable parameter of the algorithm - neighbourhood size - affects the results
  • Compare how UMAP compares to t-SNE in terms of revealing structure in the data
Part A

Run the UMAP with 50 neighbours

set.seed(123) # set seed for reproducibility
sce <- runUMAP(sce, 
               name = "UMAP_neighbors50",
               dimred = "PCA",
               ******YOUR CODE HERE******)
Hint

Check out the runUMAP help page, there should be an argument for fixing the number of nearest neighbours.

Answer
set.seed(123) # set seed for reproducibility
sce <- runUMAP(sce, 
               name = "UMAP_neighbors50",
               dimred = "PCA",
               n_neighbors = 50)
Part B

Now visualise the resulting UMAP projection colouring the cells by sample.

YOUR CODE HERE
Hint

You can use the same code as for plotting the TSNE but replacing the arguments to display the UMAP output data.

Answer
ggcells(sce, aes(x = UMAP_neighbors50.1, y = UMAP_neighbors50.2, 
                 colour = SampleName)) +
  geom_point()
Part C

Run the UMAP with 5 and 500 neighbours and compare the results

YOUR CODE HERE
Hint

Change the number of neighbours argument in the runUMAP() function to see what changes.

Answer
set.seed(123) # set seed for reproducibility
sce <- runUMAP(sce, 
               name = "UMAP_neighbors5",
               dimred = "PCA",
               n_neighbors = 5)
sce <- runUMAP(sce, 
               name = "UMAP_neighbors500",
               dimred = "PCA",
               n_neighbors = 500)

ggcells(sce, aes(x = UMAP_neighbors5.1, y = UMAP_neighbors5.2, 
                 colour = SampleName)) +
  geom_point() +
  labs(title = "Neighbours = 5")
ggcells(sce, aes(x = UMAP_neighbors500.1, y = UMAP_neighbors500.2, 
                 colour = SampleName)) +
  geom_point() +
  labs(title = "Neighbours = 500")
Part D
  • Compare the UMAP projection with the t-SNE projections
  • Would you prefer one over the other?
YOUR CODE HERE
Hint

Redraw the TSNE and compare it with your UMAP.

Answer
ggcells(sce, aes(x = TSNE_perplex50.1, y = TSNE_perplex50.2, 
                 colour = SampleName)) +
  geom_point()
ggcells(sce, aes(x = UMAP_neighbors50.1, y = UMAP_neighbors50.2, 
                 colour = SampleName)) +
  geom_point()

To answer some of those questions:

  • Increasing the number of neighbours used in the analysis results in some apparent groups of cells being “merged” with each other. Conversely, picking too small a size creates many groups of cells, possibly picking up on some noise. However, qualitatively the results are relatively robust to the number of neighbours in this dataset, with the major groups of cells consistently appearing together.
  • Both t-SNE and UMAP show similar patterns in the data, so which one to pick in this case seems to be a matter of personal preference. However, UMAP is a more modern algorithm with better properties when it comes to keeping relative positions between clusters.

5 Save SCE object

Optionally, we can save our object, which now contains our dimensionality reduction analysis in it. This is useful as it will save time next time we pick up on our analysis.

saveRDS(sce, "Robjects/caron_dimRed.Rds")

6 Extra: feature selection and dimensionality reduction with Seurat

Using the Seurat package

Seurat is another very popular scRNA-seq analysis package and provides useful tools for analysing PCA loadings. We will convert our SCE object to a Seurat object to use these tools.

sce <- readRDS("R_objects/Caron_normalized.500.rds")

# convert SCE object to a Seurat object 
library(Seurat)
seurat_obj <- as.Seurat(sce, counts = "counts", data = "logcounts")

# identify the 2000 most highly-variable genes
seurat_hvgs <- FindVariableFeatures(seurat_obj, 
                                    selection.method = "vst", 
                                    nfeatures = 2000)

# scale the data for PCA for the highly variable genes 
seurat_obj <- ScaleData(seurat_obj, features = VariableFeatures(seurat_hvgs))

# run PCA 
seurat_obj <- RunPCA(seurat_obj, features = VariableFeatures(seurat_hvgs), 
                     verbose = FALSE)

# PCA Plot 
DimPlot(seurat_obj, reduction = "pca")

We can see that the PCA is similar to what we obtained before (there are some differences, which are related to how the variable genes were chosen in this case).

To visualise the top most correlated genes with each PC, we can use the following:

VizDimLoadings(seurat_obj, dims=1:2)

This shows the genes that have highest loading (correlation) with each PC axis. If we know something about the biology of the system we are studying, these may give some clues as to what is driving the separation of cells along each PC axis.

The loadings can also be visualised as a heatmap, showing the cells ordered by the PC scores and the genes ordered by their loading along that PC:

DimHeatmap(seurat_obj, dims = 1:2, cells = 500, balanced = TRUE)

Look at Seurat’s excellent documentation to learn more about this and other uses of this package.

7 Key Points and Resources

Key Points:

  • Dimensionality reduction methods allow us to represent high-dimensional data in lower dimensions, while retaining biological signal.
  • The most common methods used in scRNA-seq analysis are PCA, t-SNE and UMAP.
  • PCA uses a linear transformation of the data, which aims at defining new dimensions (axis) that capture most of the variance observed in the original data. This allows to reduce the dimension of our data from thousands of genes to 10-20 principal components.
  • The results of PCA can be used in downstream analysis such as cell clustering, trajectory analysis and even as input to non-linear dimensionality reduction methods such as t-SNE and UMAP.
  • t-SNE and UMAP are both non-linear methods of dimensionality reduction. They aim at keeping similar cells together and dissimilar clusters of cells apart from each other.
  • Because these methods are non-linear, they should only be used for data visualisation, and not for downstream analysis.

Further reading:

8 Session information

sessionInfo()
## R version 4.3.1 (2023-06-16)
## Platform: x86_64-apple-darwin20 (64-bit)
## Running under: macOS Monterey 12.6.8
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRblas.0.dylib 
## LAPACK: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0
## 
## locale:
## [1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
## 
## time zone: Europe/London
## tzcode source: internal
## 
## attached base packages:
## [1] stats4    stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
##  [1] SeuratObject_4.1.4          Seurat_4.3.0.1             
##  [3] patchwork_1.1.3             lubridate_1.9.2            
##  [5] forcats_1.0.0               stringr_1.5.0              
##  [7] dplyr_1.1.3                 purrr_1.0.2                
##  [9] readr_2.1.4                 tidyr_1.3.0                
## [11] tibble_3.2.1                tidyverse_2.0.0            
## [13] PCAtools_2.12.0             ggrepel_0.9.3              
## [15] scran_1.28.2                scater_1.28.0              
## [17] ggplot2_3.4.3               scuttle_1.10.2             
## [19] SingleCellExperiment_1.22.0 SummarizedExperiment_1.30.2
## [21] Biobase_2.60.0              GenomicRanges_1.52.0       
## [23] GenomeInfoDb_1.36.3         IRanges_2.34.1             
## [25] S4Vectors_0.38.1            BiocGenerics_0.46.0        
## [27] MatrixGenerics_1.12.3       matrixStats_1.0.0          
## 
## loaded via a namespace (and not attached):
##   [1] RcppAnnoy_0.0.21          splines_4.3.1            
##   [3] later_1.3.1               bitops_1.0-7             
##   [5] polyclip_1.10-4           lifecycle_1.0.3          
##   [7] edgeR_3.42.4              globals_0.16.2           
##   [9] lattice_0.21-8            MASS_7.3-60              
##  [11] magrittr_2.0.3            limma_3.56.2             
##  [13] plotly_4.10.2             sass_0.4.7               
##  [15] rmarkdown_2.24            jquerylib_0.1.4          
##  [17] yaml_2.3.7                metapod_1.8.0            
##  [19] httpuv_1.6.11             sctransform_0.4.0        
##  [21] spatstat.sparse_3.0-2     sp_2.0-0                 
##  [23] reticulate_1.31           cowplot_1.1.1            
##  [25] pbapply_1.7-2             RColorBrewer_1.1-3       
##  [27] abind_1.4-5               zlibbioc_1.46.0          
##  [29] Rtsne_0.16                RCurl_1.98-1.12          
##  [31] GenomeInfoDbData_1.2.10   irlba_2.3.5.1            
##  [33] spatstat.utils_3.0-3      listenv_0.9.0            
##  [35] goftest_1.2-3             spatstat.random_3.1-6    
##  [37] dqrng_0.3.1               fitdistrplus_1.1-11      
##  [39] parallelly_1.36.0         DelayedMatrixStats_1.22.6
##  [41] leiden_0.4.3              codetools_0.2-19         
##  [43] DelayedArray_0.26.7       tidyselect_1.2.0         
##  [45] farver_2.1.1              ScaledMatrix_1.8.1       
##  [47] viridis_0.6.4             spatstat.explore_3.2-3   
##  [49] jsonlite_1.8.7            BiocNeighbors_1.18.0     
##  [51] ellipsis_0.3.2            progressr_0.14.0         
##  [53] ggridges_0.5.4            survival_3.5-7           
##  [55] tools_4.3.1               ica_1.0-3                
##  [57] Rcpp_1.0.11               glue_1.6.2               
##  [59] gridExtra_2.3             xfun_0.40                
##  [61] withr_2.5.0               fastmap_1.1.1            
##  [63] bluster_1.10.0            fansi_1.0.4              
##  [65] digest_0.6.33             rsvd_1.0.5               
##  [67] timechange_0.2.0          R6_2.5.1                 
##  [69] mime_0.12                 colorspace_2.1-0         
##  [71] scattermore_1.2           tensor_1.5               
##  [73] spatstat.data_3.0-1       utf8_1.2.3               
##  [75] generics_0.1.3            data.table_1.14.8        
##  [77] httr_1.4.7                htmlwidgets_1.6.2        
##  [79] S4Arrays_1.0.6            uwot_0.1.16              
##  [81] pkgconfig_2.0.3           gtable_0.3.4             
##  [83] lmtest_0.9-40             XVector_0.40.0           
##  [85] htmltools_0.5.6           scales_1.2.1             
##  [87] png_0.1-8                 knitr_1.43               
##  [89] rstudioapi_0.15.0         tzdb_0.4.0               
##  [91] reshape2_1.4.4            nlme_3.1-163             
##  [93] cachem_1.0.8              zoo_1.8-12               
##  [95] KernSmooth_2.23-22        parallel_4.3.1           
##  [97] miniUI_0.1.1.1            vipor_0.4.5              
##  [99] pillar_1.9.0              grid_4.3.1               
## [101] vctrs_0.6.3               RANN_2.6.1               
## [103] promises_1.2.1            BiocSingular_1.16.0      
## [105] beachmat_2.16.0           xtable_1.8-4             
## [107] cluster_2.1.4             beeswarm_0.4.0           
## [109] evaluate_0.21             cli_3.6.1                
## [111] locfit_1.5-9.8            compiler_4.3.1           
## [113] rlang_1.1.1               crayon_1.5.2             
## [115] future.apply_1.11.0       labeling_0.4.3           
## [117] plyr_1.8.8                ggbeeswarm_0.7.2         
## [119] stringi_1.7.12            deldir_1.0-9             
## [121] viridisLite_0.4.2         BiocParallel_1.34.2      
## [123] munsell_0.5.0             lazyeval_0.2.2           
## [125] spatstat.geom_3.2-5       Matrix_1.6-1             
## [127] hms_1.1.3                 sparseMatrixStats_1.12.2 
## [129] future_1.33.0             statmod_1.5.0            
## [131] shiny_1.7.5               highr_0.10               
## [133] ROCR_1.0-11               igraph_1.5.1             
## [135] bslib_0.5.1