softwareDevelopment commited on
Commit
4650d58
Β·
verified Β·
1 Parent(s): 0f16452

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +62 -32
app.R CHANGED
@@ -1,6 +1,6 @@
1
  # ============================================================
2
  # QTL MAPPING SUITE β€” Full Single-File Shiny Application
3
- # FIXED: HuggingFace compatible
4
  # ============================================================
5
 
6
  suppressPackageStartupMessages({
@@ -237,6 +237,13 @@ build_cross_from_files <- function(geno_path, pheno_path, cross_type="bc") {
237
  skipped_ids=setdiff(sample_ids, common_ids))
238
  }
239
 
 
 
 
 
 
 
 
240
  # ── UI ────────────────────────────────────────────────────
241
  ui <- fluidPage(
242
  useShinyjs(),
@@ -265,7 +272,7 @@ ui <- fluidPage(
265
  div(class="qtl-top",
266
  div(class="tlmark","Q"),
267
  span(class="tlname","QTL Mapping Suite"),
268
- span(class="tlver","v2.1"),
269
  div(class="tldiv"),
270
  div(class="stpill", div(class="dot"), span(id="stlbl","Ready")),
271
  div(class="tlright", downloadButton("dl_full","Export All",class="btn-tn"))
@@ -734,9 +741,12 @@ server <- function(input, output, session) {
734
  sel <- if (!is.null(input$ex_pheno_col) && input$ex_pheno_col %in% pn) input$ex_pheno_col else pn[1]
735
  phe <- pull.pheno(rv$cross, which(pn == sel))
736
  df <- data.frame(y=phe[!is.na(phe)])
737
- # FIX: use stat(density) instead of after_stat(density) for compatibility
 
 
738
  p <- ggplot(df, aes(x=y)) +
739
- geom_histogram(aes(y=stat(density)), bins=30, fill="#1a6b3a", color="white", alpha=.85) +
 
740
  geom_density(color="#c47c1a", linewidth=1) +
741
  labs(x=sel, y="Density", title=paste("Phenotype:",sel)) +
742
  theme_minimal() + theme(panel.grid.minor=element_blank())
@@ -778,10 +788,15 @@ server <- function(input, output, session) {
778
  cn <- intersect(c("1","2","3"), colnames(df))
779
  names(df)[names(df) %in% cn] <- lbl[cn]
780
  gn <- intersect(c("AA","AB","BB"), names(df))
781
- # FIX: use tidyr::pivot_longer safely
782
- dl <- tidyr::pivot_longer(df, cols=all_of(gn), names_to="geno", values_to="count")
 
 
 
783
  dl$idx <- match(dl$marker, unique(dl$marker))
784
- p <- ggplot(dl, aes(x=idx, y=count, fill=geno, text=paste0("Marker:",marker,"<br>Geno:",geno,"<br>N:",count))) +
 
 
785
  geom_col(position="fill", width=1) +
786
  scale_fill_manual(values=c(AA="#1a6b3a",AB="#c47c1a",BB="#1a4a8a")) +
787
  labs(x="Marker Index", y="Proportion", fill="Genotype") +
@@ -1012,13 +1027,17 @@ server <- function(input, output, session) {
1012
 
1013
  output$pl_interval <- renderPlotly({
1014
  req(length(rv$scan_list) > 0); df <- comb_df(); thr <- thresh_val()
1015
- # FIX: use base R instead of slice_max
1016
- pk <- do.call(rbind, lapply(split(df[df$lod >= thr, ], interaction(df[df$lod >= thr, "model"], df[df$lod >= thr, "chr"])), function(sub) {
1017
- if (!nrow(sub)) return(NULL)
1018
- sub[which.max(sub$lod), ]
1019
- }))
1020
- pk <- pk[!sapply(pk, is.null), ]
 
 
 
1021
  if (is.null(pk) || nrow(pk) == 0) return(plotly_empty() %>% layout(title="No significant QTL"))
 
1022
  int_df <- do.call(rbind, lapply(seq_len(nrow(pk)), function(i) {
1023
  r <- pk[i, ]; sub <- df[df$model == r$model & df$chr == r$chr, ]; sub <- sub[order(sub$pos), ]
1024
  hl <- r$lod - 1.5
@@ -1113,12 +1132,18 @@ server <- function(input, output, session) {
1113
 
1114
  output$pl_heatmap <- renderPlotly({
1115
  req(length(rv$scan_list) > 1); df <- comb_df()
1116
- # FIX: use base R instead of slice_max
1117
- top_mk <- names(sort(tapply(df$lod, df$marker, max), decreasing=TRUE)[1:min(60,length(unique(df$marker)))])
 
 
 
1118
  hm <- df[df$marker %in% top_mk, c("marker","model","lod")]
1119
- hm <- tidyr::pivot_wider(hm, names_from=model, values_from=lod, values_fill=0)
1120
- hm <- as.data.frame(hm)
1121
- rn <- hm$marker; hm$marker <- NULL; mat <- as.matrix(hm)
 
 
 
1122
  plot_ly(z=mat, x=colnames(mat), y=rn, type="heatmap", colorscale="Viridis",
1123
  hovertemplate="Model: %{x}<br>Marker: %{y}<br>LOD: %{z:.2f}<extra></extra>") %>%
1124
  layout(font=list(family="sans-serif"), xaxis=list(title="Model"), yaxis=list(title="Marker",tickfont=list(size=8)))
@@ -1166,7 +1191,7 @@ server <- function(input, output, session) {
1166
  ggplotly(p, tooltip="text") %>% layout(font=list(family="sans-serif"))
1167
  })
1168
 
1169
- # ── CIRCOS β€” fixed: removed unsupported canvas params ────
1170
  circos_render <- function() {
1171
  req(rv$cross, length(rv$scan_list) > 0)
1172
  df <- comb_df(); thr <- thresh_val(); mods <- names(rv$scan_list)
@@ -1174,32 +1199,37 @@ server <- function(input, output, session) {
1174
  clens <- sapply(map, max)
1175
  par(bg="white", mar=c(1,1,3,1))
1176
  circos.clear()
1177
- # FIX: removed canvas.xlim/canvas.ylim β€” not supported in all versions
 
1178
  circos.par(
1179
  "track.height" = 0.08,
1180
- "cell.padding" = c(0,0,0,0),
1181
  "gap.after" = rep(2, length(cnms)),
1182
  "start.degree" = 90,
1183
  "clock.wise" = TRUE
1184
  )
1185
- circos.initialize(factors=cnms, xlim=cbind(rep(0,length(cnms)), clens))
1186
- circos.trackPlotRegion(ylim=c(0,1), track.height=0.05, bg.border=NA, bg.col="#f4f4f2",
1187
- panel.fun=function(x,y) {
 
1188
  chr <- get.cell.meta.data("sector.index")
1189
  circos.text(mean(get.cell.meta.data("xlim")), 0.5, chr,
1190
  facing="clockwise", niceFacing=TRUE, cex=.6, col="#2a2a27", font=2)
1191
- })
1192
- mpal <- c("#1a6b3a","#2d9e58","#c47c1a","#1a4a8a","#b83232","#7b3fa0","#1a8a8a","#8a5a1a","#555555","#e05c00")
 
 
1193
  for (mi in seq_along(mods)) {
1194
  m <- mods[mi]
1195
- mc <- mpal[((mi-1) %% length(mpal)) + 1]
1196
- mclt <- paste0(mc,"44")
1197
  sc <- rv$scan_list[[m]]; lc <- ncol(sc)
1198
- dfm <- data.frame(chr=as.character(sc$chr), pos=sc$pos, lod=sc[,lc], stringsAsFactors=FALSE)
1199
  dfm <- dfm[dfm$chr %in% cnms, ]
1200
  mxl <- max(dfm$lod, na.rm=TRUE); if (mxl < .01) mxl <- 1
1201
- circos.trackPlotRegion(ylim=c(0,mxl), track.height=0.07, bg.border="#e8e8e5", bg.col="#fafafa",
1202
- panel.fun=function(x,y) {
 
1203
  cs <- get.cell.meta.data("sector.index")
1204
  sub <- dfm[dfm$chr == cs, , drop=FALSE]
1205
  if (!nrow(sub)) return()
@@ -1208,7 +1238,7 @@ server <- function(input, output, session) {
1208
  ys <- c(0, sub$lod, 0)
1209
  circos.polygon(xs, ys, col=mclt, border=NA)
1210
  circos.lines(sub$pos, sub$lod, col=mc, lwd=1.2)
1211
- circos.lines(range(sub$pos), c(thr,thr), col="#b83232", lwd=.7, lty=2)
1212
  ss <- sub[sub$lod >= thr, ]
1213
  if (nrow(ss) > 0) {
1214
  pk <- ss[which.max(ss$lod), ]
 
1
  # ============================================================
2
  # QTL MAPPING SUITE β€” Full Single-File Shiny Application
3
+ # FIXED v2.2: All ggplot2/dplyr/tidyr compatibility patches
4
  # ============================================================
5
 
6
  suppressPackageStartupMessages({
 
237
  skipped_ids=setdiff(sample_ids, common_ids))
238
  }
239
 
240
+ # ── Compatibility helper: detect ggplot2 version for stat() vs after_stat() ──
241
+ # FIX: avoid stat(density) which errors in ggplot2 >= 3.4.0
242
+ .gg_after_stat_density <- function() {
243
+ vv <- tryCatch(packageVersion("ggplot2"), error=function(e) package_version("3.0.0"))
244
+ vv >= "3.3.0"
245
+ }
246
+
247
  # ── UI ────────────────────────────────────────────────────
248
  ui <- fluidPage(
249
  useShinyjs(),
 
272
  div(class="qtl-top",
273
  div(class="tlmark","Q"),
274
  span(class="tlname","QTL Mapping Suite"),
275
+ span(class="tlver","v2.2"),
276
  div(class="tldiv"),
277
  div(class="stpill", div(class="dot"), span(id="stlbl","Ready")),
278
  div(class="tlright", downloadButton("dl_full","Export All",class="btn-tn"))
 
741
  sel <- if (!is.null(input$ex_pheno_col) && input$ex_pheno_col %in% pn) input$ex_pheno_col else pn[1]
742
  phe <- pull.pheno(rv$cross, which(pn == sel))
743
  df <- data.frame(y=phe[!is.na(phe)])
744
+
745
+ # FIX: Use after_stat(density) instead of deprecated stat(density)
746
+ # Both work in ggplot2 >= 3.3; after_stat is the correct modern form
747
  p <- ggplot(df, aes(x=y)) +
748
+ geom_histogram(aes(y=after_stat(density)), bins=30,
749
+ fill="#1a6b3a", color="white", alpha=.85) +
750
  geom_density(color="#c47c1a", linewidth=1) +
751
  labs(x=sel, y="Density", title=paste("Phenotype:",sel)) +
752
  theme_minimal() + theme(panel.grid.minor=element_blank())
 
788
  cn <- intersect(c("1","2","3"), colnames(df))
789
  names(df)[names(df) %in% cn] <- lbl[cn]
790
  gn <- intersect(c("AA","AB","BB"), names(df))
791
+
792
+ # FIX: Use base reshape instead of tidyr::pivot_longer to avoid all_of() issues
793
+ dl <- do.call(rbind, lapply(gn, function(g) {
794
+ data.frame(marker=df$marker, geno=g, count=df[[g]], stringsAsFactors=FALSE)
795
+ }))
796
  dl$idx <- match(dl$marker, unique(dl$marker))
797
+
798
+ p <- ggplot(dl, aes(x=idx, y=count, fill=geno,
799
+ text=paste0("Marker:",marker,"<br>Geno:",geno,"<br>N:",count))) +
800
  geom_col(position="fill", width=1) +
801
  scale_fill_manual(values=c(AA="#1a6b3a",AB="#c47c1a",BB="#1a4a8a")) +
802
  labs(x="Marker Index", y="Proportion", fill="Genotype") +
 
1027
 
1028
  output$pl_interval <- renderPlotly({
1029
  req(length(rv$scan_list) > 0); df <- comb_df(); thr <- thresh_val()
1030
+ sig_sub <- df[df$lod >= thr, ]
1031
+ if (!nrow(sig_sub)) return(plotly_empty() %>% layout(title="No significant QTL"))
1032
+
1033
+ # FIX: base R peak-per-chr-model aggregation (replaces slice_max)
1034
+ pk_list <- lapply(
1035
+ split(sig_sub, paste0(sig_sub$model, "__", sig_sub$chr)),
1036
+ function(sub) { if (!nrow(sub)) return(NULL); sub[which.max(sub$lod), ] }
1037
+ )
1038
+ pk <- do.call(rbind, Filter(Negate(is.null), pk_list))
1039
  if (is.null(pk) || nrow(pk) == 0) return(plotly_empty() %>% layout(title="No significant QTL"))
1040
+
1041
  int_df <- do.call(rbind, lapply(seq_len(nrow(pk)), function(i) {
1042
  r <- pk[i, ]; sub <- df[df$model == r$model & df$chr == r$chr, ]; sub <- sub[order(sub$pos), ]
1043
  hl <- r$lod - 1.5
 
1132
 
1133
  output$pl_heatmap <- renderPlotly({
1134
  req(length(rv$scan_list) > 1); df <- comb_df()
1135
+
1136
+ # FIX: base R top-marker selection (replaces slice_max)
1137
+ mk_max <- tapply(df$lod, df$marker, max)
1138
+ top_mk <- names(sort(mk_max, decreasing=TRUE)[seq_len(min(60, length(mk_max)))])
1139
+
1140
  hm <- df[df$marker %in% top_mk, c("marker","model","lod")]
1141
+ # FIX: base R pivot_wider equivalent
1142
+ hm_wide <- reshape(hm, idvar="marker", timevar="model", direction="wide")
1143
+ colnames(hm_wide) <- gsub("^lod\\.", "", colnames(hm_wide))
1144
+ rn <- hm_wide$marker; hm_wide$marker <- NULL
1145
+ hm_wide[is.na(hm_wide)] <- 0
1146
+ mat <- as.matrix(hm_wide)
1147
  plot_ly(z=mat, x=colnames(mat), y=rn, type="heatmap", colorscale="Viridis",
1148
  hovertemplate="Model: %{x}<br>Marker: %{y}<br>LOD: %{z:.2f}<extra></extra>") %>%
1149
  layout(font=list(family="sans-serif"), xaxis=list(title="Model"), yaxis=list(title="Marker",tickfont=list(size=8)))
 
1191
  ggplotly(p, tooltip="text") %>% layout(font=list(family="sans-serif"))
1192
  })
1193
 
1194
+ # ── CIRCOS ────────────────────────────────────────────────
1195
  circos_render <- function() {
1196
  req(rv$cross, length(rv$scan_list) > 0)
1197
  df <- comb_df(); thr <- thresh_val(); mods <- names(rv$scan_list)
 
1199
  clens <- sapply(map, max)
1200
  par(bg="white", mar=c(1,1,3,1))
1201
  circos.clear()
1202
+ # FIX: removed canvas.xlim/canvas.ylim and other unsupported params;
1203
+ # use only universally supported circos.par settings
1204
  circos.par(
1205
  "track.height" = 0.08,
1206
+ "cell.padding" = c(0, 0, 0, 0),
1207
  "gap.after" = rep(2, length(cnms)),
1208
  "start.degree" = 90,
1209
  "clock.wise" = TRUE
1210
  )
1211
+ circos.initialize(factors=cnms, xlim=cbind(rep(0, length(cnms)), clens))
1212
+ circos.trackPlotRegion(
1213
+ ylim=c(0, 1), track.height=0.05, bg.border=NA, bg.col="#f4f4f2",
1214
+ panel.fun=function(x, y) {
1215
  chr <- get.cell.meta.data("sector.index")
1216
  circos.text(mean(get.cell.meta.data("xlim")), 0.5, chr,
1217
  facing="clockwise", niceFacing=TRUE, cex=.6, col="#2a2a27", font=2)
1218
+ }
1219
+ )
1220
+ mpal <- c("#1a6b3a","#2d9e58","#c47c1a","#1a4a8a","#b83232",
1221
+ "#7b3fa0","#1a8a8a","#8a5a1a","#555555","#e05c00")
1222
  for (mi in seq_along(mods)) {
1223
  m <- mods[mi]
1224
+ mc <- mpal[((mi - 1) %% length(mpal)) + 1]
1225
+ mclt <- paste0(mc, "44")
1226
  sc <- rv$scan_list[[m]]; lc <- ncol(sc)
1227
+ dfm <- data.frame(chr=as.character(sc$chr), pos=sc$pos, lod=sc[, lc], stringsAsFactors=FALSE)
1228
  dfm <- dfm[dfm$chr %in% cnms, ]
1229
  mxl <- max(dfm$lod, na.rm=TRUE); if (mxl < .01) mxl <- 1
1230
+ circos.trackPlotRegion(
1231
+ ylim=c(0, mxl), track.height=0.07, bg.border="#e8e8e5", bg.col="#fafafa",
1232
+ panel.fun=function(x, y) {
1233
  cs <- get.cell.meta.data("sector.index")
1234
  sub <- dfm[dfm$chr == cs, , drop=FALSE]
1235
  if (!nrow(sub)) return()
 
1238
  ys <- c(0, sub$lod, 0)
1239
  circos.polygon(xs, ys, col=mclt, border=NA)
1240
  circos.lines(sub$pos, sub$lod, col=mc, lwd=1.2)
1241
+ circos.lines(range(sub$pos), c(thr, thr), col="#b83232", lwd=.7, lty=2)
1242
  ss <- sub[sub$lod >= thr, ]
1243
  if (nrow(ss) > 0) {
1244
  pk <- ss[which.max(ss$lod), ]