Before you start
- You can read a little R - variables, functions, loading packages. (New to this? Start with the R series or the Introduction.)
- A free Google account, so you can run the code in Google Colab.
- Curiosity. You do not need to understand the math behind the statistics yet - we'll point at it and keep moving.
Learning objectives
By the end of this lesson you will be able to: load a gene-count matrix, run a differential-expression analysis with DESeq2, and read which genes changed, in which direction, and how confidently.
The question we're actually answering
Before any code, let's be clear on what RNA-seq is for. Every cell carries the same DNA, but at any moment it's only reading some of those genes. The genes a cell is actively "reading" get transcribed into RNA. So if you measure how much RNA there is for each gene, you get a snapshot of what the cell is actually doing.
RNA-seq is the technology that counts that RNA, gene by gene, across thousands of genes at once. The classic experiment compares two groups - say, untreated cells vs. cells given a drug - and asks a deceptively simple question:
"Which genes change their activity between the two conditions?"
That's differential expression analysis, and it's the workhorse of modern molecular biology - used to understand diseases, find drug targets, and figure out how cells respond to their environment. That's exactly what you're about to do.
Decode the jargonCount matrix
The starting point of most RNA-seq analyses is a big table: one row per gene, one column per sample, and each cell holds a whole number - how many RNA fragments ("reads") were counted for that gene in that sample. Higher number = the gene was more active. Getting from raw sequencer output to this table (alignment and counting) is its own topic; here we start from the count matrix, which is where most analyses really begin.
The plan
Five steps. Each one is a few lines of code, and we'll explain what every line is doing and - more importantly - why.
- Set up - install one package.
- Load the data - a count matrix and a table describing the samples.
- Run the analysis - let DESeq2 do the heavy statistical lifting.
- Read the results - find the genes that changed.
- Visualize - draw a volcano plot, the signature figure of RNA-seq.
โถ Code along in Google Colab
Open a free notebook, set the runtime to R, and run each block below. Nothing to install on your computer. The one-time DESeq2 install takes about 3 to 5 minutes, which is normal, so let it finish before moving on.
1Set up
We'll use DESeq2, one of the most widely used R / Bioconductor packages for differential expression. Make sure your Colab notebook is set to the R runtime (Runtime โ Change runtime type โ R), then install it once:
# Installing Bioconductor packages takes a few minutes the first time - grab a coffee. if (!requireNamespace("BiocManager", quietly = TRUE)) install.packages("BiocManager") BiocManager::install("DESeq2", update = FALSE, ask = FALSE)
Then load the library:
library(DESeq2)
2Load the data
We'll use a real published experiment, the airway study (Himes et al., 2014, GEO accession GSE52778). Researchers took airway smooth-muscle cells from 4 donors and split each donor's cells in two: one half untreated, the other treated with dexamethasone, a steroid used to treat asthma. That gives 8 samples. It is one of the most widely used teaching datasets for exactly this workflow.
BioNexus keeps a copy as three small files, so it loads in seconds with nothing extra to install: a count matrix (genes ร samples), a sample table saying which donor each sample came from and whether it was treated, and a gene table that turns gene IDs into readable names.
base <- "https://raw.githubusercontent.com/MennahBassiouny/bionexus/main/" # Count matrix: rows = genes (Ensembl IDs), columns = the 8 samples counts <- as.matrix(read.csv(paste0(base, "airway-counts.csv"), row.names = 1)) # Sample table: which donor (cell) and whether it was treated (dex) samples <- read.csv(paste0(base, "airway-samples.csv"), row.names = 1) # Gene names, so results show DUSP1 instead of ENSG00000120129 genes <- read.csv(paste0(base, "airway-genes.csv"), row.names = 1) samples$cell <- factor(samples$cell) samples$dex <- factor(samples$dex, levels = c("untrt", "trt")) # untreated = baseline samples <- samples[colnames(counts), ] # same order as the count columns dim(counts) # genes x samples: about 33,000 x 8 head(counts) samples
Take a moment to actually look at head(counts) and samples. You should see 4 donors (the cell column), each with one untrt and one trt sample. Getting comfortable looking at your data before analyzing it is one of the most underrated habits in bioinformatics: most real-world mistakes are caught just by eyeballing the table.
Decode the jargonNormalization
You can't compare raw counts directly between samples, because some samples are sequenced more deeply than others (more total reads), making every gene look "higher" just by accident. DESeq2 normalizes to correct for this, adjusting so that differences reflect real biology, not how much data each sample happened to get. The good news: it does this for you automatically in the next step.
3Run the analysis
This is where DESeq2 earns its reputation. In two lines it normalizes the data, models the counts statistically, and prepares everything needed to test each gene.
# Build the DESeq2 dataset. The design is the question we're asking. dds <- DESeqDataSetFromMatrix(countData = counts, colData = samples, design = ~ cell + dex) dds <- DESeq(dds) # fit the model - this does the real work
The design = ~ cell + dex part is you telling DESeq2: "the thing I care about is the treatment (dex), but remember that samples come in pairs from the same donor (cell)." In effect, each treated sample is compared with its own untreated partner, so differences between donors do not get mistaken for noise. That single line frames the entire analysis.
4Read the results
Now we ask, for every gene: is the difference between treated and untreated big enough, and consistent enough across the 4 donors, to be real?
# compare treated (trt) vs untreated (untrt) res <- results(dds, contrast = c("dex", "trt", "untrt"), alpha = 0.05) summary(res) # how many genes went up / down res$symbol <- genes[rownames(res), "symbol"] # add readable gene names res <- res[order(res$padj), ] # sort by adjusted p-value head(res, 10) # the 10 most convincing hits
The results table has a few columns that matter enormously:
- log2FoldChange - how much the gene changed, on a log2 scale. +1 means it doubled in treated vs untreated; โ1 means it halved. Sign tells you direction, size tells you magnitude.
- pvalue - the probability of seeing a change at least this big if the gene truly did not change.
- padj - the p-value adjusted for the fact that we tested thousands of genes at once. This is the one you trust, not the raw p-value.
What you should see: about 4,000 genes with padj < 0.05 (roughly 2,200 up and 1,800 down). Near the top of the list are genes like DUSP1, KLF15 and PER1. These are well-known genes switched on by steroids, so finding them is a sign the pipeline worked: a correct analysis recovers known biology. You will also notice that about half the genes have NA in padj. Those had counts too low to test, and DESeq2 set them aside on purpose (more in the next lesson).
Decode the jargonWhy padj, not pvalue?
If you test 10,000 genes and call anything with p < 0.05 a "hit," you could get up to ~500 false positives by chance alone (if no gene truly changed). Adjusting for multiple testing (here, the Benjamini-Hochberg method) controls that flood of false alarms. Rule of thumb: a gene is a credible hit if padj < 0.05. We'll devote a whole lesson to this idea in Track 5 - it's that important.
5Visualize: the volcano plot
The volcano plot is the iconic RNA-seq figure. Each dot is a gene. The x-axis is how much it changed (fold change); the y-axis is how confident we are (significance). The genes that matter - big change and high confidence - fly up into the top corners, which is where the name comes from.
# Turn the results into a plain data frame and drop genes with no padj df <- as.data.frame(res) df <- df[!is.na(df$padj), ] df$sig <- df$padj < 0.05 plot(df$log2FoldChange, -log10(df$padj), pch = 20, col = ifelse(df$sig, "#a855f7", "#cbd5e1"), xlab = "log2 fold change (treated vs untreated)", ylab = "-log10 adjusted p-value", main = "Volcano plot - your first one!") abline(h = -log10(0.05), lty = 2, col = "#64748b") # the padj < 0.05 line
That's it. You just took a real published experiment, ran a proper statistical workflow, found the genes that respond to a steroid drug, and made the figure you'll see in thousands of published papers. Your plot should show thousands of grey dots near the bottom and a purple spray of hits rising on both sides.
๐ถ Try this: what does the design buy you?
Rerun step 3 with design = ~ dex (leaving out cell), then run step 4 again and compare summary(res). You should find about 2,700 significant genes instead of about 4,000. Before you read on, think about why.
Answer: without cell, DESeq2 does not know the samples come in donor pairs, so natural differences between donors look like random noise. More noise means weaker evidence, so fewer genes pass. Real experiments have structure, and putting it in the design is one of the most valuable things you can do.
Prefer Python? The same analysis with PyDESeq2
DESeq2 lives in R, but PyDESeq2 is a Python re-implementation that gives very similar results - handy if you work mostly in Python (e.g. alongside scanpy for single-cell). Here's the same five-step workflow in Python:
# install once !pip install pydeseq2 import pandas as pd from pydeseq2.dds import DeseqDataSet from pydeseq2.ds import DeseqStats base = "https://raw.githubusercontent.com/MennahBassiouny/bionexus/main/" counts = pd.read_csv(base + "airway-counts.csv", index_col=0).T # PyDESeq2 wants samples x genes samples = pd.read_csv(base + "airway-samples.csv", index_col=0) genes = pd.read_csv(base + "airway-genes.csv", index_col=0) samples = samples.loc[counts.index] # same order as the counts dds = DeseqDataSet(counts=counts, metadata=samples, design="~cell + dex") dds.deseq2() stat_res = DeseqStats(dds, contrast=["dex", "trt", "untrt"], alpha=0.05) stat_res.summary() res = stat_res.results_df.join(genes).sort_values("padj") res.head(10)
The columns (log2FoldChange, pvalue, padj) and how you read them are exactly the same as in R. The fold changes match R almost exactly; the adjusted p-values differ a little because of small differences in the internal statistics, so expect about 3,900 significant genes rather than 4,000 (tested with PyDESeq2 0.5.4).
Where this data comes from
The counts are from the Bioconductor airway package, which holds all 63,677 annotated genes; our copy keeps the 33,469 genes that have at least one read. In R you can also load the full version with BiocManager::install("airway"), then library(airway); data(airway). The raw sequencing files are on GEO under GSE52778.
The statistics behind that padj column
Why a raw p-value of 0.05 is not enough when you test thousands of genes at once, and what the adjustment actually does, is the subject of Statistics essentials and The multiple-testing problem. You do not need them to finish here, but they are the "why" behind trusting padj over the raw p-value.
Check your understanding
Sources & further reading
- Love MI, Huber W, Anders S. Moderated estimation of fold change and dispersion for RNA-seq data with DESeq2. Genome Biology 15: 550, 2014. doi:10.1186/s13059-014-0550-8
- Himes BE, Jiang X, Wagner P, et al. RNA-Seq transcriptome profiling identifies CRISPLD2 as a glucocorticoid responsive gene that modulates cytokine function in airway smooth muscle cells. PLoS One 9: e99625, 2014. (the airway dataset) doi:10.1371/journal.pone.0099625
- Muzellec B, Teleńczuk M, Cabeli V, Andreux M. PyDESeq2: a python package for bulk RNA-seq differential expression analysis. Bioinformatics 39: btad547, 2023. (the Python version) doi:10.1093/bioinformatics/btad547
- Conesa A, Madrigal P, Tarazona S, et al. A survey of best practices for RNA-seq data analysis. Genome Biology 17: 13, 2016. doi:10.1186/s13059-016-0881-8
Last reviewed: October 2026.