R
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Data Structures
-
Section 4: Data Manipulation
-
Section 5: Visualization and Statistics
-
Section 6: Advanced R
-
Section 7: Practical Projects
-
Section 8: Interview Practice
-
Section 9: More Practice Exercises
-
Section 10: Shiny Apps in Depth
-
Section 11: More Data Wrangling
-
Section 12: Tidyverse Deep Dive
-
Section 13: Statistical Modeling Deep Dive
-
Section 14: Machine Learning in R
-
Section 15: R Visualization Deep Dive
-
Section 16: R Package Development Deep Dive
-
Section 17: R for Reproducible Research
-
Section 18: R and Databases
-
Section 19: R Performance Optimization
-
Section 20: Bioinformatics and Specialized R
-
Section 21: More Shiny Practice
-
Section 22: More Practice Exercises
-
Section 23: R Data Structures Deep Dive
-
Section 24: More Interview and Review
-
Section 25: R for Business Analytics
-
Section 26: R Text Mining and NLP
-
Section 27: R Spatial Data Analysis
-
Section 28: R Deep Learning
-
Section 29: Advanced Statistical Techniques
-
Section 30: R Object Systems Deep Dive
-
Section 31: R Environments and Metaprogramming
-
Section 32: R for Finance
-
Section 33: R for Clinical and Health Data
-
Section 34: More Shiny Advanced Practice
-
Section 35: R Data Cleaning Deep Dive
-
Section 36: R Reporting Automation
-
Section 37: More Practical Projects Round 2
-
Section 38: R Ecosystem and Career
-
Section 39: More Visualization Practice
-
Section 40: R for Experimentation
-
Section 41: R for Genomics and Bioinformatics Deep Dive
-
Section 42: R for Marketing Analytics
-
Section 43: R Data Import/Export Deep Dive
-
Section 44: R String Processing Deep Dive
-
Section 45: R for Actuarial and Insurance Analytics
-
Section 46: R Testing and Quality Assurance Deep Dive
-
Section 47: R Version Control and Collaboration
-
Section 48: R Advanced Functional Programming
-
Section 49: R for Supply Chain and Operations
-
Section 50: More Practice Exercises Round 3
-
Section 51: R Dashboards and BI Integration
-
Section 52: R Data Governance and Ethics
-
Section 53: More Modeling Practice
-
Section 54: R Final Capstone Projects
-
Section 55: R for Sports Analytics
-
Section 56: More Interview Practice Round 2
-
Section 57: R Networking and APIs
-
Section 58: R for Environmental Science
-
Section 59: R Notebook and Documentation Practices
-
Section 60: More Data Wrangling Mastery
-
Section 61: R for A/B Testing at Scale
-
Section 62: R Package Ecosystem Deep Dive
162: Differential Expression Analysis with DESeq2
Differential expression analysis can feel like a black box if you just follow a tutorial blindly. Today, we're going to walk through a real scenario: imagine we have RNA-seq data from a set of lung cancer cells. Half were treated with a new kinase inhibitor, and half were left as a control. We want to know which genes were significantly "turned down" or "turned up" because of that drug.
Getting our counts and sample info ready
For DESeq2 to work, you need two things: a matrix of raw integer counts (not normalized values!) and a data frame describing your samples. I've put together a small synthetic dataset here to keep things focused.
library(DESeq2)
# Raw counts: rows are genes, columns are samples
countData <- matrix(rnbinom(1000, mu=100, size=1), ncol=6)
colnames(countData) <- c("Ctrl1", "Ctrl2", "Ctrl3", "Trt1", "Trt2", "Trt3")
rownames(countData) <- paste0("Gene", 1:167) # A few genes for the example
# Sample metadata
colData <- data.frame(
condition = factor(c("Control", "Control", "Control", "Treated", "Treated", "Treated")),
row.names = colnames(countData)
)
Notice I wrapped condition in factor(). DESeq2 relies heavily on factors to understand the experimental design. If you leave them as strings, you're asking for a headache later.
Building the DESeqDataSet object
Now we wrap these into a DESeqDataSet. This is the core object where all the magic happens. We tell it the counts, the metadata, and the design formula—which basically tells R, "I want to see how the expression changes based on the condition."
dds <- DESeqDataSetFromMatrix(countData = countData,
colData = colData,
design = ~ condition)
The "Reference Level" trap
Here is where I almost messed up this analysis. I ran DESeq(dds) immediately, but I forgot that R assigns factor levels alphabetically by default. In our case, "Control" comes before "Treated," so it worked out. But if my groups were "Placebo" and "Control," R would have treated "Control" as the baseline and "Placebo" as the treatment, flipping the sign of all my Fold Change results.
I always explicitly set the reference level to avoid this ambiguity. It's a small step that saves hours of debugging later.
# Force "Control" to be the baseline
dds$condition <- relevel(dds$condition, ref = "Control")
# Now we run the actual differential expression pipeline
dds <- DESeq(dds)
Extracting the meaningful genes
The DESeq() function does a lot under the hood: it estimates size factors for normalization, estimates dispersion, and fits a generalized linear model. To actually see the results, we use results(). I usually filter for an adjusted p-value (padj) of less than 0.05 to account for the thousands of tests we're running simultaneously.
res <- results(dds)
# Let's look at the most significant genes
resOrdered <- res[order(res$padj), ]
head(resOrdered)
If you see a positive log2FoldChange, the gene is upregulated in the Treated group compared to the Control. A negative value means it was suppressed. Simple, but incredibly powerful.
📋 Practical Task
Identify Drug-Responsive Genes from a New Dataset
You have been handed a new count matrix lung_counts and a metadata table lung_meta. The metadata contains a column called treatment with two levels: "DMSO" (the vehicle control) and "Drug_A".
Write a script to:
- Create a
DESeqDataSetobject usingtreatmentas the design. - Explicitly set
"DMSO"as the reference level for thetreatmentfactor. - Run the DESeq pipeline and extract the results.
- Create a new data frame called
significant_genesthat contains only the genes with an adjusted p-value (padj) less than 0.01.
There are no comments for now.