空间转录组 单细胞

空间转录组的邻域差异图绘制(高精度平台HD)

2024-05-24  本文已影响0人  单细胞空间交响乐

作者,Evil Genius

昨晚响应号召,一直值班到凌晨4点,各方面的消息来看,昨晚没有跳桥的。

但是官方的态度真让人寒心。

今天太原下雨,我们来画一画图吧,做空间分析的童鞋们下面的图应该是很常见的,

还有邻域差异图

还有下面这种的,区域细胞类型富集图

这种分析就是为了10X Visium HD、华大、CODEX等高精度平台准备的

简单的画一画这个热图

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)

Proximity analysis

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
)

生活很好,有你更好

上一篇 下一篇

猜你喜欢

热点阅读