前面我们使用10x visium HD结直肠癌样本数据介绍了基于H&E图像进行细胞分割,得到了单细胞精度的空间数据,然后和单细胞分析一样,进行降维聚类,接下来我们就可以就可以进行细胞类型注释了。
2024年11月20日Nature上发表了一篇文章 A Cell Atlas Foundation Model for Scalable Search of Similar Human Cells 介绍了一个名为SCimilarity的基础模型,它通过深度度量学习为单细胞RNA测序数据提供了一种统一、可解释的表征方式,用于在海量细胞数据集中快速查找相似细胞。实现对来自不同研究的数千万个细胞谱文件进行快速查询,找到与输入细胞谱文件或状态在转录水平上相似的细胞。使用监督三元组损失函数在一个包含超过2300万个单细胞和单核RNA测序谱文件的大型多样性图谱上进行训练,将来自匹配细胞类型的表达谱文件嵌入到彼此接近的位置,并使用无监督的平均平方误差重建损失来保留细胞类型内表达模式的变化,实现高效的细胞搜索和注释。今天我们尝试使用SCimilarity对我们的数据进行细胞类型注释。
官方介绍SCimilarity相较于传统方法的优势包括:
无需重新训练:预训练模型即可直接应用于新的数据集。
高效查询:对2300万细胞数据库的查询仅需数秒。
普适性:能够跨越不同组织、疾病和实验条件生成一致的细胞表征。
解释性强:通过整合梯度方法,揭示模型对每个细胞类型的重要基因特征。
文章大致看了一遍,说对比scGPT和scFoundation都有较大的提升,准备先测试看看,好用的话再回去仔细读读。
作者在github上开源了模型文件和API接口文档: https://genentech.github.io/scimilarity/index.html
分析环境加载
import os
import pandas as pd
import scanpy as sc
import squidpy as sq
from scimilarity import CellAnnotation
from scimilarity.utils import lognorm_counts, align_dataset
import matplotlib.pyplot as plt
from matplotlib.pyplot import rc_context
import warnings
warnings.filterwarnings('ignore')
plt.style.use('default')
plt.rcParams['figure.facecolor'] = 'white'
os.environ['OPENBLAS_NUM_THREADS'] = '32'
sc.settings.set_figure_params(dpi=100, dpi_save=200, figsize=(5, 5), facecolor='white')
开始测试SCimilarity的CellAnnotation功能,我们用的模型是官方提供的预训练23.4M人细胞的模型,小鼠的模型官方没有提供,但是给了训练模型的接口,可以自己尝试训练模型。
使用很简单,但是注意要查询的单细胞数据中必须包含原始counts结果的layer。细胞类型标注有两种模式,一种是Unconstrained annotation(无约束标注-细胞可以被归类为SCimilarity参考中的任何类型,SCimilarity中细胞类型共203种),另一种是Constrained classification(约束分类-我们知道要查询的数据中大概包含哪些细胞类型,将参考数据子集化,仅包含我们想要分类的细胞类型。这也有助于减少细胞类型注释中的噪声),这里我们选择约束分类,因为我们知道肠癌样本中大概包含哪些细胞类型。
adams = sc.read_h5ad('./adata_cellpose.h5ad')
adams.layers['counts'] = adams.X.copy()
# Instantiate the CellAnnotation object
model_path = "./software/SCimilarity/model_v1.1"
ca = CellAnnotation(model_path=model_path)
# Match feature space with SCimilarity models
adams = align_dataset(adams, ca.gene_order, gene_overlap_threshold=5000)
# Normalize data consistent with SCimilarity
adams = lognorm_counts(adams)
# Compute embeddings
adams.obsm["X_scimilarity"] = ca.get_embeddings(adams.X)
无约束标注,如果不确定样本中包含细胞类型,可以先无约束标注,看看大概有哪些细胞类型。
# Unconstrained annotation 无约束标注--细胞可以被归类为 SCimilarity 参考中的任何类型
predictions, nn_idxs, nn_dists, nn_stats = ca.get_predictions_knn(
adams.obsm["X_scimilarity"]
)
adams.obs["predictions_unconstrained"] = predictions.values
celltype_counts = adams.obs.predictions_unconstrained.value_counts()
well_represented_celltypes = celltype_counts[celltype_counts > 20].index
sc.pl.umap(
adams[adams.obs.predictions_unconstrained.isin(well_represented_celltypes)],
color="predictions_unconstrained",
legend_fontsize=5,
)
约束标注细胞
# Constrained classification 约束标注细胞只会被归类为给定的参考类型
target_celltypes = [
"endothelial cell",
"epithelial cell",
"colon epithelial cell",
"macrophage",
"B cell",
"plasma cell",
"fibroblast",
"monocyte",
"classical monocyte",
"non-classical monocyte",
"conventional dendritic cell",
"plasmacytoid dendritic cell",
"T cell",
"CD4-positive, alpha-beta T cell",
"regulatory T cell",
"CD8-positive, alpha-beta T cell",
"mature NK T cell",
"natural killer cell"
]
ca.safelist_celltypes(target_celltypes)
adams = ca.annotate_dataset(adams)
细胞注释完结果空间位置展示
with rc_context({'figure.figsize': (8, 8)}):
sc.pl.spatial(
adata,
color=['celltype_hint'],
title='RCTD CellType',
# library_id='P1_CRC',
alpha_img=0.3,
spot_size=100
)
对照文章中RCTD使用单细胞反卷积结果看起来基本上细胞大类位置都正确,有一点就是片子上半部分应该是杯状细胞,但是我们约束细胞类型标注没有加上杯状细胞,可以看到这部分细胞被注释到上皮细胞了。