Metadata-Version: 2.1
Name: popari
Version: 0.0.9
Summary: SpiceMix: a probabilistic graphical model for spatial transcriptomics data
Project-URL: Homepage, https://github.com/alam-shahul/popari
Project-URL: Bug Tracker, https://github.com/alam-shahul/popari/issues
Author-email: Shahul Alam <alam.shahul@gmail.com.com>
License: MIT License
        
        Copyright (c) 2020 Ma Lab at CMU
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.7
Requires-Dist: anndata>=0.8.0
Requires-Dist: matplotlib>=3.3.4
Requires-Dist: numpy>=1.20.1
Requires-Dist: pandas>=1.2.4
Requires-Dist: scanpy>=1.8.1
Requires-Dist: scikit-learn>=0.24.1
Requires-Dist: scipy>=1.7.1
Requires-Dist: seaborn>=0.11.1
Requires-Dist: squidpy>=1.2.2
Requires-Dist: torch>=1.10.1+cu113
Requires-Dist: tqdm>=4.60.0
Requires-Dist: umap-learn>=0.5.1
Description-Content-Type: text/markdown

# SpiceMix

![overview](./SpiceMix_overview.png)

SpiceMix is an unsupervised tool for analyzing data of the spatial transcriptome. SpiceMix models the observed expression of genes within a cell as a mixture of latent factors. These factors are assumed to have some spatial affinity between neighboring cells. The factors and affinities are not known a priori, but are learned by SpiceMix directly from the data, by an alternating optimization method that seeks to maximize their posterior probability given the observed gene expression. In this way, SpiceMix learns a more expressive representation of the identity of cells from their spatial transcriptome data than other available methods. 

SpiceMix can be applied to any type of spatial transcriptomics data, including MERFISH, seqFISH, HDST, and Slide-seq.

## Install

```
pip install spicemix
```

## Usage

```python
from pathlib import Path

import anndata as ad
import torch

from spicemix.model import SpiceMixPlus

# Load datasets
datasets = []
replicate_names = []
for fov in range(5):
    dataset = ad.read_h5ad(f"./example_st_dataset_fov_{replicate}.h5ad") # Each dataset must have spatial information stored as an adjacency matrix
    name = f"{fov}"
    datasets.append(dataset)
    replicate_names.append(name)

# Define hyperparameters
K = 20 # Number of metagenes
lambda_Sigma_x_inv = 1e-4 # Spatial affinity regularization hyperparameter
torch_context = dict(device='cuda:0', dtype=torch.float32) # Context for PyTorch tensor instantiation 

# Initialize
spicemixplus_demo = SpiceMixPlus(
    K=K,
    datasets=datasets,
    lambda_Sigma_x_inv=lambda_Sigma_x_inv,
    torch_context=torch_context
)
    
# Train

## Initialization with NMF
for iteration in range(10):
    spicemixplus_demo.estimate_parameters(update_spatial_affinities=False)
    spicemixplus_demo.estimate_weights(use_neighbors=False)

## Using spatial information
num_iterations = 200
for iteration in range(num_iterations):
    spicemixplus_demo.estimate_parameters()
    spicemixplus_demo.estimate_weights()

# Save to disk
result_filepath = Path(f"./demo_{num_iterations}_iterations.h5ad")
spicemixplus_demo.save_results(result_filepath)
    
# Plot results

...
```

## Tests

To run the provided tests and ensure that SpiceMix can run on your platform, follow the instructions below:

- Download this repo.
```console
git clone https://github.com/alam-shahul/SpiceMixPlus.git
```
- Install `pytest` in your environment.
```console
pip install pytest
```
- Navigate to the root directory of this repo.
- Run the following command. With GPU resources, this test should execute without errors in ~2.5 minutes:
```console
python -m pytest -s tests/test_spicemix_shared.py
```

## Cite

Cite our paper:

```
@article{chidester2020spicemix,
  title={SPICEMIX: Integrative single-cell spatial modeling for inferring cell identity},
  author={Chidester, Benjamin and Zhou, Tianming and Ma, Jian},
  journal={bioRxiv},
  year={2020},
  publisher={Cold Spring Harbor Laboratory}
}
```

![paper](./paper.png)
