$npx -y skills add K-Dense-AI/scientific-agent-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 | Requires Python 3.11+. Current stable release: 0.12.16 (released 2026-05-18). |
| 21 | |
| 22 | ```bash |
| 23 | uv pip install "anndata==0.12.16" |
| 24 | |
| 25 | # Lazy I/O and dask-backed operations |
| 26 | uv pip install "anndata[dask,lazy]==0.12.16" |
| 27 | |
| 28 | # Development / docs (contributors) |
| 29 | uv pip install "anndata[dev,test,doc]==0.12.16" |
| 30 | ``` |
| 31 | |
| 32 | Use unpinned installs only when intentionally tracking the latest compatible release. |
| 33 | |
| 34 | Current API notes: |
| 35 | - Use `anndata.io` for non-native `read_*` and `write_*` helpers. Top-level `anndata.read_h5ad` and `anndata.read_zarr` remain supported. |
| 36 | - Avoid deprecated APIs: `ad.read`, `AnnData.concatenate()`, `AnnData.*_keys()`, and `anndata.__version__`. Prefer `ad.read_h5ad`, `ad.concat`, mapping `.keys()`, and `importlib.metadata.version("anndata")`. |
| 37 | - Treat `anndata.experimental` APIs as useful but unstable. Prefer them for large-data workflows only when their current caveats are acceptable. |
| 38 | |
| 39 | ## Quick Start |
| 40 | |
| 41 | ### Creating an AnnData object |
| 42 | ```python |
| 43 | import anndata as ad |
| 44 | import numpy as np |
| 45 | import pandas as pd |
| 46 | |
| 47 | # Minimal creation |
| 48 | X = np.random.rand(100, 2000) # 100 cells × 2000 genes |
| 49 | adata = ad.AnnData(X) |
| 50 | |
| 51 | # With metadata |
| 52 | obs = pd.DataFrame({ |
| 53 | 'cell_type': ['T cell', 'B cell'] * 50, |
| 54 | 'sample': ['A', 'B'] * 50 |
| 55 | }, index=[f'cell_{i}' for i in range(100)]) |
| 56 | |
| 57 | var = pd.DataFrame({ |
| 58 | 'gene_name': [f'Gene_{i}' for i in range(2000)] |
| 59 | }, index=[f'ENSG{i:05d}' for i in range(2000)]) |
| 60 | |
| 61 | adata = ad.AnnData(X=X, obs=obs, var=var) |
| 62 | ``` |
| 63 | |
| 64 | ### Reading data |
| 65 | ```python |
| 66 | # Native formats (read_h5ad/read_zarr remain at top-level) |
| 67 | adata = ad.read_h5ad('data.h5ad') |
| 68 | adata = ad.read_h5ad('large_data.h5ad', backed='r') # lazy load for large files |
| 69 | adata = ad.read_zarr('data.zarr') |
| 70 | |
| 71 | # Other formats: prefer anndata.io (top-level imports are deprecated) |
| 72 | from anndata.io import read_csv, read_loom, read_mtx |
| 73 | |
| 74 | adata = read_csv('data.csv') |
| 75 | adata = read_loom('data.loom') |
| 76 | |
| 77 | # 10X Genomics: use scanpy (not anndata) — see scanpy skill |
| 78 | import scanpy as sc |
| 79 | adata = sc.read_10x_h5('filtered_feature_bc_matrix.h5') |
| 80 | adata = sc.read_10x_mtx('filtered_feature_bc_matrix/') |
| 81 | ``` |
| 82 | |
| 83 | ### Writing data |
| 84 | ```python |
| 85 | # Write h5ad file |
| 86 | adata.write_h5ad('output.h5ad') |
| 87 | |
| 88 | # Write with compression |
| 89 | adata.write_h5ad('output.h5ad', compression='gzip') |
| 90 | |
| 91 | # Write other formats |
| 92 | adata.write_zarr('output.zarr') |
| 93 | adata.write_csvs('output_dir/') |
| 94 | ``` |
| 95 | |
| 96 | ### Basic operations |
| 97 | ```python |
| 98 | # Subset by conditions |
| 99 | t_cells = adata[adata.obs['cell_type'] == 'T cell'] |
| 100 | |
| 101 | # Subset by indices |
| 102 | subset = adata[0:50, 0:100] |
| 103 | |
| 104 | # Add metadata |
| 105 | adata.obs['quality_score'] = np.random.rand(adata.n_obs) |
| 106 | adata.var['highly_variable'] = np.random.rand(adata.n_vars) > 0.8 |
| 107 | |
| 108 | # Access dimensions |
| 109 | print(f"{adata.n_obs} observations × {adata.n_vars} variables") |
| 110 | ``` |
| 111 | |
| 112 | ## Core Capabilities |
| 113 | |
| 114 | ### 1. Data Structure |
| 115 | |
| 116 | Understand the AnnData object structure including X, obs, var, layers, obsm, varm, obsp, varp, uns, and raw components. |
| 117 | |
| 118 | **See**: `references/data_structure.md` for comprehensive information on: |
| 119 | - Core components (X, obs, var, layers, obsm, varm, obsp, varp, uns, raw) |
| 120 | - Creating AnnData objects from various sources |
| 121 | - Accessing and manipulating data components |
| 122 | - Memory-efficient practices |
| 123 | |
| 124 | ### 2. Input/Output Operations |
| 125 | |
| 126 | Read and write data in various formats with support for compression, backed mode, and cloud storage. |
| 127 | |
| 128 | **See**: `references/io_operations.md` for details on: |
| 129 | - Native formats (h5ad, zarr) |
| 130 | - Alternative formats (CSV, MTX, Loom, 10X, Excel) |
| 131 | - Backed mode for large datasets |
| 132 | - Remote data access |
| 133 | - Format conversion |
| 134 | - Performance optimization |
| 135 | |
| 136 | Common commands: |