关于monocle2在linux服务器运行报错问题解决方案分享
# 完整替换 plot_cell_trajectory new_plot_cell_trajectory - function(cds, x 1, y 2, color_by State, show_tree TRUE, show_backbone TRUE, backbone_color black, markers NULL, use_color_gradient FALSE, markers_linear FALSE, show_cell_names FALSE, show_state_number FALSE, cell_size 1.5, cell_link_size 0.75, cell_name_size 2, state_number_size 2.9, show_branch_points TRUE, theta 0, ...) { requireNamespace(igraph) gene_short_name - NA sample_name - NA sample_state - pData(cds)$State data_dim_1 - NA data_dim_2 - NA lib_info_with_pseudo - pData(cds) if (is.null(cdsdim_reduce_type)) { stop(Error: dimensionality not yet reduced.) } if (cdsdim_reduce_type ICA) { reduced_dim_coords - reducedDimS(cds) } else if (cdsdim_reduce_type %in% c(simplePPT, DDRTree)) { reduced_dim_coords - reducedDimK(cds) } else { stop(Error: unrecognized dimensionality reduction method.) } # 关键修复用 select 替代 select_ ica_space_df - Matrix::t(reduced_dim_coords) %% as.data.frame() %% dplyr::select(all_of(c(prin_graph_dim_1 x, prin_graph_dim_2 y))) %% dplyr::mutate(sample_name rownames(.), sample_state rownames(.)) dp_mst - minSpanningTree(cds) if (is.null(dp_mst)) { stop(You must first call orderCells() before using this function) } edge_df - dp_mst %% igraph::as_data_frame() %% dplyr::select(source from, target to) %% dplyr::left_join( ica_space_df %% dplyr::select( source sample_name, source_prin_graph_dim_1 prin_graph_dim_1, source_prin_graph_dim_2 prin_graph_dim_2), by source) %% dplyr::left_join( ica_space_df %% dplyr::select( target sample_name, target_prin_graph_dim_1 prin_graph_dim_1, target_prin_graph_dim_2 prin_graph_dim_2), by target) data_df - t(monocle::reducedDimS(cds)) %% as.data.frame() %% dplyr::select(all_of(c(data_dim_1 x, data_dim_2 y))) %% tibble::rownames_to_column(sample_name) %% dplyr::mutate(sample_state) %% dplyr::left_join( lib_info_with_pseudo %% tibble::rownames_to_column(sample_name), by sample_name) return_rotation_mat - function(theta) { theta - theta/180 * pi matrix(c(cos(theta), sin(theta), -sin(theta), cos(theta)), nrow 2) } rot_mat - return_rotation_mat(theta) cn1 - c(data_dim_1, data_dim_2) cn2 - c(source_prin_graph_dim_1, source_prin_graph_dim_2) cn3 - c(target_prin_graph_dim_1, target_prin_graph_dim_2) data_df[, cn1] - as.matrix(data_df[, cn1]) %*% t(rot_mat) edge_df[, cn2] - as.matrix(edge_df[, cn2]) %*% t(rot_mat) edge_df[, cn3] - as.matrix(edge_df[, cn3]) %*% t(rot_mat) markers_exprs - NULL if (!is.null(markers)) { markers_fData - subset(fData(cds), gene_short_name %in% markers) if (nrow(markers_fData) 1) { markers_exprs - reshape2::melt(as.matrix(exprs(cds[row.names(markers_fData), ]))) colnames(markers_exprs)[1:2] - c(feature_id, cell_id) markers_exprs - merge(markers_exprs, markers_fData, by.x feature_id, by.y row.names) markers_exprs$feature_label - as.character(markers_exprs$gene_short_name) markers_exprs$feature_label[is.na(markers_exprs$feature_label)] - markers_exprs$Var1 } } if (!is.null(markers_exprs) nrow(markers_exprs) 0) { data_df - merge(data_df, markers_exprs, by.x sample_name, by.y cell_id) if (use_color_gradient) { if (markers_linear) { g - ggplot(data data_df, aes(x data_dim_1, y data_dim_2)) geom_point(aes(color value), size I(cell_size), na.rm TRUE) scale_color_viridis(name paste0(value), ...) facet_wrap(~feature_label) } else { g - ggplot(data data_df, aes(x data_dim_1, y data_dim_2)) geom_point(aes(color log10(value 0.1)), size I(cell_size), na.rm TRUE) scale_color_viridis(name paste0(log10(value 0.1)), ...) facet_wrap(~feature_label) } } else { if (markers_linear) { g - ggplot(data data_df, aes(x data_dim_1, y data_dim_2, size (value * 0.1))) facet_wrap(~feature_label) } else { g - ggplot(data data_df, aes(x data_dim_1, y data_dim_2, size log10(value 0.1))) facet_wrap(~feature_label) } } } else { g - ggplot(data data_df, aes(x data_dim_1, y data_dim_2)) } if (show_tree) { g - g geom_segment(aes_string( x source_prin_graph_dim_1, y source_prin_graph_dim_2, xend target_prin_graph_dim_1, yend target_prin_graph_dim_2), size cell_link_size, linetype solid, na.rm TRUE, data edge_df) } if (!is.null(markers_exprs) nrow(markers_exprs) 0) { if (!use_color_gradient) { g - g geom_point(aes_string(color color_by), na.rm TRUE) } } else { if (!use_color_gradient) { g - g geom_point(aes_string(color color_by), size I(cell_size), na.rm TRUE) } } if (show_branch_points cdsdim_reduce_type DDRTree) { mst_branch_nodes - cdsauxOrderingData[[cdsdim_reduce_type]]$branch_points branch_point_df - ica_space_df %% dplyr::slice(match(mst_branch_nodes, sample_name)) %% dplyr::mutate(branch_point_idx seq_len(dplyr::n())) g - g geom_point(aes_string(x prin_graph_dim_1, y prin_graph_dim_2), size 5, na.rm TRUE, branch_point_df) geom_text(aes_string(x prin_graph_dim_1, y prin_graph_dim_2, label branch_point_idx), size 4, color white, na.rm TRUE, branch_point_df) } if (show_cell_names) g - g geom_text(aes(label sample_name), size cell_name_size) if (show_state_number) g - g geom_text(aes(label sample_state), size state_number_size) g - g monocle_theme_opts() xlab(paste(Component, x)) ylab(paste(Component, y)) theme(legend.position top, legend.key.height grid::unit(0.35, in)) theme(legend.key element_blank()) theme(panel.background element_rect(fill white)) g } # 写回 monocle 命名空间 assignInNamespace(plot_cell_trajectory, new_plot_cell_trajectory, ns monocle) cat(✓ plot_cell_trajectory completely replaced!\n\n) # 运行 plot_cell_trajectory(mycds, color_by Pseudotime) plot_cell_trajectory(mycds, color_by seurat_clusters) plot_cell_trajectory(mycds, color_by State) cat(✓ Done!\n)先说我遇到的报错是什么monocle2在服务器中经常出现版本不兼容的报错问题最常见的如下mycds-estimateDispersions(mycds,cores8,relative_expr TRUE) ##使用monocle选择高变基因 Error: ! group_by_() was deprecated in dplyr 0.7.0 and is now defunct. ℹ Please use group_by() instead. ℹ See vignette(programming) for more help Run rlang::last_trace() to see where the error occurred.但是我选择降低dplyr版本时却发现服务器和低版本的dplyr不兼容make: *** [/root/autodl-tmp/home/joey/micromamba/envs/r433/lib/R/etc/Makeconf:200: RcppExports.o] Error 1 ERROR: compilation failed for package ‘dplyr’ removing ‘/root/autodl-tmp/home/joey/micromamba/envs/r433/lib/R/library/dplyr’ restoring previous ‘/root/autodl-tmp/home/joey/micromamba/envs/r433/lib/R/library/dplyr’ Warning message: In install.packages(dplyr_0.7.0.tar.gz , repos NULL, type source) : installation of package ‘dplyr_0.7.0.tar.gz ’ had non-zero exit status于是通过ai对于这一步的报错解决方案是# 直接给 dplyr 命名空间注入缺失的函数 assignInNamespace(group_by_, function(.data, ..., .dots list()) { dots - lapply(.dots, function(x) if (is.character(x)) rlang::sym(x) else x) dplyr::group_by(.data, !!!dots) }, ns dplyr) assignInNamespace(mutate_, function(.data, ..., .dots list()) { dplyr::mutate(.data, !!!.dots) }, ns dplyr) cat(✓ Patched\n) # 运行 mycds - estimateDispersions(mycds, cores 8, relative_expr TRUE)运行这一步后依然会报错报错如下Error: ! select_() was deprecated in dplyr 0.7.0 and is now defunct. ℹ Please use select() instead. Run rlang::last_trace() to see where the error occurred.解决方案# 完整修补所有 dplyr 废弃函数 patch_dplyr_for_monocle - function() { # 1. group_by_ assignInNamespace(group_by_, function(.data, ..., .dots list()) { dots - lapply(.dots, function(x) { if (is.character(x)) rlang::sym(x) else x }) dplyr::group_by(.data, !!!dots) }, ns dplyr) # 2. mutate_ assignInNamespace(mutate_, function(.data, ..., .dots list()) { dplyr::mutate(.data, !!!.dots) }, ns dplyr) # 3. select_ assignInNamespace(select_, function(.data, ..., .dots list()) { dots - c(list(...), .dots) dots - lapply(dots, function(x) { if (is.character(x)) rlang::sym(x) else x }) dplyr::select(.data, !!!dots) }, ns dplyr) # 4. filter_ assignInNamespace(filter_, function(.data, ..., .dots list()) { dots - c(list(...), .dots) dplyr::filter(.data, !!!dots) }, ns dplyr) # 5. arrange_ assignInNamespace(arrange_, function(.data, ..., .dots list()) { dots - c(list(...), .dots) dots - lapply(dots, function(x) { if (is.character(x)) rlang::sym(x) else x }) dplyr::arrange(.data, !!!dots) }, ns dplyr) # 6. summarise_ / summarize_ assignInNamespace(summarise_, function(.data, ..., .dots list()) { dplyr::summarise(.data, !!!.dots) }, ns dplyr) assignInNamespace(summarize_, function(.data, ..., .dots list()) { dplyr::summarize(.data, !!!.dots) }, ns dplyr) # 7. rename_ assignInNamespace(rename_, function(.data, ..., .dots list()) { dots - c(list(...), .dots) dots - lapply(dots, function(x) { if (is.character(x)) rlang::sym(x) else x }) dplyr::rename(.data, !!!dots) }, ns dplyr) # 8. slice_ assignInNamespace(slice_, function(.data, ..., .dots list()) { dots - c(list(...), .dots) dplyr::slice(.data, !!!dots) }, ns dplyr) # 9. transmute_ assignInNamespace(transmute_, function(.data, ..., .dots list()) { dplyr::transmute(.data, !!!.dots) }, ns dplyr) # 10. distinct_ assignInNamespace(distinct_, function(.data, ..., .dots list(), .keep_all FALSE) { dots - c(list(...), .dots) dots - lapply(dots, function(x) { if (is.character(x)) rlang::sym(x) else x }) dplyr::distinct(.data, !!!dots, .keep_all .keep_all) }, ns dplyr) # 11. count_ tryCatch({ assignInNamespace(count_, function(x, ..., vars, sort FALSE, wt NULL) { vars_syms - lapply(vars, rlang::sym) dplyr::count(x, !!!vars_syms, sort sort) }, ns dplyr) }, error function(e) NULL) # 12. tally_ tryCatch({ assignInNamespace(tally_, function(x, wt, sort FALSE) { dplyr::tally(x, sort sort) }, ns dplyr) }, error function(e) NULL) cat(✓ All deprecated dplyr functions patched for Monocle2!\n) } # 执行修补 patch_dplyr_for_monocle() # 现在运行 Monocle2 mycds - estimateDispersions(mycds, cores 8, relative_expr TRUE)打完补丁之后这个提取高变基因就可以顺利运行之后最要命的是可视化过程的报错plot_cell_trajectory(mycds, color_by Pseudotime) plot_cell_trajectory(mycds, color_by seurat_clusters) plot_cell_trajectory(mycds, color_by Region) Error: ! nei() was deprecated in igraph 2.1.0 and is now defunct. ℹ Please use .nei() instead. Run rlang::last_trace() to see where the error occurred. mycds - reduceDimension(mycds, max_components 2, method DDRTree) mycds - orderCells(mycds) plot_cell_trajectory(mycds, color_by Pseudotime) plot_cell_trajectory(mycds, color_by seurat_clusters) plot_cell_trajectory(mycds, color_by Region) cat(\n✓ Monocle2 trajectory analysis complete!\n) Batch Patching Monocle2 ✓ Patched: assign_cell_lineage ✓ Patched: buildBranchCellDataSet ✓ Patched: count_leaf_descendents ✓ Patched: cth_classifier_cds ✓ Patched: cth_classifier_cell ✓ Patched: extract_ddrtree_ordering ✓ Patched: extract_good_branched_ordering ✓ Patched: extract_good_ordering ✓ Patched: make_canonical ✓ Patched: measure_diameter_path ✓ Patched: project2MST ✓ Total patched: 11 / 11 Running Monocle2 Error in graph.dfs(dp_mst, root root_cell, mode all, unreachable FALSE, : argument neimode is missing, with no default上面两个参数我都通过手动修改参数的方式顺利进行了以下两步mycds - reduceDimension(mycds, max_components 2, method DDRTree)mycds - orderCells(mycds)解决方案trace(project2MST, where asNamespace(monocle), edit TRUE)找到报错参数所在位置先点击i键启动编辑之后点击esc键退出编辑界面最后输入:wq!就可以保存参数设置。这个报错解决后依然在下面三步会反复报错plot_cell_trajectory(mycds, color_by Pseudotime) plot_cell_trajectory(mycds, color_by seurat_clusters) plot_cell_trajectory(mycds, color_by Region) 始终会出现以下报错 ##Pseudotime轨迹可视化 plot_cell_trajectory(mycds, color_byPseudotime) plot_cell_trajectory(mycds, color_byseurat_clusters) plot_cell_trajectory(mycds, color_byState) Error: ! select_() was deprecated in dplyr 0.7.0 and is now defunct. ℹ Please use select() instead. Run rlang::last_trace() to see where the error occurred. rlang::last_trace() error/lifecycle_error_deprecated Error: ! select_() was deprecated in dplyr 0.7.0 and is now defunct. ℹ Please use select() instead. --- Backtrace: ▆ 1. ├─monocle::plot_cell_trajectory(mycds, color_by Pseudotime) 2. │ └─... %% ... 3. ├─dplyr::mutate(., sample_name rownames(.), sample_state rownames(.)) 4. └─dplyr::select_(., prin_graph_dim_1 x, prin_graph_dim_2 y) 5. └─dplyr:::lazy_defunct(select, hint FALSE) 6. └─lifecycle::deprecate_stop(...) 7. └─lifecycle:::deprecate_stop0(msg) Run rlang::last_trace(drop FALSE) to see 1 hidden frame.我发现这个时候通过手动修改参数已经无法解决问题了于是我通过ai新建了一个绘图函数结果跑通了# 完整替换 plot_cell_trajectory new_plot_cell_trajectory - function(cds, x 1, y 2, color_by State, show_tree TRUE, show_backbone TRUE, backbone_color black, markers NULL, use_color_gradient FALSE, markers_linear FALSE, show_cell_names FALSE, show_state_number FALSE, cell_size 1.5, cell_link_size 0.75, cell_name_size 2, state_number_size 2.9, show_branch_points TRUE, theta 0, ...) { requireNamespace(igraph) gene_short_name - NA sample_name - NA sample_state - pData(cds)$State data_dim_1 - NA data_dim_2 - NA lib_info_with_pseudo - pData(cds) if (is.null(cdsdim_reduce_type)) { stop(Error: dimensionality not yet reduced.) } if (cdsdim_reduce_type ICA) { reduced_dim_coords - reducedDimS(cds) } else if (cdsdim_reduce_type %in% c(simplePPT, DDRTree)) { reduced_dim_coords - reducedDimK(cds) } else { stop(Error: unrecognized dimensionality reduction method.) } # 关键修复用 select 替代 select_ ica_space_df - Matrix::t(reduced_dim_coords) %% as.data.frame() %% dplyr::select(all_of(c(prin_graph_dim_1 x, prin_graph_dim_2 y))) %% dplyr::mutate(sample_name rownames(.), sample_state rownames(.)) dp_mst - minSpanningTree(cds) if (is.null(dp_mst)) { stop(You must first call orderCells() before using this function) } edge_df - dp_mst %% igraph::as_data_frame() %% dplyr::select(source from, target to) %% dplyr::left_join( ica_space_df %% dplyr::select( source sample_name, source_prin_graph_dim_1 prin_graph_dim_1, source_prin_graph_dim_2 prin_graph_dim_2), by source) %% dplyr::left_join( ica_space_df %% dplyr::select( target sample_name, target_prin_graph_dim_1 prin_graph_dim_1, target_prin_graph_dim_2 prin_graph_dim_2), by target) data_df - t(monocle::reducedDimS(cds)) %% as.data.frame() %% dplyr::select(all_of(c(data_dim_1 x, data_dim_2 y))) %% tibble::rownames_to_column(sample_name) %% dplyr::mutate(sample_state) %% dplyr::left_join( lib_info_with_pseudo %% tibble::rownames_to_column(sample_name), by sample_name) return_rotation_mat - function(theta) { theta - theta/180 * pi matrix(c(cos(theta), sin(theta), -sin(theta), cos(theta)), nrow 2) } rot_mat - return_rotation_mat(theta) cn1 - c(data_dim_1, data_dim_2) cn2 - c(source_prin_graph_dim_1, source_prin_graph_dim_2) cn3 - c(target_prin_graph_dim_1, target_prin_graph_dim_2) data_df[, cn1] - as.matrix(data_df[, cn1]) %*% t(rot_mat) edge_df[, cn2] - as.matrix(edge_df[, cn2]) %*% t(rot_mat) edge_df[, cn3] - as.matrix(edge_df[, cn3]) %*% t(rot_mat) markers_exprs - NULL if (!is.null(markers)) { markers_fData - subset(fData(cds), gene_short_name %in% markers) if (nrow(markers_fData) 1) { markers_exprs - reshape2::melt(as.matrix(exprs(cds[row.names(markers_fData), ]))) colnames(markers_exprs)[1:2] - c(feature_id, cell_id) markers_exprs - merge(markers_exprs, markers_fData, by.x feature_id, by.y row.names) markers_exprs$feature_label - as.character(markers_exprs$gene_short_name) markers_exprs$feature_label[is.na(markers_exprs$feature_label)] - markers_exprs$Var1 } } if (!is.null(markers_exprs) nrow(markers_exprs) 0) { data_df - merge(data_df, markers_exprs, by.x sample_name, by.y cell_id) if (use_color_gradient) { if (markers_linear) { g - ggplot(data data_df, aes(x data_dim_1, y data_dim_2)) geom_point(aes(color value), size I(cell_size), na.rm TRUE) scale_color_viridis(name paste0(value), ...) facet_wrap(~feature_label) } else { g - ggplot(data data_df, aes(x data_dim_1, y data_dim_2)) geom_point(aes(color log10(value 0.1)), size I(cell_size), na.rm TRUE) scale_color_viridis(name paste0(log10(value 0.1)), ...) facet_wrap(~feature_label) } } else { if (markers_linear) { g - ggplot(data data_df, aes(x data_dim_1, y data_dim_2, size (value * 0.1))) facet_wrap(~feature_label) } else { g - ggplot(data data_df, aes(x data_dim_1, y data_dim_2, size log10(value 0.1))) facet_wrap(~feature_label) } } } else { g - ggplot(data data_df, aes(x data_dim_1, y data_dim_2)) } if (show_tree) { g - g geom_segment(aes_string( x source_prin_graph_dim_1, y source_prin_graph_dim_2, xend target_prin_graph_dim_1, yend target_prin_graph_dim_2), size cell_link_size, linetype solid, na.rm TRUE, data edge_df) } if (!is.null(markers_exprs) nrow(markers_exprs) 0) { if (!use_color_gradient) { g - g geom_point(aes_string(color color_by), na.rm TRUE) } } else { if (!use_color_gradient) { g - g geom_point(aes_string(color color_by), size I(cell_size), na.rm TRUE) } } if (show_branch_points cdsdim_reduce_type DDRTree) { mst_branch_nodes - cdsauxOrderingData[[cdsdim_reduce_type]]$branch_points branch_point_df - ica_space_df %% dplyr::slice(match(mst_branch_nodes, sample_name)) %% dplyr::mutate(branch_point_idx seq_len(dplyr::n())) g - g geom_point(aes_string(x prin_graph_dim_1, y prin_graph_dim_2), size 5, na.rm TRUE, branch_point_df) geom_text(aes_string(x prin_graph_dim_1, y prin_graph_dim_2, label branch_point_idx), size 4, color white, na.rm TRUE, branch_point_df) } if (show_cell_names) g - g geom_text(aes(label sample_name), size cell_name_size) if (show_state_number) g - g geom_text(aes(label sample_state), size state_number_size) g - g monocle_theme_opts() xlab(paste(Component, x)) ylab(paste(Component, y)) theme(legend.position top, legend.key.height grid::unit(0.35, in)) theme(legend.key element_blank()) theme(panel.background element_rect(fill white)) g } # 写回 monocle 命名空间 assignInNamespace(plot_cell_trajectory, new_plot_cell_trajectory, ns monocle) cat(✓ plot_cell_trajectory completely replaced!\n\n)以上函数建议先放在新建R文件中因为直接复制粘贴终端会截断保存到function.R文件后我通过运行source(function.R) ✓ plot_cell_trajectory completely replaced!这样就能导入自己新建的函数包然后 # 设置函数环境指向 monocle这样它能找到 monocle 内部函数 environment(new_plot_cell_trajectory) - asNamespace(monocle) new_plot_cell_trajectory(mycds, color_by Pseudotime) Warning message: The size argument of element_line() is deprecated as of ggplot2 3.4.0. ℹ Please use the linewidth argument instead. ℹ The deprecated feature was likely used in the monocle package. Please report the issue to the authors. This warning is displayed once per session. Call lifecycle::last_lifecycle_warnings() to see where this warning was generated. dev.off() null device 1到此位置我的报错终于解决啦啦啦啦啦啦啦啦啦啦这两个报错我光解决就花了两天作用现在初步解决报错特来分享心路历程感谢AI。图片引自Monocle