as_ranked: Convert DESeq2 results to a ranked gene list for GSEA

View source: R/deseq2.R

as_rankedR Documentation

Convert DESeq2 results to a ranked gene list for GSEA

Description

Extract a per-gene statistic from a DESeq2 analysis as a named numeric vector sorted in descending order, ready to be passed to gsea. This uses the full ranked list rather than a significance cut-off, so no information is discarded by dichotomising the genes.

Usage

as_ranked(x, statistic = c("stat", "log2FoldChange"), ...)

## S3 method for class 'DESeqResults'
as_ranked(x, statistic = c("stat", "log2FoldChange"), ...)

## S3 method for class 'DESeqDataSet'
as_ranked(x, statistic = c("stat", "log2FoldChange"), ...)

Arguments

x

a DESeqResults or DESeqDataSet object. For a DESeqDataSet, ... is forwarded to DESeq2::results() (e.g. contrast, name, alpha, lfcThreshold).

statistic

character, the column to rank by: "stat" (the Wald statistic, default) or "log2FoldChange". The Wald statistic is generally preferred for ranking as it incorporates the per-gene standard error.

...

additional arguments; for a DESeqDataSet these are passed to DESeq2::results().

Details

Non-finite statistics (genes with all-zero counts, genes flagged by outlier replacement, or failed fits) are dropped with a message. Duplicated gene identifiers are resolved by keeping the entry with the largest absolute statistic, with a warning. Note that lfcShrink() with type = "apeglm" does not return a stat column; use statistic = "log2FoldChange" for shrunk results, and keep in mind that shrunk fold changes are intended for ranking and visualization, not for statistical testing.

Value

A named numeric vector sorted in descending order, suitable for the geneList argument of gsea.

Examples


library(DESeq2)
dds <- makeExampleDESeqDataSet(n = 500)
## strengthen a subset of genes in condition B (keep the rest near-null
## so the size factors have a stable reference)
isB <- colData(dds)$condition == "B"
ct <- counts(dds)
ct[1:60, isB] <- ct[1:60, isB] * 5 + 500
storage.mode(ct) <- "integer"
counts(dds) <- ct
dds <- DESeq(dds, quiet = TRUE)
ranked <- as_ranked(dds, contrast = c("condition", "B", "A"))
gene_sets <- list(
    SET1 = paste0("gene", 1:50),
    SET2 = paste0("gene", 201:260)
)
res <- gsea(ranked, gene_sets, nPerm = 1000)


enrichit documentation built on Oct. 28, 2026, 9:07 a.m.