From 09684a706ae972e8cf48d2214779e8644f11a505 Mon Sep 17 00:00:00 2001 From: Lucie Pfeiferova Date: Thu, 1 Aug 2024 15:56:28 +0200 Subject: [PATCH] error correct --- R/manual_cell_annotation.R | 73 +++++++++++++++++-- R/plans_single_sample.R | 1 + R/single_sample_norm_clustering.R | 2 +- R/visualization.R | 2 +- inst/Rmd/single_sample/02_norm_clustering.Rmd | 11 +-- .../single_sample/01_input_qc.default.yaml | 2 +- .../02_norm_clustering.default.yaml | 7 +- 7 files changed, 79 insertions(+), 19 deletions(-) diff --git a/R/manual_cell_annotation.R b/R/manual_cell_annotation.R index 2bf2d2f..33e6724 100644 --- a/R/manual_cell_annotation.R +++ b/R/manual_cell_annotation.R @@ -165,6 +165,7 @@ meta_heatmap_ploting <- mid = "white", high = "red", spatial = FALSE, + dimred = dimred, make_cell_plot = FALSE, out_dir = NULL) { cell_metadata <- metadata(sce)[["clustering_enrichment"]] @@ -204,12 +205,14 @@ meta_heatmap_ploting <- metaDT <- metaDT %>% dplyr::group_by(variable) %>% dplyr::mutate(zscores_rescaled_per_gene = c(scales::rescale(zscores, to = c(-1, 1)))) - # print(head(metaDT)) + # Calculate means - testmain <- metaDT %>% - dplyr::group_by(variable, !!!rlang::syms(main_factor)) %>% - dplyr::summarise(mean_value = mean(value)) + main_factor <- clustering + testmain <- metaDT %>% + dplyr::group_by(variable, !!!rlang::syms(clustering)) %>% + dplyr::summarise(mean_value = mean(!!!rlang::syms(show_value))) + # Step 2: Define the dfunction dfunction <- function(d, col_name1, col_name2, value.var) { d %>% @@ -217,12 +220,13 @@ meta_heatmap_ploting <- } # Step 3: Apply dfunction to testmain - testmain_matrix <- dfunction(d = testmain, col_name1 = variable, col_name2 = main_factor, value.var = mean_value) + testmain_matrix <- dfunction(d = testmain, col_name1 = variable, col_name2 = clustering, value.var = mean_value) testmain_mat <- as.matrix(testmain_matrix[, -1]) + rownames(testmain_mat) <- testmain_matrix$variable # for clusters - ## this part is ridiculusely redundant...it is just sorting rows and column based on hierarchic clustering!!!! + # cormatrix <- stats::cor(x = testmain_mat, method = clus_cor_method) cordist <- stats::as.dist(1 - cormatrix, diag = T, upper = T) corclus <- stats::hclust(d = cordist, method = clus_cluster_method) @@ -252,7 +256,6 @@ meta_heatmap_ploting <- metaDT <- metaDT %>% dplyr::mutate(variable = as.character(variable)) # , levels = values_sort_names)) ## - # print(head(metaDT)) pl <- ggplot2::ggplot() pl <- pl + ggplot2::geom_tile( @@ -327,7 +330,61 @@ meta_heatmap_ploting <- anot_plot_out_pdf_file = out_pdf_file, anot_plot_out_png_file = out_png_file ) - + + p <- plotReducedDim_mod( + sce = sce, + dimred = dimred, + colour_by = glue::glue("manual_annotation_{clustering}"), + text_by = glue::glue("manual_annotation_{clustering}"), + title = glue::glue("manual_annotation_{clustering}_dimred.pdf"), + use_default_ggplot_palette = TRUE, + legend_title = "Cluster" + ) + + out_pdf_file <- + fs::path(out_dir, + glue::glue("manual_annotation_{clustering}_dimred.pdf")) + out_png_file <- out_pdf_file + fs::path_ext(out_png_file) <- "png" + pl <- tryCatch({ + scdrake::save_pdf(list(p), out_pdf_file, stop_on_error = TRUE) + ggplot2::ggsave( + filename = out_png_file, + plot = p, + device = "png", + dpi = 300 + ) + pl + }, + error = function(e) { + if (stringr::str_detect(e$message, "Viewport has zero dimension")) { + cli_alert_warning( + str_space( + "Error catched: 'Viewport has zero dimension(s)'.", + "There are probably too many levels and the legend doesn't fit into the plot.", + "Removing the legend before saving the plot image." + ) + ) + p <- p + theme(legend.position = "none") + scdrake::save_pdf(list(p), out_pdf_file) + ggplot2::ggsave( + filename = out_png_file, + plot = p, + device = "png", + dpi = 150 + ) + p + } else { + cli::cli_abort(e$message) + } + }) + dimred_par <- tibble::tibble( + title = as.character(glue::glue("manual_annotation_{clustering}_dimred.pdf")), + anot_plot = list(p), + anot_plot_out_pdf_file = out_pdf_file, + anot_plot_out_png_file = out_png_file + ) + par <- rbind(par, dimred_par) if (spatial) { man_anot_plot <- visualized_spots(sce, cell_color = glue::glue("manual_annotation_{clustering}"), diff --git a/R/plans_single_sample.R b/R/plans_single_sample.R index bc176f1..89aa296 100644 --- a/R/plans_single_sample.R +++ b/R/plans_single_sample.R @@ -420,6 +420,7 @@ get_norm_clustering_subplan <- function(cfg, cfg_pipeline, cfg_main) { show_value = !!cfg$SHOW_VALUE, out_dir = !!cfg$NORM_CLUSTERING_OTHER_PLOTS_OUT_DIR, spatial = !!cfg$SPATIAL, + dimred = !!cfg$HEATMAP_DIMRED, make_cell_plot = !!cfg$MAKE_CELL_PLOT ) ) diff --git a/R/single_sample_norm_clustering.R b/R/single_sample_norm_clustering.R index 5286b1f..f1f5992 100644 --- a/R/single_sample_norm_clustering.R +++ b/R/single_sample_norm_clustering.R @@ -331,7 +331,7 @@ sce_norm_hvg_fn <- function(sce_norm, nfeatures = hvg_selection_value ) svg_ids <- Seurat::SVFInfo(seu_sce_norm, selection.method = "moransi", ) - svg_ids <- rownames(result[result$moransi.spatially.variable == "TRUE", ]) + svg_ids <- rownames(svg_ids[svg_ids$moransi.spatially.variable == "TRUE", ]) hvg_ids <- unique(c(hvg_ids, svg_ids)) } diff --git a/R/visualization.R b/R/visualization.R index 2222563..357d949 100644 --- a/R/visualization.R +++ b/R/visualization.R @@ -346,7 +346,7 @@ dimred_plots_clustering_fn <- function(sce_dimred, palete <- c(scales::hue_pal()(par$n_clusters)) p_spat <- visualized_spots(sce_add_colData(sce_dimred, cell_data), cell_color = par$sce_column, color_as_factor = F, - point_shape = border, cell_color_code = palete, show_legend = F + point_shape = "border", cell_color_code = palete, show_legend = F ) p <- cowplot::plot_grid(p, p_spat, ncol = 2, nrow = 1, rel_widths = c(1, 1.5)) } diff --git a/inst/Rmd/single_sample/02_norm_clustering.Rmd b/inst/Rmd/single_sample/02_norm_clustering.Rmd index f673d0b..c1d7f50 100644 --- a/inst/Rmd/single_sample/02_norm_clustering.Rmd +++ b/inst/Rmd/single_sample/02_norm_clustering.Rmd @@ -374,15 +374,16 @@ if (isTRUE(cfg$MANUAL_ANNOTATION)) { } cat("\n\n") if (isTRUE(cfg$SPATIAL)) { -scdrake::create_a_link(href = drake::readd(plot_annotation)$anot_plot_out_pdf_file[2],text = "**Cell annotation plot**", href_rel_start = fs::path_dir(cfg$NORM_CLUSTERING_REPORT_HTML_FILE),do_cat = TRUE) +scdrake::create_a_link(href = drake::readd(plot_annotation)$anot_plot_out_pdf_file[3],text = "**Cell annotation plot**", href_rel_start = fs::path_dir(cfg$NORM_CLUSTERING_REPORT_HTML_FILE),do_cat = TRUE) cat("\n\n") - scdrake::create_a_link(href = drake::readd(plot_annotation)$anot_plot_out_pdf_file[3],text = "**Enrichment cells plot**", href_rel_start = fs::path_dir(cfg$NORM_CLUSTERING_REPORT_HTML_FILE),do_cat = TRUE) + scdrake::create_a_link(href = drake::readd(plot_annotation)$anot_plot_out_pdf_file[4],text = "**Enrichment cells plot**", href_rel_start = fs::path_dir(cfg$NORM_CLUSTERING_REPORT_HTML_FILE),do_cat = TRUE) cat("\n\n") - -scdrake::create_img_link(drake::readd(plot_annotation)$anot_plot_out_pdf_file[1],img_src = drake::readd(plot_annotation)$anot_plot_out_png_file[1],img_width = "500px",href_rel_start = fs::path_dir(cfg$NORM_CLUSTERING_REPORT_HTML_FILE)) +} +scdrake::create_img_link(drake::readd(plot_annotation)$anot_plot_out_pdf_file[1],img_src = drake::readd(plot_annotation)$anot_plot_out_png_file[1],img_width = "450px",href_rel_start = fs::path_dir(cfg$NORM_CLUSTERING_REPORT_HTML_FILE),do_cat = TRUE) -} +scdrake::create_img_link(drake::readd(plot_annotation)$anot_plot_out_pdf_file[2],img_src = drake::readd(plot_annotation)$anot_plot_out_png_file[2],img_width = "450px",href_rel_start = fs::path_dir(cfg$NORM_CLUSTERING_REPORT_HTML_FILE),do_cat = TRUE) + ``` diff --git a/inst/config/single_sample/01_input_qc.default.yaml b/inst/config/single_sample/01_input_qc.default.yaml index 351acc7..6d8c8b7 100644 --- a/inst/config/single_sample/01_input_qc.default.yaml +++ b/inst/config/single_sample/01_input_qc.default.yaml @@ -6,7 +6,7 @@ INPUT_DATA: target_name: "target_name" INPUT_QC_REPORT_RMD_FILE: "Rmd/single_sample/01_input_qc.Rmd" -#INPUT_QC_REPORT_RMD_FILE: "Rmd/single_saple/01_input_qc_spatial.Rmd" +#INPUT_QC_REPORT_RMD_FILE: "Rmd/single_sample/01_input_qc_spatial.Rmd" ############################################################################### ### Spatial experiment ######################################################## diff --git a/inst/config/single_sample/02_norm_clustering.default.yaml b/inst/config/single_sample/02_norm_clustering.default.yaml index b9e0c76..c66bb3a 100644 --- a/inst/config/single_sample/02_norm_clustering.default.yaml +++ b/inst/config/single_sample/02_norm_clustering.default.yaml @@ -104,12 +104,13 @@ CELL_GROUPINGS: null ### Manual cell annotation signatures ######################################### MANUAL_ANNOTATION: False -ANNOTATION_MARKERS: null +ANNOTATION_MARKERS: "input_data/brain_markers.csv" SCALE_ANNOTATION: False OVERLAP: 5 -ANNOTATION_CLUSTERING: null +ANNOTATION_CLUSTERING: "cluster_kmeans_k4" SHOW_VALUE: "value" -MAKE_CELL_PLOT: False +MAKE_CELL_PLOT: True +HEATMAP_DIMRED: "umap" ############################################################################### ### Dimensionality reduction plots ############################################