coldata <- read.table("https://raw.githubusercontent.com/GenomicsAotearoa/RNA-seq-data-analysis-workflow-NZ/refs/heads/main/data-files/coldata.txt", sep="\t", header=TRUE)
counts <- read.table("https://raw.githubusercontent.com/GenomicsAotearoa/RNA-seq-data-analysis-workflow-NZ/refs/heads/main/data-files/counts.txt", sep="\t", header=TRUE, row.names=1)Identify Differentially Expressed Genes

Statistical approach
In order to identify which genes are differentially expressed between two groups (in this case, we have 15 samples from three phenotypes), when need to test if groups are significantly different from one another, which can be a challenging question to answer. We can start by specifically asking our question as a statistical question: Are the differences we observe between two groups greater than the differences we would expect to see by chance?
In the interest of time, in this workshop we will not get into T-tests, P-values and types of errors. You can read more about that here:
In summary:
- The “standard” P-value cut-off of 0.05 is not sufficient – 5% results in too many genes that will be false positives (i.e., genes that look like they are DE, but they are not).
- We need to use a p-value correction - usually FDR, also called Benjamini and Hochberg correction, but if you want to be more strict use FWER.
- We must correct for heteroscedasticity, i.e., variance is dependent on the mean.
Identifying differentially expressed genes using DESeq2
DESeq2 is a highly-regarded R package for analysing RNA-seq data. DESeq2 uses a negative binomial method to model the count data, and combines this with a generalised linear model (GLM) to identify differentially expressed genes. For more about the DESeq2 package, you can read the original article.
Run this code to load in the data:
The DESeq2 object
The DESeq2 package requires a specific data storage object called a “DESeq Data Set” or DDS object. The DDS object contains not just the count data, but also the metadata and the design matrix. This object can be created using a function from the DESeq2 package called DESeqDataSetFromMatrix() Once we have created the dds object, we can view the data stored within using the counts function.
First make sure the sample names in coldata and counts match each other and are in the same order. If they don’t match or are not in the same order, the dge object and design matrix won’t match and the result will be the wrong metadata being attributed to the wrong sample.
Run this code to force the coldata sample column match the same order as the coldata column names. Check that they are identical (should return TRUE, then you are good to continue if so). If not, check your coldata and counts objects to see what is different.
coldata <- coldata[match(colnames(counts), coldata$sample), ] # force columns into same sample order
identical(colnames(counts), coldata$sample) # must be TRUE[1] TRUE
library(tidyverse)
library(DESeq2)
coldata$histology <- factor(coldata$histology, levels = c("F", "MT", "TPM"))
coldata$batchname <- factor(coldata$batchname)
dds <- DESeqDataSetFromMatrix(countData = counts,
colData = coldata,
design = ~histology + batchname)This is not strictly necessary – the code won’t break, as DESeq2 will automatically convert character columns to factors (although numeric columns will likely error and must be factorised first!)
But in our case we want to set an order, which we should do with factor().
This is because F, MT, and TPM represent a chronological transition in state (i.e., gonads transition from female –> multiple distinct transitional states –> terminal phase male), and we want this order reflected in our results. The same order also comes up when using ggplot2, if we want the x-axis to plot in a specific order rather than alphabetically.
The design matrix can be pulled directly from the data provided to the colData argument.
Alternatively, use design = ~1 to fit an intercept-only model (no condition effect), useful for testing out normalisation or transformation steps where group comparisons aren’t needed.
Raw counts can be retrieved from our dds object using the function counts() from the DESeq2 package.
counts(dds) |> head() SI16_21G SI16_23G SI18_30G SI18_48G SI18_52G SI16_22G SI18_15G
LOC117820358 0 0 0 0 0 0 0
LOC117813751 0 0 0 0 0 5 0
ccdc51 55 62 63 46 93 57 83
tma7 1256 2246 1492 1870 1664 2506 1514
LOC117815572 2 1 2 1 1 11 1
LOC117818779 0 1 0 0 0 2 2
SI18_16G SI18_18G SI18_22G SI16_1G SI16_2G SI16_3G SI18_47G
LOC117820358 0 0 0 0 0 0 0
LOC117813751 0 0 0 723 346 482 140
ccdc51 52 61 85 241 181 175 190
tma7 1322 1053 1129 3262 3098 3050 2336
LOC117815572 3 0 4 3 3 22 30
LOC117818779 2 0 1 0 3 0 16
SI18_49G
LOC117820358 0
LOC117813751 33
ccdc51 103
tma7 2245
LOC117815572 15
LOC117818779 0
Now fit the DESeq2 generalised linear model to the data.
dds <- DESeq(dds)estimating size factors
estimating dispersions
gene-wise dispersion estimates
mean-dispersion relationship
final dispersion estimates
fitting model and testing
Note the outputs from running this function. What are these outputs?
Estimating size factors: assesses library size and calculates factors to adjust for differences between samples. This also adjusts for compositional differences (e.g., if gene X in sample 1 takes up a very large proportion of all available reads, other genes will have correspondingly fewer genes. If this effect is not uniform across samples, it can be corrected for during this stage).
Dispersion: adjusting for heteroscedasticity. DESeq2 makes use of variability estimates from not just one gene, but from all genes to make estimates about overall levels of variance. By bringing in (or “borrowing”) information from other genes, DESeq2 compensates for a small number of samples (which can lead to artificially small variance estimates otherwise).
Fitting model and testing: fitting the GLM and identifying differentially expressed genes.
We will create a new object to store the results in. We can access those results using either the head function or the summary function, which will give us slightly different information – both are valid and useful ways of familiarising yourself with the data.
res_F_vs_TPM <- results(dds, contrast = c("histology", "F", "TPM"))
res_F_vs_TPM |> head()log2 fold change (MLE): histology F vs TPM
Wald test p-value: histology F vs TPM
DataFrame with 6 rows and 6 columns
baseMean log2FoldChange lfcSE stat pvalue
<numeric> <numeric> <numeric> <numeric> <numeric>
LOC117820358 0.00000 NA NA NA NA
LOC117813751 72.65818 -9.183293 0.967298 -9.493757 2.22854e-21
ccdc51 94.55083 -0.984036 0.375143 -2.623093 8.71355e-03
tma7 1891.60802 -0.134454 0.297961 -0.451246 6.51812e-01
LOC117815572 4.59230 -2.576477 0.906197 -2.843174 4.46666e-03
LOC117818779 1.37837 -2.735360 1.823510 -1.500052 1.33601e-01
padj
<numeric>
LOC117820358 NA
LOC117813751 4.26248e-20
ccdc51 1.53114e-02
tma7 6.99225e-01
LOC117815572 8.37165e-03
LOC117818779 1.77923e-01
For RNAseq data, we always use log2 fold change (log2FC) rather than log10 or natural log transformations. You may remember we used log2 to transform our raw counts too for plotting in the Exploratory Analysis section. This is because the log2 scale is easier to intuitively interpret up- and down-regulation. A log2FC of 1 means the gene is expressed twice as much in one condition compared to another, while a log2FC of -1 means it is expressed half as much.
To visualise this further, take this example:
| Count | log2(count) | log10(count) |
|---|---|---|
| 2 | 1 | 0.301 |
| 4 | 2 | 0.602 |
| 8 | 3 | 0.903 |
| 100 | 6.644 | 2 |
| 200 | 7.644 | 2.301 |
Each doubling in count is represented by an increase in 1 on the log2 scale, while on the log10 scale, each doubling is represented by an increase of only 0.3.
Note that log2FC is the difference in log2(count) between two conditions, rather than the log2 of a single count. This naturally extends to negative log2FC values too: for example, a log2FC of -2 means the gene is expressed a quarter as much in one condition compared to another.
| Comparison (count) | Fold change | log2FC | log10FC |
|---|---|---|---|
| 100 → 200 | 2× | 1 | 0.301 |
| 100 → 400 | 4× | 2 | 0.602 |
| 100 → 50 | 0.5× | −1 | −0.301 |
| 100 → 25 | 0.25× | −2 | −0.602 |
res_F_vs_TPM |> summary()
out of 25904 with nonzero total read count
adjusted p-value < 0.1
LFC > 0 (up) : 5654, 22%
LFC < 0 (down) : 10960, 42%
outliers [1] : 131, 0.51%
low counts [2] : 2000, 7.7%
(mean count < 0)
[1] see 'cooksCutoff' argument of ?results
[2] see 'independentFiltering' argument of ?results
We can also pull out the native comparisons that are generated during analysis using:
resultsNames(dds)[1] "Intercept" "histology_MT_vs_F" "histology_TPM_vs_F"
[4] "batchname_SI18_vs_SI16"
This lists every coefficient DESeq2 estimated directly when fitting the model. Because we set F as the reference level for histology, DESeq2 estimated each other level against F – so resultsNames() gives us histology_MT_vs_F and histology_TPM_vs_F, but not MT_vs_TPM
We can pull either of these straight out by name:
res_MT_vs_F <- results(dds, name = "histology_MT_vs_F" ) name and contrast will give you identical results whenever the comparison you want is against the reference level (here, F). name is just a shortcut that skips DESeq2 recomputing the contrast. For any comparison that isn’t against the reference level (e.g. MT vs TPM), you must use contrast instead, since that comparison/coefficient was never directly estimated.
Want to save and output your full results now as a nice human readable table? Convert the res object to a dataframe, and then read out the table as a tsv file:
res_F_vs_TPM_df <- res_F_vs_TPM |> as.data.frame()
write.table(res_F_vs_TPM_df, file = "results/deseq2_F-vs-TPM-results.tsv", sep = "\t", quote = FALSE, row.names = TRUE, col.names = NA)EXERCISE 🧠🏋️♀️ (2 mins) - Identify method for multiple testing corrections
Bioinformatics can often be something of a “black box” - we execute a function by feeding in some data, and get new data as output. It’s very easy to fall into the trap of believing your data without asking all the necessary questions. For example, we’ve just used an adjusted p-value for our threshold for significance. Did you question which method was used to adjust for multiple testing? See if you can identify, using the help menu, which method was used here. Try and specify a different method for multiple testing corrections and note how this alters your output.
The ?results help info will show the default p adjustment method is pAdjustMethod = "BH". Further reading will show that options for this come from ?p.adjust, which lists the following options:
"holm", "hochberg", "hommel", "bonferroni", "BH", "BY", "fdr", "none"
The default BH method is Benjamini & Hochberg (or the FDR method), which is described here in the R documentation.
Volcano plots
Volcano plots are a useful and commonly used method to visualise differentially expressed genes within your data set.
In the following code we will first produce the volcano plot itself, which shows the distribution of all genes according to p-value and log2 fold change.
Statistical significance is often not the only measure we will use to call genes differentially expressed. It is common to apply an additional threshold of gene expression needing to double or halve to be considered differentially expressed. On the log2FC scale, this is log2(2) (i.e., a value > 1 or < -1).
Genes will be coloured based on whether they are up (red) or down (blue) regulated.
This plot is an important “sanity check”. We can logically check that our significant genes (in red) are those with both a p-value < 0.05 and a log2 fold change greater than 1 (which is equivalent to a doubling or halving of gene expression). If we observe red marked dots in the center or bottom of the plot, we could recognise an error has occurred.
# Set up data
pval_threshold <- 0.05
fc_threshold <- 1
plot_data <- as.data.frame(res_F_vs_TPM) |>
na.omit() |>
tibble::rownames_to_column("ID") |>
mutate(
logFC = log2FoldChange,
negLogAdjPval = -log10(padj),
category = case_when(
padj <= pval_threshold & logFC >= fc_threshold ~ "Upregulated",
padj <= pval_threshold & logFC <= -fc_threshold ~ "Downregulated",
padj <= pval_threshold ~ "Passes P-value cut-off",
TRUE ~ "Not significant"
)
)library(ggplot2)
# Create plot
p <- ggplot(plot_data, aes(x = logFC, y = negLogAdjPval, colour = category, text = ID)) +
geom_point(alpha = 0.6, size = 1.5) +
scale_colour_manual(values = c(
"Upregulated" = "red",
"Downregulated" = "blue",
"Passes P-value cut-off" = "grey40",
"Not significant" = "grey80"
)) +
geom_vline(xintercept = c(-fc_threshold, fc_threshold), linetype = "dashed") +
geom_hline(yintercept = -log10(pval_threshold), linetype = "dashed") +
labs(
title = "Volcano Plot of Differential Gene Expression",
subtitle = paste("Thresholds: |log2FC| >=", fc_threshold,
"and adjusted p-value <=", pval_threshold),
x = "log2 Fold Change",
y = "-log10(adjusted p-value)",
colour = "Differential Expression"
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, size = 16),
plot.subtitle = element_text(hjust = 0.5, size = 12)
)
# display plot
p
We can also produce an interactive volcano plot with the ggigraph package.
Hover with your mouse over the points above to see pop up boxes with gene names, category, logFC and negLogPval. You can also zoom in and out, and pan around the plot to explore the data.
library(ggiraph)
library(plotly)
# Convert ggplot to an interactive plotly object
interactive_plot <- ggplotly(p, tooltip = c("text", "x", "y", "colour"))
# Customise hover text
interactive_plot <- interactive_plot |>
layout(hoverlabel = list(bgcolor = "white"),
hovermode = "closest")
# Display the interactive plot
interactive_plot
# Save the plot (optional)
dir.create("plots")
ggsave("plots/volcano_plot_F_vs_TPM.png", width = 10, height = 8, dpi = 300)library(ggiraph)
library(plotly)
# Convert ggplot to an interactive plotly object
interactive_plot <- ggplotly(p, tooltip = c("text", "x", "y", "colour"))
# Customise hover text
interactive_plot <- interactive_plot |>
layout(hoverlabel = list(bgcolor = "white"),
hovermode = "closest")
# Display the interactive plot
interactive_plotDISCUSSION 🤔 (2 mins)
We can see the blue dots are downregulated genes and the red dots are upregulated genes. But downregulated in what? Upregulated in what?
It’s relative! The contrast we defined here was histology F vs TPM. Therefore, downregulated genes (blue) are those that are expressed less in F, than in TPM, while upregulated genes (red) are expressed more in F, than in TPM.
It’s always relative to the order in which we define the contrast and relative to the groups being compared. Genes that are shown here as downregulated in F are also upregulated in TPM with the same log2FC value, but with the opposite sign.
Filter results for up or downregulated genes
The res object contains information for all genes tested. It is practical to create a new object that contains only the genes we consider differentially expressed based on the thresholds (p-value, logFC) and methods (e.g., multiple testing adjustment) that suit our situation.
We will remove any rows that have NAs in the results object, then pull out only those with an adjusted p-value less than 0.05.
The contrast we generated here is contrast = c("histology", "F", "TPM")) this means that all genes with a positive logFC are upregulated in the first condition “F”.
We want a list of genes that are upregulated in the second condition “TPM”.
But why do we want to “flip” the interpretation of which condition is upregulated? This is because sex change is a chronological process, and it feels intuitive to think about the progression of gene expression over time. The way that this contrast was set up means that a positive logFC refers to genes upregulated in females. To get genes upregulated in males, we can simply grab all genes that are less than -1 logFC because anything downregulated in the first condition “F” is inherently upregulated in the second condition “TPM”. If you regenerated the contrast with the order of “F” and “TPM” flipped, you would get the exact same genes, with the same logFC value, but the sign would be flipped.
This is an important concept in RNAseq analysis, and is worth taking a moment to think about. The contrast is always relative to the order of the conditions you specify, and the interpretation of up- or down-regulation is always relative to the first condition in the contrast. You have flexibility to define the contrast in whichever way you want, but you must be aware of how this will affect your interpretation of the results.
# Omit all rows with NA values
res_F_vs_TPM <- res_F_vs_TPM |> na.omit()
# Filter for genes upregulated in TPM (downregulated in F)
resPadjLogFC_F_vs_TPM <- res_F_vs_TPM[res_F_vs_TPM$padj <= 0.05 & res_F_vs_TPM$log2FoldChange < -1,]
# Get dimensions
resPadjLogFC_F_vs_TPM |> dim() [1] 9534 6
EXERCISE 🧠🏋️♀️ (2 mins) - Regenerate the contrast with the condition order switched
Have a look at the full results object you generated earlier res_F_vs_TPM. Can you regenerate the contrast the opposite way around, and confirm that you get the same genes, but with the opposite sign for logFC?
Hints:
- View the dataframe using
View(as.data.frame(results_object_name)) - Create the contrast as a new object and call it
res_TPM_vs_F - You don’t need to re-do the filtering step above, we can compare the two full results directly.
res_TPM_vs_F <- results(dds, contrast = c("histology", "TPM", "F"))
res_TPM_vs_F <- res_TPM_vs_F |> na.omit()View(as.data.frame(res_TPM_vs_F))Sanity check 😵💫
Confused about which genes are up or down regulated in which condition? Let’s sanity check our results by looking at the raw counts of one of our genes. Take the first gene in the dataframe for both the res_F_vs_TPM and res_TPM_vs_F objects.
The gene “LOC117813751” has a log2FC of -9.1832934 in the
res_F_vs_TPMobject.The gene “LOC117813751” has a log2FC of 9.1832934 in the
res_TPM_vs_Fobject.
So reading this as the log2FC referring to the first condition in the name of the object, this means the gene is upregulated in TPM and downregulated in F.
Let’s check the raw counts to see if this is true.
gene <- "LOC117813751"
t(counts[gene,]) |>
as.data.frame() |>
tibble::rownames_to_column("sample") |>
left_join(coldata, by="sample") |>
select(sample,all_of(gene), histology) |>
filter(histology %in% c("F", "TPM")) sample LOC117813751 histology
1 SI16_21G 0 F
2 SI16_23G 0 F
3 SI18_30G 0 F
4 SI18_48G 0 F
5 SI18_52G 0 F
6 SI16_1G 723 TPM
7 SI16_2G 346 TPM
8 SI16_3G 482 TPM
9 SI18_47G 140 TPM
10 SI18_49G 33 TPM
Above we have rearranged and pulled out the raw counts for just the gene “LOC117813751” and the two conditions we are comparing. Not only are the counts much higher in TPM (males), but this gene is not expressed in any of the female samples at all! We can be confident that:
- In
res_F_vs_TPM, positive or negative log2FC are for genes up or down regulated in females (when comparing to males)
- In
res_TPM_vs_F, positive or negative log2FC are for genes up or down regulated in males (when comparing to females)
Choose your own adventure 👣 - pick a comparison for functional analysis (5 mins)
Our workshop data consists of 3 sample groups, with 5 samples each:
- F = female gonad samples
- MT = mid-transitioning gonad samples
- TPM = terminal phase male gonad samples

What comparison are you interested in investigating further? Genes upregulated in males, compared to mid-transitioning? Genes downregulated in females, compared to male? What questions do you have about the biology of the fish that you want to investigate further?
Your task is to make a filtered results object that contains the genes you are interested in. You’ll need to generate the contrast you need from dds, filter out up- or down-regulated only genes from the contrast object using log2FC and adjusted P-value cut off and finally sanity check yourself by checking the original raw counts with one of the genes in your table (to make sure you got it right!)
Note that for functional analyisis, we are only going to need the list of gene names themselves. So if you take the genes from a “flipped” contrast, the actual log2FC sign will not matter once we start functional analysis.
Extra for experts: chose a different adjustment for p-value rather than the default “BH”.
Let’s say we want to look at all genes upregulated in MT group, vs F.
Pull out contrast:
res_MT_vs_F <- results(dds, contrast = c("histology", "MT", "F"))Or extra for experts:
res_MT_vs_F <- results(dds, contrast = c("histology", "MT", "F"), pAdjustMethod = "bonferroni")Filter out only genes upregulated in MT (log2FC > 1) and passing adjusted p-value <= 0.05
res_MT_vs_F <- res_MT_vs_F |> na.omit()
resPadjLogFC_MT_vs_F <- res_MT_vs_F[res_MT_vs_F$padj <= 0.05 & res_MT_vs_F$log2FoldChange > 1,]
# Get dimensions
resPadjLogFC_MT_vs_F |> dim() [1] 3492 6
Sanity check! We are expecting our gene to have higher raw counts in MT samples:
# Chose gene from your filtered object
gene <- "LOC117818116"
t(counts[gene,]) |>
as.data.frame() |>
tibble::rownames_to_column("sample") |>
left_join(coldata, by="sample") |>
select(sample,all_of(gene), histology) |>
filter(histology %in% c("F", "MT")) # Change to your groups! sample LOC117818116 histology
1 SI16_21G 86 F
2 SI16_23G 282 F
3 SI18_30G 33 F
4 SI18_48G 81 F
5 SI18_52G 65 F
6 SI16_22G 2405 MT
7 SI18_15G 255 MT
8 SI18_16G 626 MT
9 SI18_18G 374 MT
10 SI18_22G 263 MT
Identifying differentially expressed genes using limma
We may not have time for this section during the Spring School, but notes and code are provided below.
Differentially expressed gene lists
To recap, we’ve now produced two lists of differentially expressed genes - one produced with the Limma method (called sigGenesLimma_TPM_vs_F), one with DESeq2 (called resPadjLogFC_F_vs_TPM). In both cases we used the same threshold for statistical significance (adjusted p-value < 0.05, using the FDR correction) and biological significance (logFC more than 1 or less than negative 1, which is equivalent to a doubling or halving gene expression). The contrasts were generated in opposite directions, but the genes that are in both are all upregulated in TPM.
In our run-through, Limma identified 9232 differentially expressed genes, while DESeq2 identified 9534 genes.
These numbers are very similar, but what’s the actual overlap between these two groups? A venn diagram is an easy way to visualise the relationship between the two groups. First, load the gplots library which we will use for making venn diagrams. Create an object, which we will call setlist, that lists the two gene sets (in our case, this will be the rownames of the objects we created with Limma and DESeq2). Finally, use the venn() function to create a venn diagram.
# BiocManager::install("gplots")
library(gplots)
setlist <- list(Limma = rownames(sigGenesLimma_TPM_vs_F),
DESeq2 = rownames(resPadjLogFC_F_vs_TPM ))
venn(setlist)
This is a strong concordance between our two methods and gives us high confidence in our approach. At this point you could take the overlap (n = 8416 genes) only, take all genes (8416 + 816 + 1118 = 10350), or take genes from just one method (more relevant if one method was conservative compared to the other). Exactly how you resolve this step will depend on the exact shape of your venn diagram and your context.
For simplicity, we will take the list of all genes identified by DEseq2 and see in the next episode how we can derive meaningful biological information from this long list of genes.
# Convert venn data to list object
intersect_list <- attr(venn(setlist, show.plot = FALSE), "intersections")
intersect_list |> str()List of 3
$ Limma:DESeq2: chr [1:8416] "LOC117808765" "LOC117830219" "LOC117819294" "ribc1" ...
$ Limma : chr [1:816] "si:cabz01071907.1" "LOC117820055" "grk1a" "LOC117819808" ...
$ DESeq2 : chr [1:1118] "LOC117823012" "dnajc16l" "foxp4" "fam72a" ...
# Create vectors of the three gene lists
Limma_only_genes <- intersect_list$Limma
DEseq2_only_genes <- intersect_list$DESeq2
Both_genes <- intersect_list$`Limma:DESeq2`



