Exercise 10 — PCA, QC and Sample Structure in RNA-seq
Exercise 10 — PCA, QC and Sample Structure in RNA-seq
In real RNA-seq analysis, we never start with differential expression.
We first ask:
Do the samples behave as expected?
This is the role of:
- PCA (Principal Component Analysis)
- sample distance heatmaps
- clustering
These steps help detect:
- outliers
- batch effects
- mislabeled samples
- biological structure
Dataset
We reuse:
:contentReference[oaicite:0]{index=0}
from Exercise 8.
Part 1 — Variance Stabilization
Raw counts are not suitable for PCA.
Task 1.1
Create a transformed dataset:
vsd <- vst(dds)
Task 1.2 — Questions
- Why can’t we use raw counts for PCA?
- What does variance stabilization do conceptually?
Part 2 — Sample Distance Heatmap
Task 2.1
Compute distances:
sampleDists <- dist(t(assay(vsd)))
Task 2.2
Convert to matrix:
sampleDistMatrix <- as.matrix(sampleDists)
Task 2.3
Plot heatmap:
pheatmap(sampleDistMatrix)
Task 2.4 — Interpretation
Answer:
- Which samples cluster together?
- Do replicates cluster properly?
- Any signs of outliers?
Part 3 — PCA (Built-in DESeq2 method)
Task 3.1
Run PCA:
plotPCA(vsd, intgroup = "dex")
Task 3.2 — Questions
- What does PC1 represent?
- What does PC2 represent?
- Do treated vs untreated samples separate?
Task 3.3 — Variance Interpretation
If PCA shows:
- PC1 = 70%
- PC2 = 15%
What does this mean?
Part 4 — Custom PCA Plot (ggplot2)
Task 4.1
Extract PCA data:
pcaData <- plotPCA(vsd, intgroup = "dex", returnData = TRUE)
Task 4.2
Plot manually:
library(ggplot2)
ggplot(pcaData, aes(PC1, PC2, color = dex)) +
geom_point(size = 3)
Task 4.3 — Improve plot
Add:
- title
- axis labels
- theme
Task 4.4 — Interpretation
Answer:
- Are samples clearly separated?
- Which axis drives separation?
- Does biology or noise dominate structure?
Part 5 — Hierarchical Clustering
Task 5.1
Compute clustering:
hc <- hclust(sampleDists)
Task 5.2
Plot dendrogram:
plot(hc)
Task 5.3 — Questions
- Which samples cluster together?
- Are clusters consistent with treatment?
- Any unexpected grouping?
Part 6 — Outlier Detection
Scenario
Imagine one sample is far away in PCA space.
Task 6.1 — Questions
- Should you remove it immediately?
- What checks should you perform first?
- Possible biological vs technical reasons?
Part 7 — Biological Interpretation
Answer:
# Does treatment explain most variation?
# Are replicates consistent?
# Is there evidence of batch effects?
# Would you proceed to DESeq2 confidently?
# Why is QC before DE analysis critical?
Part 8 — Multi-panel Figure (Optional)
Create a combined figure:
- PCA plot
- sample distance heatmap
Hint:
library(patchwork)
Bonus
B1 — Colored PCA by sample type
Add shape or label for clarity.
B2 — Identify strongest genes driving PC1
Hint:
attr(plotPCA(vsd), "percentVar")
B3 — Biological reflection
Write:
- what drives variation in this dataset?
- is it treatment or something else?
- would you trust DESeq2 results if PCA looked random?
Key Takeaways
- PCA is a QC tool, not just visualization
- sample structure must be understood before DE analysis
- clustering reveals hidden batch effects
- variance stabilization is essential for meaningful PCA
- good RNA-seq analysis always starts with QC
| ⬅️ Previous Exercise | 🏠 Home | ➡️ Next Exercise |