Skip to main content

SpiceMix: a probabilistic graphical model for spatial transcriptomics data

Project description

SpiceMix

overview

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

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.
git clone https://github.com/alam-shahul/SpiceMixPlus.git
  • Install pytest in your environment.
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:
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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

popari-0.0.25.tar.gz (48.0 kB view details)

Uploaded Source

Built Distribution

popari-0.0.25-py3-none-any.whl (55.9 kB view details)

Uploaded Python 3

File details

Details for the file popari-0.0.25.tar.gz.

File metadata

  • Download URL: popari-0.0.25.tar.gz
  • Upload date:
  • Size: 48.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.23.0

File hashes

Hashes for popari-0.0.25.tar.gz
Algorithm Hash digest
SHA256 7e446c6e84a9304fb9de27b668cf88517e0ee4ab0e94ce5c5d362d7ce69144d2
MD5 8b9bbdf9d742882f9333911c42b54730
BLAKE2b-256 0cc7b45d511d81d136932a4ab08aae6583995b74efc167762b069c4464252f65

See more details on using hashes here.

File details

Details for the file popari-0.0.25-py3-none-any.whl.

File metadata

  • Download URL: popari-0.0.25-py3-none-any.whl
  • Upload date:
  • Size: 55.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.23.0

File hashes

Hashes for popari-0.0.25-py3-none-any.whl
Algorithm Hash digest
SHA256 e9c84ccc1bbb7561892f6cd23db8b6a598d89333a5a182e4d80d59720a7bb3e6
MD5 62cff8847bf0d0d8d85935d92400077c
BLAKE2b-256 0de6d1e325fb8f3cb3d0b2faa4b5b0c4151cb66f950d5fdb71009e898169a3f9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page