Loading [MathJax]/jax/input/TeX/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >R tips:解决monocle的plot_genes_branched_heatmap函数报错

R tips:解决monocle的plot_genes_branched_heatmap函数报错

作者头像
生信菜鸟团
发布于 2025-04-13 07:56:00
发布于 2025-04-13 07:56:00
900
举报
文章被收录于专栏:生信菜鸟团生信菜鸟团

最近在使用monocle的plotgenesbranched_heatmap函数进行BEAM分支热图绘制时发生了一个报错,如下:

代码语言:txt
AI代码解释
复制
p <-  
  cds[BEAM_genes,] %>% 
  plot_genes_branched_heatmap( 
    branch_point = 1,  
    num_clusters = 4, # 3,  
    show_rownames = F,  
    return_heatmap = T 
 ) 
 
<simpleError in checkwz(wz, M = M, trace = trace, wzepsilon = control$wzepsilon): NAs found in the working weights variable 'wz'> 
Error in if (x@family@vfamily %in% c("negbinomial", "negbinomial.size")) { :  
  the condition has length > 1 

老办法,先使用traceback看一下报错的调用栈:

代码语言:txt
AI代码解释
复制
traceback() 
 
11: FUN(X[[i]], ...) 
10: lapply(X = X, FUN = FUN, ...) 
9: mclapply(models, function(x) { 
 if (is.null(x)) { 
           NA 
 } 
 else { 
 if (x@family@vfamily %in% c("negbinomial", "negbinomial.size")) { 
               predict(x, newdata = newdata, type = response_type) 
 } 
 else if (x@family@vfamily %in% c("uninormal")) { 
               predict(x, newdata = newdata, type = response_type) 
 } 
 else { 
 10^predict(x, newdata = newdata, type = response_type) 
 } 
 } 
 }, mc.cores = cores) 
8: responseMatrix(list(model_fits), new_data, response_type = response_type) 
7: as.data.frame(responseMatrix(list(model_fits), new_data, response_type = response_type)) 
6: FUN(newX[, i], ...) 
5: apply(exprs(X), MARGIN, FUN, ...) 
4: smartEsApply(cds, 1, function(x, trend_formula, expressionFamily,  
       relative_expr, new_data) { 
       environment(fit_model_helper) <- environment() 
       environment(responseMatrix) <- environment() 
       model_fits <- fit_model_helper(x, modelFormulaStr = trend_formula,  
           expressionFamily = expressionFamily, relative_expr = relative_expr,  
           disp_func = cds@dispFitInfo[["blind"]]$disp_func) 
 if (is.null(model_fits))  
           expression_curve <- as.data.frame(matrix(rep(NA, nrow(new_data)),  
               nrow = 1)) 
 else expression_curve <- as.data.frame(responseMatrix(list(model_fits),  
           new_data, response_type = response_type)) 
       colnames(expression_curve) <- row.names(new_data) 
       expression_curve 
 }, convert_to_dense = TRUE, trend_formula = trend_formula, expressionFamily = expressionFamily,  
       relative_expr = relative_expr, new_data = new_data) 
3: genSmoothCurves(new_cds[, ], cores = cores, trend_formula = trend_formula,  
       relative_expr = T, new_data = rbind(newdataA, newdataB)) 
2: plot_genes_branched_heatmap(., branch_point = 1, num_clusters = 4,  
       show_rownames = F, return_heatmap = T) 
1: cds[BEAM_genes, ] %>% plot_genes_branched_heatmap(branch_point = 1,  
       num_clusters = 4, show_rownames = F, return_heatmap = T) 

梳理调用栈,并结合各节点的函数源码可以发现报错传递是:

代码语言:txt
AI代码解释
复制
plot_genes_branched_heatmap -> genSmoothCurves -> responseMatrix 

先看一下responseMatrix的源码,如下所示,可以发现报错点的if语句那里是有问题的。因为结合最开始的报错提示表明x@family@vfamily的长度可能不为1。

代码语言:txt
AI代码解释
复制
monocle:::responseMatrix 
function (models, newdata = NULL, response_type = "response",  
    cores = 1)  
{ 
    res_list <- mclapply(models, function(x) { 
 if (is.null(x)) { 
            NA 
 } 
 else { 
 ### 报错点 ### 
 if (x@family@vfamily %in% c("negbinomial", "negbinomial.size")) { 
                predict(x, newdata = newdata, type = response_type) 
 } 
 else if (x@family@vfamily %in% c("uninormal")) { 
                predict(x, newdata = newdata, type = response_type) 
 } 
 else { 
 10^predict(x, newdata = newdata, type = response_type) 
 } 
 } 
 }, mc.cores = cores) 
    res_list_lengths <- lapply(res_list[is.na(res_list) == FALSE],  
        length) 
    stopifnot(length(unique(res_list_lengths)) == 1) 
    num_na_fits <- length(res_list[is.na(res_list)]) 
 if (num_na_fits > 0) { 
        na_matrix <- matrix(rep(rep(NA, res_list_lengths[[1]]),  
            num_na_fits), nrow = num_na_fits) 
        row.names(na_matrix) <- names(res_list[is.na(res_list)]) 
        non_na_matrix <- Matrix::t(do.call(cbind, lapply(res_list[is.na(res_list) ==  
            FALSE], unlist))) 
        row.names(non_na_matrix) <- names(res_list[is.na(res_list) ==  
            FALSE]) 
        res_matrix <- rbind(non_na_matrix, na_matrix) 
        res_matrix <- res_matrix[names(res_list), ] 
 } 
 else { 
        res_matrix <- Matrix::t(do.call(cbind, lapply(res_list,  
            unlist))) 
        row.names(res_matrix) <- names(res_list[is.na(res_list) ==  
            FALSE]) 
 } 
    res_matrix 
} 
可以再看一下genSmoothCurves函数源码:

genSmoothCurves 
function (cds, new_data, trend_formula = "~sm.ns(Pseudotime, df = 3)",  
    relative_expr = T, response_type = "response", cores = 1)  
{ 
    expressionFamily <- cds@expressionFamily 
 if (cores > 1) { 
        expression_curve_matrix <- mcesApply(cds, 1, function(x,  
            trend_formula, expressionFamily, relative_expr, new_data,  
            fit_model_helper, responseMatrix, calculate_NB_dispersion_hint,  
            calculate_QP_dispersion_hint) { 
            environment(fit_model_helper) <- environment() 
            environment(responseMatrix) <- environment() 
            model_fits <- fit_model_helper(x, modelFormulaStr = trend_formula,  
                expressionFamily = expressionFamily, relative_expr = relative_expr,  
                disp_func = cds@dispFitInfo[["blind"]]$disp_func) 
 if (is.null(model_fits))  
                expression_curve <- as.data.frame(matrix(rep(NA,  
                  nrow(new_data)), nrow = 1)) 
 else expression_curve <- as.data.frame(responseMatrix(list(model_fits),  
                newdata = new_data, response_type = response_type)) 
            colnames(expression_curve) <- row.names(new_data) 
            expression_curve 
 }, required_packages = c("BiocGenerics", "Biobase", "VGAM",  
 "plyr"), cores = cores, trend_formula = trend_formula,  
            expressionFamily = expressionFamily, relative_expr = relative_expr,  
            new_data = new_data, fit_model_helper = fit_model_helper,  
            responseMatrix = responseMatrix, calculate_NB_dispersion_hint = calculate_NB_dispersion_hint,  
            calculate_QP_dispersion_hint = calculate_QP_dispersion_hint) 
        expression_curve_matrix <- as.matrix(do.call(rbind, expression_curve_matrix)) 
 return(expression_curve_matrix) 
 } 
 else { 
        expression_curve_matrix <- smartEsApply(cds, 1, function(x,  
            trend_formula, expressionFamily, relative_expr, new_data) { 
            environment(fit_model_helper) <- environment() 
            environment(responseMatrix) <- environment() 
            model_fits <- fit_model_helper(x, modelFormulaStr = trend_formula,  
                expressionFamily = expressionFamily, relative_expr = relative_expr,  
                disp_func = cds@dispFitInfo[["blind"]]$disp_func) 
 
 if (is.null(model_fits))  
                expression_curve <- as.data.frame(matrix(rep(NA,  
                  nrow(new_data)), nrow = 1)) 
 ##### 下面的responseMatrix是traceback提示的报错点 ##### 
 else expression_curve <- as.data.frame(responseMatrix(list(model_fits),  
                new_data, response_type = response_type)) 
            colnames(expression_curve) <- row.names(new_data) 
            expression_curve 
 }, convert_to_dense = TRUE, trend_formula = trend_formula,  
            expressionFamily = expressionFamily, relative_expr = relative_expr,  
            new_data = new_data) 
        expression_curve_matrix <- as.matrix(do.call(rbind, expression_curve_matrix)) 
        row.names(expression_curve_matrix) <- row.names(fData(cds)) 
 return(expression_curve_matrix) 
 } 
} 
如果debug到genSmoothCurves函数,手动走一下fitmodelhelper,如下所示,我们直接让smartEsApply返回fitmodelhelper函数拟合的model,并保存到modelfitsls2,可以发现确实有些基因的model的vfamily有两个:

model_fits_ls2 <- smartEsApply(cds, 1, function(x,  
            trend_formula, expressionFamily, relative_expr, new_data) { 
            environment(fit_model_helper) <- environment() 
            environment(responseMatrix) <- environment() 
 # 下面的fit_model_helper是traceback提示的报错点 
            model_fits <- fit_model_helper(x, modelFormulaStr = trend_formula,  
                expressionFamily = expressionFamily, relative_expr = relative_expr,  
                disp_func = cds@dispFitInfo[["blind"]]$disp_func) 
 
            model_fits 
 
 }, convert_to_dense = TRUE, trend_formula = trend_formula,  
            expressionFamily = expressionFamily, relative_expr = relative_expr,  
            new_data = new_data) 
 
 
# 发现部分model的vfamily有两个 
m <- model_fits_ls2$EVX2 
m@family@vfamily  
[1] "negbinomial" "VGAMcategorical" 
既然知道了报错所在,那就可以解决它,把monocle的源码下载了,重新安装:

# 下载monocle的bioconductor的包 
https://bioconductor.org/packages/release/bioc/html/monocle.html 
 
wget https://bioconductor.org/packages/release/bioc/src/contrib/monocle_2.34.0.tar.gz 
tar -xvzf monocle_2.34.0.tar.gz 
 
# 在源码里面找到responseMatrix函数,可以看到responseMatrix函数的定义在expr_models.R里面 
grep responseMatrix monocle/R/* 
# R/clustering.R:#' expression_curve_matrix <- responseMatrix(full_model_fits) 
# R/expr_models.R:responseMatrix <- function(models, newdata = NULL, response_type="response", cores = 1) { 
# R/expr_models.R:#' the corresponding response matrix. This function is build on other functions (fit_models and responseMatrix) and used in calILRs and calABCs functions 
# R/expr_models.R:      expression_curve_matrix <- mcesApply(cds, 1, function(x, trend_formula, expressionFamily, relative_expr, new_data, fit_model_helper, responseMatrix, 
# R/expr_models.R:            environment(responseMatrix) <- environment() 
# R/expr_models.R:                expression_curve <- as.data.frame(responseMatrix(list(model_fits), newdata = new_data, response_type=response_type)) 
# R/expr_models.R:            fit_model_helper = fit_model_helper, responseMatrix = responseMatrix, calculate_NB_dispersion_hint = calculate_NB_dispersion_hint, 
# R/expr_models.R:            environment(responseMatrix) <- environment() 
# R/expr_models.R:                expression_curve <- as.data.frame(responseMatrix(list(model_fits), new_data, response_type=response_type)) 
# R/expr_models.R:      environment(responseMatrix) <- environment() 
# R/plotting.R:#' expression_curve_matrix <- responseMatrix(full_model_fits) 
 
# 修改编辑相应的文件: 
vi monocle/R/expr_models.R 
 
# 将下面代码 
if (x@family@vfamily %in% c("negbinomial", "negbinomial.size")) { 
# 改为下面代码即可 
if (any(x@family@vfamily %in% c("negbinomial", "negbinomial.size"))) { 
 
# 在R中使用devtools重新安装 
devtools::install('monocle') 
回到R中重新载入monocle包即可:

detach("package:monocle", unload = TRUE) 
library(monocle) 
 
# 不再报错 
p <-  
  cds[BEAM_genes,] %>% 
  plot_genes_branched_heatmap( 
    branch_point = 1,  
    num_clusters = 4, # 3,  
    show_rownames = F,  
    return_heatmap = T 
 ) 
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2025-04-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 生信菜鸟团 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
单细胞转录组3大R包之monocle2
主要是针对单细胞转录组测序数据开发的,用来找不同细胞类型或者不同细胞状态的差异表达基因。分析起始是表达矩阵,作者推荐用比较老旧的Tophat+Cufflinks流程,或者RSEM, eXpress,Sailfish,等等。需要的是基于转录本的表达矩阵,我一般用subjunc+featureCounts 来获取表达矩阵。 2014年版本 由Cole Trapnell 于2014年在Nature Biotechnology 杂志发表,是一个略微复杂的R包,并给出了一个测试数据 ,下载地址是: Source co
生信技能树
2018/03/09
21.1K2
单细胞转录组3大R包之monocle2
Monocle2 踩坑教程(2)
差异基因表达分析是RNA-Seq实验中的一项常见任务。Monocle可以帮助你找到不同细胞群间差异表达的基因,并评估这些变化的统计显著性。这些比较要求您有一种方法将细胞收集到两个或更多组中。这些组由每个CellDataSet的表现型数据表中的列定义。Monocle将评估不同细胞群中每个基因表达水平的显著性。
生信技能树jimmy
2020/03/30
2.7K0
🤩 Monocle 3 | 太牛了!单细胞必学R包!~(四)(差异分析之建模与评估)
比如,在示例数据中,细胞是在不同的时间点收集的,我们可以通过首先对每个基因拟合一个广义的线性模型来检验上述任何一个基因的表达是否随时间变化。🤩
生信漫卷
2023/11/16
1.4K1
🤩 Monocle 3 | 太牛了!单细胞必学R包!~(四)(差异分析之建模与评估)
单细胞分析1—monocle3分析概览
Monocle 3被重新设计用于分析大型、复杂的单细胞数据集,核心算法具有高度可扩展性,可以处理百万级别单细胞数据。Monocle 3增加了一些强大的新功能:
生信技能树jimmy
2022/03/14
2.9K0
单细胞分析1—monocle3分析概览
比较5种scRNA鉴定HVGs方法
2万个基因,近万个细胞的表达矩阵,降维起来是一个浩大的计算量,所以有一个步骤,就是从2万个基因里面挑选一下 highly variable genes (HVG) 进行下游分析,从此就假装自己的单细胞转录组就仅仅是测到了HVGs,数量嘛,两千多吧!
生信技能树jimmy
2020/03/30
1.6K0
马上都2023了,但是CNS级别单细胞文章仍然是使用monocle2
其实大家简单的搜索就能发现 trapnell 实验室虽然出了 monocle3 ,而且写的很清楚:Monocle 2 and Monocle 3 alpha are deprecated, Please use our new package, Monocle 3 ,如下所示的链接 :
生信技能树
2022/12/16
3.3K0
马上都2023了,但是CNS级别单细胞文章仍然是使用monocle2
Monocel3简介以及安装
Monocel3是单细胞分析领域一个重要的R 包,它是之前 Monocel和 Monocel2的升级版本,之前的 Monocel2 主要用于单细胞拟时分析。而新版本的 Monocel3 在原有基础上,还可以进行聚类,单细胞亚型分类,细胞注释,拟时分析,基因表达等分析。包含了 Seurat+SingleR 的功能,可以说一个 R 包可以完成单细胞数据分析绝大部分的功能。使用同一个 R 包分析起来更加方便。
生信喵实验柴
2022/10/25
2.6K0
Monocel3简介以及安装
比较不同的对单细胞转录组数据寻找差异基因的方法
背景介绍 如果是bulk RNA-seq,那么现在最流行的就是DESeq2 和 edgeR啦,而且有很多经过了RT-qPCR 验证过的真实测序数据可以来评价不同的差异基因算法的表现。 对单细胞测序数据来说,通常需要先聚类之后把细胞群体进行分组,然后来比较不同的组的差异表达情况。当然,也有不少单细胞测序实验设计本身就有时间点,不同个体来源,不同培养条件这样的分组! 同时还有不少方法是不需要预先分类的,因为分类本身就会引入偏差。 跟bulk RNA-seq不一样的地方是,scRNA-seq通常涉及到的样本数量更
生信技能树
2018/03/09
9K0
比较不同的对单细胞转录组数据寻找差异基因的方法
R tips:monocle安装调试
如果使用monocle(非monocle3)进行轨迹分析的话,由于这个包比较古老了,年久失修,所以monocle的函数则大概会报一个错误“Error: the condition has length > 1”。 本文会叙述一下修复此bug的过程。
生信菜鸟团
2024/04/18
3210
R tips:monocle安装调试
差异分析及可视化
在分群结果可以看到,CD8+主要分成了两群,一个是红色的(170个CD8+ cytotoxic T cells,即细胞毒性T细胞),一个是浅蓝色的(429个CD8+ effector T cells,即效应T细胞)
生信技能树jimmy
2020/03/30
1.4K0
Monocle2 踩坑教程(1)
拟时(pseudotime)分析,又称细胞轨迹(cell trajectory)分析,通过拟时分析可以推断出发育过程细胞的分化轨迹或细胞亚型的演化过程,在发育相关研究中使用频率较高。主要基于关键基因的表达模式,在拟时间中对单个细胞进行排序,模拟出时间发育过程的动态变化。
生信技能树jimmy
2020/03/30
8.1K0
🤩 Monocle 3 | 太牛了!单细胞必学R包!~(六)(寻找随伪时变化的基因)
这次,我们用一下啊graph_test()函数,设置neighbor_graph="principal_graph"测试轨迹上相似位置的细胞是否具有相关的表达。🤒
生信漫卷
2023/12/19
1.4K1
🤩 Monocle 3 | 太牛了!单细胞必学R包!~(六)(寻找随伪时变化的基因)
单细胞 一文打通 拟时序分析monocle2
########################################################
生信菜鸟团
2023/09/09
1.1K0
单细胞 一文打通 拟时序分析monocle2
scRNA包学习Monocle2
第三单元第七讲:使用scRNA包学习Monocle2 课程链接在:http://jm.grazy.cn/index/mulitcourse/detail.html?cid=53 关于monocle2
生信技能树jimmy
2020/03/30
3.8K0
monocle2轨迹分析
所以要从bdata获得pd,adata.var作为fd,adata.X作为count信息
生信探索
2023/04/03
9070
使用monocle2分析文章数据
第三单元第十一讲:使用monocle2分析文章数据 课程链接在:http://jm.grazy.cn/index/mulitcourse/detail.html?cid=53 载入数据,创建对象 r
生信技能树jimmy
2020/03/30
1.7K0
🤩 Monocle 3 | 太牛了!单细胞必学R包!~(七)(分析单细胞轨迹中的分支)
然后调用graph_test()子集,识别出具有制定表达模式的基因,这些基因只落在你选择的轨迹区域内哦。😲
生信漫卷
2023/12/19
8700
🤩 Monocle 3 | 太牛了!单细胞必学R包!~(七)(分析单细胞轨迹中的分支)
monocle3轨迹分析
https://mp.weixin.qq.com/s/UsDC-t1j7NHaLTnI6xCATQ
生信探索
2023/04/27
1.8K0
单细胞转录组基础分析六:伪时间分析
单细胞测序技术是近年最大的生命科学突破之一,相关文章频繁发表于各大顶级期刊,然而单细胞数据的分析依然是大家普遍面临的障碍。本专题将针对10X Genomics单细胞转录组数据演示各种主流分析,包括基于Seurat的基础分析、以及基于clusterProfiler、Monocle、SingleR等R包的延伸分析。不足之处请大家批评指正,欢迎添加Kinesin微信交流探讨!
生信技能树jimmy
2020/09/04
14.4K0
单细胞转录组基础分析六:伪时间分析
monocle3轨迹分析
https://mp.weixin.qq.com/s/UsDC-t1j7NHaLTnI6xCATQ
生信探索
2023/04/23
8010
相关推荐
单细胞转录组3大R包之monocle2
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档