Spaces:
Sleeping
Sleeping
Update app.R
Browse files
app.R
CHANGED
|
@@ -1,16 +1,23 @@
|
|
| 1 |
# ============================================================
|
| 2 |
-
# QTL-Mapper v7 ENHANCED β
|
| 3 |
-
#
|
| 4 |
-
#
|
| 5 |
-
#
|
| 6 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# ============================================================
|
| 8 |
|
| 9 |
options(repos = c(CRAN = "https://cloud.r-project.org"))
|
| 10 |
|
| 11 |
required_packages <- c(
|
| 12 |
"shiny", "randomForest", "caret", "DT", "ggplot2", "plotly",
|
| 13 |
-
"dplyr", "glmnet", "shinyjs", "scales", "e1071", "xgboost",
|
|
|
|
| 14 |
)
|
| 15 |
|
| 16 |
for (pkg in required_packages) {
|
|
@@ -21,7 +28,7 @@ for (pkg in required_packages) {
|
|
| 21 |
}
|
| 22 |
|
| 23 |
# ============================================================
|
| 24 |
-
# 6 MODELS
|
| 25 |
# ============================================================
|
| 26 |
MAPPING_MODELS <- list(
|
| 27 |
"SIM" = list(label = "Simple Interval Mapping (SIM)", desc = "Single-marker regression; LOD = -log10(p)"),
|
|
@@ -32,7 +39,6 @@ MAPPING_MODELS <- list(
|
|
| 32 |
"XGBOOST" = list(label = "XGBoost QTL", desc = "Gradient boosting; gain importance")
|
| 33 |
)
|
| 34 |
|
| 35 |
-
# Reference-style QTL colors (red tones like the image)
|
| 36 |
MODEL_COLORS <- c(
|
| 37 |
"SIM" = "#CC0000",
|
| 38 |
"CIM" = "#FF3333",
|
|
@@ -55,6 +61,12 @@ calc_genetic_distance <- function(marker1, marker2, marker_info) {
|
|
| 55 |
return(Inf)
|
| 56 |
}
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
# ============================================================
|
| 59 |
# GENOTYPE LOADER
|
| 60 |
# ============================================================
|
|
@@ -145,7 +157,7 @@ qtl_scan_sim <- function(X_valid, y_valid, marker_info) {
|
|
| 145 |
}
|
| 146 |
|
| 147 |
# ============================================================
|
| 148 |
-
# CIM
|
| 149 |
# ============================================================
|
| 150 |
qtl_scan_cim_improved <- function(X_valid, y_valid, marker_info,
|
| 151 |
window_size = 10, n_cofactors = 5) {
|
|
@@ -466,12 +478,17 @@ add_lod_support_interval <- function(sig_df, all_df, lod_drop=1.5) {
|
|
| 466 |
}
|
| 467 |
|
| 468 |
# ============================================================
|
| 469 |
-
#
|
| 470 |
# ============================================================
|
| 471 |
make_chromosome_ideogram_reference <- function(qtl_list, all_markers_df,
|
| 472 |
-
lod_threshold=2.0,
|
| 473 |
-
model_colors=MODEL_COLORS,
|
| 474 |
-
title_str=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 475 |
if (is.null(all_markers_df) || nrow(all_markers_df) == 0) return(invisible(NULL))
|
| 476 |
|
| 477 |
chroms <- sort(unique(as.character(all_markers_df$Chromosome)))
|
|
@@ -488,17 +505,11 @@ make_chromosome_ideogram_reference <- function(qtl_list, all_markers_df,
|
|
| 488 |
ylim=c(-8, max_pos * 1.12),
|
| 489 |
xaxt="n", yaxt="n", xlab="", ylab="", main="", frame.plot=FALSE, bty="n")
|
| 490 |
|
| 491 |
-
# Y-axis
|
| 492 |
y_ticks <- pretty(c(0, max_pos), n=7)
|
| 493 |
axis(2, at=y_ticks, labels=y_ticks, cex.axis=0.72, las=1,
|
| 494 |
col="#555555", col.axis="#333333", tcl=-0.3, lwd=0.8)
|
| 495 |
mtext("Genetic Position (cM)", side=2, line=2.5, cex=0.82, col="#222222", font=2)
|
| 496 |
|
| 497 |
-
# Band colors
|
| 498 |
-
band_dark <- "#2E6B35"
|
| 499 |
-
band_light <- "#6AAB6E"
|
| 500 |
-
chr_border <- "#1A4A1F"
|
| 501 |
-
|
| 502 |
for (ci in seq_along(chroms)) {
|
| 503 |
chr <- chroms[ci]
|
| 504 |
x_center <- (ci - 1) * x_gap + 0.9
|
|
@@ -508,38 +519,35 @@ make_chromosome_ideogram_reference <- function(qtl_list, all_markers_df,
|
|
| 508 |
chr_len <- max(chr_m$Position, na.rm=TRUE)
|
| 509 |
chr_min <- min(chr_m$Position, na.rm=TRUE)
|
| 510 |
|
| 511 |
-
# Draw banded chromosome
|
| 512 |
n_bands <- max(8, round((chr_len - chr_min) / 8))
|
| 513 |
band_h <- (chr_len - chr_min) / n_bands
|
| 514 |
|
| 515 |
for (bi in 1:n_bands) {
|
| 516 |
band_bot <- chr_min + (bi-1)*band_h
|
| 517 |
band_top <- chr_min + bi*band_h
|
| 518 |
-
bcol <- if (bi %% 2 == 1)
|
| 519 |
rect(x_center - bar_w/2, band_bot, x_center + bar_w/2, band_top,
|
| 520 |
col=bcol, border=NA)
|
| 521 |
}
|
| 522 |
|
| 523 |
rect(x_center - bar_w/2, chr_min, x_center + bar_w/2, chr_len,
|
| 524 |
-
col=NA, border=
|
| 525 |
|
| 526 |
symbols(x_center, chr_min, circles=bar_w/2, inches=FALSE,
|
| 527 |
-
add=TRUE, fg=
|
| 528 |
symbols(x_center, chr_len, circles=bar_w/2, inches=FALSE,
|
| 529 |
-
add=TRUE, fg=
|
| 530 |
|
| 531 |
-
# Centromere
|
| 532 |
cen_pos <- chr_min + (chr_len - chr_min)*0.42
|
| 533 |
polygon(c(x_center-bar_w/2, x_center, x_center+bar_w/2, x_center),
|
| 534 |
c(cen_pos - chr_len*0.025, cen_pos,
|
| 535 |
cen_pos - chr_len*0.025, cen_pos - chr_len*0.05),
|
| 536 |
-
col="white", border=
|
| 537 |
polygon(c(x_center-bar_w/2, x_center, x_center+bar_w/2, x_center),
|
| 538 |
c(cen_pos + chr_len*0.025, cen_pos,
|
| 539 |
cen_pos + chr_len*0.025, cen_pos + chr_len*0.05),
|
| 540 |
-
col="white", border=
|
| 541 |
|
| 542 |
-
# Plot QTLs
|
| 543 |
n_models <- length(model_names)
|
| 544 |
for (ti in seq_along(model_names)) {
|
| 545 |
trait <- model_names[ti]
|
|
@@ -551,10 +559,10 @@ make_chromosome_ideogram_reference <- function(qtl_list, all_markers_df,
|
|
| 551 |
qtl_data$LOD >= lod_threshold, ]
|
| 552 |
if (nrow(sig_qtls)==0) next
|
| 553 |
|
| 554 |
-
col_m
|
| 555 |
if (is.na(col_m)) col_m <- "#CC0000"
|
| 556 |
|
| 557 |
-
x_off
|
| 558 |
|
| 559 |
for (ri in 1:nrow(sig_qtls)) {
|
| 560 |
pos <- sig_qtls$Position[ri]
|
|
@@ -563,7 +571,7 @@ make_chromosome_ideogram_reference <- function(qtl_list, all_markers_df,
|
|
| 563 |
segments(x_center + bar_w/2, pos, x_off - 0.04, pos,
|
| 564 |
col=col_m, lwd=1.5)
|
| 565 |
points(x_off, pos, pch=16, col=col_m,
|
| 566 |
-
cex=pmax(
|
| 567 |
text(x_off + 0.15, pos,
|
| 568 |
paste0(round(lod_val,1)),
|
| 569 |
cex=0.55, col=col_m, font=2, adj=0)
|
|
@@ -574,13 +582,12 @@ make_chromosome_ideogram_reference <- function(qtl_list, all_markers_df,
|
|
| 574 |
cex=0.72, font=2, col="#1A1A1A", adj=0.5)
|
| 575 |
}
|
| 576 |
|
| 577 |
-
# Legend
|
| 578 |
legend_x <- n_chr * x_gap + 0.5
|
| 579 |
legend_y <- max_pos * 0.95
|
| 580 |
text(legend_x, legend_y + max_pos*0.07, "Models", cex=0.75, font=2, col="#1A1A1A", adj=0)
|
| 581 |
for (ti in seq_along(model_names)) {
|
| 582 |
-
tr
|
| 583 |
-
col_m <-
|
| 584 |
y_leg <- legend_y - (ti-1)*max_pos*0.07
|
| 585 |
qtl_d <- qtl_list[[tr]]
|
| 586 |
n_sig <- if(!is.null(qtl_d)&&!"Message"%in%colnames(qtl_d))
|
|
@@ -596,123 +603,89 @@ make_chromosome_ideogram_reference <- function(qtl_list, all_markers_df,
|
|
| 596 |
}
|
| 597 |
|
| 598 |
# ============================================================
|
| 599 |
-
# CHROMOSOME-WISE MANHATTAN PLOT (
|
| 600 |
# ============================================================
|
| 601 |
-
make_plotly_manhattan_chromosomewise <- function(scan_all, lod_threshold=2.0,
|
| 602 |
title="QTL Manhattan Plot",
|
| 603 |
nonsig_color="#3A7D44",
|
| 604 |
sig_color="#CC0000",
|
| 605 |
point_size=6,
|
| 606 |
-
sig_point_size=10
|
|
|
|
|
|
|
| 607 |
blank <- plot_ly() %>% layout(title=title, plot_bgcolor="white", paper_bgcolor="white")
|
| 608 |
if (is.null(scan_all) || "Message" %in% colnames(scan_all)) return(blank)
|
| 609 |
-
|
| 610 |
pd <- scan_all[!is.na(scan_all$LOD) & !is.na(scan_all$Position) & !is.na(scan_all$Chromosome), ]
|
| 611 |
if (nrow(pd) == 0) return(blank)
|
| 612 |
-
|
| 613 |
pd$Chromosome <- as.character(pd$Chromosome)
|
| 614 |
pd$sig <- pd$LOD >= lod_threshold
|
| 615 |
-
|
| 616 |
chroms <- sort(unique(pd$Chromosome))
|
| 617 |
-
|
| 618 |
-
# Create subplot for each chromosome
|
| 619 |
subplot_list <- list()
|
| 620 |
-
|
| 621 |
for (chr in chroms) {
|
| 622 |
chr_data <- pd[pd$Chromosome == chr, ]
|
| 623 |
chr_data <- chr_data[order(chr_data$Position), ]
|
| 624 |
-
|
| 625 |
-
# Non-significant markers
|
| 626 |
-
nonsig <- chr_data[!chr_data$sig, ]
|
| 627 |
-
# Significant markers
|
| 628 |
sig_data <- chr_data[chr_data$sig, ]
|
| 629 |
-
|
| 630 |
p <- plot_ly()
|
| 631 |
-
|
| 632 |
-
# Add non-significant markers
|
| 633 |
if (nrow(nonsig) > 0) {
|
| 634 |
p <- p %>% add_trace(
|
| 635 |
-
data = nonsig,
|
| 636 |
-
|
| 637 |
-
y = ~LOD,
|
| 638 |
-
type = "scatter",
|
| 639 |
-
mode = "markers",
|
| 640 |
marker = list(size = point_size, color = nonsig_color, opacity = 0.7),
|
| 641 |
-
text = ~paste0("<b>", Marker, "</b><br>Chr: ", Chromosome,
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
hoverinfo = "text",
|
| 645 |
-
showlegend = FALSE,
|
| 646 |
-
name = paste("Chr", chr)
|
| 647 |
)
|
| 648 |
}
|
| 649 |
-
|
| 650 |
-
# Add significant markers (NO TEXT LABELS)
|
| 651 |
if (nrow(sig_data) > 0) {
|
| 652 |
p <- p %>% add_trace(
|
| 653 |
-
data = sig_data,
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
mode = "markers", # Changed from "markers+text" to just "markers"
|
| 658 |
-
marker = list(size = sig_point_size, color = sig_color,
|
| 659 |
-
symbol = "diamond", line = list(width = 1, color = "#660000")),
|
| 660 |
text = ~paste0("<b>", Marker, "</b><br>Chr: ", Chromosome,
|
| 661 |
-
|
| 662 |
-
|
| 663 |
-
hoverinfo = "text",
|
| 664 |
-
showlegend = FALSE,
|
| 665 |
-
name = paste("Chr", chr, "Significant")
|
| 666 |
)
|
| 667 |
}
|
| 668 |
-
|
| 669 |
-
# Add threshold line
|
| 670 |
p <- p %>% add_segments(
|
| 671 |
-
x = min(chr_data$Position, na.rm = TRUE),
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
line = list(color = sig_color, dash = "dash", width = 1.5),
|
| 676 |
-
showlegend = FALSE,
|
| 677 |
-
hoverinfo = "none"
|
| 678 |
)
|
| 679 |
-
|
| 680 |
-
# Layout for this chromosome
|
| 681 |
p <- p %>% layout(
|
| 682 |
-
xaxis = list(
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
zeroline = FALSE
|
| 686 |
-
),
|
| 687 |
-
yaxis = list(
|
| 688 |
-
title = if (chr == chroms[1]) "LOD Score" else "",
|
| 689 |
-
gridcolor = "#EEEEEE",
|
| 690 |
-
zeroline = FALSE
|
| 691 |
-
),
|
| 692 |
-
plot_bgcolor = "white",
|
| 693 |
-
paper_bgcolor = "white"
|
| 694 |
)
|
| 695 |
-
|
| 696 |
subplot_list[[chr]] <- p
|
| 697 |
}
|
| 698 |
-
|
| 699 |
-
# Combine all subplots
|
| 700 |
n_chroms <- length(chroms)
|
| 701 |
-
n_cols
|
| 702 |
-
n_rows
|
| 703 |
-
|
| 704 |
combined_plot <- subplot(
|
| 705 |
-
subplot_list,
|
| 706 |
-
|
| 707 |
-
shareY = TRUE,
|
| 708 |
-
titleX = TRUE,
|
| 709 |
-
titleY = TRUE,
|
| 710 |
-
margin = 0.05
|
| 711 |
)
|
| 712 |
-
|
| 713 |
combined_plot %>% layout(
|
| 714 |
title = list(
|
| 715 |
-
text = paste0(title, " | ", sum(pd$sig, na.rm
|
| 716 |
font = list(size = 14, color = "#111111")
|
| 717 |
),
|
| 718 |
showlegend = FALSE,
|
|
@@ -741,22 +714,22 @@ find_common_qtl <- function(compare_res, lod_threshold, min_models=2, window_cM=
|
|
| 741 |
common <- all_sig[all_sig$N_Models_Detected>=min_models,]
|
| 742 |
if (nrow(common)==0) return(NULL)
|
| 743 |
markers_uniq <- unique(common$Marker)
|
| 744 |
-
summary_rows <- lapply(markers_uniq,function(mk){
|
| 745 |
rows <- common[common$Marker==mk,]
|
| 746 |
-
base <- rows[which.max(rows$LOD),c("Marker","Chromosome","Position","N_Models_Detected")]
|
| 747 |
-
base$Models_List <- paste(sort(unique(rows$Model)),collapse=", ")
|
| 748 |
-
base$Mean_LOD <- round(mean(rows$LOD,na.rm=TRUE),3)
|
| 749 |
-
base$Max_LOD <- round(max(rows$LOD,na.rm=TRUE),3)
|
| 750 |
-
base$Mean_PVE <- round(mean(rows$PVE_Marker_Percent,na.rm=TRUE),3)
|
| 751 |
-
base$Mean_Add_Eff <- round(mean(rows$Additive_Effect,na.rm=TRUE),3)
|
| 752 |
-
for (mth in names(compare_res)){
|
| 753 |
r_mth <- rows[rows$Model==mth,]
|
| 754 |
base[[paste0("LOD_",mth)]] <- if(nrow(r_mth)>0) round(r_mth$LOD[1],3) else NA
|
| 755 |
}
|
| 756 |
base
|
| 757 |
})
|
| 758 |
-
result <- do.call(rbind,summary_rows)
|
| 759 |
-
result[order(-result$N_Models_Detected,-result$Max_LOD),]
|
| 760 |
}
|
| 761 |
|
| 762 |
# ============================================================
|
|
@@ -802,7 +775,7 @@ ui <- fluidPage(
|
|
| 802 |
title = "πΏ QTL-Mapper v7 Enhanced",
|
| 803 |
id = "nav",
|
| 804 |
|
| 805 |
-
# ββ DATA UPLOAD βββββββββββββββββββββββββββββββββββββββββ
|
| 806 |
tabPanel("π Data Upload",
|
| 807 |
fluidRow(
|
| 808 |
column(4,
|
|
@@ -839,21 +812,21 @@ ui <- fluidPage(
|
|
| 839 |
div(class="info-box",
|
| 840 |
"6 models: SIM Β· CIM Β· MQM Β· RF Β· SVR Β· XGBoost",br(),
|
| 841 |
"Chromosome-wise Manhattan plots (no marker labels)",br(),
|
| 842 |
-
"Full plot customization available"
|
| 843 |
)
|
| 844 |
))
|
| 845 |
)
|
| 846 |
)
|
| 847 |
),
|
| 848 |
|
| 849 |
-
# ββ PLOT CUSTOMIZATION ββββββββββββββββββββββββββββββββββ
|
| 850 |
tabPanel("π¨ Plot Settings",
|
| 851 |
fluidRow(
|
| 852 |
column(6,
|
| 853 |
div(class="card", div(class="card-body",
|
| 854 |
-
div(class="section-title","
|
| 855 |
div(class="custom-section",
|
| 856 |
-
h5("
|
| 857 |
sliderInput("manhattan_height","Plot Height (pixels)",min=400,max=1200,value=800,step=50),
|
| 858 |
sliderInput("manhattan_width","Plot Width (pixels)",min=800,max=2000,value=1400,step=100)
|
| 859 |
),
|
|
@@ -877,7 +850,7 @@ ui <- fluidPage(
|
|
| 877 |
div(class="card", div(class="card-body",
|
| 878 |
div(class="section-title","πΊ Ideogram Plot Customization"),
|
| 879 |
div(class="custom-section",
|
| 880 |
-
h5("
|
| 881 |
sliderInput("ideogram_height","Plot Height (pixels)",min=400,max=1200,value=900,step=50),
|
| 882 |
sliderInput("ideogram_width","Plot Width (pixels)",min=800,max=2000,value=1400,step=100)
|
| 883 |
),
|
|
@@ -888,16 +861,16 @@ ui <- fluidPage(
|
|
| 888 |
colourpicker::colourInput("chr_border","Border Color","#1A4A1F")
|
| 889 |
),
|
| 890 |
div(class="custom-section",
|
| 891 |
-
h5("π΄ QTL Marker
|
| 892 |
sliderInput("qtl_marker_size_min","Min Marker Size",min=0.3,max=1.5,value=0.7,step=0.1),
|
| 893 |
-
sliderInput("qtl_marker_size_max","Max Marker Size",min=1,max=3,value=2.0,step=0.1)
|
| 894 |
)
|
| 895 |
))
|
| 896 |
)
|
| 897 |
)
|
| 898 |
),
|
| 899 |
|
| 900 |
-
# ββ RESULTS βββββββββββββββββββββββββββββββββββββββββββββ
|
| 901 |
tabPanel("π Results",
|
| 902 |
div(class="download-row",
|
| 903 |
downloadButton("dl_manhattan_png","πΌ Manhattan (PNG)",class="btn btn-info"),
|
|
@@ -908,8 +881,9 @@ ui <- fluidPage(
|
|
| 908 |
fluidRow(
|
| 909 |
column(12,
|
| 910 |
div(class="card", div(class="card-body",
|
| 911 |
-
div(class="section-title","π Chromosome-wise Manhattan Plot
|
| 912 |
-
|
|
|
|
| 913 |
))
|
| 914 |
)
|
| 915 |
),
|
|
@@ -917,7 +891,7 @@ ui <- fluidPage(
|
|
| 917 |
column(12,
|
| 918 |
div(class="card", div(class="card-body",
|
| 919 |
div(class="section-title","πΊ Chromosome Ideogram"),
|
| 920 |
-
|
| 921 |
))
|
| 922 |
)
|
| 923 |
),
|
|
@@ -929,7 +903,7 @@ ui <- fluidPage(
|
|
| 929 |
))
|
| 930 |
),
|
| 931 |
|
| 932 |
-
# ββ COMPARE MODELS βββββββββββββββββββββββββββββββββββββββ
|
| 933 |
tabPanel("βοΈ Compare Models",
|
| 934 |
div(class="card", div(class="card-body",
|
| 935 |
div(class="section-title","Select Models to Compare"),
|
|
@@ -940,7 +914,7 @@ ui <- fluidPage(
|
|
| 940 |
uiOutput("compare_status_ui")
|
| 941 |
)),
|
| 942 |
div(class="download-row",
|
| 943 |
-
downloadButton("dl_compare_idiogram_png","πΌ Compare
|
| 944 |
downloadButton("dl_compare_bar_png","π Compare Bar (PNG)",class="btn btn-info"),
|
| 945 |
downloadButton("dl_compare_csv","π₯ Compare Table (CSV)",class="btn btn-success")
|
| 946 |
),
|
|
@@ -966,7 +940,7 @@ ui <- fluidPage(
|
|
| 966 |
))
|
| 967 |
),
|
| 968 |
|
| 969 |
-
# ββ COMMON QTL βββββββββββββββββββββββββββββββββββββββββββ
|
| 970 |
tabPanel("π― Common QTL",
|
| 971 |
div(class="card", div(class="card-body",
|
| 972 |
div(class="section-title","Common QTL Across Models"),
|
|
@@ -1013,6 +987,13 @@ server <- function(input, output, session) {
|
|
| 1013 |
|
| 1014 |
lod_thr <- reactive(input$lod_threshold)
|
| 1015 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1016 |
# ββ DATASET INFO ββ
|
| 1017 |
output$dataset_info_ui <- renderUI({
|
| 1018 |
if (is.null(rv$geno)) return(div(class="info-box","π No genotype file loaded"))
|
|
@@ -1061,7 +1042,7 @@ server <- function(input, output, session) {
|
|
| 1061 |
tr_idx <- caret::createDataPartition(y,p=1-input$val_prop,list=FALSE)
|
| 1062 |
X_tr <- X[tr_idx,,drop=FALSE]; y_tr <- y[tr_idx]
|
| 1063 |
withProgress(message=paste("Scanning genome with",input$mapping_method),value=0.1,{
|
| 1064 |
-
rv$scan_all <- qtl_scan(X_tr,y_tr,rv$marker_info,
|
| 1065 |
method=input$mapping_method,
|
| 1066 |
n_cofactors=input$n_cofactors,
|
| 1067 |
window_size=input$window_size)
|
|
@@ -1088,7 +1069,8 @@ server <- function(input, output, session) {
|
|
| 1088 |
for (i in seq_along(input$compare_methods)) {
|
| 1089 |
mth <- input$compare_methods[i]
|
| 1090 |
incProgress(1/length(input$compare_methods),detail=mth)
|
| 1091 |
-
res <- tryCatch(qtl_scan(X_tr,y_tr,rv$marker_info,
|
|
|
|
| 1092 |
n_cofactors=input$n_cofactors,
|
| 1093 |
window_size=input$window_size),
|
| 1094 |
error=function(e) NULL)
|
|
@@ -1106,55 +1088,82 @@ server <- function(input, output, session) {
|
|
| 1106 |
paste(names(rv$compare_res),collapse=" Β· "))
|
| 1107 |
})
|
| 1108 |
|
| 1109 |
-
# ββ PLOTS ββ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1110 |
output$manhattan_plot <- renderPlotly({
|
| 1111 |
req(rv$scan_all)
|
| 1112 |
make_plotly_manhattan_chromosomewise(
|
| 1113 |
-
rv$scan_all,
|
| 1114 |
lod_thr(),
|
| 1115 |
-
paste(input$mapping_method,"Manhattan β Trait:",input$pheno_col),
|
| 1116 |
-
nonsig_color
|
| 1117 |
-
sig_color
|
| 1118 |
-
point_size
|
| 1119 |
-
sig_point_size
|
|
|
|
|
|
|
| 1120 |
)
|
| 1121 |
})
|
| 1122 |
|
| 1123 |
-
|
| 1124 |
-
req(markers)
|
| 1125 |
-
par(bg="white")
|
| 1126 |
-
make_chromosome_ideogram_reference(qtl_list, markers, threshold, title_str=title_str)
|
| 1127 |
-
}
|
| 1128 |
-
|
| 1129 |
output$idiogram_plot <- renderPlot({
|
| 1130 |
req(rv$scan_all, rv$all_markers)
|
| 1131 |
res_list <- list()
|
| 1132 |
-
if(!is.null(rv$scan_all)&&!"Message"%in%colnames(rv$scan_all))
|
| 1133 |
res_list[[input$mapping_method]] <- rv$scan_all
|
| 1134 |
-
|
| 1135 |
-
|
| 1136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1137 |
|
| 1138 |
-
|
|
|
|
| 1139 |
req(length(rv$compare_res)>0, rv$all_markers)
|
| 1140 |
par(bg="white")
|
| 1141 |
-
make_chromosome_ideogram_reference(
|
| 1142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1143 |
}, bg="white")
|
| 1144 |
|
|
|
|
| 1145 |
output$compare_bar <- renderPlotly({
|
| 1146 |
req(length(rv$compare_res)>0)
|
| 1147 |
counts <- sapply(rv$compare_res, function(r) {
|
| 1148 |
if(is.null(r)||"Message"%in%colnames(r)) return(0)
|
| 1149 |
sum(!is.na(r$LOD)&r$LOD>=lod_thr(),na.rm=TRUE)
|
| 1150 |
})
|
| 1151 |
-
df_bar <- data.frame(Model=names(counts),
|
| 1152 |
df_bar$Color <- MODEL_COLORS[df_bar$Model]
|
| 1153 |
df_bar$Color[is.na(df_bar$Color)] <- "#CC0000"
|
| 1154 |
plot_ly(df_bar, x=~Model, y=~N_QTL, type="bar",
|
| 1155 |
-
marker=list(color=~Color,
|
| 1156 |
text=~N_QTL, textposition="outside",
|
| 1157 |
-
hovertext=~paste0(Model,": ",N_QTL," QTLs"), hoverinfo="text")%>%
|
| 1158 |
layout(title=list(text=paste0("QTLs per Model (LOD β₯ ",lod_thr(),")"),
|
| 1159 |
font=list(size=14)),
|
| 1160 |
xaxis=list(title="",color="#111111"),
|
|
@@ -1171,7 +1180,8 @@ server <- function(input, output, session) {
|
|
| 1171 |
|
| 1172 |
output$common_qtl_summary_ui <- renderUI({
|
| 1173 |
cq <- common_qtl_data()
|
| 1174 |
-
if (is.null(cq)||nrow(cq)==0)
|
|
|
|
| 1175 |
div(class="info-box",
|
| 1176 |
tags$b(nrow(cq)), " common QTLs detected across β₯ ",
|
| 1177 |
tags$b(input$min_models), " models")
|
|
@@ -1188,20 +1198,27 @@ server <- function(input, output, session) {
|
|
| 1188 |
if (nrow(common_markers)>0) cq_list[[mth]] <- common_markers
|
| 1189 |
}
|
| 1190 |
par(bg="white")
|
| 1191 |
-
make_chromosome_ideogram_reference(
|
| 1192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1193 |
}, bg="white")
|
| 1194 |
|
| 1195 |
output$common_bar <- renderPlotly({
|
| 1196 |
cq <- common_qtl_data()
|
| 1197 |
req(!is.null(cq), nrow(cq)>0)
|
| 1198 |
df_bar <- cq[order(-cq$Max_LOD),][1:min(20,nrow(cq)),]
|
| 1199 |
-
plot_ly(df_bar, x=~Max_LOD, y=~reorder(Marker,Max_LOD),
|
| 1200 |
-
orientation="h",
|
| 1201 |
-
marker=list(color="#CC0000",
|
| 1202 |
text=~paste0(Marker," | ",N_Models_Detected," models"),
|
| 1203 |
hovertext=~paste0(Marker,"<br>Chr ",Chromosome," @ ",Position," cM<br>Max LOD: ",Max_LOD),
|
| 1204 |
-
hoverinfo="text")%>%
|
| 1205 |
layout(title=list(text="Top Common QTLs by Max LOD",font=list(size=14)),
|
| 1206 |
xaxis=list(title="Max LOD",color="#111111"),
|
| 1207 |
yaxis=list(title="",color="#111111"),
|
|
@@ -1212,13 +1229,14 @@ server <- function(input, output, session) {
|
|
| 1212 |
# ββ TABLES ββ
|
| 1213 |
output$qtl_table <- renderDT({
|
| 1214 |
req(rv$scan_all)
|
| 1215 |
-
if ("Message"%in%colnames(rv$scan_all))
|
|
|
|
| 1216 |
sig <- rv$scan_all[!is.na(rv$scan_all$LOD)&rv$scan_all$LOD>=lod_thr(),,drop=FALSE]
|
| 1217 |
-
if (nrow(sig)==0)
|
|
|
|
| 1218 |
sig <- add_lod_support_interval(sig,rv$scan_all,input$lod_drop)
|
| 1219 |
sig <- sig[order(-sig$LOD),]
|
| 1220 |
-
datatable(sig,options=list(scrollX=TRUE,pageLength=15),rownames=FALSE,
|
| 1221 |
-
class="compact stripe")
|
| 1222 |
})
|
| 1223 |
|
| 1224 |
output$compare_table <- renderDT({
|
|
@@ -1238,124 +1256,142 @@ server <- function(input, output, session) {
|
|
| 1238 |
|
| 1239 |
output$common_qtl_table <- renderDT({
|
| 1240 |
cq <- common_qtl_data()
|
| 1241 |
-
if(is.null(cq)||nrow(cq)==0)
|
|
|
|
| 1242 |
datatable(cq,options=list(scrollX=TRUE,pageLength=15),rownames=FALSE,class="compact stripe")
|
| 1243 |
})
|
| 1244 |
|
| 1245 |
# ============================================================
|
| 1246 |
-
# DOWNLOADS β PNG with custom settings
|
| 1247 |
# ============================================================
|
| 1248 |
|
| 1249 |
-
|
|
|
|
| 1250 |
width, height) {
|
| 1251 |
png(file, width=width, height=height, res=150, bg="white")
|
| 1252 |
-
make_chromosome_ideogram_reference(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1253 |
dev.off()
|
| 1254 |
}
|
| 1255 |
|
| 1256 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1257 |
output$dl_manhattan_png <- downloadHandler(
|
| 1258 |
-
filename=function() paste0("manhattan_chromwise_",input$mapping_method,"_",Sys.Date(),".png"),
|
| 1259 |
-
content=function(file) {
|
| 1260 |
req(rv$scan_all)
|
| 1261 |
-
pd <- rv$scan_all[!is.na(rv$scan_all$LOD)&!is.na(rv$scan_all$Position)&
|
| 1262 |
-
|
| 1263 |
-
|
|
|
|
| 1264 |
pd$Chromosome <- as.character(pd$Chromosome)
|
| 1265 |
-
pd$sig
|
| 1266 |
-
chroms
|
| 1267 |
-
|
| 1268 |
-
|
| 1269 |
-
|
| 1270 |
-
|
| 1271 |
-
|
| 1272 |
png(file, width=input$manhattan_width, height=input$manhattan_height, res=150, bg="white")
|
| 1273 |
par(mfrow=c(n_rows, n_cols), mar=c(4,4,3,1), bg="white")
|
| 1274 |
-
|
| 1275 |
for (chr in chroms) {
|
| 1276 |
-
chr_data <- pd[pd$Chromosome
|
| 1277 |
chr_data <- chr_data[order(chr_data$Position),]
|
| 1278 |
-
|
|
|
|
| 1279 |
plot(chr_data$Position, chr_data$LOD, type="n",
|
| 1280 |
-
xlab=paste("Chr",
|
| 1281 |
-
main=paste("Chr",
|
| 1282 |
frame.plot=FALSE, col.main="#111111", cex.main=0.9, cex.lab=0.8)
|
| 1283 |
-
|
| 1284 |
-
|
| 1285 |
-
nonsig <- chr_data[!chr_data$sig,]
|
| 1286 |
-
if(nrow(nonsig)>0)
|
| 1287 |
-
points(nonsig$Position, nonsig$LOD, pch=16,
|
| 1288 |
-
cex=input$nonsig_point_size/4, col=input$nonsig_color)
|
| 1289 |
-
|
| 1290 |
-
# Significant (NO LABELS)
|
| 1291 |
sig_data <- chr_data[chr_data$sig,]
|
| 1292 |
-
|
| 1293 |
-
|
| 1294 |
-
|
| 1295 |
-
|
| 1296 |
-
|
| 1297 |
-
|
| 1298 |
-
|
| 1299 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1300 |
lwd=input$threshold_width)
|
| 1301 |
}
|
| 1302 |
-
|
| 1303 |
dev.off()
|
| 1304 |
}
|
| 1305 |
)
|
| 1306 |
|
| 1307 |
# Ideogram PNG
|
| 1308 |
output$dl_idiogram_png <- downloadHandler(
|
| 1309 |
-
filename=function() paste0("idiogram_",input$mapping_method,"_",Sys.Date(),".png"),
|
| 1310 |
-
content=function(file) {
|
| 1311 |
req(rv$scan_all, rv$all_markers)
|
| 1312 |
res_list <- list()
|
| 1313 |
-
if(!is.null(rv$scan_all)&&!"Message"%in%colnames(rv$scan_all))
|
| 1314 |
res_list[[input$mapping_method]] <- rv$scan_all
|
| 1315 |
-
|
| 1316 |
-
paste(input$mapping_method,"β Trait:",input$pheno_col),
|
| 1317 |
file, input$ideogram_width, input$ideogram_height)
|
| 1318 |
}
|
| 1319 |
)
|
| 1320 |
|
| 1321 |
-
# Compare
|
| 1322 |
output$dl_compare_idiogram_png <- downloadHandler(
|
| 1323 |
-
filename=function() paste0("compare_idiogram_",Sys.Date(),".png"),
|
| 1324 |
-
content=function(file) {
|
| 1325 |
req(length(rv$compare_res)>0, rv$all_markers)
|
| 1326 |
-
|
| 1327 |
-
"Multi-Model QTL Comparison",
|
| 1328 |
-
input$ideogram_width, input$ideogram_height)
|
| 1329 |
}
|
| 1330 |
)
|
| 1331 |
|
| 1332 |
-
# Compare bar PNG
|
| 1333 |
output$dl_compare_bar_png <- downloadHandler(
|
| 1334 |
-
filename=function() paste0("compare_bar_",Sys.Date(),".png"),
|
| 1335 |
-
content=function(file) {
|
| 1336 |
req(length(rv$compare_res)>0)
|
| 1337 |
counts <- sapply(rv$compare_res, function(r){
|
| 1338 |
if(is.null(r)||"Message"%in%colnames(r)) return(0)
|
| 1339 |
-
sum(!is.na(r$LOD)&r$LOD>=lod_thr(),na.rm=TRUE)
|
|
|
|
| 1340 |
df_bar <- data.frame(Model=names(counts),N_QTL=as.numeric(counts))
|
| 1341 |
df_bar$Color <- MODEL_COLORS[df_bar$Model]
|
| 1342 |
df_bar$Color[is.na(df_bar$Color)] <- "#CC0000"
|
| 1343 |
-
|
|
|
|
|
|
|
| 1344 |
par(mar=c(6,5,4,2), bg="white")
|
| 1345 |
bp <- barplot(df_bar$N_QTL, names.arg=df_bar$Model,
|
| 1346 |
-
|
| 1347 |
-
|
| 1348 |
-
|
| 1349 |
-
|
| 1350 |
-
text(bp, df_bar$N_QTL+0.1, df_bar$N_QTL, cex=0.8, font=2, col="#111111")
|
| 1351 |
dev.off()
|
| 1352 |
}
|
| 1353 |
)
|
| 1354 |
|
| 1355 |
# Common ideogram PNG
|
| 1356 |
output$dl_common_idiogram_png <- downloadHandler(
|
| 1357 |
-
filename=function() paste0("common_qtl_idiogram_",Sys.Date(),".png"),
|
| 1358 |
-
content=function(file) {
|
| 1359 |
cq <- common_qtl_data()
|
| 1360 |
req(!is.null(cq), nrow(cq)>0, rv$all_markers)
|
| 1361 |
cq_list <- list()
|
|
@@ -1365,16 +1401,16 @@ server <- function(input, output, session) {
|
|
| 1365 |
cm <- r[r$Marker %in% cq$Marker,]
|
| 1366 |
if(nrow(cm)>0) cq_list[[mth]] <- cm
|
| 1367 |
}
|
| 1368 |
-
|
| 1369 |
-
paste0("Common QTL (β₯",input$min_models," models)"),
|
| 1370 |
-
input$ideogram_width, input$ideogram_height)
|
| 1371 |
}
|
| 1372 |
)
|
| 1373 |
|
| 1374 |
# ββ CSV DOWNLOADS ββ
|
| 1375 |
output$dl_qtl_csv <- downloadHandler(
|
| 1376 |
-
filename=function() paste0("qtl_significant_",input$mapping_method,"_",Sys.Date(),".csv"),
|
| 1377 |
-
content=function(file) {
|
| 1378 |
req(rv$scan_all)
|
| 1379 |
if("Message"%in%colnames(rv$scan_all)){write.csv(rv$scan_all,file,row.names=FALSE);return()}
|
| 1380 |
sig <- rv$scan_all[!is.na(rv$scan_all$LOD)&rv$scan_all$LOD>=lod_thr(),,drop=FALSE]
|
|
@@ -1384,16 +1420,16 @@ server <- function(input, output, session) {
|
|
| 1384 |
)
|
| 1385 |
|
| 1386 |
output$dl_full_csv <- downloadHandler(
|
| 1387 |
-
filename=function() paste0("qtl_full_scan_",input$mapping_method,"_",Sys.Date(),".csv"),
|
| 1388 |
-
content=function(file) {
|
| 1389 |
req(rv$scan_all)
|
| 1390 |
write.csv(rv$scan_all,file,row.names=FALSE)
|
| 1391 |
}
|
| 1392 |
)
|
| 1393 |
|
| 1394 |
output$dl_compare_csv <- downloadHandler(
|
| 1395 |
-
filename=function() paste0("qtl_compare_",Sys.Date(),".csv"),
|
| 1396 |
-
content=function(file) {
|
| 1397 |
req(length(rv$compare_res)>0)
|
| 1398 |
all_res <- lapply(names(rv$compare_res), function(mth){
|
| 1399 |
r <- rv$compare_res[[mth]]
|
|
@@ -1401,15 +1437,15 @@ server <- function(input, output, session) {
|
|
| 1401 |
sig <- r[!is.na(r$LOD)&r$LOD>=lod_thr(),,drop=FALSE]
|
| 1402 |
if(nrow(sig)>0){sig$Model<-mth;sig} else NULL
|
| 1403 |
})
|
| 1404 |
-
all_res <- do.call(rbind, Filter(Negate(is.null),
|
| 1405 |
if(is.null(all_res)) all_res <- data.frame(Status="No significant QTLs")
|
| 1406 |
write.csv(all_res,file,row.names=FALSE)
|
| 1407 |
}
|
| 1408 |
)
|
| 1409 |
|
| 1410 |
output$dl_common_csv <- downloadHandler(
|
| 1411 |
-
filename=function() paste0("qtl_common_",Sys.Date(),".csv"),
|
| 1412 |
-
content=function(file) {
|
| 1413 |
cq <- common_qtl_data()
|
| 1414 |
if(is.null(cq)||nrow(cq)==0) cq <- data.frame(Status="No common QTLs")
|
| 1415 |
write.csv(cq,file,row.names=FALSE)
|
|
@@ -1417,5 +1453,5 @@ server <- function(input, output, session) {
|
|
| 1417 |
)
|
| 1418 |
}
|
| 1419 |
|
| 1420 |
-
# Run
|
| 1421 |
shinyApp(ui=ui, server=server)
|
|
|
|
| 1 |
# ============================================================
|
| 2 |
+
# QTL-Mapper v7 ENHANCED β FIXED VERSION
|
| 3 |
+
# Fixes:
|
| 4 |
+
# 1. Added colourpicker to required_packages
|
| 5 |
+
# 2. Customization controls now fully wired to plots & downloads
|
| 6 |
+
# 3. make_chromosome_ideogram_reference accepts color/size params
|
| 7 |
+
# 4. Manhattan plot height/width driven by sliders (uiOutput)
|
| 8 |
+
# 5. Ideogram width now respected in renderPlot
|
| 9 |
+
# 6. PNG downloads use custom dimensions & styles
|
| 10 |
+
# 7. threshold_style mapped correctly to base R lty values
|
| 11 |
+
# 8. compare_bar PNG uses custom dimensions
|
| 12 |
+
# 9. All colourInput references safe-guarded with defaults
|
| 13 |
# ============================================================
|
| 14 |
|
| 15 |
options(repos = c(CRAN = "https://cloud.r-project.org"))
|
| 16 |
|
| 17 |
required_packages <- c(
|
| 18 |
"shiny", "randomForest", "caret", "DT", "ggplot2", "plotly",
|
| 19 |
+
"dplyr", "glmnet", "shinyjs", "scales", "e1071", "xgboost",
|
| 20 |
+
"writexl", "colourpicker"
|
| 21 |
)
|
| 22 |
|
| 23 |
for (pkg in required_packages) {
|
|
|
|
| 28 |
}
|
| 29 |
|
| 30 |
# ============================================================
|
| 31 |
+
# 6 MODELS
|
| 32 |
# ============================================================
|
| 33 |
MAPPING_MODELS <- list(
|
| 34 |
"SIM" = list(label = "Simple Interval Mapping (SIM)", desc = "Single-marker regression; LOD = -log10(p)"),
|
|
|
|
| 39 |
"XGBOOST" = list(label = "XGBoost QTL", desc = "Gradient boosting; gain importance")
|
| 40 |
)
|
| 41 |
|
|
|
|
| 42 |
MODEL_COLORS <- c(
|
| 43 |
"SIM" = "#CC0000",
|
| 44 |
"CIM" = "#FF3333",
|
|
|
|
| 61 |
return(Inf)
|
| 62 |
}
|
| 63 |
|
| 64 |
+
# Safe colour getter with fallback
|
| 65 |
+
get_color <- function(input_val, fallback) {
|
| 66 |
+
if (is.null(input_val) || nchar(trimws(input_val)) == 0) return(fallback)
|
| 67 |
+
input_val
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
# ============================================================
|
| 71 |
# GENOTYPE LOADER
|
| 72 |
# ============================================================
|
|
|
|
| 157 |
}
|
| 158 |
|
| 159 |
# ============================================================
|
| 160 |
+
# CIM
|
| 161 |
# ============================================================
|
| 162 |
qtl_scan_cim_improved <- function(X_valid, y_valid, marker_info,
|
| 163 |
window_size = 10, n_cofactors = 5) {
|
|
|
|
| 478 |
}
|
| 479 |
|
| 480 |
# ============================================================
|
| 481 |
+
# CHROMOSOME IDEOGRAM β NOW ACCEPTS COLOR & SIZE PARAMS
|
| 482 |
# ============================================================
|
| 483 |
make_chromosome_ideogram_reference <- function(qtl_list, all_markers_df,
|
| 484 |
+
lod_threshold = 2.0,
|
| 485 |
+
model_colors = MODEL_COLORS,
|
| 486 |
+
title_str = NULL,
|
| 487 |
+
band_dark_col = "#2E6B35",
|
| 488 |
+
band_light_col = "#6AAB6E",
|
| 489 |
+
border_col = "#1A4A1F",
|
| 490 |
+
qtl_size_min = 0.7,
|
| 491 |
+
qtl_size_max = 2.0) {
|
| 492 |
if (is.null(all_markers_df) || nrow(all_markers_df) == 0) return(invisible(NULL))
|
| 493 |
|
| 494 |
chroms <- sort(unique(as.character(all_markers_df$Chromosome)))
|
|
|
|
| 505 |
ylim=c(-8, max_pos * 1.12),
|
| 506 |
xaxt="n", yaxt="n", xlab="", ylab="", main="", frame.plot=FALSE, bty="n")
|
| 507 |
|
|
|
|
| 508 |
y_ticks <- pretty(c(0, max_pos), n=7)
|
| 509 |
axis(2, at=y_ticks, labels=y_ticks, cex.axis=0.72, las=1,
|
| 510 |
col="#555555", col.axis="#333333", tcl=-0.3, lwd=0.8)
|
| 511 |
mtext("Genetic Position (cM)", side=2, line=2.5, cex=0.82, col="#222222", font=2)
|
| 512 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 513 |
for (ci in seq_along(chroms)) {
|
| 514 |
chr <- chroms[ci]
|
| 515 |
x_center <- (ci - 1) * x_gap + 0.9
|
|
|
|
| 519 |
chr_len <- max(chr_m$Position, na.rm=TRUE)
|
| 520 |
chr_min <- min(chr_m$Position, na.rm=TRUE)
|
| 521 |
|
|
|
|
| 522 |
n_bands <- max(8, round((chr_len - chr_min) / 8))
|
| 523 |
band_h <- (chr_len - chr_min) / n_bands
|
| 524 |
|
| 525 |
for (bi in 1:n_bands) {
|
| 526 |
band_bot <- chr_min + (bi-1)*band_h
|
| 527 |
band_top <- chr_min + bi*band_h
|
| 528 |
+
bcol <- if (bi %% 2 == 1) band_dark_col else band_light_col
|
| 529 |
rect(x_center - bar_w/2, band_bot, x_center + bar_w/2, band_top,
|
| 530 |
col=bcol, border=NA)
|
| 531 |
}
|
| 532 |
|
| 533 |
rect(x_center - bar_w/2, chr_min, x_center + bar_w/2, chr_len,
|
| 534 |
+
col=NA, border=border_col, lwd=1.8)
|
| 535 |
|
| 536 |
symbols(x_center, chr_min, circles=bar_w/2, inches=FALSE,
|
| 537 |
+
add=TRUE, fg=border_col, bg=band_light_col, lwd=1.2)
|
| 538 |
symbols(x_center, chr_len, circles=bar_w/2, inches=FALSE,
|
| 539 |
+
add=TRUE, fg=border_col, bg=band_dark_col, lwd=1.2)
|
| 540 |
|
|
|
|
| 541 |
cen_pos <- chr_min + (chr_len - chr_min)*0.42
|
| 542 |
polygon(c(x_center-bar_w/2, x_center, x_center+bar_w/2, x_center),
|
| 543 |
c(cen_pos - chr_len*0.025, cen_pos,
|
| 544 |
cen_pos - chr_len*0.025, cen_pos - chr_len*0.05),
|
| 545 |
+
col="white", border=border_col, lwd=1.2)
|
| 546 |
polygon(c(x_center-bar_w/2, x_center, x_center+bar_w/2, x_center),
|
| 547 |
c(cen_pos + chr_len*0.025, cen_pos,
|
| 548 |
cen_pos + chr_len*0.025, cen_pos + chr_len*0.05),
|
| 549 |
+
col="white", border=border_col, lwd=1.2)
|
| 550 |
|
|
|
|
| 551 |
n_models <- length(model_names)
|
| 552 |
for (ti in seq_along(model_names)) {
|
| 553 |
trait <- model_names[ti]
|
|
|
|
| 559 |
qtl_data$LOD >= lod_threshold, ]
|
| 560 |
if (nrow(sig_qtls)==0) next
|
| 561 |
|
| 562 |
+
col_m <- model_colors[trait]
|
| 563 |
if (is.na(col_m)) col_m <- "#CC0000"
|
| 564 |
|
| 565 |
+
x_off <- x_center + bar_w/2 + 0.08 + (ti-1)*0.22
|
| 566 |
|
| 567 |
for (ri in 1:nrow(sig_qtls)) {
|
| 568 |
pos <- sig_qtls$Position[ri]
|
|
|
|
| 571 |
segments(x_center + bar_w/2, pos, x_off - 0.04, pos,
|
| 572 |
col=col_m, lwd=1.5)
|
| 573 |
points(x_off, pos, pch=16, col=col_m,
|
| 574 |
+
cex=pmax(qtl_size_min, pmin(qtl_size_max, 0.6 + lod_val/8)))
|
| 575 |
text(x_off + 0.15, pos,
|
| 576 |
paste0(round(lod_val,1)),
|
| 577 |
cex=0.55, col=col_m, font=2, adj=0)
|
|
|
|
| 582 |
cex=0.72, font=2, col="#1A1A1A", adj=0.5)
|
| 583 |
}
|
| 584 |
|
|
|
|
| 585 |
legend_x <- n_chr * x_gap + 0.5
|
| 586 |
legend_y <- max_pos * 0.95
|
| 587 |
text(legend_x, legend_y + max_pos*0.07, "Models", cex=0.75, font=2, col="#1A1A1A", adj=0)
|
| 588 |
for (ti in seq_along(model_names)) {
|
| 589 |
+
tr <- model_names[ti]
|
| 590 |
+
col_m <- model_colors[tr]; if (is.na(col_m)) col_m <- "#CC0000"
|
| 591 |
y_leg <- legend_y - (ti-1)*max_pos*0.07
|
| 592 |
qtl_d <- qtl_list[[tr]]
|
| 593 |
n_sig <- if(!is.null(qtl_d)&&!"Message"%in%colnames(qtl_d))
|
|
|
|
| 603 |
}
|
| 604 |
|
| 605 |
# ============================================================
|
| 606 |
+
# CHROMOSOME-WISE MANHATTAN PLOT (PLOTLY) β FULLY CUSTOMIZABLE
|
| 607 |
# ============================================================
|
| 608 |
+
make_plotly_manhattan_chromosomewise <- function(scan_all, lod_threshold=2.0,
|
| 609 |
title="QTL Manhattan Plot",
|
| 610 |
nonsig_color="#3A7D44",
|
| 611 |
sig_color="#CC0000",
|
| 612 |
point_size=6,
|
| 613 |
+
sig_point_size=10,
|
| 614 |
+
threshold_line_width=1.5,
|
| 615 |
+
threshold_line_style="dash") {
|
| 616 |
blank <- plot_ly() %>% layout(title=title, plot_bgcolor="white", paper_bgcolor="white")
|
| 617 |
if (is.null(scan_all) || "Message" %in% colnames(scan_all)) return(blank)
|
| 618 |
+
|
| 619 |
pd <- scan_all[!is.na(scan_all$LOD) & !is.na(scan_all$Position) & !is.na(scan_all$Chromosome), ]
|
| 620 |
if (nrow(pd) == 0) return(blank)
|
| 621 |
+
|
| 622 |
pd$Chromosome <- as.character(pd$Chromosome)
|
| 623 |
pd$sig <- pd$LOD >= lod_threshold
|
| 624 |
+
|
| 625 |
chroms <- sort(unique(pd$Chromosome))
|
|
|
|
|
|
|
| 626 |
subplot_list <- list()
|
| 627 |
+
|
| 628 |
for (chr in chroms) {
|
| 629 |
chr_data <- pd[pd$Chromosome == chr, ]
|
| 630 |
chr_data <- chr_data[order(chr_data$Position), ]
|
| 631 |
+
nonsig <- chr_data[!chr_data$sig, ]
|
|
|
|
|
|
|
|
|
|
| 632 |
sig_data <- chr_data[chr_data$sig, ]
|
| 633 |
+
|
| 634 |
p <- plot_ly()
|
| 635 |
+
|
|
|
|
| 636 |
if (nrow(nonsig) > 0) {
|
| 637 |
p <- p %>% add_trace(
|
| 638 |
+
data = nonsig, x = ~Position, y = ~LOD,
|
| 639 |
+
type = "scatter", mode = "markers",
|
|
|
|
|
|
|
|
|
|
| 640 |
marker = list(size = point_size, color = nonsig_color, opacity = 0.7),
|
| 641 |
+
text = ~paste0("<b>", Marker, "</b><br>Chr: ", Chromosome,
|
| 642 |
+
"<br>Pos: ", round(Position, 1), " cM<br>LOD: ", round(LOD, 2),
|
| 643 |
+
"<br>PVE: ", round(PVE_Marker_Percent, 1), "%"),
|
| 644 |
+
hoverinfo = "text", showlegend = FALSE, name = paste("Chr", chr)
|
|
|
|
|
|
|
| 645 |
)
|
| 646 |
}
|
| 647 |
+
|
|
|
|
| 648 |
if (nrow(sig_data) > 0) {
|
| 649 |
p <- p %>% add_trace(
|
| 650 |
+
data = sig_data, x = ~Position, y = ~LOD,
|
| 651 |
+
type = "scatter", mode = "markers",
|
| 652 |
+
marker = list(size = sig_point_size, color = sig_color,
|
| 653 |
+
symbol = "diamond", line = list(width = 1, color = "#660000")),
|
|
|
|
|
|
|
|
|
|
| 654 |
text = ~paste0("<b>", Marker, "</b><br>Chr: ", Chromosome,
|
| 655 |
+
"<br>Pos: ", round(Position, 1), " cM<br>LOD: ", round(LOD, 2),
|
| 656 |
+
"<br>PVE: ", round(PVE_Marker_Percent, 1), "%"),
|
| 657 |
+
hoverinfo = "text", showlegend = FALSE, name = paste("Chr", chr, "Significant")
|
|
|
|
|
|
|
| 658 |
)
|
| 659 |
}
|
| 660 |
+
|
|
|
|
| 661 |
p <- p %>% add_segments(
|
| 662 |
+
x = min(chr_data$Position, na.rm=TRUE), xend = max(chr_data$Position, na.rm=TRUE),
|
| 663 |
+
y = lod_threshold, yend = lod_threshold,
|
| 664 |
+
line = list(color = sig_color, dash = threshold_line_style, width = threshold_line_width),
|
| 665 |
+
showlegend = FALSE, hoverinfo = "none"
|
|
|
|
|
|
|
|
|
|
| 666 |
)
|
| 667 |
+
|
|
|
|
| 668 |
p <- p %>% layout(
|
| 669 |
+
xaxis = list(title = paste("Chr", chr, "Position (cM)"), gridcolor = "#EEEEEE", zeroline = FALSE),
|
| 670 |
+
yaxis = list(title = if(chr == chroms[1]) "LOD Score" else "", gridcolor = "#EEEEEE", zeroline = FALSE),
|
| 671 |
+
plot_bgcolor = "white", paper_bgcolor = "white"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 672 |
)
|
| 673 |
+
|
| 674 |
subplot_list[[chr]] <- p
|
| 675 |
}
|
| 676 |
+
|
|
|
|
| 677 |
n_chroms <- length(chroms)
|
| 678 |
+
n_cols <- ceiling(sqrt(n_chroms))
|
| 679 |
+
n_rows <- ceiling(n_chroms / n_cols)
|
| 680 |
+
|
| 681 |
combined_plot <- subplot(
|
| 682 |
+
subplot_list, nrows = n_rows, shareY = TRUE,
|
| 683 |
+
titleX = TRUE, titleY = TRUE, margin = 0.05
|
|
|
|
|
|
|
|
|
|
|
|
|
| 684 |
)
|
| 685 |
+
|
| 686 |
combined_plot %>% layout(
|
| 687 |
title = list(
|
| 688 |
+
text = paste0(title, " | ", sum(pd$sig, na.rm=TRUE), " significant QTLs"),
|
| 689 |
font = list(size = 14, color = "#111111")
|
| 690 |
),
|
| 691 |
showlegend = FALSE,
|
|
|
|
| 714 |
common <- all_sig[all_sig$N_Models_Detected>=min_models,]
|
| 715 |
if (nrow(common)==0) return(NULL)
|
| 716 |
markers_uniq <- unique(common$Marker)
|
| 717 |
+
summary_rows <- lapply(markers_uniq, function(mk) {
|
| 718 |
rows <- common[common$Marker==mk,]
|
| 719 |
+
base <- rows[which.max(rows$LOD), c("Marker","Chromosome","Position","N_Models_Detected")]
|
| 720 |
+
base$Models_List <- paste(sort(unique(rows$Model)), collapse=", ")
|
| 721 |
+
base$Mean_LOD <- round(mean(rows$LOD, na.rm=TRUE), 3)
|
| 722 |
+
base$Max_LOD <- round(max(rows$LOD, na.rm=TRUE), 3)
|
| 723 |
+
base$Mean_PVE <- round(mean(rows$PVE_Marker_Percent, na.rm=TRUE), 3)
|
| 724 |
+
base$Mean_Add_Eff <- round(mean(rows$Additive_Effect, na.rm=TRUE), 3)
|
| 725 |
+
for (mth in names(compare_res)) {
|
| 726 |
r_mth <- rows[rows$Model==mth,]
|
| 727 |
base[[paste0("LOD_",mth)]] <- if(nrow(r_mth)>0) round(r_mth$LOD[1],3) else NA
|
| 728 |
}
|
| 729 |
base
|
| 730 |
})
|
| 731 |
+
result <- do.call(rbind, summary_rows)
|
| 732 |
+
result[order(-result$N_Models_Detected, -result$Max_LOD),]
|
| 733 |
}
|
| 734 |
|
| 735 |
# ============================================================
|
|
|
|
| 775 |
title = "πΏ QTL-Mapper v7 Enhanced",
|
| 776 |
id = "nav",
|
| 777 |
|
| 778 |
+
# ββ DATA UPLOAD βββββββββββββββββββββββββββββββββββββββββ
|
| 779 |
tabPanel("π Data Upload",
|
| 780 |
fluidRow(
|
| 781 |
column(4,
|
|
|
|
| 812 |
div(class="info-box",
|
| 813 |
"6 models: SIM Β· CIM Β· MQM Β· RF Β· SVR Β· XGBoost",br(),
|
| 814 |
"Chromosome-wise Manhattan plots (no marker labels)",br(),
|
| 815 |
+
"Full plot customization available in Plot Settings tab"
|
| 816 |
)
|
| 817 |
))
|
| 818 |
)
|
| 819 |
)
|
| 820 |
),
|
| 821 |
|
| 822 |
+
# ββ PLOT CUSTOMIZATION ββββββββββββββββββββββββββββββββββ
|
| 823 |
tabPanel("π¨ Plot Settings",
|
| 824 |
fluidRow(
|
| 825 |
column(6,
|
| 826 |
div(class="card", div(class="card-body",
|
| 827 |
+
div(class="section-title","π Manhattan Plot Customization"),
|
| 828 |
div(class="custom-section",
|
| 829 |
+
h5("π Plot Dimensions"),
|
| 830 |
sliderInput("manhattan_height","Plot Height (pixels)",min=400,max=1200,value=800,step=50),
|
| 831 |
sliderInput("manhattan_width","Plot Width (pixels)",min=800,max=2000,value=1400,step=100)
|
| 832 |
),
|
|
|
|
| 850 |
div(class="card", div(class="card-body",
|
| 851 |
div(class="section-title","πΊ Ideogram Plot Customization"),
|
| 852 |
div(class="custom-section",
|
| 853 |
+
h5("π Plot Dimensions"),
|
| 854 |
sliderInput("ideogram_height","Plot Height (pixels)",min=400,max=1200,value=900,step=50),
|
| 855 |
sliderInput("ideogram_width","Plot Width (pixels)",min=800,max=2000,value=1400,step=100)
|
| 856 |
),
|
|
|
|
| 861 |
colourpicker::colourInput("chr_border","Border Color","#1A4A1F")
|
| 862 |
),
|
| 863 |
div(class="custom-section",
|
| 864 |
+
h5("π΄ QTL Marker Sizing"),
|
| 865 |
sliderInput("qtl_marker_size_min","Min Marker Size",min=0.3,max=1.5,value=0.7,step=0.1),
|
| 866 |
+
sliderInput("qtl_marker_size_max","Max Marker Size",min=1.0,max=3.0,value=2.0,step=0.1)
|
| 867 |
)
|
| 868 |
))
|
| 869 |
)
|
| 870 |
)
|
| 871 |
),
|
| 872 |
|
| 873 |
+
# ββ RESULTS βββββββββββββββββββββββββββββββββββββββββββββ
|
| 874 |
tabPanel("π Results",
|
| 875 |
div(class="download-row",
|
| 876 |
downloadButton("dl_manhattan_png","πΌ Manhattan (PNG)",class="btn btn-info"),
|
|
|
|
| 881 |
fluidRow(
|
| 882 |
column(12,
|
| 883 |
div(class="card", div(class="card-body",
|
| 884 |
+
div(class="section-title","π Chromosome-wise Manhattan Plot"),
|
| 885 |
+
# Dynamic height driven by slider via uiOutput
|
| 886 |
+
uiOutput("manhattan_plot_ui")
|
| 887 |
))
|
| 888 |
)
|
| 889 |
),
|
|
|
|
| 891 |
column(12,
|
| 892 |
div(class="card", div(class="card-body",
|
| 893 |
div(class="section-title","πΊ Chromosome Ideogram"),
|
| 894 |
+
uiOutput("idiogram_plot_ui")
|
| 895 |
))
|
| 896 |
)
|
| 897 |
),
|
|
|
|
| 903 |
))
|
| 904 |
),
|
| 905 |
|
| 906 |
+
# ββ COMPARE MODELS βββββββββββββββββββββββββββββββββββββββ
|
| 907 |
tabPanel("βοΈ Compare Models",
|
| 908 |
div(class="card", div(class="card-body",
|
| 909 |
div(class="section-title","Select Models to Compare"),
|
|
|
|
| 914 |
uiOutput("compare_status_ui")
|
| 915 |
)),
|
| 916 |
div(class="download-row",
|
| 917 |
+
downloadButton("dl_compare_idiogram_png","πΌ Compare Ideogram (PNG)",class="btn btn-info"),
|
| 918 |
downloadButton("dl_compare_bar_png","π Compare Bar (PNG)",class="btn btn-info"),
|
| 919 |
downloadButton("dl_compare_csv","π₯ Compare Table (CSV)",class="btn btn-success")
|
| 920 |
),
|
|
|
|
| 940 |
))
|
| 941 |
),
|
| 942 |
|
| 943 |
+
# ββ COMMON QTL βββββββββββββββββββββββββββββββββββββββββββ
|
| 944 |
tabPanel("π― Common QTL",
|
| 945 |
div(class="card", div(class="card-body",
|
| 946 |
div(class="section-title","Common QTL Across Models"),
|
|
|
|
| 987 |
|
| 988 |
lod_thr <- reactive(input$lod_threshold)
|
| 989 |
|
| 990 |
+
# ββ Safe color helpers (with fallbacks) ββ
|
| 991 |
+
col_nonsig <- reactive(get_color(input$nonsig_color, "#3A7D44"))
|
| 992 |
+
col_sig <- reactive(get_color(input$sig_color, "#CC0000"))
|
| 993 |
+
col_chr_dark <- reactive(get_color(input$chr_dark, "#2E6B35"))
|
| 994 |
+
col_chr_light <- reactive(get_color(input$chr_light, "#6AAB6E"))
|
| 995 |
+
col_chr_border <- reactive(get_color(input$chr_border, "#1A4A1F"))
|
| 996 |
+
|
| 997 |
# ββ DATASET INFO ββ
|
| 998 |
output$dataset_info_ui <- renderUI({
|
| 999 |
if (is.null(rv$geno)) return(div(class="info-box","π No genotype file loaded"))
|
|
|
|
| 1042 |
tr_idx <- caret::createDataPartition(y,p=1-input$val_prop,list=FALSE)
|
| 1043 |
X_tr <- X[tr_idx,,drop=FALSE]; y_tr <- y[tr_idx]
|
| 1044 |
withProgress(message=paste("Scanning genome with",input$mapping_method),value=0.1,{
|
| 1045 |
+
rv$scan_all <- qtl_scan(X_tr, y_tr, rv$marker_info,
|
| 1046 |
method=input$mapping_method,
|
| 1047 |
n_cofactors=input$n_cofactors,
|
| 1048 |
window_size=input$window_size)
|
|
|
|
| 1069 |
for (i in seq_along(input$compare_methods)) {
|
| 1070 |
mth <- input$compare_methods[i]
|
| 1071 |
incProgress(1/length(input$compare_methods),detail=mth)
|
| 1072 |
+
res <- tryCatch(qtl_scan(X_tr, y_tr, rv$marker_info,
|
| 1073 |
+
method=mth,
|
| 1074 |
n_cofactors=input$n_cofactors,
|
| 1075 |
window_size=input$window_size),
|
| 1076 |
error=function(e) NULL)
|
|
|
|
| 1088 |
paste(names(rv$compare_res),collapse=" Β· "))
|
| 1089 |
})
|
| 1090 |
|
| 1091 |
+
# ββ DYNAMIC UI FOR PLOTS (height/width from sliders) ββ
|
| 1092 |
+
output$manhattan_plot_ui <- renderUI({
|
| 1093 |
+
h <- if(!is.null(input$manhattan_height)) input$manhattan_height else 800
|
| 1094 |
+
plotlyOutput("manhattan_plot", height=paste0(h,"px"))
|
| 1095 |
+
})
|
| 1096 |
+
|
| 1097 |
+
output$idiogram_plot_ui <- renderUI({
|
| 1098 |
+
h <- if(!is.null(input$ideogram_height)) input$ideogram_height else 900
|
| 1099 |
+
plotOutput("idiogram_plot", height=paste0(h,"px"))
|
| 1100 |
+
})
|
| 1101 |
+
|
| 1102 |
+
# ββ MANHATTAN PLOT (interactive, fully customised) ββ
|
| 1103 |
output$manhattan_plot <- renderPlotly({
|
| 1104 |
req(rv$scan_all)
|
| 1105 |
make_plotly_manhattan_chromosomewise(
|
| 1106 |
+
rv$scan_all,
|
| 1107 |
lod_thr(),
|
| 1108 |
+
paste(input$mapping_method, "Manhattan β Trait:", input$pheno_col),
|
| 1109 |
+
nonsig_color = col_nonsig(),
|
| 1110 |
+
sig_color = col_sig(),
|
| 1111 |
+
point_size = input$nonsig_point_size,
|
| 1112 |
+
sig_point_size = input$sig_point_size,
|
| 1113 |
+
threshold_line_width = input$threshold_width,
|
| 1114 |
+
threshold_line_style = input$threshold_style
|
| 1115 |
)
|
| 1116 |
})
|
| 1117 |
|
| 1118 |
+
# ββ IDEOGRAM PLOT (base R, fully customised) ββ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1119 |
output$idiogram_plot <- renderPlot({
|
| 1120 |
req(rv$scan_all, rv$all_markers)
|
| 1121 |
res_list <- list()
|
| 1122 |
+
if (!is.null(rv$scan_all) && !"Message" %in% colnames(rv$scan_all))
|
| 1123 |
res_list[[input$mapping_method]] <- rv$scan_all
|
| 1124 |
+
par(bg="white")
|
| 1125 |
+
make_chromosome_ideogram_reference(
|
| 1126 |
+
res_list, rv$all_markers, lod_thr(),
|
| 1127 |
+
title_str = paste(input$mapping_method, "β Trait:", input$pheno_col),
|
| 1128 |
+
band_dark_col = col_chr_dark(),
|
| 1129 |
+
band_light_col = col_chr_light(),
|
| 1130 |
+
border_col = col_chr_border(),
|
| 1131 |
+
qtl_size_min = input$qtl_marker_size_min,
|
| 1132 |
+
qtl_size_max = input$qtl_marker_size_max
|
| 1133 |
+
)
|
| 1134 |
+
}, bg="white",
|
| 1135 |
+
width = function() if(!is.null(input$ideogram_width)) input$ideogram_width else 1400,
|
| 1136 |
+
height = function() if(!is.null(input$ideogram_height)) input$ideogram_height else 900)
|
| 1137 |
|
| 1138 |
+
# ββ COMPARE IDEOGRAM ββ
|
| 1139 |
+
output$compare_ideogram <- renderPlot({
|
| 1140 |
req(length(rv$compare_res)>0, rv$all_markers)
|
| 1141 |
par(bg="white")
|
| 1142 |
+
make_chromosome_ideogram_reference(
|
| 1143 |
+
rv$compare_res, rv$all_markers, lod_thr(),
|
| 1144 |
+
title_str = "Multi-Model QTL Comparison",
|
| 1145 |
+
band_dark_col = col_chr_dark(),
|
| 1146 |
+
band_light_col = col_chr_light(),
|
| 1147 |
+
border_col = col_chr_border(),
|
| 1148 |
+
qtl_size_min = input$qtl_marker_size_min,
|
| 1149 |
+
qtl_size_max = input$qtl_marker_size_max
|
| 1150 |
+
)
|
| 1151 |
}, bg="white")
|
| 1152 |
|
| 1153 |
+
# ββ COMPARE BAR CHART ββ
|
| 1154 |
output$compare_bar <- renderPlotly({
|
| 1155 |
req(length(rv$compare_res)>0)
|
| 1156 |
counts <- sapply(rv$compare_res, function(r) {
|
| 1157 |
if(is.null(r)||"Message"%in%colnames(r)) return(0)
|
| 1158 |
sum(!is.na(r$LOD)&r$LOD>=lod_thr(),na.rm=TRUE)
|
| 1159 |
})
|
| 1160 |
+
df_bar <- data.frame(Model=names(counts),N_QTL=as.numeric(counts))
|
| 1161 |
df_bar$Color <- MODEL_COLORS[df_bar$Model]
|
| 1162 |
df_bar$Color[is.na(df_bar$Color)] <- "#CC0000"
|
| 1163 |
plot_ly(df_bar, x=~Model, y=~N_QTL, type="bar",
|
| 1164 |
+
marker=list(color=~Color,line=list(color="#330000",width=1)),
|
| 1165 |
text=~N_QTL, textposition="outside",
|
| 1166 |
+
hovertext=~paste0(Model,": ",N_QTL," QTLs"), hoverinfo="text") %>%
|
| 1167 |
layout(title=list(text=paste0("QTLs per Model (LOD β₯ ",lod_thr(),")"),
|
| 1168 |
font=list(size=14)),
|
| 1169 |
xaxis=list(title="",color="#111111"),
|
|
|
|
| 1180 |
|
| 1181 |
output$common_qtl_summary_ui <- renderUI({
|
| 1182 |
cq <- common_qtl_data()
|
| 1183 |
+
if (is.null(cq)||nrow(cq)==0)
|
| 1184 |
+
return(div(class="info-box","β οΈ No common QTLs β run Compare Models first"))
|
| 1185 |
div(class="info-box",
|
| 1186 |
tags$b(nrow(cq)), " common QTLs detected across β₯ ",
|
| 1187 |
tags$b(input$min_models), " models")
|
|
|
|
| 1198 |
if (nrow(common_markers)>0) cq_list[[mth]] <- common_markers
|
| 1199 |
}
|
| 1200 |
par(bg="white")
|
| 1201 |
+
make_chromosome_ideogram_reference(
|
| 1202 |
+
cq_list, rv$all_markers, lod_thr(),
|
| 1203 |
+
title_str = "Common QTL (across β₯2 models)",
|
| 1204 |
+
band_dark_col = col_chr_dark(),
|
| 1205 |
+
band_light_col = col_chr_light(),
|
| 1206 |
+
border_col = col_chr_border(),
|
| 1207 |
+
qtl_size_min = input$qtl_marker_size_min,
|
| 1208 |
+
qtl_size_max = input$qtl_marker_size_max
|
| 1209 |
+
)
|
| 1210 |
}, bg="white")
|
| 1211 |
|
| 1212 |
output$common_bar <- renderPlotly({
|
| 1213 |
cq <- common_qtl_data()
|
| 1214 |
req(!is.null(cq), nrow(cq)>0)
|
| 1215 |
df_bar <- cq[order(-cq$Max_LOD),][1:min(20,nrow(cq)),]
|
| 1216 |
+
plot_ly(df_bar, x=~Max_LOD, y=~reorder(Marker,Max_LOD),
|
| 1217 |
+
type="bar", orientation="h",
|
| 1218 |
+
marker=list(color="#CC0000",line=list(color="#660000",width=1)),
|
| 1219 |
text=~paste0(Marker," | ",N_Models_Detected," models"),
|
| 1220 |
hovertext=~paste0(Marker,"<br>Chr ",Chromosome," @ ",Position," cM<br>Max LOD: ",Max_LOD),
|
| 1221 |
+
hoverinfo="text") %>%
|
| 1222 |
layout(title=list(text="Top Common QTLs by Max LOD",font=list(size=14)),
|
| 1223 |
xaxis=list(title="Max LOD",color="#111111"),
|
| 1224 |
yaxis=list(title="",color="#111111"),
|
|
|
|
| 1229 |
# ββ TABLES ββ
|
| 1230 |
output$qtl_table <- renderDT({
|
| 1231 |
req(rv$scan_all)
|
| 1232 |
+
if ("Message"%in%colnames(rv$scan_all))
|
| 1233 |
+
return(datatable(data.frame(Status=rv$scan_all$Message[1])))
|
| 1234 |
sig <- rv$scan_all[!is.na(rv$scan_all$LOD)&rv$scan_all$LOD>=lod_thr(),,drop=FALSE]
|
| 1235 |
+
if (nrow(sig)==0)
|
| 1236 |
+
return(datatable(data.frame(Status="No significant QTLs at current threshold")))
|
| 1237 |
sig <- add_lod_support_interval(sig,rv$scan_all,input$lod_drop)
|
| 1238 |
sig <- sig[order(-sig$LOD),]
|
| 1239 |
+
datatable(sig,options=list(scrollX=TRUE,pageLength=15),rownames=FALSE,class="compact stripe")
|
|
|
|
| 1240 |
})
|
| 1241 |
|
| 1242 |
output$compare_table <- renderDT({
|
|
|
|
| 1256 |
|
| 1257 |
output$common_qtl_table <- renderDT({
|
| 1258 |
cq <- common_qtl_data()
|
| 1259 |
+
if(is.null(cq)||nrow(cq)==0)
|
| 1260 |
+
return(datatable(data.frame(Status="Run Compare Models first")))
|
| 1261 |
datatable(cq,options=list(scrollX=TRUE,pageLength=15),rownames=FALSE,class="compact stripe")
|
| 1262 |
})
|
| 1263 |
|
| 1264 |
# ============================================================
|
| 1265 |
+
# DOWNLOADS β PNG with full custom settings
|
| 1266 |
# ============================================================
|
| 1267 |
|
| 1268 |
+
# Helper: save ideogram PNG with all custom colours/sizes
|
| 1269 |
+
save_ideogram_png <- function(qtl_list, markers, threshold, title_str, file,
|
| 1270 |
width, height) {
|
| 1271 |
png(file, width=width, height=height, res=150, bg="white")
|
| 1272 |
+
make_chromosome_ideogram_reference(
|
| 1273 |
+
qtl_list, markers, threshold,
|
| 1274 |
+
title_str = title_str,
|
| 1275 |
+
band_dark_col = col_chr_dark(),
|
| 1276 |
+
band_light_col = col_chr_light(),
|
| 1277 |
+
border_col = col_chr_border(),
|
| 1278 |
+
qtl_size_min = input$qtl_marker_size_min,
|
| 1279 |
+
qtl_size_max = input$qtl_marker_size_max
|
| 1280 |
+
)
|
| 1281 |
dev.off()
|
| 1282 |
}
|
| 1283 |
|
| 1284 |
+
# Helper: lty integer from style name (for base R PNG)
|
| 1285 |
+
lty_from_style <- function(style) {
|
| 1286 |
+
switch(style, "solid"=1, "dash"=2, "dot"=3, "dashdot"=4, 2)
|
| 1287 |
+
}
|
| 1288 |
+
|
| 1289 |
+
# Manhattan PNG β base R, chromosome-per-panel, all custom settings
|
| 1290 |
output$dl_manhattan_png <- downloadHandler(
|
| 1291 |
+
filename = function() paste0("manhattan_chromwise_",input$mapping_method,"_",Sys.Date(),".png"),
|
| 1292 |
+
content = function(file) {
|
| 1293 |
req(rv$scan_all)
|
| 1294 |
+
pd <- rv$scan_all[!is.na(rv$scan_all$LOD) & !is.na(rv$scan_all$Position) &
|
| 1295 |
+
!is.na(rv$scan_all$Chromosome),]
|
| 1296 |
+
if (nrow(pd)==0) { png(file,100,100); dev.off(); return() }
|
| 1297 |
+
|
| 1298 |
pd$Chromosome <- as.character(pd$Chromosome)
|
| 1299 |
+
pd$sig <- pd$LOD >= lod_thr()
|
| 1300 |
+
chroms <- sort(unique(pd$Chromosome))
|
| 1301 |
+
n_chroms <- length(chroms)
|
| 1302 |
+
n_cols <- ceiling(sqrt(n_chroms))
|
| 1303 |
+
n_rows <- ceiling(n_chroms / n_cols)
|
| 1304 |
+
|
|
|
|
| 1305 |
png(file, width=input$manhattan_width, height=input$manhattan_height, res=150, bg="white")
|
| 1306 |
par(mfrow=c(n_rows, n_cols), mar=c(4,4,3,1), bg="white")
|
| 1307 |
+
|
| 1308 |
for (chr in chroms) {
|
| 1309 |
+
chr_data <- pd[pd$Chromosome==chr,]
|
| 1310 |
chr_data <- chr_data[order(chr_data$Position),]
|
| 1311 |
+
y_max <- max(chr_data$LOD, na.rm=TRUE) * 1.1
|
| 1312 |
+
|
| 1313 |
plot(chr_data$Position, chr_data$LOD, type="n",
|
| 1314 |
+
xlab=paste("Chr",chr,"Position (cM)"), ylab="LOD Score",
|
| 1315 |
+
main=paste("Chr",chr), ylim=c(0,y_max),
|
| 1316 |
frame.plot=FALSE, col.main="#111111", cex.main=0.9, cex.lab=0.8)
|
| 1317 |
+
|
| 1318 |
+
nonsig <- chr_data[!chr_data$sig,]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1319 |
sig_data <- chr_data[chr_data$sig,]
|
| 1320 |
+
|
| 1321 |
+
if (nrow(nonsig)>0)
|
| 1322 |
+
points(nonsig$Position, nonsig$LOD, pch=16,
|
| 1323 |
+
cex=input$nonsig_point_size/5, col=col_nonsig())
|
| 1324 |
+
|
| 1325 |
+
# Significant β diamond, no text labels
|
| 1326 |
+
if (nrow(sig_data)>0)
|
| 1327 |
+
points(sig_data$Position, sig_data$LOD, pch=18,
|
| 1328 |
+
cex=input$sig_point_size/5, col=col_sig())
|
| 1329 |
+
|
| 1330 |
+
abline(h=lod_thr(),
|
| 1331 |
+
lty=lty_from_style(input$threshold_style),
|
| 1332 |
+
col=col_sig(),
|
| 1333 |
lwd=input$threshold_width)
|
| 1334 |
}
|
| 1335 |
+
|
| 1336 |
dev.off()
|
| 1337 |
}
|
| 1338 |
)
|
| 1339 |
|
| 1340 |
# Ideogram PNG
|
| 1341 |
output$dl_idiogram_png <- downloadHandler(
|
| 1342 |
+
filename = function() paste0("idiogram_",input$mapping_method,"_",Sys.Date(),".png"),
|
| 1343 |
+
content = function(file) {
|
| 1344 |
req(rv$scan_all, rv$all_markers)
|
| 1345 |
res_list <- list()
|
| 1346 |
+
if (!is.null(rv$scan_all)&&!"Message"%in%colnames(rv$scan_all))
|
| 1347 |
res_list[[input$mapping_method]] <- rv$scan_all
|
| 1348 |
+
save_ideogram_png(res_list, rv$all_markers, lod_thr(),
|
| 1349 |
+
paste(input$mapping_method,"β Trait:",input$pheno_col),
|
| 1350 |
file, input$ideogram_width, input$ideogram_height)
|
| 1351 |
}
|
| 1352 |
)
|
| 1353 |
|
| 1354 |
+
# Compare ideogram PNG
|
| 1355 |
output$dl_compare_idiogram_png <- downloadHandler(
|
| 1356 |
+
filename = function() paste0("compare_idiogram_",Sys.Date(),".png"),
|
| 1357 |
+
content = function(file) {
|
| 1358 |
req(length(rv$compare_res)>0, rv$all_markers)
|
| 1359 |
+
save_ideogram_png(rv$compare_res, rv$all_markers, lod_thr(),
|
| 1360 |
+
"Multi-Model QTL Comparison",
|
| 1361 |
+
file, input$ideogram_width, input$ideogram_height)
|
| 1362 |
}
|
| 1363 |
)
|
| 1364 |
|
| 1365 |
+
# Compare bar PNG β uses custom dimensions
|
| 1366 |
output$dl_compare_bar_png <- downloadHandler(
|
| 1367 |
+
filename = function() paste0("compare_bar_",Sys.Date(),".png"),
|
| 1368 |
+
content = function(file) {
|
| 1369 |
req(length(rv$compare_res)>0)
|
| 1370 |
counts <- sapply(rv$compare_res, function(r){
|
| 1371 |
if(is.null(r)||"Message"%in%colnames(r)) return(0)
|
| 1372 |
+
sum(!is.na(r$LOD)&r$LOD>=lod_thr(),na.rm=TRUE)
|
| 1373 |
+
})
|
| 1374 |
df_bar <- data.frame(Model=names(counts),N_QTL=as.numeric(counts))
|
| 1375 |
df_bar$Color <- MODEL_COLORS[df_bar$Model]
|
| 1376 |
df_bar$Color[is.na(df_bar$Color)] <- "#CC0000"
|
| 1377 |
+
# Use manhattan width/height for bar chart download as well
|
| 1378 |
+
png(file, width=input$manhattan_width, height=max(400,input$manhattan_height/2),
|
| 1379 |
+
res=150, bg="white")
|
| 1380 |
par(mar=c(6,5,4,2), bg="white")
|
| 1381 |
bp <- barplot(df_bar$N_QTL, names.arg=df_bar$Model,
|
| 1382 |
+
col=df_bar$Color, border="#330000",
|
| 1383 |
+
main=paste0("QTLs per Model (LOD β₯ ",lod_thr(),")"),
|
| 1384 |
+
ylab="# Significant QTLs", las=2, cex.names=0.85,
|
| 1385 |
+
col.main="#111111")
|
| 1386 |
+
text(bp, df_bar$N_QTL + 0.1, df_bar$N_QTL, cex=0.8, font=2, col="#111111")
|
| 1387 |
dev.off()
|
| 1388 |
}
|
| 1389 |
)
|
| 1390 |
|
| 1391 |
# Common ideogram PNG
|
| 1392 |
output$dl_common_idiogram_png <- downloadHandler(
|
| 1393 |
+
filename = function() paste0("common_qtl_idiogram_",Sys.Date(),".png"),
|
| 1394 |
+
content = function(file) {
|
| 1395 |
cq <- common_qtl_data()
|
| 1396 |
req(!is.null(cq), nrow(cq)>0, rv$all_markers)
|
| 1397 |
cq_list <- list()
|
|
|
|
| 1401 |
cm <- r[r$Marker %in% cq$Marker,]
|
| 1402 |
if(nrow(cm)>0) cq_list[[mth]] <- cm
|
| 1403 |
}
|
| 1404 |
+
save_ideogram_png(cq_list, rv$all_markers, lod_thr(),
|
| 1405 |
+
paste0("Common QTL (β₯",input$min_models," models)"),
|
| 1406 |
+
file, input$ideogram_width, input$ideogram_height)
|
| 1407 |
}
|
| 1408 |
)
|
| 1409 |
|
| 1410 |
# ββ CSV DOWNLOADS ββ
|
| 1411 |
output$dl_qtl_csv <- downloadHandler(
|
| 1412 |
+
filename = function() paste0("qtl_significant_",input$mapping_method,"_",Sys.Date(),".csv"),
|
| 1413 |
+
content = function(file) {
|
| 1414 |
req(rv$scan_all)
|
| 1415 |
if("Message"%in%colnames(rv$scan_all)){write.csv(rv$scan_all,file,row.names=FALSE);return()}
|
| 1416 |
sig <- rv$scan_all[!is.na(rv$scan_all$LOD)&rv$scan_all$LOD>=lod_thr(),,drop=FALSE]
|
|
|
|
| 1420 |
)
|
| 1421 |
|
| 1422 |
output$dl_full_csv <- downloadHandler(
|
| 1423 |
+
filename = function() paste0("qtl_full_scan_",input$mapping_method,"_",Sys.Date(),".csv"),
|
| 1424 |
+
content = function(file) {
|
| 1425 |
req(rv$scan_all)
|
| 1426 |
write.csv(rv$scan_all,file,row.names=FALSE)
|
| 1427 |
}
|
| 1428 |
)
|
| 1429 |
|
| 1430 |
output$dl_compare_csv <- downloadHandler(
|
| 1431 |
+
filename = function() paste0("qtl_compare_",Sys.Date(),".csv"),
|
| 1432 |
+
content = function(file) {
|
| 1433 |
req(length(rv$compare_res)>0)
|
| 1434 |
all_res <- lapply(names(rv$compare_res), function(mth){
|
| 1435 |
r <- rv$compare_res[[mth]]
|
|
|
|
| 1437 |
sig <- r[!is.na(r$LOD)&r$LOD>=lod_thr(),,drop=FALSE]
|
| 1438 |
if(nrow(sig)>0){sig$Model<-mth;sig} else NULL
|
| 1439 |
})
|
| 1440 |
+
all_res <- do.call(rbind, Filter(Negate(is.null),all_res))
|
| 1441 |
if(is.null(all_res)) all_res <- data.frame(Status="No significant QTLs")
|
| 1442 |
write.csv(all_res,file,row.names=FALSE)
|
| 1443 |
}
|
| 1444 |
)
|
| 1445 |
|
| 1446 |
output$dl_common_csv <- downloadHandler(
|
| 1447 |
+
filename = function() paste0("qtl_common_",Sys.Date(),".csv"),
|
| 1448 |
+
content = function(file) {
|
| 1449 |
cq <- common_qtl_data()
|
| 1450 |
if(is.null(cq)||nrow(cq)==0) cq <- data.frame(Status="No common QTLs")
|
| 1451 |
write.csv(cq,file,row.names=FALSE)
|
|
|
|
| 1453 |
)
|
| 1454 |
}
|
| 1455 |
|
| 1456 |
+
# ββ Run ββ
|
| 1457 |
shinyApp(ui=ui, server=server)
|