$npx -y skills add K-Dense-AI/scientific-agent-skills --skill cellxgene-censusQuery the CZ CELLxGENE Census programmatically for versioned public single-cell and spatial transcriptomics data. Use when you need population-scale cell metadata, gene expression slices, Census summary counts, source H5AD URIs/downloads, embeddings, spatial Census data, or refer
| 1 | # CZ CELLxGENE Census |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | The CZ CELLxGENE Census provides programmatic access to a comprehensive, versioned collection of standardized single-cell and spatial transcriptomics data from CZ CELLxGENE Discover. This skill enables efficient querying and analysis of public Census releases without downloading whole datasets first. |
| 6 | |
| 7 | The Census includes: |
| 8 | - **217+ million total cells** and **125+ million unique cells** in the 2025-11-08 stable LTS release |
| 9 | - **1,845 datasets** in the 2025-11-08 stable LTS release |
| 10 | - **Human, mouse, marmoset, rhesus macaque, and chimpanzee** data in the current schema |
| 11 | - **Standardized metadata** (cell types, tissues, diseases, donors) |
| 12 | - **Raw gene expression** matrices and source H5AD lookup/download helpers |
| 13 | - **Pre-calculated summary counts, embeddings, and spatial data** |
| 14 | - **Integration with AnnData, Scanpy, TileDB-SOMA, TileDB-SOMA-ML, and other analysis tools** |
| 15 | |
| 16 | ## When to Use This Skill |
| 17 | |
| 18 | This skill should be used when: |
| 19 | - Querying single-cell expression data by cell type, tissue, or disease |
| 20 | - Exploring available single-cell datasets and metadata |
| 21 | - Training machine learning models on single-cell data |
| 22 | - Performing large-scale cross-dataset analyses |
| 23 | - Integrating Census data with scanpy or other analysis frameworks |
| 24 | - Computing statistics across millions of cells |
| 25 | - Accessing pre-calculated embeddings or model predictions |
| 26 | |
| 27 | ## Installation and Setup |
| 28 | |
| 29 | Install the Census API: |
| 30 | ```bash |
| 31 | uv pip install "cellxgene-census==1.17.*" |
| 32 | ``` |
| 33 | |
| 34 | For spatial workflows: |
| 35 | ```bash |
| 36 | uv pip install "cellxgene-census[spatial]==1.17.*" "spatialdata[extra]>=0.2.5" |
| 37 | ``` |
| 38 | |
| 39 | For PyTorch model training, use TileDB-SOMA-ML. The old `cellxgene_census.experimental.ml` loaders are deprecated: |
| 40 | |
| 41 | ```bash |
| 42 | uv pip install "cellxgene-census==1.17.*" tiledbsoma-ml |
| 43 | ``` |
| 44 | |
| 45 | ## Core Workflow Patterns |
| 46 | |
| 47 | ### 1. Opening the Census |
| 48 | |
| 49 | Always use the context manager to ensure proper resource cleanup: |
| 50 | |
| 51 | ```python |
| 52 | import cellxgene_census |
| 53 | |
| 54 | # Open latest stable version |
| 55 | with cellxgene_census.open_soma() as census: |
| 56 | # Work with census data |
| 57 | |
| 58 | # Open the current LTS version for reproducibility |
| 59 | with cellxgene_census.open_soma(census_version="2025-11-08") as census: |
| 60 | # Work with census data |
| 61 | ``` |
| 62 | |
| 63 | **Key points:** |
| 64 | - Use context manager (`with` statement) for automatic cleanup |
| 65 | - Specify `census_version` for reproducible analyses |
| 66 | - `stable` opens the current LTS Census release; `latest` opens the newest weekly release retained for a shorter period |
| 67 | |
| 68 | ### 2. Exploring Census Information |
| 69 | |
| 70 | Before querying expression data, explore available datasets and metadata. |
| 71 | |
| 72 | **Access summary information:** |
| 73 | ```python |
| 74 | # Get summary statistics as label/value rows |
| 75 | summary = census["census_info"]["summary"].read().concat().to_pandas() |
| 76 | summary_values = summary.set_index("label")["value"] |
| 77 | print(f"Total cells: {int(summary_values['total_cell_count']):,}") |
| 78 | print(f"Unique cells: {int(summary_values['unique_cell_count']):,}") |
| 79 | |
| 80 | # Get all datasets |
| 81 | datasets = census["census_info"]["datasets"].read().concat().to_pandas() |
| 82 | |
| 83 | # Get precomputed counts by organism, cell type, tissue, disease, and assay |
| 84 | summary_counts = census["census_info"]["summary_cell_counts"].read().concat().to_pandas() |
| 85 | tissue_counts = summary_counts[summary_counts["category"].eq("tissue_general")] |
| 86 | ``` |
| 87 | |
| 88 | **Query cell metadata to understand available data:** |
| 89 | ```python |
| 90 | # Get unique cell types in a tissue |
| 91 | cell_metadata = cellxgene_census.get_obs( |
| 92 | census, |
| 93 | "homo_sapiens", |
| 94 | value_filter="tissue_general == 'brain' and is_primary_data == True", |
| 95 | column_names=["cell_type"] |
| 96 | ) |
| 97 | unique_cell_types = cell_metadata["cell_type"].unique() |
| 98 | print(f"Found {len(unique_cell_types)} cell types in brain") |
| 99 | |
| 100 | # Count cells by tissue |
| 101 | tissue_metadata = cellxgene_census.get_obs( |
| 102 | census, |
| 103 | "homo_sapiens", |
| 104 | value_filter="is_primary_data == True", |
| 105 | column_names=["tissue_general"], |
| 106 | ) |
| 107 | tissue_counts = tissue_metadata["tissue_general"].value_counts() |
| 108 | ``` |
| 109 | |
| 110 | **Important:** Always filter for `is_primary_data == True` to avoid counting duplicate cells unless specifically analyzing duplicates. |
| 111 | |
| 112 | ### 3. Querying Expression Data (Small to Medium Scale) |
| 113 | |
| 114 | For queries returning < 100k cells that fit in memory, |