Sc preprocessing

From TianGzlab/OmicsClaw · View interactive version

Sc preprocessing is an AI agent skill from the GitHub repository TianGzlab/OmicsClaw. Load when normalising QC'd scRNA into a PCA-ready AnnData via scanpy / Seurat / SCTransform / Pearson residuals. Skip when QC thresholds are still undecided (use sc-qc); batch correction across samples (use sc-batch-integration).

How to use this skill

Skill file

sc-preprocessing

When to use

The user has a QC'd AnnData and wants the standard

"filter → normalise → HVG → PCA" pipeline before clustering or batch

integration. Four interchangeable backends are available: scanpy

(default; CP10k log + HVG seurat flavour), seurat (R-backed

LogNormalize / CLR / RC), sctransform (R-backed regularised NB), and

pearson_residuals (raw-count HVG selection plus Pearson residual

transformation). The skill stops at PCA — UMAP / clustering live in

sc-clustering, multi-sample correction in sc-batch-integration.

Use from a step

pre = load_skill("sc-preprocessing")
adata = read_input("results/01_qc/intermediate/adata_qc.h5ad")
# max_mt_pct 10: PBMC cells above 10% mitochondrial reads are mostly damaged (agreed with the user).
adata = pre.preprocess(adata, method="scanpy", max_mt_pct=10.0)
write_output(pre.run_info(adata)["filter_summary"], "tables/filter_summary.json")
write_output(pre.pca_variance_table(adata), "tables/pca_variance_ratio.csv")
write_output(adata, "intermediate/adata_preprocessed.h5ad")

A complete step that runs on demo data: examples/example_step.py.

If the input already passed through sc-filter, use

preprocess(adata, apply_filters=False). This preserves that step's retained

cells and genes, including its doublet decision, while normalization, HVG

selection and PCA still run.

API

<!-- api:begin generated from _api.py; regenerate with run.py api <skill dir> --write -->

`preprocess(adata, *, method: str='scanpy', apply_filters: bool=True, min_genes: int=200, min_cells: int=3, max_mt_pct: float=20.0, n_top_hvg: int | None=None, n_pcs: int=50, normalization_target_sum: float=10000.0, scanpy_hvg_flavor: str='seurat', pearson_hvg_flavor: str='seurat_v3', pearson_theta: float=100.0, seurat_normalize_method: str='LogNormalize', seurat_scale_factor: float=10000.0, seurat_hvg_method: str='vst', sctransform_regress_mt: bool=True, remove_doublets: bool=True, doublet_score_threshold: float=0.25, preserve_var_names: bool=False)`

Filter cells and genes, normalise, select highly variable genes and run PCA.

The input is first brought into the OmicsClaw scRNA contract (counts in

`layers['counts'], a counts snapshot in raw`) and QC metrics are added

when missing. With apply_filters=True, cells and genes are filtered by the thresholds below,

and doublets are dropped when `predicted_doublet or doublet_score`

from sc-doublet-detection are in `obs`. Finally the chosen method

normalises `X, flags var['highly_variable']` and writes

`obsm['X_pca']. What was filtered is recorded for :func:run_info`.

:param method: `"scanpy"` (normalize_total, log1p, HVG, PCA; the default),

`"pearson_residuals" (analytic Pearson residuals for HVG and PCA; X`

stays log-normalised), `"seurat"` (Seurat LogNormalize in R) or

`"sctransform"` (Seurat SCTransform in R).

:param apply_filters: Apply cell, gene and doublet filtering. Default True.

Set False after sc-filter to preserve its retained cells and genes.

:param min_genes: Drop cells with fewer detected genes. Default 200, the

scanpy and Seurat tutorial value; lower it for low-depth data.

:param min_cells: Drop genes detected in fewer cells. Default 3, the tutorial value.

:param max_mt_pct: Drop cells above this mitochondrial percentage. Default 20.0,

a permissive threshold; PBMC data usually uses 5 to 10. Agree it with the user.

:param n_top_hvg: Number of highly variable genes. Default `None`: 3000 for

`sctransform`, 2000 for the other methods (their tutorials' values).

:param n_pcs: Principal components to compute. Default 50.

:param normalization_target_sum: Counts per cell after `normalize_total`

(`scanpy` only). Default 10000.

:param scanpy_hvg_flavor: HVG flavor for `scanpy: "seurat"` (default),

`"cell_ranger" or "seurat_v3"`.

:param pearson_hvg_flavor: HVG flavor for `pearson_residuals. Default "seurat_v3"`.

:param pearson_theta: Overdispersion for Pearson residuals. Default 100, scanpy's default.

:param seurat_normalize_method: Seurat `NormalizeData method. Default "LogNormalize"`.

:param seurat_scale_factor: Seurat scale factor. Default 10000.

:param seurat_hvg_method: Seurat `FindVariableFeatures method. Default "vst"`.

:param sctransform_regress_mt: Regress out mitochondrial percentage in SCTransform. Default `True`.

:param remove_doublets: Drop cells flagged as doublets when the flags are present. Default `True`.

:param doublet_score_threshold: Score above which a cell counts as a doublet when only

`doublet_score` is present. Default 0.25.

:param preserve_var_names: Keep the input's gene identifiers instead of the

symbols chosen during standardisation. Default `False`.

:returns: A new AnnData with normalised `X, layers['counts'], raw`

counts snapshot, `var['highly_variable'], obsm['X_pca'], uns['pca']`.

:raises ValueError: an unknown method, or input with no count-like matrix.

:raises RuntimeError: the R methods fail, or their R packages are missing.

`run_info(adata, *, keep: bool=True) -> dict`

What :func:preprocess recorded about its run.

Keys: `method; filter_summary` (cells and genes before and after

filtering, cells removed by each threshold under `filter_stats`, whether QC

metrics were reused, and `input_preparation`: the matrix used as counts,

gene-name source, warnings and inferred species); `input_contract`;

`matrix_contract`.

:param keep: Leave the record in `adata.uns; False` removes it.

:returns: The record, or an empty dict when `preprocess` has not run on adata.

`hvg_table(adata, *, n_top: int=50) -> pd.DataFrame`

The most variable genes with their mean and dispersion statistics.

:param n_top: How many genes to return, most variable first. Default 50.

:returns: Column `gene plus whichever of means, variances`,

`variances_norm, dispersions, dispersions_norm` the method wrote.

`pca_variance_table(adata) -> pd.DataFrame`

Variance explained by each principal component, to choose how many to keep downstream.

:returns: Columns `pc, variance_ratio and cumulative_variance_ratio`.

`pca_embedding_table(adata, *, n_components: int=5) -> pd.DataFrame`

The first principal-component coordinates of every cell.

:param n_components: How many components to include. Default 5.

:returns: Column `cell_id followed by PC1, PC2`, ...

`qc_metrics_table(adata) -> pd.DataFrame`

The QC metrics of the cells that passed filtering.

:returns: Column `cell_id followed by whichever of n_genes_by_counts`,

`total_counts and pct_counts_mt are in obs`.

`pca_variance_figure(adata, *, n_pcs: int=50)`

An elbow plot of the variance ratio per principal component.

:param n_pcs: How many components to show. Default 50.

:returns: A matplotlib Figure.

:raises KeyError: `uns['pca'] is missing; run :func:preprocess` first.

<!-- api:end -->

Methods and parameters

| Method | Use when | Notes |

|---|---|---|

| scanpy (default) | most 10x data | normalize_total to 10,000, log1p, HVG (seurat flavor), PCA with arpack |

| pearson_residuals | very sparse or heterogeneous data | HVG and PCA on analytic Pearson residuals (Lause et al. 2021); X stays log-normalised for plotting and DE |

| seurat | the user wants Seurat's LogNormalize numbers | runs Rscript; needs Seurat, SingleCellExperiment, zellkonverter, rhdf5 |

| sctransform | the user wants SCTransform | runs Rscript; also needs sctransform; 3,000 HVGs by default |

Defaults and where they come from:

Every threshold you change goes into the step with its reason.

Gotchas

Inputs and outputs

CLI

sc_preprocess.py runs the same functions outside a project and writes a

report, figures, tables and processed.h5ad:

python <skill directory>/sc_preprocess.py --help. --demo runs it on PBMC3k.

See also

Dependencies

Python packages this skill's script needs. They are not installed for you — check before a long run.

anndata, matplotlib, numpy, pandas, phate, scanpy, scipy, seaborn

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.