Skip to content

单细胞空间转录组: 细胞通讯分析(基于 stLearn)

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

1. 模块简介

本模块基于 stLearn 算法,用于对空间转录组数据进行细胞通讯分析

stLearn是一种专门针对空间转录组数据开发的计算工具。它利用邻近位点间的配体(Ligand)和受体(Receptor)的共表达信息,结合空间距离和细胞类型的多样性,计算配体-受体(L-R)得分,进而鉴定组织内活跃的细胞通讯。实现以下核心分析目标:

  • 推断空间细胞通讯网络:评估数据集中所有潜在细胞类型对之间的相互作用,揭示细胞微环境中复杂的通讯网络拓扑结构。
  • 识别空间通讯热点区域:在给定的组织内找到通讯热点区域,在这些热点区域内,配受体相比于随机非交互基因对的空间分布,细胞类型间的交互更可能发生。

💡 Note 本流程输入为Scanpy 兼容格式数据及空间坐标文件,分析结果将生成展示细胞间互作关系的网络图、热图、和弦图及相关诊断图表。

算法原理简述: stLearn 通过整合基因表达数据与空间位置信息,首先计算受配体对的共表达,然后基于空间邻域(考虑物理距离)进行置换检验,评估观察到的受配体互作是否在空间上具有统计学显著性,从而精准推断空间微环境中的细胞通讯。

2. 输入文件准备

本模块需要提供包含表达矩阵和空间坐标的文件以及对应的 Metadata。

文件结构示例

text
filtered_feature_bc_matrix/          # rds 转化矩阵 输出目录
├── matrix.mtx.gz                    # 表达矩阵
├── barcodes.tsv.gz                  # 细胞条码
├── features.tsv.gz                  # 基因特征
└── cell_locations.tsv               # 空间坐标文件

如何从 Seurat 对象导出 stLearn 所需文件:

如果您已有 Seurat 对象(.rds),可以使用以下 R 代码片段将表达矩阵和空间坐标导出为上方所示的文件夹结构:

R
library(Seurat)
library(dplyr)
library(tibble)
library(readr)
library(Matrix)

# 1. 设定输入路径
rds_path <- "your_input.rds"
meta_path <- "your_meta.txt" 

# 2. 读取数据并整合 metadata
obj <- readRDS(rds_path)
meta <- read.delim(meta_path)
rownames(meta) <- meta$barcode
obj@meta.data <- meta

# 3. 创建输出目录
dir.create('filtered_feature_bc_matrix', showWarnings = FALSE)

# 4. 提取空间坐标并保存
spatial_data <- Embeddings(obj, reduction = 'spatial')
spatial_data <- as.data.frame(spatial_data)
colnames(spatial_data) <- c('x', 'y')
spatial_data <- rownames_to_column(spatial_data, "Barcode")
write_delim(spatial_data, 'filtered_feature_bc_matrix/cell_locations.tsv', delim = '\t')

# 5. 提取表达矩阵并保存 (MTX 格式)
writeMM(GetAssayData(obj, slot="counts"), file = 'filtered_feature_bc_matrix/matrix.mtx')

# 6. 保存基因和条码特征
Gene <- data.frame(GeneID = rownames(obj), Gene = rownames(obj))
write.table(Gene, 'filtered_feature_bc_matrix/genes.tsv', row.names = FALSE, col.names = FALSE, sep = '\t', quote = FALSE)

barcode <- data.frame(Barcode = colnames(obj))
write.table(barcode, 'filtered_feature_bc_matrix/barcodes.tsv', row.names = FALSE, col.names = FALSE, sep = '\t', quote = FALSE)

# 获取完整的 Base64 字符串,其中 info[["25020230_nao_CS_expression"]],可通过 names(obj@misc$info)进行查看
b64_string <- obj@misc$info[["25020230_nao_CS_expression"]]$img

# 去除字符串前面的 "data:;base64," 前缀
b64_clean <- sub("^data:.*base64,", "", b64_string)

# 将其解码为二进制数据并保存为 PNG 文件
bin_data <- jsonlite::base64_dec(b64_clean)

# 保存文件到当前工作目录
writeBin(bin_data, paste0("filtered_feature_bc_matrix/expression.png"))

3. 数据加载与预处理

python
import stlearn as st
import numpy as np
import pandas as pd
#matplotlib版本3.5.3,高版本的与stlearn有冲突
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import scanpy as sc
import re
import warnings
from PIL import Image
import os
output
/PROJ2/FLOAT/jinwen/apps/miniconda3/envs/stlearn/lib/python3.9/site-packages/stlearn/tools/microenv/cci/het.py:192: NumbaDeprecationWarning: The keyword argument 'nopython=False' was supplied. From Numba 0.59.0 the default is being changed to True and use of 'nopython=False' will raise a warning as the argument will have no effect. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.
@jit(parallel=True, nopython=False)
python

## sample_name:样本名
sample_name = "25020230_nao_CS_expression"
## image_path:空间HE染色相片路径
image_path = ""
## meta_path:注释的细胞类型文件路径
meta_path = "/PROJ2/FLOAT/jinwen/project/other_ans_project/25020230_nao_CS_demo/temp-20250814092026/data/rds/meta.tsv"
## spot_diameter_fullres:全分辨率下的 spot 直径
spot_diameter_fullres = 50
## species:物种选择,可选 "human" 或者 "mouse"
species = "mouse"
## grid_step:是否对空间切片进行网格划分
grid_step = False
n_ = 125
meta_celltype_colums_name = "RNA_snn_res.0.2"
python
# 读取SeekSpace数据并进行标准化、降维、聚类
warnings.filterwarnings('ignore')
warnings.simplefilter('ignore')
adata = sc.read_10x_mtx("./filtered_feature_bc_matrix/")
spatial = pd.read_csv('./filtered_feature_bc_matrix/cell_locations.tsv',sep="\t",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,spot_diameter_fullres=spot_diameter_fullres)
a.layers["raw_count"] = a.X
# Preprocessing
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)
output
Normalization step is finished in adata.X
Log transformation step is finished in adata.X
Scale step is finished in adata.X
PCA is done! Generated in adata.obsm['X_pca'], adata.uns['pca'] and adata.varm['PCs']


2026-05-28 16:37:31.067583: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2026-05-28 16:37:31.175224: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory
2026-05-28 16:37:31.175245: I tensorflow/compiler/xla/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2026-05-28 16:37:38.340693: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
2026-05-28 16:37:38.340786: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
2026-05-28 16:37:38.340792: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.


Created k-Nearest-Neighbor graph in adata.uns['neighbors']
Applying Louvain cluster ...n Louvain cluster is done! The labels are stored in adata.obs['louvain']
python
# 将注释好的细胞类型添加到adata对象中
cluster_name = "celltype"
celltype = pd.read_csv(meta_path,index_col=0,sep = "\t")
celltype = celltype.loc[a.obs.index]
a.obs[cluster_name] = celltype[meta_celltype_colums_name]
a.obs[cluster_name] = a.obs[cluster_name].astype('category')
python
# 将空间切片划分为长为n_,宽为n_
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
# 加载受配体库,计算受配体表达的显著性
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=16, # Number of CPUs for parallel. If None, detects & use all available.
                  )
output
Calculating neighbours...n 9 spots with no neighbours, 45 median spot neighbours.
Spot neighbour indices stored in adata.obsm['spot_neighbours'] & adata.obsm['spot_neigh_bcs'].
Altogether 1460 valid L-R pairs



enerating backgrounds & testing each LR pair...: 100%|██████████ [ time left: 00:00 ]


Storing results:

lr_scores stored in adata.obsm['lr_scores'].
p_vals stored in adata.obsm['p_vals'].
p_adjs stored in adata.obsm['p_adjs'].
-log10(p_adjs) stored in adata.obsm['-log10(p_adjs)'].
lr_sig_scores stored in adata.obsm['lr_sig_scores'].

Per-spot results in adata.obsm have columns in same order as rows in adata.uns['lr_summary'].
Summary of LR results in adata.uns['lr_summary'].

4. 细胞通讯计算结果

4.1 受配体表达显著性结果

python
lr_info = a.uns['lr_summary']

图注说明: 汇总表中展示了受配体互作的统计结果:

  • :代表不同的受配体互作对(Ligand-Receptor pairs)。
  • n_spot (第一列):在空间中存在互作的细胞/Spot 总数(未经过 P 值显著性过滤)。
  • n_spots_sig (第二列):经过多重假设检验校正后,具有统计学显著性的互作细胞/Spot 数量。
  • n_spots_sig_pval (第三列):根据用户设定的 P 值阈值过滤后的显著互作细胞/Spot 数量。
python
lr_info
n_spotsn_spots_sign_spots_sig_pval
Agrn_Atp1a31316428564935
Robo1_Robo1676227424992
Cadm1_Cadm11004626694703
Serpine2_Lrp1861124123242
Ptprm_Ptprm645223993745
............
Prok2_Prokr223723
Tnfsf13b_Tnfrsf13c24724
Spx_Galr325725
Ccl5_Ackr121621
Prl_Prlr21321

1460 rows × 3 columns

python

st.tl.cci.adj_pvals(a, correct_axis='spot',
                   pval_adj_cutoff=0.05, adj_method='fdr_bh')

4.2 受配体诊断图

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

图注说明: 受配体诊断图用于确认显著的互作热点与表达丰度或表达频率之间是否不存在强相关性(无偏性)。

  • 左图(表达水平):横轴为按照显著互作 细胞/Spot 数量排序的受配体对;纵轴为该受配体对在非零表达细胞中的表达中位值。
  • 右图(表达频率):横轴同左图;纵轴为受配体表达值为 0 的细胞占所有细胞的比例(即 Dropout 比例)。
python

st.pl.lr_diagnostics(a, show=False,figsize=(15,5))
plt.tight_layout()

4.3 显著互作的受配体散点图

图注说明: 显著互作的受配体散点图。

  • 横坐标(X 轴):代表按显著性排序的 Top 50 受配体对。
  • 纵坐标(Y 轴):代表经过 P 值过滤和校正后,该受配体对发生显著互作的细胞/Spot 数量(n_spots_sig)。
python

#st.pl.lr_summary(a, n_top=500,show=False)
st.pl.lr_summary(a, n_top=50, show=False,figsize=(10,5))
plt.tight_layout()

4.4 显著互作的受配体柱状图

图注说明: 受配体互作频次柱状图。

  • 横坐标(X 轴):代表按显著性排序的 Top 受配体对。
  • 纵坐标(Y 轴):代表存在互作的细胞/Spot 总数(包含显著与不显著)。
  • 柱子颜色绿色代表显著互作的比例,蓝色代表不显著的互作比例。
python

st.pl.lr_n_spots(a, n_top=50,max_text=100,show=False,figsize=(11, 5))
plt.tight_layout()

4.5 受配体互作得分空间分布图

本部分通过空间散点图,直观展示了特定受配体对在组织切片上的原始互作打分。互作得分(LR Score)是基于配体和受体在空间相邻细胞中的共表达丰度计算得出的连续数值。

图注说明:受配体互作得分(LR Score)空间散点图。

  • 底图:灰度显示的组织切片背景(如 H&E 或 DAPI 染色图),提供组织形态学参考。
  • 散点(点):图中的每一个点代表空间转录组切片上的一个特定细胞/Spot。
  • 点的颜色 (LR_score):点的颜色映射了该空间位置的受配体互作得分。颜色越亮(通常偏向黄/绿色,视色带而定),表示该局部的受体和配体共表达水平越高,潜在的细胞间通讯越强烈;颜色越暗(偏向深蓝/紫色),表示互作得分越低。
python
lr_scores = pd.DataFrame(a.obsm["lr_scores"])
lr_scores.columns = lr_info.index
lr_scores.index = a.obs.index

p_vals = pd.DataFrame(a.obsm["p_vals"])
p_vals.columns = lr_info.index
p_vals.index = a.obs.index

p_adjs = pd.DataFrame(a.obsm["p_adjs"])
p_adjs.columns = lr_info.index
p_adjs.index = a.obs.index

log10_p_adjs = pd.DataFrame(a.obsm["-log10(p_adjs)"])
log10_p_adjs.columns = lr_info.index
log10_p_adjs.index = a.obs.index
python
# 读取空间HE或者DAPI
background_image = mpimg.imread('filtered_feature_bc_matrix/expression.png')

lrs = a.uns['lr_summary'].index.values[0:3]
for best_lr in lrs[0:3]:
    if grid_step:
        tmp = pd.DataFrame(a.obsm["spatial"])
        tmp.columns = ["x","y"]
        tmp.index = a.obs.index
        tmp = tmp / 0.265385
        spatial_tmp = tmp.copy()
    else:
        spatial_tmp = spatial.copy()
    spatial_tmp["LR_score"] = lr_scores[best_lr]
    spatial_tmp["p_vals"] = p_vals[best_lr]
    spatial_tmp["p_adjs"] = p_adjs[best_lr]
    spatial_tmp["-log10(p_adjs)"] = log10_p_adjs[best_lr]
    
    # 创建一个新的图形
    fig, ax = plt.subplots(figsize=(12, 8))
    
    # 显示底片图像
    #ax.imshow(background_image, extent=[0, 55050, 0, 19906], aspect='auto',)
    ax.imshow(background_image, extent=[0, 55050, 0, 19906], aspect='auto', zorder=0, cmap='gray') 
    # 选择用于颜色渐变的数值列(请根据您的数据修改列名)
    # 例如:'expression_level', 'score', 'intensity' 等
    value_column = 'LR_score'  # 请替换为实际的数值列名
    
    # 创建颜色映射
    # 可以选择不同的colormap:'viridis', 'plasma', 'inferno', 'magma', 'coolwarm', 'RdBu_r' 等
    cmap = plt.cm.viridis
    norm = plt.Normalize(vmin=spatial_tmp[value_column].min(), 
                        vmax=spatial_tmp[value_column].max())
    
    # 创建散点图,根据数值列设置颜色
    scatter = ax.scatter(spatial_tmp['x'], spatial_tmp['y'], 
                        c=spatial_tmp[value_column], 
                        cmap=cmap, norm=norm, 
                        alpha=0.7, s=6, edgecolors='none')
    
    # 添加颜色条
    cbar = plt.colorbar(scatter, ax=ax, shrink=0.8)
    cbar.set_label(value_column, fontsize=12)
    
    # 添加标签和标题
    ax.set_xlabel('Spatial_1', fontsize=12)
    ax.set_ylabel('Spatial_2', fontsize=12)
    ax.set_title(f'{best_lr} {value_column}', fontsize=14)
    
    # 移除坐标轴刻度(可选,使图片更美观)
    ax.set_xticks([])
    ax.set_yticks([])
    ax.set_aspect("equal")
    # 显示图形
    plt.tight_layout()


    
    # 仍然显示(可选)
    plt.show()

4.6 受配体互作显著性 (p_adjs) 空间分布图

本部分展示了特定受配体对在各个空间位置互作的统计学显著性(调整后的 P 值)。这有助于过滤掉仅由随机共表达引起的背景噪声,精确定位具有真实生物学意义的通讯热点。

图注说明:特定受配体对的 P 值 (p_adjs) 空间分布散点图。

  • 底图:灰度显示的组织切片背景(如 H&E 或 DAPI 染色图),提供组织形态学参考。
  • 散点:图中的每一个点代表空间转录组切片上的一个特定细胞/Spot。
  • 颜色映射 (Colorbar):表示该位置受配体互作的调整后 P 值(p_adjs)。通常颜色越深(数值越小,接近 0),代表该区域的细胞通讯越具有统计学显著性。
python
# 读取空间HE或者DAPI
background_image = mpimg.imread('filtered_feature_bc_matrix/expression.png')

for best_lr in lrs[0:3]:
    if grid_step:
        tmp = pd.DataFrame(a.obsm["spatial"])
        tmp.columns = ["x","y"]
        tmp.index = a.obs.index
        tmp = tmp / 0.265385
        spatial_tmp = tmp.copy()
    else:
        spatial_tmp = spatial.copy()
    
    
    spatial_tmp["LR_score"] = lr_scores[best_lr]
    spatial_tmp["p_vals"] = p_vals[best_lr]
    spatial_tmp["p_adjs"] = p_adjs[best_lr]
    spatial_tmp["-log10(p_adjs)"] = log10_p_adjs[best_lr]

    # 创建一个新的图形
    fig, ax = plt.subplots(figsize=(12, 8))
    
    # 显示底片图像
    ax.imshow(background_image, extent=[0, 55050, 0, 19906], aspect='auto', zorder=0, cmap='gray')
    
    # 选择用于颜色渐变的数值列(请根据您的数据修改列名)
    # 例如:'expression_level', 'score', 'intensity' 等
    value_column = 'p_adjs'  # 请替换为实际的数值列名
    
    # 创建颜色映射
    # 可以选择不同的colormap:'viridis', 'plasma', 'inferno', 'magma', 'coolwarm', 'RdBu_r' 等
    cmap = plt.cm.viridis
    norm = plt.Normalize(vmin=spatial_tmp[value_column].min(), 
                        vmax=spatial_tmp[value_column].max())
    
    # 创建散点图,根据数值列设置颜色
    scatter = ax.scatter(spatial_tmp['x'], spatial_tmp['y'], 
                        c=spatial_tmp[value_column], 
                        cmap=cmap, norm=norm, 
                        alpha=0.7, s=6, edgecolors='none')
    
    # 添加颜色条
    cbar = plt.colorbar(scatter, ax=ax, shrink=0.8)
    cbar.set_label(value_column, fontsize=12)
    
    # 添加标签和标题
    ax.set_xlabel('Spatial_1', fontsize=12)
    ax.set_ylabel('Spatial_2', fontsize=12)
    ax.set_title(f'{best_lr} {value_column}', fontsize=14)
    
    # 移除坐标轴刻度(可选,使图片更美观)
    ax.set_xticks([])
    ax.set_yticks([])
    ax.set_aspect("equal")
    # 显示图形
    plt.tight_layout()

    
    # 仍然显示(可选)
    plt.show()

4.7 空间受配体共表达图

图注说明:特定受配体对的空间共表达图(展示全景)。

  • 散点结构:每个细胞/Spot被渲染为双层结构,分别代表配体和受体的表达状态。
  • 颜色判读
    • 若点为蓝色,表示该细胞/Spot同时高表达配体和受体(存在潜在的自分泌或紧密旁分泌互作)。
    • 若点仅红色或绿色,表示该 细胞/Spot仅单方面表达配体或受体。
  • 展示范围:展示组织切片上所有细胞/Spot的配受体表达状态,不受 P 值显著性过滤的限制。
python
# 1. 读取大尺寸底片
img_path = 'filtered_feature_bc_matrix/expression.png'
background_image = mpimg.imread(img_path)

for best_lr in lrs[0:3]:
    # 2. 备份当前的 spatial 坐标
    # 注意:前面的代码环境里对象名叫 a,如果你习惯用 block1,请把下面的 a 全部换成 block1
    original_spatial = a.obsm['spatial'].copy()
    
    # 3. 将细胞坐标放大回未缩放的尺寸,以完美匹配底片 [0, 55050, 0, 19906] 的范围
    a.obsm['spatial'] = original_spatial / 0.265385
    
    # 4. 创建唯一的画板
    fig, ax = plt.subplots(figsize=(12, 8))
    
    # 5. 按照你的要求绘制原始拉伸尺寸的底片,并置于最底层 (zorder=0)
    ax.imshow(background_image, extent=[0, 55050, 0, 19906], aspect='auto', zorder=0, cmap='gray')
    
    st.pl.lr_plot(a, best_lr, inner_size_prop=0.1, outer_mode='binary', pt_scale=5, 
                  use_label=None, show_image=False, 
                  sig_spots=False, bbox_to_anchor=(1.5, 1), 
                  fig=fig, ax=ax)  # <- 必须同时传 fig 和 ax 破解分图 bug
    
    # 6. 恢复原始缩放坐标,避免影响后续其他分析模块
    a.obsm['spatial'] = original_spatial
    
    # 7. 坐标轴美化及显示
    ax.set_xticks([])
    ax.set_yticks([])
    ax.set_aspect("equal")
    plt.tight_layout()
    
    # 仍然显示(可选)
    plt.show()

4.8 空间显著互作受配体共表达图

在上一部分全景展示的基础上,本部分进一步应用统计学显著性过滤,仅保留那些真正发生强烈且非随机互作的区域。

图注说明:特定受配体对的空间共表达图。

  • 散点结构:特定受配体对的空间共表达图(展示全景)。
  • 显著性过滤:图中仅高亮保留那些 P 值达到统计学显著性的互作热点区域,其余不显著的背景区域的散点将被隐藏或弱化。
  • 生物学意义:颜色显示的区域代表该受配体通讯在局部微环境中异常活跃,是组织特异性通讯的核心枢纽。
python
# 1. 读取大尺寸底片
img_path = 'filtered_feature_bc_matrix/expression.png'
background_image = mpimg.imread(img_path)

for best_lr in lrs[0:3]:
    # 2. 备份当前的 spatial 坐标
    # 注意:前面的代码环境里对象名叫 a,如果你习惯用 block1,请把下面的 a 全部换成 block1
    original_spatial = a.obsm['spatial'].copy()
    
    # 3. 将细胞坐标放大回未缩放的尺寸,以完美匹配底片 [0, 55050, 0, 19906] 的范围
    a.obsm['spatial'] = original_spatial / 0.265385
    
    # 4. 创建唯一的画板
    fig, ax = plt.subplots(figsize=(12, 8))
    
    # 5. 按照你的要求绘制原始拉伸尺寸的底片,并置于最底层 (zorder=0)
    ax.imshow(background_image, extent=[0, 55050, 0, 19906], aspect='auto', zorder=0, cmap='gray')
    
    st.pl.lr_plot(a, best_lr, outer_size_prop=1, outer_mode='binary', pt_scale=5,
                  use_label=None, show_image=False,
                  sig_spots=True, bbox_to_anchor=(1.5, 1),
                  fig=fig, ax=ax)
    
    # 6. 恢复原始缩放坐标,避免影响后续其他分析模块
    a.obsm['spatial'] = original_spatial
    
    # 7. 坐标轴美化及显示
    ax.set_xticks([])
    ax.set_yticks([])
    ax.set_aspect("equal")
    plt.tight_layout()
    
    # 仍然显示(可选)
    plt.show()
python
#若采用downsample,则运行下面代码(前提生成downsample后的barcode文件)
barcode = pd.read_csv("downsample_filtered_feature_bc_matrix/barcodes.tsv",index_col=None,header=None,names=['barcode'])
a = a[a.obs.index.isin(barcode["barcode"])]
python
# 预测显著互作的细胞类型(此步骤运行时间随细胞数增加而增加),教程用了downsample策略
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
                 )
output
Getting cached neighbourhood information...n Getting information for CCI counting...n


ounting celltype-celltype interactions per LR and permutating 1000 times.: 100%|██████████ [ time left: 00:00 ]

Significant counts of cci_rank interactions for all LR pairs in data.uns[lr_cci_celltype]
Significant counts of cci_rank interactions for each LR pair stored in dictionary data.uns[per_lr_cci_celltype]

4.9 细胞类型诊断图

检查受配体互作和细胞类型的细胞数之间的相关性,如果 permutations 的数量足够,下面的图表应该显示几乎没有或没有相关性;否则,建议将 n_perms(置换次数)的值调高。

图注说明: 细胞类型诊断图,用于检查受配体互作是否受细胞群体规模的偏倚影响。

  • 横坐标(X 轴):代表不同的细胞类型。
  • 柱状图(左侧纵轴):代表每种细胞类型的总细胞数量。
  • 折线图(右侧纵轴):代表该细胞类型参与互作的显著受配体数量。
python
st.pl.cci_check(a, cluster_name,show=False,figsize=(10,4),cell_label_size=10,axis_text_size=12)
plt.tight_layout()

4.10 细胞类型互作网络图

图注说明: 空间细胞类型互作网络图。

  • 节点(实心圆):每种颜色代表一种特定的细胞类型。
  • 边(连线):线条连接信号的发送方与接收方。箭头指向自身代表自发分泌通讯,指向其他细胞代表旁分泌通讯。
  • 连线颜色深浅:颜色的深浅代表这两种细胞类型之间通讯互作的数量。
python
st.pl.ccinet_plot(a, cluster_name, return_pos=True,pad=0.3,node_size_exp=0.8)
fig = plt.gcf()
fig.suptitle("Cell-Cell LR Interaction", fontsize=10)
fig.set_size_inches(5, 5)

lr_pair = a.uns['lr_summary'].index.values[0:3]
st.pl.ccinet_plot(a, cluster_name, lr_pair[0], min_counts=2,pad=0.3,node_size_exp=0.8)
fig = plt.gcf()
fig.suptitle("Cell-Cell "+lr_pair[0]+" Interaction", fontsize=10)
fig.set_size_inches(5, 5)

4.11 细胞类型互作和弦图

图注说明: 细胞类型互作和弦图。

  • 外环分段:圆环上的不同颜色块代表着不同的细胞类型。
  • 内侧弦线:连接存在通讯互作的两种细胞类型。
  • 弦线宽度:弦的宽度直观反映了细胞间相互作用的数量,弦越宽代表细胞间相互作用数量越多。
python
st.pl.lr_chord_plot(a, cluster_name,show=False)
fig = plt.gcf()
fig.suptitle("Cell-Cell LR Interaction", fontsize=10)
fig.set_size_inches(5, 5)


st.pl.lr_chord_plot(a, cluster_name,show=False,lr=lr_pair[0])
fig = plt.gcf()
fig.suptitle("Cell-Cell "+lr_pair[0]+" Interaction", fontsize=10)
fig.set_size_inches(5, 5)

4.12 细胞类型互作热图

图注说明: 受配体对在细胞间的互作强度热图。

  • 横坐标(X 轴):代表发生互作的细胞类型对(Sender -> Receiver)。
  • 纵坐标(Y 轴):代表具体的受配体对。
  • 颜色深浅:颜色偏红/深表示互作数高,颜色偏蓝/浅表示互作数低。
python

st.pl.lr_cci_map(a, cluster_name, lrs=None, min_total=100, figsize=(20,4),show=False)
output

图注说明: 细胞群整体互作强度热图。

  • 横坐标与纵坐标:均代表不同的细胞类型。
  • 热图方块:方块颜色的深浅反映了任意两种细胞类型之间整体通讯互作的数量。
python
st.pl.cci_map(a, cluster_name,show=False,figsize=(5,4))
fig = plt.gcf()
fig.suptitle("Cell-Cell LR Interaction", fontsize=10)
#fig.set_size_inches(5, 5)
plt.tight_layout()

st.pl.cci_map(a, cluster_name, lr=lr_pair[0],show=False,figsize=(5,4))
fig = plt.gcf()
fig.suptitle("Cell-Cell "+lr_pair[0]+" Interaction", fontsize=10)
#fig.set_size_inches(5, 5)
plt.tight_layout()
0 条评论·0 条回复