$npx -y skills add LeonChaoX/qinyan-academic-skills --skill cellxgene-censusQuery the CELLxGENE Census (61M+ cells) programmatically. Use when you need expression data across tissues, diseases, or cell types from the largest curated single-cell atlas. Best for population-scale queries, reference atlas comparisons. For analyzing your own data use scanpy o
| 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 genomics data from CZ CELLxGENE Discover. This skill enables efficient querying and analysis of millions of cells across thousands of datasets. |
| 6 | |
| 7 | The Census includes: |
| 8 | - **61+ million cells** from human and mouse |
| 9 | - **Standardized metadata** (cell types, tissues, diseases, donors) |
| 10 | - **Raw gene expression** matrices |
| 11 | - **Pre-calculated embeddings** and statistics |
| 12 | - **Integration with PyTorch, scanpy, and other analysis tools** |
| 13 | |
| 14 | ## When to Use This Skill |
| 15 | |
| 16 | This skill should be used when: |
| 17 | - Querying single-cell expression data by cell type, tissue, or disease |
| 18 | - Exploring available single-cell datasets and metadata |
| 19 | - Training machine learning models on single-cell data |
| 20 | - Performing large-scale cross-dataset analyses |
| 21 | - Integrating Census data with scanpy or other analysis frameworks |
| 22 | - Computing statistics across millions of cells |
| 23 | - Accessing pre-calculated embeddings or model predictions |
| 24 | |
| 25 | ## Installation and Setup |
| 26 | |
| 27 | Install the Census API: |
| 28 | ```bash |
| 29 | uv pip install cellxgene-census |
| 30 | ``` |
| 31 | |
| 32 | For machine learning workflows, install additional dependencies: |
| 33 | ```bash |
| 34 | uv pip install cellxgene-census[experimental] |
| 35 | ``` |
| 36 | |
| 37 | ## Core Workflow Patterns |
| 38 | |
| 39 | ### 1. Opening the Census |
| 40 | |
| 41 | Always use the context manager to ensure proper resource cleanup: |
| 42 | |
| 43 | ```python |
| 44 | import cellxgene_census |
| 45 | |
| 46 | # Open latest stable version |
| 47 | with cellxgene_census.open_soma() as census: |
| 48 | # Work with census data |
| 49 | |
| 50 | # Open specific version for reproducibility |
| 51 | with cellxgene_census.open_soma(census_version="2023-07-25") as census: |
| 52 | # Work with census data |
| 53 | ``` |
| 54 | |
| 55 | **Key points:** |
| 56 | - Use context manager (`with` statement) for automatic cleanup |
| 57 | - Specify `census_version` for reproducible analyses |
| 58 | - Default opens latest "stable" release |
| 59 | |
| 60 | ### 2. Exploring Census Information |
| 61 | |
| 62 | Before querying expression data, explore available datasets and metadata. |
| 63 | |
| 64 | **Access summary information:** |
| 65 | ```python |
| 66 | # Get summary statistics |
| 67 | summary = census["census_info"]["summary"].read().concat().to_pandas() |
| 68 | print(f"Total cells: {summary['total_cell_count'][0]}") |
| 69 | |
| 70 | # Get all datasets |
| 71 | datasets = census["census_info"]["datasets"].read().concat().to_pandas() |
| 72 | |
| 73 | # Filter datasets by criteria |
| 74 | covid_datasets = datasets[datasets["disease"].str.contains("COVID", na=False)] |
| 75 | ``` |
| 76 | |
| 77 | **Query cell metadata to understand available data:** |
| 78 | ```python |
| 79 | # Get unique cell types in a tissue |
| 80 | cell_metadata = cellxgene_census.get_obs( |
| 81 | census, |
| 82 | "homo_sapiens", |
| 83 | value_filter="tissue_general == 'brain' and is_primary_data == True", |
| 84 | column_names=["cell_type"] |
| 85 | ) |
| 86 | unique_cell_types = cell_metadata["cell_type"].unique() |
| 87 | print(f"Found {len(unique_cell_types)} cell types in brain") |
| 88 | |
| 89 | # Count cells by tissue |
| 90 | tissue_counts = cell_metadata.groupby("tissue_general").size() |
| 91 | ``` |
| 92 | |
| 93 | **Important:** Always filter for `is_primary_data == True` to avoid counting duplicate cells unless specifically analyzing duplicates. |
| 94 | |
| 95 | ### 3. Querying Expression Data (Small to Medium Scale) |
| 96 | |
| 97 | For queries returning < 100k cells that fit in memory, use `get_anndata()`: |
| 98 | |
| 99 | ```python |
| 100 | # Basic query with cell type and tissue filters |
| 101 | adata = cellxgene_census.get_anndata( |
| 102 | census=census, |
| 103 | organism="Homo sapiens", # or "Mus musculus" |
| 104 | obs_value_filter="cell_type == 'B cell' and tissue_general == 'lung' and is_primary_data == True", |
| 105 | obs_column_names=["assay", "disease", "sex", "donor_id"], |
| 106 | ) |
| 107 | |
| 108 | # Query specific genes with multiple filters |
| 109 | adata = cellxgene_census.get_anndata( |
| 110 | census=census, |
| 111 | organism="Homo sapiens", |
| 112 | var_value_filter="feature_name in ['CD4', 'CD8A', 'CD19', 'FOXP3']", |
| 113 | obs_value_filter="cell_type == 'T cell' and disease == 'COVID-19' and is_primary_data == True", |
| 114 | obs_column_names=["cell_type", "tissue_general", "donor_id"], |
| 115 | ) |
| 116 | ``` |
| 117 | |
| 118 | **Filter syntax:** |
| 119 | - Use `obs_value_filter` for cell filtering |
| 120 | - Use `var_value_filter` for gene filtering |
| 121 | - Combine conditions with `and`, `or` |
| 122 | - Use `in` for multiple values: `tissue in ['lung', 'liver']` |
| 123 | - Select only needed columns with `obs_column_names` |
| 124 | |
| 125 | **Getting metadata separately:** |
| 126 | ```python |
| 127 | # Query cell metadata |
| 128 | cell_metadata = cellxgene_census.get_obs( |
| 129 | census, "homo_sapiens", |
| 130 | value_filter="disease == 'COVID-19' and is_primary_data == True", |
| 131 | column_names=["cell_type", "tissue_general", "donor_id"] |
| 132 | ) |
| 133 | |
| 134 | # Query gene metadata |
| 135 | gene_metadata = cellxgene_census.get_var( |
| 136 | census, "homo_sapiens", |
| 137 | value_filter="feature_name in ['CD4', 'CD8A']", |
| 138 | column_names=["feature_id", "feature_name", "feature_length"] |
| 139 | ) |
| 140 | ``` |
| 141 | |
| 142 | ### 4. Large-Scale Q |