Spaces:
Sleeping
Sleeping
Update app.R
Browse files
app.R
CHANGED
|
@@ -392,8 +392,17 @@ make_ggplot_manhattan <- function(cim_all, lod_threshold = 2.0, title = "CIM Man
|
|
| 392 |
}
|
| 393 |
|
| 394 |
# ============================================================
|
| 395 |
-
# GGPLOT2 CHROMOSOME VISUALIZATION β
|
| 396 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 397 |
# ============================================================
|
| 398 |
make_ggplot_chromosome_viz <- function(qtl_effects, all_markers_df, cim_all = NULL,
|
| 399 |
lod_threshold = 2.0) {
|
|
@@ -407,106 +416,181 @@ make_ggplot_chromosome_viz <- function(qtl_effects, all_markers_df, cim_all = NU
|
|
| 407 |
chr_markers <- all_markers_df[all_markers_df$Chromosome == chr, ]
|
| 408 |
chr_markers <- chr_markers[order(chr_markers$Position), ]
|
| 409 |
chr_qtls <- qtl_effects[qtl_effects$Chromosome == chr, ]
|
| 410 |
-
|
| 411 |
if (nrow(chr_markers) == 0) next
|
| 412 |
|
| 413 |
-
min_pos
|
| 414 |
-
max_pos
|
|
|
|
| 415 |
|
| 416 |
-
|
| 417 |
-
chr_markers$mtype[chr_markers$Marker %in% chr_qtls$Marker] <- "QTL"
|
| 418 |
flank_mks <- c(chr_qtls$Low_Flank, chr_qtls$High_Flank)
|
| 419 |
flank_mks <- flank_mks[!is.na(flank_mks) & flank_mks != ""]
|
| 420 |
-
chr_markers$mtype[chr_markers$Marker %in% flank_mks & chr_markers$mtype != "QTL"] <- "Flanking"
|
| 421 |
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 428 |
|
| 429 |
p <- ggplot() +
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
|
|
|
|
|
|
|
|
|
| 434 |
{
|
| 435 |
-
if (
|
| 436 |
qtl_int <- chr_qtls[!is.na(chr_qtls$Interval_Start) & !is.na(chr_qtls$Interval_End), ]
|
| 437 |
-
if (nrow(qtl_int) > 0)
|
|
|
|
|
|
|
|
|
|
| 438 |
annotate("rect",
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
|
|
|
| 443 |
} else list()
|
| 444 |
} +
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 458 |
{
|
| 459 |
-
if (nrow(
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
label = paste0(Marker, "\nLOD=", round(LOD, 1),
|
| 464 |
-
"\nPVE=", round(PVE_CIM_Percent, 1), "%")),
|
| 465 |
-
size = 2.8, fill = "white", color = "#DC2626",
|
| 466 |
-
box.padding = 0.25, max.overlaps = 10, segment.color = "#DC2626",
|
| 467 |
-
fontface = "bold")
|
| 468 |
else list()
|
| 469 |
} +
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
annotate("text",
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
annotate("
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
|
|
|
| 492 |
|
| 493 |
plot_list[[as.character(chr)]] <- p
|
| 494 |
}
|
| 495 |
|
| 496 |
if (length(plot_list) == 0) return(NULL)
|
| 497 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 498 |
n_cols <- min(2, length(plot_list))
|
| 499 |
-
n_rows <- ceiling(length(plot_list) / n_cols)
|
| 500 |
|
| 501 |
gridExtra::arrangeGrob(
|
| 502 |
-
grobs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 503 |
top = grid::textGrob(
|
| 504 |
-
"QTL Chromosome Map β
|
| 505 |
-
gp = grid::gpar(fontsize =
|
| 506 |
bottom = grid::textGrob(
|
| 507 |
paste0("Generated: ", format(Sys.time(), "%Y-%m-%d %H:%M"),
|
| 508 |
-
"
|
| 509 |
-
gp = grid::gpar(fontsize =
|
| 510 |
)
|
| 511 |
}
|
| 512 |
|
|
@@ -1735,13 +1819,18 @@ server <- function(input, output, session) {
|
|
| 1735 |
lod_threshold = values$lod_threshold
|
| 1736 |
)
|
| 1737 |
if (is.null(grob)) {
|
| 1738 |
-
p <- ggplot() + labs(title = "No QTL data") + theme_bw()
|
| 1739 |
-
|
|
|
|
|
|
|
| 1740 |
} else {
|
| 1741 |
-
|
| 1742 |
-
|
| 1743 |
-
|
| 1744 |
-
|
|
|
|
|
|
|
|
|
|
| 1745 |
grid::grid.draw(grob)
|
| 1746 |
dev.off()
|
| 1747 |
}
|
|
|
|
| 392 |
}
|
| 393 |
|
| 394 |
# ============================================================
|
| 395 |
+
# GGPLOT2 CHROMOSOME VISUALIZATION β EXACT pixel match of on-screen HTML viz
|
| 396 |
+
#
|
| 397 |
+
# Replicates the HTML/CSS display faithfully:
|
| 398 |
+
# - Grey rounded chromosome bar (y = 0.35 to 0.65, same as CSS height 24px)
|
| 399 |
+
# - Red interval-band shading (rgba(220,38,38,0.12) = #FEE2E2 alpha)
|
| 400 |
+
# - Grey background marker ticks (color:#94A3B8, thin, short = CSS default)
|
| 401 |
+
# - Blue flanking marker ticks (color:#1D4ED8, taller)
|
| 402 |
+
# - Red significant QTL ticks (color:#DC2626, tallest, widest)
|
| 403 |
+
# - bp labels at left and right ends exactly like chromosome-legend div
|
| 404 |
+
# - Title: "Chromosome X β N markers, N QTLs (LOD-1.5: X Mb)"
|
| 405 |
+
# - Legend row: Redβ Significant QTL Blueβ Flanking marker Pinkβ LOD-1.5 interval Greyβ All markers
|
| 406 |
# ============================================================
|
| 407 |
make_ggplot_chromosome_viz <- function(qtl_effects, all_markers_df, cim_all = NULL,
|
| 408 |
lod_threshold = 2.0) {
|
|
|
|
| 416 |
chr_markers <- all_markers_df[all_markers_df$Chromosome == chr, ]
|
| 417 |
chr_markers <- chr_markers[order(chr_markers$Position), ]
|
| 418 |
chr_qtls <- qtl_effects[qtl_effects$Chromosome == chr, ]
|
|
|
|
| 419 |
if (nrow(chr_markers) == 0) next
|
| 420 |
|
| 421 |
+
min_pos <- min(chr_markers$Position, na.rm = TRUE)
|
| 422 |
+
max_pos <- max(chr_markers$Position, na.rm = TRUE)
|
| 423 |
+
range_pos <- max_pos - min_pos
|
| 424 |
|
| 425 |
+
# Classify markers exactly as the HTML renderUI does
|
|
|
|
| 426 |
flank_mks <- c(chr_qtls$Low_Flank, chr_qtls$High_Flank)
|
| 427 |
flank_mks <- flank_mks[!is.na(flank_mks) & flank_mks != ""]
|
|
|
|
| 428 |
|
| 429 |
+
chr_markers$mtype <- "background"
|
| 430 |
+
chr_markers$mtype[chr_markers$Marker %in% flank_mks] <- "flanking"
|
| 431 |
+
chr_markers$mtype[chr_markers$Marker %in% chr_qtls$Marker] <- "significant" # significant wins
|
| 432 |
+
|
| 433 |
+
# y-ranges matching CSS pixel heights (normalised to 0-1 chromosome bar space):
|
| 434 |
+
# chromosome bar: y 0.30 β 0.70 (matches CSS height:24px within 34px total marker space)
|
| 435 |
+
# background tick: y 0.25 β 0.75 (CSS: height:34px, top:-5px β covers bar Β± small extension)
|
| 436 |
+
# flanking tick: y 0.10 β 0.90 (CSS: height:30px, top:-3px β taller)
|
| 437 |
+
# significant tick: y 0.00 β 1.00 (CSS: height:42px, top:-9px β full height)
|
| 438 |
+
|
| 439 |
+
bg_mk <- chr_markers[chr_markers$mtype == "background", ]
|
| 440 |
+
fl_mk <- chr_markers[chr_markers$mtype == "flanking", ]
|
| 441 |
+
sig_mk <- chr_markers[chr_markers$mtype == "significant", ]
|
| 442 |
+
|
| 443 |
+
# Build interval rectangles from the HTML logic:
|
| 444 |
+
# left% = (Interval_Start - min_pos) / range_pos * 100
|
| 445 |
+
# width% = (Interval_End - Interval_Start) / range_pos * 100
|
| 446 |
+
has_intervals <- nrow(chr_qtls) > 0 &&
|
| 447 |
+
"Interval_Start" %in% colnames(chr_qtls) &&
|
| 448 |
+
any(!is.na(chr_qtls$Interval_Start))
|
| 449 |
+
|
| 450 |
+
# Title string identical to HTML chromosome-title div
|
| 451 |
+
int_sizes <- if ("Interval_Size_Mb" %in% colnames(chr_qtls)) {
|
| 452 |
+
sz <- chr_qtls$Interval_Size_Mb[!is.na(chr_qtls$Interval_Size_Mb)]
|
| 453 |
+
if (length(sz) > 0) paste0(" (LOD-1.5: ", paste(round(sz, 2), collapse = ", "), " Mb)") else ""
|
| 454 |
+
} else ""
|
| 455 |
+
title_str <- paste0("Chromosome ", chr, " β ", nrow(chr_markers),
|
| 456 |
+
" markers, ", nrow(chr_qtls), " QTLs", int_sizes)
|
| 457 |
+
|
| 458 |
+
# x-axis bp labels identical to chromosome-legend (format with commas, "bp" suffix)
|
| 459 |
+
left_label <- paste0(format(min_pos, big.mark = ","), " bp")
|
| 460 |
+
right_label <- paste0(format(max_pos, big.mark = ","), " bp")
|
| 461 |
|
| 462 |
p <- ggplot() +
|
| 463 |
+
|
| 464 |
+
# ββ Chromosome body (rounded rect, CSS: background:#E2E8F0, border:#CBD5E1) ββ
|
| 465 |
+
annotate("rect",
|
| 466 |
+
xmin = min_pos, xmax = max_pos, ymin = 0.30, ymax = 0.70,
|
| 467 |
+
fill = "#E2E8F0", color = "#CBD5E1", linewidth = 0.6) +
|
| 468 |
+
|
| 469 |
+
# ββ LOD-1.5 interval shading (CSS: interval-band rgba(220,38,38,0.12)) ββ
|
| 470 |
{
|
| 471 |
+
if (has_intervals) {
|
| 472 |
qtl_int <- chr_qtls[!is.na(chr_qtls$Interval_Start) & !is.na(chr_qtls$Interval_End), ]
|
| 473 |
+
if (nrow(qtl_int) > 0) {
|
| 474 |
+
# clamp interval to chromosome bounds
|
| 475 |
+
qtl_int$xs <- pmax(qtl_int$Interval_Start, min_pos)
|
| 476 |
+
qtl_int$xe <- pmin(qtl_int$Interval_End, max_pos)
|
| 477 |
annotate("rect",
|
| 478 |
+
xmin = qtl_int$xs, xmax = qtl_int$xe,
|
| 479 |
+
ymin = 0.00, ymax = 1.00,
|
| 480 |
+
fill = "#FEE2E2", alpha = 0.7,
|
| 481 |
+
color = "#FCA5A5", linewidth = 0.35)
|
| 482 |
+
} else list()
|
| 483 |
} else list()
|
| 484 |
} +
|
| 485 |
+
|
| 486 |
+
# ββ Background markers (CSS: color:#94A3B8, thin grey tick) ββ
|
| 487 |
+
{
|
| 488 |
+
if (nrow(bg_mk) > 0)
|
| 489 |
+
geom_segment(data = bg_mk,
|
| 490 |
+
aes(x = Position, xend = Position, y = 0.25, yend = 0.75),
|
| 491 |
+
color = "#94A3B8", linewidth = 0.35, alpha = 0.6)
|
| 492 |
+
else list()
|
| 493 |
+
} +
|
| 494 |
+
|
| 495 |
+
# ββ Flanking markers (CSS: color:#1D4ED8, taller blue tick) ββ
|
| 496 |
+
{
|
| 497 |
+
if (nrow(fl_mk) > 0)
|
| 498 |
+
geom_segment(data = fl_mk,
|
| 499 |
+
aes(x = Position, xend = Position, y = 0.10, yend = 0.90),
|
| 500 |
+
color = "#1D4ED8", linewidth = 0.9)
|
| 501 |
+
else list()
|
| 502 |
+
} +
|
| 503 |
+
|
| 504 |
+
# ββ Significant QTL markers (CSS: color:#DC2626, full-height red tick) ββ
|
| 505 |
{
|
| 506 |
+
if (nrow(sig_mk) > 0)
|
| 507 |
+
geom_segment(data = sig_mk,
|
| 508 |
+
aes(x = Position, xend = Position, y = 0.00, yend = 1.00),
|
| 509 |
+
color = "#DC2626", linewidth = 2.0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 510 |
else list()
|
| 511 |
} +
|
| 512 |
+
|
| 513 |
+
# ββ bp labels at ends β replicate chromosome-legend div ββ
|
| 514 |
+
annotate("text", x = min_pos, y = -0.08,
|
| 515 |
+
label = left_label, hjust = 0, size = 3.0,
|
| 516 |
+
color = "#94A3B8", family = "mono") +
|
| 517 |
+
annotate("text", x = max_pos, y = -0.08,
|
| 518 |
+
label = right_label, hjust = 1.0, size = 3.0,
|
| 519 |
+
color = "#94A3B8", family = "mono") +
|
| 520 |
+
|
| 521 |
+
# ββ Axis: show actual bp positions (not scaled), x-axis removed (legend handles it) ββ
|
| 522 |
+
scale_x_continuous(expand = expansion(mult = 0.02)) +
|
| 523 |
+
coord_cartesian(ylim = c(-0.22, 1.22), xlim = c(min_pos, max_pos)) +
|
| 524 |
+
|
| 525 |
+
labs(title = title_str, x = NULL, y = NULL) +
|
| 526 |
+
|
| 527 |
+
theme_void(base_size = 11) +
|
| 528 |
+
theme(
|
| 529 |
+
plot.background = element_rect(fill = "white", color = "#E2E8F0", linewidth = 0.5),
|
| 530 |
+
panel.background = element_rect(fill = "white"),
|
| 531 |
+
plot.title = element_text(face = "bold", size = 10.5, color = "#1D4ED8",
|
| 532 |
+
family = "mono", margin = margin(b = 4)),
|
| 533 |
+
plot.margin = margin(t = 8, r = 12, b = 8, l = 12)
|
| 534 |
+
)
|
| 535 |
|
| 536 |
plot_list[[as.character(chr)]] <- p
|
| 537 |
}
|
| 538 |
|
| 539 |
if (length(plot_list) == 0) return(NULL)
|
| 540 |
|
| 541 |
+
# ββ Legend row β matches chr-legend div ββ
|
| 542 |
+
legend_grob <- grid::textGrob(
|
| 543 |
+
paste0(
|
| 544 |
+
"\u25A0 Significant QTL ",
|
| 545 |
+
"\u25A0 Flanking marker ",
|
| 546 |
+
"\u25A0 LOD-1.5 interval ",
|
| 547 |
+
"\u25A0 All markers"
|
| 548 |
+
),
|
| 549 |
+
gp = grid::gpar(
|
| 550 |
+
fontsize = 9,
|
| 551 |
+
col = c("#DC2626","#1D4ED8","#FCA5A5","#94A3B8")[1], # textGrob is monochrome; use subtitle instead
|
| 552 |
+
col = "#475569"
|
| 553 |
+
)
|
| 554 |
+
)
|
| 555 |
+
|
| 556 |
+
# Build coloured legend via a tiny ggplot
|
| 557 |
+
legend_df <- data.frame(
|
| 558 |
+
label = c("Significant QTL", "Flanking marker", "LOD-1.5 interval", "All markers"),
|
| 559 |
+
col = c("#DC2626", "#1D4ED8", "#FCA5A5", "#94A3B8"),
|
| 560 |
+
x = c(1, 2, 3, 4),
|
| 561 |
+
stringsAsFactors = FALSE
|
| 562 |
+
)
|
| 563 |
+
leg_plot <- ggplot(legend_df) +
|
| 564 |
+
geom_point(aes(x = x, y = 1, color = label), shape = 15, size = 5, show.legend = FALSE) +
|
| 565 |
+
geom_text(aes(x = x + 0.15, y = 1, label = label, color = label),
|
| 566 |
+
hjust = 0, size = 3.2, show.legend = FALSE) +
|
| 567 |
+
scale_color_manual(values = setNames(legend_df$col, legend_df$label)) +
|
| 568 |
+
scale_x_continuous(limits = c(0.8, 5.5)) +
|
| 569 |
+
theme_void() +
|
| 570 |
+
theme(plot.background = element_rect(fill = "white", color = NA),
|
| 571 |
+
plot.margin = margin(4, 4, 4, 4))
|
| 572 |
+
|
| 573 |
n_cols <- min(2, length(plot_list))
|
|
|
|
| 574 |
|
| 575 |
gridExtra::arrangeGrob(
|
| 576 |
+
grobs = c(plot_list, list(leg_plot)),
|
| 577 |
+
layout_matrix = {
|
| 578 |
+
n <- length(plot_list)
|
| 579 |
+
nc <- n_cols
|
| 580 |
+
nr <- ceiling(n / nc)
|
| 581 |
+
# chromosome plots fill the grid; legend spans full bottom row
|
| 582 |
+
mat <- matrix(seq_len(n), nrow = nr, ncol = nc, byrow = TRUE)
|
| 583 |
+
mat[mat > n] <- NA
|
| 584 |
+
rbind(mat, rep(n + 1L, nc)) # legend row at bottom
|
| 585 |
+
},
|
| 586 |
+
heights = c(rep(1, ceiling(length(plot_list) / n_cols)), 0.18),
|
| 587 |
top = grid::textGrob(
|
| 588 |
+
"QTL Chromosome Map β CIM v5",
|
| 589 |
+
gp = grid::gpar(fontsize = 13, fontface = "bold", col = "#1E3A5F")),
|
| 590 |
bottom = grid::textGrob(
|
| 591 |
paste0("Generated: ", format(Sys.time(), "%Y-%m-%d %H:%M"),
|
| 592 |
+
" | LOD-1.5 support intervals | -1/0/+1 additive coding"),
|
| 593 |
+
gp = grid::gpar(fontsize = 7.5, col = "#94A3B8"))
|
| 594 |
)
|
| 595 |
}
|
| 596 |
|
|
|
|
| 1819 |
lod_threshold = values$lod_threshold
|
| 1820 |
)
|
| 1821 |
if (is.null(grob)) {
|
| 1822 |
+
p <- ggplot() + labs(title = "No QTL data") + theme_bw() +
|
| 1823 |
+
theme(plot.background = element_rect(fill = "white"),
|
| 1824 |
+
panel.background = element_rect(fill = "white"))
|
| 1825 |
+
ggsave(file, plot = p, width = 10, height = 4, dpi = 200, bg = "white")
|
| 1826 |
} else {
|
| 1827 |
+
# Each chromosome row ~1.1 inches at 200 dpi; header+legend+padding ~1.2 in
|
| 1828 |
+
n_chrs <- length(unique(values$qtl_effects_train$Chromosome))
|
| 1829 |
+
n_rows <- ceiling(n_chrs / 2)
|
| 1830 |
+
img_h_in <- 1.2 + n_rows * 1.1
|
| 1831 |
+
img_w_in <- 16
|
| 1832 |
+
png(file, width = round(img_w_in * 200), height = round(img_h_in * 200),
|
| 1833 |
+
res = 200, bg = "white")
|
| 1834 |
grid::grid.draw(grob)
|
| 1835 |
dev.off()
|
| 1836 |
}
|