$npx -y skills add anthropics/knowledge-work-plugins --skill single-cell-rna-qcPerforms quality control on single-cell RNA-seq data (.h5ad or .h5 files) using scverse best practices with MAD-based filtering and comprehensive visualizations. Use when users request QC analysis, filtering low-quality cells, assessing data quality, or following scverse/scanpy b
| 1 | # Single-Cell RNA-seq Quality Control |
| 2 | |
| 3 | Automated QC workflow for single-cell RNA-seq data following scverse best practices. |
| 4 | |
| 5 | ## When to Use This Skill |
| 6 | |
| 7 | Use when users: |
| 8 | - Request quality control or QC on single-cell RNA-seq data |
| 9 | - Want to filter low-quality cells or assess data quality |
| 10 | - Need QC visualizations or metrics |
| 11 | - Ask to follow scverse/scanpy best practices |
| 12 | - Request MAD-based filtering or outlier detection |
| 13 | |
| 14 | **Supported input formats:** |
| 15 | - `.h5ad` files (AnnData format from scanpy/Python workflows) |
| 16 | - `.h5` files (10X Genomics Cell Ranger output) |
| 17 | |
| 18 | **Default recommendation**: Use Approach 1 (complete pipeline) unless the user has specific custom requirements or explicitly requests non-standard filtering logic. |
| 19 | |
| 20 | ## Approach 1: Complete QC Pipeline (Recommended for Standard Workflows) |
| 21 | |
| 22 | For standard QC following scverse best practices, use the convenience script `scripts/qc_analysis.py`: |
| 23 | |
| 24 | ```bash |
| 25 | python3 scripts/qc_analysis.py input.h5ad |
| 26 | # or for 10X Genomics .h5 files: |
| 27 | python3 scripts/qc_analysis.py raw_feature_bc_matrix.h5 |
| 28 | ``` |
| 29 | |
| 30 | The script automatically detects the file format and loads it appropriately. |
| 31 | |
| 32 | **When to use this approach:** |
| 33 | - Standard QC workflow with adjustable thresholds (all cells filtered the same way) |
| 34 | - Batch processing multiple datasets |
| 35 | - Quick exploratory analysis |
| 36 | - User wants the "just works" solution |
| 37 | |
| 38 | **Requirements:** anndata, scanpy, scipy, matplotlib, seaborn, numpy |
| 39 | |
| 40 | **Parameters:** |
| 41 | |
| 42 | Customize filtering thresholds and gene patterns using command-line parameters: |
| 43 | - `--output-dir` - Output directory |
| 44 | - `--mad-counts`, `--mad-genes`, `--mad-mt` - MAD thresholds for counts/genes/MT% |
| 45 | - `--mt-threshold` - Hard mitochondrial % cutoff |
| 46 | - `--min-cells` - Gene filtering threshold |
| 47 | - `--mt-pattern`, `--ribo-pattern`, `--hb-pattern` - Gene name patterns for different species |
| 48 | |
| 49 | Use `--help` to see current default values. |
| 50 | |
| 51 | **Outputs:** |
| 52 | |
| 53 | All files are saved to `<input_basename>_qc_results/` directory by default (or to the directory specified by `--output-dir`): |
| 54 | - `qc_metrics_before_filtering.png` - Pre-filtering visualizations |
| 55 | - `qc_filtering_thresholds.png` - MAD-based threshold overlays |
| 56 | - `qc_metrics_after_filtering.png` - Post-filtering quality metrics |
| 57 | - `<input_basename>_filtered.h5ad` - Clean, filtered dataset ready for downstream analysis |
| 58 | - `<input_basename>_with_qc.h5ad` - Original data with QC annotations preserved |
| 59 | |
| 60 | If copying outputs for user access, copy individual files (not the entire directory) so users can preview them directly. |
| 61 | |
| 62 | ### Workflow Steps |
| 63 | |
| 64 | The script performs the following steps: |
| 65 | |
| 66 | 1. **Calculate QC metrics** - Count depth, gene detection, mitochondrial/ribosomal/hemoglobin content |
| 67 | 2. **Apply MAD-based filtering** - Permissive outlier detection using MAD thresholds for counts/genes/MT% |
| 68 | 3. **Filter genes** - Remove genes detected in few cells |
| 69 | 4. **Generate visualizations** - Comprehensive before/after plots with threshold overlays |
| 70 | |
| 71 | ## Approach 2: Modular Building Blocks (For Custom Workflows) |
| 72 | |
| 73 | For custom analysis workflows or non-standard requirements, use the modular utility functions from `scripts/qc_core.py` and `scripts/qc_plotting.py`: |
| 74 | |
| 75 | ```python |
| 76 | # Run from scripts/ directory, or add scripts/ to sys.path if needed |
| 77 | import anndata as ad |
| 78 | from qc_core import calculate_qc_metrics, detect_outliers_mad, filter_cells |
| 79 | from qc_plotting import plot_qc_distributions # Only if visualization needed |
| 80 | |
| 81 | adata = ad.read_h5ad('input.h5ad') |
| 82 | calculate_qc_metrics(adata, inplace=True) |
| 83 | # ... custom analysis logic here |
| 84 | ``` |
| 85 | |
| 86 | **When to use this approach:** |
| 87 | - Different workflow needed (skip steps, change order, apply different thresholds to subsets) |
| 88 | - Conditional logic (e.g., filter neurons differently than other cells) |
| 89 | - Partial execution (only metrics/visualization, no filtering) |
| 90 | - Integration with other analysis steps in a larger pipeline |
| 91 | - Custom filtering criteria beyond what command-line params support |
| 92 | |
| 93 | **Available utility functions:** |
| 94 | |
| 95 | From `qc_core.py` (core QC operations): |
| 96 | - `calculate_qc_metrics(adata, mt_pattern, ribo_pattern, hb_pattern, inplace=True)` - Calculate QC metrics and annotate adata |
| 97 | - `detect_outliers_mad(adata, metric, n_mads, verbose=True)` - MAD-based outlier detection, returns boolean mask |
| 98 | - `apply_hard_threshold(adata, metric, threshold, operator='>', verbose=True)` - Apply hard cutoffs, returns boolean mask |
| 99 | - `filter_cells(adata, mask, inplace=False)` - Apply boolean mask to filter cells |
| 100 | - `filter_genes(adata, min_cells=20, min_counts=None, inplace=True)` - Filter genes by detection |
| 101 | - `print_qc_summary(adata, label='')` - Print summary statistics |
| 102 | |
| 103 | From `qc_plotting.py` (visualization): |
| 104 | - `plot_qc_distributions(a |