Tutorial 5: Mouse Brain Analysis (Stereo-seq)
Import necessary packages
from sklearn import metrics
import torch
import copy
import os
import random
import numpy as np
from semanticst.loading_batches import PrepareDataloader
from semanticst.loading_batches import Dataloader
import scanpy as sc
import matplotlib.pyplot as plt
import pandas as pd
from pathlib import Path
import torch.utils.data as data
from semanticst.main import Config
import warnings
warnings.filterwarnings("ignore")
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
/home/roxana/anaconda3/envs/semanticst3/lib/python3.9/site-packages/torch/__config__.py:10: UserWarning: CUDA initialization: The NVIDIA driver on your system is too old (found version 11040). Please update your GPU driver by downloading and installing a new version from the URL: http://www.nvidia.com/Download/index.aspx Alternatively, go to: https://pytorch.org to install a PyTorch version that has been compiled with your version of the CUDA driver. (Triggered internally at ../c10/cuda/CUDAFunctions.cpp:108.)
return torch._C._show_config()
Read data and import device
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(f"You are using *{device}*")
BASE_PATH = Path('/home/roxana/Projects/Data/E9.5_E1S1.MOSTA.h5ad-20240116T040300Z-001/E9.5_E1S1.MOSTA.h5ad')
spot_paths= Path(f'{BASE_PATH}')
You are using *cpu*
dataset="Mouse embryo"
adata = sc.read_h5ad(spot_paths)
adata.var_names_make_unique()
Train the model
Available data types: ‘Xenium’, ‘Visium’, ‘Stereo’, ‘Slide’.
It is essential to specify the type of ST data, as different ST technologies require distinct preprocessing steps. Additionally, you have the option to select between mini-batch training for large datasets and full dataset training for smaller ones, ensuring efficient data processing and model performance.
dtype = "Stereo"
config=Config(device=device,dtype=dtype, use_mini_batch=False)
from semanticst.SemanticST_main import Semantic as Trainer
config_used = copy.copy(config)
model = Trainer(adata,config)
adata=model.train()
🚀 Welcome to SemanticST! 🚀
📢 Recommendation: If your dataset contains more than 40000 spots or cells, we suggest using **mini-batch training** for efficiency.
✅ Using Full Dataset Training (No Mini-Batching). 🔥
Begin to train ST data...
building sparse Matrix
40267
Learning Semantic graphs: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 250/250 [00:16<00:00, 15.26epoch/s]
Semantic Graph Learning Completed
Feature Learning Epochs: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1000/1000 [09:24<00:00, 1.77it/s]
Clustering
n_cluster =22
tool='mclust'
from semanticst.utils import clustering
clustering(adata,seed=2025, n_clusters=n_cluster, method=tool,key='emb_decoder')
fitting ...
|======================================================================| 100%
import numpy as np
import matplotlib.pyplot as plt
import scanpy as sc
adata.obsm['spatial'][:, 1] = -1*adata.obsm['spatial'][:, 1]
#adata.obs['domain'] = adata.obs['domain'].astype('category')
plt.rcParams["figure.figsize"] = (3, 4)
plot_color=["#F56867","#556B2F","#C798EE","#59BE86","#006400","#8470FF",
"#CD69C9","#EE7621","#B22222","#FFD700","#CD5555","#DB4C6C",
"#8B658B","#1E90FF","#AF5F3C","#CAFF70", "#F9BD3F","#DAB370",
"#877F6C","#268785", '#82EF2D', '#B4EEB4']
ax = sc.pl.embedding(adata, basis="spatial",
color="domain",
s=30,
show=False,
palette=plot_color)
ax.set_title("")
ax.axis('off')
/home/tower2/anaconda3/envs/new_stellar/lib/python3.9/site-packages/scanpy/plotting/_tools/scatterplots.py:1251: FutureWarning: The default value of 'ignore' for the `na_action` parameter in pandas.Categorical.map is deprecated and will be changed to 'None' in a future version. Please set na_action to the desired value to avoid seeing this warning
color_vector = pd.Categorical(values.map(color_map))
/home/tower2/anaconda3/envs/new_stellar/lib/python3.9/site-packages/scanpy/plotting/_tools/scatterplots.py:394: UserWarning: No data for colormapping provided via 'c'. Parameters 'cmap' will be ignored
cax = scatter(
(73.05000000000001, 159.95, 141.7, 258.3)
import matplotlib.pyplot as plt
import scanpy as sc
#adata.obsm['spatial'][:, 1] = -1 * adata.obsm['spatial'][:, 1]
plt.rcParams["figure.figsize"] = (3,4)
# Define your custom color palette
plot_color=['#8B658B', '#F56867', '#DAB370', '#CAFF70', '#AF5F3C', '#82EF2D', '#59BE86', '#268785',
'#EE7621', '#1E90FF', '#DB4C6C','#556B2F', '#B4EEB4', '#4169E1', '#CD5555', '#FFD700', '#C798EE', '#8470FF',
'#006400', '#F9BD3F', '#CD69C9', '#877F6C']
unique_labels = sorted(adata.obs['domain'].unique())
label_to_color = {label: plot_color[i % len(plot_color)] for i, label in enumerate(unique_labels)}
label_to_color['5'] = '#B22222'
sc.pl.embedding(adata, basis="spatial",
color="domain",
s=30,
palette=label_to_color,
title="SemanticST",
show=False)
plt.axis('off')
plt.savefig("emb9/lov/SemanticST_emb9_lovf_wgf20.png", dpi=600,bbox_inches='tight')
for label in unique_labels:
fig, ax = plt.subplots()
ax.scatter(adata.obsm['spatial'][:, 0], adata.obsm['spatial'][:, 1],
c='lightgray', s=30)
label_indices = adata.obs['domain'] == label
ax.scatter(adata.obsm['spatial'][label_indices, 0],
adata.obsm['spatial'][label_indices, 1],
c=[label_to_color[label]], s=6)
ax.axis('off')
plt.show()
/home/tower2/anaconda3/envs/new_stellar/lib/python3.9/site-packages/scanpy/plotting/_tools/scatterplots.py:1251: FutureWarning: The default value of 'ignore' for the `na_action` parameter in pandas.Categorical.map is deprecated and will be changed to 'None' in a future version. Please set na_action to the desired value to avoid seeing this warning
color_vector = pd.Categorical(values.map(color_map))
/home/tower2/anaconda3/envs/new_stellar/lib/python3.9/site-packages/scanpy/plotting/_tools/scatterplots.py:394: UserWarning: No data for colormapping provided via 'c'. Parameters 'cmap' will be ignored
cax = scatter(