Skip to content

CellCharter 空间转录组分析:空间域识别与细胞微环境解析

作者: SeekGene
时长: 6 分钟
字数: 1.5k 字
更新: 2026-02-28
阅读: 0 次
空间转录组 分析指南 Notebooks 细胞通讯分析

背景介绍

算法原理如下:

  • CellCharter 首先根据细胞或斑点的空间坐标构建一个空间网络。在这个网络中,每个细胞或斑点被视为一个节点,节点之间的连接基于它们的空间邻近性。

  • 邻域富集分析:CellCharter 计算两个细胞群之间的连接数,并与随机预期进行比较。

    • 观察值(Observed):两个细胞群之间的实际连接数。

    • 预期值(Expected):基于节点度数的随机预期连接数。

    • 邻域富集分数(NE)为观察值与预期值的比值:NE = Observed/Expected

  • 差异邻域富集分析

  • CellCharter 还可以比较不同条件下(如健康与疾病)的邻域富集分析结果,计算差异邻域富集分数(Differential NE)。

    • 差异邻域富集分数为两个条件下的邻域富集分数之差。为了评估差异的显著性,CellCharter 可以通过随机采样条件标签来计算 P 值。

CellCharter 的邻域富集分析通过构建空间网络,计算细胞群之间的空间邻近性,并与随机预期进行比较,从而揭示细胞在组织中的空间相互作用。其非对称邻域富集分析进一步揭示了细胞群之间的偏好性连接,而差异邻域富集分析则用于比较不同条件下的空间相互作用变化。

CellCharter 的计算结果

加载分析需要的包

python
import squidpy as sq
import cellcharter as cc
import scanpy as sc
import pandas as pd
import matplotlib.pyplot as plt
from itertools import combinations
import matplotlib.image as mpimg
import os
output
/PROJ2/FLOAT/jinwen/apps/miniconda3/envs/cellcharter-env/lib/python3.9/site-packages/numba/core/decorators.py:246: RuntimeWarning: nopython is set for njit and is ignored
warnings.warn('nopython is set for njit and is ignored', RuntimeWarning)
/PROJ2/FLOAT/jinwen/apps/miniconda3/envs/cellcharter-env/lib/python3.9/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
from .autonotebook import tqdm as notebook_tqdm
python
meta_path="../../data/AY1748480899609/meta.tsv"
col_sam="Sample"
sample_name="N1_expression,N2_expression"
col_celltype="CellAnnotation"
col_group="Sample"
python
sample_name=sample_name.strip().split(",")

读取数据

python
adata = sc.read_10x_mtx("filtered_feature_bc_matrix/",cache=False) 

## 添加坐标信息
spatial_df = pd.read_csv("filtered_feature_bc_matrix/cell_locations.tsv",index_col=0,sep='\t')
spatial_df1 = spatial_df[spatial_df.index.isin(adata.obs_names)]
adata.obsm["spatial"] = spatial_df1.values
meta_df = pd.read_csv(meta_path,index_col=0,sep='\t')
meta_df = meta_df.reindex(adata.obs_names)
adata.obs = pd.concat([adata.obs, meta_df], axis=1)
python
adata.obs[col_sam] = adata.obs[col_sam].astype('category')
adata.obs[col_celltype] = adata.obs[col_celltype].astype('category')
python
sq.gr.spatial_neighbors(adata, library_key=col_sam, coord_type='generic', delaunay=True)

单样本细胞类型共定位

评估两个细胞群(或细胞状态)之间的空间邻近性是否高于随机预期。

python

for i in range(len(sample_name)):
    adata1 = adata[adata.obs[col_sam] == sample_name[i]]
    cc.gr.nhood_enrichment(
        adata1,
        cluster_key=col_celltype,
    )

    cc.pl.nhood_enrichment(
        adata1,
        cluster_key=col_celltype,
        annotate=True,
        vmin=-0.1,
        vmax=0.1,
        figsize=(5,5),
        fontsize=7,
        title=sample_name[i]+" Neighborhood enrichment",
        save="./"+sample_name[i]+"_"+col_celltype+"_enrichment_heatmap.png"
    )
output
/PROJ2/FLOAT/jinwen/apps/miniconda3/envs/cellcharter-env/lib/python3.9/site-packages/cellcharter/gr/_nhood.py:236: ImplicitModificationWarning: Trying to modify attribute \`._uns\` of view, initializing view as actual.
adata.uns[f"{cluster_key}_nhood_enrichment"] = result
/PROJ2/FLOAT/jinwen/apps/miniconda3/envs/cellcharter-env/lib/python3.9/site-packages/cellcharter/gr/_nhood.py:236: ImplicitModificationWarning: Trying to modify attribute \`._uns\` of view, initializing view as actual.
adata.uns[f"{cluster_key}_nhood_enrichment"] = result
- 行:源细胞类型 Ci;列:目标细胞类型 Cj。
- 正数(Observed > Expected):表示细胞群 Ci 与细胞群 Cj 在空间上倾向于**富集**,即更有可能相邻。Ci 倾向于靠近 Cj
- 负数(Observed < Expected):表示细胞群 Ci 与细胞群 Cj 在空间上倾向于**排斥**,即更有可能分离。Ci 倾向于远离 Cj
- 零(Observed = Expected):表示细胞群 Ci 与细胞群 Cj 的连接数与随机预期一致,没有明显的富集或排斥。

多样本比较细胞类型共定位

比较不同条件(样本/组)邻域富集情况

python
cc.gr.diff_nhood_enrichment(
    adata,
    cluster_key=col_celltype,
    condition_key=col_sam,
    library_key=col_group,
    pvalues=True,
    n_jobs=15,
    n_perms=100
)
output
00%|██████████| 100/100 [00:01<00:00, 55.62it/s]
python
combinations_list = list(combinations(sample_name, 2))
for i in range(len(combinations_list)):
    cc.pl.diff_nhood_enrichment(
        adata,
        cluster_key=col_celltype,
        condition_key=col_group,
        condition_groups=list(combinations_list[i]),
        annotate=True,
        figsize=(5,5),
        #significance=0.05,
        fontsize=5,
        save="./"+list(combinations_list[i])[0]+"vs"+list(combinations_list[i])[1]+"_"+col_celltype+"_enrichment_heatmap.png"
    )
- 行:源细胞类型 Ci;列:目标细胞类型 Cj。
- 正数(Observed > Expected):表示在实验组(vs 前)中细胞群 Ci 与细胞群 Cj 在空间上倾向于**富集**,即更有可能相邻。
- 负数(Observed < Expected):表示在实验组(vs 前)中细胞群 Ci 与细胞群 Cj 在空间上倾向于**排斥**,即更有可能分离。
- 零(Observed = Expected):表示细胞群 Ci 与细胞群 Cj 的连接数与随机预期一致,没有明显的富集或排斥。

结果文件

  • *_enrichment_heatmap.png/pdf 细胞类型共定位图

参考资料

[1] Varrone M, Tavernari D, Santamaria-Martínez A et al. CellCharter reveals spatial cell niches associated with tissue remodeling and cell plasticity[J]. Nat Genet, 2024, 56: 74–84.

附录

.xls,.txt :结果数据表格文件,文件以制表符(Tab)分隔。unix/Linux/Mac 用户使用 less 或 more 命令查看;windows 用户使用高级文本编辑器 Notepad++ 等查看,也可以用 Microsoft Excel 打开。

.png:结果图像文件,位图,无损压缩。

.pdf:结果图像文件,矢量图,可以放大和缩小而不失真,方便用户查看和编辑处理,可使用 Adobe Illustrator 进行图片编辑,用于文章发表等。

0 条评论·0 条回复