admin
发布于 2025-04-21 / 8 阅读
0

CellChat运行性能实测:不同细胞数下的内存与耗时表现

在单细胞转录组分析中,细胞通讯分析已成为解读细胞间相互作用的重要工具。CellChat 作为一款广受欢迎的 R 包,具备高灵活性和丰富的可视化能力。但在处理大规模数据时,它的计算资源消耗究竟有多大?本次我们基于真实数据集,对不同细胞量级下 CellChat 的运行时间与内存消耗进行了系统测试。

稿定设计-4.jpg


一、测试数据准备

本次测试使用的数据来源于 GEO 数据库中的两个单细胞测序数据集:GSE131907 和 GSE222318。为了模拟不同数据量的实际分析需求,我们将整合后的数据集按照细胞数划分为五个子集:

  • 2.5万细胞

  • 5万细胞

  • 10万细胞

  • 20万细胞

  • 30万细胞

这些数据在预处理阶段已完成整合与下采样处理,并以 .rds 格式保存,方便直接读取并进入 CellChat 分析流程。

# r复制编辑
suppressMessages({
  library(Seurat)
  library(CellChat)
  library(peakRAM)
  library(patchwork)
})

# 读取整合后的数据对象
obj_30w <- readRDS("../11_monocle//obj_30w.rds")

# 分别构建不同细胞数量的数据集
obj_2.5w <- subset(obj_30w, downsample=370)
obj_5w   <- subset(obj_30w, downsample=770)
obj_10w  <- subset(obj_30w, downsample=1900)
obj_20w  <- subset(obj_30w, downsample=5000)

二、性能测试流程

本次测试将逐个分析不同细胞数量的数据集,在相同的计算环境下运行相同的 CellChat 分析流程,并记录内存使用与耗时表现。核心分析过程包括:

  • 构建 CellChat 对象

  • 加载数据库

  • 表达量预处理

  • 通讯计算与聚合

以下是关键代码框架:

r


复制编辑
for (obj in c(obj_2.5w,obj_5w,obj_10w,obj_20w,obj_30w)){
  print(obj)
  print(
    peakRAM({
      data.input <- GetAssayData(obj, layer = "data")
      meta <- obj@meta.data 
      meta$seurat_clusters <- paste0("g",meta$seurat_clusters)
      
      cellchat <- createCellChat(object = data.input, meta = meta, group.by = "seurat_clusters")
      cellchat <- addMeta(cellchat, meta = meta)
      cellchat <- setIdent(cellchat, ident.use = "seurat_clusters")
      
      CellChatDB.use <- subsetDB(CellChatDB.human, search = "Secreted Signaling")
      cellchat@DB <- CellChatDB.use
      
      cellchat <- subsetData(cellchat)
      cellchat <- identifyOverExpressedGenes(cellchat)
      cellchat <- identifyOverExpressedInteractions(cellchat)
      cellchat <- smoothData(cellchat, adj = PPI.human)
      cellchat <- computeCommunProb(cellchat, raw.use = TRUE)
      cellchat <- filterCommunication(cellchat, min.cells = 10)
      cellchat <- computeCommunProbPathway(cellchat)
      cellchat <- aggregateNet(cellchat)
    })
  )
}

三、性能测试结果

我们整理了不同细胞数下的运行时长(分钟)与峰值内存使用(MB),如下表所示:

计算耗时 vs 细胞数

# 复制编辑
ggplot(sum, aes(x = Category , y = time, fill= Category)) + 
  geom_bar(stat = "identity") +
  labs(x = "细胞数", y = "耗时 (分钟)", title = "不同细胞量 CellChat 耗时比较") +
  theme_minimal()

峰值内存 vs 细胞数

# 复制编辑
ggplot(sum, aes(x = Category, y = peak_mem, fill= Category)) + 
  geom_bar(stat = "identity") +
  labs(x = "细胞数", y = "峰值内存 (MB)", title = "不同细胞量 CellChat 内存消耗") +
  theme_minimal()

从图中可以清晰看出:随着细胞数量的增加,CellChat 的计算耗时和内存占用呈显著上升趋势。其中30万细胞的数据集在普通电脑上运行会占用接近60GB的内存,分析时间接近50分钟。


稿定设计-4.jpg

四、建议与总结

本次测试表明,CellChat 在大数据量下具有显著的资源消耗。因此对于细胞数量超过10万的样本,强烈建议在配置较高的服务器环境中运行分析流程,以防内存溢出或运行失败。

如果你是初学者或设备资源有限,可以优先尝试下采样后的小规模数据,快速了解 CellChat 的分析逻辑与可视化结果,之后再扩展到全量数据。