Spaces:
Sleeping
Sleeping
Update app.R
Browse files
app.R
CHANGED
|
@@ -17,9 +17,12 @@
|
|
| 17 |
# per-marker regression uses the original unscaled values for
|
| 18 |
# effect estimation.
|
| 19 |
# 8. ALL previous v11 fixes retained.
|
|
|
|
|
|
|
| 20 |
# ============================================================
|
| 21 |
|
| 22 |
options(repos = c(CRAN = "https://cloud.r-project.org"))
|
|
|
|
| 23 |
|
| 24 |
required_packages <- c(
|
| 25 |
"shiny", "randomForest", "caret", "DT", "ggplot2", "plotly",
|
|
@@ -963,14 +966,16 @@ qtl_scan_rf <- function(X, y, marker_info) {
|
|
| 963 |
# DISPATCHER
|
| 964 |
# ============================================================
|
| 965 |
qtl_scan <- function(X, y, marker_info, method = "CIM",
|
| 966 |
-
n_cofactors = 10, window_size = 10
|
|
|
|
| 967 |
cat("\n=== QTL SCAN | Method:", method, "===\n")
|
| 968 |
valid <- !is.na(y)
|
| 969 |
X <- X[valid, , drop = FALSE]; y <- y[valid]
|
| 970 |
nzv <- apply(X, 2, function(x) length(unique(na.omit(x))) < 2)
|
| 971 |
X <- X[, !nzv, drop = FALSE]
|
| 972 |
cat(" Markers after NZV:", ncol(X), "\n")
|
| 973 |
-
|
|
|
|
| 974 |
SIM = qtl_scan_sim(X, y, marker_info),
|
| 975 |
CIM = qtl_scan_cim(X, y, marker_info, n_cofactors, window_size),
|
| 976 |
MQM = qtl_scan_mqm(X, y, marker_info, n_cofactors, window_size),
|
|
@@ -979,6 +984,9 @@ qtl_scan <- function(X, y, marker_info, method = "CIM",
|
|
| 979 |
RF = qtl_scan_rf(X, y, marker_info),
|
| 980 |
data.frame(Message = paste("Unknown method:", method))
|
| 981 |
)
|
|
|
|
|
|
|
|
|
|
| 982 |
}
|
| 983 |
|
| 984 |
# ============================================================
|
|
@@ -1045,6 +1053,8 @@ permutation_threshold <- function(X, y, marker_info, method = "SIM",
|
|
| 1045 |
|
| 1046 |
thr <- quantile(max_lods, 1 - alpha, na.rm = TRUE)
|
| 1047 |
cat(" Permutation LOD threshold (alpha =", alpha, "):", round(thr, 3), "\n")
|
|
|
|
|
|
|
| 1048 |
thr
|
| 1049 |
}
|
| 1050 |
|
|
@@ -1470,6 +1480,8 @@ ui <- fluidPage(
|
|
| 1470 |
.speed-badge{background:#8338EC;color:#fff;border-radius:12px;padding:2px 8px;font-size:10px;font-weight:700;letter-spacing:0.5px;}
|
| 1471 |
.fix-badge{background:#06D6A0;color:#fff;border-radius:12px;padding:2px 8px;font-size:10px;font-weight:700;letter-spacing:0.5px;}
|
| 1472 |
.diag-box{background:#fff3cd;border:1px solid #ffc107;border-left:4px solid #fd7e14;border-radius:6px;padding:10px 14px;margin:6px 0;font-size:12px;font-family:monospace;}
|
|
|
|
|
|
|
| 1473 |
"))
|
| 1474 |
),
|
| 1475 |
|
|
@@ -1857,7 +1869,8 @@ server <- function(input, output, session) {
|
|
| 1857 |
compare_res = list(),
|
| 1858 |
compare_peaks = list(),
|
| 1859 |
perm_threshold = NULL,
|
| 1860 |
-
diag_info = NULL
|
|
|
|
| 1861 |
)
|
| 1862 |
|
| 1863 |
lod_thr <- reactive({ input$lod_threshold })
|
|
@@ -1997,10 +2010,21 @@ server <- function(input, output, session) {
|
|
| 1997 |
output$prev_pred <- renderDT({ req(rv$pred_test); datatable(rv$pred_test, options=list(scrollX=TRUE,pageLength=10)) })
|
| 1998 |
output$prev_pop <- renderDT({ req(rv$pop_data); datatable(head(rv$pop_data,20), options=list(scrollX=TRUE,pageLength=10)) })
|
| 1999 |
|
| 2000 |
-
# ---- MAIN ANALYSIS ----
|
| 2001 |
observeEvent(input$run_btn, {
|
| 2002 |
req(rv$geno, rv$pheno, input$pheno_col)
|
| 2003 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2004 |
|
| 2005 |
tryCatch({
|
| 2006 |
cs <- intersect(rownames(rv$geno), rownames(rv$pheno))
|
|
@@ -2012,6 +2036,8 @@ server <- function(input, output, session) {
|
|
| 2012 |
"Geno example: '", rownames(rv$geno)[1], "' | ",
|
| 2013 |
"Pheno example: '", rownames(rv$pheno)[1], "'"),
|
| 2014 |
type="error", duration=10)
|
|
|
|
|
|
|
| 2015 |
return()
|
| 2016 |
}
|
| 2017 |
|
|
@@ -2022,16 +2048,26 @@ server <- function(input, output, session) {
|
|
| 2022 |
if (sum(!is.na(y)) < 10) {
|
| 2023 |
showNotification(
|
| 2024 |
paste0("Only ", sum(!is.na(y)), " non-NA phenotype values. Need at least 10."),
|
| 2025 |
-
type="error", duration=10)
|
|
|
|
|
|
|
|
|
|
| 2026 |
}
|
| 2027 |
|
| 2028 |
if (var(y, na.rm=TRUE) < 1e-10) {
|
| 2029 |
-
showNotification("Phenotype has zero variance.", type="error", duration=8)
|
|
|
|
|
|
|
|
|
|
| 2030 |
}
|
| 2031 |
|
| 2032 |
partition <- tryCatch(safe_partition(y, val_prop = input$val_prop),
|
| 2033 |
error = function(e) { showNotification(e$message, type="error", duration=10); NULL })
|
| 2034 |
-
if (is.null(partition))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2035 |
|
| 2036 |
tr_idx <- partition$train; val_idx <- partition$val
|
| 2037 |
|
|
@@ -2043,10 +2079,14 @@ server <- function(input, output, session) {
|
|
| 2043 |
|
| 2044 |
cat(" Final train N:", length(y_tr), " val N:", length(y_v), "\n")
|
| 2045 |
|
| 2046 |
-
|
| 2047 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2048 |
if (isTRUE(input$run_permutation)) {
|
| 2049 |
-
incProgress(0, detail="Running parallel permutations...")
|
| 2050 |
rv$perm_threshold <- permutation_threshold(
|
| 2051 |
X_tr, y_tr, rv$marker_info,
|
| 2052 |
method = input$mapping_method, n_perm = input$n_perm,
|
|
@@ -2057,21 +2097,27 @@ server <- function(input, output, session) {
|
|
| 2057 |
" — click 'Apply Permutation Threshold' to use it."),
|
| 2058 |
type="message", duration=6)
|
| 2059 |
}
|
| 2060 |
-
|
| 2061 |
-
|
|
|
|
|
|
|
| 2062 |
rv$scan_all <- qtl_scan(X_tr, y_tr, rv$marker_info,
|
| 2063 |
method = input$mapping_method,
|
| 2064 |
n_cofactors = input$n_cofactors,
|
| 2065 |
window_size = input$window_size)
|
| 2066 |
-
|
| 2067 |
-
|
|
|
|
|
|
|
| 2068 |
rv$qtl_peaks <- find_qtl_peaks(rv$scan_all, lod_thr(), input$min_peak_distance)
|
| 2069 |
if (!is.null(rv$qtl_peaks) && nrow(rv$qtl_peaks) > 0) {
|
| 2070 |
rv$qtl_peaks <- add_precision_support_intervals(rv$qtl_peaks, rv$scan_all, input$lod_drop)
|
| 2071 |
cat("Found", nrow(rv$qtl_peaks), "QTL peaks\n")
|
| 2072 |
}
|
| 2073 |
-
|
| 2074 |
-
|
|
|
|
|
|
|
| 2075 |
nzv_i <- tryCatch(caret::nearZeroVar(X_tr), error=function(e) integer(0))
|
| 2076 |
Xtr_rf <- if (length(nzv_i) > 0) X_tr[,-nzv_i,drop=FALSE] else X_tr
|
| 2077 |
Xv_rf <- X_v[, intersect(colnames(Xtr_rf), colnames(X_v)), drop=FALSE]
|
|
@@ -2119,8 +2165,9 @@ server <- function(input, output, session) {
|
|
| 2119 |
rv$tr_pred <- data.frame(Observed=y_tr, Predicted=ytr_p)
|
| 2120 |
rv$val_pred <- data.frame(Observed=y_v, Predicted=yv_p)
|
| 2121 |
}
|
| 2122 |
-
|
| 2123 |
-
|
|
|
|
| 2124 |
|
| 2125 |
n_sig <- if (!is.null(rv$qtl_peaks) && nrow(rv$qtl_peaks) > 0) nrow(rv$qtl_peaks) else 0
|
| 2126 |
showNotification(paste(input$mapping_method,"done —",n_sig,"QTL peaks detected"),
|
|
@@ -2131,9 +2178,12 @@ server <- function(input, output, session) {
|
|
| 2131 |
showNotification(paste("Analysis error:", e$message), type="error", duration=10)
|
| 2132 |
cat("ERROR:", e$message, "\n")
|
| 2133 |
})
|
|
|
|
|
|
|
|
|
|
| 2134 |
})
|
| 2135 |
|
| 2136 |
-
# ---- COMPARE MODELS ----
|
| 2137 |
output$compare_status_ui <- renderUI({
|
| 2138 |
if (length(rv$compare_res) == 0) return(NULL)
|
| 2139 |
div(class="good-box",
|
|
@@ -2152,11 +2202,15 @@ server <- function(input, output, session) {
|
|
| 2152 |
observeEvent(input$run_compare, {
|
| 2153 |
req(rv$geno, rv$pheno, input$compare_methods, input$pheno_col)
|
| 2154 |
if (length(input$compare_methods) == 0) {
|
| 2155 |
-
showNotification("Select at least one method", type="warning")
|
|
|
|
|
|
|
| 2156 |
|
| 2157 |
cs <- intersect(rownames(rv$geno), rownames(rv$pheno))
|
| 2158 |
if (length(cs) == 0) {
|
| 2159 |
-
showNotification("No common samples — check file IDs match", type="error")
|
|
|
|
|
|
|
| 2160 |
|
| 2161 |
X <- rv$geno[cs, , drop=FALSE]
|
| 2162 |
y <- suppressWarnings(as.numeric(as.character(rv$pheno[cs, input$pheno_col])))
|
|
@@ -2171,9 +2225,13 @@ server <- function(input, output, session) {
|
|
| 2171 |
X_tr <- X_tr[ok,,drop=FALSE]; y_tr <- y_tr[ok]
|
| 2172 |
|
| 2173 |
clist <- list(); peaks_list <- list()
|
| 2174 |
-
|
| 2175 |
-
|
| 2176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2177 |
res <- tryCatch(
|
| 2178 |
qtl_scan(X_tr, y_tr, rv$marker_info, method=mth,
|
| 2179 |
n_cofactors=input$n_cofactors, window_size=input$window_size),
|
|
@@ -2188,6 +2246,7 @@ server <- function(input, output, session) {
|
|
| 2188 |
}
|
| 2189 |
}
|
| 2190 |
})
|
|
|
|
| 2191 |
rv$compare_res <- clist
|
| 2192 |
rv$compare_peaks <- peaks_list
|
| 2193 |
showNotification(paste("Compared", length(clist),"models"), type="message")
|
|
@@ -2421,7 +2480,9 @@ server <- function(input, output, session) {
|
|
| 2421 |
observeEvent(input$pred_btn, {
|
| 2422 |
if (is.null(rv$model_fit)) { showNotification("Run analysis first",type="error"); return() }
|
| 2423 |
if (is.null(rv$test_geno)) { showNotification("Upload test genotype first",type="error"); return() }
|
|
|
|
| 2424 |
withProgress(message="Predicting test samples...", value=0.3, {
|
|
|
|
| 2425 |
train_cols <- rv$model_cols
|
| 2426 |
Xpred <- as.data.frame(matrix(0, nrow=nrow(rv$test_geno), ncol=length(train_cols),
|
| 2427 |
dimnames=list(rownames(rv$test_geno), train_cols)))
|
|
@@ -2429,9 +2490,10 @@ server <- function(input, output, session) {
|
|
| 2429 |
v <- suppressWarnings(as.numeric(as.character(rv$test_geno[[col]])))
|
| 2430 |
v[is.na(v)] <- 0; Xpred[[col]] <- v
|
| 2431 |
}
|
| 2432 |
-
incProgress(0.
|
| 2433 |
preds <- tryCatch(as.numeric(predict(rv$model_fit, newdata=Xpred)),
|
| 2434 |
error=function(e) rep(NA, nrow(Xpred)))
|
|
|
|
| 2435 |
rv$pred_test <- data.frame(Sample_ID=rownames(rv$test_geno),
|
| 2436 |
Predicted_Phenotype=round(preds,4))
|
| 2437 |
})
|
|
@@ -2441,18 +2503,34 @@ server <- function(input, output, session) {
|
|
| 2441 |
# ---- DOMESTICATION ----
|
| 2442 |
observeEvent(input$run_dom, {
|
| 2443 |
req(rv$geno, rv$pop_data, rv$marker_info)
|
| 2444 |
-
|
| 2445 |
-
|
| 2446 |
-
|
| 2447 |
-
|
| 2448 |
-
|
| 2449 |
-
|
| 2450 |
-
|
| 2451 |
-
|
| 2452 |
-
|
| 2453 |
-
|
| 2454 |
-
|
| 2455 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2456 |
})
|
| 2457 |
|
| 2458 |
make_dom_plotly <- function(data, y_col, title, threshold, y_lab=y_col) {
|
|
|
|
| 17 |
# per-marker regression uses the original unscaled values for
|
| 18 |
# effect estimation.
|
| 19 |
# 8. ALL previous v11 fixes retained.
|
| 20 |
+
# 9. PROGRESS BAR FIX: Added proper progress tracking with incProgress()
|
| 21 |
+
# and detailed status messages for all long-running operations.
|
| 22 |
# ============================================================
|
| 23 |
|
| 24 |
options(repos = c(CRAN = "https://cloud.r-project.org"))
|
| 25 |
+
options(shiny.maxRequestSize = 100 * 1024^2) # 100MB max upload
|
| 26 |
|
| 27 |
required_packages <- c(
|
| 28 |
"shiny", "randomForest", "caret", "DT", "ggplot2", "plotly",
|
|
|
|
| 966 |
# DISPATCHER
|
| 967 |
# ============================================================
|
| 968 |
qtl_scan <- function(X, y, marker_info, method = "CIM",
|
| 969 |
+
n_cofactors = 10, window_size = 10,
|
| 970 |
+
progress_cb = NULL) {
|
| 971 |
cat("\n=== QTL SCAN | Method:", method, "===\n")
|
| 972 |
valid <- !is.na(y)
|
| 973 |
X <- X[valid, , drop = FALSE]; y <- y[valid]
|
| 974 |
nzv <- apply(X, 2, function(x) length(unique(na.omit(x))) < 2)
|
| 975 |
X <- X[, !nzv, drop = FALSE]
|
| 976 |
cat(" Markers after NZV:", ncol(X), "\n")
|
| 977 |
+
|
| 978 |
+
result <- switch(method,
|
| 979 |
SIM = qtl_scan_sim(X, y, marker_info),
|
| 980 |
CIM = qtl_scan_cim(X, y, marker_info, n_cofactors, window_size),
|
| 981 |
MQM = qtl_scan_mqm(X, y, marker_info, n_cofactors, window_size),
|
|
|
|
| 984 |
RF = qtl_scan_rf(X, y, marker_info),
|
| 985 |
data.frame(Message = paste("Unknown method:", method))
|
| 986 |
)
|
| 987 |
+
|
| 988 |
+
if (!is.null(progress_cb)) progress_cb()
|
| 989 |
+
result
|
| 990 |
}
|
| 991 |
|
| 992 |
# ============================================================
|
|
|
|
| 1053 |
|
| 1054 |
thr <- quantile(max_lods, 1 - alpha, na.rm = TRUE)
|
| 1055 |
cat(" Permutation LOD threshold (alpha =", alpha, "):", round(thr, 3), "\n")
|
| 1056 |
+
|
| 1057 |
+
if (!is.null(progress_cb)) progress_cb()
|
| 1058 |
thr
|
| 1059 |
}
|
| 1060 |
|
|
|
|
| 1480 |
.speed-badge{background:#8338EC;color:#fff;border-radius:12px;padding:2px 8px;font-size:10px;font-weight:700;letter-spacing:0.5px;}
|
| 1481 |
.fix-badge{background:#06D6A0;color:#fff;border-radius:12px;padding:2px 8px;font-size:10px;font-weight:700;letter-spacing:0.5px;}
|
| 1482 |
.diag-box{background:#fff3cd;border:1px solid #ffc107;border-left:4px solid #fd7e14;border-radius:6px;padding:10px 14px;margin:6px 0;font-size:12px;font-family:monospace;}
|
| 1483 |
+
.progress-bar-custom{height:6px;background:#e9ecef;border-radius:3px;overflow:hidden;margin:10px 0;}
|
| 1484 |
+
.progress-fill{width:0%;height:100%;background:#06D6A0;transition:width 0.3s ease;}
|
| 1485 |
"))
|
| 1486 |
),
|
| 1487 |
|
|
|
|
| 1869 |
compare_res = list(),
|
| 1870 |
compare_peaks = list(),
|
| 1871 |
perm_threshold = NULL,
|
| 1872 |
+
diag_info = NULL,
|
| 1873 |
+
analysis_running = FALSE
|
| 1874 |
)
|
| 1875 |
|
| 1876 |
lod_thr <- reactive({ input$lod_threshold })
|
|
|
|
| 2010 |
output$prev_pred <- renderDT({ req(rv$pred_test); datatable(rv$pred_test, options=list(scrollX=TRUE,pageLength=10)) })
|
| 2011 |
output$prev_pop <- renderDT({ req(rv$pop_data); datatable(head(rv$pop_data,20), options=list(scrollX=TRUE,pageLength=10)) })
|
| 2012 |
|
| 2013 |
+
# ---- MAIN ANALYSIS with PROGRESS BAR ----
|
| 2014 |
observeEvent(input$run_btn, {
|
| 2015 |
req(rv$geno, rv$pheno, input$pheno_col)
|
| 2016 |
+
|
| 2017 |
+
if (rv$analysis_running) {
|
| 2018 |
+
showNotification("Analysis already running. Please wait...", type = "warning")
|
| 2019 |
+
return()
|
| 2020 |
+
}
|
| 2021 |
+
|
| 2022 |
+
rv$analysis_running <- TRUE
|
| 2023 |
+
|
| 2024 |
+
# Disable the run button during analysis
|
| 2025 |
+
shinyjs::disable("run_btn")
|
| 2026 |
+
|
| 2027 |
+
showNotification(paste("Starting", input$mapping_method, "analysis..."), type = "message")
|
| 2028 |
|
| 2029 |
tryCatch({
|
| 2030 |
cs <- intersect(rownames(rv$geno), rownames(rv$pheno))
|
|
|
|
| 2036 |
"Geno example: '", rownames(rv$geno)[1], "' | ",
|
| 2037 |
"Pheno example: '", rownames(rv$pheno)[1], "'"),
|
| 2038 |
type="error", duration=10)
|
| 2039 |
+
rv$analysis_running <- FALSE
|
| 2040 |
+
shinyjs::enable("run_btn")
|
| 2041 |
return()
|
| 2042 |
}
|
| 2043 |
|
|
|
|
| 2048 |
if (sum(!is.na(y)) < 10) {
|
| 2049 |
showNotification(
|
| 2050 |
paste0("Only ", sum(!is.na(y)), " non-NA phenotype values. Need at least 10."),
|
| 2051 |
+
type="error", duration=10)
|
| 2052 |
+
rv$analysis_running <- FALSE
|
| 2053 |
+
shinyjs::enable("run_btn")
|
| 2054 |
+
return()
|
| 2055 |
}
|
| 2056 |
|
| 2057 |
if (var(y, na.rm=TRUE) < 1e-10) {
|
| 2058 |
+
showNotification("Phenotype has zero variance.", type="error", duration=8)
|
| 2059 |
+
rv$analysis_running <- FALSE
|
| 2060 |
+
shinyjs::enable("run_btn")
|
| 2061 |
+
return()
|
| 2062 |
}
|
| 2063 |
|
| 2064 |
partition <- tryCatch(safe_partition(y, val_prop = input$val_prop),
|
| 2065 |
error = function(e) { showNotification(e$message, type="error", duration=10); NULL })
|
| 2066 |
+
if (is.null(partition)) {
|
| 2067 |
+
rv$analysis_running <- FALSE
|
| 2068 |
+
shinyjs::enable("run_btn")
|
| 2069 |
+
return()
|
| 2070 |
+
}
|
| 2071 |
|
| 2072 |
tr_idx <- partition$train; val_idx <- partition$val
|
| 2073 |
|
|
|
|
| 2079 |
|
| 2080 |
cat(" Final train N:", length(y_tr), " val N:", length(y_v), "\n")
|
| 2081 |
|
| 2082 |
+
# Show progress bar
|
| 2083 |
+
withProgress(message = paste("Running", input$mapping_method, "analysis..."), value = 0, {
|
| 2084 |
+
|
| 2085 |
+
# Step 1: Permutation (if selected) - 0% to 20%
|
| 2086 |
+
incProgress(0.05, detail = "Preparing data...")
|
| 2087 |
+
|
| 2088 |
if (isTRUE(input$run_permutation)) {
|
| 2089 |
+
incProgress(0, detail = "Running parallel permutations...")
|
| 2090 |
rv$perm_threshold <- permutation_threshold(
|
| 2091 |
X_tr, y_tr, rv$marker_info,
|
| 2092 |
method = input$mapping_method, n_perm = input$n_perm,
|
|
|
|
| 2097 |
" — click 'Apply Permutation Threshold' to use it."),
|
| 2098 |
type="message", duration=6)
|
| 2099 |
}
|
| 2100 |
+
|
| 2101 |
+
incProgress(0.2, detail = "Running QTL scan...")
|
| 2102 |
+
|
| 2103 |
+
# Step 2: QTL Scan - 20% to 60%
|
| 2104 |
rv$scan_all <- qtl_scan(X_tr, y_tr, rv$marker_info,
|
| 2105 |
method = input$mapping_method,
|
| 2106 |
n_cofactors = input$n_cofactors,
|
| 2107 |
window_size = input$window_size)
|
| 2108 |
+
|
| 2109 |
+
incProgress(0.4, detail = "Detecting QTL peaks...")
|
| 2110 |
+
|
| 2111 |
+
# Step 3: Peak detection - 60% to 70%
|
| 2112 |
rv$qtl_peaks <- find_qtl_peaks(rv$scan_all, lod_thr(), input$min_peak_distance)
|
| 2113 |
if (!is.null(rv$qtl_peaks) && nrow(rv$qtl_peaks) > 0) {
|
| 2114 |
rv$qtl_peaks <- add_precision_support_intervals(rv$qtl_peaks, rv$scan_all, input$lod_drop)
|
| 2115 |
cat("Found", nrow(rv$qtl_peaks), "QTL peaks\n")
|
| 2116 |
}
|
| 2117 |
+
|
| 2118 |
+
incProgress(0.1, detail = "Building prediction model...")
|
| 2119 |
+
|
| 2120 |
+
# Step 4: Prediction model - 70% to 90%
|
| 2121 |
nzv_i <- tryCatch(caret::nearZeroVar(X_tr), error=function(e) integer(0))
|
| 2122 |
Xtr_rf <- if (length(nzv_i) > 0) X_tr[,-nzv_i,drop=FALSE] else X_tr
|
| 2123 |
Xv_rf <- X_v[, intersect(colnames(Xtr_rf), colnames(X_v)), drop=FALSE]
|
|
|
|
| 2165 |
rv$tr_pred <- data.frame(Observed=y_tr, Predicted=ytr_p)
|
| 2166 |
rv$val_pred <- data.frame(Observed=y_v, Predicted=yv_p)
|
| 2167 |
}
|
| 2168 |
+
|
| 2169 |
+
incProgress(0.1, detail = "Finalizing...")
|
| 2170 |
+
}) # End withProgress
|
| 2171 |
|
| 2172 |
n_sig <- if (!is.null(rv$qtl_peaks) && nrow(rv$qtl_peaks) > 0) nrow(rv$qtl_peaks) else 0
|
| 2173 |
showNotification(paste(input$mapping_method,"done —",n_sig,"QTL peaks detected"),
|
|
|
|
| 2178 |
showNotification(paste("Analysis error:", e$message), type="error", duration=10)
|
| 2179 |
cat("ERROR:", e$message, "\n")
|
| 2180 |
})
|
| 2181 |
+
|
| 2182 |
+
rv$analysis_running <- FALSE
|
| 2183 |
+
shinyjs::enable("run_btn")
|
| 2184 |
})
|
| 2185 |
|
| 2186 |
+
# ---- COMPARE MODELS with PROGRESS BAR ----
|
| 2187 |
output$compare_status_ui <- renderUI({
|
| 2188 |
if (length(rv$compare_res) == 0) return(NULL)
|
| 2189 |
div(class="good-box",
|
|
|
|
| 2202 |
observeEvent(input$run_compare, {
|
| 2203 |
req(rv$geno, rv$pheno, input$compare_methods, input$pheno_col)
|
| 2204 |
if (length(input$compare_methods) == 0) {
|
| 2205 |
+
showNotification("Select at least one method", type="warning")
|
| 2206 |
+
return()
|
| 2207 |
+
}
|
| 2208 |
|
| 2209 |
cs <- intersect(rownames(rv$geno), rownames(rv$pheno))
|
| 2210 |
if (length(cs) == 0) {
|
| 2211 |
+
showNotification("No common samples — check file IDs match", type="error")
|
| 2212 |
+
return()
|
| 2213 |
+
}
|
| 2214 |
|
| 2215 |
X <- rv$geno[cs, , drop=FALSE]
|
| 2216 |
y <- suppressWarnings(as.numeric(as.character(rv$pheno[cs, input$pheno_col])))
|
|
|
|
| 2225 |
X_tr <- X_tr[ok,,drop=FALSE]; y_tr <- y_tr[ok]
|
| 2226 |
|
| 2227 |
clist <- list(); peaks_list <- list()
|
| 2228 |
+
|
| 2229 |
+
withProgress(message = "Comparing models...", value = 0, {
|
| 2230 |
+
total_methods <- length(input$compare_methods)
|
| 2231 |
+
for (i in seq_along(input$compare_methods)) {
|
| 2232 |
+
mth <- input$compare_methods[i]
|
| 2233 |
+
incProgress(1/total_methods, detail = paste("Running", mth))
|
| 2234 |
+
|
| 2235 |
res <- tryCatch(
|
| 2236 |
qtl_scan(X_tr, y_tr, rv$marker_info, method=mth,
|
| 2237 |
n_cofactors=input$n_cofactors, window_size=input$window_size),
|
|
|
|
| 2246 |
}
|
| 2247 |
}
|
| 2248 |
})
|
| 2249 |
+
|
| 2250 |
rv$compare_res <- clist
|
| 2251 |
rv$compare_peaks <- peaks_list
|
| 2252 |
showNotification(paste("Compared", length(clist),"models"), type="message")
|
|
|
|
| 2480 |
observeEvent(input$pred_btn, {
|
| 2481 |
if (is.null(rv$model_fit)) { showNotification("Run analysis first",type="error"); return() }
|
| 2482 |
if (is.null(rv$test_geno)) { showNotification("Upload test genotype first",type="error"); return() }
|
| 2483 |
+
|
| 2484 |
withProgress(message="Predicting test samples...", value=0.3, {
|
| 2485 |
+
incProgress(0.1, detail="Preparing data...")
|
| 2486 |
train_cols <- rv$model_cols
|
| 2487 |
Xpred <- as.data.frame(matrix(0, nrow=nrow(rv$test_geno), ncol=length(train_cols),
|
| 2488 |
dimnames=list(rownames(rv$test_geno), train_cols)))
|
|
|
|
| 2490 |
v <- suppressWarnings(as.numeric(as.character(rv$test_geno[[col]])))
|
| 2491 |
v[is.na(v)] <- 0; Xpred[[col]] <- v
|
| 2492 |
}
|
| 2493 |
+
incProgress(0.4, detail="Running predictions...")
|
| 2494 |
preds <- tryCatch(as.numeric(predict(rv$model_fit, newdata=Xpred)),
|
| 2495 |
error=function(e) rep(NA, nrow(Xpred)))
|
| 2496 |
+
incProgress(0.5, detail="Finalizing...")
|
| 2497 |
rv$pred_test <- data.frame(Sample_ID=rownames(rv$test_geno),
|
| 2498 |
Predicted_Phenotype=round(preds,4))
|
| 2499 |
})
|
|
|
|
| 2503 |
# ---- DOMESTICATION ----
|
| 2504 |
observeEvent(input$run_dom, {
|
| 2505 |
req(rv$geno, rv$pop_data, rv$marker_info)
|
| 2506 |
+
|
| 2507 |
+
withProgress(message="Running domestication analysis...", value=0, {
|
| 2508 |
+
incProgress(0.2, detail="Matching samples...")
|
| 2509 |
+
|
| 2510 |
+
tryCatch({
|
| 2511 |
+
cs <- intersect(rownames(rv$geno), rv$pop_data$id)
|
| 2512 |
+
X <- rv$geno[cs, , drop=FALSE]
|
| 2513 |
+
ps <- rv$pop_data[rv$pop_data$id %in% cs, ]
|
| 2514 |
+
|
| 2515 |
+
incProgress(0.4, detail="Calculating Fst and Tajima's D...")
|
| 2516 |
+
sel <- calc_selection_metrics(X, ps, rv$marker_info)
|
| 2517 |
+
|
| 2518 |
+
if (is.null(sel)) {
|
| 2519 |
+
showNotification("No selection metrics computed",type="error")
|
| 2520 |
+
incProgress(1)
|
| 2521 |
+
return()
|
| 2522 |
+
}
|
| 2523 |
+
|
| 2524 |
+
incProgress(0.3, detail="Identifying signatures...")
|
| 2525 |
+
sel$Significant <- (!is.na(sel$Fst) & sel$Fst > input$fst_thr) |
|
| 2526 |
+
(!is.na(sel$Tajima_D_Dom) & sel$Tajima_D_Dom < input$tajima_thr)
|
| 2527 |
+
rv$sel_metrics <- sel
|
| 2528 |
+
|
| 2529 |
+
incProgress(0.1, detail="Done")
|
| 2530 |
+
showNotification(paste("Found",sum(sel$Significant,na.rm=TRUE),"selection signatures"),
|
| 2531 |
+
type="message")
|
| 2532 |
+
}, error=function(e) showNotification(paste("Domestication error:",e$message),type="error"))
|
| 2533 |
+
})
|
| 2534 |
})
|
| 2535 |
|
| 2536 |
make_dom_plotly <- function(data, y_col, title, threshold, y_lab=y_col) {
|