compare-conditions-by-celltype
Condition-vs-control differential expression within each cell type (for example KO vs WT, or disease vs control, per cell type) with scanpy rank_genes_groups (Wilcoxon, explicit reference group). Takes an AnnData with a condition column and an existing cell-type column and writes per-cell-type DE tables, a volcano plot per cell type, a summary table, the genes changed in the same direction in two or more cell types, and the de_KEY_all.csv and universe hand-off files that enrichment reads. Use for a stratified condition comparison outside AiScientist, or inside it when the design is one the built-in tool cannot express (a paired or covariate-adjusted comparison, a custom shared-signature rule, a non-standard figure). In AiScientist, run_de with groupby, reference and stratify_by covers the common case.
- License 许可
- MIT
- 最近更新
- 2026-11-14
- Stars
- ★ 0
- Forks
- 2
01 前言 Front Matter
- name
- compare-conditions-by-celltype
- license
- MIT
- metadata
- { "tags": "scrna-seq, snrna-seq, differential-expression, scanpy, wilcoxon, cell-type", "author": "Yijun Sun", "status": "community", "aliases": "condition_by_celltype", "compute": "1 CPU, under 1 GB RAM (0.3 GB peak), about 2 s for 260 cells x 50 genes", "version": "1.0.0", "category": "single-cell", "scheduler": "none", "tested-with": "scanpy 1.12.1, anndata 0.12.16, pandas 2.3.3, numpy 2.4.6, matplotlib 3.10.9, Python 3.13.9", "requires-gpu": "false", "requires-network": "install", "requires-secrets": "none" }
- description
- Condition-vs-control differential expression within each cell type (for example KO vs WT, or disease vs control, per cell type) with scanpy rank_genes_groups (Wilcoxon, explicit reference group). Takes an AnnData with a condition column and an existing cell-type column and writes per-cell-type DE tables, a volcano plot per cell type, a summary table, the genes changed in the same direction in two or more cell types, and the de_KEY_all.csv and universe hand-off files that enrichment reads. Use for a stratified condition comparison outside AiScientist, or inside it when the design is one the built-in tool cannot express (a paired or covariate-adjusted comparison, a custom shared-signature rule, a non-standard figure). In AiScientist, run_de with groupby, reference and stratify_by covers the common case.
- compatibility
- Python 3.12+ with scanpy, anndata and matplotlib (conda or pip). No GPU, no scheduler.
02 资源文件 2
- 脚本scripts/reference.py7.8 KB
- 脚本tests/test_compare_conditions_by_celltype.py4.9 KB
03 SKILL.md 正文
Compare conditions within each cell type
scripts/reference.py runs, for each cell type, sc.tl.rank_genes_groups(..., groups=[CONDITION], reference=REFERENCE, method="wilcoxon") on the cells of that type, then collects the changed
genes and looks for a shared cross-cell-type signature: genes changed in the same direction in at
least two cell types. It is the pattern behind a KO-vs-WT or disease-vs-control report.
The most likely mistakes are re-clustering a dataset that already carries cell-type labels
(reuse the existing column) and dropping the hand-off files tables/de_<CELLTYPE_KEY>_all.csv and
tables/de_<CELLTYPE_KEY>_universe.txt. Enrichment tools discover DE results by exactly those
names. Without them, AiScientist's run_enrichment falls back to a single pooled gene list (every
cell type collapsed into one group, the stratification lost) and to a constant 20,000-gene
over-representation background, which inflates every enrichment p-value.
Tested on synthetic data only (tests/: 260 cells x 50 genes, log-normalised; Rod, Cone and MG
with 40 KO and 40 WT cells each, a rare type with 10 + 10 cells that must be skipped, two genes
planted up and two down in KO in both Rod and Cone, one up only in MG) with scanpy 1.12.1,
anndata 0.12.16, pandas 2.3.3, numpy 2.4.6, matplotlib 3.10.9 and Python 3.13.9.
When to use it
In AiScientist, use the tool first:
run_de(groupby="<condition column>", reference="<control level>", stratify_by="<cell-type column>")
It runs condition-vs-reference within each cell type, writes the tables run_enrichment and
run_gsea_prerank discover, reports up/down counts per cell type, and lists the cell types it
skipped for too few cells. It needs only QC (run_scanpy_qc) first. Use this script when the tool
cannot express the design (a paired or covariate-adjusted comparison, a custom shared-signature
rule, a figure the tool does not draw), or outside AiScientist. It is the same
rank_genes_groups(reference=...) loop, written out so you can change it.
1. Environment setup
mamba create -y -n scanpy_1.12 -c conda-forge python=3.13 scanpy=1.12.1 anndata=0.12.16
source "$(conda info --base)/etc/profile.d/conda.sh"
conda activate scanpy_1.12
python -c "import scanpy; print(scanpy.__version__)"
Inside an AiScientist run, the analysis image already has scanpy.
2. Prepare inputs
- An AnnData with log-normalised expression (Wilcoxon fold changes assume log1p data), a
condition column and an existing cell-type column in
obs. The script reads$AISCIENTIST_WORK/adata_qc.h5ad(the QC checkpoint, which keeps the originalobslabels), else the file named by$AISCIENTIST_DATASET. - If the object has
.raw, scanpy tests.rawby default; otherwiseX. $AISCIENTIST_ARTIFACTSis where tables and figures go.
Check before running: both columns exist, both condition levels are present, and how many cells
of each level each cell type has (pd.crosstab(adata.obs[CELLTYPE_KEY], adata.obs[CONDITION_KEY])).
3. Configure
Edit the CONFIG block at the top of the script to columns and levels that exist in adata.obs:
| Variable | Meaning | Default |
|---|---|---|
CONDITION_KEY |
obs column naming the experimental groups |
sampleid |
CONDITION |
the group of interest (case) | KO |
REFERENCE |
the baseline / control group | WT |
CELLTYPE_KEY |
existing cell-type column to stratify by (reuse it) | majorclass |
MIN_CELLS |
skip a cell type with fewer cells than this in either group | 30 |
LFC |
|log2FC| threshold for "significant" | 1.0 |
PADJ |
adjusted-p threshold | 0.05 |
4. Run
export AISCIENTIST_WORK=/path/to/work AISCIENTIST_ARTIFACTS=/path/to/results
export AISCIENTIST_DATASET=/path/to/dataset.h5ad # only read when work/ has no adata_qc.h5ad
python scripts/reference.py
In AiScientist: read the script with read_skill_reference, adapt the CONFIG values to this
dataset, and run it with run_code, which sets the variables. Afterwards call run_enrichment
with no genes argument, so it picks up tables/de_<CELLTYPE_KEY>_all.csv itself and runs
over-representation per cell type and per direction.
Outputs:
| Path | Contents |
|---|---|
tables/DEG/DEG_<celltype>.csv |
every gene's statistics for that cell type, sorted by adjusted p |
figures/DEG/volcano_<celltype>.png |
log2FC against −log10(adjusted p), significant genes highlighted |
tables/DEG_summary.csv |
per cell type: cells per group, significant / up / down counts, skipped |
tables/shared_up_DEGs.csv, shared_down_DEGs.csv |
genes significant in the same direction in two or more cell types, most shared first |
tables/de_<CELLTYPE_KEY>_all.csv |
hand-off: significant rows of every cell type, both directions, columns group, gene, log2fc, pval, pval_adj, score |
tables/de_<CELLTYPE_KEY>_universe.txt |
hand-off: every gene tested, one per line (the enrichment background) |
Keep the two hand-off files exactly as named. Their group column is the cell type, and both
directions are in them: a rule that keeps only the top of a score-ordered list would silently drop
every down-regulated gene. No DE tool labels this table, so prioritize-disease-genes-with-atlas
reads it only with de_is_condition_contrast = True.
5. Validate before using the output
per_celltypein the printed summary: which cell types were tested and which were skipped astoo_few_cells(unused categories of a categorical column also appear there with 0 cells).DEG_<celltype>.csv: fold changes of a few units, not tens (tens suggest raw counts in the matrix). Housekeeping, mitochondrial or ribosomal genes at the top of every cell type suggest a quality difference between the conditions rather than biology.- The shared signature: a gene shared across many cell types is either a real global response or an ambient-RNA or batch effect; check whether it is a highly expressed marker of one abundant cell type.
- The hand-off files exist and the table holds both signs of
log2fc.
Report
The comparison (condition column, case, reference), the cell-type column, the thresholds, per cell type the cell counts and up/down numbers, the skipped cell types, and the shared up and down genes. Cells are not independent replicates: with several samples per condition, say so, treat the Wilcoxon p-values as an exploratory ranking, and prefer a pseudobulk test per sample.
04 同仓库更多 Skills
verb-object-with-tool
One or two sentences on what the skill does and when to use it. Name the tool, the input it takes and the output it produces, then list the situations that should trigger it (for example "Use when setting up X, writing a Y config, or diagnosing a failed Z run").
write-variant-tables
Write the five standard variant-annotation result tables - consequence, impact and ClinVar-significance distributions, a ClinVar pathogenic and likely-pathogenic list, and a shortlist of rare, damaging variants not in ClinVar - plus a JSON summary, from an annotated per-variant TSV (VEP consequence and impact, SIFT, PolyPhen, max population AF, rsID, ClinVar significance), in pure Python without re-parsing the VCF or re-running VEP. Use when a study needs these tables with a different rarity cutoff, impact set or damaging-predictor rule, or when rebuilding them from an existing annotation table. In AiScientist, annotate_variants already writes the default tables, so use this only for a custom cut.
rank-phenotype-differentials-with-mcp
Map clinical phenotype text to grounded HPO terms and rank a research differential through an already configured phenotype-diagnosis MCP service with LIRICAL and optional literature adjudication. Use for explicit service-based analysis while preserving negation, uncertainty and original probabilities; retain the established workflow when the service is unavailable.
qc-vcf-callset
Judge whether a germline VCF callset is trustworthy before interpreting any variant - Ti/Tv, Het/Hom, SNP, indel and multiallelic counts, sample count and genotype call rate from bcftools stats (falling back to cyvcf2, then to a plain-Python Ti/Tv scan) - and flag values outside the expected human WGS or WES ranges. Writes tables/vcf_qc_summary.csv and data/vcf_qc.json with a qc_flags list. Use when a VCF (one sample or a cohort) arrives for annotation or prioritization, when a Ti/Tv or Het/Hom value needs interpreting, or when deciding whether downstream variant tables must carry a low-confidence caveat.
prioritize-clinical-variants
Rank annotated variants into research-triage tiers (ClinVar pathogenic, likely pathogenic, VUS favouring pathogenic, VUS, likely benign, benign) from ClinVar significance used verbatim, gnomAD allele frequency against a dominant or recessive disease-model cutoff and the ACMG BA1 5 percent rule, and in-silico predictors (SIFT, PolyPhen, REVEL, CADD, AlphaMissense) as supporting evidence only. Pure Python; reads a per-variant annotation TSV and writes a tier-sorted CSV with the evidence for each tier plus a JSON of tier counts. Use when a rare-variant list from VEP, ClinVar and gnomAD annotation must be ordered for review (which variants to look at first) or when choosing the disease model and cutoffs for that ordering. Research triage only, NOT a clinical ACMG/AMP classification.
normalize-vcf-with-bcftools
Normalize a VCF with bcftools norm before annotation or ClinVar/gnomAD matching - split multiallelic sites into biallelic records, atomize MNPs into SNPs, then left-align and trim indels against the reference FASTA of the same build - writing an indexed normalized.vcf.gz and a JSON of record counts before and after. Use when a VCF not known to be normalized (especially an indel-heavy one) will be matched against ClinVar, gnomAD or dbSNP, compared between callers or loaded into PLINK, or when a known pathogenic indel comes back as not in ClinVar. In AiScientist, the offline annotate_variants path already normalizes, so do not plan it as a separate step there.