# Did the single-cell handoff preserve the metadata and counts?

A count matrix can survive an export while the metadata used to group observations changes. The reverse is also possible. This tutorial runs both checks on the same five observations and shows each case with native Python and R objects, a pair of sparse exports, and receipts you can replay.

The two companion tools are [CellMeta Receipt v0.1.0](https://github.com/vxxqv/cellmeta-receipt/releases/tag/v0.1.0) and [Counts Matrix Receipt v0.1.0](https://github.com/vxxqv/counts-matrix-receipt/releases/tag/v0.1.0). The sample contains only synthetic identifiers and counts.

## 1. Inspect the source and the three handoffs

The source has three feature IDs and five observation IDs. Its typed observation metadata includes a donor code with leading zeros, an ordered category with an unused level, a nullable Boolean, and separate representations of `NA`, true missingness, and an empty string. AnnData stores a 5 × 3 assay with observations in rows. The separate 3 × 5 Matrix Market source stores features in rows and keeps one repeated triplet and one explicit zero. [Source H5AD](examples/baseline/object.h5ad) · [Source metadata snapshot](examples/baseline/metadata.json) · [Source count bundle](examples/baseline/bundle.json)

Each R candidate is a `SummarizedExperiment` with a real sparse `counts` assay and typed `colData`. Each reorders both axes, then writes the sparse sidecar bundle with observations in rows. The guide declares those orientations and permits only the known observation reorder in the metadata policy. Both checks still report the reorder. [Policy](examples/allow-row-reorder.json) · [Matrix Market format](https://math.nist.gov/MatrixMarket/formats.html) · [R Matrix export](https://stat.ethz.ch/R-manual/R-devel/library/Matrix/html/externalFormats.html)

| Candidate | CellMeta Receipt | Counts Matrix Receipt |
| --- | --- | --- |
| `matched` | Exit `0`, no unexpected changes. [Receipt](examples/matched/metadata-receipt.json) | Exit `0`, same counts and semantic hash. [Receipt](examples/matched/counts-receipt.json) |
| `metadata-drift` | Exit `1`, donor codes became integers. One kind change and five value changes. [Receipt](examples/metadata-drift/metadata-receipt.json) | Exit `0`, same counts and semantic hash. [Receipt](examples/metadata-drift/counts-receipt.json) |
| `counts-drift` | Exit `0`, no unexpected changes. [Receipt](examples/counts-drift/metadata-receipt.json) | Exit `1`, four changed counts. After alignment, dimensions, both sets of marginal totals, and the nonzero count still agree. [Receipt](examples/counts-drift/counts-receipt.json) |

Every metadata receipt records one expected reorder. Both drift scenarios were deliberately constructed in R to exercise the detectors. The tested wheel and Windows executable match the published release assets. [Verified environment and results](examples/results.json)

## 2. Run both checks on the saved fixture

Install the wheel from the [CellMeta release](https://github.com/vxxqv/cellmeta-receipt/releases/tag/v0.1.0) into an active Python environment. Download the [Counts Matrix release binary](https://github.com/vxxqv/counts-matrix-receipt/releases/tag/v0.1.0) for your system and put it on your `PATH` as `counts-matrix-receipt` or `counts-matrix-receipt.exe`. You can run the saved fixtures without AnnData, SciPy, or R.

From this guide’s folder, first run the matched case. Both commands should exit `0`.

```sh
cellmeta-receipt check examples/baseline/metadata.json examples/matched/metadata.json --policy examples/allow-row-reorder.json
counts-matrix-receipt compare --baseline examples/baseline/bundle.json --candidate examples/matched/bundle.json --format json
```

Repeat with `metadata-drift` and then `counts-drift` in place of `matched`. Exit `1` is a valid comparison with findings. The command still emits the corresponding receipt. Exit `2` means the comparison could not run or the inputs were invalid. A gate requiring both typed metadata and counts to survive should proceed only if both exit codes are `0`.

## 3. Follow one finding from each tool

In `metadata-drift`, observation `O-B` has donor code `"001"` before export and integer `1` afterward. All count artifacts for this case match `matched` byte for byte. CellMeta checks the declared type and the literal identifier spelling, so this change is visible even though Counts Matrix Receipt accepts the assay.

In `counts-drift`, consider the block where rows `001` and `F-001` meet observations `O-B` and `O-D`.

| Feature | `O-B` before → after | `O-D` before → after | Row total within block |
| --- | --- | --- | --- |
| `001` | `1 → 2` | `6 → 5` | `7 → 7` |
| `F-001` | `5 → 4` | `2 → 3` | `7 → 7` |
| **Column total within block** | **`6 → 6`** | **`8 → 8`** | |

All other values are unchanged, so each full row and column total is still identical. Exact alignment by the two IDs finds all four differences. The `colData` frame preserves the original types and values by ID, so the metadata gate accepts that part of the handoff.

## 4. Rebuild from native files

Use the [pinned Python producer dependencies](requirements.txt) and an R 4.6.1 environment with Matrix, SummarizedExperiment, and jsonlite. Use the [CellMeta v0.1.0 source](https://github.com/vxxqv/cellmeta-receipt/tree/v0.1.0) for its format adapters and install the same release’s wheel in the active environment. The checked run used AnnData 0.13.3.post0, SciPy 1.17.1, Matrix 1.7.5, SummarizedExperiment 1.42.0, and both receipt CLIs at v0.1.0. [Environment record](examples/results.json)

```sh
python -m pip install -r requirements.txt
python replay.py --cellmeta-source /path/to/cellmeta-receipt --counts-cli /path/to/counts-matrix-receipt --output replay
```

If needed, pass `--rscript` with the path to `Rscript`. Use a new output folder for each replay. The Python producer checks the H5AD assay and typed frame after reopening them. The R producer checks the native assays and `colData` after reopening the RDS files and independently rereads each Matrix Market export. It checks the full keyed row and column totals and the nonzero count before exporting the count-drift case. The driver verifies both CLI outcomes, all source hashes, and that each metadata snapshot and matrix sidecar identify the same observations in the same order. [Python driver](replay.py) · [R producer](export-candidates.R)

The handoff compares the passed observation fields and the labeled counts in the supplied sparse bundles. Source hashes can vary with native serialization, line endings, or a regenerated export. Regenerated receipts identify the bytes from that run, while an equivalent transpose and reorder retain the same count semantics.

## License

The MIT license covers this tutorial, its replay scripts, and its original synthetic examples and receipts. Install the separately distributed receipt tools from their own repositories. [License](LICENSE)
