K-Dense-AI/scientific-agent-skills / skills/scanpy
scanpy
VerifiedPerforms Scanpy single-cell RNA-seq QC, normalization, HVG selection, PCA/UMAP/t-SNE, clustering, exploratory marker ranking, pseudobulk preparation, visualization, and Seurat or SingleCellExperiment RDS conversion to h5ad. Applies to established exploratory scRNA-seq workflows with explicit count and expression provenance; complementary skills cover scvi-tools models and AnnData format details.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 19.8 KB |
| assets/analysis_template.py | script | 10.0 KB |
| assets/celltype_mapping.json | config | 184 B |
| assets/gene_signatures.json | config | 355 B |
| assets/pipeline_config.json | config | 380 B |
| references/analysis_workflow.md | markdown | 7.6 KB |
| references/api_reference.md | markdown | 9.1 KB |
| references/plotting_guide.md | markdown | 10.5 KB |
| references/r_interop.md | markdown | 11.8 KB |
| references/standard_workflow.md | markdown | 7.0 KB |
| references/upstream-review.md | markdown | 8.3 KB |
| scripts/_common.py | script | 10.0 KB |
| scripts/annotate.py | script | 3.7 KB |
| scripts/batch_correct.py | script | 3.1 KB |
| scripts/cluster.py | script | 2.5 KB |
| scripts/convert.py | script | 1.5 KB |
| scripts/find_markers.py | script | 4.3 KB |
| scripts/inspect_data.py | script | 3.1 KB |
| scripts/plot.py | script | 3.6 KB |
| scripts/preprocess.py | script | 3.5 KB |
| scripts/pseudobulk.py | script | 4.6 KB |
| scripts/qc_analysis.pyexecutable | script | 5.1 KB |
| scripts/reduce_dimensions.py | script | 2.8 KB |
| scripts/run_pipeline.py | script | 9.4 KB |
| scripts/score_genes.py | script | 3.9 KB |
| scripts/subset.py | script | 2.6 KB |
scripts/qc_analysis.py
View on GitHub#!/usr/bin/env python3
"""
Quality control and filtering for single-cell RNA-seq data.
Calculates QC metrics (genes/counts per cell, mitochondrial / ribosomal /
hemoglobin fractions), writes before/after QC plots, optionally runs Scrublet
doublet detection, and filters cells and genes by the given thresholds.
Run this FIRST on raw counts, before normalization.
Examples:
python qc_analysis.py raw.h5ad -o filtered.h5ad
python qc_analysis.py raw.h5ad -o filtered.h5ad --mt-threshold 10 --min-genes 500
python qc_analysis.py 10x_dir/ -o filtered.h5ad --max-genes 6000 --scrublet
"""
import argparse
from _common import add_io_args, configure_scanpy, info, load_anndata, save_anndata, die, prepare_counts, ensure_categories
def annotate_gene_classes(adata):
"""Flag mitochondrial, ribosomal, and hemoglobin genes (human or mouse names)."""
names = adata.var_names
adata.var["mt"] = names.str.startswith(("MT-", "mt-", "Mt-"))
adata.var["ribo"] = names.str.startswith(("RPS", "RPL", "Rps", "Rpl"))
adata.var["hb"] = names.str.contains(r"^HB[^P]|^Hb[^p]", regex=True)
return [v for v in ["mt", "ribo", "hb"] if adata.var[v].any()]
def make_qc_plots(sc, adata, prefix):
qc_keys = ["n_genes_by_counts", "total_counts", "pct_counts_mt"]
qc_keys = [k for k in qc_keys if k in adata.obs.columns]
sc.pl.violin(adata, qc_keys, jitter=0.4, multi_panel=True,
show=False, save=f"_{prefix}_violin.png")
if "pct_counts_mt" in adata.obs.columns:
sc.pl.scatter(adata, x="total_counts", y="pct_counts_mt",
show=False, save=f"_{prefix}_mt.png")
sc.pl.scatter(adata, x="total_counts", y="n_genes_by_counts",
show=False, save=f"_{prefix}_counts.png")
def main():
p = argparse.ArgumentParser(description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
add_io_args(p, default_output="qc_filtered.h5ad")
p.add_argument("--counts-layer", default=None, help="Explicit raw-count layer instead of X")
p.add_argument("--batch-key", default=None, help="Capture/library ID for separate Scrublet runs")
p.add_argument("--min-genes", type=int, default=200, help="Min genes per cell (default 200)")
p.add_argument("--max-genes", type=int, default=None, help="Max genes per cell (upper outliers)")
p.add_argument("--min-counts", type=int, default=None, help="Min total counts per cell")
p.add_argument("--max-counts", type=int, default=None, help="Max total counts per cell")
p.add_argument("--min-cells", type=int, default=3, help="Min cells per gene (default 3)")
p.add_argument("--mt-threshold", type=float, default=5, help="Max pct mitochondrial counts (default 5)")
p.add_argument("--scrublet", action="store_true", help="Run Scrublet doublet detection and drop doublets")
p.add_argument("--no-plots", action="store_true", help="Skip QC plots")
args = p.parse_args()
sc = configure_scanpy(figdir=args.figdir)
adata = load_anndata(args.input)
prepare_counts(adata, args.counts_layer)
if args.batch_key:
ensure_categories(adata, args.batch_key)
adata.var_names_make_unique()
info(f"Loaded {adata.n_obs} cells x {adata.n_vars} genes")
qc_vars = annotate_gene_classes(adata)
sc.pp.calculate_qc_metrics(adata, qc_vars=qc_vars, percent_top=None,
log1p=False, inplace=True)
info(f"Mean genes/cell={adata.obs['n_genes_by_counts'].mean():.0f} "
f"mean counts/cell={adata.obs['total_counts'].mean():.0f} "
f"mean pct_mt={(adata.obs['pct_counts_mt'].mean() if 'pct_counts_mt' in adata.obs else 0):.1f}")
if not args.no_plots:
make_qc_plots(sc, adata, "qc_before")
n0, g0 = adata.n_obs, adata.n_vars
sc.pp.filter_cells(adata, min_genes=args.min_genes)
if args.min_counts:
sc.pp.filter_cells(adata, min_counts=args.min_counts)
if args.max_genes:
adata = adata[adata.obs["n_genes_by_counts"] < args.max_genes, :].copy()
if args.max_counts:
adata = adata[adata.obs["total_counts"] < args.max_counts, :].copy()
if "pct_counts_mt" in adata.obs.columns:
adata = adata[adata.obs["pct_counts_mt"] < args.mt_threshold, :].copy()
sc.pp.filter_genes(adata, min_cells=args.min_cells)
if min(adata.shape) == 0:
die("QC removed all cells or genes; inspect thresholds")
if args.scrublet:
info("Running Scrublet doublet detection...")
try:
sc.pp.scrublet(adata, batch_key=args.batch_key, random_state=0)
except (ImportError, ValueError) as e:
die(f"Scrublet failed ({e}). Check raw counts, library size and scikit-image installation")
n_dbl = int(adata.obs["predicted_doublet"].sum())
adata = adata[~adata.obs["predicted_doublet"], :].copy()
info(f"Removed {n_dbl} predicted doublets")
info(f"Cells {n0} -> {adata.n_obs} ({adata.n_obs / n0 * 100:.1f}% kept) "
f"Genes {g0} -> {adata.n_vars}")
if not args.no_plots:
make_qc_plots(sc, adata, "qc_after")
save_anndata(adata, args.output)
if __name__ == "__main__":
main()
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness. | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependencies | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is available | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |