-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
整合单细胞和空转数据多种方法之Cell2location #5592
Comments
整合单细胞和空转数据多种方法之Cell2location by 生信随笔除了我们上期介绍过的
结果显示,cell2location、spatialDWLS、RCTD等算法具有更优异的去卷积性能,能够更准确地拆分空间转录组数据的细胞类型组成。
“Taken together patterns observed from internal and external reference, our results suggest that RCTD, cell2location and stereoscope are among the most robust to batch effects between reference scRNA-seq and target ST data.” 同样表明RCTD和cell2location算法依旧"遥遥领先"! 整合算法很多,我们按照排名一个一个来,这次我们先介绍cell2location以及代码实战。 Cell2location发表于2022年的Nature Biotechnology,题为《Cell2location maps fine-grained cell types in spatial transcriptomics》。该方法依赖于贝叶斯模型,旨在解析空间转录组数据中更为精细的细胞类型的,并创建不同组织的细胞组成图谱。 image-20231228154347929本文将介绍:
一. Cell2location算法的工作流程image-20231228154505635Cell2location采用贝叶斯层次框架。首先使用外部单细胞RNA测序数据作为参考数据,估计细胞类型特异性基因表达特征。然后,通过负二项分布(negative binomial regression)对观察到的空间表达计数矩阵建模,其中均值参数取决于参考细胞类型的特征,过度分散参数使用指数-伽马复合先验建模,旨在使大多数基因具有低过度分散。基因特异性技术灵敏度和基因-位置特异性的加性偏移被包括在均值参数的一部分,每个都使用单独的层次伽马先验进行建模。Cell2location进一步使用层次伽马先验对细胞类型特征的回归权重进行建模,并将回归权重分解为多个潜在组的贡献,这可以解释为具有共享细胞类型丰度特征的斑点,旨在跨具有相似细胞组成的位置借用强度。最后,cell2location采用变分贝叶斯推断来近似后验分布,并相应地生成参数估计。总的来说,Cell2location提供了一种可靠的途径来处理模型中的不确定因素,可以解释细胞类型丰度的线性依存关系。相比于其他的联合分析工具,Cell2location体现了更高的运算效率。 注意Cell2location运行非常慢,建议使用GPU进行加速! 二. Cell2location代码实现Step0.环境部署github在https://github.com/BayraktarLab/cell2location#Installation 官方教程在:https://cell2location.readthedocs.io/en/latest/ 可以使用Conda安装Cell2location:
如果需要衔接
Step1.加载示例数据及预处理示例数据我们使用上期的 Python包加载:
注意:cell2location需要count原始数据作为input数据,不能用标准化后的。参考:https://github.com/BayraktarLab/cell2location/issues/249 1.1 加载示例空转数据:
检查数据: image-20231228210651319线粒体编码基因与空间定位无关,因为它们的表达代表单细胞和细胞核数据中的技术产物,而不是线粒体的生物丰度。然而,这些基因在每个位置组成了15-40%的mRNA。因此,为了避免噪音,我们强烈建议去除线粒体基因:
过滤掉一些线粒体基因。 1.2 加载示例单细胞数据:
检查数据: image-20231228212749887注意:在估计参考细胞类型的signature之前,作者建议进行宽松一点的基因选择。作者更推荐于这种方式,而不是标准的高变异基因选择,因为作者的程序保留了罕见基因的标记,同时去除了大多数无信息的基因。 默认参数 image-20231228211554743在这个二维直方图中,橙色矩形突出显示了基于表达该基因的细胞数量(Y轴)和检测到该基因的细胞的平均RNA计数(X轴)的组合而排除的基因。 Step2. 参考细胞类型特征估计(NB回归)根据scRNA-seq数据估计特征,考虑批效应,使用负二项回归模型。这个步骤非常慢,最好有GPU加速: 首先,为回归模型准备一个数据对象:
训练模型。现在我们训练模型以估计参考细胞类型的特征。 请注意,为了在您的数据上实现收敛(即获得损失的稳定),用户可能需要增加 还请注意,这里我们使用的
确定模型是否需要更多训练。 在这里,我们绘制训练过程中的 ELBO 损失历史,从图中去掉前 20 个 epochs。这个图应该呈下降趋势,并在训练结束时趋于平稳。如果仍在下降,请增加 image-20231228215456711
用户可以直接计算后验分布的5%、50%和95%分位数,而不是使用1000个样本(或其他任何分位数)从分布中抽取。这样可以加快在大型数据集上的应用并减少内存需求,但无法用这种方式计算后验均值和标准差。 下面使用的是官方的代码报错了: image-20231228215931322我检查了这个函数: image-20231228220006931
提取参考细胞类型签名作为 负二项回归模型的所有参数都被导出到参考 anndata 对象中,但是对于空间映射,我们只需要每个基因在每个细胞类型中的估计表达。在这里,我们从标准输出中提取这些信息: image-20231228220607391Step3. Cell2location: spatial mapping寻找共享基因,准备数据。基于参考signature,对空转数据取子集:
重要提示: 要使用cell2location空间映射模型,用户需要指定两个超参数(
将预期的每个组织位置的细胞丰度N_cells_per_location调整到实际组织情况是很有用的。这个值可以从成对的组织学图像中估计,如上面的说明所述。请将本教程中呈现的值(N_cells_per_location=30)更改为您的组织中观察到的值。
为了提高在幻灯片/批次内RNA检测敏感性存在较大技术变异性的数据集上的准确性和灵敏性 - 用户需要放宽对每个位置规范化的正则化(使用detection_alpha=20)。当用户观察到每个位置的总RNA计数的空间分布与基于组织学检查的预期细胞数不匹配时,说明在用户的样本中存在较大的RNA检测敏感性技术变异性。 作者最初选择高正则化(detection_alpha=200)作为默认值,因为作者在论文中使用的小鼠大脑和人类淋巴结数据集具有较低的技术效应,并且使用高正则化强度提高了每个位置的总估计细胞丰度与组织学定量核数之间的一致性(请参见cell2location论文的图S8F)。然而,在许多合作中,作者发现在人类组织的Visium实验中存在技术效应。这促使了将detection_alpha的新默认值设为20以及建议在用户的数据上测试这两个设置(detection_alpha=20和detection_alpha=200)。 image-20231228221410647Training cell2location(这个步骤非常慢):
这里我遇到报错了:
查了github:
原来是我的空转数据不是raw count数据,我重新把空转数据读入scanpy...这里跳过这个debug步骤,再次强调:cell2location的单细胞和空转需要raw count数据作为input! 另外,遇到报错不用紧张,多查查github或者是中英文帖子,如果一个算法不是很小众的话,基本会有答案的! 这里为了节省时间,我没有跑30000次迭代(在GPU的加速下需要运行50分钟),而是选择了300次,在GPU的加速下运行1分钟不到。 Step4. 导出预测的细胞丰度结果
模型和输出h5ad,可以像这样重新加载:
image-20231229105129862图形呈对角线状说明质量较好。 导出预测的细胞丰度结果为csv文件:
image-20231228231738126
Step5. 可视化
作者建议使用后验分布的5%分位数的结果,表示模型具有高置信度的细胞丰度值(即“至少存在这个数量”):
我们这里只有一张切片,就不用跑这个步骤了 image-20231228230033246image-20231228225831646 image-20231228230852858 image-20231228230914261Step6. 下游分析6.1 用Leiden聚类识别离散组织区域通过使用由cell2location估计的细胞丰度对位置进行聚类,我们识别在细胞组成方面存在差异的组织区域。 通过使用每种细胞类型的估计细胞丰度对Visium点进行聚类,我们找到组织区域。我们构建一个表示在估计的细胞丰度中位置相似性的K最近邻(KNN)图,然后应用Leiden聚类。KNN邻居的数量应根据数据集的大小和解剖学定义的区域大小进行调整(例如,与大脑的大小相比,海马区域相对较小,因此可能被大量邻居掩盖)。可以在一系列的KNN邻居和Leiden聚类分辨率下执行此操作,直到获得与组织解剖结构匹配的聚类。 聚类是在所有Visium截面/批次中共同完成的,因此区域身份是直接可比的。当存在多个批次之间的强技术效应时(在本例中并非如此),原则上可以使用sc.external.pp.bbknn来在KNN构建过程中考虑这些效应。
image-20231229114655817 image-202312291145164336.2 使用矩阵分解(NMF)识别细胞区室/组织区在这里,我们使用cell2location映射结果来识别细胞类型的空间共存,以更好地理解组织结构并预测细胞间的相互作用。我们对来自cell2location的细胞类型丰度估计进行了非负矩阵分解(NMF)。类似于将NMF应用于常规scRNA-seq的已建立的优势,加性NMF分解将空间细胞类型丰度文件组成组件,捕获共定位的细胞类型。这种基于NMF的分解自然地考虑了多个细胞类型和微环境可以在同一Visium位置共存的事实,同时在组织区域之间共享信息。 提示: 在实践中,最好对一系列因子 如果要找到一些最明显的细胞区,使用较少的因子。如果要找到非常强的共定位信号并假设大多数细胞类型不共定位,则使用大量因子(>30)。 下面作者展示了如何执行这个分析。作者将NMF包装成一个pipeline:
对于每个因子编号,模型都会生成以下文件夹输出列表:
提示: NMF模型的输出,如因子加载,存储在 image-202312291251515366.3 估计空间数据中每个基因的细胞类型特异性表达在空间坐标中绘制基因的细胞类型特异性表达。
遇到报错了,解决方案:
运行上述代码前需要先运行:
然后运行:
然后运行就成功了: image-20231229143541554
除了这些可视化内容,cell2location还有一些个性化绘图内容,有机会我们再做介绍! - END - |
https://mp.weixin.qq.com/s/Y1rBQh-G7R_EaEdNMENHdA
The text was updated successfully, but these errors were encountered: