/bio-differential-expression-batch-correction
Remove batch effects from RNA-seq data using ComBat, ComBat-Seq, limma removeBatchEffect, and SVA for unknown batch variables. Use when correcting batch effects in expression data.
$ npx -y skills add FreedomIntelligence/OpenClaw-Medical-Skills --skill bio-differential-expression-batch-correction --agent claude-codeHow it fires
How this skill gets triggered: by you, by Claude, or both.
- Fires itselfAuto-invocation. Claude auto-loads it when your prompt matches the work.Auto-invocation is when the right skill fires by itself at the right moment, driven by a FLOW.md router and a hook, instead of you invoking it by name. It is the difference between a skill being installed and a skill actually getting used.Read the full definition →
- You can call itInvoke it directly when you want it.
- Slash command
/bio-differential-expression-batch-correction
Context preview
The summary Claude sees to decide when to auto-load this skill.
Remove batch effects from RNA-seq data using ComBat, ComBat-Seq, limma removeBatchEffect, and SVA for unknown batch variables. Use when correcting batch effects in expression data.
SKILL.md
bio-differential-expression-batch-correction.SKILL.mdname: bio-differential-expression-batch-correction
description: Remove batch effects from RNA-seq data using ComBat, ComBat-Seq, limma removeBatchEffect, and SVA for unknown batch variables. Use when correcting batch effects in expression data.
tool_type: r
primary_tool: sva
Version Compatibility
Reference examples tested with: DESeq2 1.42+, ggplot2 3.5+, limma 3.58+, scanpy 1.10+
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.
Batch Effect Correction
ComBat-Seq (Count Data)
**Goal:** Remove batch effects from raw count data while preserving biological group differences.
**Approach:** Apply ComBat-Seq's negative binomial regression to adjust counts, keeping the integer nature of the data.
**"Remove batch effects from my RNA-seq counts"** → Adjust raw count matrix for known batch labels using negative binomial modeling, preserving biological condition effects.
library(sva)
# counts: raw count matrix (genes x samples)
# batch: vector of batch labels
# group: vector of biological condition (optional, to preserve)
corrected_counts <- ComBat_seq(counts = as.matrix(counts),
batch = batch,
group = condition,
full_mod = TRUE)
# Result is batch-corrected count matrix
# Use for visualization, clustering, but NOT for DE (use design formula instead)ComBat (Normalized Data)
**Goal:** Remove batch effects from normalized (log-transformed or TPM) expression data.
**Approach:** Apply parametric empirical Bayes adjustment to normalized expression while protecting biological covariates.
library(sva)
# For normalized expression (log-transformed, TPM, etc.)
# NOT for raw counts
# Create model matrix
mod <- model.matrix(~ condition, data = metadata)
mod0 <- model.matrix(~ 1, data = metadata)
# Run ComBat
corrected_expr <- ComBat(dat = as.matrix(normalized_expr),
batch = metadata$batch,
mod = mod,
par.prior = TRUE)limma removeBatchEffect
**Goal:** Produce batch-corrected expression values for visualization while preserving group differences.
**Approach:** Regress out the batch effect from normalized expression using limma's linear model.
library(limma)
# For visualization/clustering only
# Preserves group differences while removing batch
design <- model.matrix(~ condition, data = metadata)
corrected_expr <- removeBatchEffect(normalized_expr,
batch = metadata$batch,
design = design)
# For PCA, heatmaps, etc.DESeq2 Design Formula (Recommended for DE)
**Goal:** Account for batch effects during DE testing without modifying the count data.
**Approach:** Include batch as a covariate in the DESeq2 design formula so batch variance is modeled, not removed.
library(DESeq2)
# Include batch in design formula - preferred for DE analysis
dds <- DESeqDataSetFromMatrix(countData = counts,
colData = metadata,
design = ~ batch + condition)
# Batch is modeled, not removed
# DE results are adjusted for batch
dds <- DESeq(dds)
res <- results(dds, contrast = c('condition', 'treatment', 'control'))Surrogate Variable Analysis (SVA)
**Goal:** Discover and correct for unknown sources of variation (hidden batch effects).
**Approach:** Estimate surrogate variables from the residual variation not explained by the biological model.
**"Correct for unknown batch effects in my expression data"** → Estimate latent surrogate variables capturing unwanted variation, then include them as covariates in the DE model.
library(sva)
# When batch is unknown, estimate surrogate variables
mod <- model.matrix(~ condition, data = metadata)
mod0 <- model.matrix(~ 1, data = metadata)
# Estimate number of surrogate variables
n_sv <- num.sv(normalized_expr, mod, method = 'leek')
# Estimate surrogate variables
svobj <- sva(normalized_expr, mod, mod0, n.sv = n_sv)
# Add SVs to design for DE
design_with_sv <- cbind(mod, svobj$sv)
SVA with DESeq2
**Goal:** Integrate surrogate variables into DESeq2 to adjust for hidden confounders during DE testing.
**Approach:** Estimate SVs from normalized counts, add them to colData, and update the design formula.
library(DESeq2)
library(sva)
# Normalize for SV estimation
dds <- DESeqDataSetFromMatrix(countData = counts, colData = metadata, design = ~ condition)
dds <- estimateSizeFactors(dds)
norm_counts <- counts(dds, normalized = TRUE)
# Estimate SVs
mod <- model.matrix(~ condition, data = metadata)
mod0 <- model.matrix(~ 1, data = metadata)
svobj <- sva(norm_counts, mod, mod0)
# Add SVs to colData
for (i in seq_len(ncol(svobj$sv))) {
colData(dds)[[paste0('SV', i)]] <- svobj$sv[, i]
}
# Update design
sv_formula <- as.formula(paste('~', paste(paste0('SV', 1:ncol(svobj$sv)), collapse = ' + '), '+ condition'))
design(dds) <- sv_formula
# Run DESeq2
dds <- DESeq(dds)Visualize Batch Effects
**Goal:** Confirm batch effect removal by comparing PCA plots before and after correction.
**Approach:** Run PCA on pre- and post-correction expression, coloring points by batch and condition.
library(ggplot2)
# PCA before correction
pca_before <- prcomp(t(normalized_expr), scale. = TRUE)
pca_df <- data.frame(PC1 = pca_before$x[, 1], PC2 = pca_before$x[, 2],
batch = metadata$batch, condition = metadata$condition)
p1 <- ggplot(pca_df, aes(PC1, PC2, color = batch, shape = condition)) +
geom_point(size = 3) + ggtitle('Before Correction')
# PCA after correction
pca_after <- prcomp(t(corrected_expr), scRead more
name: bio-differential-expression-batch-correction description: Remove batch effects from RNA-seq data using ComBat, ComBat-Seq, limma removeBatchEffect, and SVA for unknown batch variables. Use when correcting batch effects in expression data. tool_type: r primary_tool: sva
Version Compatibility
Reference examples tested with: DESeq2 1.42+, ggplot2 3.5+, limma 3.58+, scanpy 1.10+
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.
Batch Effect Correction
ComBat-Seq (Count Data)
**Goal:** Remove batch effects from raw count data while preserving biological group differences.
**Approach:** Apply ComBat-Seq's negative binomial regression to adjust counts, keeping the integer nature of the data.
**"Remove batch effects from my RNA-seq counts"** → Adjust raw count matrix for known batch labels using negative binomial modeling, preserving biological condition effects.
library(sva)
# counts: raw count matrix (genes x samples)
# batch: vector of batch labels
# group: vector of biological condition (optional, to preserve)
corrected_counts <- ComBat_seq(counts = as.matrix(counts),
batch = batch,
group = condition,
full_mod = TRUE)
# Result is batch-corrected count matrix
# Use for visualization, clustering, but NOT for DE (use design formula instead)ComBat (Normalized Data)
**Goal:** Remove batch effects from normalized (log-transformed or TPM) expression data.
**Approach:** Apply parametric empirical Bayes adjustment to normalized expression while protecting biological covariates.
library(sva)
# For normalized expression (log-transformed, TPM, etc.)
# NOT for raw counts
# Create model matrix
mod <- model.matrix(~ condition, data = metadata)
mod0 <- model.matrix(~ 1, data = metadata)
# Run ComBat
corrected_expr <- ComBat(dat = as.matrix(normalized_expr),
batch = metadata$batch,
mod = mod,
par.prior = TRUE)limma removeBatchEffect
**Goal:** Produce batch-corrected expression values for visualization while preserving group differences.
**Approach:** Regress out the batch effect from normalized expression using limma's linear model.
library(limma)
# For visualization/clustering only
# Preserves group differences while removing batch
design <- model.matrix(~ condition, data = metadata)
corrected_expr <- removeBatchEffect(normalized_expr,
batch = metadata$batch,
design = design)
# For PCA, heatmaps, etc.DESeq2 Design Formula (Recommended for DE)
**Goal:** Account for batch effects during DE testing without modifying the count data.
**Approach:** Include batch as a covariate in the DESeq2 design formula so batch variance is modeled, not removed.
library(DESeq2)
# Include batch in design formula - preferred for DE analysis
dds <- DESeqDataSetFromMatrix(countData = counts,
colData = metadata,
design = ~ batch + condition)
# Batch is modeled, not removed
# DE results are adjusted for batch
dds <- DESeq(dds)
res <- results(dds, contrast = c('condition', 'treatment', 'control'))Surrogate Variable Analysis (SVA)
**Goal:** Discover and correct for unknown sources of variation (hidden batch effects).
**Approach:** Estimate surrogate variables from the residual variation not explained by the biological model.
**"Correct for unknown batch effects in my expression data"** → Estimate latent surrogate variables capturing unwanted variation, then include them as covariates in the DE model.
library(sva) # When batch is unknown, estimate surrogate variables mod <- model.matrix(~ condition, data = metadata) mod0 <- model.matrix(~ 1, data = metadata) # Estimate number of surrogate variables n_sv <- num.sv(normalized_expr, mod, method = 'leek') # Estimate surrogate variables svobj <- sva(normalized_expr, mod, mod0, n.sv = n_sv) # Add SVs to design for DE design_with_sv <- cbind(mod, svobj$sv)
SVA with DESeq2
**Goal:** Integrate surrogate variables into DESeq2 to adjust for hidden confounders during DE testing.
**Approach:** Estimate SVs from normalized counts, add them to colData, and update the design formula.
library(DESeq2)
library(sva)
# Normalize for SV estimation
dds <- DESeqDataSetFromMatrix(countData = counts, colData = metadata, design = ~ condition)
dds <- estimateSizeFactors(dds)
norm_counts <- counts(dds, normalized = TRUE)
# Estimate SVs
mod <- model.matrix(~ condition, data = metadata)
mod0 <- model.matrix(~ 1, data = metadata)
svobj <- sva(norm_counts, mod, mod0)
# Add SVs to colData
for (i in seq_len(ncol(svobj$sv))) {
colData(dds)[[paste0('SV', i)]] <- svobj$sv[, i]
}
# Update design
sv_formula <- as.formula(paste('~', paste(paste0('SV', 1:ncol(svobj$sv)), collapse = ' + '), '+ condition'))
design(dds) <- sv_formula
# Run DESeq2
dds <- DESeq(dds)Visualize Batch Effects
**Goal:** Confirm batch effect removal by comparing PCA plots before and after correction.
**Approach:** Run PCA on pre- and post-correction expression, coloring points by batch and condition.
library(ggplot2)
# PCA before correction
pca_before <- prcomp(t(normalized_expr), scale. = TRUE)
pca_df <- data.frame(PC1 = pca_before$x[, 1], PC2 = pca_before$x[, 2],
batch = metadata$batch, condition = metadata$condition)
p1 <- ggplot(pca_df, aes(PC1, PC2, color = batch, shape = condition)) +
geom_point(size = 3) + ggtitle('Before Correction')
# PCA after correction
pca_after <- prcomp(t(corrected_expr), scThe largest open-source medical AI skill library for OpenClaw.
Other skills on openclaw-medical-skills.
adaptyv
Cloud laboratory platform for automated protein testing and validation. Use when designing proteins and needing experimental validation including binding…
adhd-daily-planner
Time-blind friendly planning, executive function support, and daily structure for ADHD brains. Specializes in realistic time estimation, dopamine-aware task…
aeon
This skill should be used for time series machine learning tasks including classification, regression, clustering, forecasting, anomaly detection,…
agent-browser
Browse the web for any task — research topics, read articles, interact with web apps, fill forms, take screenshots, extract data, and test web pages. Use…

