Metadata-Version: 2.4
Name: SpaMIE
Version: 0.1.1
Summary: Graph-based prediction and integration of spatial multi-omics data
Author-email: Xiang dw <3369813855@qq.com>
License-Expression: MIT
Project-URL: Homepage, https://github.com/xxdwdwd/SpaMIE
Project-URL: Issues, https://github.com/xxdwdwd/SpaMIE/issues
Keywords: spatial omics,multi-omics,GraphSAGE,bioinformatics
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
Requires-Python: <3.12,>=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy<2,>=1.22.4
Requires-Dist: pandas<2.1,>=1.5
Requires-Dist: scipy<1.12,>=1.10
Requires-Dist: scikit-learn<1.4,>=1.2
Requires-Dist: anndata<0.10,>=0.9.2
Requires-Dist: scanpy<1.10,>=1.9.6
Requires-Dist: scikit-misc<0.4,>=0.3.1
Requires-Dist: torch<2.4,>=2.0
Requires-Dist: dgl<2,>=1.1.2
Provides-Extra: batch
Requires-Dist: harmonypy==0.0.10; extra == "batch"
Provides-Extra: notebook
Requires-Dist: jupyterlab<5,>=4; extra == "notebook"
Requires-Dist: harmonypy==0.0.10; extra == "notebook"
Provides-Extra: dev
Requires-Dist: build; extra == "dev"
Requires-Dist: twine; extra == "dev"
Requires-Dist: ruff; extra == "dev"
Dynamic: license-file

# SpaMIE

SpaMIE provides graph-based cross-modality prediction and joint integration for
spatial multi-omics data. It combines spatial and feature neighbor graphs,
GraphSAGE encoders, learned layer weights, and attention aggregation.

## Installation

Use Python 3.9, 3.10, or 3.11. From this release directory:

```powershell
python -m pip install .
```

For Harmony batch correction, install the optional dependencies:

```powershell
python -m pip install ".[batch]"
```

The default dependency set uses DGL 1.1.x and PyTorch 2.0–2.3. It avoids the
GraphBolt/torchdata binary compatibility requirements of DGL 2.x. Windows has
a PyPI wheel for DGL 1.1.2; Linux may resolve DGL 1.1.3. GPU installations require
matching PyTorch, CUDA, and DGL binaries from their official installation guides:
[PyTorch](https://pytorch.org/get-started/locally/) and
[DGL](https://www.dgl.ai/pages/start.html). A CPU DGL wheel cannot run CUDA graphs.
The original DGL 2.2.1 + CUDA 12.1 environment is separate from this default
installation configuration.

## Quick start

After installation, run the self-contained synthetic example:

```powershell
python examples/quickstart.py
```

It runs prediction and integration on CPU without downloading research data.

For real data, provide two `anndata.AnnData` objects with unique, matching
observation names and spatial coordinates in `obsm['spatial']`. Preprocessing
works on copies, aligns shared observations in first-modality order, and stores
PCA features in `obsm['feat']`. Batch correction also requires `obs['batch']`.

```python
import torch
from SpaMIE import Sagegraph, Sagewrapper

device = torch.device('cpu')
spatial, feature, first, second = Sagegraph(
    [adata_rna, adata_protein], device,
    task='Prediction', datatype='Stereo-CITE-seq', batch=False,
)
model = Sagewrapper(
    seed=2024, device=device,
    in_feat=first.obsm['feat'].shape[1], n_hidden=256,
    out_feat=second.n_vars, task='prediction', datatype='Stereo-CITE-seq',
    sagetype='mean', layers_nums=3, weight=[0., 0., 1.],
    activation=torch.nn.LeakyReLU, epoch=350, lr=1e-4, lr2=0.01,
)
first, second, test_idx, train_idx = model.fit(first, second, spatial, feature)
prediction = second.obsm['predict']
latents = second.obsm['latents']
```

Supported `datatype` values:

- `Stereo-CITE-seq`: RNA → protein; RNA/protein integration.
- `spots`: spot-level RNA/protein prediction and integration.
- `Spatial_RNA_Epigenome`: RNA → epigenome; joint integration.
- `Spatial_Epigenome_RNA`: epigenome → RNA; joint integration.
- `Spatial-epigenome-transcriptome-RNA-ATAC`: RNA → ATAC; joint integration.
- `Spatial-epigenome-transcriptome-ATAC-RNA`: ATAC → RNA; joint integration.
- `simu`: already prepared simulation matrices, reduced with PCA.

Use raw nonnegative counts for branches that perform Seurat highly variable gene
selection or CLR normalization. The `simu` branch accepts prepared matrices.
Previously listed names without implemented preprocessing branches are rejected
with a clear error rather than failing later.

## Return values and output files

`Sagegraph(..., task='Prediction')` returns:
`spatial1, feature1, first, second`.

`Sagegraph(..., task='Integration')` returns:
`spatial1, feature1, spatial2, feature2, first, second`.

`Sagewrapper.fit` always takes the two AnnData objects first, followed by graphs.
Task strings accept either capitalization. Prediction returns four values by
default, or six with `weight=True` (adds layer weights and graph attention).
Both prediction paths save `predict` and `latents` arrays in the second AnnData.

Integration returns nine values:
`embedding, layer_weights, reconstruction1, reconstruction2, latent1, latent2,
cross_latents1, cross_latents2, modality_attention`.
Store the embedding using `output[0].cpu().numpy()`.
Integration retains the original weighted aggregation behavior; the wrapper's
`weight` argument applies to prediction.

Without `output_dir`, prediction writes no files. With `output_dir`, it saves
`model/sage_model.pth`, a dictionary containing `state_dict`, constructor settings,
and version. This changes the old full-model pickle format. With `save_csv=True`,
it also saves test predictions, test truths, and a test-index CSV under
`SpaMIE pred result`. Use different output directories for different model runs.

`train_size` is an integer count of available paired observations, with validation
held out from those observations. It is not a fraction. Otherwise, approximately
70% of observations are used for training, 5% for validation, and 25% for testing.
Validation loss controls prediction early stopping (patience 20). Integration
monitors its reconstruction objective. Both restore the best monitored model and
return results from evaluation mode. Inspect `history_` and `n_epochs_` on the
wrapper for training details. The workflow uses all samples to construct graphs
and PCA features, consistent with the original transductive design.

## Examples and checks

The `notebooks/` directory contains the seven research notebooks with corrected
interfaces and cleared outputs. Replace `/data/xiangdw/...` paths with local
dataset paths before execution. Research datasets and presentation figures are
not bundled in the distribution.

```powershell
python -m unittest discover -s tests -v
```

See [CHANGELOG.md](CHANGELOG.md) for behavior changes and
[RELEASE_GUIDE_ZH.md](RELEASE_GUIDE_ZH.md) for build and upload instructions.
MIT license, consistent with the original project's declared classifier.
