Skip to content

Commit 41346d9

Browse files
committed
13/11/2026
1 parent a279b50 commit 41346d9

10 files changed

Lines changed: 7983 additions & 38 deletions
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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")
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env python3
2+
"""Binding > Similar Profiles: separate bar-plot / density-plot downloads with figure-friendly sizes.
3+
Layout: 2x2 grid (bar plots on top, density plots below, each 8 in wide) + pooled density (16 in wide) underneath.
4+
Run from the CHARM repo folder: python3 patch_binding_downloads.py
5+
Edits app.R and helper_functions.R in place (backups saved as *.bak). Safe to run twice."""
6+
import re, shutil, sys
7+
8+
def load(p): return open(p, encoding="utf-8").read()
9+
def save(p, s):
10+
shutil.copy(p, p + ".bak"); open(p, "w", encoding="utf-8").write(s)
11+
12+
# ---------------- helper_functions.R ----------------
13+
h = load("helper_functions.R")
14+
if "bar_plot_only" in h:
15+
print("helper_functions.R: already patched")
16+
else:
17+
old = ' tbl_cols <- c("Profile", "RBP", "Target", "Correlation", if (has_ref) c("Percentile", "SameTarget"))\n if (has_ref) {'
18+
assert h.count(old) == 1, "anchor 1 not found in helper_functions.R"
19+
h = h.replace(old, ' tbl_cols <- c("Profile", "RBP", "Target", "Correlation", if (has_ref) c("Percentile", "SameTarget"))\n'
20+
' bar_plot_only <- heatmap_plot # bar plot alone (separate download)\n'
21+
' dens_only <- NULL # density plot alone (separate download)\n'
22+
' if (has_ref) {')
23+
old = ' 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 )'
24+
assert h.count(old) == 1, "anchor 2 not found in helper_functions.R"
25+
h = h.replace(old, ' bar_plot_only <- heatmap_plot\n dens_only <- dens\n'
26+
' heatmap_plot <- ggpubr::ggarrange(heatmap_plot, dens, ncol = 1, heights = c(1, 0.9))\n }\n'
27+
' list(\n correlation_table = cor_df[, tbl_cols],\n heatmap = heatmap_plot,\n'
28+
' bar_plot = bar_plot_only,\n density_plot = dens_only\n )')
29+
save("helper_functions.R", h); print("helper_functions.R: patched")
30+
31+
# ---------------- app.R ----------------
32+
a = load("app.R")
33+
old_v1 = '"_", dir_local, "_density"),\n width = 16, height = 4'
34+
if old_v1 in a: # first version of this script was run: only the density width changes
35+
a = a.replace(old_v1, '"_", dir_local, "_density"),\n width = 8, height = 4')
36+
save("app.R", a); print("app.R: density width corrected (16 -> 8)"); sys.exit(0)
37+
if 'dir_local, "_barplot"' in a:
38+
print("app.R: already patched"); sys.exit(0)
39+
40+
# 1. density downloads: full figure width (2 x 8 in) x 4 in
41+
a = a.replace('paste0(prefix, "_density"), width = 8, height = 4)', 'paste0(prefix, "_density"), width = 16, height = 4)')
42+
43+
# 2. store separately
44+
old = " dyn_dl_store[[plot_id]] <- res$heatmap\n res$heatmap"
45+
assert a.count(old) == 1, "anchor store not found"
46+
a = a.replace(old, " corr_store_plots(plot_id, res)\n res$heatmap")
47+
48+
# 3. handlers for the 6 bar+density plots
49+
old = ''' dl_id <- paste0("dl_", plot_id)
50+
output[[dl_id]] <- make_dyn_dl_handler(
51+
plot_id,
52+
paste0("binding_similar_profile_", ds_key, "_", dir_local)
53+
)'''
54+
assert a.count(old) == 1, "anchor handler not found"
55+
a = a.replace(old, ''' # bar plot and density plot are downloaded as two independent images
56+
output[[paste0("dl_", plot_id, "_bar")]] <- make_dyn_dl_handler(
57+
paste0(plot_id, "_bar"),
58+
paste0("binding_similar_profile_", ds_key, "_", dir_local, "_barplot"),
59+
width = 8, height = 6
60+
)
61+
output[[paste0("dl_", plot_id, "_density")]] <- make_dyn_dl_handler(
62+
paste0(plot_id, "_density"),
63+
paste0("binding_similar_profile_", ds_key, "_", dir_local, "_density"),
64+
width = 8, height = 4
65+
)''')
66+
67+
# 4. pooled (inc + dec) density: full width too
68+
old = 'plot_id, paste0("binding_similar_profile_", ds_key, "_together")\n )'
69+
assert a.count(old) == 1, "anchor pooled not found"
70+
a = a.replace(old, 'plot_id, paste0("binding_similar_profile_", ds_key, "_together"),\n width = 16, height = 4\n )')
71+
72+
# 5. buttons
73+
pat = re.compile(r'downloadButton\("dl_binding_sim_plot_(both|K562|HEPG2)_(inc|dec)", "[^"]*Download plot",\n\s*class = "btn btn-sm btn-default"\)')
74+
assert len(pat.findall(a)) == 6, "expected 6 buttons, found %d" % len(pat.findall(a))
75+
a = pat.sub(lambda m: ('tagList(downloadButton("dl_binding_sim_plot_%s_%s_bar", "\\u2193 Download bar plot", '
76+
'class = "btn btn-sm btn-default", style = "margin-right:6px;"), '
77+
'downloadButton("dl_binding_sim_plot_%s_%s_density", "\\u2193 Download density plot", '
78+
'class = "btn btn-sm btn-default"))') % (m.group(1), m.group(2), m.group(1), m.group(2)), a)
79+
save("app.R", a); print("app.R: patched")

‎Images/BindingPlots.png‎

477 KB
Loading

‎Images/BindingSimilarity.png‎

573 KB
Loading

‎Images/SplicingPlots.png‎

894 KB
Loading

‎Sups/CHARM_V2_NoNetwork.docx‎

1.96 MB
Binary file not shown.

0 commit comments

Comments
 (0)