/lucid-coder:variance-partitionfrom lucid-coder
Variance partitioning of pseudobulk gene expression across sample covariates with R variancePartition: voomWithDreamWeights, then fitExtractVarPartModel with categorical covariates as random effects (1|x) and continuous ones as fixed, after a canCorPairs collinearity gate. Writes per-gene variance fractions, a median summary, the canonical-correlation matrix, a violin plot, percent bars and a heatmap. Fire on "variance partition", "variancePartition", "fitVarPart", "plotVarPart", "how much variance does donor explain", "which covariates drive expression", "covariate selection before DE", "canCorPairs". Do NOT fire for differential expression (use pseudobulk-de), for PCA-only covariate screens, for cell-level variance (no pseudobulk), or for cell-type composition.
Ships in lucid-coder. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them; scripts run unread.
You are the dispatcher for variance-partition. The script does the analysis; you collect its arguments, run it, and explain its outputs.
Run scripts/variance_partition.py. Never re-implement the analysis, change a
threshold, or pick a value the user has not given. If a required argument is
unknown, ask the user (these are grill questions), or run --demo only when the
user asks for a demo.
Then read result.json and relay chat_summary_lines and preferred_artifacts.
If the script prints ERROR: ... and exits 2, tell the user the message and the
argument it names. Do not work around it.
Environment: pixi feature bioc-de (pixi install -e bioc-de, from templates/pixi.toml).
Give one of two inputs: --counts with --meta and --donor-col, or --adata with
--gene-id-col, --sample-col, --donor-col and --min-cells. Giving both is an error.
| Input | Format | Required content | Example |
|---|---|---|---|
--counts | TSV | columns gene_id (Ensembl; empty when the source has none) and gene_symbol, then one column per sample; non-negative integer summed counts | pseudobulk-de tables/pseudobulk_counts.tsv; examples/demo_counts.tsv |
--meta | TSV | first column sample IDs, the same set as the --counts columns; every formula term and the --donor-col column | pseudobulk-de tables/sample_meta.tsv; examples/demo_meta.tsv |
--adata | .h5ad | raw integer counts in X, or in --layer; one cell per row | one cell type's cells |
--gene-id-col | var column | Ensembl gene ids; var_names are the symbols; none when the source has no ids | gene_ids |
--sample-col | obs column | the sample each cell belongs to; every formula term is constant within a sample | sample |
--donor-col | obs column (--adata) or --meta column (--counts) | the donor each sample comes from; constant within a sample | donor |
A (1|x) term is categorical, even when its values look numeric. A plain term
must be numeric (a continuous covariate); a categorical plain term is an error,
because variancePartition needs categorical covariates as random effects.
Required checks (docs/pipeline-skills.md, adopted 2026-10-27):
skillkit.check_unique stops on a repeated sample id in
--counts (header read as written) and --meta, and on a repeated cell id in
--adata.data.ids): skillkit.check_gene_ids stops on a
repeated gene id or a symbol with several ids. With no ids (--gene-id-col none, or an empty gene_id column) symbols must be unique and gene_id is
written as NA, never guessed.skillkit.check_design_rank;
aliased numeric covariates stop the run. Random terms are checked by
--max-cancor.skillkit.check_replication stops when a
donor gives several samples and the formula has no donor term. With --counts
the donor comes from the --donor-col column of --meta. result.json
reports n_donors.--reference (genome build and annotation release) is
required and written to result.json.dropped_samples).Ask these through the analysis contract: ${CLAUDE_PLUGIN_ROOT}/skills/lucid-coder/references/contracts.md.
Each row is a grill question. Ask the user; never fill one in.
| Argument | Meaning | Grill question | Demo-only value |
|---|---|---|---|
--counts | summed counts TSV | Which pseudobulk counts table? | examples/demo_counts.tsv |
--meta | sample metadata TSV | Which sample table holds the covariates? | examples/demo_meta.tsv |
--adata | cell-level h5ad (instead of the two above) | Which h5ad holds the cells, already subset to one cell type? | absent |
--layer (optional, --adata only) | raw-count layer | Which layer holds the raw counts? Absent means X. | absent |
--gene-id-col (--adata only) | var column of Ensembl ids, or none | Which var column holds the Ensembl gene ids (or none if the source has none)? | absent |
--sample-col (--adata only) | obs column of samples | Which obs column names the sample? | absent |
--donor-col | obs column (--adata) or --meta column (--counts) of donors | Which column names the donor? Several samples per donor need a donor term in --formula. | donor (--counts demo); absent with --adata |
--min-cells (--adata only) | sample floor | How many cells must a sample have to be kept? | absent |
--formula | model | Which covariates enter, and which are categorical (`(1 | x)) or continuous (x`)? |
--min-frac | gene filter | In what fraction of cells (--adata) or samples (--counts) must a gene be detected? | 0.1 |
--n-top | genes per term in the percent bars | How many top genes per term go in the percent bars? | 5 |
--max-cancor | collinearity ceiling | Above which canonical correlation between two terms should the run stop? | 0.9 |
--norm-method | edgeR normalisation | Which calcNormFactors method: TMM, TMMwsp, RLE, upperquartile or none? No default. Recommended: TMM, the method this skill used before it became an argument (edgeR 4.8.2 default of calcNormFactors). | TMM |
--voom | expression scale | yes: precision weights from voomWithDreamWeights (variancePartition 1.40.2) with the formula; no: edgeR cpm(log = TRUE) (log2 CPM, edgeR 4.8.2 default prior.count), no weights. No default. Recommended: yes, the call this skill used before it became an argument. | yes |
--reference | genome build and annotation | Which genome build and annotation release are the genes from (e.g. GRCh38, GENCODE 44)? | synthetic demo genes, no genome build |
--context | biological context, printed first in the report and summary | Species, tissue or cell type, condition and dataset? | synthetic demo: 12 donors, ctrl vs stim pseudobulk, no real tissue |
--seed | seed | Which seed? (Recorded; the lme4 fits draw no random numbers.) | 0 |
--outdir | new output directory | Which new directory gets the outputs? It must not exist. | variance_partition_demo |
The last column is demo-only, not defaults. --demo alone pins all of them.
With --demo, only --outdir may also be given; any other argument is an error.
python scripts/variance_partition.py \
--counts results/2026-10-27_mono-de/tables/pseudobulk_counts.tsv \
--meta results/2026-10-27_mono-de/tables/sample_meta.tsv \
--formula "~ (1|donor) + (1|condition) + (1|sex) + age" \
--donor-col donor --norm-method TMM --voom yes \
--min-frac 0.1 --n-top 5 --max-cancor 0.9 --reference "GRCh38, GENCODE 44" \
--context "human PBMC CD14 monocytes, IFN-beta vs control, 8 donors" \
--seed 0 --outdir results/2026-10-27_mono-varpart
# From cells
python scripts/variance_partition.py --adata data/mono.h5ad --layer counts \
--gene-id-col gene_ids --sample-col sample --donor-col donor --min-cells 10 \
--context "human PBMC CD14 monocytes, IFN-beta vs control, 8 donors" \
--formula "~ (1|donor) + (1|condition)" --min-frac 0.1 --n-top 5 \
--norm-method TMM --voom yes --max-cancor 0.9 --reference "GRCh38, GENCODE 44" --seed 0 --outdir results/mono-vp
# Demo (only when the user asks for one)
python scripts/variance_partition.py --demo --outdir demo_out
ERROR: <message>, exit 2.
The formula accepts only name and (1|name) terms joined by +, at most 23 (the largest colour-blind-safe palette).
Unique gene and sample ids; fewer than 3 --counts samples stops the run.--adata only): sum raw counts per --sample-col, then drop
samples with fewer than --min-cells cells, as in pseudobulk-de. Fewer than
3 samples left stops the run; then the donor-versus-sample check.--min-frac of the kept
samples' cells (--adata) or of the samples (--counts). The design rank of
the plain terms is checked here.canCorPairs(formula, meta). Any pair above
--max-cancor stops the run before the outdir exists; the error lists every
pair's value. Every refusal leaves no outdir.DGEList, calcNormFactors(method = --norm-method), then
voomWithDreamWeights with the formula (--voom yes) or cpm(log = TRUE)
(--voom no). result.json and the report summary name both.fitExtractVarPartModel(vobj, formula, meta), one mixed model per gene.sortCols does (median descending, Residuals last). Top genes per term: the
--n-top largest fractions, ties by gene name.variance_fractions.tsv has one row per input gene, sorted by gene symbol:
gene_symbol, gene_id, the terms in formula order, Residuals, status. A
tested row sums to 1. status (house-style data.status) is tested, or
not_detected (zero everywhere) or not_tested (under --min-frac) with NA
fractions; status_counts in result.json counts each. The summary table and
figures use tested genes only. summary.tsv is sorted by median fraction
descending. cancor.tsv keeps formula order on both axes.
outdir/
├── report.md # params, summary, figures with captions
├── result.json # n_samples, n_donors, n_genes, status_counts, gene_id_col (--adata), dropped_samples, context, reference, top_covariate, median_fractions, max_cancor, top_genes, norm_method, voom, params, seed, chat_summary_lines, preferred_artifacts
├── tables/
│ ├── variance_fractions.tsv # gene_symbol, gene_id, one column per term, Residuals, status
│ ├── summary.tsv # term, median_fraction, mean_fraction
│ └── cancor.tsv # term x term canonical correlations
├── figures/
│ ├── variance_violin.pdf
│ ├── variance_violin.png
│ ├── variance_violin.csv
│ ├── percent_bars.pdf
│ ├── percent_bars.png
│ ├── percent_bars.csv
│ ├── cancor_heatmap.pdf
│ ├── cancor_heatmap.png
│ └── cancor_heatmap.csv
└── reproducibility/
├── commands.sh # argv as run, and the resolved command with every argument
├── environment.txt # Python packages; R, variancePartition, limma, edgeR, lme4
└── checksums.sha256 # tables and figure PDFs; sha256sum -c from outdir
~ donor + condition) because DE formulas look like that. Do not.
variancePartition needs (1|donor); the script refuses the fixed form.--max-cancor or drop a term when the
collinearity check stops the run. Do not. Report the pairs and values from
the error message; which term to drop is the user's call.claims.strength).--reference from gene symbols. Do not. Ask
the user for the genome build and annotation release.--counts.
Do not. The script needs raw summed counts; voom does the normalisation.--demo only when asked for one.Not arguments: the script sets these, and reproducibility/environment.txt
records the package versions. A change is a maintainer edit, not a user choice.
fitExtractVarPartModel with package defaults (lme4
REML for random terms); voomWithDreamWeights with the formula when
--voom yes; canCorPairs for the collinearity check.cpm(log = TRUE) for --voom no uses the package default
prior.count (2).Corpus sources, ClawBio mechanisms, tests, citations and the plot spec are in references/maintainer.md. The agent running the script does not need them.
examples/demo_counts.tsv (opens in a new tab)examples/demo_meta.tsv (opens in a new tab)examples/make_demo.py (opens in a new tab)references/maintainer.md (opens in a new tab)scripts/palettes.py (opens in a new tab)scripts/plots.py (opens in a new tab)scripts/skillkit.py (opens in a new tab)scripts/theme.py (opens in a new tab)scripts/variance_partition.py (opens in a new tab)scripts/varpart.R (opens in a new tab)tests/test_variance_partition.py (opens in a new tab)What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub mdmanurung/lucid-coder --plugin lucid-coder27 skills in this plugin
/alignInstall the lucid-coder house-style configs into this repository, one file at a time, after showing each diff and asking. Use when the user says "align this repo", "install the house style configs", "set up air/ruff/lintr", or "add the palette file"./cccLigand-receptor cell-cell communication between cell types from a log-normalised single-cell h5ad with LIANA+ rank_aggregate (CellPhoneDB, Connectome, log2FC, NATMI, SingleCellSignalR, or a subset), a bundled or exported ligand-receptor resource TSV, a fixed pass rule on magnitude and specificity ranks, a source x target count heatmap, an LR dot plot, a report and a reproducibility bundle. Optional per-sample runs. Fire on "cell-cell communication", "ligand-receptor analysis", "which cell types talk to which", "LIANA", "CellPhoneDB", "rank_aggregate", "CCC between clusters". Do NOT fire for spatial CCC (bivariate, MISTy), for factorising CCC across many samples (Tensor-Cell2cell, MOFAtalk), for DE-driven CCC between conditions, for non-human data without a translated resource, or for raw counts./compositionCell-type composition and differential abundance with crumblr: counts cells per sample and label (from a cell-level h5ad or a counts TSV), drops rare labels, runs crumblr's precision-weighted CLR and variancePartition dream (fixed and random effects, e.g. (1|donor)), and writes tables, a stacked composition bar, per-label proportion boxes, an effect forest, a report and a reproducibility bundle. Fire on "cell-type composition", "differential abundance of cell types", "do cell-type proportions change", "crumblr", "CLR of cell frequencies", "which populations expand after stimulation". Do NOT fire for neighbourhood-level DA on a KNN graph (Milo), gene expression DE (pseudobulk or cell-level), scCODA / Bayesian composition models, or a plain Wilcoxon on frequencies (group-tests).Other plugins' skills for the same job