Batch Effect Correction logo

Batch Effect Correction

OrganizationPopular
aipoch
batch-effect-correction

Use when correcting batch effects in merged bulk expression matrices with sample-level batch metadata while preserving biological group structure and generating before-and-after QC plots. NOT for: single-cell integration, raw FASTQ processing, differential expression without batch labels, or datasets without biological groups.

Overview

Publisheraipoch
Repositorymedical-research-skills
Skill namebatch-effect-correction
Stars
1.9K
Forks
175
Bundled files
17
LicenseMIT
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 17 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by aipoch on GitHub. Read the source before you install it.

Installation

Install the Batch Effect Correction AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.

Use it in TypingMind

Enable Batch Effect Correction in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Batch Effect Correction on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Batch Effect Correction is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

Batch Effect Correction

Prerequisites

Run the following before the first analysis to install all required R packages:

bash
Rscript -e "if (!require('BiocManager', quietly=TRUE)) install.packages('BiocManager'); BiocManager::install(c('sva', 'limma')); install.packages('ggplot2', repos='https://cloud.r-project.org')"

Note: sva and limma are Bioconductor packages and require BiocManager for installation. ggplot2 is a standard CRAN package.

The skill cannot run until these packages are installed. In new or bare R environments, always run the prerequisite step first.


When to Read External Files

SituationFile to ReadPurpose
Need algorithm detailsreferences/algorithm.mdComBat workflow, assumptions, and QC logic
Need to run analysisscripts/main.RExecute: Rscript scripts/main.R --input_file ... --group_file ...
Encounter errorsreferences/troubleshooting.mdCommon errors and solutions
Need CLI examplesreferences/cli-guide.mdDetailed CLI usage examples and baseline run record
Need test datatests/data/Sample input files for testing

Usage

bash
Rscript scripts/main.R \
  --input_file ./expression_matrix.csv \
  --group_file ./sample_info.csv \
  --output_dir ./output/ \
  --batch_column batch \
  --group_column group \
  --sample_column sample \
  --log_transform auto \
  --timeout_seconds 600 \
  --seed 42

Arguments

ShortLongTypeDefaultDescription
-i--input_filecharacterrequiredExpression matrix file (genes as rows, samples as columns)
-g--group_filecharacterrequiredSample metadata file (sample ID, group, and batch columns)
-o--output_dircharacter./output/Output directory
-b--batch_columncharacterbatchBatch column name in metadata
-c--group_columncharactergroupBiological group column name in metadata
-n--sample_columncharactersampleSample ID column name in metadata
-l--log_transformcharacterautoLog transform mode: auto, yes, no
-t--timeout_secondsinteger600Elapsed time limit in seconds; use 0 to disable
-s--seedinteger42Random seed for reproducibility

Input Format

Expression Matrix (input_file)

Genes as rows, samples as columns, CSV format with gene ID in the first column.

csv
"","Sample01","Sample02","Sample03"
"GeneA",5.12,4.87,6.03
"GeneB",8.44,8.11,7.95

Requirements:

  • Gene IDs must be unique and non-empty
  • Sample column names must be unique and non-empty
  • Expression values must be numeric and finite
  • Extra expression-matrix sample columns not present in metadata are allowed and will be ignored with a warning

Sample Metadata (group_file)

CSV with sample ID, biological group, and batch columns.

csv
"sample","group","batch"
"Sample01","Control","Batch1"
"Sample02","Case","Batch1"
"Sample03","Case","Batch2"

Requirements:

  • Sample IDs must be unique and non-empty
  • At least 2 biological groups are required
  • At least 2 batches are required
  • Each group and each batch must contain at least 2 samples
  • Metadata may describe a subset of expression-matrix samples; the analysis will keep only metadata-matched samples and warn about ignored expression columns

Output Files

FileDescription
corrected_expression_matrix.csvBatch-corrected expression matrix
matched_sample_info.csvStandardized metadata used in the analysis
batch_before_boxplot.pdfSample distribution boxplot before correction
batch_after_boxplot.pdfSample distribution boxplot after correction
batch_before_pca.pdfPCA scatter plot before correction with batch-colored points
batch_after_pca.pdfPCA scatter plot after correction with batch-colored points
batch_before_clustering.pdfHierarchical clustering before correction
batch_after_clustering.pdfHierarchical clustering after correction
session_info.txtR session and package version info

Workflow

Step 1: Validate Input

  • Check file existence and non-empty input files
  • Validate metadata column presence
  • Verify expression values are numeric and finite
  • Confirm at least 2 groups, 2 batches, and at least 2 samples per group/batch

Step 2: Align and Prepare Matrix

  • Reorder expression columns to match metadata sample order
  • Keep only metadata-matched samples; warn if the expression matrix contains extra samples absent from metadata
  • Decide whether log transformation is needed (auto, yes, or no)
  • Apply log2(x + 1) only when required

Step 3: Run Batch Correction

  • Build the design matrix with biological group information
  • Run sva::ComBat() to remove batch-driven variation
  • Preserve modeled biological group structure during correction

Step 4: Normalize and Export Results

  • Apply limma::normalizeBetweenArrays() after ComBat
  • Write the corrected matrix and matched metadata
  • Save before/after QC plots and session information

Methods

ComBat

Empirical Bayes batch-effect correction using sva::ComBat(). Recommended when merged bulk expression datasets contain known batch labels and at least two biological groups.

Log Transformation

Supports auto, yes, and no. The auto mode applies log2(x + 1) only when the matrix appears to be on a raw-like scale.

normalizeBetweenArrays

Post-correction normalization with limma::normalizeBetweenArrays() to reduce remaining cross-sample distribution differences.

QC Visualization

Generates paired boxplots, PCA scatter plots with conditional batch ellipses, and hierarchical clustering plots before and after correction to assess whether batch-driven structure is reduced.


Agent Response Contract

After a successful run, report:

  1. Sample count retained after metadata matching and any subset filtering
  2. Batch count and group count used in the ComBat design matrix
  3. Log transformation applied (auto-detected, forced yes, or skipped)
  4. QC assessment: describe whether before/after PCA plots show reduced batch clustering
  5. Artifact paths: corrected_expression_matrix.csv, batch_after_pca.pdf, batch_after_clustering.pdf

Examples

Basic Usage

bash
Rscript scripts/main.R \
  -i expression_matrix.csv \
  -g sample_info.csv \
  -o ./output

With Custom Metadata Columns

bash
Rscript scripts/main.R \
  -i expression_matrix.csv \
  -g metadata.csv \
  -o ./output \
  -n sample_id \
  -c condition \
  -b platform_batch

Disable Log Transform and Timeout

bash
Rscript scripts/main.R \
  -i expression_matrix.csv \
  -g sample_info.csv \
  -o ./output \
  -l no \
  -t 0 \
  -s 42

Error Handling

Common Errors

ErrorCauseSolution
SKILL_FILE_NOT_FOUNDInput file does not existCheck file path
SKILL_EMPTY_FILEInput file exists but contains no dataRecreate or re-export the file
SKILL_MISSING_COLUMNSMetadata file is missing sample, group, or batch columnsCheck header names or pass custom column names
SKILL_SAMPLE_MISMATCHMetadata sample IDs do not match expression matrix columnsVerify sample names between files
SKILL_INVALID_DATADataset fails minimum design checks (< 2 batches, < 2 groups, < 2 samples per batch/group)Review group counts, batch counts, and ID validity
SKILL_INVALID_TYPEExpression values are non-numeric or non-finiteClean matrix values before running
SKILL_TIMEOUTRun exceeded the configured time limitIncrease --timeout_seconds or set it to 0
SKILL_DEPENDENCY_MISSINGRequired R package is not installedInstall with: Rscript -e "BiocManager::install(c('sva','limma')); install.packages('ggplot2')"
SKILL_RUNTIME_ERRORRuntime I/O or filesystem error occurredCheck read/write permissions and environment

IF error persists, READ: references/troubleshooting.md

Troubleshooting note: In environments where packages are not yet installed, SKILL_DEPENDENCY_MISSING will fire before file-validation or --help. Install dependencies first, then re-run to expose file-related errors or access --help.


Input Validation

This skill accepts:

  1. A bulk RNA-seq or microarray expression matrix (CSV, genes as rows, samples as columns)
  2. A sample metadata file (CSV) with sample ID, biological group, and batch columns; at least 2 batches and 2 biological groups are required

If the user's request does not involve batch effect correction on merged bulk expression matrices — for example, asking to integrate single-cell RNA-seq data, process raw FASTQ files, run differential expression without batch labels, or analyze datasets with only one batch — do not proceed with the workflow. Instead respond:

"Batch Effect Correction is designed to remove batch-driven variation from merged bulk expression matrices using ComBat, while preserving biological group structure. Your request appears to be outside this scope. Please provide a multi-batch expression matrix with sample-level batch metadata, or use a more appropriate tool for single-cell integration, differential expression, or raw sequencing processing."


Testing

Test with Sample Data

bash
# Check help (requires packages installed)
Rscript scripts/main.R --help

# Run with bundled test data
Rscript scripts/main.R \
  -i tests/data/expression_matrix_merged.csv \
  -g tests/data/sample_info.csv \
  -o tests/output/

Validation Commands

bash
# Check corrected matrix exists
ls -la tests/output/corrected_expression_matrix.csv

# Check matched metadata exists
ls -la tests/output/matched_sample_info.csv

# Check PCA output exists
ls -la tests/output/batch_after_pca.pdf

Implementation Checklist

  • CLI parsing with optparse
  • set.seed() for reproducibility
  • requireNamespace() dependency checks
  • Session info recording
  • Time-limit support through setTimeLimit()
  • File reading instructions in SKILL.md
  • Modular script structure in scripts/
  • Test data provided
  • Error handling with SKILL_* codes
  • QC plots generated before and after correction
  • References in references/ directory

Last updated: 2026-04-27 | Version: 1.1.0

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the Batch Effect Correction AI skill do?

Use when correcting batch effects in merged bulk expression matrices with sample-level batch metadata while preserving biological group structure and generating before-and-after QC plots. NOT for: single-cell integration, raw FASTQ processing, differential expression without batch labels, or datasets without biological groups.

Why use Batch Effect Correction on TypingMind?

Because you install it once and use it with any model. Batch Effect Correction is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Batch Effect Correction in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/aipoch/medical-research-skills/tree/main/awesome-med-research-skills/Data%20Analysis/batch-effect-correction. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use Batch Effect Correction?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Batch Effect Correction?

As many as you like. As long as a model supports skills, you can use Batch Effect Correction with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Batch Effect Correction AI skill free?

Yes. It is published on GitHub by aipoch under the MIT license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇