scrna-qc-preprocess
Skill4 starsChanged 5 months agoMITView on GitHub
scRNA-seq quality control and preprocessing pipeline. Handles doublet detection, mitochondrial/ribosomal QC filtering, normalization, HVG selection, and batch-aware preprocessing. Optimized for raw count matrices (.h5ad).
In this repository8 other files it gives its agents.
The file
We split this skill into the rules it gives an agent
3 rules. About 0 are common, 0 are distinctive, and 3 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 36
Always check if adata.X is sparse before operations.
- Very specific: line 44
Re-running on already preprocessed data: Check adata.raw or whether adata.X contains log-normalized values.
- Very specific: line 46
Forgetting batch_key: When multiple samples exist, omitting batch_key in HVG selection introduces batch-driven gene selection bias
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
scRNA-seq Quality Control & Preprocessing
When to Use
- Starting from raw count data that needs QC and filtering
- Removing low-quality cells (high mito%, low gene count, doublets)
- Normalizing and log-transforming expression data
- Selecting highly variable genes for downstream analysis
- Preparing data for clustering, trajectory, or communication analysis
Do NOT use when you need to:
- 数据已预处理且有 PCA/embeddings → (use scrna-clustering-umap directly)
- 需要批次校正多个样本 → (use scrna-batch-correction)
- 分析空间转录组数据 → (use spatial-neighborhood-analysis)
Prerequisites
- Python >= 3.10
- Required packages:
scanpy,anndata,numpy,scipy,pandas - Optional:
scrublet(doublet detection) - Input:
.h5adfile with raw count matrix (cells x genes). Expectedadata.Xto contain unnormalized integer counts.
Core Rules
- Always check if
adata.Xis sparse before operations; prefer sparse-compatible code (sc.pp.*handles this automatically) - Save raw counts in
adata.layers["counts"]before normalization — never lose the originals - Set
batch_keyinsc.pp.highly_variable_genes()when multiple samples/batches are present - For HVG selection, try
seurat_v3first (requires raw counts), fall back toseuratorcell_rangerwith logged data - Save intermediate results (e.g., after filtering, after normalization) to avoid re-computation on failures
Common Mistakes
- Re-running on already preprocessed data: Check
adata.rawor whetheradata.Xcontains log-normalized values; if so, skip normalization and HVG steps - Using dense matrix for large datasets: Convert to sparse with
adata.X = sparse.csr_matrix(adata.X)to save memory - Forgetting batch_key: When multiple samples exist, omitting
batch_keyin HVG selection introduces batch-driven gene selection bias - Over-filtering: Setting
min_genestoo high removes rare but biologically important cell types
Troubleshooting
| 问题 | 原因 | 解决方案 |
HVG selection fails with seurat_v3 | adata.X is not raw counts | Use flavor="seurat" which works on logged data |
| Memory error during processing | Dense matrix on large dataset | Convert to sparse: adata.X = sparse.csr_matrix(adata.X) |
| All cells filtered out | Thresholds too strict | Lower min_genes to 100, raise pct_counts_mt to 25 |
| PCA has NaN values | NaN in scaled data | Run np.nan_to_num(adata.X, nan=0.0) before sc.pp.scale |
Output
写入 --output 指定的 JSON 文件:
{
"skill": "scrna-qc-preprocess",
"status": "success",
"steps": ["load", "qc_metrics", "doublet_detection", "filtering", "normalization", "hvg_selection", "scaling", "pca"],
"result": {
"output_h5ad": "path/to/preprocessed.h5ad",
"n_cells": 5000,
"n_genes": 2000,
"n_hvg": 2000,
"pct_mito_filtered": 5.2,
"n_doublets_detected": 120
}
}Workflow
Step 1: Load and Inspect Data
import scanpy as sc
import numpy as np
import pandas as pd
from scipy import sparse
adata = sc.read_h5ad("input.h5ad")
print(f"Shape: {adata.shape}")
print(f"X type: {type(adata.X).__name__}")
print(f"Is sparse: {sparse.issparse(adata.X)}")
print(f"obs columns: {list(adata.obs.columns)}")
print(f"var columns: {list(adata.var.columns)}")Step 2: QC Metrics Calculation
# Identify mitochondrial and ribosomal genes
adata.var["mt"] = adata.var_names.str.startswith(("MT-", "mt-", "Mt-"))
adata.var["ribo"] = adata.var_names.str.startswith(("RPS", "RPL", "rps", "rpl"))
# Calculate QC metrics
sc.pp.calculate_qc_metrics(
adata, qc_vars=["mt", "ribo"], percent_top=None, log1p=False, inplace=True
)
# Key QC columns now in adata.obs:
# - n_genes_by_counts: number of genes expressed per cell
# - total_counts: total UMI counts per cell
# - pct_counts_mt: percentage of mitochondrial reads
# - pct_counts_ribo: percentage of ribosomal readsStep 3: Doublet Detection (Optional but Recommended)
try:
import scrublet as scr
scrub = scr.Scrublet(adata.X)
doublet_scores, predicted_doublets = scrub.scrub_doublets(
min_counts=2, min_cells=3, n_prin_comps=30
)
adata.obs["doublet_score"] = doublet_scores
adata.obs["predicted_doublet"] = predicted_doublets
print(f"Detected {predicted_doublets.sum()} doublets ({predicted_doublets.mean():.1%})")
except ImportError:
print("scrublet not available, skipping doublet detection")Step 4: Filtering
# Standard filtering thresholds (adjust per dataset)
sc.pp.filter_cells(adata, min_genes=200)
sc.pp.filter_genes(adata, min_cells=3)
# Mito filter: keep cells with < 20% mito reads
adata = adata[adata.obs.pct_counts_mt < 20].copy()
# Gene count filter: remove cells with too many genes (potential doublets)
upper_limit = np.percentile(adata.obs.n_genes_by_counts, 98)
adata = adata[adata.obs.n_genes_by_counts < upper_limit].copy()
print(f"After filtering: {adata.shape}")Step 5: Normalization and Log Transform
# Save raw counts
adata.layers["counts"] = adata.X.copy()
# Normalize to 10,000 counts per cell
sc.pp.normalize_total(adata, target_sum=1e4)
# Log transform
sc.pp.log1p(adata)
# Store normalized data as raw for DEG testing
adata.raw = adataStep 6: Highly Variable Gene Selection
# Find highly variable genes
# Try multiple flavors for robustness across data types
n_hvg = 0
for flavor in ["seurat_v3", "cell_ranger", "seurat"]:
try:
sc.pp.highly_variable_genes(
adata,
n_top_genes=2000,
flavor=flavor,
layer="counts" if flavor == "seurat_v3" else None,
batch_key="batch" if "batch" in adata.obs.columns else None,
)
n_hvg = adata.var.highly_variable.sum()
if n_hvg > 0:
break
except (ValueError, TypeError, ImportError):
continue
# Ultimate fallback: variance-based selection
if n_hvg == 0:
from scipy import sparse as sp
if sp.issparse(adata.X):
variances = np.array(adata.X.toarray()).var(axis=0)
else:
variances = adata.X.var(axis=0)
top_idx = np.argsort(variances)[-2000:]
adata.var["highly_variable"] = False
adata.var.iloc[top_idx, adata.var.columns.get_loc("highly_variable")] = True
print("Used variance-based HVG selection (fallback)")
print(f"HVGs: {adata.var.highly_variable.sum()}")
# Subset to HVGs
adata = adata[:, adata.var.highly_variable].copy()Step 7: Scaling and PCA
# Handle NaN values before scaling
import numpy as np
if sparse.issparse(adata.X):
adata.X = np.nan_to_num(adata.X.toarray(), nan=0.0)
else:
adata.X = np.nan_to_num(adata.X, nan=0.0)
# Scale to unit variance
sc.pp.scale(adata, max_value=10)
# PCA
sc.tl.pca(adata, n_comps=50, svd_solver="arpack")
# Store PCA result
print(f"PCA shape: {adata.obsm["X_pca"].shape}")Step 8: Save Output
adata.write_h5ad("preprocessed.h5ad")
print(f"Saved: {adata.shape}")Key Parameters to Adjust
| Parameter | Default | When to Change |
pct_counts_mt threshold | 20 | Lower (10) for high-quality nuclei; higher for stressed cells |
min_genes | 200 | Lower for sparse protocols (e.g., Drop-seq) |
min_cells | 3 | Lower for rare cell populations |
n_top_genes | 2000 | 3000-5000 for complex tissues; 1000-1500 for focused studies |
target_sum | 1e4 | 1e5 for deeper sequencing |
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

