Last Updated: March 23 2021, 5pm
Part 4: PCA and choice in number of PCS
Load libraries
library(Seurat)
library(ggplot2)
Load the Seurat object
load(file="pre_sample_corrected.RData")
experiment.aggregate
Now doing so for ‘real’
ScaleData - Scales and centers genes in the dataset. If variables are provided in vars.to.regress, they are individually regressed against each gene, and the resulting residuals are then scaled and centered unless otherwise specified. Here we regress out cell cycle results S.Score and G2M.Score, percentage mitochondria (percent.mito) and the number of features (nFeature_RNA).
experiment.aggregate <- ScaleData(
object = experiment.aggregate,
vars.to.regress = c("batchid", "S.Score", "G2M.Score", "percent.mito", "nFeature_RNA"))
Dimensionality reduction with PCA
Next we perform PCA (principal components analysis) on the scaled data.
?RunPCA
experiment.aggregate <- RunPCA(object = experiment.aggregate, npcs=100)
Seurat then provides a number of ways to visualize the PCA results
Visualize PCA loadings
VizDimLoadings(experiment.aggregate, dims = 1, ncol = 1) + theme_minimal(base_size = 8)
VizDimLoadings(experiment.aggregate, dims = 2, ncol = 1) + theme_minimal(base_size = 8)
Principal components plot
DimPlot(object = experiment.aggregate, reduction = "pca")
Draws a heatmap focusing on a principal component. Both cells and genes are sorted by their principal component scores. Allows for nice visualization of sources of heterogeneity in the dataset.
DimHeatmap(object = experiment.aggregate, dims = 1:6, cells = 500, balanced = TRUE)
DimHeatmap(object = experiment.aggregate, dims = 7:12, cells = 500, balanced = TRUE)
Questions
- Go back to the original data (rerun the load RData section) and then try modifying the ScaleData vars.to.regres, remove some variables, try adding in orig.ident? See how choices effect the pca plot
Selecting which PCs to use
To overcome the extensive technical noise in any single gene, Seurat clusters cells based on their PCA scores, with each PC essentially representing a metagene that combines information across a correlated gene set. Determining how many PCs to include downstream is therefore an important step.
ElbowPlot plots the standard deviations (or approximate singular values if running PCAFast) of the principle components for easy identification of an elbow in the graph. This elbow often corresponds well with the significant PCs and is much faster to run. This is the traditional approach to selecting principal components.
ElbowPlot(experiment.aggregate, ndims = 0:100)
The JackStraw function randomly permutes a subset of data, and calculates projected PCA scores for these ‘random’ genes, then compares the PCA scores for the ‘random’ genes with the observed PCA scores to determine statistical signifance. End result is a p-value for each gene’s association with each principal component. We identify significant PCs as those who have a strong enrichment of low p-value genes.
experiment.aggregate <- JackStraw(object = experiment.aggregate, dims = 100)
experiment.aggregate <- ScoreJackStraw(experiment.aggregate, dims = 1:100)
JackStrawPlot(object = experiment.aggregate, dims = 1:100) + theme(legend.position="bottom")
Finally, lets save the filtered and normalized data
save(experiment.aggregate, file="pca_sample_corrected.RData")
Get the next Rmd file
download.file("https://raw.githubusercontent.com/ucdavis-bioinformatics-training/2021-March-Single-Cell-RNA-Seq-Analysis/master/data_analysis/scRNA_Workshop-PART5.Rmd", "scRNA_Workshop-PART5.Rmd")
Session Information
sessionInfo()