Spatially multimodal and multiscale network for representation learning from spatial multi-omics
- Clone this repo.
- Copy the "spamm" and "datasets" folders into your project directory.
For testing, we recommend using Anaconda to run our project. You can run the following commands in Anaconda to create the required environment.
conda create -n spamm python=3.10 -y
conda activate spamm
pip install torch==2.5.1+cu124 torchvision torchaudio \
--index-url https://download.pytorch.org/whl/cu124
pip install torch_geometric==2.6.1 \
-f https://data.pyg.org/whl/torch-2.5.1+cu124.html
pip install numpy==2.2.6 scipy==1.15.3 pandas==2.3.1 pip install scikit_learn==1.7.1 tqdm==4.67.1 igraph==0.11.8
pip install scanpy==1.11.4
You can load the .h5ad files with scanpy, and then process the objects with the preprocessing and adata_const functions provided by the 'spamm'.
import scanpy as sc
import spamm as spm
# load data
om1_adata = sc.read_h5ad('/path/to/om1.h5ad')
om2_adata = sc.read_h5ad('/path/to/om2.h5ad')
# data preprocessing(Optional)
# This step performs PCA to generate the "X_pca" matrix in om1_adata.obsm and om2_adata.obsm.
spm.preprocessing(
om1_adata, om2_adata,
n_comps = 50, random_state = 2025
)
# generate the input adata of SpaMM
adata = spm.adata_const(
om1_adata, om2_adata,
spatial_net_k = 8,
om_net_k = [8, 15]
)
Here, we provide three AnnData objects generated by spm.adata_const(), which can be loaded for testing.
adata = spm.load_test_data(idx = 0)
# If a fixed random seed is required, please run this line of code (optional).
spm.set_random_seed(2025, acc_ctrl = True)
output, model = spm.run_spamm(
adata, scale = 3, epochs = 600, lr = 0.001, lr_step = [100, 200], gamma = 0.1,
rtn_model = True, device = 'cuda:0'
)
The 'output' is a dictionary with:
- 'feat' as a fixed key
- Dynamic keys 'scale1', 'scale2', ..., up to the number you specified in the 'scale' parameter.
If you see:RuntimeError: CUDA error: no kernel image is available for execution on the device.
This means your installed PyTorch does not support your GPU architecture.
Please use "decice = 'cpu'", or upgrade PyTorch:
pip uninstall -y torch torchvision torchaudio torch_geometric pyg_lib torch_scatter torch_sparse torch_cluster torch_spline_conv
pip install --no-cache-dir torch torchvision torchaudio \
--index-url https://download.pytorch.org/whl/cu128
pip install --no-cache-dir torch_geometric pyg_lib torch_scatter torch_sparse torch_cluster torch_spline_conv \
-f https://data.pyg.org/whl/torch-2.7.0+cu128.html
Then you can perform the clustering with the fused features.
feat = output["feat"].cpu().detach().numpy()
adata_feat = sc.AnnData(X = feat)
sc.pp.neighbors(
adata_feat,
n_neighbors=50,
random_state=2025
)
sc.tl.leiden(
adata_feat,
resolution = 1,
flavor="igraph",
random_state=2025
)