softwareDevelopment commited on
Commit
7f2b863
Β·
verified Β·
1 Parent(s): 749c3ca

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +169 -80
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 β€” matches on-screen display
396
- # This is the SAME layout used for download (PNG export)
 
 
 
 
 
 
 
 
 
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 <- min(chr_markers$Position, na.rm = TRUE)
414
- max_pos <- max(chr_markers$Position, na.rm = TRUE)
 
415
 
416
- chr_markers$mtype <- "Background"
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
- # Use LOD trace from cim_all if available
423
- lod_trace <- NULL
424
- if (!is.null(cim_all) && !"Message" %in% colnames(cim_all)) {
425
- lod_trace <- cim_all[cim_all$Chromosome == chr & !is.na(cim_all$LOD), ]
426
- lod_trace <- lod_trace[order(lod_trace$Position), ]
427
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
428
 
429
  p <- ggplot() +
430
- # Chromosome body
431
- annotate("rect", xmin = min_pos, xmax = max_pos, ymin = 0.35, ymax = 0.65,
432
- fill = "#E2E8F0", color = "#94A3B8", linewidth = 0.5) +
433
- # LOD-1.5 interval shading
 
 
 
434
  {
435
- if (nrow(chr_qtls) > 0 && "Interval_Start" %in% colnames(chr_qtls)) {
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
- xmin = qtl_int$Interval_Start, xmax = qtl_int$Interval_End,
440
- ymin = 0.15, ymax = 0.85,
441
- fill = "#FEE2E2", alpha = 0.55, color = "#FCA5A5", linewidth = 0.4)
442
- else list()
 
443
  } else list()
444
  } +
445
- # Background markers
446
- geom_segment(data = chr_markers[chr_markers$mtype == "Background", ],
447
- aes(x = Position, xend = Position, y = 0.25, yend = 0.75),
448
- color = "#CBD5E1", linewidth = 0.3, alpha = 0.5) +
449
- # Flanking markers
450
- geom_segment(data = chr_markers[chr_markers$mtype == "Flanking", ],
451
- aes(x = Position, xend = Position, y = 0.1, yend = 0.9),
452
- color = "#2563EB", linewidth = 1.0) +
453
- # QTL markers
454
- geom_segment(data = chr_markers[chr_markers$mtype == "QTL", ],
455
- aes(x = Position, xend = Position, y = 0.0, yend = 1.0),
456
- color = "#DC2626", linewidth = 2.2) +
457
- # QTL labels
 
 
 
 
 
 
 
458
  {
459
- if (nrow(chr_qtls) > 0)
460
- ggrepel::geom_label_repel(
461
- data = chr_qtls,
462
- aes(x = Position, y = 1.12,
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
- # Legend dots at bottom
471
- annotate("point", x = min_pos + (max_pos - min_pos) * 0.02, y = -0.12, color = "#DC2626", size = 3, shape = 15) +
472
- annotate("text", x = min_pos + (max_pos - min_pos) * 0.05, y = -0.12, label = "QTL peak", hjust = 0, size = 2.5, color = "#DC2626") +
473
- annotate("point", x = min_pos + (max_pos - min_pos) * 0.18, y = -0.12, color = "#2563EB", size = 3, shape = 15) +
474
- annotate("text", x = min_pos + (max_pos - min_pos) * 0.21, y = -0.12, label = "Flanking", hjust = 0, size = 2.5, color = "#2563EB") +
475
- annotate("rect", xmin = min_pos + (max_pos - min_pos) * 0.34, xmax = min_pos + (max_pos - min_pos) * 0.36,
476
- ymin = -0.16, ymax = -0.08, fill = "#FEE2E2", color = "#FCA5A5") +
477
- annotate("text", x = min_pos + (max_pos - min_pos) * 0.37, y = -0.12, label = "LOD-1.5 interval", hjust = 0, size = 2.5, color = "#B45309") +
478
- scale_x_continuous(labels = function(x) paste0(round(x / 1e6, 1), " Mb")) +
479
- ylim(-0.25, 1.45) +
480
- labs(title = paste("Chromosome", chr,
481
- paste0("(", nrow(chr_markers), " markers, ", nrow(chr_qtls), " QTL(s))")),
482
- x = "Genomic Position", y = NULL) +
483
- theme_bw(base_size = 11) +
484
- theme(panel.background = element_rect(fill = "white"),
485
- plot.background = element_rect(fill = "white"),
486
- axis.text.y = element_blank(),
487
- axis.ticks.y = element_blank(),
488
- panel.grid = element_blank(),
489
- panel.border = element_blank(),
490
- axis.line.x = element_line(color = "grey70"),
491
- plot.title = element_text(face = "bold", size = 11, color = "#1E3A5F"))
 
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 = plot_list, ncol = n_cols,
 
 
 
 
 
 
 
 
 
 
503
  top = grid::textGrob(
504
- "QTL Chromosome Map β€” LOD-1.5 Support Intervals (CIM v5)",
505
- gp = grid::gpar(fontsize = 14, fontface = "bold", col = "#1E3A5F")),
506
  bottom = grid::textGrob(
507
  paste0("Generated: ", format(Sys.time(), "%Y-%m-%d %H:%M"),
508
- " | Red = QTL peak | Blue = Flanking markers | Pink shading = LOD-1.5 interval"),
509
- gp = grid::gpar(fontsize = 8, col = "#64748B"))
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
- ggsave(file, plot = p, width = 10, height = 6, dpi = 200, bg = "white")
 
 
1740
  } else {
1741
- n_chrs <- length(unique(values$qtl_effects_train$Chromosome))
1742
- n_rows <- ceiling(n_chrs / 2)
1743
- img_h <- max(600, 380 * n_rows + 200)
1744
- png(file, width = 3200, height = img_h, res = 200, bg = "white")
 
 
 
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
  }