r_xiaobai666 2025-09-19 22:42 采纳率: 39.3%
浏览 10
已结题

R语言单细胞 细胞通讯报错

R语言单细胞 细胞通讯报错

for(i in names(scRNA.list)){
    
    
    data.input <- GetAssayData(scRNA.list[[i]], layer = "counts")
    meta <- scRNA.list[[i]]@meta.data[,c("group","celltype")]
    
    # 建议提前对celltype进行排序~ 
    identical(rownames(meta),colnames(data.input))
    celltype_order <- c("Aerocyte",
                        "Alveolar macrophage",                                     
                        "AT2",
                        "B cell",
                        "Capillary EC",
                        "CD16 monocyte",
                        "CD16 NK cell",
                        "CD4 T cell",
                        "CD8 Tcell",
                        "cDC1",
                        "cDC2",
                        "CFD+MGP+fibroblast",
                        "Cycling T cell",
                        "Cycling T/NK cell",
                        "G2/M phase myeloid cell",
                        "Goblet cell",
                        "GZMB CD8 T cell",
                        "GZMK CD8 T cell", 
                        "M1 macrophage",
                        "Macrophage",
                        "Mast cell",
                        "Memory CD4 T cell", 
                        "Monocyte",  
                        "Multiciliated cell",
                        "NK cell",
                        "Pericyte",
                        "Plasma cell",  
                        "Treg cell")
    
    meta$celltype <- factor(meta$celltype ,levels = celltype_order)
    # 根据 meta$celltypes 的顺序进行排序
    ordered_indices <- order(meta$celltype)
    
    # 对 meta 和 data.input 进行排序
    meta <- meta[ordered_indices, ]
    data.input <- data.input[, ordered_indices]
    identical(rownames(meta),colnames(data.input))
    
    
    # 构建cellchat
    cellchat <- createCellChat(object = data.input, 
                               meta = meta, 
                               group.by = "celltype")
    
    cellchat@idents <- factor(cellchat@idents, levels = celltype_order)
    levels(cellchat@idents)
  
  
    #设置配体-受体相互作用数据库
    
    CellChatDB <- CellChatDB.human # use CellChatDB.mouse if running on mouse data
    
    showDatabaseCategory(CellChatDB)
    dplyr::glimpse(CellChatDB$interaction)
    
    # 使用CellChatDB的中特定的数据库进行细胞-细胞通信分析
    # 示例中使用了Secreted Signaling
    # CellChatDB.use <- subsetDB(CellChatDB, search = "Secreted Signaling", key = "annotation") 
    
    # Only uses the Secreted Signaling from CellChatDB v1
    #  CellChatDB.use <- subsetDB(CellChatDB, search = list(c("Secreted Signaling"), c("CellChatDB v1")), key = c("annotation", "version"))
    
    # 使用所有CellChatDB数据进行细胞-细胞通信分析。
    CellChatDB.use <- CellChatDB 
    
    # 在构建的cellchat中设定需要使用的数据库
    cellchat@DB <- CellChatDB.use
    
    ######预处理细胞-细胞通讯分析的表达数据
    
    cellchat <- subsetData(cellchat) 
    # future::plan("multisession", workers = 1) # do parallel
    cellchat <- identifyOverExpressedGenes(cellchat)
    cellchat <- identifyOverExpressedInteractions(cellchat)
    # 默认情况下,cellchat使用object@data.signaling进行网络推断
    # 此外提供了projectData函数,将基因投射到PPI,开发者说PPI并不会或导致很少的伪通讯
    cellchat <- projectData(cellchat, PPI.human)
    
    #####预测细胞-细胞通信网络
    # 该分析的关键参数是类型,即计算每个细胞组的平均基因表达的方法。
    # 默认情况下,type = “triMean”,产生较少但更强的交互。
    # 当设置 type = “truncatedMean” 时,应对trim设置一个值,进而产生更多交互。
    meta$labels = droplevels(meta$labels, exclude = setdiff(levels(meta$labels),unique(meta$labels)))
    cellchat <- computeCommunProb(cellchat, type = "triMean",raw.use = TRUE) 
    # 如下
    # computeAveExpr(cellchat, features = c("CXCL12","CXCR4"),
    #                type =  "truncatedMean",trim = 0.1)
    
    # min.cells是设置阈值,最小是需要10个细胞参与通讯推断(可以自定义)
    cellchat <- filterCommunication(cellchat, min.cells = 10)
    
    #####在信号通路水平上推断细胞间通讯
    
    # CellChat通过汇总与每个信号通路相关的所有配体-受体相互作用的通信概率来计算信号通路水平上的通信概率。 
    # NB:推断的每个配体-受体对的细胞间通信网络和每个信号通路分别存储在槽'net''netP'中。
    cellchat <- computeCommunProbPathway(cellchat)
    
    # 计算聚合细胞-细胞通信网络
    # 互作网络整合,可以设置soure和target,不设置就是默认全部
    cellchat <- aggregateNet(cellchat)
    
    #数据提取,subsetCommunication函数
    df.net <- subsetCommunication(cellchat)
    
    saveRDS(cellchat,paste0(i,"_Cellchat.rds"))
  }

img

  • 写回答

5条回答 默认 最新

  • 宝码香车 优质创作者: JavaScript技术领域 2025-09-19 22:44
    关注

    让【宝码香车】来帮您解答,本回答参考DeepSeek编写,并整理提供。

    如果答案让您满意,请采纳、点赞、关注,非常感谢!
    .
    每日一句正能量,让它带给您好心情:把烦恼留在昨天,今天只负责快乐。
    .

    根据您提供的错误信息和代码,问题出现在droplevels函数的使用上。错误提示显示“没有适用于'NULL'目标对象的方法”,这表明您尝试对NULL值应用droplevels函数。

    问题分析

    在您的代码中,这一行存在问题:

    meta$labels = droplevels(meta$labels, exclude = setdiff(levels(meta$labels),unique(meta$labels)))
    

    问题可能的原因:

    1. meta$labels 列不存在(为NULL)
    2. meta$labels 不是因子类型

    解决方案

    方案1:检查并创建labels列

    在您的代码中,meta数据框只有"group"和"celltype"两列,缺少"labels"列:

    # 修改这部分代码
    meta <- scRNA.list[[i]]@meta.data[,c("group","celltype")]
    
    
    # 添加labels列(如果需要的话)
    meta$labels <- meta$celltype  # 或者使用其他适当的列
    

    方案2:移除或修正droplevels调用

    如果您不需要labels列,可以直接移除这行代码:

    # 注释掉或删除这行
    # meta$labels = droplevels(meta$labels, exclude = setdiff(levels(meta$labels),unique(meta$labels)))
    

    或者确保labels列存在且是因子:

    # 确保labels列存在且是因子
    if (!"labels" %in% colnames(meta)) {
      meta$labels <- meta$celltype  # 或其他适当的默认值
    }
    if (!is.factor(meta$labels)) {
      meta$labels <- as.factor(meta$labels)
    }
    meta$labels <- droplevels(meta$labels)
    

    方案3:完整的修正代码

    for(i in names(scRNA.list)){
        
        data.input <- GetAssayData(scRNA.list[[i]], layer = "counts")
        meta <- scRNA.list[[i]]@meta.data[,c("group","celltype")]
        
        # 添加labels列
        meta$labels <- meta$celltype
        
        identical(rownames(meta),colnames(data.input))
        
        celltype_order <- c("Aerocyte", "Alveolar macrophage", "AT2", "B cell", 
                           "Capillary EC", "CD16 monocyte", "CD16 NK cell", 
                           "CD4 T cell", "CD8 Tcell", "cDC1", "cDC2", 
                           "CFD+MGP+fibroblast", "Cycling T cell", 
                           "Cycling T/NK cell", "G2/M phase myeloid cell", 
                           "Goblet cell", "GZMB CD8 T cell", "GZMK CD8 T cell", 
                           "M1 macrophage", "Macrophage", "Mast cell", 
                           "Memory CD4 T cell", "Monocyte", "Multiciliated cell", 
                           "NK cell", "Pericyte", "Plasma cell", "Treg cell")
        
        meta$celltype <- factor(meta$celltype, levels = celltype_order)
        meta$labels <- factor(meta$labels, levels = celltype_order)
        
        ordered_indices <- order(meta$celltype)
        meta <- meta[ordered_indices, ]
        data.input <- data.input[, ordered_indices]
        identical(rownames(meta),colnames(data.input))
        
        # 构建cellchat
        cellchat <- createCellChat(object = data.input, 
                                   meta = meta, 
                                   group.by = "celltype")
        
        cellchat@idents <- factor(cellchat@idents, levels = celltype_order)
        
        # 设置数据库
        CellChatDB <- CellChatDB.human
        CellChatDB.use <- CellChatDB 
        cellchat@DB <- CellChatDB.use
        
        # 预处理
        cellchat <- subsetData(cellchat) 
        cellchat <- identifyOverExpressedGenes(cellchat)
        cellchat <- identifyOverExpressedInteractions(cellchat)
        cellchat <- projectData(cellchat, PPI.human)
        
        # 确保labels是因子且去除非使用水平
        meta$labels <- droplevels(meta$labels)
        
        # 计算通信概率
        cellchat <- computeCommunProb(cellchat, type = "triMean", raw.use = TRUE) 
        cellchat <- filterCommunication(cellchat, min.cells = 10)
        cellchat <- computeCommunProbPathway(cellchat)
        cellchat <- aggregateNet(cellchat)
        
        df.net <- subsetCommunication(cellchat)
        saveRDS(cellchat, paste0(i, "_Cellchat.rds"))
    }
    

    建议

    1. 检查数据:确保您的单细胞数据对象包含所有需要的列
    2. 逐步调试:在循环外部先测试一个样本,确保代码正常运行
    3. 版本兼容性:检查CellChat包的版本,确保函数用法与当前版本匹配

    如果问题仍然存在,请提供更多关于您的数据结构和CellChat包版本的信息。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 9月27日
  • 已采纳回答 9月19日
  • 创建了问题 9月19日