Extract, filter, annotate, and export differential expression results from DESeq2 or edgeR. Use for identifying significant genes, applying multiple testing corrections, adding gene annotations, and preparing results for downstream analysis. Use when filtering and exporting DE analysis results.
Extract, filter, annotate, and export differential expression results from DESeq2 or edgeR. Use for identifying significant genes, applying multiple testing corrections, adding gene annotations, and preparing results for downstream analysis. Use when filtering and exporting DE analysis results.
Before using code patterns, verify installed versions match. If versions differ:
R: packageVersion('<pkg>') then ?function_name to verify parameters
If code throws ImportError, AttributeError, or TypeError, introspect the installed
package and adapt the example to match the actual API rather than retrying.
DE Results
Extract, filter, and export differential expression results.
Required Libraries
library(DESeq2)# or library(edgeR)
library(dplyr)# For data manipulation
Extracting DESeq2 Results
Goal: Retrieve DE statistics from a fitted DESeq2 model as a usable data frame.
Call results() with optional shrinkage, then convert to a data frame with gene identifiers.
Approach:
# Basic results
res <- results(dds)# With specific alpha (adjusted p-value threshold)
res <- results(dds, alpha =0.05)# With log fold change shrinkage
res <- lfcShrink(dds, coef ='condition_treated_vs_control', type ='apeglm')# Convert to data frame
res_df <- as.data.frame(res)
res_df$gene <- rownames(res_df)
Extracting edgeR Results
Goal: Retrieve DE statistics from a fitted edgeR model as a data frame.
Approach: Use topTags with n=Inf to extract all gene-level results.
# Get all results
results <- topTags(qlf, n =Inf)$table
# Add gene column
results$gene <- rownames(results)
Approach: Subset results by adjusted p-value, fold change magnitude, and expression level thresholds.
"Get the significant differentially expressed genes" → Filter DE results by adjusted p-value and fold change cutoffs to produce up- and down-regulated gene lists.
Goal: Assess concordance between DESeq2 and edgeR results to identify robust DE genes.
Approach: Compute set overlaps and visualize with a Venn diagram.
# Get significant genes from both methods
deseq2_sig <- rownames(subset(deseq2_res, padj <0.05))
edger_sig <- rownames(subset(edger_results, FDR <0.05))# Overlap
common <- intersect(deseq2_sig, edger_sig)
deseq2_only <- setdiff(deseq2_sig, edger_sig)
edger_only <- setdiff(edger_sig, deseq2_sig)
cat(sprintf('DESeq2 significant: %d\n',length(deseq2_sig)))
cat(sprintf('edgeR significant: %d\n',length(edger_sig)))
cat(sprintf('Common: %d\n',length(common)))
cat(sprintf('DESeq2 only: %d\n',length(deseq2_only)))
cat(sprintf('edgeR only: %d\n',length(edger_only)))# Venn diagram
library(VennDiagram)
venn.diagram(
x =list(DESeq2 = deseq2_sig, edgeR = edger_sig),
filename ='de_overlap.png',
fill =c('steelblue','coral'))
Multiple Testing Correction
Goal: Apply or compare multiple testing correction methods for DE p-values.
Approach: Use Benjamini-Hochberg (default), Bonferroni, or IHW for adjusted p-values.
# DESeq2 uses Benjamini-Hochberg by default# To use different methods:# Independent Hypothesis Weighting (more powerful)
library(IHW)
res_ihw <- results(dds, filterFun = ihw)# Manual p-value adjustment
res_df$padj_bonferroni <- p.adjust(res_df$pvalue, method ='bonferroni')
res_df$padj_bh <- p.adjust(res_df$pvalue, method ='BH')
res_df$padj_fdr <- p.adjust(res_df$pvalue, method ='fdr')
Handling NA Values
Goal: Understand and handle missing values in DE results caused by filtering or outlier detection.
Approach: Identify the source of NAs (zero counts, independent filtering, outliers) and remove or investigate them.
# Count NAssum(is.na(res$padj))# Remove genes with NA padj
res_complete <- res[!is.na(res$padj),]# Understand why NAs occur# - baseMean = 0: No counts# - NA only in padj: Outlier or low count filtered by independent filtering# Check outliers
res[which(is.na(res$pvalue)& res$baseMean >0),]