import squidpy as sq
import cellcharter as cc
import scanpy as sc
from lightning.pytorch import seed_everything
adata = cc.datasets.codex_mouse_spleen('./data/codex_mouse_spleen.h5ad')
adata.raw = adata.copy()
for sample in adata.obs['sample'].cat.categories:
adata.X[adata.obs['sample'] == sample, :] = sc.pp.scale(adata[adata.obs['sample'] == sample], copy=True).X
model = cc.tl.TRVAE.load('./tutorial_models/codex_mouse_spleen_trvae', adata, map_location='cpu')
adata.obsm['X_trVAE'] = model.get_latent(adata.X, adata.obs['dataset'])
sq.gr.spatial_neighbors(adata, library_key='sample', coord_type='generic', delaunay=True)
cc.gr.aggregate_neighbors(adata, n_layers=3, use_rep='X_trVAE')
gmm = cc.tl.Cluster(
n_clusters=11,
random_state=12345,
# If running on GPU
#trainer_params=dict(accelerator='gpu', devices=1)
)
gmm.fit(adata, use_rep='X_cellcharter')
adata.obs['spatial_cluster'] = gmm.predict(adata, use_rep='X_cellcharter')
sq.pl.spatial_scatter(
adata,
color='spatial_cluster',
library_key='sample',
img=None,
title=['BALBc-1', 'MRL-4 (early)', 'MRL-8 (intermediate)', 'MRL-9 (late)'],
size=2500,
ncols=2,
)
cc.gr.enrichment(adata, group_key='cell_type', label_key='spatial_cluster', observed_expected=True)
cc.pl.enrichment(adata, group_key='cell_type', label_key='spatial_cluster', color_threshold=0.58, size_threshold=0.58)
adata.obs['condition'] = adata.obs['sample'].str.split('-').str[0].astype('category')
adata_balbc = adata[adata.obs['condition'] == 'BALBc']
cc.gr.nhood_enrichment(
adata_balbc,
cluster_key='spatial_cluster',
)
cc.pl.nhood_enrichment(
adata_balbc,
cluster_key='spatial_cluster',
annotate=True,
vmin=-1,
vmax=1,
)
cc.gr.diff_nhood_enrichment(
adata,
cluster_key='spatial_cluster',
condition_key='condition',
library_key='sample',
pvalues=True,
n_jobs=15,
n_perms=100
)
cc.pl.diff_nhood_enrichment(
adata,
cluster_key='spatial_cluster',
condition_key='condition',
condition_groups=['MRL', 'BALBc'],
annotate=True,
figsize=(7,7),
significance=0.05
)
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。