softwareDevelopment commited on
Commit
50a6bc6
Β·
verified Β·
1 Parent(s): 27ddbf0

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +262 -132
app.R CHANGED
@@ -1,6 +1,7 @@
1
  # ============================================================
2
- # QTL-Mapper v7 β€” 6 Models: SIM, CIM, MQM, RF, XGBoost, SVR
3
- # - Reference-style chromosome ideogram (dark green banded + red QTL markers)
 
4
  # - PNG download for all plots
5
  # - CSV download for all tables
6
  # ============================================================
@@ -466,7 +467,6 @@ add_lod_support_interval <- function(sig_df, all_df, lod_drop=1.5) {
466
 
467
  # ============================================================
468
  # REFERENCE-STYLE CHROMOSOME IDEOGRAM
469
- # Dark green banded chromosomes + red QTL markers (like user image)
470
  # ============================================================
471
  make_chromosome_ideogram_reference <- function(qtl_list, all_markers_df,
472
  lod_threshold=2.0,
@@ -488,15 +488,15 @@ 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 (genetic position)
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 pattern colors mimicking reference image (dark green stripes)
498
- band_dark <- "#2E6B35" # dark green
499
- band_light <- "#6AAB6E" # lighter green
500
  chr_border <- "#1A4A1F"
501
 
502
  for (ci in seq_along(chroms)) {
@@ -508,7 +508,7 @@ 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 (alternating dark/light bands)
512
  n_bands <- max(8, round((chr_len - chr_min) / 8))
513
  band_h <- (chr_len - chr_min) / n_bands
514
 
@@ -520,17 +520,15 @@ make_chromosome_ideogram_reference <- function(qtl_list, all_markers_df,
520
  col=bcol, border=NA)
521
  }
522
 
523
- # Chromosome outline
524
  rect(x_center - bar_w/2, chr_min, x_center + bar_w/2, chr_len,
525
  col=NA, border=chr_border, lwd=1.8)
526
 
527
- # Rounded telomere caps (top and bottom)
528
  symbols(x_center, chr_min, circles=bar_w/2, inches=FALSE,
529
  add=TRUE, fg=chr_border, bg=band_light, lwd=1.2)
530
  symbols(x_center, chr_len, circles=bar_w/2, inches=FALSE,
531
  add=TRUE, fg=chr_border, bg=band_dark, lwd=1.2)
532
 
533
- # Centromere constriction
534
  cen_pos <- chr_min + (chr_len - chr_min)*0.42
535
  polygon(c(x_center-bar_w/2, x_center, x_center+bar_w/2, x_center),
536
  c(cen_pos - chr_len*0.025, cen_pos,
@@ -541,7 +539,7 @@ make_chromosome_ideogram_reference <- function(qtl_list, all_markers_df,
541
  cen_pos + chr_len*0.025, cen_pos + chr_len*0.05),
542
  col="white", border=chr_border, lwd=1.2)
543
 
544
- # Plot QTLs on chromosome β€” each model offset slightly
545
  n_models <- length(model_names)
546
  for (ti in seq_along(model_names)) {
547
  trait <- model_names[ti]
@@ -556,33 +554,27 @@ make_chromosome_ideogram_reference <- function(qtl_list, all_markers_df,
556
  col_m <- MODEL_COLORS[trait]
557
  if (is.na(col_m)) col_m <- "#CC0000"
558
 
559
- # Offset x for multiple models (stagger to right)
560
  x_off <- x_center + bar_w/2 + 0.08 + (ti-1)*0.22
561
 
562
  for (ri in 1:nrow(sig_qtls)) {
563
  pos <- sig_qtls$Position[ri]
564
  lod_val <- sig_qtls$LOD[ri]
565
- mname <- sig_qtls$Marker[ri]
566
 
567
- # Horizontal tick on chromosome
568
  segments(x_center + bar_w/2, pos, x_off - 0.04, pos,
569
  col=col_m, lwd=1.5)
570
- # Marker dot
571
  points(x_off, pos, pch=16, col=col_m,
572
  cex=pmax(0.7, pmin(2.0, 0.6 + lod_val/8)))
573
- # LOD label in red (matching reference style)
574
  text(x_off + 0.15, pos,
575
  paste0(round(lod_val,1)),
576
  cex=0.55, col=col_m, font=2, adj=0)
577
  }
578
  }
579
 
580
- # Chromosome label below
581
  text(x_center, chr_min - max_pos*0.04, paste0("Chr", chr),
582
  cex=0.72, font=2, col="#1A1A1A", adj=0.5)
583
  }
584
 
585
- # Legend (outside right margin)
586
  legend_x <- n_chr * x_gap + 0.5
587
  legend_y <- max_pos * 0.95
588
  text(legend_x, legend_y + max_pos*0.07, "Models", cex=0.75, font=2, col="#1A1A1A", adj=0)
@@ -604,64 +596,129 @@ make_chromosome_ideogram_reference <- function(qtl_list, all_markers_df,
604
  }
605
 
606
  # ============================================================
607
- # PLOTLY MANHATTAN β€” red significant markers
608
  # ============================================================
609
- make_plotly_manhattan <- function(scan_all, lod_threshold=2.0, title="QTL Manhattan Plot") {
610
- blank <- plot_ly()%>%layout(title=title,plot_bgcolor="white",paper_bgcolor="white")
611
- if (is.null(scan_all)||"Message"%in%colnames(scan_all)) return(blank)
612
- pd <- scan_all[!is.na(scan_all$LOD)&!is.na(scan_all$Position)&!is.na(scan_all$Chromosome),]
613
- if (nrow(pd)==0) return(blank)
 
 
 
 
 
 
 
614
  pd$Chromosome <- as.character(pd$Chromosome)
 
 
615
  chroms <- sort(unique(pd$Chromosome))
616
- offsets <- numeric(length(chroms)); names(offsets) <- chroms
617
- for (i in seq_along(chroms)){
618
- if(i==1){offsets[chroms[1]]<-0;next}
619
- offsets[chroms[i]] <- offsets[chroms[i-1]]+max(pd$Position[pd$Chromosome==chroms[i-1]],na.rm=TRUE)*1.05
620
- }
621
- pd$cum_pos <- pd$Position+offsets[pd$Chromosome]
622
- pd$sig <- pd$LOD>=lod_threshold
623
- chr_mids <- tapply(pd$cum_pos,pd$Chromosome,median,na.rm=TRUE)
624
-
625
- # Alternating dark/light green chromosome colors
626
- chr_cols <- rep(c("#3A7D44","#6AAB6E"),ceiling(length(chroms)/2))[seq_along(chroms)]
627
- names(chr_cols) <- chroms
628
-
629
- p <- plot_ly()
630
- for (ch in chroms) {
631
- sub <- pd[pd$Chromosome==ch&!pd$sig,]
632
- if (nrow(sub)>0)
633
- p <- p%>%add_trace(data=sub,x=~cum_pos,y=~LOD,type="scatter",mode="markers",
634
- name=paste("Chr",ch),
635
- marker=list(size=5,color=chr_cols[ch],opacity=0.65,line=list(width=0)),
636
- text=~paste0("<b>",Marker,"</b><br>Chr: ",Chromosome,"<br>Pos: ",round(Position,1),
637
- " cM<br>LOD: ",round(LOD,2),"<br>PVE: ",round(PVE_Marker_Percent,1),"%"),
638
- hoverinfo="text",showlegend=FALSE)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
639
  }
640
- sig_pd <- pd[pd$sig,]
641
- if (nrow(sig_pd)>0)
642
- p <- p%>%add_trace(data=sig_pd,x=~cum_pos,y=~LOD,type="scatter",mode="markers+text",
643
- name="Significant",
644
- marker=list(size=9,color="#CC0000",symbol="diamond",line=list(width=1,color="#660000")),
645
- text=~Marker, textposition="top center",
646
- textfont=list(color="#CC0000",size=9),
647
- hovertext=~paste0("<b>",Marker,"</b><br>Chr: ",Chromosome,"<br>Pos: ",round(Position,1),
648
- " cM<br>LOD: ",round(LOD,2),"<br>PVE: ",round(PVE_Marker_Percent,1),"%"),
649
- hoverinfo="text",showlegend=TRUE)
650
-
651
- p%>%add_segments(x=min(pd$cum_pos,na.rm=TRUE),xend=max(pd$cum_pos,na.rm=TRUE),
652
- y=lod_threshold,yend=lod_threshold,
653
- line=list(color="#CC0000",dash="dash",width=1.5),showlegend=FALSE,name="Threshold")%>%
654
- layout(
655
- title=list(text=paste0(title," | ",sum(pd$sig,na.rm=TRUE)," significant QTLs"),
656
- font=list(size=14,color="#111111")),
657
- xaxis=list(title="Chromosome",tickvals=as.list(chr_mids[chroms]),
658
- ticktext=as.list(paste0("Chr",chroms)),tickangle=45,
659
- gridcolor="#EEEEEE",color="#333333",zeroline=FALSE),
660
- yaxis=list(title="LOD Score",gridcolor="#EEEEEE",color="#333333",zeroline=FALSE),
661
- plot_bgcolor="white",paper_bgcolor="white",
662
- font=list(color="#111111"),
663
- legend=list(orientation="h",y=-0.15,font=list(size=10)),
664
- hoverlabel=list(bgcolor="#FFFFFF",font=list(color="#111111",size=11)))
665
  }
666
 
667
  # ============================================================
@@ -736,11 +793,13 @@ ui <- fluidPage(
736
  h4{color:#1A4A1F;font-weight:600;}
737
  .section-title{font-size:13px;font-weight:600;color:#2E6B35;margin-bottom:6px;
738
  border-bottom:1px solid #CCDDCC;padding-bottom:4px;}
 
 
739
  "))
740
  ),
741
 
742
  navbarPage(
743
- title = "🌿 QTL-Mapper v7",
744
  id = "nav",
745
 
746
  # ── DATA UPLOAD ──────────────────────────────────────────────────────
@@ -779,7 +838,59 @@ ui <- fluidPage(
779
  br(),
780
  div(class="info-box",
781
  "6 models: SIM Β· CIM Β· MQM Β· RF Β· SVR Β· XGBoost",br(),
782
- "Idiogram matches reference style (banded chromosomes + red QTL markers)"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
783
  )
784
  ))
785
  )
@@ -790,27 +901,29 @@ ui <- fluidPage(
790
  tabPanel("πŸ“Š Results",
791
  div(class="download-row",
792
  downloadButton("dl_manhattan_png","πŸ–Ό Manhattan (PNG)",class="btn btn-info"),
793
- downloadButton("dl_idiogram_png","πŸ—Ί Idiogram (PNG)",class="btn btn-info"),
794
  downloadButton("dl_qtl_csv","πŸ“₯ QTL Table (CSV)",class="btn btn-success"),
795
  downloadButton("dl_full_csv","πŸ“₯ Full Scan (CSV)",class="btn btn-warning")
796
  ),
797
  fluidRow(
798
- column(8,
799
  div(class="card", div(class="card-body",
800
- div(class="section-title","Manhattan Plot"),
801
- plotlyOutput("manhattan_plot",height="460px")
802
  ))
803
- ),
804
- column(4,
 
 
805
  div(class="card", div(class="card-body",
806
- div(class="section-title","Chromosome Ideogram"),
807
- plotOutput("idiogram_plot",height="460px")
808
  ))
809
  )
810
  ),
811
  fluidRow(column(12,
812
  div(class="card", div(class="card-body",
813
- div(class="section-title","Significant QTL Table"),
814
  DTOutput("qtl_table")
815
  ))
816
  ))
@@ -835,7 +948,7 @@ ui <- fluidPage(
835
  column(6,
836
  div(class="card", div(class="card-body",
837
  div(class="section-title","Multi-Model Chromosome Ideogram"),
838
- plotOutput("compare_idiogram",height="520px")
839
  ))
840
  ),
841
  column(6,
@@ -861,14 +974,14 @@ ui <- fluidPage(
861
  uiOutput("common_qtl_summary_ui")
862
  )),
863
  div(class="download-row",
864
- downloadButton("dl_common_idiogram_png","πŸ–Ό Common Idiogram (PNG)",class="btn btn-info"),
865
  downloadButton("dl_common_csv","πŸ“₯ Common QTL (CSV)",class="btn btn-success")
866
  ),
867
  fluidRow(
868
  column(6,
869
  div(class="card", div(class="card-body",
870
  div(class="section-title","Common QTL Ideogram"),
871
- plotOutput("common_idiogram",height="500px")
872
  ))
873
  ),
874
  column(6,
@@ -996,8 +1109,15 @@ server <- function(input, output, session) {
996
  # ── PLOTS ──
997
  output$manhattan_plot <- renderPlotly({
998
  req(rv$scan_all)
999
- make_plotly_manhattan(rv$scan_all, lod_thr(),
1000
- paste(input$mapping_method,"Manhattan β€” Trait:",input$pheno_col))
 
 
 
 
 
 
 
1001
  })
1002
 
1003
  idiogram_render <- function(qtl_list, markers, threshold, title_str=NULL) {
@@ -1013,7 +1133,7 @@ server <- function(input, output, session) {
1013
  res_list[[input$mapping_method]] <- rv$scan_all
1014
  idiogram_render(res_list, rv$all_markers, lod_thr(),
1015
  paste(input$mapping_method,"β€” Trait:",input$pheno_col))
1016
- }, bg="white")
1017
 
1018
  output$compare_idiogram <- renderPlot({
1019
  req(length(rv$compare_res)>0, rv$all_markers)
@@ -1057,10 +1177,9 @@ server <- function(input, output, session) {
1057
  tags$b(input$min_models), " models")
1058
  })
1059
 
1060
- output$common_idiogram <- renderPlot({
1061
  cq <- common_qtl_data()
1062
  req(!is.null(cq), nrow(cq)>0, rv$all_markers)
1063
- # Build a list with one entry per model that has common QTLs
1064
  cq_list <- list()
1065
  for (mth in names(rv$compare_res)) {
1066
  r <- rv$compare_res[[mth]]
@@ -1124,60 +1243,68 @@ server <- function(input, output, session) {
1124
  })
1125
 
1126
  # ============================================================
1127
- # DOWNLOADS β€” PNG for all plots
1128
  # ============================================================
1129
 
1130
- # Helper: render idiogram to PNG
1131
- save_idiogram_png <- function(qtl_list, markers, threshold, title_str, file) {
1132
- png(file, width=1400, height=900, res=150, bg="white")
1133
  make_chromosome_ideogram_reference(qtl_list, markers, threshold, title_str=title_str)
1134
  dev.off()
1135
  }
1136
 
1137
- # Manhattan PNG (ggplot reconstruction for PNG export)
1138
  output$dl_manhattan_png <- downloadHandler(
1139
- filename=function() paste0("manhattan_",input$mapping_method,"_",Sys.Date(),".png"),
1140
  content=function(file) {
1141
  req(rv$scan_all)
1142
  pd <- rv$scan_all[!is.na(rv$scan_all$LOD)&!is.na(rv$scan_all$Position)&!is.na(rv$scan_all$Chromosome),]
1143
  if (nrow(pd)==0){png(file,100,100);dev.off();return()}
 
1144
  pd$Chromosome <- as.character(pd$Chromosome)
 
1145
  chroms <- sort(unique(pd$Chromosome))
1146
- offsets <- numeric(length(chroms)); names(offsets) <- chroms
1147
- for (i in seq_along(chroms)){
1148
- if(i==1){offsets[chroms[1]]<-0;next}
1149
- offsets[chroms[i]] <- offsets[chroms[i-1]]+max(pd$Position[pd$Chromosome==chroms[i-1]],na.rm=TRUE)*1.05
1150
- }
1151
- pd$cum_pos <- pd$Position+offsets[pd$Chromosome]
1152
- pd$sig <- pd$LOD>=lod_thr()
1153
- chr_mids <- tapply(pd$cum_pos,pd$Chromosome,median,na.rm=TRUE)
1154
- chr_cols <- rep(c("#3A7D44","#6AAB6E"),ceiling(length(chroms)/2))[seq_along(chroms)]
1155
- names(chr_cols) <- chroms
1156
- pd$chr_color <- chr_cols[pd$Chromosome]
1157
- png(file, width=1600, height=700, res=150, bg="white")
1158
- par(mar=c(5,5,4,2), bg="white")
1159
- plot(pd$cum_pos, pd$LOD, type="n",
1160
- xlab="Chromosome", ylab="LOD Score",
1161
- main=paste(input$mapping_method,"Manhattan β€” Trait:",input$pheno_col),
1162
- xaxt="n", frame.plot=FALSE, col.main="#111111",
1163
- cex.main=1, cex.lab=0.9)
1164
- for (ch in chroms) {
1165
- sub <- pd[pd$Chromosome==ch,]
1166
- points(sub$cum_pos, sub$LOD, pch=16, cex=0.6, col=chr_cols[ch])
 
 
 
 
 
 
 
 
 
 
 
 
 
1167
  }
1168
- sig_pd <- pd[pd$sig,]
1169
- if(nrow(sig_pd)>0)
1170
- points(sig_pd$cum_pos, sig_pd$LOD, pch=18, cex=1.4, col="#CC0000")
1171
- abline(h=lod_thr(), lty=2, col="#CC0000", lwd=1.5)
1172
- axis(1, at=chr_mids[chroms], labels=paste0("Chr",chroms), las=2, cex.axis=0.7)
1173
- if(nrow(sig_pd)>0)
1174
- text(sig_pd$cum_pos, sig_pd$LOD+max(pd$LOD,na.rm=TRUE)*0.02,
1175
- sig_pd$Marker, cex=0.5, col="#CC0000", font=2)
1176
  dev.off()
1177
  }
1178
  )
1179
 
1180
- # Idiogram PNG β€” single model
1181
  output$dl_idiogram_png <- downloadHandler(
1182
  filename=function() paste0("idiogram_",input$mapping_method,"_",Sys.Date(),".png"),
1183
  content=function(file) {
@@ -1186,7 +1313,8 @@ server <- function(input, output, session) {
1186
  if(!is.null(rv$scan_all)&&!"Message"%in%colnames(rv$scan_all))
1187
  res_list[[input$mapping_method]] <- rv$scan_all
1188
  save_idiogram_png(res_list, rv$all_markers, lod_thr(),
1189
- paste(input$mapping_method,"β€” Trait:",input$pheno_col), file)
 
1190
  }
1191
  )
1192
 
@@ -1196,7 +1324,8 @@ server <- function(input, output, session) {
1196
  content=function(file) {
1197
  req(length(rv$compare_res)>0, rv$all_markers)
1198
  save_idiogram_png(rv$compare_res, rv$all_markers, lod_thr(),
1199
- "Multi-Model QTL Comparison", file)
 
1200
  }
1201
  )
1202
 
@@ -1223,7 +1352,7 @@ server <- function(input, output, session) {
1223
  }
1224
  )
1225
 
1226
- # Common idiogram PNG
1227
  output$dl_common_idiogram_png <- downloadHandler(
1228
  filename=function() paste0("common_qtl_idiogram_",Sys.Date(),".png"),
1229
  content=function(file) {
@@ -1237,7 +1366,8 @@ server <- function(input, output, session) {
1237
  if(nrow(cm)>0) cq_list[[mth]] <- cm
1238
  }
1239
  save_idiogram_png(cq_list, rv$all_markers, lod_thr(),
1240
- paste0("Common QTL (β‰₯",input$min_models," models)"), file)
 
1241
  }
1242
  )
1243
 
 
1
  # ============================================================
2
+ # QTL-Mapper v7 ENHANCED β€” Chromosome-wise Manhattan + Customization
3
+ # - Chromosome-wise Manhattan plots (no marker labels)
4
+ # - Extensive plot customization controls
5
  # - PNG download for all plots
6
  # - CSV download for all tables
7
  # ============================================================
 
467
 
468
  # ============================================================
469
  # REFERENCE-STYLE CHROMOSOME IDEOGRAM
 
470
  # ============================================================
471
  make_chromosome_ideogram_reference <- function(qtl_list, all_markers_df,
472
  lod_threshold=2.0,
 
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)) {
 
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
 
 
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=chr_border, lwd=1.8)
525
 
 
526
  symbols(x_center, chr_min, circles=bar_w/2, inches=FALSE,
527
  add=TRUE, fg=chr_border, bg=band_light, lwd=1.2)
528
  symbols(x_center, chr_len, circles=bar_w/2, inches=FALSE,
529
  add=TRUE, fg=chr_border, bg=band_dark, lwd=1.2)
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,
 
539
  cen_pos + chr_len*0.025, cen_pos + chr_len*0.05),
540
  col="white", border=chr_border, lwd=1.2)
541
 
542
+ # Plot QTLs
543
  n_models <- length(model_names)
544
  for (ti in seq_along(model_names)) {
545
  trait <- model_names[ti]
 
554
  col_m <- MODEL_COLORS[trait]
555
  if (is.na(col_m)) col_m <- "#CC0000"
556
 
 
557
  x_off <- x_center + bar_w/2 + 0.08 + (ti-1)*0.22
558
 
559
  for (ri in 1:nrow(sig_qtls)) {
560
  pos <- sig_qtls$Position[ri]
561
  lod_val <- sig_qtls$LOD[ri]
 
562
 
 
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(0.7, pmin(2.0, 0.6 + lod_val/8)))
 
567
  text(x_off + 0.15, pos,
568
  paste0(round(lod_val,1)),
569
  cex=0.55, col=col_m, font=2, adj=0)
570
  }
571
  }
572
 
 
573
  text(x_center, chr_min - max_pos*0.04, paste0("Chr", chr),
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)
 
596
  }
597
 
598
  # ============================================================
599
+ # CHROMOSOME-WISE MANHATTAN PLOT (NO LABELS)
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
+ x = ~Position,
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
+ "<br>Pos: ", round(Position, 1), " cM<br>LOD: ", round(LOD, 2),
643
+ "<br>PVE: ", round(PVE_Marker_Percent, 1), "%"),
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
+ x = ~Position,
655
+ y = ~LOD,
656
+ type = "scatter",
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
+ "<br>Pos: ", round(Position, 1), " cM<br>LOD: ", round(LOD, 2),
662
+ "<br>PVE: ", round(PVE_Marker_Percent, 1), "%"),
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
+ xend = max(chr_data$Position, na.rm = TRUE),
673
+ y = lod_threshold,
674
+ yend = lod_threshold,
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
+ title = paste("Chr", chr, "Position (cM)"),
684
+ gridcolor = "#EEEEEE",
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 <- ceiling(sqrt(n_chroms))
702
+ n_rows <- ceiling(n_chroms / n_cols)
703
+
704
+ combined_plot <- subplot(
705
+ subplot_list,
706
+ nrows = n_rows,
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 = TRUE), " significant QTLs"),
716
+ font = list(size = 14, color = "#111111")
717
+ ),
718
+ showlegend = FALSE,
719
+ font = list(color = "#111111"),
720
+ hoverlabel = list(bgcolor = "#FFFFFF", font = list(color = "#111111", size = 11))
721
+ )
 
722
  }
723
 
724
  # ============================================================
 
793
  h4{color:#1A4A1F;font-weight:600;}
794
  .section-title{font-size:13px;font-weight:600;color:#2E6B35;margin-bottom:6px;
795
  border-bottom:1px solid #CCDDCC;padding-bottom:4px;}
796
+ .custom-section{background:#F9FBFA;padding:12px;border-radius:6px;margin:8px 0;
797
+ border:1px solid #D5E8D5;}
798
  "))
799
  ),
800
 
801
  navbarPage(
802
+ title = "🌿 QTL-Mapper v7 Enhanced",
803
  id = "nav",
804
 
805
  # ── DATA UPLOAD ──────────────────────────────────────────────────────
 
838
  br(),
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","🎨 Manhattan Plot Customization"),
855
+ div(class="custom-section",
856
+ h5("πŸ“Š Plot Dimensions"),
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
+ ),
860
+ div(class="custom-section",
861
+ h5("🎯 Point Styling"),
862
+ sliderInput("nonsig_point_size","Non-Significant Point Size",min=2,max=12,value=6,step=1),
863
+ sliderInput("sig_point_size","Significant Point Size",min=4,max=20,value=10,step=1),
864
+ colourpicker::colourInput("nonsig_color","Non-Significant Color","#3A7D44"),
865
+ colourpicker::colourInput("sig_color","Significant Color","#CC0000")
866
+ ),
867
+ div(class="custom-section",
868
+ h5("πŸ“ Threshold Line"),
869
+ sliderInput("threshold_width","Threshold Line Width",min=0.5,max=4,value=1.5,step=0.5),
870
+ selectInput("threshold_style","Threshold Line Style",
871
+ choices=c("Solid"="solid","Dashed"="dash","Dotted"="dot","Dash-Dot"="dashdot"),
872
+ selected="dash")
873
+ )
874
+ ))
875
+ ),
876
+ column(6,
877
+ div(class="card", div(class="card-body",
878
+ div(class="section-title","πŸ—Ί Ideogram Plot Customization"),
879
+ div(class="custom-section",
880
+ h5("πŸ“Š Plot Dimensions"),
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
+ ),
884
+ div(class="custom-section",
885
+ h5("🎨 Chromosome Colors"),
886
+ colourpicker::colourInput("chr_dark","Dark Band Color","#2E6B35"),
887
+ colourpicker::colourInput("chr_light","Light Band Color","#6AAB6E"),
888
+ colourpicker::colourInput("chr_border","Border Color","#1A4A1F")
889
+ ),
890
+ div(class="custom-section",
891
+ h5("πŸ”΄ QTL Marker Styling"),
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
  )
 
901
  tabPanel("πŸ“Š Results",
902
  div(class="download-row",
903
  downloadButton("dl_manhattan_png","πŸ–Ό Manhattan (PNG)",class="btn btn-info"),
904
+ downloadButton("dl_idiogram_png","πŸ—Ί Ideogram (PNG)",class="btn btn-info"),
905
  downloadButton("dl_qtl_csv","πŸ“₯ QTL Table (CSV)",class="btn btn-success"),
906
  downloadButton("dl_full_csv","πŸ“₯ Full Scan (CSV)",class="btn btn-warning")
907
  ),
908
  fluidRow(
909
+ column(12,
910
  div(class="card", div(class="card-body",
911
+ div(class="section-title","πŸ“Š Chromosome-wise Manhattan Plot (No Marker Labels)"),
912
+ plotlyOutput("manhattan_plot",height="800px")
913
  ))
914
+ )
915
+ ),
916
+ fluidRow(
917
+ column(12,
918
  div(class="card", div(class="card-body",
919
+ div(class="section-title","πŸ—Ί Chromosome Ideogram"),
920
+ plotOutput("idiogram_plot",height="900px")
921
  ))
922
  )
923
  ),
924
  fluidRow(column(12,
925
  div(class="card", div(class="card-body",
926
+ div(class="section-title","πŸ“‹ Significant QTL Table"),
927
  DTOutput("qtl_table")
928
  ))
929
  ))
 
948
  column(6,
949
  div(class="card", div(class="card-body",
950
  div(class="section-title","Multi-Model Chromosome Ideogram"),
951
+ plotOutput("compare_ideogram",height="520px")
952
  ))
953
  ),
954
  column(6,
 
974
  uiOutput("common_qtl_summary_ui")
975
  )),
976
  div(class="download-row",
977
+ downloadButton("dl_common_idiogram_png","πŸ–Ό Common Ideogram (PNG)",class="btn btn-info"),
978
  downloadButton("dl_common_csv","πŸ“₯ Common QTL (CSV)",class="btn btn-success")
979
  ),
980
  fluidRow(
981
  column(6,
982
  div(class="card", div(class="card-body",
983
  div(class="section-title","Common QTL Ideogram"),
984
+ plotOutput("common_ideogram",height="500px")
985
  ))
986
  ),
987
  column(6,
 
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 = input$nonsig_color,
1117
+ sig_color = input$sig_color,
1118
+ point_size = input$nonsig_point_size,
1119
+ sig_point_size = input$sig_point_size
1120
+ )
1121
  })
1122
 
1123
  idiogram_render <- function(qtl_list, markers, threshold, title_str=NULL) {
 
1133
  res_list[[input$mapping_method]] <- rv$scan_all
1134
  idiogram_render(res_list, rv$all_markers, lod_thr(),
1135
  paste(input$mapping_method,"β€” Trait:",input$pheno_col))
1136
+ }, bg="white", height=function() input$ideogram_height)
1137
 
1138
  output$compare_idiogram <- renderPlot({
1139
  req(length(rv$compare_res)>0, rv$all_markers)
 
1177
  tags$b(input$min_models), " models")
1178
  })
1179
 
1180
+ output$common_ideogram <- renderPlot({
1181
  cq <- common_qtl_data()
1182
  req(!is.null(cq), nrow(cq)>0, rv$all_markers)
 
1183
  cq_list <- list()
1184
  for (mth in names(rv$compare_res)) {
1185
  r <- rv$compare_res[[mth]]
 
1243
  })
1244
 
1245
  # ============================================================
1246
+ # DOWNLOADS β€” PNG with custom settings
1247
  # ============================================================
1248
 
1249
+ save_idiogram_png <- function(qtl_list, markers, threshold, title_str, file,
1250
+ width, height) {
1251
+ png(file, width=width, height=height, res=150, bg="white")
1252
  make_chromosome_ideogram_reference(qtl_list, markers, threshold, title_str=title_str)
1253
  dev.off()
1254
  }
1255
 
1256
+ # Manhattan PNG
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)&!is.na(rv$scan_all$Chromosome),]
1262
  if (nrow(pd)==0){png(file,100,100);dev.off();return()}
1263
+
1264
  pd$Chromosome <- as.character(pd$Chromosome)
1265
+ pd$sig <- pd$LOD >= lod_thr()
1266
  chroms <- sort(unique(pd$Chromosome))
1267
+
1268
+ n_chroms <- length(chroms)
1269
+ n_cols <- ceiling(sqrt(n_chroms))
1270
+ n_rows <- ceiling(n_chroms / n_cols)
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 == chr,]
1277
+ chr_data <- chr_data[order(chr_data$Position),]
1278
+
1279
+ plot(chr_data$Position, chr_data$LOD, type="n",
1280
+ xlab=paste("Chr", chr, "Position (cM)"), ylab="LOD Score",
1281
+ main=paste("Chr", chr),
1282
+ frame.plot=FALSE, col.main="#111111", cex.main=0.9, cex.lab=0.8)
1283
+
1284
+ # Non-significant
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
+ if(nrow(sig_data)>0)
1293
+ points(sig_data$Position, sig_data$LOD, pch=18,
1294
+ cex=input$sig_point_size/4, col=input$sig_color)
1295
+
1296
+ # Threshold line
1297
+ lty_val <- switch(input$threshold_style,
1298
+ "solid"=1, "dash"=2, "dot"=3, "dashdot"=4, 2)
1299
+ abline(h=lod_thr(), lty=lty_val, col=input$sig_color,
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) {
 
1313
  if(!is.null(rv$scan_all)&&!"Message"%in%colnames(rv$scan_all))
1314
  res_list[[input$mapping_method]] <- rv$scan_all
1315
  save_idiogram_png(res_list, rv$all_markers, lod_thr(),
1316
+ paste(input$mapping_method,"β€” Trait:",input$pheno_col),
1317
+ file, input$ideogram_width, input$ideogram_height)
1318
  }
1319
  )
1320
 
 
1324
  content=function(file) {
1325
  req(length(rv$compare_res)>0, rv$all_markers)
1326
  save_idiogram_png(rv$compare_res, rv$all_markers, lod_thr(),
1327
+ "Multi-Model QTL Comparison", file,
1328
+ input$ideogram_width, input$ideogram_height)
1329
  }
1330
  )
1331
 
 
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) {
 
1366
  if(nrow(cm)>0) cq_list[[mth]] <- cm
1367
  }
1368
  save_idiogram_png(cq_list, rv$all_markers, lod_thr(),
1369
+ paste0("Common QTL (β‰₯",input$min_models," models)"), file,
1370
+ input$ideogram_width, input$ideogram_height)
1371
  }
1372
  )
1373