eResearch - single cell RNA-Seq analysis (10x Genomics)
- 1 Aim
- 2 Requirements
- 3 1. nfcore/scrnaseq
- 4 2. Downstream analysis in R
- 5 3. Analysis using the seurat package
- 5.1 3a. Select a sample to work with and import the data into R
- 5.2 3b. Identify markers in cells
- 5.3 3c. Processing expression data (dimensionality reduction)
- 5.4 3d. Plot of highly variable genes
- 5.5 3e. PCA, UMAP and t-SNE plots (plotting dimensionality reduction data)
- 5.6 3f. Remove low quality or outlier cells
- 5.7 3g. Visualise gene expression by marker
- 5.8 3h. Remove non-target cells
- 5.9 3i. Clustering by gene expression
- 5.10 3j. Plot marker expression before and after filtration
- 5.11 3k. Output filtered results
- 6 4. Aggregate clustering
- 7 5. Differential expression
- 7.1 5a. Select a sample to work with and import sample data
- 7.2 5b. Explanation of differential expression strategies
- 7.3 5c. Differential expression: one cluster vs all other cells
- 7.4 5d. Differential expression: one cluster vs another cluster
- 7.5 5e. Differential expression: every cluster vs all other cells
Aim
Analyse 10x Genomics* single cell RNA-Seq data.
This analysis workflow is split into 2 main sections: 1) ‘Upstream’ analysis on QUT’s HPC (high performance compute cluster) using a Nextflow workflow, nfcore/scrnaseq, 2) ‘Downstream’ analysis in R, primarily using the package seurat.
*Note: this workflow can be adapted to work with scRNA-Seq datasets generated by other sequencing technologies than 10x Genomics.
Requirements
A HPC account. If you do not have one, please request one here.
Access to an rVDI virtual desktop machine with 64GB RAM. For information about rVDI and requesting a virtual machine, see here.
Nextflow installed on your HPC home account. If you haven’t already installed Nextflow, do so by following the guide here.
Your scRNA-Seq data (fastq files) are on the HPC. If you are having difficulties transferring them to the HPC, submit a support ticket here.
Connect to an rVDI virtual desktop machine
Ensure you already have access to a 64GB RAM rVDI virtual machine, or request access by following the guide here.
scRNA-Seq datasets are often very large, requiring a lot of memory to run. Downstream analysis is run in R, on a Windows machine. Your PC is unlikely to have enough RAM, thus we’re using virtual machines with 64GB RAM. In addition, you can run the rest of this analysis in the virtual machine.
To access and run your rVDI virtual desktop:
Go to https://rvdi.qut.edu.au/
Click on ‘VMware Horizon HTML Access’
Log on with your QUT username and password
*NOTE: you need to be connected to the QUT network first, either being on campus or connecting remotely via VPN.
Set up your rVDI environment
Now that you’ve connected to an rVDI virtual machine, you’ll need to set it up to:
Connect to your home directory on the HPC, so you can access your data files
Install R and RStudio for running the Seurat analysis
Connect to your HPC home directory
Using the Windows File Explorer, we can map our HPC Home folder and the shared Work folder to drive letters. Here we will map our home drive to ‘H' and our shared work directory to ‘W’.
Open File Explorer (folder icon in the Windows task bar).
Right click “This PC” and choose Map Network Drive.
Home drive: select ‘H' as the drive letter, then copy and paste \\hpc-fs\home into the Folder box. Click 'Finish’.
Work drive: In file Explorer, again right click “This PC” and choose Map Network Drive. select ‘W' as the drive letter, then copy and paste \\hpc-fs\work into the Folder box. Click 'Finish’.
To see this demonstrated watch this video:
Transfer Files to the HPC - QUT MediaHub
Now you’ll be able to browse and copy files between your virtual Windows machine and the HPC.
Installing R and RStudio
We’ll be analysing our data in an R environment, so first we need to install R and RStudio on the rVDI machine. You will be copying and pasting script from this workflow into RStudio.
Download and install R, following the default prompts:
Download R-4.3.2 for Windows. The R-project for statistical computing.
Download and install RStudio, following the default prompts:
https://posit.co/download/rstudio-desktop/
1. nfcore/scrnaseq
10x scRNA-Seq data is typically processed using various Cell Ranger software tools. These (and other) tools have been combined in an nfcore Nextflow workflow called scrnaseq.
NOTE: sometimes your 10x data has already been processed by your sequencing company, using Cell Ranger. In this case you can skip the nfcore/scrnaseq analysis and go straight to the downstream Seurat analysis.
1a. Workflow overview
As can be seen in the workflow below, there are several workflow options. The one we’ll be using is the complete Cell Ranger workflow, using the tools cellranger mkgtf and cellranger mkref for reference genome preparation and cellranger count for both aligning sequences to the reference genome and quantifying expression per gene per cell, for each sample.
1b. Creating a samplesheet
To run, nfcore/scrnaseq requires: 1) Your data files in gzipped fastq format (*fastq.gz), 2) A samplesheet that lists the sample names and the fastq files associated with each sample.
See this page for the required structure and content of the samplesheet:
Because sample names are specific to a project, and typically a single samples is associated with multiple fastq files, you will need to manually create this samplesheet. You can create it in Excel, then save it as a comma-separated file called ‘samplesheet.csv’, then copy this up to the HPC. Or you can manually create it on the command-line in the HPC, using a text editor such as nano.
Note: in the samplesheet, you must provide the full path for the fastq files. This is not shown in the nfcore/scrnaseq usage guide.
You can find the full path by typing pwd
in the command-line, while in the directory containing your fastq files.
For example, if your fastq files are in /home/username/mydata
and you have a fastq file called Liver_S2_L001_I1_001.fastq.gz
then the full path for that fastq file would be: /home/username/mydata/Liver_S2_L001_R1.fastq.gz
An example datasheet, with 2 samples (Liver and Kidneys), where each sample has 2 fastq files associated with it, might look something like this:
sample, | fastq_1, | fastq_2, |
---|---|---|
Liver, | /home/username/mydata/Liver_L001_R1.fastq.gz, | /home/username/mydata/Liver_L001_R1.fastq.gz, |
Liver, | /home/username/mydata/Liver_L002_R1.fastq.gz, | /home/username/mydata/Liver_L002_R1.fastq.gz, |
Kidney, | /home/username/mydata/Kidney_L001_R1.fastq.gz, | /home/username/mydata/Kidney_L001_R1.fastq.gz, |
Kidney, | /home/username/mydata/Kidney_L001_R1.fastq.gz, | /home/username/mydata/Kidney_L001_R1.fastq.gz, |
1c. Running nfcore/scrnaseq as a PBS script
2. Downstream analysis in R
We’ll be using various R packages to analyse our 10x single cell data. In this section we’ll create an R script in RStudio and install the required R packages.
2a. Open RStudio and create a new R script
RStudio is a GUI (graphical user interface) for R. It makes navigating R easier.
Open RStudio (you can type it in the Windows search bar)
Create a new R script: ‘File’ → “New File” → “R script”
Save this script where your samples folders are (‘File’ → ‘Save’). These should be on your H or W drive. Save the script file as ‘scrnaseq.R’
In the following sections you will be copying and running the R code into your scrnaseq.R script.
Cell Ranger (and nfcore/scrnaseq) generates a default folder and file output structure. There will be a main folder that contains all the sample subfolders (NOTE: this is where you must save your R script). Each sample folder will have an ‘outs’ subfolder. This ‘outs’ folder contains a ‘filtered_feature_bc_matrix’ folder, which contains the files that Seurat uses in its analysis.
If you have already created this script in a previous session, you can just re-open it by browsing to the location (somewhere in your mapped H drive, probably) and double clicking on it.
2b. Set your working directory
In R, your working directory is the location from which your data files are read in to R and any output files are deposited. For our purposes we need to set the working directory to the location on the HPC where your scRNASeq dataset is.
Most of the scripts can be run without modification, but there are a few lines that you will need to change, such as the working directory (which will differ for each researcher’s dataset).
When you see **USER INPUT** in the script, this means you have to modify the line below this.
You can manually set your working directory in RStudio by selecting ‘Session' -> 'Set working directory' -> 'Choose directory'. Choose the same directory as you saved your scrnaseq.R script, previous section. This will output the setwd(...)
command with your working directory into the console window (bottom left panel). Copy this command to replace the default setwd(...)
line in your R script.
Copy and paste the following code into the R script you just created, then run the code (highlight all the code in your R script, then press the run button).
#### 2b. Set your working directory ####
# Change the below to the directory that contains your sample folders (you may have to browse H or W drive to find this)
# **USER INPUT**
setwd("H:/sam_dando/dataset1/count")
# You can see the sample subdirectories by:
list.dirs(full.names = F, recursive = F)
# You should see directories that are names after your samples.
# If you don't see this, browse through your H or W drives to find the correct path for your sample directories.
2c. Installing packages
This will install all the required packages and dependencies and may take 30 minutes or more to complete. It may prompt you occasionally to update packages - select 'a' for all if/when this occurs.
#### 2c. Installing required packages ####
# This section only needs to be run once on a computer.
# One the packages are installed, they need to be loaded every time they will be used (next section)
# Create vector of required package names
bioconductor_packages <- c("clusterProfiler", "pathview", "AnnotationHub", "org.Mm.eg.db")
cran_packages <- c("Seurat", "patchwork", "ggplot2", "tidyverse", "viridis", "plyr", "readxl", "scales")
# Compares installed packages to above packages and returns a vector of missing packages
new_packages <- bioconductor_packages[!(bioconductor_packages %in% installed.packages()[,"Package"])]
new_cran_packages <- cran_packages[!(cran_packages %in% installed.packages()[,"Package"])]
# Install missing bioconductor packages
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install(new_packages)
# Install missing cran packages
if (length(new_cran_packages)) install.packages(new_cran_packages, repos = "http://cran.us.r-project.org")
# Update all installed packages to the latest version
update.packages(bioconductor_packages, ask = FALSE)
update.packages(cran_packages, ask = FALSE, repos = "http://cran.us.r-project.org")
2d. Loading packages
#### 2d. Loading required packages ####
# This section needs to be run every time
# Load packages
bioconductor_packages <- c("clusterProfiler", "pathview", "AnnotationHub", "org.Mm.eg.db")
cran_packages <- c("Seurat", "patchwork", "ggplot2", "tidyverse", "viridis", "plyr", "readxl", "scales")
lapply(cran_packages, require, character.only = TRUE)
lapply(bioconductor_packages, require, character.only = TRUE)
3. Analysis using the seurat package
Seurat is:
A toolkit for quality control, analysis, and exploration of single cell RNA sequencing data. 'Seurat' aims to enable users to identify and interpret sources of heterogeneity from single cell transcriptomic measurements, and to integrate diverse types of single cell data.
In this section 3 we’ll be using a variety of Seurat functions to analyse our scRNA-Seq data.
3a. Select a sample to work with and import the data into R
################################################################
#TO DO: Make sure the folder structure provided follows the Cell ranger output format.
Clarify how the files will be output from the upstream Nextflow pipeline and update text accordingly to guide to the output folder.
################################################################
In this section we’ll choose one of our samples to analyse, then import that sample’s scRNA-Seq dataset into R. You can re-run this, and following sections, for each sample dataset that you have. Just replace the sample name in sample <- "xxxxxx"
below.
3b. Identify markers in cells
Now we're going to identify individual markers that were present (i.e. were expressed) in our dataset.
IMPORTANT: the gene symbols you provide in the following section have to exactly match the gene symbols in your dataset (including capitalisation).
Gene symbols are more like 'common names' and can vary between databases.
Your main gene identifiers are Ensembl IDs and we need to find the gene symbols that match these Ensembl IDs.
For example, the gene P2ry12 is also called ADPG-R, BDPLT8, HORK3 and various other IDs, depending on the database it's listed in.
In the Ensemble database it's listed as P2ry12 (not P2RY12, remember, case matters) and matches Ensembl ID ENSMUSG00000036353.
For this reason it's advisable to first search the Ensembl website for your markers of interest and for your organism, to ensure you are providing gene symbols that match the Ensembl IDs.
https://asia.ensembl.org/Mus_musculus/Info/Index
3c. Processing expression data (dimensionality reduction)
There are a variety of methods to visualise expression in single cell data. The most commonly used methods - PCA, t-SNE and UMAP - involve 'dimensionality', i.e. converting expression to x-n dimensions (which can then be plotted) based on gene expression per cell.
Seurat can generate and store PCA, t-SNE and UMAP data in the Seurat object we created previously ('mat2'), but first the raw data needs to be processed in a variety of ways:
Normalise the data by log transformation
Identify genes that exhibit high cell-to-cell variation
Scale the data so that highly expressed genes don't dominate the visual representation of expression
Perform the linear dimensional reduction that converts expression to dimensions
Plot the x-y dimension data (i.e. first 2 dimensions)
The first 4 steps are completed in the code below (this may take a few minutes to run)
3d. Plot of highly variable genes
################################################################
Question for Paul:
The following command returns a warning.
################################################################
Using the FindVariableFeatures
results, we can visualise the most highly variable genes, including a count of variable and non variable genes in your dataset. The below code outputs the top 10 genes, but you can adjust this number as desired (i.e. in top_genes <- head(VariableFeatures(mat3), 10)
change 10
to another number).
NOTE In the below plot you can change a number of parameters to modify the plot to look how you like. This can be done for any of the plots in these notebooks. In the plot below you can change:
Dot size: pt.size = 2
. Increase or decrease the number to increase or decrease dot size.
Dot colours: cols = c("black", "firebrick"))
. Change the colours to whatever you like. A list of R colour names is here: http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf
Theme: theme_bw()
. There are several default plot themes you can choose from, that change a variety of plot parameters. See here: Complete themes — ggtheme
Axis text size: theme(text = element_text(size = 17))
. There are a large number of parameters that can be modified with theme()
. Here we've just changed the axis text to size 17. See here for other parameters that can be changed with theme()
: Modify components of a theme — theme
Seurat plots are based on the ggplot package. There are a multitude of other modifications you can make to a ggplot, too many to describe in this notebook. But there are plenty of online guides on how to modify ggplot plots. Here's an example: Be Awesome in ggplot2: A Practical Guide to be Highly Effective - R software and data visualization - Easy Guides - Wiki - STHDA
3e. PCA, UMAP and t-SNE plots (plotting dimensionality reduction data)
################################################################
Question for Paul:
one warning comes up when running RunUMAP
:
################################################################
In the section 3c we ran dimensionality reduction based on Principal Component Analysis (PCA).
There are other dimensionality reduction methods that are more commonly presented in singe cell papers, particularly Uniform Manifold Approximation and Projection (UMAP and t-distributed stochastic neighbour embedding (t-SNE) dimensionality reduction, so here we will generate and visualise dimensionality reduction from all 3 methods (PCA, UMAP, t-SNE).
3f. Remove low quality or outlier cells
################################################################
Question for Paul:
################################################################
From the Seurat website:
Seurat allows you to easily explore QC metrics and filter cells based on any user-defined criteria. A few QC metrics commonly used by the community include
- Low-quality cells or empty droplets will often have very few genes
- Cell doublets or multiplets may exhibit an aberrantly high gene count
So in this section we can filter out cells that have very low and very high gene counts.
First you’ll need to visualise the spread of genes and reads using a violin plot and/or scatter plot.
You can then choose to filter out the top and bottom 'outliers' based on these violin and scatter plots, by entering a maximum and minimum nFeature_RNA
below.
This max/min number can vary greatly depending on the sequencing depth of your samples and other factors. Use the violin plot to guide your decision.
For example, if your violin plot looked like the below figure, a reasonable nFeature_RNA
maximum would be 4000 and a minimum of 200. This would remove outliers.
3g. Visualise gene expression by marker
In the section 3e (plotting dimensionality reduction data) we visualised total expression, i.e. all genes within each cell.
In this section we will visualise expression for specific markers within each cell, using the same dimensionality reduction data (PCA, UMAP and t-SNE) that we generated in the previous section.
This has a variety of uses: to identify patterns of differential expression between cells for specific markers, identify 'non-target' cells - i.e. expression of markers that are known to be not expressed in target cells, marker-based heterogeneity of expression, etc.
In section 2f you selected a set of markers. To confirm which markers they are:
If you wish to plot a different set of markers, you can do so by changing the set of markers in the markers <- c(..)
code and re-running that line. You can choose 1 marker, or as many as you like. Be aware though that there will be a plot generated for every marker provided.
Now generate the plots. You can change the colours in the plots (cols = c("red", "lightgrey")
and the point size (pt.size = 1
). Note that the default colours show the lowest expression in red. This is so you can more easily see which cells don't express the diagnostic markers.
In addition to the dimensionality reduction plots, you can visualise expression for your selected markers with a heatmap (note: this requires at least 2 markers, preferably several, to be visually meaningful). You can change the colour range as you like by providing a high, centre and low colour (scale_fill_gradientn(colors = c("darkorange", "floralwhite", "dodgerblue4"))
)
3h. Remove non-target cells
In this section we will remove any 'non-target' cells from our dataset. 'Non-target' cells are defined as those with 0 reads (i.e. 0 expression) for our marker(s) of choice.
If you don't want to remove any cells based on expression of specific markers, skip this section
3i. Clustering by gene expression
This section examines clustering by gene expression similarity for each sample. PCA, t-SNE and UMAP plots are used to visualize the gene expression patterns and clusters.
This section is split into three subsections - “1) Choosing the correct resolution”, where you’ll use the clustree tool to identify the optimal number of clusters, and “2) Calculate the clusters” where you’ll use the clustree results to generate optimised clusters, and “3) Plot the results“, where you’ll generate 'before and after' plots, to visualise how filtration (inc. removing the non-target cells) changed the data structure.
You can use this section to examine if your cell filtration had a meaningful effect on your data structure. If it didn't, you may want to choose a different set of markers or filtering parameters to filter with.
1) Choosing the correct resolution
A cluster represents a unique group of cells, based on gene expression patterns. But what consitutes 'unique'? When you calculate the clustering (using Seurat's FindClusters()
function), it's important to use the correct resolution
score to generate accurate, biologically meaningful clusters. Using a lower resolution
score will generate fewer clusters (but you risk combining two clusters that should be distinct), a higher score will generate more clusters (but you risk falsely splitting a biologically relevant cluster of cells). Every single cell dataset is different (cell population similarity, sequencing depth, etc) and as such the optimal resolution
score needs to be chosen for each dataset.
The package clustree generates a tree based on multiple resolution
scores, which can help you in picking the optimal score.
Read the clustree manual to understand how to interpret the generated tree: Plotting clustering trees
2) Calculate the clusters
In this subsection we’ll re-cluster the data based on the clustree results.
Choose the the resolution = xx
score in FindClusters()
to the optimal resolution that we can see in the clustree plot (e.g. 0.5, or 0.6, etc.).
3) Plot the results
Here we’ll plot ‘before and after’ filtration plots, to examine how filtration affected the data structure.
We’ll plot all clusters, for PCA, t-SNE and UMAP, and you can also choose an individual cluster to plot for any of these dimensionality reduction plot types.
3j. Plot marker expression before and after filtration
Then we can see how the expression of specific markers changed before and after filtration.
3k. Output filtered results
Now will export the filtered dataset (non-target cells removed, other filtration applied), for analysis in the next sections of this analysis workflow.
You need to run this entire '3. Analysis using the seurat package' section once for every sample you have, because the other sections use the output created here for each sample.
This will create a file called <samplename>_seurat_filtered.rds
in your working directory. This contains all the data in your mat3_filt
object, including raw counts, filtered counts, gene and sample IDs, dimensionality reduction data, etc.
4. Aggregate clustering
This section of the report involves combining (aggregating) two or more sample datasets. This allows a direct comparison of these samples, rather than analysing them separately.
IMPORTANT: To run this section, you must have processed all the samples you wish to combine in the '3. Analysis using the seurat package' section. This means that outliers and non-target cells have already been identified and removed. Section 3 only has to run once for each sample, as it outputs a datafile for each sample that is imported into this section.
ALSO IMPORTANT: Aggregate datasets can be very large and utilise a lot of memory. Keep an eye on your memory usage (in the Environment tab in RStudio). If you use all available memory your rVDI machine may crash.
4a. Choose samples to aggregate and import data
Running your sample through section 3 created a datafile called <samplename>_seurat_filtered.rds
, so if your sample was called 'liver', the file would be liver_seurat_filtered.rds
.
These datafiles will be in your working directories.
4b. Processing expression data (dimensionality reduction)
This is essentially a repeat of dimensionality reduction for individual samples in section 3, so see that section for details. The following steps are required to generate dimensionality reduction analysis (PCA, T-SNE and UMAP) for the aggregate data.
NOTE: this regenerates clustering based on the combined data. Any plots and analysis in this section are therefore based on this, rather than the individual sample clustering. If you wish to compare clustering per sample, you'll need to use the results in section 3, where you analysed each sample separately.
4c. PCA, UMAP and t-SNE plots (plotting dimensionality reduction data)
4d. Visualise gene expression by marker
This section allows you to produce a side-by-side visualisation of gene expression for your aggregated samples, for specific genes.
4e. Differential expression between samples
In the next section (section 5) we examine differential expression between clusters in individual samples. In this section we examine differential expression between samples.
We’ll be using the Seurat function FindMarkers
to identify differentially expressed genes. This may require some modification (in particular the logfc.threshold
parameter), depending on how many DE genes in your dataset and how significant they are.
By default, we’ve set logfc.threshold = 0.2
. This only tests genes with at least 0.2 log fold difference in expression and speeds up the analysis considerably. But it could also remove some significant DE genes.
To test for this, look at the bottom 6 genes (ordered by p value) by running the tail(DE_genes)
command (in the script below). If these genes are all non-significant (i.e. p_val_adj > 0.05) then you have captured all significant genes. If all these genes are significant (p_val_adj < 0.05) then re-run FindMarkers()
with logfc.threshold = 0.1
. This will take much longer to run, but should then capture all DE genes (if not, reduce logfc.threshold = 0
, but will take a very long time to run).
5. Differential expression
This section examines significantly differentially expressed genes for each cluster, for a selected sample. Significantly DE genes are those with false-discovery adjusted p values of < 0.05.
IMPORTANT: To run this section, you must have processed your sample(s) by completing the '3.Analysis using the seurat package' section. That section removes outliers and non-target cells and identifies clusters. This differential expression analysis uses these filtered cells and cluster information. Section 3 only has to run once for each sample, as it outputs a datafile for each sample that is imported into this section.
5a. Select a sample to work with and import sample data
5b. Explanation of differential expression strategies
Bulk RNA-Seq usually examines differentially expressed genes between two treatments or tissues. scRNA-Seq is somewhat different, in that it is based on heterogeneity of gene expression within a group of cells, so differential expression can also be examined between clusters.
Given that there are usually multiple clusters, and differential expression analysis requires a comparison between two groups, there are different strategies used to combine the two group analysis with the multiple cluster analysis. Three strategies included in this workflow include:
Comparing a single cluster to all other clusters/cells (section 5c). This enables one to examine how gene expression in a single cluster differs from the entire dataset.
Comparing one cluster to another cluster (section 5d). This requires two clusters being initially chosen, then examining how gene expression differs between them. Note that to do every possible combination of cluster comparisons may require running this section many times. E.g. if your sample has 8 clusters, this represents 7+6+5+4+3+2+1 = you would have to run that section 28 times to compare every cluster to every other cluster.
Doing a batch comparison of each cluster to all other clusters/cells (section 5e). Seurat has a function (
FindAllMarkers()
) to accomplish this, but this can take some time to run, as multiple clusters are being compared at once, and it also produces a multilevel dataset. This is an alternative to running each cluster one at a time, as in section 5c.
IMPORTANT: the first cluster you choose in each analysis below represents the baseline or control group. Genes are either upregulated (i.e. positive log fold change) or downregulated (-ve lfc) in comparison to this baseline group. This means that any downregulated genes (i.e. negative log fold change) are more highly expressed in this baseline cluster, but any upregulated genes (+ lfc) are more highly expressed in the cluster(s) being compared to this baseline cluster.
5c. Differential expression: one cluster vs all other cells
In this section you'll select a single cluster, then compare gene expression in this cluster to all other combined clusters. Any upregulated genes (positive log fold change) are more highly expressed in the combined clusters.
5d. Differential expression: one cluster vs another cluster
5e. Differential expression: every cluster vs all other cells