AGENT SKILL

compare-perturbations-to-control

Per-perturbation differential expression against one shared non-targeting control in a pooled CRISPR screen with single-cell readout (Perturb-seq, CROP-seq, CRISPRi or CRISPRa) with scanpy rank_genes_groups (Wilcoxon, explicit reference), from an AnnData with a guide or perturbation column to one DE table per perturbation, a summary table with a target self-knockdown positive control, and the genes shared across perturbations. Use when every guide or target gene must be compared with the same control cells (the AiScientist run_de tool does per-cluster one-vs-rest and does not do this), when collapsing guides to target genes, or when checking which guides lowered their own target.

License 许可
MIT
最近更新
2026-11-14
Stars
★ 0
Forks
2

01 前言 Front Matter

name
compare-perturbations-to-control
license
MIT
metadata
{ "tags": "crispr, perturb-seq, crop-seq, scrna-seq, differential-expression, scanpy, wilcoxon", "author": "Yijun Sun", "status": "community", "aliases": "perturbation_de_vs_control", "compute": "1 CPU, under 0.5 GB RAM, seconds for 270 cells x 49 genes (4 perturbations)", "version": "1.0.0", "category": "crispr-screens", "scheduler": "none", "tested-with": "scanpy 1.12.1, anndata 0.12.16, pandas 2.3.3, numpy 2.4.6, scipy 1.17.1, Python 3.13.9", "requires-gpu": "false", "requires-network": "install", "requires-secrets": "none" }
description
Per-perturbation differential expression against one shared non-targeting control in a pooled CRISPR screen with single-cell readout (Perturb-seq, CROP-seq, CRISPRi or CRISPRa) with scanpy rank_genes_groups (Wilcoxon, explicit reference), from an AnnData with a guide or perturbation column to one DE table per perturbation, a summary table with a target self-knockdown positive control, and the genes shared across perturbations. Use when every guide or target gene must be compared with the same control cells (the AiScientist run_de tool does per-cluster one-vs-rest and does not do this), when collapsing guides to target genes, or when checking which guides lowered their own target.
compatibility
Python 3.12+ with scanpy and anndata (conda or pip). No GPU, no scheduler.

02 资源文件 2

03 SKILL.md 正文

Compare each perturbation with the non-targeting control

scripts/reference.py is the core of a Perturb-seq report. For each perturbation it compares that perturbation's cells with the one non-targeting control group, using sc.tl.rank_genes_groups(..., groups=[pert], reference=CONTROL, method="wilcoxon") in a loop over the perturbation column. Unlike a two-group condition study, many perturbations share a single reference, so a per-cluster one-vs-rest test (AiScientist's run_de) does not answer the question. The script writes one DE table per perturbation, a summary table with a self-knockdown positive control, and the genes that move the same way in two or more perturbations, and prints a JSON summary.

The most likely mistake is running it on every guide. In a pooled screen most guides are silent, and testing them all spends power and fills the report with noise. Rank the perturbations first (the rank-perturbations-by-edistance skill) and set ONLY_PERTURBATIONS to its hits.

Tested on synthetic data only (tests/: 270 cells, 49 genes, a non-targeting control of 80 cells; one knockout that loses its own target and switches on a 5-gene program, one silent guide, one guide with 10 cells, one guide that switches on the same program without losing its target; and a guide-level variant with two 20-cell guides of one gene) with scanpy 1.12.1, anndata 0.12.16, pandas 2.3.3, numpy 2.4.6, scipy 1.17.1 and Python 3.13.9. It has been the AiScientist template for this step of the Perturb-seq pipeline; no real-screen run is recorded here.

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__)"

pip install scanpy==1.12.1 anndata==0.12.16 in a Python 3.12+ venv works too. Inside an AiScientist run, the analysis image already has scanpy.

2. Prepare inputs

  • An AnnData file with log-normalized expression in X (the Wilcoxon fold changes assume log1p data) and a guide or perturbation label per cell in an obs column. The script reads adata_qc.h5ad from $AISCIENTIST_WORK (it keeps the original obs labels), else the file named by $AISCIENTIST_DATASET.
  • $AISCIENTIST_ARTIFACTS is where the tables go (tables/ is created).

Find the screen design in the data before configuring:

  1. The perturbation column: a usually high-cardinality obs column naming the guide or target per cell (perturbation, guide, gRNA, sgRNA, target, target_gene, gene_target, knockout, feature_call). Dozens or hundreds of levels are what separate a screen from a two-group condition dataset.
  2. The non-targeting control level: NT, NTC, non-targeting, control, ctrl, sgLacZ, sgGFP, sgScramble, scramble, unperturbed, none. State which level you chose. If it is genuinely ambiguous, ask rather than guess: a wrong control mis-references every comparison.
  3. Guides or targets: with several sgRNAs per gene (TP53_sg1, TP53_sg2), concordant guides of the same gene are biological replicates. Collapsing them to the target gene adds power when guide-level groups are small.
  4. No perturbation column? If guide identity lives in a separate guide-count layer, obsm or a feature-barcode matrix, cells must be assigned to a guide first (max-count or demultiplexing). That is a separate step. Never invent a perturbation column.

Count cells per perturbation and keep the control cells through QC: every comparison is against them.

3. Configure

Edit the values at the top of the script:

Variable Meaning Default
PERT_KEY the obs column naming the guide or perturbation perturbation
CONTROL the shared non-targeting level of PERT_KEY NT
ONLY_PERTURBATIONS None for every non-control level, or a list (the E-distance hits) None
COLLAPSE_GUIDE_TO_TARGET True maps TP53_sg1, TP53-sg1, TP53_2, TP53-2 to TP53 (strips one trailing _ or -, optional sg, digits); the control level is left as is False
MIN_CELLS a perturbation with fewer cells is skipped; a control with fewer stops the run 30
LFC |log2 fold change| threshold for a significant gene 1.0
PADJ adjusted p-value threshold 0.05

Before collapsing, print the mapping from guide to target once. Control guides named NT_1, NT_2 collapse into NT, which is usually what you want. A label without a guide suffix whose gene symbol itself ends in -<n> loses it (NKX2-1 becomes NKX2), so check symbols such as NKX2-1 or KRTAP1-1.

To use the cells left after removing escapers (the filter-escapers-with-mixscape skill), change the checkpoint line to read adata_mixscape.h5ad instead of adata_qc.h5ad. Change the thresholds only with a reason, and say so in the report.

4. Run

export AISCIENTIST_WORK=/path/to/work AISCIENTIST_ARTIFACTS=/path/to/results
# or, with no adata_qc.h5ad in the work directory:
export AISCIENTIST_DATASET=/path/to/screen.h5ad
python scripts/reference.py

In AiScientist: read the file with read_skill_reference, set the values for this dataset, and run it with run_code, which sets the variables.

Memory: the AnnData is loaded once and only one two-group subset (one perturbation plus the control) exists at a time, freed after each perturbation. Do not rewrite the loop to hold every subset at once: a large screen then runs out of memory and is killed. Give large screens a job with a real memory limit.

5. Validate before using the output

Outputs under $AISCIENTIST_ARTIFACTS/tables/:

Path Contents
perturbation_DE/DE_<perturbation>.csv every gene's statistics for one perturbation vs control, sorted by pvals_adj
perturbation_DE_summary.csv per perturbation: n_cells, n_control, n_DEG, n_up, n_down, target_self_knockdown, skipped
shared_up_across_perturbations.csv, shared_down_across_perturbations.csv genes significant in the same direction in two or more perturbations, most shared first

Check, in this order:

  1. The JSON summary: control, level (guide or target), n_perturbations_tested and n_skipped_low_cells. Every skipped perturbation is in the summary table with skipped = too_few_cells; list them in the report.
  2. The positive control (target_self_knockdown): a working knockout or knockdown shows its own target gene down. down passes; NOT_down(lfc=...) is listed in failed_self_knockdown_positive_control and points to a mislabel or no editing. A silent guide that also fails it most likely never edited. The check runs only when the label is a gene symbol in var_names, so guide-level labels such as TP53_sg1 give n/a; collapse to targets to get it. Two limits: in a CRISPRa screen the target should go up, so read NOT_down there as expected; and a Cas9 knockout does not always lower the transcript (it depends on nonsense-mediated decay), so a NOT_down knockout is weaker evidence than a NOT_down CRISPRi guide.
  3. Each DE table: fold changes of a few units, not tens (tens suggest raw counts in X). Mitochondrial, ribosomal or stress genes at the top of many perturbations suggest a quality difference between the perturbation's cells and the control rather than biology.
  4. Shared genes: programs that several perturbations move the same way are candidate shared pathways or modules. They are hypotheses, not findings.

The JSON next hint suggests enrichment on each strong perturbation's up and down gene lists (AiScientist: run_enrichment); send gene symbols only.

Report

The perturbation column, the control level, guide or target level, MIN_CELLS, the thresholds, and every perturbation skipped for low cell count. Per tested perturbation: cell count, n_DEG (up/down), the top genes with log fold change and adjusted p-value, never gene names alone, and the self-knockdown result; flag every guide that fails it. Say whether escaping cells were removed first (Mixscape) or the DE ran on all guide-assigned cells (conservative, dilutes effects). Silent perturbations are a result, not a failure. Cells are not independent replicates, so the Wilcoxon p-values are optimistic; with several replicate lanes or donors, say so. Never fabricate a gene, fold change, pathway or perturbation, and frame the biology as hypotheses to validate.

04 同仓库更多 Skills

lijinbio
lijinbio/aiscientist-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").

★ 0
lijinbio
lijinbio/aiscientist-skills

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.

★ 0
lijinbio
lijinbio/aiscientist-skills

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.

★ 0
lijinbio
lijinbio/aiscientist-skills

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.

★ 0
lijinbio
lijinbio/aiscientist-skills

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.

★ 0
lijinbio
lijinbio/aiscientist-skills

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.

★ 0