Metadata-Version: 2.4
Name: gotmap
Version: 0.2.1
Summary: Query-to-Reference Atlas Mapping for Single-Cell Data via Grassmann Optimal Transport
Author: GOTMap Developers
License: MIT
Project-URL: Homepage, https://github.com/ZengFLab/gotmap
Requires-Python: >=3.9
Description-Content-Type: text/markdown
Requires-Dist: numpy>=1.21
Requires-Dist: scipy>=1.8
Requires-Dist: torch>=1.12
Requires-Dist: scanpy>=1.9
Requires-Dist: grassmann-ot>=0.3.1
Requires-Dist: scikit-learn>=1.0
Requires-Dist: matplotlib>=3.5

# GOTMap

> **Query-to-Reference Atlas Mapping via Grassmann Optimal Transport**

GOTMap maps query single-cell data into a fixed reference atlas using
[grassmann-ot](https://github.com/ZengFLab/grassmann-ot). It is NOT a
symmetric batch-integration tool — its purpose is to project new samples
into an existing atlas for annotation transfer and quality control.

---

## Why GOTMap?

| | Symmetric Integration | **GOTMap (Query → Atlas)** |
|---|---|---|
| Goal | Remove batch effects | **Map new data into a stable atlas** |
| Symmetry | Bidirectional | **Unidirectional (reference fixed)** |
| Output | Joint embedding | **Query coords in reference space** |
| Labels | Optional | **First-class (transfer + confidence)** |
| Use case | Combining batches | **Clinical / atlas extension / QC** |

---

## Installation

```bash
pip install grassmann-ot scanpy torch
pip install gotmap-0.1.0.zip
```

---

## Quick start

```python
import scanpy as sc
import gotmap
from gotmap.pp import preprocess_reference, preprocess_query
from gotmap.tl import map_query_to_reference, transfer_labels, evaluate_mapping
from gotmap.pl import sanity_check, label_transfer_confidence

# ---- 1. Build & freeze reference atlas ----
ref = sc.read_h5ad("atlas.h5ad")
ref = preprocess_reference(ref, n_comps=50, n_top_genes=2000)

# ---- 2. Preprocess and map query ----
query = sc.read_h5ad("new_sample.h5ad")
query = preprocess_query(query, ref, n_comps=50)

mapped = map_query_to_reference(query, ref, method="nn",
                                hidden_dim=128, epochs=200)

# ---- 3. Transfer labels ----
labelled = transfer_labels(query, ref, label_key="cell_type")

# ---- 4. Evaluate ----
quality = evaluate_mapping(query, ref)
print(quality)  # {'mean_confidence': ..., 'mapping_rate': ..., 'n_anchors': ...}

# ---- 5. Visualize ----
sanity_check(ref, query, mapped_query=mapped, label_key="cell_type")
label_transfer_confidence(labelled, label_key="cell_type")
```

---

## API

### `gotmap.pp` — Preprocessing

| Function | Purpose |
|---|---|
| `preprocess_reference(adata, n_comps=50, ...)` | Full pipeline: normalize → HVG → PCA |
| `preprocess_query(adata, reference, n_comps=50)` | Normalize + project query into **reference PCA space** |
| `normalize_and_hvg(adata, ...)` | Normalize and select HVGs |

### `gotmap.tl` — Tools

| Function | Purpose |
|---|---|
| `map_query_to_reference(query, ref, method="nn", ...)` | Map query → reference space |
| `transfer_labels(query, ref, label_key, ...)` | Transfer labels + confidence |
| `evaluate_mapping(query, ref, ...)` | Mapping quality metrics |

### `gotmap.pl` — Plotting

| Function | Purpose |
|---|---|
| `mapping_scatter(adata, basis="X_gotmap", color=...)` | Scatter of mapped cells |
| `label_transfer_confidence(adata, label_key)` | Confidence histogram + by-label boxplot |
| `embedding(adata, basis=..., color=...)` | Generic embedding plot |
| `sanity_check(ref, query, mapped_query=...)` | Reference / mapped / overlay panels |

### `gotmap.GOTMap` — Low-level class

```python
from gotmap import GOTMap

mapper = GOTMap(ref_adata=ref, use_rep="X_pca", k=30, p=3, reg=0.1)
mapper.fit(query)

# neural-network projection
mapped = mapper.transform(query, method="nn", hidden_dim=128, epochs=200)

# kNN projection
mapped_knn = mapper.transform(query, method="knn")

# transfer labels
labelled = mapper.transfer_labels(query, label_key="cell_type")

# quality
quality = mapper.evaluate_mapping(query)
```

### `gotmap.FrozenReference` — Lightweight update (scArches-style)

```python
from gotmap import FrozenReference

# freeze
frozen = FrozenReference(ref)
frozen.save("./frozen_atlas")

# load (no recomputation)
frozen = FrozenReference.load("./frozen_atlas")

# project any new query into the SAME space
query_pca = frozen.project_query(new_query)

# extend with new validated cells
report = frozen.extend(new_cells, min_new_cells=500, update_ratio=0.1)
# - lightweight: append only (PCA unchanged)
# - full rebuild triggered when new cells exceed threshold

# full rebuild (call sparingly)
frozen.rebuild_reference(full_atlas)
frozen.save("./frozen_atlas_v2")
```

---

## How it works

1. **Grassmann subspaces** — For each cell, a local PCA neighborhood
   defines a point on the Grassmann manifold (dimension `p`).
2. **Optimal transport** — Sinkhorn OT computes a soft coupling
   `gamma` (n_ref × n_query) and hard anchors between reference and
   query cells.
3. **Mapping** — Anchors (+ gamma-sampled soft pairs) supervise a
   neural network that projects query cells into reference PCA space.
4. **Label transfer** — Weighted kNN in reference space assigns
   cell-type labels; confidence = max class probability.

GOTMap calls `grassmann-ot` only for coupling (`fit`, `get_hard_anchors`).
All mapping logic is implemented independently.

---

## Requirements

- Python ≥ 3.9
- scanpy ≥ 1.9
- grassmann-ot ≥ 0.3.1
- torch ≥ 1.12
- numpy, scipy, scikit-learn

---

## License

MIT
