Skip to content

SeekSpace 高级分析:基于 stLearn 的细胞通讯热点识别

作者: SeekGene
时长: 9 分钟
字数: 2.0k 字
更新: 2026-02-28
阅读: 0 次
空间转录组 分析指南 Notebooks 细胞通讯分析
python
import stlearn as st
import numpy as np
import pandas as pd
#matplotlib版本3.5.3,高版本的与stlearn有冲突
import matplotlib.pyplot as plt
import scanpy as sc
import re

stLearn 细胞通讯模块输入参数介绍

matrix_path:matrix,feature,barcode 三个文件路径
spatial_path:空间位置路径
SAMple_name:样本名
image_path:空间 HE 染色相片路径
celltype_path:注释的细胞类型文件路径,文件中需要以 barcode 为行名,列名为“celltype”(细胞类型)为列
spot_diameter_fullres:细胞/spot 的大小,与分析步骤中的空间距离范围相关,可选 50 或者 100
species:物种,与下面参数 lr_database_bool 有关,若 lr_database_bool 为 True,则该处只能填"human"或者"mouse";若为 False,则不需要填写
lr_database_bool:是否用 stlearn 自带的受配体库,默认是 True。若不使用 stlearn 自带的受配体库,填 False,并在 lr_database 处填写相应的受配体库
lr_database_path:与 lr_database_bool 联用,若 lr_database_bool 参数填 False,则需要提供受配体库路径,文件内容包含受配体列(有行名,没有列名),如 ligand_receptor
ncpus:线程大小,关乎运行速度,可设置大写
cluster_name:设置 adata 对象 obs 中细胞类型的列名
lr_pair:是否自己设置展示的受配体对,如果不设置,填 None,则展示显著性高的 3 个受配体对;设置,填受配体对名,多个受配体对以逗号分隔,"A_B,C_D"
grid_step:是否画格子,对于细胞数量过多的数据,运行时间会长,推荐填 True;不使用则填 False
n_:长和宽划分格子的数量,若 grid_step 参数填 True,则 n_必填

python
matrix_path = ""
spatial_path = ""
sample_name = ""
image_path = ""
celltype_path = ""
spot_diameter_fullres = 50
species = "human"
lr_database_bool = True
lr_database_path = ""
n_cpus = 16
cluster_name = "celltype"
lr_pair = "None"
grid_step = True
n_ = 125

读取 SeekSpace 数据并进行标准化、降维、聚类

python
adata = sc.read_10x_mtx(matrix_path)
spatial = pd.read_csv(spatial_path,sep=",",index_col=0)
spatial = spatial.loc[:,("x","y")]
selected_rows = spatial.loc[spatial.index.isin(adata.obs_names)]
selected_rows.columns = ["imagecol","imagerow"]
selected_rows = selected_rows.reindex(adata.obs_names)
selected_rows = selected_rows*0.265385
a = st.create_stlearn(count=adata.to_df(),spatial=selected_rows,library_id=sample_name, scale=1,image_path=image_path,spot_diameter_fullres=spot_diameter_fullres)
a.layers["raw_count"] = a.X
# Preprocessing
#st.pp.filter_genes(a,min_cells=3)
st.pp.normalize_total(a)
st.pp.log1p(a)
# Keep raw data
a.raw = a
st.pp.scale(a)
st.em.run_pca(a,n_comps=50,random_state=0)
st.pp.neighbors(a,n_neighbors=25,use_rep='X_pca',random_state=0)
st.tl.clustering.louvain(a,random_state=0)
sc.tl.umap(a)

将注释好的细胞类型添加到 adata 对象中

python
celltype = pd.read_csv(celltype_path,index_col=0)
celltype.index = [re.sub(r'_9$', '', s) for s in celltype.index]
celltype = celltype.loc[a.obs.index]
a.obs[cluster_name] = celltype["celltype"]
a.obs[cluster_name] = a.obs[cluster_name].astype('category')

将空间切片划分为长为 n_,宽为 n_

python
if grid_step:
    #n_ = 125
    print(f'{n_} by {n_} has this many spots:\n', n_*n_)
    a = st.tl.cci.grid(a,n_row=n_, n_col=n_, use_label = cluster_name)
    print(a.shape )

加载受配体库,计算受配体表达的显著性

python

if lr_database_bool:
    lrs = st.tl.cci.load_lrs(['connectomeDB2020_lit'], species=species)
else:
    lrs = pd.read_csv(lr_database_path,names=["0"])
    lrs = np.array(list(lrs["0"]))

#lrs = st.tl.cci.load_lrs(['connectomeDB2020_lit'], species=species)
st.tl.cci.run(a, lrs,
                  min_spots = 20, #Filter out any LR pairs with no scores for less than min_spots
                  distance=None, # None defaults to spot+immediate neighbours; distance=0 for within-spot mode
                  n_pairs=100, # Number of random pairs to generate; low as example, recommend ~10,000
                  n_cpus=8, # Number of CPUs for parallel. If None, detects & use all available.
                  )

受配体表达显著性结果表格展示

行:受配体对
第一列 n_spot:未经过 pval 过滤受配体对互作的细胞/spot 数量(总)
第二列 n_spots_sig:经过矫正后的 pval 过滤后的受配体对显著互作的细胞/spot 数量
第三列 n_spots_sig_pval:根据设定的 pval 过滤后的受配体对互作的细胞/spot 数量

python
lr_info = a.uns['lr_summary']
lr_info
python
st.tl.cci.adj_pvals(a, correct_axis='spot',
                   pval_adj_cutoff=0.05, adj_method='fdr_bh')

诊断图(基于 lrfeatures 结果作图)

受配体分析的一个关键方面是在确定显著 hotspot 时,控制受配体的表达水平和频率。
因此,诊断图应显示受配体对的热点与表达水平和表达频率之间几乎没有相关性。
以下诊断方法可以帮助检查和确认这一点;如果不是这样,可能表明需要更大数量的 Permutations。

  • 左图:
    横轴为 n_spots_sig 的排序受配体对
    纵轴为受配体对(非零)表达中位值
  • 右图:
    横轴为 n_spots_sig 的排序受配体对
    纵轴为受配体对表达值为 0 的细胞占所有细胞的比例
python
st.pl.lr_diagnostics(a, figsize=(10,2.5))

基于 lr_summary 的结果作图:展示了 top500 和 top50 的受配体对
横轴代表 n_spots_sig 的排序受配体对
纵轴代表经过矫正后的 pval 过滤后的受配体对显著互作的细胞/spot 数量

python
st.pl.lr_summary(a, n_top=500)
st.pl.lr_summary(a, n_top=50, figsize=(10,3))

基于 lr_summary 的结果作图:展示了 top500 和 top50 的受配体对
横轴代表 n_spots_sig 的排序受配体对
纵轴代表未经过 pval 过滤受配体对互作的细胞/spot 数量(总) 颜色代表显著和不显著受配体对

python
st.pl.lr_n_spots(a, n_top=50, figsize=(11, 3),
                    max_text=100)
st.pl.lr_n_spots(a, n_top=500, figsize=(11, 3),
                    max_text=100)

预测显著互作的细胞类型

python
st.tl.cci.run_cci(a, cluster_name, # Spot cell information either in data.obs or data.uns
                  min_spots=3, # Minimum number of spots for LR to be tested.
                  sig_spots=True, # Only consider neighbourhoods of spots which had significant LR scores.
                  n_perms=1000, # Permutations of cell information to get background, recommend ~1000
                  n_cpus=16
                 )

诊断图:检查受配体互作和细胞类型的细胞数之间的相关性

如果 permutations 的数量足够,下面的图表应该显示几乎没有或没有相关性;否则,建议将 n_perms(置换次数)的值调高。
横轴代表细胞类型
柱状图对应的左纵轴代表细胞类型数量
折线对应的由纵轴代表细胞类型互作的受配体数量

python
st.pl.cci_check(a, cluster_name)

CCI 网络图

图中展示对应受配体对的互作图,通过结果 per_lr_cci_*绘制的

python
# Visualising the no. of interactions between cell types across all LR pairs #
pos_1 = st.pl.ccinet_plot(a, cluster_name, return_pos=True)

# Just examining the cell type interactions between selected pairs #

if lr_pair == None:
    lr_pair=None
else:
    lr_pair=lr_pair.strip.split(",")

if lr_pair is not None:
    for best_lr in lr_pair:
            st.pl.ccinet_plot(a, cluster_name, best_lr, min_counts=2,
                              figsize=(10,7.5), pos=pos_1
                             )
else:
    lr_pair = a.uns['lr_summary'].index.values[0:3]
    for best_lr in lr_pair[0:3]:
        st.pl.ccinet_plot(a, cluster_name, best_lr, min_counts=2,
                          figsize=(10,7.5), pos=pos_1
                         )

CCI 和弦图

图中展示对应受配体对的互作弦和图

python
st.pl.lr_chord_plot(a, cluster_name)
for lr in lr_pair:
    st.pl.lr_chord_plot(a, cluster_name, lr)

CCI 热图

图中展示对应受配体对的互作热图,横轴展示互作的细胞类型,纵轴展示互作的受配体,可通过参数 n_top_lrs 和 n_top_ccis 调整展示的个数,或者指定受配体对

python
st.pl.lr_cci_map(a, cluster_name, lrs=None, min_total=100, figsize=(20,4))
python
st.pl.lr_cci_map(a, cluster_name, lrs=lr_pair, min_total=100, figsize=(20,4))

图中展示对应受配体对的互作热图,横轴和纵轴展示细胞类型,可指定展示的受配体对

python
st.pl.cci_map(a, cluster_name)

lr_pair = a.uns['lr_summary'].index.values[0:3]
for lr in lr_pair[0:3]:
    st.pl.cci_map(a, cluster_name, lr)

文献案例解析

  • 文献一:
    文献《Spatially organized tumor-stroma boundary determines the efficacy of immunotherapy in colorectal cancer patients》使用 stlearn 去揭示肿瘤与基质区的交界处的细胞类型互作情况。

参考文献

Pham, D., Tan, X., Balderson, B. et al. Robust mapping of spatiotemporal trajectories and cell–cell interactions in healthy and diseased tissues. Nat Commun 14, 7739 (2023).
Feng, Y., Ma, W., Zang, Y. et al. Spatially organized tumor-stroma boundary determines the efficacy of immunotherapy in colorectal cancer patients. Nat Commun 15, 10259 (2024).

0 条评论·0 条回复