4: The Tidyverse for Biological Data Wrangling
Chapter 4: The Tidyverse for Biological Data Wrangling
Genomic datasets frequently arrive as messy spreadsheets, variant tables, or multi-sample expression matrices. The Tidyverse (featuring dplyr, tidyr, and the pipe operator) offers an intuitive grammar for filtering, mutating, reshaping, and summarizing biological observations.
1. The Pipe Operator (%>% and |>)
Pipes pass the result of the left-hand expression as the first argument to the right-hand function, transforming deeply nested code into readable linear pipelines:
library(dplyr)
# Filter for significant cancer drivers and sort by magnitude
sig_drivers <- de_table %>%
filter(padj < 0.01, abs(log2FoldChange) >= 1.5) %>%
mutate(
direction = ifelse(log2FoldChange > 0, "UP", "DOWN"),
neg_log10_padj = -log10(padj)
) %>%
arrange(desc(abs(log2FoldChange)))2. Cohort Aggregations with group_by & summarise
Computing summary statistics across biological chromosome groups or experimental cohorts:
chr_summary <- sig_drivers %>%
group_by(chr) %>%
summarise(
gene_count = n(),
mean_fc = mean(log2FoldChange),
max_sig = max(neg_log10_padj)
)
print(chr_summary)3. Reshaping Count Matrices: Wide to Long with pivot_longer
Standard count matrices are wide (one column per sample), but ggplot2 requires long tidy data:
library(tidyr)
long_counts <- wide_counts %>%
pivot_longer(
cols = -gene_id,
names_to = "Sample_ID",
values_to = "Read_Count"
) %>%
mutate(Condition = ifelse(grepl("^Ctrl", Sample_ID), "Control", "Treated"))
print(head(long_counts, 6))