$npx -y skills add LeonChaoX/qinyan-academic-skills --skill anndataData structure for annotated matrices in single-cell analysis. Use when working with .h5ad files or integrating with the scverse ecosystem. This is the data format skill—for analysis workflows use scanpy; for probabilistic models use scvi-tools; for population-scale queries use c
| 1 | # AnnData |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | AnnData is a Python package for handling annotated data matrices, storing experimental measurements (X) alongside observation metadata (obs), variable metadata (var), and multi-dimensional annotations (obsm, varm, obsp, varp, uns). Originally designed for single-cell genomics through Scanpy, it now serves as a general-purpose framework for any annotated data requiring efficient storage, manipulation, and analysis. |
| 6 | |
| 7 | ## When to Use This Skill |
| 8 | |
| 9 | Use this skill when: |
| 10 | - Creating, reading, or writing AnnData objects |
| 11 | - Working with h5ad, zarr, or other genomics data formats |
| 12 | - Performing single-cell RNA-seq analysis |
| 13 | - Managing large datasets with sparse matrices or backed mode |
| 14 | - Concatenating multiple datasets or experimental batches |
| 15 | - Subsetting, filtering, or transforming annotated data |
| 16 | - Integrating with scanpy, scvi-tools, or other scverse ecosystem tools |
| 17 | |
| 18 | ## Installation |
| 19 | |
| 20 | ```bash |
| 21 | uv pip install anndata |
| 22 | |
| 23 | # With optional dependencies |
| 24 | uv pip install anndata[dev,test,doc] |
| 25 | ``` |
| 26 | |
| 27 | ## Quick Start |
| 28 | |
| 29 | ### Creating an AnnData object |
| 30 | ```python |
| 31 | import anndata as ad |
| 32 | import numpy as np |
| 33 | import pandas as pd |
| 34 | |
| 35 | # Minimal creation |
| 36 | X = np.random.rand(100, 2000) # 100 cells × 2000 genes |
| 37 | adata = ad.AnnData(X) |
| 38 | |
| 39 | # With metadata |
| 40 | obs = pd.DataFrame({ |
| 41 | 'cell_type': ['T cell', 'B cell'] * 50, |
| 42 | 'sample': ['A', 'B'] * 50 |
| 43 | }, index=[f'cell_{i}' for i in range(100)]) |
| 44 | |
| 45 | var = pd.DataFrame({ |
| 46 | 'gene_name': [f'Gene_{i}' for i in range(2000)] |
| 47 | }, index=[f'ENSG{i:05d}' for i in range(2000)]) |
| 48 | |
| 49 | adata = ad.AnnData(X=X, obs=obs, var=var) |
| 50 | ``` |
| 51 | |
| 52 | ### Reading data |
| 53 | ```python |
| 54 | # Read h5ad file |
| 55 | adata = ad.read_h5ad('data.h5ad') |
| 56 | |
| 57 | # Read with backed mode (for large files) |
| 58 | adata = ad.read_h5ad('large_data.h5ad', backed='r') |
| 59 | |
| 60 | # Read other formats |
| 61 | adata = ad.read_csv('data.csv') |
| 62 | adata = ad.read_loom('data.loom') |
| 63 | adata = ad.read_10x_h5('filtered_feature_bc_matrix.h5') |
| 64 | ``` |
| 65 | |
| 66 | ### Writing data |
| 67 | ```python |
| 68 | # Write h5ad file |
| 69 | adata.write_h5ad('output.h5ad') |
| 70 | |
| 71 | # Write with compression |
| 72 | adata.write_h5ad('output.h5ad', compression='gzip') |
| 73 | |
| 74 | # Write other formats |
| 75 | adata.write_zarr('output.zarr') |
| 76 | adata.write_csvs('output_dir/') |
| 77 | ``` |
| 78 | |
| 79 | ### Basic operations |
| 80 | ```python |
| 81 | # Subset by conditions |
| 82 | t_cells = adata[adata.obs['cell_type'] == 'T cell'] |
| 83 | |
| 84 | # Subset by indices |
| 85 | subset = adata[0:50, 0:100] |
| 86 | |
| 87 | # Add metadata |
| 88 | adata.obs['quality_score'] = np.random.rand(adata.n_obs) |
| 89 | adata.var['highly_variable'] = np.random.rand(adata.n_vars) > 0.8 |
| 90 | |
| 91 | # Access dimensions |
| 92 | print(f"{adata.n_obs} observations × {adata.n_vars} variables") |
| 93 | ``` |
| 94 | |
| 95 | ## Core Capabilities |
| 96 | |
| 97 | ### 1. Data Structure |
| 98 | |
| 99 | Understand the AnnData object structure including X, obs, var, layers, obsm, varm, obsp, varp, uns, and raw components. |
| 100 | |
| 101 | **See**: `references/data_structure.md` for comprehensive information on: |
| 102 | - Core components (X, obs, var, layers, obsm, varm, obsp, varp, uns, raw) |
| 103 | - Creating AnnData objects from various sources |
| 104 | - Accessing and manipulating data components |
| 105 | - Memory-efficient practices |
| 106 | |
| 107 | ### 2. Input/Output Operations |
| 108 | |
| 109 | Read and write data in various formats with support for compression, backed mode, and cloud storage. |
| 110 | |
| 111 | **See**: `references/io_operations.md` for details on: |
| 112 | - Native formats (h5ad, zarr) |
| 113 | - Alternative formats (CSV, MTX, Loom, 10X, Excel) |
| 114 | - Backed mode for large datasets |
| 115 | - Remote data access |
| 116 | - Format conversion |
| 117 | - Performance optimization |
| 118 | |
| 119 | Common commands: |
| 120 | ```python |
| 121 | # Read/write h5ad |
| 122 | adata = ad.read_h5ad('data.h5ad', backed='r') |
| 123 | adata.write_h5ad('output.h5ad', compression='gzip') |
| 124 | |
| 125 | # Read 10X data |
| 126 | adata = ad.read_10x_h5('filtered_feature_bc_matrix.h5') |
| 127 | |
| 128 | # Read MTX format |
| 129 | adata = ad.read_mtx('matrix.mtx').T |
| 130 | ``` |
| 131 | |
| 132 | ### 3. Concatenation |
| 133 | |
| 134 | Combine multiple AnnData objects along observations or variables with flexible join strategies. |
| 135 | |
| 136 | **See**: `references/concatenation.md` for comprehensive coverage of: |
| 137 | - Basic concatenation (axis=0 for observations, axis=1 for variables) |
| 138 | - Join types (inner, outer) |
| 139 | - Merge strategies (same, unique, first, only) |
| 140 | - Tracking data sources with labels |
| 141 | - Lazy concatenation (AnnCollection) |
| 142 | - On-disk concatenation for large datasets |
| 143 | |
| 144 | Common commands: |
| 145 | ```python |
| 146 | # Concatenate observations (combine samples) |
| 147 | adata = ad.concat( |
| 148 | [adata1, adata2, adata3], |
| 149 | axis=0, |
| 150 | join='inner', |
| 151 | label='batch', |
| 152 | keys=['batch1', 'batch2', 'batch3'] |
| 153 | ) |
| 154 | |
| 155 | # Concatenate variables (combine modalities) |
| 156 | adata = ad.concat([adata_rna, adata_protein], axis=1) |
| 157 | |
| 158 | # Lazy concatenation |
| 159 | from anndata.experimental import AnnCollection |
| 160 | collection = AnnCollection( |
| 161 | ['data1.h5ad', 'data2.h5ad'], |
| 162 | join_obs='outer', |
| 163 | label='dataset' |
| 164 | ) |
| 165 | ``` |
| 166 | |
| 167 | ### 4. Data Manipulation |
| 168 | |
| 169 | Transform, subset, filter, and reorganiz |