|
| 1 | +# Binding > Similar Profiles: separate bar-plot / density-plot downloads with figure-friendly sizes. |
| 2 | +# Layout: 2x2 grid (bar plots on top, density plots below, each 8 in wide) + pooled density (16 in wide) underneath. |
| 3 | +# Run from the CHARM folder (R console or: Rscript patch_binding_downloads.R). Edits app.R and |
| 4 | +# helper_functions.R in place (backups: *.bak). Safe to run more than once. |
| 5 | + |
| 6 | +rd <- function(p) { x <- rawToChar(readBin(p, "raw", file.size(p))); Encoding(x) <- "UTF-8"; x } |
| 7 | +wr <- function(p, x) { file.copy(p, paste0(p, ".bak"), overwrite = TRUE); writeBin(charToRaw(x), p) } |
| 8 | +cnt <- function(x, pat) { m <- gregexpr(pat, x, fixed = TRUE)[[1]]; if (m[1] == -1) 0L else length(m) } |
| 9 | +sub1 <- function(x, old, new, what) { |
| 10 | + if (cnt(x, old) != 1) stop("Could not find expected code for: ", what, " (found ", cnt(x, old), "x). Nothing was changed in this file.") |
| 11 | + sub(old, gsub("\\", "\\\\", new, fixed = TRUE), x, fixed = TRUE) |
| 12 | +} |
| 13 | +stopifnot(file.exists("app.R"), file.exists("helper_functions.R")) |
| 14 | + |
| 15 | +# ---------------- helper_functions.R ---------------- |
| 16 | +h <- rd("helper_functions.R") |
| 17 | +if (grepl("bar_plot_only", h, fixed = TRUE)) { |
| 18 | + cat("helper_functions.R: already patched\n") |
| 19 | +} else { |
| 20 | + h <- sub1(h, |
| 21 | + ' tbl_cols <- c("Profile", "RBP", "Target", "Correlation", if (has_ref) c("Percentile", "SameTarget"))\n if (has_ref) {', |
| 22 | + paste0(' tbl_cols <- c("Profile", "RBP", "Target", "Correlation", if (has_ref) c("Percentile", "SameTarget"))\n', |
| 23 | + ' bar_plot_only <- heatmap_plot # bar plot alone (separate download)\n', |
| 24 | + ' dens_only <- NULL # density plot alone (separate download)\n', |
| 25 | + ' if (has_ref) {'), "helper anchor 1") |
| 26 | + h <- sub1(h, |
| 27 | + ' heatmap_plot <- ggpubr::ggarrange(heatmap_plot, dens, ncol = 1, heights = c(1, 0.9))\n }\n list(\n correlation_table = cor_df[, tbl_cols],\n heatmap = heatmap_plot\n )', |
| 28 | + paste0(' bar_plot_only <- heatmap_plot\n dens_only <- dens\n', |
| 29 | + ' heatmap_plot <- ggpubr::ggarrange(heatmap_plot, dens, ncol = 1, heights = c(1, 0.9))\n }\n', |
| 30 | + ' list(\n correlation_table = cor_df[, tbl_cols],\n heatmap = heatmap_plot,\n', |
| 31 | + ' bar_plot = bar_plot_only,\n density_plot = dens_only\n )'), "helper anchor 2") |
| 32 | + wr("helper_functions.R", h); cat("helper_functions.R: patched\n") |
| 33 | +} |
| 34 | + |
| 35 | +# ---------------- app.R ---------------- |
| 36 | +a <- rd("app.R") |
| 37 | +v1 <- '"_", dir_local, "_density"),\n width = 16, height = 4' |
| 38 | +if (grepl(v1, a, fixed = TRUE)) { # an earlier (Python) version was applied: only the width changes |
| 39 | + a <- sub(v1, '"_", dir_local, "_density"),\n width = 8, height = 4', a, fixed = TRUE) |
| 40 | + wr("app.R", a); cat("app.R: density width corrected (16 -> 8)\n") |
| 41 | +} else if (grepl('dir_local, "_barplot"', a, fixed = TRUE)) { |
| 42 | + cat("app.R: already patched\n") |
| 43 | +} else { |
| 44 | + # 1. Expression/Splicing density downloads: full figure width |
| 45 | + a <- sub('paste0(prefix, "_density"), width = 8, height = 4)', 'paste0(prefix, "_density"), width = 16, height = 4)', a, fixed = TRUE) |
| 46 | + # 2. store bar and density separately |
| 47 | + a <- sub1(a, " dyn_dl_store[[plot_id]] <- res$heatmap\n res$heatmap", |
| 48 | + " corr_store_plots(plot_id, res)\n res$heatmap", "store plots") |
| 49 | + # 3. handlers for the six bar + density panels |
| 50 | + a <- sub1(a, |
| 51 | +' dl_id <- paste0("dl_", plot_id) |
| 52 | + output[[dl_id]] <- make_dyn_dl_handler( |
| 53 | + plot_id, |
| 54 | + paste0("binding_similar_profile_", ds_key, "_", dir_local) |
| 55 | + )', |
| 56 | +' # bar plot and density plot are downloaded as two independent images |
| 57 | + output[[paste0("dl_", plot_id, "_bar")]] <- make_dyn_dl_handler( |
| 58 | + paste0(plot_id, "_bar"), |
| 59 | + paste0("binding_similar_profile_", ds_key, "_", dir_local, "_barplot"), |
| 60 | + width = 8, height = 6 |
| 61 | + ) |
| 62 | + output[[paste0("dl_", plot_id, "_density")]] <- make_dyn_dl_handler( |
| 63 | + paste0(plot_id, "_density"), |
| 64 | + paste0("binding_similar_profile_", ds_key, "_", dir_local, "_density"), |
| 65 | + width = 8, height = 4 |
| 66 | + )', "per-panel handlers") |
| 67 | + # 4. pooled (increased + decreased) density: full width |
| 68 | + a <- sub1(a, 'plot_id, paste0("binding_similar_profile_", ds_key, "_together")\n )', |
| 69 | + 'plot_id, paste0("binding_similar_profile_", ds_key, "_together"),\n width = 16, height = 4\n )', "pooled density handler") |
| 70 | + # 5. the six buttons |
| 71 | + pat <- 'downloadButton\\("dl_binding_sim_plot_(both|K562|HEPG2)_(inc|dec)", "[^"]*Download plot",\n\\s*class = "btn btn-sm btn-default"\\)' |
| 72 | + if (length(gregexpr(pat, a, perl = TRUE)[[1]]) != 6) stop("Expected 6 download buttons; nothing was changed in app.R.") |
| 73 | + a <- gsub(pat, |
| 74 | + 'tagList(downloadButton("dl_binding_sim_plot_\\1_\\2_bar", "\\\\u2193 Download bar plot", class = "btn btn-sm btn-default", style = "margin-right:6px;"), downloadButton("dl_binding_sim_plot_\\1_\\2_density", "\\\\u2193 Download density plot", class = "btn btn-sm btn-default"))', |
| 75 | + a, perl = TRUE) |
| 76 | + wr("app.R", a); cat("app.R: patched\n") |
| 77 | +} |
| 78 | +invisible(parse("app.R")); invisible(parse("helper_functions.R")); cat("Both files parse correctly. Restart the app.\n") |
0 commit comments