Spaces:
Sleeping
Sleeping
Update app.R
Browse files
app.R
CHANGED
|
@@ -1,17 +1,17 @@
|
|
| 1 |
# ============================================================
|
| 2 |
-
# QTL-Mapper
|
| 3 |
#
|
| 4 |
-
#
|
| 5 |
-
# -
|
| 6 |
-
#
|
| 7 |
-
# -
|
| 8 |
-
#
|
| 9 |
-
#
|
| 10 |
-
#
|
| 11 |
-
# •
|
| 12 |
-
# •
|
| 13 |
-
#
|
| 14 |
-
#
|
| 15 |
# ============================================================
|
| 16 |
|
| 17 |
options(repos = c(CRAN = "https://cloud.r-project.org"))
|
|
@@ -39,7 +39,7 @@ cn <- function(x) {
|
|
| 39 |
}
|
| 40 |
|
| 41 |
# ============================================================
|
| 42 |
-
# COLOR PALETTES
|
| 43 |
# ============================================================
|
| 44 |
CHR_PALETTE <- c(
|
| 45 |
"#FF006E","#FB5607","#FFBE0B","#8338EC","#3A86FF",
|
|
@@ -56,7 +56,6 @@ MODEL_COLORS <- c(
|
|
| 56 |
RF = "#8338EC"
|
| 57 |
)
|
| 58 |
|
| 59 |
-
# LOD-based color scale: low=blue → mid=yellow → high=red
|
| 60 |
LOD_COLOR_SCALE <- c("#3A86FF","#06D6A0","#FFBE0B","#FB5607","#FF006E","#8338EC")
|
| 61 |
|
| 62 |
# ============================================================
|
|
@@ -66,7 +65,7 @@ MAPPING_MODELS <- list(
|
|
| 66 |
"SIM" = list(label = "Simple Interval Mapping (SIM)",
|
| 67 |
desc = "QTL Cartographer-style single-marker regression; LOD = (n/2)*log10(RSS0/RSS1)"),
|
| 68 |
"CIM" = list(label = "Composite Interval Mapping (CIM)",
|
| 69 |
-
desc = "QTL Cartographer CIM
|
| 70 |
"MQM" = list(label = "Multiple QTL Model (MQM)",
|
| 71 |
desc = "Multiple QTL stepwise model; each QTL tested against a multi-QTL background"),
|
| 72 |
"LASSO" = list(label = "LASSO QTL (L1 penalized)",
|
|
@@ -108,32 +107,30 @@ dom_class <- function(a, d) {
|
|
| 108 |
# FLANKING MARKER DETECTION
|
| 109 |
# ============================================================
|
| 110 |
find_flanking_markers <- function(marker_name, marker_info) {
|
| 111 |
-
mi
|
| 112 |
if (nrow(mi) == 0) return(list(left = NA_character_, right = NA_character_))
|
| 113 |
-
chr
|
| 114 |
-
pos
|
| 115 |
chr_mk <- marker_info[as.character(marker_info$Chromosome) == chr, ]
|
| 116 |
chr_mk <- chr_mk[order(chr_mk$Position), ]
|
| 117 |
-
idx
|
| 118 |
-
if (length(idx) == 0) {
|
| 119 |
-
dists <- abs(chr_mk$Position - pos)
|
| 120 |
-
idx <- which.min(dists)
|
| 121 |
-
}
|
| 122 |
idx <- idx[1]
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
| 126 |
}
|
| 127 |
|
| 128 |
# ============================================================
|
| 129 |
# LOD SUPPORT INTERVAL
|
| 130 |
# ============================================================
|
| 131 |
add_support_intervals <- function(sig_df, all_df, lod_drop = 1.5) {
|
| 132 |
-
sig_df$Interval_Start
|
| 133 |
-
sig_df$Interval_End
|
| 134 |
-
sig_df$Interval_Size_cM
|
| 135 |
-
sig_df$Left_Flanking_Marker
|
| 136 |
-
sig_df$Right_Flanking_Marker
|
| 137 |
if (nrow(sig_df) == 0) return(sig_df)
|
| 138 |
all_df$Chromosome <- as.character(all_df$Chromosome)
|
| 139 |
for (i in seq_len(nrow(sig_df))) {
|
|
@@ -141,12 +138,12 @@ add_support_intervals <- function(sig_df, all_df, lod_drop = 1.5) {
|
|
| 141 |
chr <- as.character(sig_df$Chromosome[i])
|
| 142 |
pk <- sig_df$LOD[i]
|
| 143 |
pos_i <- sig_df$Position[i]
|
| 144 |
-
cd
|
| 145 |
-
cd
|
| 146 |
if (nrow(cd) < 2) {
|
| 147 |
-
sig_df$Interval_Start[i]
|
| 148 |
-
sig_df$Interval_End[i]
|
| 149 |
-
sig_df$Interval_Size_cM[i]
|
| 150 |
fl <- find_flanking_in_sorted(mk, cd)
|
| 151 |
sig_df$Left_Flanking_Marker[i] <- fl$left
|
| 152 |
sig_df$Right_Flanking_Marker[i] <- fl$right
|
|
@@ -180,6 +177,27 @@ find_flanking_in_sorted <- function(marker_name, sorted_cd) {
|
|
| 180 |
)
|
| 181 |
}
|
| 182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
# ============================================================
|
| 184 |
# GENOTYPE LOADER
|
| 185 |
# ============================================================
|
|
@@ -231,9 +249,9 @@ load_genotype_file <- function(filepath) {
|
|
| 231 |
# ADDITIVE / DOMINANCE EFFECT ESTIMATION
|
| 232 |
# ============================================================
|
| 233 |
estimate_effects <- function(geno_v, pheno_v) {
|
| 234 |
-
a_code
|
| 235 |
-
d_code
|
| 236 |
-
has_het
|
| 237 |
a_eff <- se_a <- d_eff <- se_d <- dom_ratio <- NA_real_
|
| 238 |
dom_cl <- NA_character_
|
| 239 |
if (has_het && length(unique(d_code)) > 1) {
|
|
@@ -301,71 +319,141 @@ qtl_scan_sim <- function(X, y, marker_info) {
|
|
| 301 |
}
|
| 302 |
|
| 303 |
# ============================================================
|
| 304 |
-
# CIM
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
# ============================================================
|
| 306 |
qtl_scan_cim <- function(X, y, marker_info, n_cofactors = 10, window_size = 10) {
|
| 307 |
-
cat("\n--- CIM ---\n")
|
| 308 |
valid <- !is.na(y)
|
| 309 |
X <- X[valid, , drop = FALSE]; y <- y[valid]
|
| 310 |
nzv <- apply(X, 2, function(x) length(unique(na.omit(x))) < 2)
|
| 311 |
X <- X[, !nzv, drop = FALSE]
|
| 312 |
n <- length(y)
|
| 313 |
max_k <- max(3, min(n_cofactors, floor(sqrt(n))))
|
| 314 |
-
|
| 315 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 316 |
for (step in seq_len(max_k)) {
|
| 317 |
if (length(remaining) == 0) break
|
| 318 |
-
best_m
|
|
|
|
|
|
|
| 319 |
for (m in remaining) {
|
| 320 |
tryCatch({
|
| 321 |
cand <- c(cofactors, m)
|
| 322 |
-
Xc <- as.data.frame(X[, cand, drop = FALSE])
|
| 323 |
-
|
|
|
|
| 324 |
if (lod > best_lod) { best_lod <- lod; best_m <- m }
|
| 325 |
}, error = function(e) NULL)
|
| 326 |
}
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
|
|
|
|
|
|
|
|
|
| 330 |
cat(" Cofactor", length(cofactors), ":", best_m, " LOD:", round(best_lod, 2), "\n")
|
| 331 |
-
} else
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 332 |
}
|
| 333 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 334 |
res_list <- vector("list", ncol(X)); names(res_list) <- colnames(X)
|
|
|
|
| 335 |
for (m in colnames(X)) {
|
| 336 |
g <- X[, m]; ok <- !is.na(g); gv <- g[ok]; pv <- y[ok]
|
| 337 |
if (length(unique(gv)) < 2 || length(pv) < 8) next
|
| 338 |
mi <- marker_info[marker_info$Marker == m, ]
|
| 339 |
if (nrow(mi) == 0 || is.na(mi$Position[1])) next
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
|
|
|
|
|
|
|
|
|
| 350 |
tryCatch({
|
| 351 |
df <- data.frame(y = pv, focal = gv)
|
| 352 |
if (length(active_cof) > 0) {
|
| 353 |
-
|
| 354 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 355 |
}
|
| 356 |
df <- df[complete.cases(df), ]
|
| 357 |
-
if (nrow(df) < 8)
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 361 |
} else {
|
| 362 |
-
fit_full <- lm(y ~ focal, data = df)
|
|
|
|
| 363 |
}
|
| 364 |
-
|
|
|
|
|
|
|
| 365 |
ct <- coef(summary(fit_full))
|
| 366 |
if (!"focal" %in% rownames(ct)) next
|
| 367 |
pval <- ct["focal", "Pr(>|t|)"]
|
| 368 |
eff <- estimate_effects(gv, pv)
|
|
|
|
| 369 |
res_list[[m]] <- data.frame(
|
| 370 |
Marker=m, Chromosome=m_chr, Position=m_pos, LOD=lod, P_Value=pval,
|
| 371 |
PVE_Marker_Percent=pve, PVE_CIM_Percent=pve,
|
|
@@ -377,6 +465,7 @@ qtl_scan_cim <- function(X, y, marker_info, n_cofactors = 10, window_size = 10)
|
|
| 377 |
Method="CIM", stringsAsFactors=FALSE)
|
| 378 |
}, error = function(e) NULL)
|
| 379 |
}
|
|
|
|
| 380 |
res <- do.call(rbind, Filter(Negate(is.null), res_list))
|
| 381 |
if (is.null(res) || nrow(res) == 0) return(data.frame(Message = "CIM: no results"))
|
| 382 |
res[order(res$Chromosome, res$Position), ]
|
|
@@ -396,7 +485,7 @@ qtl_scan_mqm <- function(X, y, marker_info, n_cofactors = 10, window_size = 10)
|
|
| 396 |
n <- length(y); max_k <- min(n_cofactors, floor(sqrt(n)))
|
| 397 |
sim_res <- sim_res[order(-sim_res$LOD), ]
|
| 398 |
candidates <- head(sim_res$Marker[sim_res$Marker %in% colnames(X)], max_k * 2)
|
| 399 |
-
selected <- character(0)
|
| 400 |
for (cand in candidates) {
|
| 401 |
if (length(selected) >= max_k) break
|
| 402 |
tryCatch({
|
|
@@ -409,12 +498,23 @@ qtl_scan_mqm <- function(X, y, marker_info, n_cofactors = 10, window_size = 10)
|
|
| 409 |
} else lod_base <- 0
|
| 410 |
gain <- lod_full - lod_base
|
| 411 |
if (gain >= 2.0) {
|
| 412 |
-
selected <- test_set
|
| 413 |
cat(" MQM QTL:", cand, " gain:", round(gain, 2), "\n")
|
| 414 |
}
|
| 415 |
}, error = function(e) NULL)
|
| 416 |
}
|
| 417 |
cat(" MQM selected", length(selected), "QTLs\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 418 |
res_list <- vector("list", ncol(X)); names(res_list) <- colnames(X)
|
| 419 |
for (m in colnames(X)) {
|
| 420 |
g <- X[, m]; ok <- !is.na(g); gv <- g[ok]; pv <- y[ok]
|
|
@@ -422,28 +522,30 @@ qtl_scan_mqm <- function(X, y, marker_info, n_cofactors = 10, window_size = 10)
|
|
| 422 |
mi <- marker_info[marker_info$Marker == m, ]
|
| 423 |
if (nrow(mi) == 0 || is.na(mi$Position[1])) next
|
| 424 |
m_chr <- as.character(mi$Chromosome[1]); m_pos <- mi$Position[1]
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
}
|
| 433 |
-
active_cof <- setdiff(selected, nearby)
|
| 434 |
tryCatch({
|
| 435 |
df <- data.frame(y = pv, focal = gv)
|
| 436 |
if (length(active_cof) > 0) {
|
| 437 |
-
|
| 438 |
-
|
|
|
|
|
|
|
|
|
|
| 439 |
}
|
| 440 |
df <- df[complete.cases(df), ]
|
| 441 |
-
if (nrow(df) < 8)
|
| 442 |
-
if (length(active_cof) > 0) {
|
| 443 |
-
|
| 444 |
-
|
|
|
|
| 445 |
} else {
|
| 446 |
-
fit_full <- lm(y ~ focal, data
|
| 447 |
}
|
| 448 |
lod <- calc_lod(fit_full, fit_null); pve <- calc_pve(fit_full, fit_null)
|
| 449 |
ct <- coef(summary(fit_full))
|
|
@@ -596,91 +698,39 @@ qtl_scan <- function(X, y, marker_info, method = "CIM",
|
|
| 596 |
# ============================================================
|
| 597 |
# FAST PARALLEL PERMUTATION THRESHOLD
|
| 598 |
# ============================================================
|
| 599 |
-
# Key speedups vs v8:
|
| 600 |
-
# 1. Uses parallel::mclapply (Unix) or parLapply (Windows)
|
| 601 |
-
# 2. Uses only SIM for the null distribution regardless of chosen method
|
| 602 |
-
# (SIM is ~10x faster and yields conservative threshold; good practice)
|
| 603 |
-
# 3. Sub-samples markers to 200 for the permutation null (adequate for threshold)
|
| 604 |
-
# 4. Auto-detects core count; uses min(n_cores-1, 8)
|
| 605 |
permutation_threshold <- function(X, y, marker_info, method = "SIM",
|
| 606 |
n_perm = 100, alpha = 0.05,
|
| 607 |
n_cofactors = 5, window_size = 5,
|
| 608 |
progress_cb = NULL) {
|
| 609 |
-
|
| 610 |
-
cat(" Running", n_perm, "PARALLEL permutations (method:", method, ")...\n")
|
| 611 |
-
|
| 612 |
-
# Subsample markers for speed (max 200 random markers)
|
| 613 |
n_markers_use <- min(200, ncol(X))
|
| 614 |
if (ncol(X) > n_markers_use) {
|
| 615 |
set.seed(99)
|
| 616 |
-
keep_cols
|
| 617 |
-
X_perm
|
| 618 |
-
mi_perm
|
| 619 |
} else {
|
| 620 |
-
X_perm
|
| 621 |
-
mi_perm <- marker_info
|
| 622 |
}
|
| 623 |
-
|
| 624 |
-
# Always use SIM for permutations (fastest, good null distribution)
|
| 625 |
-
perm_method <- "SIM"
|
| 626 |
-
|
| 627 |
-
# Detect cores
|
| 628 |
-
n_cores <- max(1, parallel::detectCores(logical = TRUE) - 1)
|
| 629 |
-
n_cores <- min(n_cores, 8)
|
| 630 |
cat(" Using", n_cores, "cores\n")
|
| 631 |
|
| 632 |
-
# Worker function — runs one permutation
|
| 633 |
one_perm <- function(seed) {
|
| 634 |
set.seed(seed)
|
| 635 |
yp <- sample(y)
|
| 636 |
-
res <- tryCatch(
|
| 637 |
-
qtl_scan_sim(X_perm, yp, mi_perm),
|
| 638 |
-
error = function(e) NULL)
|
| 639 |
if (!is.null(res) && !"Message" %in% colnames(res) && "LOD" %in% colnames(res))
|
| 640 |
return(max(res$LOD, na.rm = TRUE))
|
| 641 |
return(0)
|
| 642 |
}
|
| 643 |
-
|
| 644 |
seeds <- sample.int(1e6, n_perm)
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
if (is_windows) {
|
| 650 |
-
# Windows: use parLapply with snow cluster
|
| 651 |
-
cl <- tryCatch({
|
| 652 |
-
cl2 <- parallel::makeCluster(n_cores)
|
| 653 |
-
parallel::clusterEvalQ(cl2, {
|
| 654 |
-
library(glmnet)
|
| 655 |
-
})
|
| 656 |
-
parallel::clusterExport(cl2,
|
| 657 |
-
varlist = c("qtl_scan_sim","calc_lod","calc_pve",
|
| 658 |
-
"estimate_effects","dom_class",
|
| 659 |
-
"X_perm","y","mi_perm"),
|
| 660 |
-
envir = environment())
|
| 661 |
-
cl2
|
| 662 |
-
}, error = function(e) NULL)
|
| 663 |
-
|
| 664 |
-
if (!is.null(cl)) {
|
| 665 |
-
max_lods <- tryCatch({
|
| 666 |
-
unlist(parallel::parLapply(cl, seeds, one_perm))
|
| 667 |
-
}, error = function(e) sapply(seeds, one_perm))
|
| 668 |
-
parallel::stopCluster(cl)
|
| 669 |
-
} else {
|
| 670 |
-
max_lods <- sapply(seeds, one_perm)
|
| 671 |
-
}
|
| 672 |
-
} else {
|
| 673 |
-
# Unix/Mac: use mclapply
|
| 674 |
-
max_lods <- tryCatch(
|
| 675 |
-
unlist(parallel::mclapply(seeds, one_perm, mc.cores = n_cores,
|
| 676 |
-
mc.preschedule = TRUE)),
|
| 677 |
-
error = function(e) sapply(seeds, one_perm)
|
| 678 |
-
)
|
| 679 |
-
}
|
| 680 |
} else {
|
| 681 |
max_lods <- sapply(seeds, one_perm)
|
| 682 |
}
|
| 683 |
-
|
| 684 |
thr <- quantile(max_lods, 1 - alpha, na.rm = TRUE)
|
| 685 |
cat(" Permutation LOD threshold (alpha =", alpha, "):", round(thr, 3), "\n")
|
| 686 |
thr
|
|
@@ -723,108 +773,196 @@ find_common_qtl <- function(compare_res, lod_threshold, min_models = 2) {
|
|
| 723 |
}
|
| 724 |
|
| 725 |
# ============================================================
|
| 726 |
-
#
|
| 727 |
-
# —
|
|
|
|
| 728 |
# ============================================================
|
| 729 |
-
|
| 730 |
-
|
|
|
|
| 731 |
if (is.null(scan_df) || "Message" %in% colnames(scan_df))
|
| 732 |
-
return(ggplot() + labs(title =
|
|
|
|
| 733 |
pd <- scan_df[!is.na(scan_df$LOD) & !is.na(scan_df$Position) & !is.na(scan_df$Chromosome), ]
|
| 734 |
-
if (nrow(pd) == 0) return(ggplot() + labs(title =
|
| 735 |
|
| 736 |
pd$Chromosome <- as.character(pd$Chromosome)
|
| 737 |
-
chroms
|
| 738 |
-
|
| 739 |
-
|
| 740 |
-
|
| 741 |
-
|
| 742 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 743 |
}
|
| 744 |
-
pd$
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
|
| 748 |
-
|
| 749 |
-
|
| 750 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 751 |
|
| 752 |
-
|
| 753 |
-
|
| 754 |
-
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 759 |
|
| 760 |
-
#
|
| 761 |
-
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
|
| 766 |
-
color = "#FFBE0B", size = 7, alpha = 0.35, shape = 16) +
|
| 767 |
-
geom_point(data = sig_pd, aes(x = cum_pos, y = LOD, fill = LOD),
|
| 768 |
-
size = 5, shape = 23, color = "white", stroke = 0.8) +
|
| 769 |
-
scale_fill_gradientn(
|
| 770 |
-
colors = c("#FB5607","#FFBE0B","#FF006E","#8338EC"),
|
| 771 |
-
name = "LOD Score",
|
| 772 |
-
guide = guide_colorbar(barwidth = 8, barheight = 0.8, title.position = "top")) +
|
| 773 |
-
ggrepel::geom_label_repel(
|
| 774 |
-
data = if (nrow(sig_pd) <= 40) sig_pd else head(sig_pd[order(-sig_pd$LOD), ], 40),
|
| 775 |
-
aes(x = cum_pos, y = LOD, label = Marker),
|
| 776 |
-
size = 2.8, max.overlaps = 25,
|
| 777 |
-
fill = "white", color = "#FF006E",
|
| 778 |
-
fontface = "bold",
|
| 779 |
-
segment.color = "#FF006E", segment.size = 0.5,
|
| 780 |
-
label.padding = unit(0.2, "lines"),
|
| 781 |
-
box.padding = unit(0.3, "lines"))
|
| 782 |
}
|
| 783 |
|
| 784 |
-
|
| 785 |
-
|
| 786 |
-
|
| 787 |
-
|
| 788 |
-
|
| 789 |
-
|
| 790 |
-
|
| 791 |
-
|
| 792 |
-
|
| 793 |
-
|
| 794 |
-
" significant QTLs • ", length(chroms), " chromosomes • ",
|
| 795 |
-
nrow(pd), " markers"),
|
| 796 |
-
x = "Chromosome", y = "LOD Score") +
|
| 797 |
-
theme_bw(base_size = 13) +
|
| 798 |
-
theme(
|
| 799 |
-
panel.background = element_rect(fill = "white", color = "grey85"),
|
| 800 |
-
plot.background = element_rect(fill = "white", color = NA),
|
| 801 |
-
panel.grid.minor = element_blank(),
|
| 802 |
-
panel.grid.major.x = element_blank(),
|
| 803 |
-
panel.grid.major.y = element_line(color = "grey92", linetype = "dotted"),
|
| 804 |
-
axis.text.x = element_text(angle = 45, hjust = 1, color = "grey30", size = 10),
|
| 805 |
-
axis.text.y = element_text(color = "grey30"),
|
| 806 |
-
axis.title = element_text(face = "bold", color = "grey20"),
|
| 807 |
-
plot.title = element_text(face = "bold", size = 15, color = "#212529"),
|
| 808 |
-
plot.subtitle = element_text(color = "grey40", size = 11),
|
| 809 |
-
legend.position = "bottom",
|
| 810 |
-
legend.background = element_rect(fill = "white", color = NA),
|
| 811 |
-
legend.text = element_text(size = 9, color = "grey30"),
|
| 812 |
-
legend.title = element_text(size = 10, color = "grey20", face = "bold")
|
| 813 |
-
)
|
| 814 |
-
p
|
| 815 |
-
}
|
| 816 |
|
| 817 |
-
|
| 818 |
-
# we reset after the first scale_color_manual for background points.
|
| 819 |
-
# We use a workaround by splitting into two separate geom layers.
|
| 820 |
-
new_scale_color_workaround <- function() {
|
| 821 |
-
# No-op — we handle it by not using color aes for significant points
|
| 822 |
-
list()
|
| 823 |
}
|
| 824 |
|
| 825 |
# ============================================================
|
| 826 |
# COLORFUL PLOTLY MANHATTAN (interactive)
|
| 827 |
-
# — LOD-gradient marker colors, hover with rich info
|
| 828 |
# ============================================================
|
| 829 |
make_plotly_manhattan <- function(scan_df, lod_threshold = 2.0, title = "Manhattan Plot") {
|
| 830 |
blank <- plot_ly() %>%
|
|
@@ -832,7 +970,6 @@ make_plotly_manhattan <- function(scan_df, lod_threshold = 2.0, title = "Manhatt
|
|
| 832 |
if (is.null(scan_df) || "Message" %in% colnames(scan_df)) return(blank)
|
| 833 |
pd <- scan_df[!is.na(scan_df$LOD) & !is.na(scan_df$Position) & !is.na(scan_df$Chromosome), ]
|
| 834 |
if (nrow(pd) == 0) return(blank)
|
| 835 |
-
|
| 836 |
pd$Chromosome <- as.character(pd$Chromosome)
|
| 837 |
chroms <- sort(unique(pd$Chromosome), method = "radix")
|
| 838 |
offsets <- setNames(numeric(length(chroms)), chroms)
|
|
@@ -843,13 +980,9 @@ make_plotly_manhattan <- function(scan_df, lod_threshold = 2.0, title = "Manhatt
|
|
| 843 |
}
|
| 844 |
pd$cum_pos <- pd$Position + offsets[pd$Chromosome]
|
| 845 |
pd$sig <- pd$LOD >= lod_threshold
|
| 846 |
-
|
| 847 |
-
chr_mids <- tapply(pd$cum_pos, pd$Chromosome, median, na.rm = TRUE)
|
| 848 |
-
|
| 849 |
-
# Two-tone alternating background markers
|
| 850 |
chr_bg_colors <- rep(c("#3A86FF","#FB5607"), length.out = length(chroms))
|
| 851 |
names(chr_bg_colors) <- chroms
|
| 852 |
-
|
| 853 |
pl <- plot_ly()
|
| 854 |
for (ch in chroms) {
|
| 855 |
sub <- pd[pd$Chromosome == ch & !pd$sig, ]
|
|
@@ -864,29 +997,18 @@ make_plotly_manhattan <- function(scan_df, lod_threshold = 2.0, title = "Manhatt
|
|
| 864 |
"<br>PVE: ", round(PVE_Marker_Percent, 2), "%"),
|
| 865 |
hoverinfo = "text")
|
| 866 |
}
|
| 867 |
-
|
| 868 |
-
# Significant markers: LOD-mapped vivid colors
|
| 869 |
sig_pd <- pd[pd$sig, ]
|
| 870 |
if (nrow(sig_pd) > 0) {
|
| 871 |
-
|
| 872 |
-
|
| 873 |
-
lod_max <- max(sig_pd$LOD, na.rm = TRUE)
|
| 874 |
lod_norm <- if (lod_max > lod_min) (sig_pd$LOD - lod_min) / (lod_max - lod_min) else rep(0.5, nrow(sig_pd))
|
| 875 |
-
|
| 876 |
-
# Map to a vivid color scale: purple → orange → pink
|
| 877 |
sig_colors <- colorRampPalette(c("#8338EC","#3A86FF","#FFBE0B","#FB5607","#FF006E"))(100)
|
| 878 |
pt_colors <- sig_colors[pmax(1, pmin(100, round(lod_norm * 99) + 1))]
|
| 879 |
-
|
| 880 |
pl <- pl %>% add_trace(
|
| 881 |
data = sig_pd, x = ~cum_pos, y = ~LOD, type = "scatter", mode = "markers",
|
| 882 |
name = "Significant QTL",
|
| 883 |
-
marker = list(
|
| 884 |
-
|
| 885 |
-
color = pt_colors,
|
| 886 |
-
symbol = "diamond",
|
| 887 |
-
line = list(width = 1.5, color = "white"),
|
| 888 |
-
opacity = 0.95
|
| 889 |
-
),
|
| 890 |
text = ~paste0("<b>\u2605 ", Marker, " \u2605</b><br>Chr: ", Chromosome,
|
| 891 |
"<br>Pos: ", round(Position, 1), " cM",
|
| 892 |
"<br>LOD: ", round(LOD, 3),
|
|
@@ -894,74 +1016,41 @@ make_plotly_manhattan <- function(scan_df, lod_threshold = 2.0, title = "Manhatt
|
|
| 894 |
"<br>Add. Effect: ", round(Additive_Effect, 4),
|
| 895 |
"<br>Dom. Class: ", Dominance_Class),
|
| 896 |
hoverinfo = "text")
|
| 897 |
-
|
| 898 |
-
# Colorbar trace for significant markers
|
| 899 |
-
pl <- pl %>% add_trace(
|
| 900 |
-
x = ~cum_pos, y = ~LOD,
|
| 901 |
-
data = sig_pd,
|
| 902 |
-
type = "scatter", mode = "markers",
|
| 903 |
-
marker = list(
|
| 904 |
-
color = ~LOD,
|
| 905 |
-
colorscale = list(
|
| 906 |
-
c(0, "#8338EC"), c(0.25, "#3A86FF"),
|
| 907 |
-
c(0.5, "#FFBE0B"), c(0.75, "#FB5607"), c(1, "#FF006E")
|
| 908 |
-
),
|
| 909 |
-
showscale = TRUE,
|
| 910 |
-
colorbar = list(title = "LOD Score", thickness = 14, len = 0.5,
|
| 911 |
-
y = 0.85, font = list(color = "grey20", size = 11)),
|
| 912 |
-
size = 0.001, opacity = 0
|
| 913 |
-
),
|
| 914 |
-
showlegend = FALSE, hoverinfo = "none")
|
| 915 |
}
|
| 916 |
-
|
| 917 |
-
|
| 918 |
-
|
| 919 |
-
|
| 920 |
-
|
| 921 |
-
xend = max(pd$cum_pos, na.rm = TRUE),
|
| 922 |
-
y = lod_threshold, yend = lod_threshold,
|
| 923 |
-
line = list(color = "#FF006E", dash = "dash", width = 2),
|
| 924 |
-
showlegend = FALSE) %>%
|
| 925 |
layout(
|
| 926 |
title = list(
|
| 927 |
text = paste0("<b>", title, "</b> <span style='font-size:12px;color:#888'>",
|
| 928 |
sum(pd$sig, na.rm = TRUE), " significant QTLs</span>"),
|
| 929 |
font = list(size = 15, color = "#212529")),
|
| 930 |
-
xaxis = list(
|
| 931 |
-
|
| 932 |
-
|
| 933 |
-
|
| 934 |
-
|
| 935 |
-
|
| 936 |
-
|
| 937 |
-
title = "LOD Score",
|
| 938 |
-
gridcolor = "rgba(0,0,0,0.07)", color = "grey25", zeroline = FALSE),
|
| 939 |
paper_bgcolor = "white", plot_bgcolor = "white",
|
| 940 |
font = list(color = "grey20"),
|
| 941 |
-
legend = list(
|
| 942 |
-
|
| 943 |
-
|
| 944 |
-
hoverlabel = list(
|
| 945 |
-
bgcolor = "white",
|
| 946 |
-
font = list(color = "grey10", size = 11),
|
| 947 |
-
bordercolor = "grey60")
|
| 948 |
-
)
|
| 949 |
-
pl
|
| 950 |
}
|
| 951 |
|
| 952 |
# ============================================================
|
| 953 |
# COLORFUL CHROMOSOME IDIOGRAM
|
| 954 |
-
# — bold per-model colors, LOD-scaled size, glow halos
|
| 955 |
# ============================================================
|
| 956 |
make_idiogram_ggplot <- function(qtl_list, all_markers_df, lod_threshold = 2.0) {
|
| 957 |
if (is.null(all_markers_df) || nrow(all_markers_df) == 0)
|
| 958 |
return(ggplot() + labs(title = "No marker data") + theme_bw())
|
| 959 |
-
|
| 960 |
all_markers_df$Chromosome <- as.character(all_markers_df$Chromosome)
|
| 961 |
chroms <- sort(unique(all_markers_df$Chromosome))
|
| 962 |
n_chr <- length(chroms)
|
| 963 |
chr_idx <- setNames(seq_along(chroms), chroms)
|
| 964 |
-
|
| 965 |
chr_summary <- do.call(rbind, lapply(chroms, function(ch) {
|
| 966 |
mk <- all_markers_df[all_markers_df$Chromosome == ch, ]
|
| 967 |
data.frame(Chromosome = ch, x = chr_idx[ch],
|
|
@@ -969,21 +1058,16 @@ make_idiogram_ggplot <- function(qtl_list, all_markers_df, lod_threshold = 2.0)
|
|
| 969 |
ymax = max(mk$Position, na.rm = TRUE),
|
| 970 |
stringsAsFactors = FALSE)
|
| 971 |
}))
|
| 972 |
-
|
| 973 |
max_pos <- max(all_markers_df$Position, na.rm = TRUE)
|
| 974 |
-
|
| 975 |
p <- ggplot() +
|
| 976 |
-
# Chromosome body — gradient-like with two-tone fill
|
| 977 |
geom_rect(data = chr_summary,
|
| 978 |
aes(xmin = x - 0.28, xmax = x + 0.28, ymin = ymin, ymax = ymax),
|
| 979 |
fill = "#F0F4FF", color = "#ADB5BD", linewidth = 0.7) +
|
| 980 |
-
# Centromere
|
| 981 |
geom_rect(data = chr_summary,
|
| 982 |
aes(xmin = x - 0.28, xmax = x + 0.28,
|
| 983 |
ymin = ymin + (ymax - ymin) * 0.38,
|
| 984 |
ymax = ymin + (ymax - ymin) * 0.42),
|
| 985 |
fill = "#6C757D", color = NA) +
|
| 986 |
-
# Chromosome labels
|
| 987 |
geom_text(data = chr_summary,
|
| 988 |
aes(x = x, y = ymax + max_pos * 0.045,
|
| 989 |
label = paste0("Chr\n", Chromosome)),
|
|
@@ -1003,55 +1087,39 @@ make_idiogram_ggplot <- function(qtl_list, all_markers_df, lod_threshold = 2.0)
|
|
| 1003 |
plot.title = element_text(face = "bold", size = 14, color = "#212529"),
|
| 1004 |
axis.title.y = element_text(face = "bold", color = "grey20")
|
| 1005 |
)
|
| 1006 |
-
|
| 1007 |
-
# Vivid model color palette
|
| 1008 |
-
model_names <- names(qtl_list)
|
| 1009 |
-
n_models <- length(model_names)
|
| 1010 |
vivid_pal <- c("#FF006E","#3A86FF","#06D6A0","#FFBE0B","#FB5607","#8338EC",
|
| 1011 |
"#F72585","#4CC9F0","#2A9D8F","#E9C46A")
|
| 1012 |
model_colors_use <- setNames(
|
| 1013 |
-
rep(vivid_pal, ceiling(n_models / length(vivid_pal)))[seq_len(n_models)],
|
| 1014 |
-
model_names
|
| 1015 |
-
)
|
| 1016 |
-
# Override with MODEL_COLORS if known
|
| 1017 |
for (m in intersect(model_names, names(MODEL_COLORS)))
|
| 1018 |
model_colors_use[m] <- MODEL_COLORS[m]
|
| 1019 |
-
|
| 1020 |
for (ti in seq_along(model_names)) {
|
| 1021 |
-
tr
|
| 1022 |
-
qdf
|
| 1023 |
if (is.null(qdf) || "Message" %in% colnames(qdf)) next
|
| 1024 |
-
sig
|
| 1025 |
if (nrow(sig) == 0) next
|
| 1026 |
sig$Chromosome <- as.character(sig$Chromosome)
|
| 1027 |
-
sig
|
| 1028 |
if (nrow(sig) == 0) next
|
| 1029 |
sig$x_base <- chr_idx[sig$Chromosome]
|
| 1030 |
-
|
| 1031 |
side <- ifelse(ti %% 2 == 1, 1, -1)
|
| 1032 |
x_off <- sig$x_base + side * (0.33 + (((ti - 1) %/% 2) * 0.13))
|
| 1033 |
lod_sz <- pmin(7, pmax(2.5, sig$LOD * 0.9))
|
| 1034 |
mc <- model_colors_use[tr]
|
| 1035 |
-
|
| 1036 |
-
|
| 1037 |
-
|
| 1038 |
-
|
| 1039 |
-
|
| 1040 |
-
|
| 1041 |
-
pt_df <- data.frame(
|
| 1042 |
-
x = x_off, y = sig$Position,
|
| 1043 |
-
model = tr, size_val = lod_sz,
|
| 1044 |
-
stringsAsFactors = FALSE)
|
| 1045 |
-
|
| 1046 |
-
# Glow halo
|
| 1047 |
p <- p +
|
| 1048 |
geom_point(data = pt_df, aes(x = x, y = y),
|
| 1049 |
color = mc, size = lod_sz * 2.2, alpha = 0.12, shape = 16) +
|
| 1050 |
geom_segment(data = seg_df,
|
| 1051 |
aes(x = x, xend = xend, y = y, yend = yend, color = model),
|
| 1052 |
linewidth = 0.9, alpha = 0.85) +
|
| 1053 |
-
geom_point(data = pt_df,
|
| 1054 |
-
aes(x = x, y = y, color = model, size = size_val),
|
| 1055 |
shape = 18, alpha = 0.95) +
|
| 1056 |
scale_color_manual(values = model_colors_use, name = "Model") +
|
| 1057 |
scale_size_continuous(range = c(2.5, 7), guide = "none")
|
|
@@ -1134,24 +1202,28 @@ ui <- fluidPage(
|
|
| 1134 |
.model-badge{display:inline-block;background:#e8f4fd;border:1px solid #90caf9;border-radius:20px;padding:3px 10px;font-size:11px;font-weight:600;color:#3A86FF;margin:2px;}
|
| 1135 |
.perm-info{background:linear-gradient(135deg,#e8f4fd,#f0f8ff);border:1px solid #90caf9;border-left:4px solid #8338EC;border-radius:8px;padding:12px 16px;margin:8px 0;font-size:13px;}
|
| 1136 |
.speed-badge{background:#8338EC;color:#fff;border-radius:12px;padding:2px 8px;font-size:10px;font-weight:700;letter-spacing:0.5px;}
|
|
|
|
| 1137 |
"))
|
| 1138 |
),
|
| 1139 |
|
| 1140 |
navbarPage(
|
| 1141 |
-
title = "QTL-Mapper
|
| 1142 |
id = "nav",
|
| 1143 |
|
| 1144 |
# ── 1. DATA UPLOAD ─────────────────────────────────────
|
| 1145 |
tabPanel("📁 Data Upload", icon = icon("upload"),
|
| 1146 |
fluidRow(column(12,
|
| 1147 |
div(class = "info-box",
|
| 1148 |
-
tags$b("QTL-Mapper
|
|
|
|
|
|
|
|
|
|
| 1149 |
tags$br(),
|
| 1150 |
-
|
| 1151 |
-
"
|
| 1152 |
tags$br(),
|
| 1153 |
-
tags$span(
|
| 1154 |
-
|
| 1155 |
)
|
| 1156 |
)),
|
| 1157 |
fluidRow(
|
|
@@ -1184,6 +1256,7 @@ ui <- fluidPage(
|
|
| 1184 |
numericInput("window_size", "Exclusion Window (±markers)", value = 10, min = 1, max = 30)
|
| 1185 |
),
|
| 1186 |
numericInput("lod_drop", "LOD Drop for Support Interval", value = 1.5, min = 0.5, max = 3.0, step = 0.5),
|
|
|
|
| 1187 |
actionButton("run_btn", "▶ Run QTL Analysis",
|
| 1188 |
class = "btn btn-primary btn-block",
|
| 1189 |
style = "margin-top:10px;width:100%;"),
|
|
@@ -1192,9 +1265,8 @@ ui <- fluidPage(
|
|
| 1192 |
conditionalPanel("input.run_permutation",
|
| 1193 |
div(class = "perm-info",
|
| 1194 |
tags$span(class = "speed-badge", "FAST"),
|
| 1195 |
-
tags$b(" Parallel Permutations"),
|
| 1196 |
-
tags$
|
| 1197 |
-
tags$small("Auto-detects CPU cores. Uses SIM on 200 marker subsample for speed. ~10× faster than v8."),
|
| 1198 |
tags$br(), tags$br(),
|
| 1199 |
numericInput("n_perm", "Number of Permutations", value = 100, min = 50, max = 1000, step = 50),
|
| 1200 |
numericInput("perm_alpha", "Alpha (significance)", value = 0.05, min = 0.01, max = 0.20, step = 0.01)
|
|
@@ -1239,9 +1311,10 @@ ui <- fluidPage(
|
|
| 1239 |
tabPanel("📊 Results", icon = icon("chart-line"),
|
| 1240 |
fluidRow(column(12,
|
| 1241 |
div(style = "display:flex;gap:10px;flex-wrap:wrap;margin-bottom:14px;",
|
| 1242 |
-
downloadButton("dl_manhattan",
|
| 1243 |
-
downloadButton("dl_idiogram",
|
| 1244 |
-
downloadButton("
|
|
|
|
| 1245 |
)
|
| 1246 |
)),
|
| 1247 |
fluidRow(
|
|
@@ -1258,6 +1331,19 @@ ui <- fluidPage(
|
|
| 1258 |
)
|
| 1259 |
)
|
| 1260 |
),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1261 |
fluidRow(
|
| 1262 |
column(4,
|
| 1263 |
div(class = "card",
|
|
@@ -1298,8 +1384,7 @@ ui <- fluidPage(
|
|
| 1298 |
div(class = "card",
|
| 1299 |
div(class = "card-hd", "Multi-Model Comparison"),
|
| 1300 |
div(class = "card-bd",
|
| 1301 |
-
div(class = "info-box",
|
| 1302 |
-
"Select methods, click Run. Each runs a full genome scan independently."),
|
| 1303 |
checkboxGroupInput("compare_methods", "Methods to Compare:",
|
| 1304 |
choices = setNames(names(MAPPING_MODELS), sapply(MAPPING_MODELS, `[[`, "label")),
|
| 1305 |
selected = c("SIM", "CIM", "LASSO"), inline = TRUE),
|
|
@@ -1421,26 +1506,19 @@ ui <- fluidPage(
|
|
| 1421 |
fluidRow(column(10, offset = 1,
|
| 1422 |
div(class = "card",
|
| 1423 |
div(class = "card-bd",
|
| 1424 |
-
tags$h4(style = "color:#FF006E;", "QTL-Mapper
|
| 1425 |
div(class = "info-box",
|
| 1426 |
-
tags$b("
|
| 1427 |
-
tags$span(class = "
|
| 1428 |
-
"
|
| 1429 |
-
|
| 1430 |
-
"
|
| 1431 |
-
|
| 1432 |
-
"
|
| 1433 |
-
|
| 1434 |
-
|
| 1435 |
-
tags$
|
| 1436 |
-
"
|
| 1437 |
-
"• LOD = (n/2)·log10(RSS0/RSS1) — matches QTL Cartographer definition", tags$br(),
|
| 1438 |
-
"• Additive + Dominance effects with SE and CI", tags$br(),
|
| 1439 |
-
"• Dominance class: Additive / Partial / Dominance / Overdominance", tags$br(),
|
| 1440 |
-
"• LOD-drop support intervals with Left & Right Flanking Markers", tags$br(),
|
| 1441 |
-
"• Multi-model comparison + Common QTL detection", tags$br(),
|
| 1442 |
-
"• Domestication: Fst and Tajima's D", tags$br(),
|
| 1443 |
-
"• Excel export with all columns"
|
| 1444 |
),
|
| 1445 |
tags$h5(style = "margin-top:18px;font-weight:600;color:#3A86FF;", "References"),
|
| 1446 |
tags$ul(
|
|
@@ -1453,7 +1531,7 @@ ui <- fluidPage(
|
|
| 1453 |
tags$hr(),
|
| 1454 |
tags$p(style = "text-align:center;color:#6c757d;font-size:12px;",
|
| 1455 |
"Contact: ashmithamganesan@gmail.com | P.sinha@cgiar.org",
|
| 1456 |
-
tags$br(), "QTL-Mapper
|
| 1457 |
)
|
| 1458 |
)
|
| 1459 |
))
|
|
@@ -1513,8 +1591,7 @@ server <- function(input, output, session) {
|
|
| 1513 |
if (!is.null(rv$perm_threshold))
|
| 1514 |
div(class = "perm-info", style = "margin-top:8px;",
|
| 1515 |
tags$span(class = "speed-badge", "FAST"),
|
| 1516 |
-
tags$b(" Permutation LOD threshold: "),
|
| 1517 |
-
round(rv$perm_threshold, 3))
|
| 1518 |
)
|
| 1519 |
})
|
| 1520 |
|
|
@@ -1581,25 +1658,19 @@ server <- function(input, output, session) {
|
|
| 1581 |
X_v <- X[-tr_idx, , drop = FALSE]; y_v <- y[-tr_idx]
|
| 1582 |
|
| 1583 |
withProgress(message = paste("Running", input$mapping_method, "..."), value = 0.1, {
|
| 1584 |
-
|
| 1585 |
-
# Optional FAST parallel permutation threshold
|
| 1586 |
if (isTRUE(input$run_permutation)) {
|
| 1587 |
incProgress(0, detail = "Running parallel permutations (fast)...")
|
| 1588 |
n_cores_avail <- max(1, parallel::detectCores(logical = TRUE) - 1)
|
| 1589 |
showNotification(
|
| 1590 |
paste0("Permutation test: ", input$n_perm, " perms on ",
|
| 1591 |
-
min(n_cores_avail, 8), " cores
|
| 1592 |
type = "message", duration = 4)
|
| 1593 |
rv$perm_threshold <- permutation_threshold(
|
| 1594 |
X_tr, y_tr, rv$marker_info,
|
| 1595 |
-
method
|
| 1596 |
-
|
| 1597 |
-
alpha = input$perm_alpha,
|
| 1598 |
-
n_cofactors = input$n_cofactors,
|
| 1599 |
window_size = input$window_size)
|
| 1600 |
-
|
| 1601 |
-
updateSliderInput(session, "lod_threshold",
|
| 1602 |
-
value = round(rv$perm_threshold, 2))
|
| 1603 |
}
|
| 1604 |
incProgress(0.15)
|
| 1605 |
|
|
@@ -1614,7 +1685,6 @@ server <- function(input, output, session) {
|
|
| 1614 |
Xtr_rf <- if (length(nzv_i) > 0) X_tr[, -nzv_i, drop = FALSE] else X_tr
|
| 1615 |
Xv_rf <- X_v[, colnames(Xtr_rf), drop = FALSE]
|
| 1616 |
rv$model_cols <- colnames(Xtr_rf)
|
| 1617 |
-
|
| 1618 |
fit <- tryCatch(
|
| 1619 |
caret::train(x = Xtr_rf, y = y_tr, method = "rf",
|
| 1620 |
trControl = caret::trainControl(method = "cv", number = 5), tuneLength = 3),
|
|
@@ -1622,11 +1692,9 @@ server <- function(input, output, session) {
|
|
| 1622 |
caret::train(x = Xtr_rf, y = y_tr, method = "lm",
|
| 1623 |
trControl = caret::trainControl(method = "cv", number = 5)))
|
| 1624 |
rv$model_fit <- fit
|
| 1625 |
-
|
| 1626 |
-
|
| 1627 |
-
|
| 1628 |
-
v_r2 <- max(0, 1 - sum((y_v - yv_p)^2) / sum((y_v - mean(y_v))^2))
|
| 1629 |
-
|
| 1630 |
rv$perf <- data.frame(
|
| 1631 |
Metric = c("R² Train", "R Train", "R² Validation", "R Validation"),
|
| 1632 |
Value = c(tr_r2, cor(y_tr, ytr_p), v_r2, cor(y_v, yv_p)))
|
|
@@ -1646,12 +1714,42 @@ server <- function(input, output, session) {
|
|
| 1646 |
})
|
| 1647 |
})
|
| 1648 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1649 |
# ---- COMPARE MODELS ----
|
| 1650 |
output$compare_status_ui <- renderUI({
|
| 1651 |
if (length(rv$compare_res) == 0) return(NULL)
|
| 1652 |
div(class = "good-box",
|
| 1653 |
-
tags$b("Completed: "), paste(names(rv$compare_res), collapse = ", "),
|
| 1654 |
-
tags$br(),
|
| 1655 |
lapply(names(rv$compare_res), function(m) {
|
| 1656 |
r <- rv$compare_res[[m]]
|
| 1657 |
n <- if (!is.null(r) && !"Message" %in% colnames(r))
|
|
@@ -1710,7 +1808,6 @@ server <- function(input, output, session) {
|
|
| 1710 |
df <- do.call(rbind, rows)
|
| 1711 |
df$Chromosome <- factor(df$Chromosome, levels = sort(unique(df$Chromosome)))
|
| 1712 |
n_m <- length(unique(df$Method))
|
| 1713 |
-
# Vivid distinct colors per model
|
| 1714 |
vivid <- c("#FF006E","#3A86FF","#06D6A0","#FFBE0B","#FB5607","#8338EC")
|
| 1715 |
mcols <- vivid[seq_len(n_m)]
|
| 1716 |
plot_ly(df, x = ~Chromosome, y = ~Count, color = ~Method, type = "bar",
|
|
@@ -1734,9 +1831,13 @@ server <- function(input, output, session) {
|
|
| 1734 |
if (is.null(r) || "Message" %in% colnames(r)) next
|
| 1735 |
sig <- r[!is.na(r$LOD) & r$LOD >= lod_thr(), ]
|
| 1736 |
if (nrow(sig) == 0) next
|
|
|
|
| 1737 |
sig$Model_Used <- mth
|
| 1738 |
-
|
| 1739 |
-
|
|
|
|
|
|
|
|
|
|
| 1740 |
}
|
| 1741 |
if (length(rows) == 0) return(datatable(data.frame(Status = "No QTLs found")))
|
| 1742 |
df <- do.call(rbind, rows)
|
|
@@ -1760,8 +1861,7 @@ server <- function(input, output, session) {
|
|
| 1760 |
n_all <- nrow(cq)
|
| 1761 |
n_max <- sum(cq$N_Models_Detected == length(rv$compare_res), na.rm = TRUE)
|
| 1762 |
div(class = "good-box",
|
| 1763 |
-
tags$b(n_all), " common QTLs | ", tags$b(n_max), " in ALL models",
|
| 1764 |
-
tags$br(),
|
| 1765 |
tags$small("Chromosomes: ", paste(sort(unique(cq$Chromosome)), collapse = ", ")))
|
| 1766 |
})
|
| 1767 |
|
|
@@ -1770,6 +1870,8 @@ server <- function(input, output, session) {
|
|
| 1770 |
if (is.null(cq) || nrow(cq) == 0)
|
| 1771 |
return(datatable(data.frame(Status = "Run Compare Models tab first"),
|
| 1772 |
options = list(dom = "t")))
|
|
|
|
|
|
|
| 1773 |
num_cols <- intersect(
|
| 1774 |
c("Position","Mean_LOD","Max_LOD","Mean_PVE","Mean_Add_Eff",
|
| 1775 |
paste0("LOD_", names(rv$compare_res))),
|
|
@@ -1790,7 +1892,7 @@ server <- function(input, output, session) {
|
|
| 1790 |
output$common_idiogram <- renderPlot({
|
| 1791 |
cq <- common_qtl_data()
|
| 1792 |
req(!is.null(cq), nrow(cq) > 0, rv$marker_info)
|
| 1793 |
-
n_levels
|
| 1794 |
qtl_list_cq <- setNames(lapply(n_levels, function(nl) {
|
| 1795 |
sub <- cq[cq$N_Models_Detected == nl,
|
| 1796 |
c("Marker","Chromosome","Position","Max_LOD","Mean_PVE","Mean_Add_Eff")]
|
|
@@ -1837,11 +1939,8 @@ server <- function(input, output, session) {
|
|
| 1837 |
req(rv$scan_all, rv$marker_info)
|
| 1838 |
valid <- if (!is.null(rv$scan_all) && !"Message" %in% colnames(rv$scan_all))
|
| 1839 |
list(setNames(list(rv$scan_all), input$mapping_method)) else list()
|
| 1840 |
-
if (length(valid) == 0)
|
| 1841 |
-
|
| 1842 |
-
} else {
|
| 1843 |
-
make_idiogram_ggplot(valid[[1]], rv$marker_info, lod_thr())
|
| 1844 |
-
}
|
| 1845 |
}, bg = "white")
|
| 1846 |
|
| 1847 |
output$perf_plot <- renderPlotly({
|
|
@@ -1922,18 +2021,19 @@ server <- function(input, output, session) {
|
|
| 1922 |
req(rv$scan_all)
|
| 1923 |
if ("Message" %in% colnames(rv$scan_all))
|
| 1924 |
return(datatable(data.frame(Status = rv$scan_all$Message[1]), options = list(dom = "t")))
|
| 1925 |
-
|
|
|
|
|
|
|
| 1926 |
options = list(scrollX = TRUE, pageLength = 15, dom = "Blfrtip",
|
| 1927 |
order = list(list(4, "desc"))),
|
| 1928 |
rownames = FALSE, class = "cell-border stripe hover") %>%
|
| 1929 |
-
formatRound(intersect(c("LOD","PVE_Marker_Percent","Additive_Effect"),
|
| 1930 |
-
|
| 1931 |
-
formatSignif(intersect("P_Value", colnames(rv$scan_all)), digits = 3)
|
| 1932 |
})
|
| 1933 |
|
| 1934 |
# ---- PREDICT TEST ----
|
| 1935 |
observeEvent(input$pred_btn, {
|
| 1936 |
-
if (is.null(rv$model_fit)) { showNotification("Run analysis first",
|
| 1937 |
if (is.null(rv$test_geno)) { showNotification("Upload test genotype first", type = "error"); return() }
|
| 1938 |
withProgress(message = "Predicting test samples...", value = 0.3, {
|
| 1939 |
train_cols <- rv$model_cols
|
|
@@ -1969,10 +2069,8 @@ server <- function(input, output, session) {
|
|
| 1969 |
}, error = function(e) showNotification(paste("Domestication error:", e$message), type = "error"))
|
| 1970 |
})
|
| 1971 |
|
| 1972 |
-
# Generic colorful plotly for Fst / Tajima
|
| 1973 |
make_dom_plotly <- function(data, y_col, title, threshold, y_lab = y_col) {
|
| 1974 |
-
blank <- plot_ly() %>%
|
| 1975 |
-
layout(title = title, paper_bgcolor = "white", plot_bgcolor = "white")
|
| 1976 |
if (is.null(data) || nrow(data) == 0) return(blank)
|
| 1977 |
pd <- data[!is.na(data[[y_col]]) & !is.na(data$Chromosome), ]
|
| 1978 |
if (nrow(pd) == 0) return(blank)
|
|
@@ -1981,8 +2079,8 @@ server <- function(input, output, session) {
|
|
| 1981 |
offsets <- setNames(numeric(length(chroms)), chroms)
|
| 1982 |
for (i in seq_along(chroms)) {
|
| 1983 |
if (i == 1) { offsets[chroms[1]] <- 0; next }
|
| 1984 |
-
offsets[chroms[i]] <- offsets[chroms[i
|
| 1985 |
-
max(pd$Position[pd$Chromosome == chroms[i
|
| 1986 |
}
|
| 1987 |
pd$cum_pos <- pd$Position + offsets[pd$Chromosome]
|
| 1988 |
chr_mids <- tapply(pd$cum_pos, pd$Chromosome, median, na.rm = TRUE)
|
|
@@ -1996,13 +2094,12 @@ server <- function(input, output, session) {
|
|
| 1996 |
type = "scatter", mode = "markers", name = paste("Chr", ch), showlegend = FALSE,
|
| 1997 |
marker = list(size = 6, color = chr_clrs[ch], opacity = 0.75),
|
| 1998 |
text = ~paste0("<b>", Marker, "</b><br>Chr: ", Chromosome,
|
| 1999 |
-
"<br>Pos: ", round(Position, 1),
|
| 2000 |
-
"<br>", y_lab, ": ", round(get(y_col), 4)),
|
| 2001 |
hoverinfo = "text")
|
| 2002 |
}
|
| 2003 |
if (!is.null(threshold))
|
| 2004 |
pl <- pl %>% add_segments(
|
| 2005 |
-
x = min(pd$cum_pos, na.rm
|
| 2006 |
y = threshold, yend = threshold,
|
| 2007 |
line = list(color = "#FF006E", dash = "dash", width = 1.8), showlegend = FALSE)
|
| 2008 |
pl %>% layout(
|
|
@@ -2025,13 +2122,49 @@ server <- function(input, output, session) {
|
|
| 2025 |
formatRound(c("Fst","Pi_Dom","Pi_Wild","Tajima_D_Dom"), 4)
|
| 2026 |
})
|
| 2027 |
|
| 2028 |
-
#
|
|
|
|
|
|
|
| 2029 |
output$dl_manhattan <- downloadHandler(
|
| 2030 |
filename = function() paste0("manhattan_", Sys.Date(), ".png"),
|
| 2031 |
content = function(file) {
|
| 2032 |
req(rv$scan_all)
|
| 2033 |
-
|
| 2034 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2035 |
ggsave(file, plot = p, width = 14, height = 6, dpi = 200, bg = "white")
|
| 2036 |
}
|
| 2037 |
)
|
|
@@ -2050,38 +2183,89 @@ server <- function(input, output, session) {
|
|
| 2050 |
}
|
| 2051 |
)
|
| 2052 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2053 |
output$dl_results <- downloadHandler(
|
| 2054 |
-
filename = function() paste0("
|
| 2055 |
content = function(file) {
|
| 2056 |
sheets <- list()
|
|
|
|
|
|
|
| 2057 |
if (!is.null(rv$scan_all) && !"Message" %in% colnames(rv$scan_all)) {
|
| 2058 |
sig <- rv$scan_all[!is.na(rv$scan_all$LOD) & rv$scan_all$LOD >= lod_thr(), ]
|
| 2059 |
if (nrow(sig) > 0) {
|
| 2060 |
sig <- add_support_intervals(sig, rv$scan_all, isolate(input$lod_drop))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2061 |
sheets[["QTL_Significant"]] <- sig[order(-sig$LOD), ]
|
| 2062 |
}
|
| 2063 |
-
|
|
|
|
|
|
|
| 2064 |
}
|
|
|
|
| 2065 |
if (!is.null(rv$perf)) sheets[["Performance"]] <- rv$perf
|
|
|
|
|
|
|
| 2066 |
for (m in names(rv$compare_res)) {
|
| 2067 |
r <- rv$compare_res[[m]]
|
| 2068 |
if (!is.null(r) && !"Message" %in% colnames(r)) {
|
| 2069 |
sig_m <- r[!is.na(r$LOD) & r$LOD >= lod_thr(), ]
|
| 2070 |
if (nrow(sig_m) > 0) {
|
| 2071 |
sig_m <- add_support_intervals(sig_m, r, isolate(input$lod_drop))
|
|
|
|
|
|
|
|
|
|
| 2072 |
sheets[[paste0("Model_", m)]] <- sig_m
|
| 2073 |
}
|
|
|
|
|
|
|
|
|
|
| 2074 |
}
|
| 2075 |
}
|
|
|
|
|
|
|
| 2076 |
cq <- tryCatch(find_common_qtl(rv$compare_res, lod_thr(), isolate(input$min_models)),
|
| 2077 |
error = function(e) NULL)
|
| 2078 |
-
if (!is.null(cq) && nrow(cq) > 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2079 |
sheets[["Methods"]] <- data.frame(
|
| 2080 |
Method = names(MAPPING_MODELS),
|
| 2081 |
Label = sapply(MAPPING_MODELS, `[[`, "label"),
|
| 2082 |
Description = sapply(MAPPING_MODELS, `[[`, "desc"))
|
|
|
|
| 2083 |
if (!is.null(rv$sel_metrics) && nrow(rv$sel_metrics) > 0)
|
| 2084 |
sheets[["Selection_Signatures"]] <- rv$sel_metrics
|
|
|
|
| 2085 |
if (length(sheets) == 0) sheets[["Empty"]] <- data.frame(msg = "Run analysis first")
|
| 2086 |
writexl::write_xlsx(sheets, file)
|
| 2087 |
}
|
|
@@ -2100,11 +2284,13 @@ server <- function(input, output, session) {
|
|
| 2100 |
content = function(file) {
|
| 2101 |
cq <- tryCatch(find_common_qtl(rv$compare_res, lod_thr(), isolate(input$min_models)),
|
| 2102 |
error = function(e) NULL)
|
| 2103 |
-
if (!is.null(cq) && nrow(cq) > 0)
|
|
|
|
| 2104 |
writexl::write_xlsx(list(Common_QTL = cq), file)
|
| 2105 |
-
else
|
| 2106 |
writexl::write_xlsx(
|
| 2107 |
list(Empty = data.frame(msg = "No common QTLs or run Compare Models first")), file)
|
|
|
|
| 2108 |
}
|
| 2109 |
)
|
| 2110 |
|
|
|
|
| 1 |
# ============================================================
|
| 2 |
+
# QTL-Mapper v10 — Fixed CIM + Flanking Markers in Excel + Per-Chromosome LOD Plots
|
| 3 |
#
|
| 4 |
+
# FIXES over v9:
|
| 5 |
+
# - CIM BUG FIX: Cofactor selection now uses lower threshold (0.1 LOD gain)
|
| 6 |
+
# and falls back to top-N markers by SIM LOD if stepwise fails
|
| 7 |
+
# - CIM window exclusion uses position-based cM distance, not marker index count
|
| 8 |
+
# - Flanking markers (Left_Flanking_Marker, Right_Flanking_Marker) now included
|
| 9 |
+
# in ALL Excel output sheets (Significant QTLs, Full Scan, Model_*, Common_QTL)
|
| 10 |
+
# - NEW: Per-chromosome QTL mapping plot (like QTL Cartographer output):
|
| 11 |
+
# • Each chromosome panel shows idiogram on left + LOD curve on right
|
| 12 |
+
# • QTL positions marked with arrows and labels (e.g. qGER1)
|
| 13 |
+
# • Significance threshold shown as dashed red line
|
| 14 |
+
# • Available in Results tab as "Chr-by-Chr LOD Plot" and as PNG download
|
| 15 |
# ============================================================
|
| 16 |
|
| 17 |
options(repos = c(CRAN = "https://cloud.r-project.org"))
|
|
|
|
| 39 |
}
|
| 40 |
|
| 41 |
# ============================================================
|
| 42 |
+
# COLOR PALETTES
|
| 43 |
# ============================================================
|
| 44 |
CHR_PALETTE <- c(
|
| 45 |
"#FF006E","#FB5607","#FFBE0B","#8338EC","#3A86FF",
|
|
|
|
| 56 |
RF = "#8338EC"
|
| 57 |
)
|
| 58 |
|
|
|
|
| 59 |
LOD_COLOR_SCALE <- c("#3A86FF","#06D6A0","#FFBE0B","#FB5607","#FF006E","#8338EC")
|
| 60 |
|
| 61 |
# ============================================================
|
|
|
|
| 65 |
"SIM" = list(label = "Simple Interval Mapping (SIM)",
|
| 66 |
desc = "QTL Cartographer-style single-marker regression; LOD = (n/2)*log10(RSS0/RSS1)"),
|
| 67 |
"CIM" = list(label = "Composite Interval Mapping (CIM)",
|
| 68 |
+
desc = "QTL Cartographer CIM: background marker cofactors selected by forward stepwise regression (min LOD gain 0.1). Falls back to top-SIM markers if stepwise yields no cofactors."),
|
| 69 |
"MQM" = list(label = "Multiple QTL Model (MQM)",
|
| 70 |
desc = "Multiple QTL stepwise model; each QTL tested against a multi-QTL background"),
|
| 71 |
"LASSO" = list(label = "LASSO QTL (L1 penalized)",
|
|
|
|
| 107 |
# FLANKING MARKER DETECTION
|
| 108 |
# ============================================================
|
| 109 |
find_flanking_markers <- function(marker_name, marker_info) {
|
| 110 |
+
mi <- marker_info[marker_info$Marker == marker_name, ]
|
| 111 |
if (nrow(mi) == 0) return(list(left = NA_character_, right = NA_character_))
|
| 112 |
+
chr <- as.character(mi$Chromosome[1])
|
| 113 |
+
pos <- mi$Position[1]
|
| 114 |
chr_mk <- marker_info[as.character(marker_info$Chromosome) == chr, ]
|
| 115 |
chr_mk <- chr_mk[order(chr_mk$Position), ]
|
| 116 |
+
idx <- which(chr_mk$Marker == marker_name)
|
| 117 |
+
if (length(idx) == 0) { dists <- abs(chr_mk$Position - pos); idx <- which.min(dists) }
|
|
|
|
|
|
|
|
|
|
| 118 |
idx <- idx[1]
|
| 119 |
+
list(
|
| 120 |
+
left = if (idx > 1) chr_mk$Marker[idx - 1] else NA_character_,
|
| 121 |
+
right = if (idx < nrow(chr_mk)) chr_mk$Marker[idx + 1] else NA_character_
|
| 122 |
+
)
|
| 123 |
}
|
| 124 |
|
| 125 |
# ============================================================
|
| 126 |
# LOD SUPPORT INTERVAL
|
| 127 |
# ============================================================
|
| 128 |
add_support_intervals <- function(sig_df, all_df, lod_drop = 1.5) {
|
| 129 |
+
sig_df$Interval_Start <- NA_real_
|
| 130 |
+
sig_df$Interval_End <- NA_real_
|
| 131 |
+
sig_df$Interval_Size_cM <- NA_real_
|
| 132 |
+
sig_df$Left_Flanking_Marker <- NA_character_
|
| 133 |
+
sig_df$Right_Flanking_Marker <- NA_character_
|
| 134 |
if (nrow(sig_df) == 0) return(sig_df)
|
| 135 |
all_df$Chromosome <- as.character(all_df$Chromosome)
|
| 136 |
for (i in seq_len(nrow(sig_df))) {
|
|
|
|
| 138 |
chr <- as.character(sig_df$Chromosome[i])
|
| 139 |
pk <- sig_df$LOD[i]
|
| 140 |
pos_i <- sig_df$Position[i]
|
| 141 |
+
cd <- all_df[all_df$Chromosome == chr & !is.na(all_df$LOD) & !is.na(all_df$Position), ]
|
| 142 |
+
cd <- cd[order(cd$Position), ]
|
| 143 |
if (nrow(cd) < 2) {
|
| 144 |
+
sig_df$Interval_Start[i] <- pos_i
|
| 145 |
+
sig_df$Interval_End[i] <- pos_i
|
| 146 |
+
sig_df$Interval_Size_cM[i] <- 0
|
| 147 |
fl <- find_flanking_in_sorted(mk, cd)
|
| 148 |
sig_df$Left_Flanking_Marker[i] <- fl$left
|
| 149 |
sig_df$Right_Flanking_Marker[i] <- fl$right
|
|
|
|
| 177 |
)
|
| 178 |
}
|
| 179 |
|
| 180 |
+
# ============================================================
|
| 181 |
+
# Add flanking markers to ANY scan result (full scan or sig)
|
| 182 |
+
# ============================================================
|
| 183 |
+
add_flanking_to_df <- function(df, marker_info) {
|
| 184 |
+
if (is.null(df) || nrow(df) == 0) return(df)
|
| 185 |
+
if (!"Left_Flanking_Marker" %in% colnames(df)) df$Left_Flanking_Marker <- NA_character_
|
| 186 |
+
if (!"Right_Flanking_Marker" %in% colnames(df)) df$Right_Flanking_Marker <- NA_character_
|
| 187 |
+
for (i in seq_len(nrow(df))) {
|
| 188 |
+
mk <- df$Marker[i]
|
| 189 |
+
chr <- as.character(df$Chromosome[i])
|
| 190 |
+
chr_mk <- marker_info[as.character(marker_info$Chromosome) == chr, ]
|
| 191 |
+
chr_mk <- chr_mk[order(chr_mk$Position), ]
|
| 192 |
+
idx <- which(chr_mk$Marker == mk)
|
| 193 |
+
if (length(idx) == 0) next
|
| 194 |
+
idx <- idx[1]
|
| 195 |
+
df$Left_Flanking_Marker[i] <- if (idx > 1) chr_mk$Marker[idx - 1] else NA_character_
|
| 196 |
+
df$Right_Flanking_Marker[i] <- if (idx < nrow(chr_mk)) chr_mk$Marker[idx + 1] else NA_character_
|
| 197 |
+
}
|
| 198 |
+
df
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
# ============================================================
|
| 202 |
# GENOTYPE LOADER
|
| 203 |
# ============================================================
|
|
|
|
| 249 |
# ADDITIVE / DOMINANCE EFFECT ESTIMATION
|
| 250 |
# ============================================================
|
| 251 |
estimate_effects <- function(geno_v, pheno_v) {
|
| 252 |
+
a_code <- geno_v - 1
|
| 253 |
+
d_code <- as.integer(geno_v == 1)
|
| 254 |
+
has_het <- sum(geno_v == 1, na.rm = TRUE) >= 3
|
| 255 |
a_eff <- se_a <- d_eff <- se_d <- dom_ratio <- NA_real_
|
| 256 |
dom_cl <- NA_character_
|
| 257 |
if (has_het && length(unique(d_code)) > 1) {
|
|
|
|
| 319 |
}
|
| 320 |
|
| 321 |
# ============================================================
|
| 322 |
+
# CIM — FIXED VERSION
|
| 323 |
+
# Key fixes:
|
| 324 |
+
# 1. Cofactor selection threshold lowered to 0.1 LOD gain (was 0.5)
|
| 325 |
+
# 2. Fallback: if stepwise finds 0 cofactors, use top-K markers by SIM LOD
|
| 326 |
+
# 3. Window exclusion uses cM distance (window_cM), not marker count
|
| 327 |
+
# 4. Complete-cases check per marker (not global) to maximize marker coverage
|
| 328 |
# ============================================================
|
| 329 |
qtl_scan_cim <- function(X, y, marker_info, n_cofactors = 10, window_size = 10) {
|
| 330 |
+
cat("\n--- CIM (FIXED) ---\n")
|
| 331 |
valid <- !is.na(y)
|
| 332 |
X <- X[valid, , drop = FALSE]; y <- y[valid]
|
| 333 |
nzv <- apply(X, 2, function(x) length(unique(na.omit(x))) < 2)
|
| 334 |
X <- X[, !nzv, drop = FALSE]
|
| 335 |
n <- length(y)
|
| 336 |
max_k <- max(3, min(n_cofactors, floor(sqrt(n))))
|
| 337 |
+
|
| 338 |
+
# --- Step 1: Forward stepwise cofactor selection with LOW threshold ---
|
| 339 |
+
cat(" Selecting cofactors (max_k =", max_k, ")...\n")
|
| 340 |
+
cofactors <- character(0)
|
| 341 |
+
remaining <- colnames(X)
|
| 342 |
+
null_fit <- lm(y ~ 1)
|
| 343 |
+
base_lod <- 0
|
| 344 |
+
|
| 345 |
for (step in seq_len(max_k)) {
|
| 346 |
if (length(remaining) == 0) break
|
| 347 |
+
best_m <- NULL
|
| 348 |
+
best_lod <- base_lod
|
| 349 |
+
|
| 350 |
for (m in remaining) {
|
| 351 |
tryCatch({
|
| 352 |
cand <- c(cofactors, m)
|
| 353 |
+
Xc <- as.data.frame(X[, cand, drop = FALSE])
|
| 354 |
+
Xc[is.na(Xc)] <- 0
|
| 355 |
+
lod <- calc_lod(lm(y ~ ., data = Xc), null_fit)
|
| 356 |
if (lod > best_lod) { best_lod <- lod; best_m <- m }
|
| 357 |
}, error = function(e) NULL)
|
| 358 |
}
|
| 359 |
+
|
| 360 |
+
# FIX: Use 0.1 LOD gain threshold (was 0.5 — too strict)
|
| 361 |
+
if (!is.null(best_m) && (best_lod - base_lod) > 0.1) {
|
| 362 |
+
cofactors <- c(cofactors, best_m)
|
| 363 |
+
remaining <- setdiff(remaining, best_m)
|
| 364 |
+
base_lod <- best_lod
|
| 365 |
cat(" Cofactor", length(cofactors), ":", best_m, " LOD:", round(best_lod, 2), "\n")
|
| 366 |
+
} else {
|
| 367 |
+
break
|
| 368 |
+
}
|
| 369 |
+
}
|
| 370 |
+
|
| 371 |
+
# --- Step 2: FALLBACK if no cofactors found ---
|
| 372 |
+
if (length(cofactors) == 0) {
|
| 373 |
+
cat(" Stepwise found 0 cofactors — using fallback: top SIM LOD markers\n")
|
| 374 |
+
sim_res <- qtl_scan_sim(X, y, marker_info)
|
| 375 |
+
if (!"Message" %in% colnames(sim_res)) {
|
| 376 |
+
sim_res <- sim_res[order(-sim_res$LOD), ]
|
| 377 |
+
cofactors <- head(sim_res$Marker[sim_res$Marker %in% colnames(X)], max_k)
|
| 378 |
+
}
|
| 379 |
}
|
| 380 |
+
|
| 381 |
+
cat(" Using", length(cofactors), "cofactors:", paste(head(cofactors, 5), collapse=", "),
|
| 382 |
+
if(length(cofactors)>5) "..." else "", "\n")
|
| 383 |
+
|
| 384 |
+
# --- Step 3: Window size in cM ---
|
| 385 |
+
# Convert marker-count window to cM: use median inter-marker distance * window_size
|
| 386 |
+
chr_list <- sort(unique(as.character(marker_info$Chromosome)))
|
| 387 |
+
all_gaps <- numeric(0)
|
| 388 |
+
for (ch in chr_list) {
|
| 389 |
+
mk_ch <- marker_info[as.character(marker_info$Chromosome) == ch, ]
|
| 390 |
+
mk_ch <- mk_ch[order(mk_ch$Position), ]
|
| 391 |
+
if (nrow(mk_ch) > 1) all_gaps <- c(all_gaps, diff(mk_ch$Position))
|
| 392 |
+
}
|
| 393 |
+
med_gap <- if (length(all_gaps) > 0) median(all_gaps[all_gaps > 0], na.rm = TRUE) else 5
|
| 394 |
+
window_cM <- window_size * med_gap
|
| 395 |
+
cat(" Exclusion window:", round(window_cM, 1), "cM\n")
|
| 396 |
+
|
| 397 |
+
# --- Step 4: Scan each marker ---
|
| 398 |
res_list <- vector("list", ncol(X)); names(res_list) <- colnames(X)
|
| 399 |
+
|
| 400 |
for (m in colnames(X)) {
|
| 401 |
g <- X[, m]; ok <- !is.na(g); gv <- g[ok]; pv <- y[ok]
|
| 402 |
if (length(unique(gv)) < 2 || length(pv) < 8) next
|
| 403 |
mi <- marker_info[marker_info$Marker == m, ]
|
| 404 |
if (nrow(mi) == 0 || is.na(mi$Position[1])) next
|
| 405 |
+
|
| 406 |
+
m_chr <- as.character(mi$Chromosome[1])
|
| 407 |
+
m_pos <- mi$Position[1]
|
| 408 |
+
|
| 409 |
+
# Exclude cofactors within window_cM on same chromosome
|
| 410 |
+
cof_pos <- sapply(cofactors, function(cf) {
|
| 411 |
+
cf_mi <- marker_info[marker_info$Marker == cf, ]
|
| 412 |
+
if (nrow(cf_mi) == 0) return(Inf)
|
| 413 |
+
if (as.character(cf_mi$Chromosome[1]) != m_chr) return(Inf)
|
| 414 |
+
cf_mi$Position[1]
|
| 415 |
+
})
|
| 416 |
+
active_cof <- cofactors[abs(cof_pos - m_pos) > window_cM]
|
| 417 |
+
|
| 418 |
tryCatch({
|
| 419 |
df <- data.frame(y = pv, focal = gv)
|
| 420 |
if (length(active_cof) > 0) {
|
| 421 |
+
# Only include cofactors that are in X and have no NA for these samples
|
| 422 |
+
valid_cof <- active_cof[active_cof %in% colnames(X)]
|
| 423 |
+
if (length(valid_cof) > 0) {
|
| 424 |
+
cm <- X[ok, valid_cof, drop = FALSE]
|
| 425 |
+
cm[is.na(cm)] <- 0
|
| 426 |
+
df <- cbind(df, as.data.frame(cm))
|
| 427 |
+
} else {
|
| 428 |
+
active_cof <- character(0)
|
| 429 |
+
}
|
| 430 |
}
|
| 431 |
df <- df[complete.cases(df), ]
|
| 432 |
+
if (nrow(df) < 8) next
|
| 433 |
+
|
| 434 |
+
if (length(active_cof) > 0 && length(intersect(active_cof, colnames(X))) > 0) {
|
| 435 |
+
valid_cof_names <- intersect(active_cof, colnames(df))
|
| 436 |
+
if (length(valid_cof_names) > 0) {
|
| 437 |
+
fit_full <- lm(as.formula(
|
| 438 |
+
paste("y ~ focal +", paste(valid_cof_names, collapse = "+"))), data = df)
|
| 439 |
+
fit_null <- lm(as.formula(
|
| 440 |
+
paste("y ~", paste(valid_cof_names, collapse = "+"))), data = df)
|
| 441 |
+
} else {
|
| 442 |
+
fit_full <- lm(y ~ focal, data = df)
|
| 443 |
+
fit_null <- lm(y ~ 1, data = df)
|
| 444 |
+
}
|
| 445 |
} else {
|
| 446 |
+
fit_full <- lm(y ~ focal, data = df)
|
| 447 |
+
fit_null <- lm(y ~ 1, data = df)
|
| 448 |
}
|
| 449 |
+
|
| 450 |
+
lod <- calc_lod(fit_full, fit_null)
|
| 451 |
+
pve <- calc_pve(fit_full, fit_null)
|
| 452 |
ct <- coef(summary(fit_full))
|
| 453 |
if (!"focal" %in% rownames(ct)) next
|
| 454 |
pval <- ct["focal", "Pr(>|t|)"]
|
| 455 |
eff <- estimate_effects(gv, pv)
|
| 456 |
+
|
| 457 |
res_list[[m]] <- data.frame(
|
| 458 |
Marker=m, Chromosome=m_chr, Position=m_pos, LOD=lod, P_Value=pval,
|
| 459 |
PVE_Marker_Percent=pve, PVE_CIM_Percent=pve,
|
|
|
|
| 465 |
Method="CIM", stringsAsFactors=FALSE)
|
| 466 |
}, error = function(e) NULL)
|
| 467 |
}
|
| 468 |
+
|
| 469 |
res <- do.call(rbind, Filter(Negate(is.null), res_list))
|
| 470 |
if (is.null(res) || nrow(res) == 0) return(data.frame(Message = "CIM: no results"))
|
| 471 |
res[order(res$Chromosome, res$Position), ]
|
|
|
|
| 485 |
n <- length(y); max_k <- min(n_cofactors, floor(sqrt(n)))
|
| 486 |
sim_res <- sim_res[order(-sim_res$LOD), ]
|
| 487 |
candidates <- head(sim_res$Marker[sim_res$Marker %in% colnames(X)], max_k * 2)
|
| 488 |
+
selected <- character(0)
|
| 489 |
for (cand in candidates) {
|
| 490 |
if (length(selected) >= max_k) break
|
| 491 |
tryCatch({
|
|
|
|
| 498 |
} else lod_base <- 0
|
| 499 |
gain <- lod_full - lod_base
|
| 500 |
if (gain >= 2.0) {
|
| 501 |
+
selected <- test_set
|
| 502 |
cat(" MQM QTL:", cand, " gain:", round(gain, 2), "\n")
|
| 503 |
}
|
| 504 |
}, error = function(e) NULL)
|
| 505 |
}
|
| 506 |
cat(" MQM selected", length(selected), "QTLs\n")
|
| 507 |
+
|
| 508 |
+
# cM window
|
| 509 |
+
all_gaps <- numeric(0)
|
| 510 |
+
for (ch in sort(unique(as.character(marker_info$Chromosome)))) {
|
| 511 |
+
mk_ch <- marker_info[as.character(marker_info$Chromosome) == ch, ]
|
| 512 |
+
mk_ch <- mk_ch[order(mk_ch$Position), ]
|
| 513 |
+
if (nrow(mk_ch) > 1) all_gaps <- c(all_gaps, diff(mk_ch$Position))
|
| 514 |
+
}
|
| 515 |
+
med_gap <- if (length(all_gaps) > 0) median(all_gaps[all_gaps > 0], na.rm = TRUE) else 5
|
| 516 |
+
window_cM <- window_size * med_gap
|
| 517 |
+
|
| 518 |
res_list <- vector("list", ncol(X)); names(res_list) <- colnames(X)
|
| 519 |
for (m in colnames(X)) {
|
| 520 |
g <- X[, m]; ok <- !is.na(g); gv <- g[ok]; pv <- y[ok]
|
|
|
|
| 522 |
mi <- marker_info[marker_info$Marker == m, ]
|
| 523 |
if (nrow(mi) == 0 || is.na(mi$Position[1])) next
|
| 524 |
m_chr <- as.character(mi$Chromosome[1]); m_pos <- mi$Position[1]
|
| 525 |
+
cof_pos <- sapply(selected, function(cf) {
|
| 526 |
+
cf_mi <- marker_info[marker_info$Marker == cf, ]
|
| 527 |
+
if (nrow(cf_mi) == 0) return(Inf)
|
| 528 |
+
if (as.character(cf_mi$Chromosome[1]) != m_chr) return(Inf)
|
| 529 |
+
cf_mi$Position[1]
|
| 530 |
+
})
|
| 531 |
+
active_cof <- selected[abs(cof_pos - m_pos) > window_cM]
|
|
|
|
|
|
|
| 532 |
tryCatch({
|
| 533 |
df <- data.frame(y = pv, focal = gv)
|
| 534 |
if (length(active_cof) > 0) {
|
| 535 |
+
valid_cof <- active_cof[active_cof %in% colnames(X)]
|
| 536 |
+
if (length(valid_cof) > 0) {
|
| 537 |
+
cm <- X[ok, valid_cof, drop = FALSE]; cm[is.na(cm)] <- 0
|
| 538 |
+
df <- cbind(df, as.data.frame(cm))
|
| 539 |
+
} else active_cof <- character(0)
|
| 540 |
}
|
| 541 |
df <- df[complete.cases(df), ]
|
| 542 |
+
if (nrow(df) < 8) next
|
| 543 |
+
if (length(active_cof) > 0 && any(active_cof %in% colnames(df))) {
|
| 544 |
+
valid_nm <- intersect(active_cof, colnames(df))
|
| 545 |
+
fit_full <- lm(as.formula(paste("y ~ focal +", paste(valid_nm, collapse="+"))), data=df)
|
| 546 |
+
fit_null <- lm(as.formula(paste("y ~", paste(valid_nm, collapse="+"))), data=df)
|
| 547 |
} else {
|
| 548 |
+
fit_full <- lm(y ~ focal, data=df); fit_null <- lm(y ~ 1, data=df)
|
| 549 |
}
|
| 550 |
lod <- calc_lod(fit_full, fit_null); pve <- calc_pve(fit_full, fit_null)
|
| 551 |
ct <- coef(summary(fit_full))
|
|
|
|
| 698 |
# ============================================================
|
| 699 |
# FAST PARALLEL PERMUTATION THRESHOLD
|
| 700 |
# ============================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 701 |
permutation_threshold <- function(X, y, marker_info, method = "SIM",
|
| 702 |
n_perm = 100, alpha = 0.05,
|
| 703 |
n_cofactors = 5, window_size = 5,
|
| 704 |
progress_cb = NULL) {
|
| 705 |
+
cat(" Running", n_perm, "PARALLEL permutations...\n")
|
|
|
|
|
|
|
|
|
|
| 706 |
n_markers_use <- min(200, ncol(X))
|
| 707 |
if (ncol(X) > n_markers_use) {
|
| 708 |
set.seed(99)
|
| 709 |
+
keep_cols <- sample(colnames(X), n_markers_use)
|
| 710 |
+
X_perm <- X[, keep_cols, drop = FALSE]
|
| 711 |
+
mi_perm <- marker_info[marker_info$Marker %in% keep_cols, ]
|
| 712 |
} else {
|
| 713 |
+
X_perm <- X; mi_perm <- marker_info
|
|
|
|
| 714 |
}
|
| 715 |
+
n_cores <- max(1, min(parallel::detectCores(logical = TRUE) - 1, 8))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 716 |
cat(" Using", n_cores, "cores\n")
|
| 717 |
|
|
|
|
| 718 |
one_perm <- function(seed) {
|
| 719 |
set.seed(seed)
|
| 720 |
yp <- sample(y)
|
| 721 |
+
res <- tryCatch(qtl_scan_sim(X_perm, yp, mi_perm), error = function(e) NULL)
|
|
|
|
|
|
|
| 722 |
if (!is.null(res) && !"Message" %in% colnames(res) && "LOD" %in% colnames(res))
|
| 723 |
return(max(res$LOD, na.rm = TRUE))
|
| 724 |
return(0)
|
| 725 |
}
|
|
|
|
| 726 |
seeds <- sample.int(1e6, n_perm)
|
| 727 |
+
if (n_cores > 1 && .Platform$OS.type != "windows") {
|
| 728 |
+
max_lods <- tryCatch(
|
| 729 |
+
unlist(parallel::mclapply(seeds, one_perm, mc.cores = n_cores)),
|
| 730 |
+
error = function(e) sapply(seeds, one_perm))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 731 |
} else {
|
| 732 |
max_lods <- sapply(seeds, one_perm)
|
| 733 |
}
|
|
|
|
| 734 |
thr <- quantile(max_lods, 1 - alpha, na.rm = TRUE)
|
| 735 |
cat(" Permutation LOD threshold (alpha =", alpha, "):", round(thr, 3), "\n")
|
| 736 |
thr
|
|
|
|
| 773 |
}
|
| 774 |
|
| 775 |
# ============================================================
|
| 776 |
+
# PER-CHROMOSOME LOD PLOT (QTL Cartographer style)
|
| 777 |
+
# — Each chromosome: idiogram bar on left, LOD curve on right
|
| 778 |
+
# — Significant QTLs marked with arrows + labels (qTRAIT_chrN)
|
| 779 |
# ============================================================
|
| 780 |
+
make_chr_lod_plot <- function(scan_df, marker_info, lod_threshold = 2.0,
|
| 781 |
+
trait_name = "Trait", method = "CIM",
|
| 782 |
+
n_cols = 4) {
|
| 783 |
if (is.null(scan_df) || "Message" %in% colnames(scan_df))
|
| 784 |
+
return(ggplot() + labs(title = "No data") + theme_bw())
|
| 785 |
+
|
| 786 |
pd <- scan_df[!is.na(scan_df$LOD) & !is.na(scan_df$Position) & !is.na(scan_df$Chromosome), ]
|
| 787 |
+
if (nrow(pd) == 0) return(ggplot() + labs(title = "No scan data") + theme_bw())
|
| 788 |
|
| 789 |
pd$Chromosome <- as.character(pd$Chromosome)
|
| 790 |
+
chroms <- sort(unique(pd$Chromosome), method = "radix")
|
| 791 |
+
n_chr <- length(chroms)
|
| 792 |
+
n_rows_layout <- ceiling(n_chr / n_cols)
|
| 793 |
+
|
| 794 |
+
# Auto QTL naming: qTRAIT_chrN_rank
|
| 795 |
+
sig_all <- pd[pd$LOD >= lod_threshold, ]
|
| 796 |
+
qtl_labels <- character(nrow(pd))
|
| 797 |
+
if (nrow(sig_all) > 0) {
|
| 798 |
+
sig_all <- sig_all[order(sig_all$Chromosome, -sig_all$LOD), ]
|
| 799 |
+
for (ch in chroms) {
|
| 800 |
+
ch_sig <- sig_all[sig_all$Chromosome == ch, ]
|
| 801 |
+
if (nrow(ch_sig) == 0) next
|
| 802 |
+
# Cluster nearby peaks (>= 10 cM apart)
|
| 803 |
+
peak_rows <- ch_sig[1, , drop = FALSE]
|
| 804 |
+
for (j in seq_len(nrow(ch_sig))) {
|
| 805 |
+
p_pos <- ch_sig$Position[j]
|
| 806 |
+
if (all(abs(peak_rows$Position - p_pos) > 10)) {
|
| 807 |
+
peak_rows <- rbind(peak_rows, ch_sig[j, ])
|
| 808 |
+
}
|
| 809 |
+
}
|
| 810 |
+
peak_rows <- peak_rows[order(-peak_rows$LOD), ]
|
| 811 |
+
chr_num <- gsub("[^0-9A-Za-z]", "", ch)
|
| 812 |
+
for (k in seq_len(nrow(peak_rows))) {
|
| 813 |
+
lbl <- paste0("q", substr(toupper(trait_name), 1, 3), chr_num,
|
| 814 |
+
if (nrow(peak_rows) > 1) k else "")
|
| 815 |
+
idx <- which(pd$Marker == peak_rows$Marker[k])
|
| 816 |
+
if (length(idx) > 0) qtl_labels[idx[1]] <- lbl
|
| 817 |
+
}
|
| 818 |
+
}
|
| 819 |
}
|
| 820 |
+
pd$QTL_Label <- qtl_labels
|
| 821 |
+
|
| 822 |
+
# Markers per chromosome for idiogram
|
| 823 |
+
marker_info$Chromosome <- as.character(marker_info$Chromosome)
|
| 824 |
+
chr_info <- do.call(rbind, lapply(chroms, function(ch) {
|
| 825 |
+
mk_ch <- marker_info[marker_info$Chromosome == ch, ]
|
| 826 |
+
data.frame(Chromosome = ch,
|
| 827 |
+
chr_start = min(mk_ch$Position, na.rm = TRUE),
|
| 828 |
+
chr_end = max(mk_ch$Position, na.rm = TRUE),
|
| 829 |
+
stringsAsFactors = FALSE)
|
| 830 |
+
}))
|
| 831 |
|
| 832 |
+
global_lod_max <- max(pd$LOD, na.rm = TRUE) * 1.15
|
| 833 |
+
|
| 834 |
+
# Build per-chromosome panels
|
| 835 |
+
plot_list <- lapply(chroms, function(ch) {
|
| 836 |
+
ch_pd <- pd[pd$Chromosome == ch, ]
|
| 837 |
+
ch_pd <- ch_pd[order(ch_pd$Position), ]
|
| 838 |
+
ci <- chr_info[chr_info$Chromosome == ch, ]
|
| 839 |
+
chr_len <- ci$chr_end - ci$chr_start
|
| 840 |
+
ch_sig <- ch_pd[ch_pd$LOD >= lod_threshold & nchar(ch_pd$QTL_Label) > 0, ]
|
| 841 |
+
|
| 842 |
+
# Map position to y-axis (flip: top = 0 cM)
|
| 843 |
+
pos_range <- c(ci$chr_start, ci$chr_end)
|
| 844 |
+
lod_max <- global_lod_max
|
| 845 |
+
|
| 846 |
+
# Chromosome teal color from reference image
|
| 847 |
+
chr_color <- "#4DB6AC"
|
| 848 |
+
|
| 849 |
+
p <- ggplot(ch_pd, aes(x = LOD, y = Position)) +
|
| 850 |
+
# ---- Chromosome ideogram (left side, x < 0) ----
|
| 851 |
+
# Chromosome body
|
| 852 |
+
annotate("rect",
|
| 853 |
+
xmin = -lod_max * 0.28, xmax = -lod_max * 0.08,
|
| 854 |
+
ymin = ci$chr_start, ymax = ci$chr_end,
|
| 855 |
+
fill = "#E0F2F1", color = "#78909C", linewidth = 0.6) +
|
| 856 |
+
# Centromere band
|
| 857 |
+
annotate("rect",
|
| 858 |
+
xmin = -lod_max * 0.28, xmax = -lod_max * 0.08,
|
| 859 |
+
ymin = ci$chr_start + chr_len * 0.38,
|
| 860 |
+
ymax = ci$chr_start + chr_len * 0.45,
|
| 861 |
+
fill = "#78909C", color = NA) +
|
| 862 |
+
# Telomere caps
|
| 863 |
+
annotate("rect",
|
| 864 |
+
xmin = -lod_max * 0.28, xmax = -lod_max * 0.08,
|
| 865 |
+
ymin = ci$chr_start,
|
| 866 |
+
ymax = ci$chr_start + chr_len * 0.06,
|
| 867 |
+
fill = "#B2DFDB", color = NA) +
|
| 868 |
+
annotate("rect",
|
| 869 |
+
xmin = -lod_max * 0.28, xmax = -lod_max * 0.08,
|
| 870 |
+
ymin = ci$chr_end - chr_len * 0.06,
|
| 871 |
+
ymax = ci$chr_end,
|
| 872 |
+
fill = "#B2DFDB", color = NA) +
|
| 873 |
+
# ---- Marker tick marks on idiogram ----
|
| 874 |
+
geom_segment(data = marker_info[marker_info$Chromosome == ch, ],
|
| 875 |
+
aes(x = -lod_max * 0.08, xend = -lod_max * 0.04,
|
| 876 |
+
y = Position, yend = Position),
|
| 877 |
+
color = "#546E7A", linewidth = 0.25, inherit.aes = FALSE) +
|
| 878 |
+
# ---- LOD curve ----
|
| 879 |
+
geom_line(color = chr_color, linewidth = 1.0, alpha = 0.9) +
|
| 880 |
+
# Area fill under LOD curve
|
| 881 |
+
geom_area(fill = chr_color, alpha = 0.18) +
|
| 882 |
+
# ---- Significance threshold ----
|
| 883 |
+
geom_vline(xintercept = lod_threshold,
|
| 884 |
+
linetype = "dashed", color = "#E53935", linewidth = 0.7, alpha = 0.85) +
|
| 885 |
+
# ---- Significant QTL markers ----
|
| 886 |
+
{if (nrow(ch_sig) > 0)
|
| 887 |
+
geom_point(data = ch_sig, aes(x = LOD, y = Position),
|
| 888 |
+
color = "#E53935", size = 2.5, shape = 18, inherit.aes = FALSE)
|
| 889 |
+
} +
|
| 890 |
+
# ---- QTL labels ----
|
| 891 |
+
{if (nrow(ch_sig) > 0)
|
| 892 |
+
ggrepel::geom_label_repel(
|
| 893 |
+
data = ch_sig,
|
| 894 |
+
aes(x = LOD, y = Position, label = QTL_Label),
|
| 895 |
+
size = 2.8,
|
| 896 |
+
fontface = "bold",
|
| 897 |
+
fill = "white",
|
| 898 |
+
color = "#B71C1C",
|
| 899 |
+
label.padding = unit(0.15, "lines"),
|
| 900 |
+
segment.color = "#E53935",
|
| 901 |
+
segment.size = 0.5,
|
| 902 |
+
direction = "y",
|
| 903 |
+
nudge_x = lod_max * 0.05,
|
| 904 |
+
min.segment.length = 0,
|
| 905 |
+
max.overlaps = 20,
|
| 906 |
+
inherit.aes = FALSE)
|
| 907 |
+
} +
|
| 908 |
+
# ---- Axes / scales ----
|
| 909 |
+
scale_x_continuous(
|
| 910 |
+
limits = c(-lod_max * 0.30, lod_max),
|
| 911 |
+
breaks = c(0, round(lod_threshold, 1),
|
| 912 |
+
round(lod_max * 0.5, 1), round(lod_max * 0.9, 1)),
|
| 913 |
+
labels = function(x) ifelse(x < 0, "", as.character(round(x, 1))),
|
| 914 |
+
expand = expansion(mult = c(0.01, 0.05))) +
|
| 915 |
+
scale_y_continuous(
|
| 916 |
+
limits = c(ci$chr_start - chr_len * 0.08,
|
| 917 |
+
ci$chr_end + chr_len * 0.12),
|
| 918 |
+
breaks = pretty(c(ci$chr_start, ci$chr_end), n = 5),
|
| 919 |
+
expand = expansion(mult = c(0.02, 0.02))) +
|
| 920 |
+
coord_flip() +
|
| 921 |
+
labs(title = paste0("Chr ", ch),
|
| 922 |
+
x = "LOD Score",
|
| 923 |
+
y = "Position (cM)") +
|
| 924 |
+
theme_bw(base_size = 10) +
|
| 925 |
+
theme(
|
| 926 |
+
panel.background = element_rect(fill = "white", color = NA),
|
| 927 |
+
plot.background = element_rect(fill = "white", color = NA),
|
| 928 |
+
panel.grid.major = element_line(color = "grey94", linewidth = 0.4),
|
| 929 |
+
panel.grid.minor = element_blank(),
|
| 930 |
+
panel.border = element_rect(color = "grey80", linewidth = 0.5),
|
| 931 |
+
plot.title = element_text(face = "bold", size = 10,
|
| 932 |
+
color = "#263238", hjust = 0.5,
|
| 933 |
+
margin = margin(b = 2)),
|
| 934 |
+
axis.text = element_text(size = 7, color = "grey30"),
|
| 935 |
+
axis.title.x = element_text(size = 8, color = "grey20", face = "bold"),
|
| 936 |
+
axis.title.y = element_text(size = 7, color = "grey40"),
|
| 937 |
+
plot.margin = unit(c(3, 4, 3, 2), "pt")
|
| 938 |
+
)
|
| 939 |
+
p
|
| 940 |
+
})
|
| 941 |
|
| 942 |
+
# Combine into grid
|
| 943 |
+
n_real <- length(plot_list)
|
| 944 |
+
n_empty <- n_cols * n_rows_layout - n_real
|
| 945 |
+
if (n_empty > 0) {
|
| 946 |
+
for (i in seq_len(n_empty))
|
| 947 |
+
plot_list <- c(plot_list, list(ggplot() + theme_void()))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 948 |
}
|
| 949 |
|
| 950 |
+
title_grob <- grid::textGrob(
|
| 951 |
+
paste0("QTL Mapping — ", trait_name, " | Method: ", method,
|
| 952 |
+
" | LOD threshold: ", lod_threshold),
|
| 953 |
+
gp = grid::gpar(fontsize = 13, fontface = "bold", col = "#212529"))
|
| 954 |
+
|
| 955 |
+
grid_plot <- gridExtra::arrangeGrob(
|
| 956 |
+
grobs = plot_list,
|
| 957 |
+
ncol = n_cols,
|
| 958 |
+
nrow = n_rows_layout,
|
| 959 |
+
top = title_grob)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 960 |
|
| 961 |
+
grid_plot
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 962 |
}
|
| 963 |
|
| 964 |
# ============================================================
|
| 965 |
# COLORFUL PLOTLY MANHATTAN (interactive)
|
|
|
|
| 966 |
# ============================================================
|
| 967 |
make_plotly_manhattan <- function(scan_df, lod_threshold = 2.0, title = "Manhattan Plot") {
|
| 968 |
blank <- plot_ly() %>%
|
|
|
|
| 970 |
if (is.null(scan_df) || "Message" %in% colnames(scan_df)) return(blank)
|
| 971 |
pd <- scan_df[!is.na(scan_df$LOD) & !is.na(scan_df$Position) & !is.na(scan_df$Chromosome), ]
|
| 972 |
if (nrow(pd) == 0) return(blank)
|
|
|
|
| 973 |
pd$Chromosome <- as.character(pd$Chromosome)
|
| 974 |
chroms <- sort(unique(pd$Chromosome), method = "radix")
|
| 975 |
offsets <- setNames(numeric(length(chroms)), chroms)
|
|
|
|
| 980 |
}
|
| 981 |
pd$cum_pos <- pd$Position + offsets[pd$Chromosome]
|
| 982 |
pd$sig <- pd$LOD >= lod_threshold
|
| 983 |
+
chr_mids <- tapply(pd$cum_pos, pd$Chromosome, median, na.rm = TRUE)
|
|
|
|
|
|
|
|
|
|
| 984 |
chr_bg_colors <- rep(c("#3A86FF","#FB5607"), length.out = length(chroms))
|
| 985 |
names(chr_bg_colors) <- chroms
|
|
|
|
| 986 |
pl <- plot_ly()
|
| 987 |
for (ch in chroms) {
|
| 988 |
sub <- pd[pd$Chromosome == ch & !pd$sig, ]
|
|
|
|
| 997 |
"<br>PVE: ", round(PVE_Marker_Percent, 2), "%"),
|
| 998 |
hoverinfo = "text")
|
| 999 |
}
|
|
|
|
|
|
|
| 1000 |
sig_pd <- pd[pd$sig, ]
|
| 1001 |
if (nrow(sig_pd) > 0) {
|
| 1002 |
+
lod_min <- min(sig_pd$LOD, na.rm = TRUE)
|
| 1003 |
+
lod_max <- max(sig_pd$LOD, na.rm = TRUE)
|
|
|
|
| 1004 |
lod_norm <- if (lod_max > lod_min) (sig_pd$LOD - lod_min) / (lod_max - lod_min) else rep(0.5, nrow(sig_pd))
|
|
|
|
|
|
|
| 1005 |
sig_colors <- colorRampPalette(c("#8338EC","#3A86FF","#FFBE0B","#FB5607","#FF006E"))(100)
|
| 1006 |
pt_colors <- sig_colors[pmax(1, pmin(100, round(lod_norm * 99) + 1))]
|
|
|
|
| 1007 |
pl <- pl %>% add_trace(
|
| 1008 |
data = sig_pd, x = ~cum_pos, y = ~LOD, type = "scatter", mode = "markers",
|
| 1009 |
name = "Significant QTL",
|
| 1010 |
+
marker = list(size = 14, color = pt_colors, symbol = "diamond",
|
| 1011 |
+
line = list(width = 1.5, color = "white"), opacity = 0.95),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1012 |
text = ~paste0("<b>\u2605 ", Marker, " \u2605</b><br>Chr: ", Chromosome,
|
| 1013 |
"<br>Pos: ", round(Position, 1), " cM",
|
| 1014 |
"<br>LOD: ", round(LOD, 3),
|
|
|
|
| 1016 |
"<br>Add. Effect: ", round(Additive_Effect, 4),
|
| 1017 |
"<br>Dom. Class: ", Dominance_Class),
|
| 1018 |
hoverinfo = "text")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1019 |
}
|
| 1020 |
+
pl %>% add_segments(
|
| 1021 |
+
x = min(pd$cum_pos, na.rm=TRUE), xend = max(pd$cum_pos, na.rm=TRUE),
|
| 1022 |
+
y = lod_threshold, yend = lod_threshold,
|
| 1023 |
+
line = list(color = "#FF006E", dash = "dash", width = 2),
|
| 1024 |
+
showlegend = FALSE) %>%
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1025 |
layout(
|
| 1026 |
title = list(
|
| 1027 |
text = paste0("<b>", title, "</b> <span style='font-size:12px;color:#888'>",
|
| 1028 |
sum(pd$sig, na.rm = TRUE), " significant QTLs</span>"),
|
| 1029 |
font = list(size = 15, color = "#212529")),
|
| 1030 |
+
xaxis = list(title = "Chromosome",
|
| 1031 |
+
tickvals = as.list(chr_mids[chroms]),
|
| 1032 |
+
ticktext = as.list(paste0("Chr ", chroms)),
|
| 1033 |
+
tickangle = 45,
|
| 1034 |
+
gridcolor = "rgba(0,0,0,0.07)", color = "grey25", zeroline = FALSE),
|
| 1035 |
+
yaxis = list(title = "LOD Score",
|
| 1036 |
+
gridcolor = "rgba(0,0,0,0.07)", color = "grey25", zeroline = FALSE),
|
|
|
|
|
|
|
| 1037 |
paper_bgcolor = "white", plot_bgcolor = "white",
|
| 1038 |
font = list(color = "grey20"),
|
| 1039 |
+
legend = list(orientation = "h", y = -0.18, font = list(size = 10, color = "grey20")),
|
| 1040 |
+
hoverlabel = list(bgcolor = "white", font = list(color = "grey10", size = 11),
|
| 1041 |
+
bordercolor = "grey60"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1042 |
}
|
| 1043 |
|
| 1044 |
# ============================================================
|
| 1045 |
# COLORFUL CHROMOSOME IDIOGRAM
|
|
|
|
| 1046 |
# ============================================================
|
| 1047 |
make_idiogram_ggplot <- function(qtl_list, all_markers_df, lod_threshold = 2.0) {
|
| 1048 |
if (is.null(all_markers_df) || nrow(all_markers_df) == 0)
|
| 1049 |
return(ggplot() + labs(title = "No marker data") + theme_bw())
|
|
|
|
| 1050 |
all_markers_df$Chromosome <- as.character(all_markers_df$Chromosome)
|
| 1051 |
chroms <- sort(unique(all_markers_df$Chromosome))
|
| 1052 |
n_chr <- length(chroms)
|
| 1053 |
chr_idx <- setNames(seq_along(chroms), chroms)
|
|
|
|
| 1054 |
chr_summary <- do.call(rbind, lapply(chroms, function(ch) {
|
| 1055 |
mk <- all_markers_df[all_markers_df$Chromosome == ch, ]
|
| 1056 |
data.frame(Chromosome = ch, x = chr_idx[ch],
|
|
|
|
| 1058 |
ymax = max(mk$Position, na.rm = TRUE),
|
| 1059 |
stringsAsFactors = FALSE)
|
| 1060 |
}))
|
|
|
|
| 1061 |
max_pos <- max(all_markers_df$Position, na.rm = TRUE)
|
|
|
|
| 1062 |
p <- ggplot() +
|
|
|
|
| 1063 |
geom_rect(data = chr_summary,
|
| 1064 |
aes(xmin = x - 0.28, xmax = x + 0.28, ymin = ymin, ymax = ymax),
|
| 1065 |
fill = "#F0F4FF", color = "#ADB5BD", linewidth = 0.7) +
|
|
|
|
| 1066 |
geom_rect(data = chr_summary,
|
| 1067 |
aes(xmin = x - 0.28, xmax = x + 0.28,
|
| 1068 |
ymin = ymin + (ymax - ymin) * 0.38,
|
| 1069 |
ymax = ymin + (ymax - ymin) * 0.42),
|
| 1070 |
fill = "#6C757D", color = NA) +
|
|
|
|
| 1071 |
geom_text(data = chr_summary,
|
| 1072 |
aes(x = x, y = ymax + max_pos * 0.045,
|
| 1073 |
label = paste0("Chr\n", Chromosome)),
|
|
|
|
| 1087 |
plot.title = element_text(face = "bold", size = 14, color = "#212529"),
|
| 1088 |
axis.title.y = element_text(face = "bold", color = "grey20")
|
| 1089 |
)
|
| 1090 |
+
model_names <- names(qtl_list); n_models <- length(model_names)
|
|
|
|
|
|
|
|
|
|
| 1091 |
vivid_pal <- c("#FF006E","#3A86FF","#06D6A0","#FFBE0B","#FB5607","#8338EC",
|
| 1092 |
"#F72585","#4CC9F0","#2A9D8F","#E9C46A")
|
| 1093 |
model_colors_use <- setNames(
|
| 1094 |
+
rep(vivid_pal, ceiling(n_models / length(vivid_pal)))[seq_len(n_models)], model_names)
|
|
|
|
|
|
|
|
|
|
| 1095 |
for (m in intersect(model_names, names(MODEL_COLORS)))
|
| 1096 |
model_colors_use[m] <- MODEL_COLORS[m]
|
|
|
|
| 1097 |
for (ti in seq_along(model_names)) {
|
| 1098 |
+
tr <- model_names[ti]
|
| 1099 |
+
qdf <- qtl_list[[tr]]
|
| 1100 |
if (is.null(qdf) || "Message" %in% colnames(qdf)) next
|
| 1101 |
+
sig <- qdf[!is.na(qdf$LOD) & qdf$LOD >= lod_threshold, ]
|
| 1102 |
if (nrow(sig) == 0) next
|
| 1103 |
sig$Chromosome <- as.character(sig$Chromosome)
|
| 1104 |
+
sig <- sig[sig$Chromosome %in% chroms, ]
|
| 1105 |
if (nrow(sig) == 0) next
|
| 1106 |
sig$x_base <- chr_idx[sig$Chromosome]
|
|
|
|
| 1107 |
side <- ifelse(ti %% 2 == 1, 1, -1)
|
| 1108 |
x_off <- sig$x_base + side * (0.33 + (((ti - 1) %/% 2) * 0.13))
|
| 1109 |
lod_sz <- pmin(7, pmax(2.5, sig$LOD * 0.9))
|
| 1110 |
mc <- model_colors_use[tr]
|
| 1111 |
+
seg_df <- data.frame(x = sig$x_base + side * 0.28, xend = x_off,
|
| 1112 |
+
y = sig$Position, yend = sig$Position,
|
| 1113 |
+
model = tr, stringsAsFactors = FALSE)
|
| 1114 |
+
pt_df <- data.frame(x = x_off, y = sig$Position,
|
| 1115 |
+
model = tr, size_val = lod_sz, stringsAsFactors = FALSE)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1116 |
p <- p +
|
| 1117 |
geom_point(data = pt_df, aes(x = x, y = y),
|
| 1118 |
color = mc, size = lod_sz * 2.2, alpha = 0.12, shape = 16) +
|
| 1119 |
geom_segment(data = seg_df,
|
| 1120 |
aes(x = x, xend = xend, y = y, yend = yend, color = model),
|
| 1121 |
linewidth = 0.9, alpha = 0.85) +
|
| 1122 |
+
geom_point(data = pt_df, aes(x = x, y = y, color = model, size = size_val),
|
|
|
|
| 1123 |
shape = 18, alpha = 0.95) +
|
| 1124 |
scale_color_manual(values = model_colors_use, name = "Model") +
|
| 1125 |
scale_size_continuous(range = c(2.5, 7), guide = "none")
|
|
|
|
| 1202 |
.model-badge{display:inline-block;background:#e8f4fd;border:1px solid #90caf9;border-radius:20px;padding:3px 10px;font-size:11px;font-weight:600;color:#3A86FF;margin:2px;}
|
| 1203 |
.perm-info{background:linear-gradient(135deg,#e8f4fd,#f0f8ff);border:1px solid #90caf9;border-left:4px solid #8338EC;border-radius:8px;padding:12px 16px;margin:8px 0;font-size:13px;}
|
| 1204 |
.speed-badge{background:#8338EC;color:#fff;border-radius:12px;padding:2px 8px;font-size:10px;font-weight:700;letter-spacing:0.5px;}
|
| 1205 |
+
.fix-badge{background:#06D6A0;color:#fff;border-radius:12px;padding:2px 8px;font-size:10px;font-weight:700;letter-spacing:0.5px;}
|
| 1206 |
"))
|
| 1207 |
),
|
| 1208 |
|
| 1209 |
navbarPage(
|
| 1210 |
+
title = "QTL-Mapper v10",
|
| 1211 |
id = "nav",
|
| 1212 |
|
| 1213 |
# ── 1. DATA UPLOAD ─────────────────────────────────────
|
| 1214 |
tabPanel("📁 Data Upload", icon = icon("upload"),
|
| 1215 |
fluidRow(column(12,
|
| 1216 |
div(class = "info-box",
|
| 1217 |
+
tags$b("QTL-Mapper v10 — CIM Fixed + Flanking Markers in Excel + Per-Chromosome LOD Plots"),
|
| 1218 |
+
tags$br(),
|
| 1219 |
+
tags$span(class = "fix-badge", "CIM FIXED"),
|
| 1220 |
+
" CIM cofactor threshold lowered (0.1 LOD gain) + fallback to top SIM markers + cM-based window exclusion",
|
| 1221 |
tags$br(),
|
| 1222 |
+
tags$span(class = "fix-badge", "FLANKING"),
|
| 1223 |
+
" Left/Right flanking markers now in all Excel sheets",
|
| 1224 |
tags$br(),
|
| 1225 |
+
tags$span(class = "fix-badge", "CHR LOD PLOT"),
|
| 1226 |
+
" New per-chromosome LOD plots in QTL Cartographer style (see Results tab)"
|
| 1227 |
)
|
| 1228 |
)),
|
| 1229 |
fluidRow(
|
|
|
|
| 1256 |
numericInput("window_size", "Exclusion Window (±markers)", value = 10, min = 1, max = 30)
|
| 1257 |
),
|
| 1258 |
numericInput("lod_drop", "LOD Drop for Support Interval", value = 1.5, min = 0.5, max = 3.0, step = 0.5),
|
| 1259 |
+
numericInput("chr_lod_ncols", "Chr-LOD Plot Columns", value = 4, min = 2, max = 6),
|
| 1260 |
actionButton("run_btn", "▶ Run QTL Analysis",
|
| 1261 |
class = "btn btn-primary btn-block",
|
| 1262 |
style = "margin-top:10px;width:100%;"),
|
|
|
|
| 1265 |
conditionalPanel("input.run_permutation",
|
| 1266 |
div(class = "perm-info",
|
| 1267 |
tags$span(class = "speed-badge", "FAST"),
|
| 1268 |
+
tags$b(" Parallel Permutations"), tags$br(),
|
| 1269 |
+
tags$small("Auto-detects CPU cores. Uses SIM on 200 marker subsample. ~10× faster than v8."),
|
|
|
|
| 1270 |
tags$br(), tags$br(),
|
| 1271 |
numericInput("n_perm", "Number of Permutations", value = 100, min = 50, max = 1000, step = 50),
|
| 1272 |
numericInput("perm_alpha", "Alpha (significance)", value = 0.05, min = 0.01, max = 0.20, step = 0.01)
|
|
|
|
| 1311 |
tabPanel("📊 Results", icon = icon("chart-line"),
|
| 1312 |
fluidRow(column(12,
|
| 1313 |
div(style = "display:flex;gap:10px;flex-wrap:wrap;margin-bottom:14px;",
|
| 1314 |
+
downloadButton("dl_manhattan", "Manhattan Plot (PNG)", class = "btn btn-info"),
|
| 1315 |
+
downloadButton("dl_idiogram", "Chromosome Map (PNG)", class = "btn btn-info"),
|
| 1316 |
+
downloadButton("dl_chr_lod_plot", "Chr-by-Chr LOD Plot (PNG)", class = "btn btn-info"),
|
| 1317 |
+
downloadButton("dl_results", "Full Results (.xlsx)", class = "btn btn-success")
|
| 1318 |
)
|
| 1319 |
)),
|
| 1320 |
fluidRow(
|
|
|
|
| 1331 |
)
|
| 1332 |
)
|
| 1333 |
),
|
| 1334 |
+
# NEW: Per-chromosome LOD plot
|
| 1335 |
+
fluidRow(column(12,
|
| 1336 |
+
div(class = "card",
|
| 1337 |
+
div(class = "card-hd", "Per-Chromosome LOD Score Plot (QTL Cartographer Style)"),
|
| 1338 |
+
div(class = "card-bd",
|
| 1339 |
+
div(class = "info-box", style = "font-size:12px;margin-bottom:8px;",
|
| 1340 |
+
"Each panel: left = chromosome ideogram with marker positions | ",
|
| 1341 |
+
"right = LOD curve | red dashed = significance threshold | ",
|
| 1342 |
+
"diamonds = significant QTLs with auto-labels (qXXX)"),
|
| 1343 |
+
uiOutput("chr_lod_plot_ui")
|
| 1344 |
+
)
|
| 1345 |
+
)
|
| 1346 |
+
)),
|
| 1347 |
fluidRow(
|
| 1348 |
column(4,
|
| 1349 |
div(class = "card",
|
|
|
|
| 1384 |
div(class = "card",
|
| 1385 |
div(class = "card-hd", "Multi-Model Comparison"),
|
| 1386 |
div(class = "card-bd",
|
| 1387 |
+
div(class = "info-box", "Select methods, click Run. Each runs a full genome scan independently."),
|
|
|
|
| 1388 |
checkboxGroupInput("compare_methods", "Methods to Compare:",
|
| 1389 |
choices = setNames(names(MAPPING_MODELS), sapply(MAPPING_MODELS, `[[`, "label")),
|
| 1390 |
selected = c("SIM", "CIM", "LASSO"), inline = TRUE),
|
|
|
|
| 1506 |
fluidRow(column(10, offset = 1,
|
| 1507 |
div(class = "card",
|
| 1508 |
div(class = "card-bd",
|
| 1509 |
+
tags$h4(style = "color:#FF006E;", "QTL-Mapper v10 — QTL Cartographer-Compatible Platform"),
|
| 1510 |
div(class = "info-box",
|
| 1511 |
+
tags$b("v10 Fixes & Improvements:"), tags$br(),
|
| 1512 |
+
tags$span(class = "fix-badge", "CIM FIXED"),
|
| 1513 |
+
" Cofactor threshold lowered from 0.5 → 0.1 LOD gain (was too strict)", tags$br(),
|
| 1514 |
+
tags$span(class = "fix-badge", "CIM FIXED"),
|
| 1515 |
+
" Fallback: if stepwise finds no cofactors, uses top-K SIM LOD markers", tags$br(),
|
| 1516 |
+
tags$span(class = "fix-badge", "CIM FIXED"),
|
| 1517 |
+
" Window exclusion now uses cM distance (not marker count)", tags$br(),
|
| 1518 |
+
tags$span(class = "fix-badge", "FLANKING"),
|
| 1519 |
+
" Left_Flanking_Marker + Right_Flanking_Marker in ALL Excel sheets", tags$br(),
|
| 1520 |
+
tags$span(class = "fix-badge", "NEW PLOT"),
|
| 1521 |
+
" Per-chromosome LOD plots (QTL Cartographer style) with idiogram + auto QTL labels"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1522 |
),
|
| 1523 |
tags$h5(style = "margin-top:18px;font-weight:600;color:#3A86FF;", "References"),
|
| 1524 |
tags$ul(
|
|
|
|
| 1531 |
tags$hr(),
|
| 1532 |
tags$p(style = "text-align:center;color:#6c757d;font-size:12px;",
|
| 1533 |
"Contact: ashmithamganesan@gmail.com | P.sinha@cgiar.org",
|
| 1534 |
+
tags$br(), "QTL-Mapper v10 © 2024")
|
| 1535 |
)
|
| 1536 |
)
|
| 1537 |
))
|
|
|
|
| 1591 |
if (!is.null(rv$perm_threshold))
|
| 1592 |
div(class = "perm-info", style = "margin-top:8px;",
|
| 1593 |
tags$span(class = "speed-badge", "FAST"),
|
| 1594 |
+
tags$b(" Permutation LOD threshold: "), round(rv$perm_threshold, 3))
|
|
|
|
| 1595 |
)
|
| 1596 |
})
|
| 1597 |
|
|
|
|
| 1658 |
X_v <- X[-tr_idx, , drop = FALSE]; y_v <- y[-tr_idx]
|
| 1659 |
|
| 1660 |
withProgress(message = paste("Running", input$mapping_method, "..."), value = 0.1, {
|
|
|
|
|
|
|
| 1661 |
if (isTRUE(input$run_permutation)) {
|
| 1662 |
incProgress(0, detail = "Running parallel permutations (fast)...")
|
| 1663 |
n_cores_avail <- max(1, parallel::detectCores(logical = TRUE) - 1)
|
| 1664 |
showNotification(
|
| 1665 |
paste0("Permutation test: ", input$n_perm, " perms on ",
|
| 1666 |
+
min(n_cores_avail, 8), " cores"),
|
| 1667 |
type = "message", duration = 4)
|
| 1668 |
rv$perm_threshold <- permutation_threshold(
|
| 1669 |
X_tr, y_tr, rv$marker_info,
|
| 1670 |
+
method = input$mapping_method, n_perm = input$n_perm,
|
| 1671 |
+
alpha = input$perm_alpha, n_cofactors = input$n_cofactors,
|
|
|
|
|
|
|
| 1672 |
window_size = input$window_size)
|
| 1673 |
+
updateSliderInput(session, "lod_threshold", value = round(rv$perm_threshold, 2))
|
|
|
|
|
|
|
| 1674 |
}
|
| 1675 |
incProgress(0.15)
|
| 1676 |
|
|
|
|
| 1685 |
Xtr_rf <- if (length(nzv_i) > 0) X_tr[, -nzv_i, drop = FALSE] else X_tr
|
| 1686 |
Xv_rf <- X_v[, colnames(Xtr_rf), drop = FALSE]
|
| 1687 |
rv$model_cols <- colnames(Xtr_rf)
|
|
|
|
| 1688 |
fit <- tryCatch(
|
| 1689 |
caret::train(x = Xtr_rf, y = y_tr, method = "rf",
|
| 1690 |
trControl = caret::trainControl(method = "cv", number = 5), tuneLength = 3),
|
|
|
|
| 1692 |
caret::train(x = Xtr_rf, y = y_tr, method = "lm",
|
| 1693 |
trControl = caret::trainControl(method = "cv", number = 5)))
|
| 1694 |
rv$model_fit <- fit
|
| 1695 |
+
ytr_p <- predict(fit, Xtr_rf); yv_p <- predict(fit, Xv_rf)
|
| 1696 |
+
tr_r2 <- max(0, 1 - sum((y_tr - ytr_p)^2) / sum((y_tr - mean(y_tr))^2))
|
| 1697 |
+
v_r2 <- max(0, 1 - sum((y_v - yv_p)^2) / sum((y_v - mean(y_v))^2))
|
|
|
|
|
|
|
| 1698 |
rv$perf <- data.frame(
|
| 1699 |
Metric = c("R² Train", "R Train", "R² Validation", "R Validation"),
|
| 1700 |
Value = c(tr_r2, cor(y_tr, ytr_p), v_r2, cor(y_v, yv_p)))
|
|
|
|
| 1714 |
})
|
| 1715 |
})
|
| 1716 |
|
| 1717 |
+
# ---- PER-CHROMOSOME LOD PLOT (reactive) ----
|
| 1718 |
+
chr_lod_plot_reactive <- reactive({
|
| 1719 |
+
req(rv$scan_all, rv$marker_info)
|
| 1720 |
+
if ("Message" %in% colnames(rv$scan_all)) return(NULL)
|
| 1721 |
+
make_chr_lod_plot(
|
| 1722 |
+
scan_df = rv$scan_all,
|
| 1723 |
+
marker_info = rv$marker_info,
|
| 1724 |
+
lod_threshold = lod_thr(),
|
| 1725 |
+
trait_name = isolate(input$pheno_col),
|
| 1726 |
+
method = isolate(input$mapping_method),
|
| 1727 |
+
n_cols = input$chr_lod_ncols
|
| 1728 |
+
)
|
| 1729 |
+
})
|
| 1730 |
+
|
| 1731 |
+
output$chr_lod_plot_ui <- renderUI({
|
| 1732 |
+
req(rv$scan_all, rv$marker_info)
|
| 1733 |
+
if ("Message" %in% colnames(rv$scan_all))
|
| 1734 |
+
return(div(class = "warn-box", "Run analysis first"))
|
| 1735 |
+
n_chr <- length(unique(as.character(rv$marker_info$Chromosome)))
|
| 1736 |
+
n_cols <- max(2, min(input$chr_lod_ncols, 6))
|
| 1737 |
+
n_rows <- ceiling(n_chr / n_cols)
|
| 1738 |
+
ht <- max(300, n_rows * 200)
|
| 1739 |
+
plotOutput("chr_lod_plot_output", height = paste0(ht, "px"))
|
| 1740 |
+
})
|
| 1741 |
+
|
| 1742 |
+
output$chr_lod_plot_output <- renderPlot({
|
| 1743 |
+
p <- chr_lod_plot_reactive()
|
| 1744 |
+
req(!is.null(p))
|
| 1745 |
+
grid::grid.draw(p)
|
| 1746 |
+
}, bg = "white")
|
| 1747 |
+
|
| 1748 |
# ---- COMPARE MODELS ----
|
| 1749 |
output$compare_status_ui <- renderUI({
|
| 1750 |
if (length(rv$compare_res) == 0) return(NULL)
|
| 1751 |
div(class = "good-box",
|
| 1752 |
+
tags$b("Completed: "), paste(names(rv$compare_res), collapse = ", "), tags$br(),
|
|
|
|
| 1753 |
lapply(names(rv$compare_res), function(m) {
|
| 1754 |
r <- rv$compare_res[[m]]
|
| 1755 |
n <- if (!is.null(r) && !"Message" %in% colnames(r))
|
|
|
|
| 1808 |
df <- do.call(rbind, rows)
|
| 1809 |
df$Chromosome <- factor(df$Chromosome, levels = sort(unique(df$Chromosome)))
|
| 1810 |
n_m <- length(unique(df$Method))
|
|
|
|
| 1811 |
vivid <- c("#FF006E","#3A86FF","#06D6A0","#FFBE0B","#FB5607","#8338EC")
|
| 1812 |
mcols <- vivid[seq_len(n_m)]
|
| 1813 |
plot_ly(df, x = ~Chromosome, y = ~Count, color = ~Method, type = "bar",
|
|
|
|
| 1831 |
if (is.null(r) || "Message" %in% colnames(r)) next
|
| 1832 |
sig <- r[!is.na(r$LOD) & r$LOD >= lod_thr(), ]
|
| 1833 |
if (nrow(sig) == 0) next
|
| 1834 |
+
sig <- add_support_intervals(sig, r, input$lod_drop)
|
| 1835 |
sig$Model_Used <- mth
|
| 1836 |
+
keep_cols <- intersect(c("Marker","Chromosome","Position","LOD",
|
| 1837 |
+
"PVE_Marker_Percent","Additive_Effect",
|
| 1838 |
+
"Left_Flanking_Marker","Right_Flanking_Marker","Model_Used"),
|
| 1839 |
+
colnames(sig))
|
| 1840 |
+
rows <- c(rows, list(sig[, keep_cols, drop = FALSE]))
|
| 1841 |
}
|
| 1842 |
if (length(rows) == 0) return(datatable(data.frame(Status = "No QTLs found")))
|
| 1843 |
df <- do.call(rbind, rows)
|
|
|
|
| 1861 |
n_all <- nrow(cq)
|
| 1862 |
n_max <- sum(cq$N_Models_Detected == length(rv$compare_res), na.rm = TRUE)
|
| 1863 |
div(class = "good-box",
|
| 1864 |
+
tags$b(n_all), " common QTLs | ", tags$b(n_max), " in ALL models", tags$br(),
|
|
|
|
| 1865 |
tags$small("Chromosomes: ", paste(sort(unique(cq$Chromosome)), collapse = ", ")))
|
| 1866 |
})
|
| 1867 |
|
|
|
|
| 1870 |
if (is.null(cq) || nrow(cq) == 0)
|
| 1871 |
return(datatable(data.frame(Status = "Run Compare Models tab first"),
|
| 1872 |
options = list(dom = "t")))
|
| 1873 |
+
# Add flanking markers
|
| 1874 |
+
cq <- add_flanking_to_df(cq, rv$marker_info)
|
| 1875 |
num_cols <- intersect(
|
| 1876 |
c("Position","Mean_LOD","Max_LOD","Mean_PVE","Mean_Add_Eff",
|
| 1877 |
paste0("LOD_", names(rv$compare_res))),
|
|
|
|
| 1892 |
output$common_idiogram <- renderPlot({
|
| 1893 |
cq <- common_qtl_data()
|
| 1894 |
req(!is.null(cq), nrow(cq) > 0, rv$marker_info)
|
| 1895 |
+
n_levels <- sort(unique(cq$N_Models_Detected), decreasing = TRUE)
|
| 1896 |
qtl_list_cq <- setNames(lapply(n_levels, function(nl) {
|
| 1897 |
sub <- cq[cq$N_Models_Detected == nl,
|
| 1898 |
c("Marker","Chromosome","Position","Max_LOD","Mean_PVE","Mean_Add_Eff")]
|
|
|
|
| 1939 |
req(rv$scan_all, rv$marker_info)
|
| 1940 |
valid <- if (!is.null(rv$scan_all) && !"Message" %in% colnames(rv$scan_all))
|
| 1941 |
list(setNames(list(rv$scan_all), input$mapping_method)) else list()
|
| 1942 |
+
if (length(valid) == 0) ggplot() + labs(title = "Run analysis first") + theme_bw()
|
| 1943 |
+
else make_idiogram_ggplot(valid[[1]], rv$marker_info, lod_thr())
|
|
|
|
|
|
|
|
|
|
| 1944 |
}, bg = "white")
|
| 1945 |
|
| 1946 |
output$perf_plot <- renderPlotly({
|
|
|
|
| 2021 |
req(rv$scan_all)
|
| 2022 |
if ("Message" %in% colnames(rv$scan_all))
|
| 2023 |
return(datatable(data.frame(Status = rv$scan_all$Message[1]), options = list(dom = "t")))
|
| 2024 |
+
# Add flanking markers to full scan table
|
| 2025 |
+
fs <- add_flanking_to_df(rv$scan_all, rv$marker_info)
|
| 2026 |
+
datatable(fs,
|
| 2027 |
options = list(scrollX = TRUE, pageLength = 15, dom = "Blfrtip",
|
| 2028 |
order = list(list(4, "desc"))),
|
| 2029 |
rownames = FALSE, class = "cell-border stripe hover") %>%
|
| 2030 |
+
formatRound(intersect(c("LOD","PVE_Marker_Percent","Additive_Effect"), colnames(fs)), 3) %>%
|
| 2031 |
+
formatSignif(intersect("P_Value", colnames(fs)), digits = 3)
|
|
|
|
| 2032 |
})
|
| 2033 |
|
| 2034 |
# ---- PREDICT TEST ----
|
| 2035 |
observeEvent(input$pred_btn, {
|
| 2036 |
+
if (is.null(rv$model_fit)) { showNotification("Run analysis first", type = "error"); return() }
|
| 2037 |
if (is.null(rv$test_geno)) { showNotification("Upload test genotype first", type = "error"); return() }
|
| 2038 |
withProgress(message = "Predicting test samples...", value = 0.3, {
|
| 2039 |
train_cols <- rv$model_cols
|
|
|
|
| 2069 |
}, error = function(e) showNotification(paste("Domestication error:", e$message), type = "error"))
|
| 2070 |
})
|
| 2071 |
|
|
|
|
| 2072 |
make_dom_plotly <- function(data, y_col, title, threshold, y_lab = y_col) {
|
| 2073 |
+
blank <- plot_ly() %>% layout(title = title, paper_bgcolor = "white", plot_bgcolor = "white")
|
|
|
|
| 2074 |
if (is.null(data) || nrow(data) == 0) return(blank)
|
| 2075 |
pd <- data[!is.na(data[[y_col]]) & !is.na(data$Chromosome), ]
|
| 2076 |
if (nrow(pd) == 0) return(blank)
|
|
|
|
| 2079 |
offsets <- setNames(numeric(length(chroms)), chroms)
|
| 2080 |
for (i in seq_along(chroms)) {
|
| 2081 |
if (i == 1) { offsets[chroms[1]] <- 0; next }
|
| 2082 |
+
offsets[chroms[i]] <- offsets[chroms[i-1]] +
|
| 2083 |
+
max(pd$Position[pd$Chromosome == chroms[i-1]], na.rm = TRUE) * 1.05
|
| 2084 |
}
|
| 2085 |
pd$cum_pos <- pd$Position + offsets[pd$Chromosome]
|
| 2086 |
chr_mids <- tapply(pd$cum_pos, pd$Chromosome, median, na.rm = TRUE)
|
|
|
|
| 2094 |
type = "scatter", mode = "markers", name = paste("Chr", ch), showlegend = FALSE,
|
| 2095 |
marker = list(size = 6, color = chr_clrs[ch], opacity = 0.75),
|
| 2096 |
text = ~paste0("<b>", Marker, "</b><br>Chr: ", Chromosome,
|
| 2097 |
+
"<br>Pos: ", round(Position, 1), "<br>", y_lab, ": ", round(get(y_col), 4)),
|
|
|
|
| 2098 |
hoverinfo = "text")
|
| 2099 |
}
|
| 2100 |
if (!is.null(threshold))
|
| 2101 |
pl <- pl %>% add_segments(
|
| 2102 |
+
x = min(pd$cum_pos, na.rm=TRUE), xend = max(pd$cum_pos, na.rm=TRUE),
|
| 2103 |
y = threshold, yend = threshold,
|
| 2104 |
line = list(color = "#FF006E", dash = "dash", width = 1.8), showlegend = FALSE)
|
| 2105 |
pl %>% layout(
|
|
|
|
| 2122 |
formatRound(c("Fst","Pi_Dom","Pi_Wild","Tajima_D_Dom"), 4)
|
| 2123 |
})
|
| 2124 |
|
| 2125 |
+
# ============================================================
|
| 2126 |
+
# DOWNLOADS — ALL INCLUDE FLANKING MARKERS
|
| 2127 |
+
# ============================================================
|
| 2128 |
output$dl_manhattan <- downloadHandler(
|
| 2129 |
filename = function() paste0("manhattan_", Sys.Date(), ".png"),
|
| 2130 |
content = function(file) {
|
| 2131 |
req(rv$scan_all)
|
| 2132 |
+
# Use ggplot2 version for PNG
|
| 2133 |
+
pd <- rv$scan_all[!is.na(rv$scan_all$LOD) & !is.na(rv$scan_all$Position), ]
|
| 2134 |
+
pd$Chromosome <- as.character(pd$Chromosome)
|
| 2135 |
+
chroms <- sort(unique(pd$Chromosome), method = "radix")
|
| 2136 |
+
offsets <- setNames(numeric(length(chroms)), chroms)
|
| 2137 |
+
for (i in seq_along(chroms)) {
|
| 2138 |
+
if (i == 1) { offsets[chroms[1]] <- 0; next }
|
| 2139 |
+
prev <- chroms[i - 1]
|
| 2140 |
+
offsets[chroms[i]] <- offsets[prev] + max(pd$Position[pd$Chromosome == prev], na.rm=TRUE) * 1.05
|
| 2141 |
+
}
|
| 2142 |
+
pd$cum_pos <- pd$Position + offsets[pd$Chromosome]
|
| 2143 |
+
pd$sig <- pd$LOD >= lod_thr()
|
| 2144 |
+
chr_mids <- tapply(pd$cum_pos, pd$Chromosome, median, na.rm = TRUE)
|
| 2145 |
+
chr_colors <- setNames(rep(c("#3A86FF","#FB5607"), length.out = length(chroms)), chroms)
|
| 2146 |
+
p <- ggplot(pd[!pd$sig, ], aes(x = cum_pos, y = LOD)) +
|
| 2147 |
+
geom_point(aes(color = Chromosome), size = 1.6, alpha = 0.6, show.legend = FALSE) +
|
| 2148 |
+
scale_color_manual(values = chr_colors) +
|
| 2149 |
+
{if (sum(pd$sig) > 0) list(
|
| 2150 |
+
geom_point(data = pd[pd$sig,], aes(x=cum_pos,y=LOD),
|
| 2151 |
+
color="#FFBE0B", size=7, alpha=0.35, shape=16),
|
| 2152 |
+
geom_point(data = pd[pd$sig,], aes(x=cum_pos,y=LOD,fill=LOD),
|
| 2153 |
+
size=5, shape=23, color="white", stroke=0.8),
|
| 2154 |
+
scale_fill_gradientn(colors=c("#FB5607","#FFBE0B","#FF006E","#8338EC"),
|
| 2155 |
+
name="LOD Score")
|
| 2156 |
+
)} +
|
| 2157 |
+
geom_hline(yintercept = lod_thr(), linetype = "dashed",
|
| 2158 |
+
color = "#FF006E", linewidth = 1.0) +
|
| 2159 |
+
scale_x_continuous(breaks = chr_mids[chroms],
|
| 2160 |
+
labels = paste0("Chr ", chroms)) +
|
| 2161 |
+
labs(title = paste(input$mapping_method, "Manhattan Plot"),
|
| 2162 |
+
x = "Chromosome", y = "LOD Score") +
|
| 2163 |
+
theme_bw(base_size = 13) +
|
| 2164 |
+
theme(axis.text.x = element_text(angle = 45, hjust = 1),
|
| 2165 |
+
panel.grid.minor = element_blank(),
|
| 2166 |
+
panel.grid.major.x = element_blank(),
|
| 2167 |
+
plot.background = element_rect(fill = "white", color = NA))
|
| 2168 |
ggsave(file, plot = p, width = 14, height = 6, dpi = 200, bg = "white")
|
| 2169 |
}
|
| 2170 |
)
|
|
|
|
| 2183 |
}
|
| 2184 |
)
|
| 2185 |
|
| 2186 |
+
output$dl_chr_lod_plot <- downloadHandler(
|
| 2187 |
+
filename = function() paste0("chr_lod_plot_", Sys.Date(), ".png"),
|
| 2188 |
+
content = function(file) {
|
| 2189 |
+
req(rv$scan_all, rv$marker_info)
|
| 2190 |
+
if ("Message" %in% colnames(rv$scan_all)) return()
|
| 2191 |
+
p <- make_chr_lod_plot(
|
| 2192 |
+
scan_df = rv$scan_all,
|
| 2193 |
+
marker_info = rv$marker_info,
|
| 2194 |
+
lod_threshold = lod_thr(),
|
| 2195 |
+
trait_name = isolate(input$pheno_col),
|
| 2196 |
+
method = isolate(input$mapping_method),
|
| 2197 |
+
n_cols = isolate(input$chr_lod_ncols))
|
| 2198 |
+
n_chr <- length(unique(as.character(rv$marker_info$Chromosome)))
|
| 2199 |
+
n_cols <- max(2, min(isolate(input$chr_lod_ncols), 6))
|
| 2200 |
+
n_rows <- ceiling(n_chr / n_cols)
|
| 2201 |
+
ggplot2::ggsave(file, plot = grid::grid.draw(p),
|
| 2202 |
+
width = n_cols * 3.5, height = n_rows * 3,
|
| 2203 |
+
dpi = 200, bg = "white",
|
| 2204 |
+
device = "png")
|
| 2205 |
+
# Alternative: use png + grid
|
| 2206 |
+
png(file, width = n_cols * 3.5 * 200, height = n_rows * 3 * 200, res = 200)
|
| 2207 |
+
grid::grid.draw(p)
|
| 2208 |
+
dev.off()
|
| 2209 |
+
}
|
| 2210 |
+
)
|
| 2211 |
+
|
| 2212 |
output$dl_results <- downloadHandler(
|
| 2213 |
+
filename = function() paste0("qtl_results_v10_", Sys.Date(), ".xlsx"),
|
| 2214 |
content = function(file) {
|
| 2215 |
sheets <- list()
|
| 2216 |
+
|
| 2217 |
+
# ---- Significant QTLs with full flanking marker info ----
|
| 2218 |
if (!is.null(rv$scan_all) && !"Message" %in% colnames(rv$scan_all)) {
|
| 2219 |
sig <- rv$scan_all[!is.na(rv$scan_all$LOD) & rv$scan_all$LOD >= lod_thr(), ]
|
| 2220 |
if (nrow(sig) > 0) {
|
| 2221 |
sig <- add_support_intervals(sig, rv$scan_all, isolate(input$lod_drop))
|
| 2222 |
+
# Ensure flanking markers are present
|
| 2223 |
+
if (!"Left_Flanking_Marker" %in% colnames(sig) ||
|
| 2224 |
+
all(is.na(sig$Left_Flanking_Marker)))
|
| 2225 |
+
sig <- add_flanking_to_df(sig, rv$marker_info)
|
| 2226 |
sheets[["QTL_Significant"]] <- sig[order(-sig$LOD), ]
|
| 2227 |
}
|
| 2228 |
+
# Full scan with flanking markers
|
| 2229 |
+
full_fl <- add_flanking_to_df(rv$scan_all, rv$marker_info)
|
| 2230 |
+
sheets[["Full_Scan"]] <- full_fl
|
| 2231 |
}
|
| 2232 |
+
|
| 2233 |
if (!is.null(rv$perf)) sheets[["Performance"]] <- rv$perf
|
| 2234 |
+
|
| 2235 |
+
# ---- Per-model results with flanking markers ----
|
| 2236 |
for (m in names(rv$compare_res)) {
|
| 2237 |
r <- rv$compare_res[[m]]
|
| 2238 |
if (!is.null(r) && !"Message" %in% colnames(r)) {
|
| 2239 |
sig_m <- r[!is.na(r$LOD) & r$LOD >= lod_thr(), ]
|
| 2240 |
if (nrow(sig_m) > 0) {
|
| 2241 |
sig_m <- add_support_intervals(sig_m, r, isolate(input$lod_drop))
|
| 2242 |
+
if (!"Left_Flanking_Marker" %in% colnames(sig_m) ||
|
| 2243 |
+
all(is.na(sig_m$Left_Flanking_Marker)))
|
| 2244 |
+
sig_m <- add_flanking_to_df(sig_m, rv$marker_info)
|
| 2245 |
sheets[[paste0("Model_", m)]] <- sig_m
|
| 2246 |
}
|
| 2247 |
+
# Full scan per model with flanking
|
| 2248 |
+
full_m <- add_flanking_to_df(r, rv$marker_info)
|
| 2249 |
+
sheets[[paste0("FullScan_", m)]] <- full_m
|
| 2250 |
}
|
| 2251 |
}
|
| 2252 |
+
|
| 2253 |
+
# ---- Common QTL with flanking markers ----
|
| 2254 |
cq <- tryCatch(find_common_qtl(rv$compare_res, lod_thr(), isolate(input$min_models)),
|
| 2255 |
error = function(e) NULL)
|
| 2256 |
+
if (!is.null(cq) && nrow(cq) > 0) {
|
| 2257 |
+
cq <- add_flanking_to_df(cq, rv$marker_info)
|
| 2258 |
+
sheets[["Common_QTL"]] <- cq
|
| 2259 |
+
}
|
| 2260 |
+
|
| 2261 |
sheets[["Methods"]] <- data.frame(
|
| 2262 |
Method = names(MAPPING_MODELS),
|
| 2263 |
Label = sapply(MAPPING_MODELS, `[[`, "label"),
|
| 2264 |
Description = sapply(MAPPING_MODELS, `[[`, "desc"))
|
| 2265 |
+
|
| 2266 |
if (!is.null(rv$sel_metrics) && nrow(rv$sel_metrics) > 0)
|
| 2267 |
sheets[["Selection_Signatures"]] <- rv$sel_metrics
|
| 2268 |
+
|
| 2269 |
if (length(sheets) == 0) sheets[["Empty"]] <- data.frame(msg = "Run analysis first")
|
| 2270 |
writexl::write_xlsx(sheets, file)
|
| 2271 |
}
|
|
|
|
| 2284 |
content = function(file) {
|
| 2285 |
cq <- tryCatch(find_common_qtl(rv$compare_res, lod_thr(), isolate(input$min_models)),
|
| 2286 |
error = function(e) NULL)
|
| 2287 |
+
if (!is.null(cq) && nrow(cq) > 0) {
|
| 2288 |
+
cq <- add_flanking_to_df(cq, rv$marker_info)
|
| 2289 |
writexl::write_xlsx(list(Common_QTL = cq), file)
|
| 2290 |
+
} else {
|
| 2291 |
writexl::write_xlsx(
|
| 2292 |
list(Empty = data.frame(msg = "No common QTLs or run Compare Models first")), file)
|
| 2293 |
+
}
|
| 2294 |
}
|
| 2295 |
)
|
| 2296 |
|