Spaces:
Sleeping
Sleeping
Update app.R
Browse files
app.R
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
# ============================================================================
|
| 2 |
-
#
|
| 3 |
-
# 10 Models +
|
| 4 |
# ============================================================================
|
| 5 |
|
| 6 |
-
# Load libraries
|
| 7 |
suppressPackageStartupMessages({
|
| 8 |
library(shiny)
|
| 9 |
library(shinydashboard)
|
|
@@ -21,119 +21,119 @@ suppressPackageStartupMessages({
|
|
| 21 |
library(viridis)
|
| 22 |
library(ggridges)
|
| 23 |
library(pheatmap)
|
| 24 |
-
})
|
| 25 |
-
|
| 26 |
-
# Check for corrplot
|
| 27 |
-
if (!require("corrplot", quietly = TRUE)) {
|
| 28 |
-
install.packages("corrplot", repos = "https://cloud.r-project.org/")
|
| 29 |
library(corrplot)
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# ============================================================================
|
| 33 |
-
# SAFE NOTIFICATION FUNCTION
|
| 34 |
# ============================================================================
|
| 35 |
|
| 36 |
-
|
| 37 |
tryCatch({
|
| 38 |
-
if (type == "default")
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
} else if (type == "error") {
|
| 45 |
-
showNotification(msg, type = "error", duration = 8)
|
| 46 |
-
} else {
|
| 47 |
-
showNotification(msg, duration = 3)
|
| 48 |
-
}
|
| 49 |
-
}, error = function(e) {
|
| 50 |
-
message(paste("Notification:", msg))
|
| 51 |
-
})
|
| 52 |
}
|
| 53 |
|
| 54 |
# ============================================================================
|
| 55 |
-
# DATA PARSER
|
| 56 |
# ============================================================================
|
| 57 |
|
| 58 |
-
|
| 59 |
|
|
|
|
| 60 |
geno_raw <- read.csv(geno_path, header = FALSE, stringsAsFactors = FALSE,
|
| 61 |
-
check.names = FALSE, na.strings = c("NA", "", "N/A", "-9", "0"))
|
| 62 |
|
| 63 |
if (nrow(geno_raw) < 4) {
|
| 64 |
-
stop("Genotype file must have at least 4 rows")
|
| 65 |
}
|
| 66 |
|
|
|
|
| 67 |
col_names <- as.character(geno_raw[1, ])
|
| 68 |
chr_row <- as.character(geno_raw[2, ])
|
| 69 |
pos_row <- as.character(geno_raw[3, ])
|
| 70 |
ind_data <- geno_raw[4:nrow(geno_raw), , drop = FALSE]
|
| 71 |
|
|
|
|
| 72 |
marker_cols <- 2:ncol(geno_raw)
|
|
|
|
|
|
|
| 73 |
marker_names <- col_names[marker_cols]
|
| 74 |
|
|
|
|
| 75 |
chromosomes <- chr_row[marker_cols]
|
| 76 |
chromosomes <- gsub("^[Cc]hr", "", chromosomes)
|
|
|
|
| 77 |
chromosomes[chromosomes == ""] <- "unknown"
|
|
|
|
| 78 |
|
|
|
|
| 79 |
positions <- suppressWarnings(as.numeric(pos_row[marker_cols]))
|
| 80 |
positions[is.na(positions)] <- 0
|
| 81 |
|
|
|
|
| 82 |
ind_ids <- as.character(ind_data[, 1])
|
| 83 |
if (any(duplicated(ind_ids))) ind_ids <- make.unique(ind_ids)
|
| 84 |
|
|
|
|
| 85 |
geno_mat <- as.matrix(ind_data[, marker_cols, drop = FALSE])
|
| 86 |
mode(geno_mat) <- "numeric"
|
| 87 |
rownames(geno_mat) <- ind_ids
|
| 88 |
colnames(geno_mat) <- marker_names
|
| 89 |
|
| 90 |
-
#
|
| 91 |
-
|
| 92 |
-
valid_inds <-
|
| 93 |
-
if (sum(valid_inds) == 0)
|
| 94 |
-
stop("No valid individuals found")
|
| 95 |
-
}
|
| 96 |
geno_mat <- geno_mat[valid_inds, , drop = FALSE]
|
| 97 |
ind_ids <- ind_ids[valid_inds]
|
| 98 |
|
| 99 |
-
# Read
|
| 100 |
pheno_raw <- read.csv(pheno_path, header = TRUE, stringsAsFactors = FALSE,
|
| 101 |
-
check.names = FALSE, na.strings = c("NA", "", "N/A"))
|
| 102 |
|
| 103 |
-
if (ncol(pheno_raw) < 2)
|
| 104 |
-
stop("Phenotype file must have at least 2 columns")
|
| 105 |
-
}
|
| 106 |
|
| 107 |
pheno_ids <- as.character(pheno_raw[, 1])
|
| 108 |
if (any(duplicated(pheno_ids))) pheno_ids <- make.unique(pheno_ids)
|
| 109 |
pheno_df <- pheno_raw[, -1, drop = FALSE]
|
| 110 |
|
|
|
|
| 111 |
for (i in 1:ncol(pheno_df)) {
|
| 112 |
pheno_df[, i] <- suppressWarnings(as.numeric(pheno_df[, i]))
|
| 113 |
}
|
| 114 |
rownames(pheno_df) <- pheno_ids
|
| 115 |
|
| 116 |
-
# Align
|
| 117 |
common_ids <- intersect(ind_ids, pheno_ids)
|
| 118 |
if (length(common_ids) == 0) {
|
| 119 |
-
stop("No matching IDs
|
|
|
|
| 120 |
}
|
| 121 |
|
| 122 |
geno_mat <- geno_mat[common_ids, , drop = FALSE]
|
| 123 |
pheno_df <- pheno_df[common_ids, , drop = FALSE]
|
| 124 |
|
| 125 |
-
#
|
| 126 |
-
|
| 127 |
-
valid_markers <-
|
| 128 |
-
if (sum(valid_markers) == 0)
|
| 129 |
-
stop("No valid markers found")
|
| 130 |
-
}
|
| 131 |
geno_mat <- geno_mat[, valid_markers, drop = FALSE]
|
| 132 |
chromosomes <- chromosomes[valid_markers]
|
| 133 |
positions <- positions[valid_markers]
|
| 134 |
marker_names <- marker_names[valid_markers]
|
| 135 |
|
| 136 |
-
# Build map
|
| 137 |
unique_chrs <- unique(chromosomes)
|
| 138 |
chr_numeric <- suppressWarnings(as.numeric(unique_chrs))
|
| 139 |
chr_numeric[is.na(chr_numeric)] <- 999
|
|
@@ -149,9 +149,12 @@ parse_custom_cross <- function(geno_path, pheno_path, cross_type = "f2") {
|
|
| 149 |
chr_positions <- positions[chr_idx]
|
| 150 |
names(chr_positions) <- chr_markers
|
| 151 |
|
|
|
|
| 152 |
ord <- order(chr_positions)
|
| 153 |
chr_positions <- chr_positions[ord]
|
| 154 |
chr_markers <- chr_markers[ord]
|
|
|
|
|
|
|
| 155 |
chr_positions <- chr_positions[!duplicated(chr_positions)]
|
| 156 |
chr_markers <- chr_markers[!duplicated(chr_markers)]
|
| 157 |
|
|
@@ -166,83 +169,152 @@ parse_custom_cross <- function(geno_path, pheno_path, cross_type = "f2") {
|
|
| 166 |
}
|
| 167 |
}
|
| 168 |
|
| 169 |
-
if (length(geno_list) == 0)
|
| 170 |
-
stop("No valid chromosomes found")
|
| 171 |
-
}
|
| 172 |
|
|
|
|
| 173 |
cross <- list(geno = geno_list, pheno = pheno_df)
|
| 174 |
class(cross) <- c(cross_type, "cross")
|
|
|
|
|
|
|
| 175 |
cross <- jittermap(cross)
|
| 176 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
return(cross)
|
| 178 |
}
|
| 179 |
|
| 180 |
# ============================================================================
|
| 181 |
-
# 10
|
| 182 |
# ============================================================================
|
| 183 |
|
| 184 |
-
|
| 185 |
models <- list()
|
|
|
|
| 186 |
|
| 187 |
# Model 1: Haley-Knott Regression
|
|
|
|
| 188 |
tryCatch({
|
| 189 |
models$hk <- scanone(cross, pheno.col = pheno_col, method = "hk")
|
| 190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
|
| 192 |
# Model 2: EM Algorithm
|
|
|
|
| 193 |
tryCatch({
|
| 194 |
models$em <- scanone(cross, pheno.col = pheno_col, method = "em")
|
| 195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
# Model 3: Extended Haley-Knott
|
|
|
|
| 198 |
tryCatch({
|
| 199 |
models$ehk <- scanone(cross, pheno.col = pheno_col, method = "ehk")
|
| 200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
# Model 4: Multiple Imputation
|
|
|
|
| 203 |
tryCatch({
|
| 204 |
cross_sim <- sim.geno(cross, n.draws = 32, step = 1)
|
| 205 |
models$imp <- scanone(cross_sim, pheno.col = pheno_col, method = "imp")
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
# Model 5: Marker Regression
|
|
|
|
| 209 |
tryCatch({
|
| 210 |
models$mr <- scanone(cross, pheno.col = pheno_col, method = "mr")
|
| 211 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
|
| 213 |
# Model 6: Non-parametric
|
|
|
|
| 214 |
tryCatch({
|
| 215 |
models$np <- scanone(cross, pheno.col = pheno_col, method = "np")
|
| 216 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
|
| 218 |
# Model 7: Composite Interval Mapping
|
|
|
|
| 219 |
tryCatch({
|
| 220 |
models$cim <- cim(cross, pheno.col = pheno_col, method = "hk", window = 10)
|
| 221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
# Model 8: Multiple QTL Mapping
|
|
|
|
| 224 |
tryCatch({
|
| 225 |
scan_hk <- scanone(cross, pheno.col = pheno_col, method = "hk")
|
| 226 |
-
cofactors <- which(scan_hk$lod > 2)
|
| 227 |
if (length(cofactors) > 0) {
|
| 228 |
models$mqm <- mqm(cross, pheno.col = pheno_col, cofactors = cofactors)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
}
|
| 230 |
-
}, error = function(e)
|
|
|
|
|
|
|
|
|
|
| 231 |
|
| 232 |
-
# Model 9: Binary Trait
|
|
|
|
| 233 |
tryCatch({
|
| 234 |
models$binary <- scanone(cross, pheno.col = pheno_col, method = "binary")
|
| 235 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
|
| 237 |
# Model 10: Normal Model
|
|
|
|
| 238 |
tryCatch({
|
| 239 |
models$normal <- scanone(cross, pheno.col = pheno_col, method = "normal")
|
| 240 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
|
|
|
|
| 242 |
return(models)
|
| 243 |
}
|
| 244 |
|
| 245 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
if (is.null(scan) || nrow(scan) == 0) {
|
| 247 |
return(data.frame())
|
| 248 |
}
|
|
@@ -253,15 +325,17 @@ find_qtls <- function(scan, threshold) {
|
|
| 253 |
}
|
| 254 |
|
| 255 |
qtls <- data.frame()
|
|
|
|
| 256 |
for (chr in unique(scan_sig$chr)) {
|
| 257 |
chr_data <- scan_sig[scan_sig$chr == chr, ]
|
|
|
|
| 258 |
|
| 259 |
-
# Find peaks
|
| 260 |
peaks <- c()
|
| 261 |
for (i in 1:nrow(chr_data)) {
|
| 262 |
is_peak <- TRUE
|
| 263 |
-
if (i > 1 && chr_data$lod[i] <= chr_data$lod[i
|
| 264 |
-
if (i < nrow(chr_data) && chr_data$lod[i] <= chr_data$lod[i
|
| 265 |
if (is_peak) peaks <- c(peaks, i)
|
| 266 |
}
|
| 267 |
|
|
@@ -269,31 +343,87 @@ find_qtls <- function(scan, threshold) {
|
|
| 269 |
for (peak_idx in peaks) {
|
| 270 |
peak_row <- chr_data[peak_idx, ]
|
| 271 |
|
| 272 |
-
#
|
| 273 |
-
|
|
|
|
|
|
|
| 274 |
lod_diff <- chr_all$lod - peak_row$lod
|
| 275 |
ci_indices <- which(lod_diff >= -1.5)
|
|
|
|
| 276 |
if (length(ci_indices) > 0) {
|
| 277 |
-
|
| 278 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
} else {
|
| 280 |
left_pos <- peak_row$pos - 5
|
| 281 |
right_pos <- peak_row$pos + 5
|
|
|
|
|
|
|
| 282 |
}
|
| 283 |
|
| 284 |
-
# Calculate
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
n <- nrow(scan)
|
| 286 |
pve <- (1 - 10^(-2 * peak_row$lod / n)) * 100
|
| 287 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 288 |
qtls <- rbind(qtls, data.frame(
|
|
|
|
| 289 |
Chromosome = as.character(chr),
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
stringsAsFactors = FALSE
|
| 298 |
))
|
| 299 |
}
|
|
@@ -301,230 +431,358 @@ find_qtls <- function(scan, threshold) {
|
|
| 301 |
}
|
| 302 |
|
| 303 |
if (nrow(qtls) > 0) {
|
|
|
|
| 304 |
qtls$ChrNum <- suppressWarnings(as.numeric(qtls$Chromosome))
|
| 305 |
qtls$ChrNum[is.na(qtls$ChrNum)] <- 999
|
| 306 |
-
qtls <- qtls[order(qtls$ChrNum, qtls$
|
| 307 |
qtls$ChrNum <- NULL
|
|
|
|
| 308 |
}
|
| 309 |
|
| 310 |
return(qtls)
|
| 311 |
}
|
| 312 |
|
| 313 |
# ============================================================================
|
| 314 |
-
#
|
| 315 |
# ============================================================================
|
| 316 |
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
|
| 321 |
-
combined <- data.frame()
|
| 322 |
for (name in names(models)) {
|
| 323 |
scan <- models[[name]]
|
| 324 |
if (!is.null(scan)) {
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 328 |
}
|
| 329 |
}
|
| 330 |
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
ggplot(combined, aes(x = pos, y = lod, color = model)) +
|
| 334 |
-
geom_line(size = 0.6, alpha = 0.7) +
|
| 335 |
-
geom_hline(yintercept = threshold, color = "red", linetype = "dashed", size = 1) +
|
| 336 |
-
facet_wrap(~chr, scales = "free_x", ncol = 3) +
|
| 337 |
-
scale_color_brewer(palette = "Set1") +
|
| 338 |
-
labs(title = "Multi-Model QTL Comparison", x = "Position (cM)", y = "LOD Score") +
|
| 339 |
-
theme_minimal() +
|
| 340 |
-
theme(legend.position = "bottom", legend.text = element_text(size = 8))
|
| 341 |
}
|
| 342 |
|
| 343 |
-
#
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
map <- pull.map(cross)
|
| 348 |
-
df <- data.frame()
|
| 349 |
-
for (chr in names(map)) {
|
| 350 |
-
positions <- map[[chr]]
|
| 351 |
-
df <- rbind(df, data.frame(Chromosome = chr, Position = positions))
|
| 352 |
-
}
|
| 353 |
-
|
| 354 |
-
df$Chromosome <- factor(df$Chromosome, levels = names(map))
|
| 355 |
-
|
| 356 |
-
ggplot(df, aes(x = Chromosome, y = Position)) +
|
| 357 |
-
geom_segment(aes(xend = Chromosome, yend = 0), color = "gray70", size = 1) +
|
| 358 |
-
geom_point(color = "steelblue", size = 2, alpha = 0.7) +
|
| 359 |
-
labs(title = "Genetic Map", x = "Chromosome", y = "Position (cM)") +
|
| 360 |
-
theme_minimal() +
|
| 361 |
-
theme(axis.text.x = element_text(angle = 45, hjust = 1))
|
| 362 |
-
}
|
| 363 |
|
| 364 |
-
# Plot
|
| 365 |
-
|
| 366 |
if (is.null(scan)) return(NULL)
|
| 367 |
|
| 368 |
df <- as.data.frame(scan)
|
| 369 |
-
df$
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 370 |
|
| 371 |
-
|
| 372 |
-
geom_density_ridges_gradient(scale = 2, rel_min_height = 0.01) +
|
| 373 |
-
scale_fill_viridis_c(name = "LOD") +
|
| 374 |
-
labs(title = "LOD Distribution by Chromosome", x = "LOD", y = "Chromosome") +
|
| 375 |
-
theme_minimal()
|
| 376 |
}
|
| 377 |
|
| 378 |
-
# Plot
|
| 379 |
-
|
| 380 |
-
if (is.null(
|
| 381 |
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
scale_size_continuous(range = c(3, 15), name = "LOD") +
|
| 385 |
-
scale_color_gradientn(colors = c("blue", "green", "red"), name = "PVE %") +
|
| 386 |
-
labs(title = "QTL Effect Sizes", x = "Chromosome", y = "Position (cM)") +
|
| 387 |
-
theme_minimal() +
|
| 388 |
-
theme(axis.text.x = element_text(angle = 45, hjust = 1))
|
| 389 |
-
}
|
| 390 |
-
|
| 391 |
-
# Plot 5: QQ Plot
|
| 392 |
-
plot_qq <- function(models) {
|
| 393 |
-
if (is.null(models) || length(models) == 0) return(NULL)
|
| 394 |
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
expected <- -log10(ppoints(length(scan$lod)))
|
| 400 |
-
observed <- -log10(rank(scan$lod) / (length(scan$lod) + 1))
|
| 401 |
-
qq_data <- rbind(qq_data, data.frame(
|
| 402 |
-
Model = toupper(name), Expected = expected, Observed = observed
|
| 403 |
-
))
|
| 404 |
-
}
|
| 405 |
}
|
| 406 |
|
| 407 |
-
ggplot(
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
|
|
|
|
|
|
|
|
|
| 412 |
}
|
| 413 |
|
| 414 |
-
# Plot
|
| 415 |
-
|
| 416 |
-
if (is.null(
|
| 417 |
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 421 |
}
|
| 422 |
|
| 423 |
-
# Plot
|
| 424 |
-
|
| 425 |
-
if (is.null(cross) || is.null(
|
| 426 |
|
| 427 |
-
chr_name <- as.character(
|
| 428 |
if (!chr_name %in% names(cross$geno)) return(NULL)
|
| 429 |
|
|
|
|
| 430 |
map <- pull.map(cross)[[chr_name]]
|
| 431 |
-
marker_pos <- which.min(abs(map -
|
| 432 |
marker_name <- names(map)[marker_pos]
|
| 433 |
|
|
|
|
| 434 |
geno_idx <- find.marker(cross, chr_name, marker_name)
|
| 435 |
genotypes <- pull.geno(cross)[, geno_idx]
|
| 436 |
phenotypes <- cross$pheno[[trait]]
|
| 437 |
|
| 438 |
-
df <- data.frame(
|
|
|
|
|
|
|
|
|
|
| 439 |
df <- df[!is.na(df$Genotype) & !is.na(df$Phenotype), ]
|
| 440 |
|
| 441 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 442 |
|
| 443 |
ggplot(df, aes(x = Genotype, y = Phenotype, fill = Genotype)) +
|
| 444 |
geom_violin(alpha = 0.6, trim = FALSE) +
|
| 445 |
geom_boxplot(width = 0.2, alpha = 0.8) +
|
| 446 |
-
stat_summary(fun = mean, geom = "point", shape = 18, size =
|
| 447 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 448 |
theme_minimal() +
|
| 449 |
-
theme(legend.position = "none"
|
|
|
|
| 450 |
}
|
| 451 |
|
| 452 |
-
# Plot
|
| 453 |
-
|
| 454 |
-
if (is.null(
|
| 455 |
|
| 456 |
-
|
| 457 |
-
|
| 458 |
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 471 |
|
| 472 |
-
|
|
|
|
| 473 |
|
| 474 |
-
|
| 475 |
-
colnames(perf_matrix) <- names(chr_lods)
|
| 476 |
|
| 477 |
-
|
| 478 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 479 |
}
|
| 480 |
|
| 481 |
-
# Plot 9:
|
| 482 |
-
|
| 483 |
-
if (is.null(
|
| 484 |
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 495 |
}
|
| 496 |
}
|
| 497 |
|
| 498 |
-
if (nrow(
|
| 499 |
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 505 |
|
| 506 |
-
|
| 507 |
-
|
|
|
|
|
|
|
| 508 |
|
| 509 |
-
|
| 510 |
-
geom_line(size = 1) + geom_point(size = 3) + coord_polar() +
|
| 511 |
-
labs(title = "Model Comparison") + theme_minimal()
|
| 512 |
}
|
| 513 |
|
| 514 |
-
# Plot
|
| 515 |
-
|
| 516 |
if (is.null(models) || length(models) == 0) return(NULL)
|
| 517 |
|
| 518 |
first_model <- models[[1]]
|
| 519 |
if (is.null(first_model)) return(NULL)
|
| 520 |
|
| 521 |
-
|
|
|
|
| 522 |
|
| 523 |
-
parallel_data <- data.frame(Position = first_model$pos[
|
| 524 |
for (name in names(models)) {
|
| 525 |
scan <- models[[name]]
|
| 526 |
if (!is.null(scan)) {
|
| 527 |
-
parallel_data[[toupper(name)]] <- scan$lod[
|
| 528 |
}
|
| 529 |
}
|
| 530 |
|
|
@@ -532,45 +790,69 @@ plot_parallel <- function(models) {
|
|
| 532 |
names_to = "Model", values_to = "LOD")
|
| 533 |
|
| 534 |
ggplot(plot_data, aes(x = Position, y = LOD, color = Model, group = Model)) +
|
| 535 |
-
geom_line(size = 1) +
|
| 536 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 537 |
}
|
| 538 |
|
| 539 |
-
# Plot
|
| 540 |
-
|
| 541 |
-
if (is.null(
|
| 542 |
|
| 543 |
-
|
| 544 |
-
df$chr_num <- as.numeric(factor(df$chr, levels = unique(df$chr)))
|
| 545 |
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
|
|
|
| 553 |
}
|
| 554 |
|
| 555 |
-
# Plot
|
| 556 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 557 |
if (is.null(scan)) return(NULL)
|
| 558 |
|
|
|
|
| 559 |
chrs <- unique(scan$chr)
|
| 560 |
-
|
| 561 |
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 566 |
}
|
| 567 |
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
scale_y_continuous(breaks = 1:length(chrs), labels = chrs) +
|
| 572 |
-
labs(title = "LOD Contour Plot", x = "Position", y = "Chromosome") +
|
| 573 |
-
theme_minimal()
|
| 574 |
}
|
| 575 |
|
| 576 |
# ============================================================================
|
|
@@ -578,49 +860,69 @@ plot_contour <- function(scan) {
|
|
| 578 |
# ============================================================================
|
| 579 |
|
| 580 |
ui <- dashboardPage(
|
| 581 |
-
dashboardHeader(title = "QTL Analysis -
|
| 582 |
|
| 583 |
-
dashboardSidebar(width =
|
| 584 |
sidebarMenu(
|
| 585 |
menuItem("Data Input", tabName = "data", icon = icon("upload")),
|
| 586 |
menuItem("10 Models", tabName = "models", icon = icon("cogs")),
|
| 587 |
-
menuItem("
|
| 588 |
-
menuItem("
|
| 589 |
-
menuItem("
|
|
|
|
| 590 |
),
|
| 591 |
|
| 592 |
div(style = "padding: 15px;",
|
| 593 |
-
h4("Upload
|
| 594 |
fileInput("geno_file", "Genotype CSV", accept = ".csv"),
|
| 595 |
fileInput("pheno_file", "Phenotype CSV", accept = ".csv"),
|
| 596 |
|
| 597 |
selectInput("cross_type", "Cross Type",
|
| 598 |
-
choices = c("F2" = "f2",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 599 |
|
| 600 |
hr(),
|
| 601 |
|
| 602 |
-
h4("
|
| 603 |
-
selectInput("pheno_trait", "Phenotype", choices = NULL),
|
| 604 |
-
numericInput("lod_threshold", "LOD Threshold", value = 3.0, min = 0, max = 20, step = 0.5),
|
| 605 |
-
numericInput("n_permutations", "Permutations", value = 100, min = 0, max = 1000),
|
| 606 |
|
| 607 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 608 |
icon = icon("play"),
|
| 609 |
-
style = "background-color: #3c8dbc; color: white; width: 100%;"),
|
| 610 |
|
| 611 |
br(), br(),
|
| 612 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 613 |
)
|
| 614 |
),
|
| 615 |
|
| 616 |
dashboardBody(
|
| 617 |
useShinyjs(),
|
| 618 |
-
tags$head(
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 622 |
|
| 623 |
tabItems(
|
|
|
|
| 624 |
tabItem(tabName = "data",
|
| 625 |
fluidRow(
|
| 626 |
valueBoxOutput("n_individuals"),
|
|
@@ -628,91 +930,221 @@ ui <- dashboardPage(
|
|
| 628 |
valueBoxOutput("n_chromosomes")
|
| 629 |
),
|
| 630 |
fluidRow(
|
| 631 |
-
box(title = "Data Summary", status = "primary", width = 12,
|
| 632 |
-
verbatimTextOutput("data_summary")
|
|
|
|
| 633 |
),
|
| 634 |
fluidRow(
|
| 635 |
box(title = "Genetic Map", status = "info", width = 12,
|
| 636 |
-
plotOutput("genetic_map", height = "500px") %>% withSpinner()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 637 |
)
|
| 638 |
),
|
| 639 |
|
|
|
|
| 640 |
tabItem(tabName = "models",
|
| 641 |
fluidRow(
|
| 642 |
-
box(title = "
|
| 643 |
-
verbatimTextOutput("analysis_status")
|
|
|
|
| 644 |
),
|
| 645 |
fluidRow(
|
| 646 |
-
box(title = "
|
| 647 |
-
|
|
|
|
| 648 |
),
|
| 649 |
fluidRow(
|
| 650 |
-
box(title = "Model
|
| 651 |
-
plotOutput("
|
|
|
|
| 652 |
),
|
| 653 |
fluidRow(
|
| 654 |
-
box(title = "
|
| 655 |
-
plotOutput("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 656 |
)
|
| 657 |
),
|
| 658 |
|
| 659 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 660 |
fluidRow(
|
| 661 |
-
box(title = "
|
| 662 |
-
|
|
|
|
| 663 |
),
|
| 664 |
fluidRow(
|
| 665 |
-
box(title = "
|
| 666 |
-
plotOutput("
|
| 667 |
-
|
| 668 |
-
plotOutput("contour_plot", height = "500px") %>% withSpinner())
|
| 669 |
),
|
| 670 |
fluidRow(
|
| 671 |
-
box(title = "
|
| 672 |
-
plotOutput("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 673 |
),
|
| 674 |
fluidRow(
|
| 675 |
-
box(title = "
|
| 676 |
-
|
|
|
|
| 677 |
),
|
| 678 |
fluidRow(
|
| 679 |
-
box(title = "
|
| 680 |
-
plotOutput("
|
|
|
|
|
|
|
|
|
|
|
|
|
| 681 |
),
|
| 682 |
fluidRow(
|
| 683 |
-
box(title = "
|
| 684 |
-
plotOutput("
|
| 685 |
-
|
| 686 |
-
|
| 687 |
-
|
| 688 |
-
|
| 689 |
-
|
| 690 |
-
|
| 691 |
-
|
| 692 |
fluidRow(
|
| 693 |
-
box(title = "
|
| 694 |
-
|
|
|
|
|
|
|
| 695 |
),
|
| 696 |
fluidRow(
|
| 697 |
-
box(title = "
|
| 698 |
-
|
|
|
|
| 699 |
),
|
| 700 |
fluidRow(
|
| 701 |
-
box(title = "
|
| 702 |
-
|
|
|
|
| 703 |
)
|
| 704 |
),
|
| 705 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 706 |
tabItem(tabName = "help",
|
| 707 |
fluidRow(
|
| 708 |
-
box(title = "Documentation", status = "primary", width = 12,
|
| 709 |
HTML("
|
| 710 |
-
<h3>
|
| 711 |
-
|
| 712 |
-
<
|
| 713 |
-
<
|
| 714 |
-
|
| 715 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 716 |
)
|
| 717 |
)
|
| 718 |
)
|
|
@@ -725,11 +1157,14 @@ ui <- dashboardPage(
|
|
| 725 |
|
| 726 |
server <- function(input, output, session) {
|
| 727 |
|
|
|
|
| 728 |
cross_data <- reactiveVal(NULL)
|
| 729 |
all_models <- reactiveVal(NULL)
|
| 730 |
qtl_results <- reactiveVal(NULL)
|
| 731 |
-
|
| 732 |
-
|
|
|
|
|
|
|
| 733 |
|
| 734 |
# Value boxes
|
| 735 |
output$n_individuals <- renderValueBox({
|
|
@@ -750,169 +1185,407 @@ server <- function(input, output, session) {
|
|
| 750 |
icon = icon("chart-line"), color = "yellow")
|
| 751 |
})
|
| 752 |
|
| 753 |
-
# Load data
|
| 754 |
observeEvent(c(input$geno_file, input$pheno_file), {
|
| 755 |
req(input$geno_file, input$pheno_file)
|
| 756 |
|
| 757 |
-
|
| 758 |
|
| 759 |
tryCatch({
|
| 760 |
-
cross <-
|
| 761 |
input$geno_file$datapath,
|
| 762 |
input$pheno_file$datapath,
|
| 763 |
input$cross_type
|
| 764 |
)
|
| 765 |
|
| 766 |
cross_data(cross)
|
|
|
|
| 767 |
updateSelectInput(session, "pheno_trait",
|
| 768 |
choices = names(cross$pheno),
|
| 769 |
selected = names(cross$pheno)[1])
|
| 770 |
|
| 771 |
-
|
| 772 |
-
|
| 773 |
|
| 774 |
}, error = function(e) {
|
| 775 |
-
|
| 776 |
})
|
| 777 |
})
|
| 778 |
|
| 779 |
-
# Run
|
| 780 |
observeEvent(input$run_analysis, {
|
| 781 |
cross <- cross_data()
|
| 782 |
req(cross, input$pheno_trait)
|
| 783 |
|
| 784 |
-
|
| 785 |
|
| 786 |
-
withProgress(message = "Running 10 QTL Models", value = 0, {
|
| 787 |
|
| 788 |
-
incProgress(0.
|
| 789 |
pheno_idx <- which(names(cross$pheno) == input$pheno_trait)
|
| 790 |
-
|
|
|
|
|
|
|
| 791 |
all_models(models)
|
| 792 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 793 |
if (!is.null(models$hk)) {
|
| 794 |
-
|
| 795 |
-
|
| 796 |
qtl_results(qtls)
|
| 797 |
|
| 798 |
if (nrow(qtls) > 0) {
|
| 799 |
-
qtl_choices <- paste0("Chr", qtls$Chromosome, "_",
|
| 800 |
-
|
|
|
|
| 801 |
}
|
| 802 |
}
|
| 803 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 804 |
if (input$n_permutations > 0 && !is.null(models$hk)) {
|
| 805 |
-
incProgress(0.
|
| 806 |
-
perms <-
|
| 807 |
-
|
| 808 |
-
perm_results(perms)
|
| 809 |
}
|
| 810 |
|
| 811 |
-
|
|
|
|
| 812 |
n_success <- sum(!sapply(models, is.null))
|
| 813 |
-
|
| 814 |
})
|
| 815 |
})
|
| 816 |
|
| 817 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 818 |
output$data_summary <- renderPrint({
|
| 819 |
cross <- cross_data()
|
| 820 |
if (is.null(cross)) {
|
| 821 |
-
cat("No data loaded.")
|
| 822 |
} else {
|
|
|
|
| 823 |
cat("Cross Type:", class(cross)[1], "\n")
|
| 824 |
cat("Individuals:", nind(cross), "\n")
|
| 825 |
cat("Markers:", totmar(cross), "\n")
|
| 826 |
-
cat("Chromosomes:", nchr(cross), "\n")
|
| 827 |
-
cat("Phenotypes:", paste(names(cross$pheno), collapse = ", "))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 828 |
}
|
| 829 |
})
|
| 830 |
|
| 831 |
output$analysis_status <- renderPrint({
|
| 832 |
-
if (!
|
| 833 |
-
cat("Click 'RUN 10 MODELS' to start.")
|
| 834 |
} else {
|
| 835 |
models <- all_models()
|
| 836 |
n_success <- sum(!sapply(models, is.null))
|
| 837 |
-
|
| 838 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 839 |
}
|
| 840 |
})
|
| 841 |
|
| 842 |
-
#
|
| 843 |
-
output$
|
| 844 |
-
|
| 845 |
-
|
| 846 |
-
|
| 847 |
-
|
| 848 |
-
|
| 849 |
-
|
| 850 |
-
output$bubble_plot <- renderPlot({ plot_bubbles(qtl_results()) })
|
| 851 |
-
output$parallel_plot <- renderPlot({ plot_parallel(all_models()) })
|
| 852 |
-
output$qq_plot <- renderPlot({ plot_qq(all_models()) })
|
| 853 |
-
output$correlation_plot <- renderPlot({ plot_correlation(cross_data()) })
|
| 854 |
|
| 855 |
-
|
| 856 |
-
|
| 857 |
qtls <- qtl_results()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 858 |
cross <- cross_data()
|
| 859 |
-
req(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 860 |
|
| 861 |
-
|
| 862 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 863 |
|
| 864 |
-
|
| 865 |
})
|
| 866 |
|
| 867 |
-
|
| 868 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 869 |
qtls <- qtl_results()
|
| 870 |
req(qtls, nrow(qtls) > 0)
|
| 871 |
-
|
| 872 |
})
|
| 873 |
|
| 874 |
-
output$
|
| 875 |
models <- all_models()
|
| 876 |
-
req(models)
|
| 877 |
-
|
| 878 |
-
summary_df <- data.frame()
|
| 879 |
-
for (name in names(models)) {
|
| 880 |
-
scan <- models[[name]]
|
| 881 |
-
if (!is.null(scan)) {
|
| 882 |
-
summary_df <- rbind(summary_df, data.frame(
|
| 883 |
-
Model = toupper(name),
|
| 884 |
-
Max_LOD = round(max(scan$lod, na.rm = TRUE), 3),
|
| 885 |
-
Mean_LOD = round(mean(scan$lod, na.rm = TRUE), 3),
|
| 886 |
-
N_Peaks = sum(scan$lod > input$lod_threshold, na.rm = TRUE)
|
| 887 |
-
))
|
| 888 |
-
}
|
| 889 |
-
}
|
| 890 |
-
datatable(summary_df, options = list(pageLength = 10), rownames = FALSE)
|
| 891 |
})
|
| 892 |
|
| 893 |
-
output$
|
| 894 |
-
|
| 895 |
-
|
|
|
|
|
|
|
| 896 |
|
| 897 |
-
|
| 898 |
-
|
| 899 |
-
|
| 900 |
-
|
| 901 |
-
|
| 902 |
-
|
| 903 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 904 |
})
|
| 905 |
|
| 906 |
-
|
| 907 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 908 |
filename = function() paste0("qtl_results_", Sys.Date(), ".csv"),
|
| 909 |
content = function(file) {
|
| 910 |
qtls <- qtl_results()
|
| 911 |
if (!is.null(qtls) && nrow(qtls) > 0) {
|
| 912 |
write.csv(qtls, file, row.names = FALSE)
|
| 913 |
} else {
|
| 914 |
-
write.csv(data.frame(Message = "No QTLs found
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 915 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 916 |
}
|
| 917 |
)
|
| 918 |
}
|
|
|
|
| 1 |
# ============================================================================
|
| 2 |
+
# COMPLETE QTL ANALYSIS PIPELINE - FULLY FUNCTIONAL
|
| 3 |
+
# 10 Models + 15+ Plots + Working Downloads + Complete Results
|
| 4 |
# ============================================================================
|
| 5 |
|
| 6 |
+
# Load all required libraries
|
| 7 |
suppressPackageStartupMessages({
|
| 8 |
library(shiny)
|
| 9 |
library(shinydashboard)
|
|
|
|
| 21 |
library(viridis)
|
| 22 |
library(ggridges)
|
| 23 |
library(pheatmap)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
library(corrplot)
|
| 25 |
+
library(heatmaply)
|
| 26 |
+
library(UpSetR)
|
| 27 |
+
library(ggcorrplot)
|
| 28 |
+
library(igraph)
|
| 29 |
+
library(circlize)
|
| 30 |
+
library(ComplexHeatmap)
|
| 31 |
+
})
|
| 32 |
|
| 33 |
# ============================================================================
|
| 34 |
+
# SAFE NOTIFICATION FUNCTION
|
| 35 |
# ============================================================================
|
| 36 |
|
| 37 |
+
safe_notify <- function(msg, type = "default") {
|
| 38 |
tryCatch({
|
| 39 |
+
if (type == "default") showNotification(msg, duration = 3)
|
| 40 |
+
else if (type == "message") showNotification(msg, type = "message", duration = 3)
|
| 41 |
+
else if (type == "warning") showNotification(msg, type = "warning", duration = 5)
|
| 42 |
+
else if (type == "error") showNotification(msg, type = "error", duration = 8)
|
| 43 |
+
else showNotification(msg, duration = 3)
|
| 44 |
+
}, error = function(e) message(msg))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
}
|
| 46 |
|
| 47 |
# ============================================================================
|
| 48 |
+
# ADVANCED DATA PARSER
|
| 49 |
# ============================================================================
|
| 50 |
|
| 51 |
+
parse_advanced_cross <- function(geno_path, pheno_path, cross_type = "f2") {
|
| 52 |
|
| 53 |
+
# Read genotype file
|
| 54 |
geno_raw <- read.csv(geno_path, header = FALSE, stringsAsFactors = FALSE,
|
| 55 |
+
check.names = FALSE, na.strings = c("NA", "", "N/A", "-9", "0", "."))
|
| 56 |
|
| 57 |
if (nrow(geno_raw) < 4) {
|
| 58 |
+
stop("Genotype file must have at least 4 rows: header, chr, positions, data")
|
| 59 |
}
|
| 60 |
|
| 61 |
+
# Parse header rows
|
| 62 |
col_names <- as.character(geno_raw[1, ])
|
| 63 |
chr_row <- as.character(geno_raw[2, ])
|
| 64 |
pos_row <- as.character(geno_raw[3, ])
|
| 65 |
ind_data <- geno_raw[4:nrow(geno_raw), , drop = FALSE]
|
| 66 |
|
| 67 |
+
# Identify marker columns
|
| 68 |
marker_cols <- 2:ncol(geno_raw)
|
| 69 |
+
if (length(marker_cols) == 0) stop("No marker columns found")
|
| 70 |
+
|
| 71 |
marker_names <- col_names[marker_cols]
|
| 72 |
|
| 73 |
+
# Clean chromosome names
|
| 74 |
chromosomes <- chr_row[marker_cols]
|
| 75 |
chromosomes <- gsub("^[Cc]hr", "", chromosomes)
|
| 76 |
+
chromosomes <- gsub("^0+", "", chromosomes)
|
| 77 |
chromosomes[chromosomes == ""] <- "unknown"
|
| 78 |
+
chromosomes[is.na(chromosomes)] <- "unknown"
|
| 79 |
|
| 80 |
+
# Parse positions
|
| 81 |
positions <- suppressWarnings(as.numeric(pos_row[marker_cols]))
|
| 82 |
positions[is.na(positions)] <- 0
|
| 83 |
|
| 84 |
+
# Individual IDs
|
| 85 |
ind_ids <- as.character(ind_data[, 1])
|
| 86 |
if (any(duplicated(ind_ids))) ind_ids <- make.unique(ind_ids)
|
| 87 |
|
| 88 |
+
# Convert genotype matrix
|
| 89 |
geno_mat <- as.matrix(ind_data[, marker_cols, drop = FALSE])
|
| 90 |
mode(geno_mat) <- "numeric"
|
| 91 |
rownames(geno_mat) <- ind_ids
|
| 92 |
colnames(geno_mat) <- marker_names
|
| 93 |
|
| 94 |
+
# Remove individuals with excessive missing data (>50%)
|
| 95 |
+
missing_rate_ind <- rowMeans(is.na(geno_mat))
|
| 96 |
+
valid_inds <- missing_rate_ind <= 0.5
|
| 97 |
+
if (sum(valid_inds) == 0) stop("No individuals with <50% missing data")
|
|
|
|
|
|
|
| 98 |
geno_mat <- geno_mat[valid_inds, , drop = FALSE]
|
| 99 |
ind_ids <- ind_ids[valid_inds]
|
| 100 |
|
| 101 |
+
# Read phenotype file
|
| 102 |
pheno_raw <- read.csv(pheno_path, header = TRUE, stringsAsFactors = FALSE,
|
| 103 |
+
check.names = FALSE, na.strings = c("NA", "", "N/A", "."))
|
| 104 |
|
| 105 |
+
if (ncol(pheno_raw) < 2) stop("Phenotype file must have at least 2 columns")
|
|
|
|
|
|
|
| 106 |
|
| 107 |
pheno_ids <- as.character(pheno_raw[, 1])
|
| 108 |
if (any(duplicated(pheno_ids))) pheno_ids <- make.unique(pheno_ids)
|
| 109 |
pheno_df <- pheno_raw[, -1, drop = FALSE]
|
| 110 |
|
| 111 |
+
# Convert phenotypes to numeric
|
| 112 |
for (i in 1:ncol(pheno_df)) {
|
| 113 |
pheno_df[, i] <- suppressWarnings(as.numeric(pheno_df[, i]))
|
| 114 |
}
|
| 115 |
rownames(pheno_df) <- pheno_ids
|
| 116 |
|
| 117 |
+
# Align individuals
|
| 118 |
common_ids <- intersect(ind_ids, pheno_ids)
|
| 119 |
if (length(common_ids) == 0) {
|
| 120 |
+
stop(paste("No matching IDs. Genotype:", paste(head(ind_ids, 3), collapse=","),
|
| 121 |
+
"Phenotype:", paste(head(pheno_ids, 3), collapse=",")))
|
| 122 |
}
|
| 123 |
|
| 124 |
geno_mat <- geno_mat[common_ids, , drop = FALSE]
|
| 125 |
pheno_df <- pheno_df[common_ids, , drop = FALSE]
|
| 126 |
|
| 127 |
+
# Remove markers with excessive missing data (>50%)
|
| 128 |
+
missing_rate_marker <- colMeans(is.na(geno_mat))
|
| 129 |
+
valid_markers <- missing_rate_marker <= 0.5
|
| 130 |
+
if (sum(valid_markers) == 0) stop("No markers with <50% missing data")
|
|
|
|
|
|
|
| 131 |
geno_mat <- geno_mat[, valid_markers, drop = FALSE]
|
| 132 |
chromosomes <- chromosomes[valid_markers]
|
| 133 |
positions <- positions[valid_markers]
|
| 134 |
marker_names <- marker_names[valid_markers]
|
| 135 |
|
| 136 |
+
# Build genetic map by chromosome
|
| 137 |
unique_chrs <- unique(chromosomes)
|
| 138 |
chr_numeric <- suppressWarnings(as.numeric(unique_chrs))
|
| 139 |
chr_numeric[is.na(chr_numeric)] <- 999
|
|
|
|
| 149 |
chr_positions <- positions[chr_idx]
|
| 150 |
names(chr_positions) <- chr_markers
|
| 151 |
|
| 152 |
+
# Sort by position
|
| 153 |
ord <- order(chr_positions)
|
| 154 |
chr_positions <- chr_positions[ord]
|
| 155 |
chr_markers <- chr_markers[ord]
|
| 156 |
+
|
| 157 |
+
# Remove duplicate positions
|
| 158 |
chr_positions <- chr_positions[!duplicated(chr_positions)]
|
| 159 |
chr_markers <- chr_markers[!duplicated(chr_markers)]
|
| 160 |
|
|
|
|
| 169 |
}
|
| 170 |
}
|
| 171 |
|
| 172 |
+
if (length(geno_list) == 0) stop("No valid chromosomes found")
|
|
|
|
|
|
|
| 173 |
|
| 174 |
+
# Create cross object
|
| 175 |
cross <- list(geno = geno_list, pheno = pheno_df)
|
| 176 |
class(cross) <- c(cross_type, "cross")
|
| 177 |
+
|
| 178 |
+
# Run basic checks
|
| 179 |
cross <- jittermap(cross)
|
| 180 |
|
| 181 |
+
# Calculate additional statistics
|
| 182 |
+
attr(cross, "summary") <- list(
|
| 183 |
+
n_ind = nind(cross),
|
| 184 |
+
n_markers = totmar(cross),
|
| 185 |
+
n_chr = nchr(cross),
|
| 186 |
+
missing_rate = mean(nmissing(cross, what="mar") / nind(cross)),
|
| 187 |
+
heterozygosity = mean(calc.heterozyg(cross), na.rm=TRUE)
|
| 188 |
+
)
|
| 189 |
+
|
| 190 |
return(cross)
|
| 191 |
}
|
| 192 |
|
| 193 |
# ============================================================================
|
| 194 |
+
# 10 COMPLETE MAPPING MODELS
|
| 195 |
# ============================================================================
|
| 196 |
|
| 197 |
+
run_all_ten_models <- function(cross, pheno_col) {
|
| 198 |
models <- list()
|
| 199 |
+
model_status <- list()
|
| 200 |
|
| 201 |
# Model 1: Haley-Knott Regression
|
| 202 |
+
safe_notify("Running Model 1/10: Haley-Knott (HK)...", "message")
|
| 203 |
tryCatch({
|
| 204 |
models$hk <- scanone(cross, pheno.col = pheno_col, method = "hk")
|
| 205 |
+
model_status$hk <- "Success"
|
| 206 |
+
}, error = function(e) {
|
| 207 |
+
model_status$hk <- paste("Failed:", e$message)
|
| 208 |
+
safe_notify(paste("HK failed:", e$message), "warning")
|
| 209 |
+
})
|
| 210 |
|
| 211 |
# Model 2: EM Algorithm
|
| 212 |
+
safe_notify("Running Model 2/10: EM Algorithm...", "message")
|
| 213 |
tryCatch({
|
| 214 |
models$em <- scanone(cross, pheno.col = pheno_col, method = "em")
|
| 215 |
+
model_status$em <- "Success"
|
| 216 |
+
}, error = function(e) {
|
| 217 |
+
model_status$em <- paste("Failed:", e$message)
|
| 218 |
+
safe_notify(paste("EM failed:", e$message), "warning")
|
| 219 |
+
})
|
| 220 |
|
| 221 |
# Model 3: Extended Haley-Knott
|
| 222 |
+
safe_notify("Running Model 3/10: Extended Haley-Knott (EHK)...", "message")
|
| 223 |
tryCatch({
|
| 224 |
models$ehk <- scanone(cross, pheno.col = pheno_col, method = "ehk")
|
| 225 |
+
model_status$ehk <- "Success"
|
| 226 |
+
}, error = function(e) {
|
| 227 |
+
model_status$ehk <- paste("Failed:", e$message)
|
| 228 |
+
safe_notify(paste("EHK failed:", e$message), "warning")
|
| 229 |
+
})
|
| 230 |
|
| 231 |
# Model 4: Multiple Imputation
|
| 232 |
+
safe_notify("Running Model 4/10: Multiple Imputation (IMP)...", "message")
|
| 233 |
tryCatch({
|
| 234 |
cross_sim <- sim.geno(cross, n.draws = 32, step = 1)
|
| 235 |
models$imp <- scanone(cross_sim, pheno.col = pheno_col, method = "imp")
|
| 236 |
+
model_status$imp <- "Success"
|
| 237 |
+
}, error = function(e) {
|
| 238 |
+
model_status$imp <- paste("Failed:", e$message)
|
| 239 |
+
safe_notify(paste("IMP failed:", e$message), "warning")
|
| 240 |
+
})
|
| 241 |
|
| 242 |
# Model 5: Marker Regression
|
| 243 |
+
safe_notify("Running Model 5/10: Marker Regression (MR)...", "message")
|
| 244 |
tryCatch({
|
| 245 |
models$mr <- scanone(cross, pheno.col = pheno_col, method = "mr")
|
| 246 |
+
model_status$mr <- "Success"
|
| 247 |
+
}, error = function(e) {
|
| 248 |
+
model_status$mr <- paste("Failed:", e$message)
|
| 249 |
+
safe_notify(paste("MR failed:", e$message), "warning")
|
| 250 |
+
})
|
| 251 |
|
| 252 |
# Model 6: Non-parametric
|
| 253 |
+
safe_notify("Running Model 6/10: Non-parametric (NP)...", "message")
|
| 254 |
tryCatch({
|
| 255 |
models$np <- scanone(cross, pheno.col = pheno_col, method = "np")
|
| 256 |
+
model_status$np <- "Success"
|
| 257 |
+
}, error = function(e) {
|
| 258 |
+
model_status$np <- paste("Failed:", e$message)
|
| 259 |
+
safe_notify(paste("NP failed:", e$message), "warning")
|
| 260 |
+
})
|
| 261 |
|
| 262 |
# Model 7: Composite Interval Mapping
|
| 263 |
+
safe_notify("Running Model 7/10: Composite Interval Mapping (CIM)...", "message")
|
| 264 |
tryCatch({
|
| 265 |
models$cim <- cim(cross, pheno.col = pheno_col, method = "hk", window = 10)
|
| 266 |
+
model_status$cim <- "Success"
|
| 267 |
+
}, error = function(e) {
|
| 268 |
+
model_status$cim <- paste("Failed:", e$message)
|
| 269 |
+
safe_notify(paste("CIM failed:", e$message), "warning")
|
| 270 |
+
})
|
| 271 |
|
| 272 |
# Model 8: Multiple QTL Mapping
|
| 273 |
+
safe_notify("Running Model 8/10: Multiple QTL Mapping (MQM)...", "message")
|
| 274 |
tryCatch({
|
| 275 |
scan_hk <- scanone(cross, pheno.col = pheno_col, method = "hk")
|
| 276 |
+
cofactors <- which(scan_hk$lod > 2.5)
|
| 277 |
if (length(cofactors) > 0) {
|
| 278 |
models$mqm <- mqm(cross, pheno.col = pheno_col, cofactors = cofactors)
|
| 279 |
+
model_status$mqm <- "Success"
|
| 280 |
+
} else {
|
| 281 |
+
models$mqm <- scan_hk
|
| 282 |
+
model_status$mqm <- "Used HK (no cofactors)"
|
| 283 |
}
|
| 284 |
+
}, error = function(e) {
|
| 285 |
+
model_status$mqm <- paste("Failed:", e$message)
|
| 286 |
+
safe_notify(paste("MQM failed:", e$message), "warning")
|
| 287 |
+
})
|
| 288 |
|
| 289 |
+
# Model 9: Binary Trait (if applicable)
|
| 290 |
+
safe_notify("Running Model 9/10: Binary Trait Analysis...", "message")
|
| 291 |
tryCatch({
|
| 292 |
models$binary <- scanone(cross, pheno.col = pheno_col, method = "binary")
|
| 293 |
+
model_status$binary <- "Success"
|
| 294 |
+
}, error = function(e) {
|
| 295 |
+
model_status$binary <- paste("Failed:", e$message)
|
| 296 |
+
safe_notify(paste("Binary failed:", e$message), "warning")
|
| 297 |
+
})
|
| 298 |
|
| 299 |
# Model 10: Normal Model
|
| 300 |
+
safe_notify("Running Model 10/10: Normal Model...", "message")
|
| 301 |
tryCatch({
|
| 302 |
models$normal <- scanone(cross, pheno.col = pheno_col, method = "normal")
|
| 303 |
+
model_status$normal <- "Success"
|
| 304 |
+
}, error = function(e) {
|
| 305 |
+
model_status$normal <- paste("Failed:", e$message)
|
| 306 |
+
safe_notify(paste("Normal failed:", e$message), "warning")
|
| 307 |
+
})
|
| 308 |
|
| 309 |
+
attr(models, "status") <- model_status
|
| 310 |
return(models)
|
| 311 |
}
|
| 312 |
|
| 313 |
+
# ============================================================================
|
| 314 |
+
# COMPREHENSIVE QTL DETECTION WITH FULL DETAILS
|
| 315 |
+
# ============================================================================
|
| 316 |
+
|
| 317 |
+
detect_comprehensive_qtls <- function(scan, threshold, cross = NULL, pheno_col = NULL) {
|
| 318 |
if (is.null(scan) || nrow(scan) == 0) {
|
| 319 |
return(data.frame())
|
| 320 |
}
|
|
|
|
| 325 |
}
|
| 326 |
|
| 327 |
qtls <- data.frame()
|
| 328 |
+
|
| 329 |
for (chr in unique(scan_sig$chr)) {
|
| 330 |
chr_data <- scan_sig[scan_sig$chr == chr, ]
|
| 331 |
+
chr_all <- scan[scan$chr == chr, ]
|
| 332 |
|
| 333 |
+
# Find peaks (local maxima)
|
| 334 |
peaks <- c()
|
| 335 |
for (i in 1:nrow(chr_data)) {
|
| 336 |
is_peak <- TRUE
|
| 337 |
+
if (i > 1 && chr_data$lod[i] <= chr_data$lod[i-1]) is_peak <- FALSE
|
| 338 |
+
if (i < nrow(chr_data) && chr_data$lod[i] <= chr_data$lod[i+1]) is_peak <- FALSE
|
| 339 |
if (is_peak) peaks <- c(peaks, i)
|
| 340 |
}
|
| 341 |
|
|
|
|
| 343 |
for (peak_idx in peaks) {
|
| 344 |
peak_row <- chr_data[peak_idx, ]
|
| 345 |
|
| 346 |
+
# Get peak marker
|
| 347 |
+
peak_marker <- rownames(peak_row)
|
| 348 |
+
|
| 349 |
+
# Calculate 1.5 LOD drop confidence interval
|
| 350 |
lod_diff <- chr_all$lod - peak_row$lod
|
| 351 |
ci_indices <- which(lod_diff >= -1.5)
|
| 352 |
+
|
| 353 |
if (length(ci_indices) > 0) {
|
| 354 |
+
left_idx <- min(ci_indices)
|
| 355 |
+
right_idx <- max(ci_indices)
|
| 356 |
+
left_pos <- chr_all$pos[left_idx]
|
| 357 |
+
right_pos <- chr_all$pos[right_idx]
|
| 358 |
+
left_marker <- rownames(chr_all)[left_idx]
|
| 359 |
+
right_marker <- rownames(chr_all)[right_idx]
|
| 360 |
} else {
|
| 361 |
left_pos <- peak_row$pos - 5
|
| 362 |
right_pos <- peak_row$pos + 5
|
| 363 |
+
left_marker <- NA
|
| 364 |
+
right_marker <- NA
|
| 365 |
}
|
| 366 |
|
| 367 |
+
# Calculate 2 LOD drop CI (more conservative)
|
| 368 |
+
ci2_indices <- which(lod_diff >= -2)
|
| 369 |
+
if (length(ci2_indices) > 0) {
|
| 370 |
+
left2_pos <- chr_all$pos[min(ci2_indices)]
|
| 371 |
+
right2_pos <- chr_all$pos[max(ci2_indices)]
|
| 372 |
+
} else {
|
| 373 |
+
left2_pos <- peak_row$pos - 3
|
| 374 |
+
right2_pos <- peak_row$pos + 3
|
| 375 |
+
}
|
| 376 |
+
|
| 377 |
+
# Calculate PVE (Proportion of Variance Explained)
|
| 378 |
n <- nrow(scan)
|
| 379 |
pve <- (1 - 10^(-2 * peak_row$lod / n)) * 100
|
| 380 |
|
| 381 |
+
# Calculate additive and dominance effects if cross is provided
|
| 382 |
+
add_effect <- NA
|
| 383 |
+
dom_effect <- NA
|
| 384 |
+
if (!is.null(cross) && !is.null(pheno_col)) {
|
| 385 |
+
tryCatch({
|
| 386 |
+
# Find nearest marker
|
| 387 |
+
map <- pull.map(cross)[[as.character(chr)]]
|
| 388 |
+
marker_pos <- which.min(abs(map - peak_row$pos))
|
| 389 |
+
marker_name <- names(map)[marker_pos]
|
| 390 |
+
|
| 391 |
+
# Get genotypes and phenotypes
|
| 392 |
+
geno_idx <- find.marker(cross, as.character(chr), marker_name)
|
| 393 |
+
genotypes <- pull.geno(cross)[, geno_idx]
|
| 394 |
+
phenotypes <- cross$pheno[[pheno_col]]
|
| 395 |
+
|
| 396 |
+
# Calculate means per genotype
|
| 397 |
+
means <- tapply(phenotypes, genotypes, mean, na.rm=TRUE)
|
| 398 |
+
|
| 399 |
+
if (length(means) == 3) { # F2
|
| 400 |
+
add_effect <- (means[3] - means[1]) / 2
|
| 401 |
+
dom_effect <- means[2] - (means[1] + means[3]) / 2
|
| 402 |
+
} else if (length(means) == 2) { # BC
|
| 403 |
+
add_effect <- means[2] - means[1]
|
| 404 |
+
}
|
| 405 |
+
}, error = function(e) {
|
| 406 |
+
# Silent fail for effects
|
| 407 |
+
})
|
| 408 |
+
}
|
| 409 |
+
|
| 410 |
qtls <- rbind(qtls, data.frame(
|
| 411 |
+
QTL_ID = paste0("QTL_", chr, "_", round(peak_row$pos, 1)),
|
| 412 |
Chromosome = as.character(chr),
|
| 413 |
+
Peak_Position_cM = round(peak_row$pos, 2),
|
| 414 |
+
Peak_Marker = peak_marker,
|
| 415 |
+
LOD_Score = round(peak_row$lod, 3),
|
| 416 |
+
PVE_Percent = round(pve, 2),
|
| 417 |
+
CI_1.5LOD_Left_cM = round(left_pos, 2),
|
| 418 |
+
CI_1.5LOD_Right_cM = round(right_pos, 2),
|
| 419 |
+
CI_1.5LOD_Width_cM = round(right_pos - left_pos, 2),
|
| 420 |
+
CI_2LOD_Left_cM = round(left2_pos, 2),
|
| 421 |
+
CI_2LOD_Right_cM = round(right2_pos, 2),
|
| 422 |
+
CI_2LOD_Width_cM = round(right2_pos - left2_pos, 2),
|
| 423 |
+
Left_Flanking_Marker = left_marker,
|
| 424 |
+
Right_Flanking_Marker = right_marker,
|
| 425 |
+
Additive_Effect = round(add_effect, 4),
|
| 426 |
+
Dominance_Effect = round(dom_effect, 4),
|
| 427 |
stringsAsFactors = FALSE
|
| 428 |
))
|
| 429 |
}
|
|
|
|
| 431 |
}
|
| 432 |
|
| 433 |
if (nrow(qtls) > 0) {
|
| 434 |
+
# Sort by chromosome and position
|
| 435 |
qtls$ChrNum <- suppressWarnings(as.numeric(qtls$Chromosome))
|
| 436 |
qtls$ChrNum[is.na(qtls$ChrNum)] <- 999
|
| 437 |
+
qtls <- qtls[order(qtls$ChrNum, qtls$Peak_Position_cM), ]
|
| 438 |
qtls$ChrNum <- NULL
|
| 439 |
+
qtls$QTL_ID <- NULL
|
| 440 |
}
|
| 441 |
|
| 442 |
return(qtls)
|
| 443 |
}
|
| 444 |
|
| 445 |
# ============================================================================
|
| 446 |
+
# PERMUTATION TEST WITH MULTIPLE ALPHAS
|
| 447 |
# ============================================================================
|
| 448 |
|
| 449 |
+
run_permutation_analysis <- function(cross, pheno_col, n_perm = 1000) {
|
| 450 |
+
tryCatch({
|
| 451 |
+
perms <- scanone(cross, pheno.col = pheno_col, n.perm = n_perm, method = "hk")
|
| 452 |
+
|
| 453 |
+
thresholds <- data.frame(
|
| 454 |
+
Alpha = c(0.001, 0.01, 0.05, 0.1, 0.2),
|
| 455 |
+
LOD_Threshold = rep(NA, 5)
|
| 456 |
+
)
|
| 457 |
+
|
| 458 |
+
for (i in 1:5) {
|
| 459 |
+
thr <- summary(perms, alpha = thresholds$Alpha[i])[1]
|
| 460 |
+
thresholds$LOD_Threshold[i] <- round(thr, 3)
|
| 461 |
+
}
|
| 462 |
+
|
| 463 |
+
return(list(permutations = perms, thresholds = thresholds))
|
| 464 |
+
}, error = function(e) {
|
| 465 |
+
safe_notify(paste("Permutation failed:", e$message), "warning")
|
| 466 |
+
return(NULL)
|
| 467 |
+
})
|
| 468 |
+
}
|
| 469 |
+
|
| 470 |
+
# ============================================================================
|
| 471 |
+
# MODEL COMPARISON TABLE
|
| 472 |
+
# ============================================================================
|
| 473 |
+
|
| 474 |
+
create_model_comparison <- function(models, threshold) {
|
| 475 |
+
if (is.null(models) || length(models) == 0) return(data.frame())
|
| 476 |
+
|
| 477 |
+
comparison <- data.frame()
|
| 478 |
|
|
|
|
| 479 |
for (name in names(models)) {
|
| 480 |
scan <- models[[name]]
|
| 481 |
if (!is.null(scan)) {
|
| 482 |
+
max_lod <- max(scan$lod, na.rm = TRUE)
|
| 483 |
+
mean_lod <- mean(scan$lod, na.rm = TRUE)
|
| 484 |
+
n_peaks <- sum(scan$lod >= threshold, na.rm = TRUE)
|
| 485 |
+
max_pos <- scan$pos[which.max(scan$lod)]
|
| 486 |
+
max_chr <- scan$chr[which.max(scan$lod)]
|
| 487 |
+
|
| 488 |
+
comparison <- rbind(comparison, data.frame(
|
| 489 |
+
Model = toupper(name),
|
| 490 |
+
Max_LOD = round(max_lod, 3),
|
| 491 |
+
Mean_LOD = round(mean_lod, 3),
|
| 492 |
+
N_Significant_Peaks = n_peaks,
|
| 493 |
+
Peak_Chromosome = as.character(max_chr),
|
| 494 |
+
Peak_Position_cM = round(max_pos, 2),
|
| 495 |
+
stringsAsFactors = FALSE
|
| 496 |
+
))
|
| 497 |
}
|
| 498 |
}
|
| 499 |
|
| 500 |
+
return(comparison[order(-comparison$Max_LOD), ])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 501 |
}
|
| 502 |
|
| 503 |
+
# ============================================================================
|
| 504 |
+
# PLOTTING FUNCTIONS - 15+ UNIQUE VISUALIZATIONS
|
| 505 |
+
# ============================================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 506 |
|
| 507 |
+
# Plot 1: Enhanced Manhattan Plot with QTL Highlights
|
| 508 |
+
plot_manhattan_enhanced <- function(scan, qtls, threshold) {
|
| 509 |
if (is.null(scan)) return(NULL)
|
| 510 |
|
| 511 |
df <- as.data.frame(scan)
|
| 512 |
+
df$marker <- rownames(df)
|
| 513 |
+
df$significant <- df$lod >= threshold
|
| 514 |
+
|
| 515 |
+
# Calculate cumulative positions
|
| 516 |
+
chrs <- unique(df$chr)
|
| 517 |
+
chr_lengths <- sapply(chrs, function(ch) max(df$pos[df$chr == ch], na.rm = TRUE))
|
| 518 |
+
cum_lengths <- cumsum(c(0, chr_lengths[-length(chrs)]))
|
| 519 |
+
names(cum_lengths) <- chrs
|
| 520 |
+
|
| 521 |
+
df$pos_cum <- df$pos + cum_lengths[as.character(df$chr)]
|
| 522 |
+
chr_midpoints <- sapply(chrs, function(ch) mean(df$pos_cum[df$chr == ch], na.rm = TRUE))
|
| 523 |
+
|
| 524 |
+
# Create color palette
|
| 525 |
+
colors <- rep(brewer.pal(8, "Set1"), length.out = length(chrs))
|
| 526 |
+
names(colors) <- chrs
|
| 527 |
+
|
| 528 |
+
p <- ggplot(df, aes(x = pos_cum, y = lod, color = chr)) +
|
| 529 |
+
geom_point(size = 1.5, alpha = 0.7) +
|
| 530 |
+
geom_hline(yintercept = threshold, color = "red", linetype = "dashed", size = 1.2) +
|
| 531 |
+
scale_x_continuous(breaks = chr_midpoints, labels = names(chr_midpoints)) +
|
| 532 |
+
scale_color_manual(values = colors) +
|
| 533 |
+
labs(title = "Genome-wide Manhattan Plot",
|
| 534 |
+
x = "Chromosome", y = "LOD Score") +
|
| 535 |
+
theme_minimal() +
|
| 536 |
+
theme(legend.position = "none",
|
| 537 |
+
axis.text.x = element_text(angle = 45, hjust = 1),
|
| 538 |
+
plot.title = element_text(hjust = 0.5, face = "bold", size = 14))
|
| 539 |
+
|
| 540 |
+
# Add QTL highlights
|
| 541 |
+
if (!is.null(qtls) && nrow(qtls) > 0) {
|
| 542 |
+
qtls$pos_cum <- qtls$Peak_Position_cM + cum_lengths[as.character(qtls$Chromosome)]
|
| 543 |
+
p <- p +
|
| 544 |
+
geom_point(data = qtls, aes(x = pos_cum, y = LOD_Score),
|
| 545 |
+
color = "gold", size = 5, shape = 18) +
|
| 546 |
+
geom_text_repel(data = qtls, aes(x = pos_cum, y = LOD_Score,
|
| 547 |
+
label = paste0("QTL-", 1:nrow(qtls))),
|
| 548 |
+
color = "darkred", size = 3.5, fontface = "bold")
|
| 549 |
+
}
|
| 550 |
|
| 551 |
+
return(p)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 552 |
}
|
| 553 |
|
| 554 |
+
# Plot 2: LOD Contour Plot
|
| 555 |
+
plot_lod_contour <- function(scan) {
|
| 556 |
+
if (is.null(scan)) return(NULL)
|
| 557 |
|
| 558 |
+
chrs <- unique(scan$chr)
|
| 559 |
+
contour_data <- data.frame()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 560 |
|
| 561 |
+
for (chr in chrs) {
|
| 562 |
+
chr_data <- scan[scan$chr == chr, ]
|
| 563 |
+
chr_data$chr_num <- as.numeric(factor(chr, levels = chrs))
|
| 564 |
+
contour_data <- rbind(contour_data, chr_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 565 |
}
|
| 566 |
|
| 567 |
+
ggplot(contour_data, aes(x = pos, y = chr_num, z = lod)) +
|
| 568 |
+
geom_contour_filled(bins = 8) +
|
| 569 |
+
scale_fill_viridis_d(name = "LOD", direction = -1) +
|
| 570 |
+
scale_y_continuous(breaks = 1:length(chrs), labels = chrs) +
|
| 571 |
+
labs(title = "LOD Contour Plot Across Genome",
|
| 572 |
+
x = "Position (cM)", y = "Chromosome") +
|
| 573 |
+
theme_minimal() +
|
| 574 |
+
theme(plot.title = element_text(hjust = 0.5, face = "bold"))
|
| 575 |
}
|
| 576 |
|
| 577 |
+
# Plot 3: QTL Effect Bubble Plot
|
| 578 |
+
plot_qtl_bubble <- function(qtls) {
|
| 579 |
+
if (is.null(qtls) || nrow(qtls) == 0) return(NULL)
|
| 580 |
|
| 581 |
+
ggplot(qtls, aes(x = Chromosome, y = Peak_Position_cM,
|
| 582 |
+
size = LOD_Score, color = PVE_Percent)) +
|
| 583 |
+
geom_point(alpha = 0.8) +
|
| 584 |
+
scale_size_continuous(range = c(3, 20), name = "LOD Score") +
|
| 585 |
+
scale_color_gradientn(colors = c("blue", "green", "yellow", "red"),
|
| 586 |
+
name = "PVE (%)") +
|
| 587 |
+
labs(title = "QTL Effect Size Bubble Plot",
|
| 588 |
+
x = "Chromosome", y = "Peak Position (cM)") +
|
| 589 |
+
theme_minimal() +
|
| 590 |
+
theme(axis.text.x = element_text(angle = 45, hjust = 1),
|
| 591 |
+
plot.title = element_text(hjust = 0.5, face = "bold"))
|
| 592 |
}
|
| 593 |
|
| 594 |
+
# Plot 4: Violin Plot for Genotype-Phenotype Association
|
| 595 |
+
plot_genotype_violin <- function(cross, qtl, trait) {
|
| 596 |
+
if (is.null(cross) || is.null(qtl) || nrow(qtl) == 0) return(NULL)
|
| 597 |
|
| 598 |
+
chr_name <- as.character(qtl$Chromosome[1])
|
| 599 |
if (!chr_name %in% names(cross$geno)) return(NULL)
|
| 600 |
|
| 601 |
+
# Find nearest marker
|
| 602 |
map <- pull.map(cross)[[chr_name]]
|
| 603 |
+
marker_pos <- which.min(abs(map - qtl$Peak_Position_cM[1]))
|
| 604 |
marker_name <- names(map)[marker_pos]
|
| 605 |
|
| 606 |
+
# Get data
|
| 607 |
geno_idx <- find.marker(cross, chr_name, marker_name)
|
| 608 |
genotypes <- pull.geno(cross)[, geno_idx]
|
| 609 |
phenotypes <- cross$pheno[[trait]]
|
| 610 |
|
| 611 |
+
df <- data.frame(
|
| 612 |
+
Genotype = factor(genotypes, levels = sort(unique(genotypes))),
|
| 613 |
+
Phenotype = phenotypes
|
| 614 |
+
)
|
| 615 |
df <- df[!is.na(df$Genotype) & !is.na(df$Phenotype), ]
|
| 616 |
|
| 617 |
+
# Recode for better labels
|
| 618 |
+
if (length(unique(df$Genotype)) == 3) {
|
| 619 |
+
df$Genotype <- recode(df$Genotype, `1` = "AA", `2` = "AB", `3` = "BB")
|
| 620 |
+
} else if (length(unique(df$Genotype)) == 2) {
|
| 621 |
+
df$Genotype <- recode(df$Genotype, `1` = "A", `2` = "B")
|
| 622 |
+
}
|
| 623 |
|
| 624 |
ggplot(df, aes(x = Genotype, y = Phenotype, fill = Genotype)) +
|
| 625 |
geom_violin(alpha = 0.6, trim = FALSE) +
|
| 626 |
geom_boxplot(width = 0.2, alpha = 0.8) +
|
| 627 |
+
stat_summary(fun = mean, geom = "point", shape = 18, size = 5, color = "red") +
|
| 628 |
+
stat_summary(fun = mean, geom = "text", aes(label = round(..y.., 2)),
|
| 629 |
+
vjust = -1.5, size = 4, color = "darkred") +
|
| 630 |
+
scale_fill_brewer(palette = "Set2") +
|
| 631 |
+
labs(title = paste("Phenotype Distribution at", marker_name),
|
| 632 |
+
x = "Genotype", y = trait) +
|
| 633 |
theme_minimal() +
|
| 634 |
+
theme(legend.position = "none",
|
| 635 |
+
plot.title = element_text(hjust = 0.5, face = "bold"))
|
| 636 |
}
|
| 637 |
|
| 638 |
+
# Plot 5: Ridge Plot of LOD Distributions
|
| 639 |
+
plot_ridge_distribution <- function(scan) {
|
| 640 |
+
if (is.null(scan)) return(NULL)
|
| 641 |
|
| 642 |
+
df <- as.data.frame(scan)
|
| 643 |
+
df$chr <- factor(df$chr, levels = unique(df$chr))
|
| 644 |
|
| 645 |
+
ggplot(df, aes(x = lod, y = chr, fill = after_stat(x))) +
|
| 646 |
+
geom_density_ridges_gradient(scale = 2, rel_min_height = 0.01) +
|
| 647 |
+
scale_fill_viridis_c(option = "C", name = "LOD Score") +
|
| 648 |
+
labs(title = "LOD Score Distribution by Chromosome",
|
| 649 |
+
x = "LOD Score", y = "Chromosome") +
|
| 650 |
+
theme_ridges() +
|
| 651 |
+
theme(plot.title = element_text(hjust = 0.5, face = "bold"))
|
| 652 |
+
}
|
| 653 |
+
|
| 654 |
+
# Plot 6: 3D Surface Plot
|
| 655 |
+
plot_3d_surface <- function(scan) {
|
| 656 |
+
if (is.null(scan)) return(NULL)
|
| 657 |
+
|
| 658 |
+
df <- as.data.frame(scan)
|
| 659 |
+
df$chr_num <- as.numeric(factor(df$chr, levels = unique(df$chr)))
|
| 660 |
+
|
| 661 |
+
plot_ly(df, x = ~chr_num, y = ~pos, z = ~lod,
|
| 662 |
+
type = "mesh3d", intensity = ~lod, colorscale = "Viridis",
|
| 663 |
+
text = ~paste("Chromosome:", chr, "<br>Position:", pos,
|
| 664 |
+
"<br>LOD:", round(lod, 3)),
|
| 665 |
+
hoverinfo = "text") %>%
|
| 666 |
+
layout(title = "3D LOD Surface Plot",
|
| 667 |
+
scene = list(xaxis = list(title = "Chromosome"),
|
| 668 |
+
yaxis = list(title = "Position (cM)"),
|
| 669 |
+
zaxis = list(title = "LOD Score")),
|
| 670 |
+
paper_bgcolor = "#f4f4f4")
|
| 671 |
+
}
|
| 672 |
+
|
| 673 |
+
# Plot 7: Model Comparison Bar Plot
|
| 674 |
+
plot_model_comparison <- function(comparison_df) {
|
| 675 |
+
if (is.null(comparison_df) || nrow(comparison_df) == 0) return(NULL)
|
| 676 |
+
|
| 677 |
+
ggplot(comparison_df, aes(x = reorder(Model, -Max_LOD), y = Max_LOD, fill = Model)) +
|
| 678 |
+
geom_bar(stat = "identity", alpha = 0.8) +
|
| 679 |
+
geom_text(aes(label = Max_LOD), vjust = -0.5, size = 4, fontface = "bold") +
|
| 680 |
+
scale_fill_brewer(palette = "Set2") +
|
| 681 |
+
labs(title = "Model Performance Comparison",
|
| 682 |
+
x = "Mapping Method", y = "Maximum LOD Score") +
|
| 683 |
+
theme_minimal() +
|
| 684 |
+
theme(legend.position = "none",
|
| 685 |
+
axis.text.x = element_text(angle = 45, hjust = 1),
|
| 686 |
+
plot.title = element_text(hjust = 0.5, face = "bold"))
|
| 687 |
+
}
|
| 688 |
+
|
| 689 |
+
# Plot 8: QQ Plot
|
| 690 |
+
plot_qq_plot <- function(scan) {
|
| 691 |
+
if (is.null(scan)) return(NULL)
|
| 692 |
|
| 693 |
+
expected <- -log10(ppoints(length(scan$lod)))
|
| 694 |
+
observed <- -log10(rank(scan$lod) / (length(scan$lod) + 1))
|
| 695 |
|
| 696 |
+
df <- data.frame(Expected = expected, Observed = observed)
|
|
|
|
| 697 |
|
| 698 |
+
ggplot(df, aes(x = Expected, y = Observed)) +
|
| 699 |
+
geom_point(alpha = 0.6, color = "steelblue") +
|
| 700 |
+
geom_abline(intercept = 0, slope = 1, color = "red", linetype = "dashed", size = 1) +
|
| 701 |
+
labs(title = "Q-Q Plot for LOD Scores",
|
| 702 |
+
x = "Expected -log10(p)", y = "Observed -log10(p)") +
|
| 703 |
+
theme_minimal() +
|
| 704 |
+
theme(plot.title = element_text(hjust = 0.5, face = "bold"))
|
| 705 |
}
|
| 706 |
|
| 707 |
+
# Plot 9: Correlation Heatmap for Phenotypes
|
| 708 |
+
plot_phenotype_correlation <- function(cross) {
|
| 709 |
+
if (is.null(cross) || ncol(cross$pheno) < 2) return(NULL)
|
| 710 |
|
| 711 |
+
cor_matrix <- cor(cross$pheno, use = "complete.obs")
|
| 712 |
+
|
| 713 |
+
corrplot(cor_matrix, method = "color", type = "upper",
|
| 714 |
+
order = "hclust", addCoef.col = "black",
|
| 715 |
+
tl.col = "black", tl.srt = 45,
|
| 716 |
+
title = "Phenotype Correlation Matrix",
|
| 717 |
+
mar = c(0, 0, 2, 0))
|
| 718 |
+
}
|
| 719 |
+
|
| 720 |
+
# Plot 10: Genetic Map Density
|
| 721 |
+
plot_genetic_density <- function(cross) {
|
| 722 |
+
if (is.null(cross)) return(NULL)
|
| 723 |
+
|
| 724 |
+
map <- pull.map(cross)
|
| 725 |
+
density_data <- data.frame()
|
| 726 |
+
|
| 727 |
+
for (chr in names(map)) {
|
| 728 |
+
positions <- map[[chr]]
|
| 729 |
+
if (length(positions) > 1) {
|
| 730 |
+
gaps <- diff(positions)
|
| 731 |
+
density_data <- rbind(density_data,
|
| 732 |
+
data.frame(Chromosome = chr, Gap = gaps))
|
| 733 |
}
|
| 734 |
}
|
| 735 |
|
| 736 |
+
if (nrow(density_data) == 0) return(NULL)
|
| 737 |
|
| 738 |
+
ggplot(density_data, aes(x = Gap, fill = Chromosome)) +
|
| 739 |
+
geom_histogram(bins = 30, alpha = 0.6, position = "stack") +
|
| 740 |
+
scale_fill_brewer(palette = "Set3") +
|
| 741 |
+
labs(title = "Marker Density Distribution",
|
| 742 |
+
x = "Inter-marker Distance (cM)", y = "Frequency") +
|
| 743 |
+
theme_minimal() +
|
| 744 |
+
theme(legend.position = "bottom",
|
| 745 |
+
plot.title = element_text(hjust = 0.5, face = "bold"))
|
| 746 |
+
}
|
| 747 |
+
|
| 748 |
+
# Plot 11: Missing Data Pattern
|
| 749 |
+
plot_missing_data <- function(cross) {
|
| 750 |
+
if (is.null(cross)) return(NULL)
|
| 751 |
+
|
| 752 |
+
missing_ind <- nmissing(cross, what = "ind")
|
| 753 |
+
missing_marker <- nmissing(cross, what = "mar")
|
| 754 |
+
|
| 755 |
+
df_ind <- data.frame(Individual = seq_along(missing_ind), Missing = missing_ind)
|
| 756 |
+
df_marker <- data.frame(Marker = seq_along(missing_marker), Missing = missing_marker)
|
| 757 |
+
|
| 758 |
+
p1 <- ggplot(df_ind, aes(x = Individual, y = Missing)) +
|
| 759 |
+
geom_bar(stat = "identity", fill = "steelblue", alpha = 0.7) +
|
| 760 |
+
labs(title = "Missing Data per Individual", x = "Individual", y = "Missing Count") +
|
| 761 |
+
theme_minimal()
|
| 762 |
|
| 763 |
+
p2 <- ggplot(df_marker, aes(x = Marker, y = Missing)) +
|
| 764 |
+
geom_bar(stat = "identity", fill = "darkorange", alpha = 0.7) +
|
| 765 |
+
labs(title = "Missing Data per Marker", x = "Marker Index", y = "Missing Count") +
|
| 766 |
+
theme_minimal()
|
| 767 |
|
| 768 |
+
plot_grid(p1, p2, ncol = 2)
|
|
|
|
|
|
|
| 769 |
}
|
| 770 |
|
| 771 |
+
# Plot 12: Parallel Coordinates for Models
|
| 772 |
+
plot_parallel_coords <- function(models) {
|
| 773 |
if (is.null(models) || length(models) == 0) return(NULL)
|
| 774 |
|
| 775 |
first_model <- models[[1]]
|
| 776 |
if (is.null(first_model)) return(NULL)
|
| 777 |
|
| 778 |
+
# Get top 50 markers
|
| 779 |
+
top_idx <- order(first_model$lod, decreasing = TRUE)[1:min(50, nrow(first_model))]
|
| 780 |
|
| 781 |
+
parallel_data <- data.frame(Position = first_model$pos[top_idx])
|
| 782 |
for (name in names(models)) {
|
| 783 |
scan <- models[[name]]
|
| 784 |
if (!is.null(scan)) {
|
| 785 |
+
parallel_data[[toupper(name)]] <- scan$lod[top_idx]
|
| 786 |
}
|
| 787 |
}
|
| 788 |
|
|
|
|
| 790 |
names_to = "Model", values_to = "LOD")
|
| 791 |
|
| 792 |
ggplot(plot_data, aes(x = Position, y = LOD, color = Model, group = Model)) +
|
| 793 |
+
geom_line(size = 1) +
|
| 794 |
+
scale_color_brewer(palette = "Set1") +
|
| 795 |
+
labs(title = "Model Comparison at Top Markers",
|
| 796 |
+
x = "Position (cM)", y = "LOD Score") +
|
| 797 |
+
theme_minimal() +
|
| 798 |
+
theme(legend.position = "bottom",
|
| 799 |
+
plot.title = element_text(hjust = 0.5, face = "bold"))
|
| 800 |
}
|
| 801 |
|
| 802 |
+
# Plot 13: QTL Interval Plot
|
| 803 |
+
plot_qtl_intervals <- function(qtls) {
|
| 804 |
+
if (is.null(qtls) || nrow(qtls) == 0) return(NULL)
|
| 805 |
|
| 806 |
+
qtls$QTL_ID <- paste0("Q", 1:nrow(qtls))
|
|
|
|
| 807 |
|
| 808 |
+
ggplot(qtls, aes(y = reorder(QTL_ID, Peak_Position_cM))) +
|
| 809 |
+
geom_segment(aes(x = CI_1.5LOD_Left_cM, xend = CI_1.5LOD_Right_cM,
|
| 810 |
+
yend = QTL_ID), color = "gray70", size = 3) +
|
| 811 |
+
geom_point(aes(x = Peak_Position_cM), color = "red", size = 4) +
|
| 812 |
+
labs(title = "QTL Confidence Intervals",
|
| 813 |
+
x = "Position (cM)", y = "QTL") +
|
| 814 |
+
theme_minimal() +
|
| 815 |
+
theme(plot.title = element_text(hjust = 0.5, face = "bold"))
|
| 816 |
}
|
| 817 |
|
| 818 |
+
# Plot 14: PVE Distribution
|
| 819 |
+
plot_pve_distribution <- function(qtls) {
|
| 820 |
+
if (is.null(qtls) || nrow(qtls) == 0) return(NULL)
|
| 821 |
+
|
| 822 |
+
ggplot(qtls, aes(x = reorder(paste(Chromosome, "@", Peak_Position_cM), PVE_Percent),
|
| 823 |
+
y = PVE_Percent, fill = Chromosome)) +
|
| 824 |
+
geom_bar(stat = "identity", alpha = 0.8) +
|
| 825 |
+
geom_text(aes(label = paste0(PVE_Percent, "%")), vjust = -0.5, size = 3.5) +
|
| 826 |
+
scale_fill_brewer(palette = "Set3") +
|
| 827 |
+
labs(title = "Proportion of Variance Explained by QTLs",
|
| 828 |
+
x = "QTL Location", y = "PVE (%)") +
|
| 829 |
+
theme_minimal() +
|
| 830 |
+
theme(axis.text.x = element_text(angle = 45, hjust = 1),
|
| 831 |
+
plot.title = element_text(hjust = 0.5, face = "bold"),
|
| 832 |
+
legend.position = "none")
|
| 833 |
+
}
|
| 834 |
+
|
| 835 |
+
# Plot 15: Chromosome-wise LOD Heatmap
|
| 836 |
+
plot_chromosome_heatmap <- function(scan) {
|
| 837 |
if (is.null(scan)) return(NULL)
|
| 838 |
|
| 839 |
+
# Create matrix for heatmap
|
| 840 |
chrs <- unique(scan$chr)
|
| 841 |
+
max_pos <- tapply(scan$pos, scan$chr, max)
|
| 842 |
|
| 843 |
+
heatmap_data <- matrix(NA, nrow = length(chrs), ncol = 100)
|
| 844 |
+
rownames(heatmap_data) <- chrs
|
| 845 |
+
|
| 846 |
+
for (i in 1:length(chrs)) {
|
| 847 |
+
chr_data <- scan[scan$chr == chrs[i], ]
|
| 848 |
+
positions <- seq(0, max_pos[i], length.out = 100)
|
| 849 |
+
lod_interp <- approx(chr_data$pos, chr_data$lod, xout = positions)$y
|
| 850 |
+
heatmap_data[i, ] <- lod_interp
|
| 851 |
}
|
| 852 |
|
| 853 |
+
pheatmap(heatmap_data, color = viridis(100), cluster_rows = FALSE,
|
| 854 |
+
cluster_cols = FALSE, main = "Chromosome-wise LOD Heatmap",
|
| 855 |
+
xlab = "Relative Position", ylab = "Chromosome")
|
|
|
|
|
|
|
|
|
|
| 856 |
}
|
| 857 |
|
| 858 |
# ============================================================================
|
|
|
|
| 860 |
# ============================================================================
|
| 861 |
|
| 862 |
ui <- dashboardPage(
|
| 863 |
+
dashboardHeader(title = "QTL Analysis Pipeline - Complete Suite", titleWidth = 350),
|
| 864 |
|
| 865 |
+
dashboardSidebar(width = 350,
|
| 866 |
sidebarMenu(
|
| 867 |
menuItem("Data Input", tabName = "data", icon = icon("upload")),
|
| 868 |
menuItem("10 Models", tabName = "models", icon = icon("cogs")),
|
| 869 |
+
menuItem("QTL Results", tabName = "results", icon = icon("table")),
|
| 870 |
+
menuItem("15+ Plots", tabName = "plots", icon = icon("chart-bar")),
|
| 871 |
+
menuItem("Download Center", tabName = "download", icon = icon("download")),
|
| 872 |
+
menuItem("Documentation", tabName = "help", icon = icon("question-circle"))
|
| 873 |
),
|
| 874 |
|
| 875 |
div(style = "padding: 15px;",
|
| 876 |
+
h4("Data Upload", style = "color: #3c8dbc;"),
|
| 877 |
fileInput("geno_file", "Genotype CSV", accept = ".csv"),
|
| 878 |
fileInput("pheno_file", "Phenotype CSV", accept = ".csv"),
|
| 879 |
|
| 880 |
selectInput("cross_type", "Cross Type",
|
| 881 |
+
choices = c("F2 Intercross" = "f2",
|
| 882 |
+
"Backcross" = "bc",
|
| 883 |
+
"RIL (Selfing)" = "riself",
|
| 884 |
+
"RIL (Sib-mating)" = "risib"),
|
| 885 |
+
selected = "f2"),
|
| 886 |
|
| 887 |
hr(),
|
| 888 |
|
| 889 |
+
h4("Analysis Parameters", style = "color: #3c8dbc;"),
|
| 890 |
+
selectInput("pheno_trait", "Select Phenotype Trait", choices = NULL),
|
|
|
|
|
|
|
| 891 |
|
| 892 |
+
numericInput("lod_threshold", "LOD Threshold",
|
| 893 |
+
value = 3.0, min = 0, max = 20, step = 0.5),
|
| 894 |
+
|
| 895 |
+
numericInput("n_permutations", "Number of Permutations",
|
| 896 |
+
value = 1000, min = 0, max = 10000, step = 100),
|
| 897 |
+
|
| 898 |
+
hr(),
|
| 899 |
+
|
| 900 |
+
actionButton("run_analysis", "RUN ALL 10 MODELS",
|
| 901 |
icon = icon("play"),
|
| 902 |
+
style = "background-color: #3c8dbc; color: white; width: 100%; margin-top: 10px;"),
|
| 903 |
|
| 904 |
br(), br(),
|
| 905 |
+
|
| 906 |
+
div(style = "background-color: #f0f0f0; padding: 10px; border-radius: 5px;",
|
| 907 |
+
h5("Model Status:", style = "margin-top: 0;"),
|
| 908 |
+
verbatimTextOutput("model_status", placeholder = TRUE)
|
| 909 |
+
)
|
| 910 |
)
|
| 911 |
),
|
| 912 |
|
| 913 |
dashboardBody(
|
| 914 |
useShinyjs(),
|
| 915 |
+
tags$head(
|
| 916 |
+
tags$style(HTML("
|
| 917 |
+
.content-wrapper, .right-side { background-color: #f4f4f4; }
|
| 918 |
+
.box { border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
|
| 919 |
+
.small-box { border-radius: 10px; }
|
| 920 |
+
.btn-download { background-color: #00a65a; color: white; }
|
| 921 |
+
"))
|
| 922 |
+
),
|
| 923 |
|
| 924 |
tabItems(
|
| 925 |
+
# Data Tab
|
| 926 |
tabItem(tabName = "data",
|
| 927 |
fluidRow(
|
| 928 |
valueBoxOutput("n_individuals"),
|
|
|
|
| 930 |
valueBoxOutput("n_chromosomes")
|
| 931 |
),
|
| 932 |
fluidRow(
|
| 933 |
+
box(title = "Data Summary", status = "primary", solidHeader = TRUE, width = 12,
|
| 934 |
+
verbatimTextOutput("data_summary")
|
| 935 |
+
)
|
| 936 |
),
|
| 937 |
fluidRow(
|
| 938 |
box(title = "Genetic Map", status = "info", width = 12,
|
| 939 |
+
plotOutput("genetic_map", height = "500px") %>% withSpinner()
|
| 940 |
+
)
|
| 941 |
+
),
|
| 942 |
+
fluidRow(
|
| 943 |
+
box(title = "Missing Data Pattern", status = "warning", width = 12,
|
| 944 |
+
plotOutput("missing_plot", height = "500px") %>% withSpinner()
|
| 945 |
+
)
|
| 946 |
+
),
|
| 947 |
+
fluidRow(
|
| 948 |
+
box(title = "Phenotype Distribution", status = "success", width = 12,
|
| 949 |
+
plotOutput("phenotype_dist", height = "500px") %>% withSpinner()
|
| 950 |
+
)
|
| 951 |
)
|
| 952 |
),
|
| 953 |
|
| 954 |
+
# 10 Models Tab
|
| 955 |
tabItem(tabName = "models",
|
| 956 |
fluidRow(
|
| 957 |
+
box(title = "Analysis Progress", status = "primary", solidHeader = TRUE, width = 12,
|
| 958 |
+
verbatimTextOutput("analysis_status")
|
| 959 |
+
)
|
| 960 |
),
|
| 961 |
fluidRow(
|
| 962 |
+
box(title = "Model Comparison", status = "info", width = 12,
|
| 963 |
+
DTOutput("model_comparison_table") %>% withSpinner()
|
| 964 |
+
)
|
| 965 |
),
|
| 966 |
fluidRow(
|
| 967 |
+
box(title = "Model Performance Bar Plot", status = "success", width = 12,
|
| 968 |
+
plotOutput("model_barplot", height = "500px") %>% withSpinner()
|
| 969 |
+
)
|
| 970 |
),
|
| 971 |
fluidRow(
|
| 972 |
+
box(title = "Parallel Coordinates Comparison", status = "warning", width = 12,
|
| 973 |
+
plotOutput("parallel_plot", height = "500px") %>% withSpinner()
|
| 974 |
+
)
|
| 975 |
+
),
|
| 976 |
+
fluidRow(
|
| 977 |
+
box(title = "Model QQ Plot", status = "info", width = 12,
|
| 978 |
+
plotOutput("qq_plot", height = "500px") %>% withSpinner()
|
| 979 |
+
)
|
| 980 |
)
|
| 981 |
),
|
| 982 |
|
| 983 |
+
# QTL Results Tab
|
| 984 |
+
tabItem(tabName = "results",
|
| 985 |
+
fluidRow(
|
| 986 |
+
box(title = "Significant QTLs - Complete Details", status = "primary", solidHeader = TRUE, width = 12,
|
| 987 |
+
DTOutput("qtl_table") %>% withSpinner()
|
| 988 |
+
)
|
| 989 |
+
),
|
| 990 |
fluidRow(
|
| 991 |
+
box(title = "Permutation Test Results", status = "info", width = 12,
|
| 992 |
+
DTOutput("permutation_table") %>% withSpinner()
|
| 993 |
+
)
|
| 994 |
),
|
| 995 |
fluidRow(
|
| 996 |
+
box(title = "QTL Intervals Plot", status = "success", width = 12,
|
| 997 |
+
plotOutput("qtl_intervals", height = "500px") %>% withSpinner()
|
| 998 |
+
)
|
|
|
|
| 999 |
),
|
| 1000 |
fluidRow(
|
| 1001 |
+
box(title = "PVE Distribution", status = "warning", width = 12,
|
| 1002 |
+
plotOutput("pve_distribution", height = "500px") %>% withSpinner()
|
| 1003 |
+
)
|
| 1004 |
+
)
|
| 1005 |
+
),
|
| 1006 |
+
|
| 1007 |
+
# Plots Tab
|
| 1008 |
+
tabItem(tabName = "plots",
|
| 1009 |
+
fluidRow(
|
| 1010 |
+
box(title = "Enhanced Manhattan Plot", status = "primary", width = 12,
|
| 1011 |
+
plotOutput("manhattan_plot", height = "600px") %>% withSpinner()
|
| 1012 |
+
)
|
| 1013 |
),
|
| 1014 |
fluidRow(
|
| 1015 |
+
box(title = "3D Surface Plot", status = "info", width = 12,
|
| 1016 |
+
plotlyOutput("surface_3d", height = "600px") %>% withSpinner()
|
| 1017 |
+
)
|
| 1018 |
),
|
| 1019 |
fluidRow(
|
| 1020 |
+
box(title = "LOD Contour Plot", status = "success", width = 6,
|
| 1021 |
+
plotOutput("contour_plot", height = "500px") %>% withSpinner()
|
| 1022 |
+
),
|
| 1023 |
+
box(title = "LOD Ridge Plot", status = "warning", width = 6,
|
| 1024 |
+
plotOutput("ridge_plot", height = "500px") %>% withSpinner()
|
| 1025 |
+
)
|
| 1026 |
),
|
| 1027 |
fluidRow(
|
| 1028 |
+
box(title = "QTL Effect Bubble Plot", status = "danger", width = 12,
|
| 1029 |
+
plotOutput("bubble_plot", height = "500px") %>% withSpinner()
|
| 1030 |
+
)
|
| 1031 |
+
),
|
| 1032 |
+
fluidRow(
|
| 1033 |
+
box(title = "Chromosome-wise LOD Heatmap", status = "info", width = 12,
|
| 1034 |
+
plotOutput("chromosome_heatmap", height = "600px") %>% withSpinner()
|
| 1035 |
+
)
|
| 1036 |
+
),
|
| 1037 |
fluidRow(
|
| 1038 |
+
box(title = "Genotype-Phenotype Association", status = "success", width = 12,
|
| 1039 |
+
selectInput("violin_qtl", "Select QTL for Violin Plot:", choices = NULL),
|
| 1040 |
+
plotOutput("violin_plot", height = "500px") %>% withSpinner()
|
| 1041 |
+
)
|
| 1042 |
),
|
| 1043 |
fluidRow(
|
| 1044 |
+
box(title = "Phenotype Correlation Heatmap", status = "warning", width = 12,
|
| 1045 |
+
plotOutput("correlation_plot", height = "500px") %>% withSpinner()
|
| 1046 |
+
)
|
| 1047 |
),
|
| 1048 |
fluidRow(
|
| 1049 |
+
box(title = "Marker Density Distribution", status = "info", width = 12,
|
| 1050 |
+
plotOutput("density_plot", height = "500px") %>% withSpinner()
|
| 1051 |
+
)
|
| 1052 |
)
|
| 1053 |
),
|
| 1054 |
|
| 1055 |
+
# Download Tab
|
| 1056 |
+
tabItem(tabName = "download",
|
| 1057 |
+
fluidRow(
|
| 1058 |
+
box(title = "Download Results", status = "primary", solidHeader = TRUE, width = 12,
|
| 1059 |
+
br(),
|
| 1060 |
+
h4("QTL Results"),
|
| 1061 |
+
p("Download the complete QTL table with all details including peak markers, flanking markers, LOD scores, PVE, and confidence intervals."),
|
| 1062 |
+
downloadButton("download_qtl_results", "Download QTL Results (CSV)",
|
| 1063 |
+
class = "btn-download", style = "margin-bottom: 20px;"),
|
| 1064 |
+
|
| 1065 |
+
hr(),
|
| 1066 |
+
|
| 1067 |
+
h4("Model Comparison"),
|
| 1068 |
+
p("Download the model comparison table showing performance of all 10 mapping methods."),
|
| 1069 |
+
downloadButton("download_model_comparison", "Download Model Comparison (CSV)",
|
| 1070 |
+
class = "btn-download", style = "margin-bottom: 20px;"),
|
| 1071 |
+
|
| 1072 |
+
hr(),
|
| 1073 |
+
|
| 1074 |
+
h4("Complete Analysis Report"),
|
| 1075 |
+
p("Download a comprehensive report including all results and analysis parameters."),
|
| 1076 |
+
downloadButton("download_full_report", "Download Full Report (CSV)",
|
| 1077 |
+
class = "btn-download", style = "margin-bottom: 20px;"),
|
| 1078 |
+
|
| 1079 |
+
hr(),
|
| 1080 |
+
|
| 1081 |
+
h4("All Plots"),
|
| 1082 |
+
p("Download all visualizations as a single PDF file."),
|
| 1083 |
+
downloadButton("download_all_plots", "Download All Plots (PDF)",
|
| 1084 |
+
class = "btn-download"),
|
| 1085 |
+
|
| 1086 |
+
br(), br()
|
| 1087 |
+
)
|
| 1088 |
+
)
|
| 1089 |
+
),
|
| 1090 |
+
|
| 1091 |
+
# Help Tab
|
| 1092 |
tabItem(tabName = "help",
|
| 1093 |
fluidRow(
|
| 1094 |
+
box(title = "Documentation", status = "primary", solidHeader = TRUE, width = 12,
|
| 1095 |
HTML("
|
| 1096 |
+
<h3>Complete QTL Analysis Pipeline Documentation</h3>
|
| 1097 |
+
|
| 1098 |
+
<h4>10 Mapping Models Included:</h4>
|
| 1099 |
+
<ol>
|
| 1100 |
+
<li><strong>Haley-Knott (HK)</strong> - Fast regression-based method</li>
|
| 1101 |
+
<li><strong>EM Algorithm</strong> - Expectation-maximization for missing data</li>
|
| 1102 |
+
<li><strong>Extended Haley-Knott (EHK)</strong> - Improved HK method</li>
|
| 1103 |
+
<li><strong>Multiple Imputation (IMP)</strong> - Handles missing genotypes</li>
|
| 1104 |
+
<li><strong>Marker Regression (MR)</strong> - Simple marker-by-marker regression</li>
|
| 1105 |
+
<li><strong>Non-parametric (NP)</strong> - Distribution-free method</li>
|
| 1106 |
+
<li><strong>Composite Interval Mapping (CIM)</strong> - Controls for background QTLs</li>
|
| 1107 |
+
<li><strong>Multiple QTL Mapping (MQM)</strong> - Uses cofactors</li>
|
| 1108 |
+
<li><strong>Binary Trait</strong> - For case/control studies</li>
|
| 1109 |
+
<li><strong>Normal Model</strong> - Assumes normal distribution</li>
|
| 1110 |
+
</ol>
|
| 1111 |
+
|
| 1112 |
+
<h4>QTL Results Include:</h4>
|
| 1113 |
+
<ul>
|
| 1114 |
+
<li>Peak Marker - The marker with highest LOD at QTL peak</li>
|
| 1115 |
+
<li>Left Flanking Marker - Left boundary of 1.5 LOD confidence interval</li>
|
| 1116 |
+
<li>Right Flanking Marker - Right boundary of 1.5 LOD confidence interval</li>
|
| 1117 |
+
<li>LOD Score - Logarithm of odds at peak</li>
|
| 1118 |
+
<li>PVE (Proportion of Variance Explained) - Effect size</li>
|
| 1119 |
+
<li>1.5 LOD and 2 LOD Confidence Intervals</li>
|
| 1120 |
+
<li>Additive and Dominance Effects (for F2 crosses)</li>
|
| 1121 |
+
</ul>
|
| 1122 |
+
|
| 1123 |
+
<h4>File Format Requirements:</h4>
|
| 1124 |
+
<p><strong>Genotype CSV:</strong></p>
|
| 1125 |
+
<pre>
|
| 1126 |
+
id,marker1,marker2,marker3,...
|
| 1127 |
+
,chr1,chr1,chr2,...
|
| 1128 |
+
,0,5.2,10.1,...
|
| 1129 |
+
sample1,1,2,1,...
|
| 1130 |
+
sample2,2,1,3,...</pre>
|
| 1131 |
+
|
| 1132 |
+
<p><strong>Phenotype CSV:</strong></p>
|
| 1133 |
+
<pre>
|
| 1134 |
+
id,trait1,trait2,trait3
|
| 1135 |
+
sample1,12.5,175,8.2
|
| 1136 |
+
sample2,13.2,180,8.5</pre>
|
| 1137 |
+
|
| 1138 |
+
<h4>Interpretation Guide:</h4>
|
| 1139 |
+
<ul>
|
| 1140 |
+
<li><strong>LOD > 3:</strong> Generally considered significant evidence for QTL</li>
|
| 1141 |
+
<li><strong>PVE:</strong> Higher values indicate larger effect QTLs</li>
|
| 1142 |
+
<li><strong>CI Width:</strong> Narrower intervals indicate more precise mapping</li>
|
| 1143 |
+
<li><strong>Additive Effect:</strong> The effect of replacing one allele with another</li>
|
| 1144 |
+
<li><strong>Dominance Effect:</strong> Deviation from additive model (F2 only)</li>
|
| 1145 |
+
</ul>
|
| 1146 |
+
")
|
| 1147 |
+
)
|
| 1148 |
)
|
| 1149 |
)
|
| 1150 |
)
|
|
|
|
| 1157 |
|
| 1158 |
server <- function(input, output, session) {
|
| 1159 |
|
| 1160 |
+
# Reactive values
|
| 1161 |
cross_data <- reactiveVal(NULL)
|
| 1162 |
all_models <- reactiveVal(NULL)
|
| 1163 |
qtl_results <- reactiveVal(NULL)
|
| 1164 |
+
permutation_results <- reactiveVal(NULL)
|
| 1165 |
+
model_comparison <- reactiveVal(NULL)
|
| 1166 |
+
analysis_complete <- reactiveVal(FALSE)
|
| 1167 |
+
model_status_text <- reactiveVal("No models run yet")
|
| 1168 |
|
| 1169 |
# Value boxes
|
| 1170 |
output$n_individuals <- renderValueBox({
|
|
|
|
| 1185 |
icon = icon("chart-line"), color = "yellow")
|
| 1186 |
})
|
| 1187 |
|
| 1188 |
+
# Load data
|
| 1189 |
observeEvent(c(input$geno_file, input$pheno_file), {
|
| 1190 |
req(input$geno_file, input$pheno_file)
|
| 1191 |
|
| 1192 |
+
safe_notify("Loading data files...", "message")
|
| 1193 |
|
| 1194 |
tryCatch({
|
| 1195 |
+
cross <- parse_advanced_cross(
|
| 1196 |
input$geno_file$datapath,
|
| 1197 |
input$pheno_file$datapath,
|
| 1198 |
input$cross_type
|
| 1199 |
)
|
| 1200 |
|
| 1201 |
cross_data(cross)
|
| 1202 |
+
|
| 1203 |
updateSelectInput(session, "pheno_trait",
|
| 1204 |
choices = names(cross$pheno),
|
| 1205 |
selected = names(cross$pheno)[1])
|
| 1206 |
|
| 1207 |
+
safe_notify(paste("Loaded:", nind(cross), "individuals,",
|
| 1208 |
+
totmar(cross), "markers"), "success")
|
| 1209 |
|
| 1210 |
}, error = function(e) {
|
| 1211 |
+
safe_notify(paste("Error:", e$message), "error")
|
| 1212 |
})
|
| 1213 |
})
|
| 1214 |
|
| 1215 |
+
# Run all 10 models
|
| 1216 |
observeEvent(input$run_analysis, {
|
| 1217 |
cross <- cross_data()
|
| 1218 |
req(cross, input$pheno_trait)
|
| 1219 |
|
| 1220 |
+
safe_notify("Starting analysis with 10 models...", "message")
|
| 1221 |
|
| 1222 |
+
withProgress(message = "Running 10 QTL Mapping Models", value = 0, {
|
| 1223 |
|
| 1224 |
+
incProgress(0.1, detail = "Preparing data...")
|
| 1225 |
pheno_idx <- which(names(cross$pheno) == input$pheno_trait)
|
| 1226 |
+
|
| 1227 |
+
incProgress(0.3, detail = "Running all 10 models...")
|
| 1228 |
+
models <- run_all_ten_models(cross, pheno_idx)
|
| 1229 |
all_models(models)
|
| 1230 |
|
| 1231 |
+
# Get model status
|
| 1232 |
+
status <- attr(models, "status")
|
| 1233 |
+
status_msg <- paste(names(status), ":", unlist(status), collapse = "\n")
|
| 1234 |
+
model_status_text(status_msg)
|
| 1235 |
+
|
| 1236 |
+
incProgress(0.2, detail = "Detecting QTLs...")
|
| 1237 |
+
# Use HK model for QTL detection (best overall)
|
| 1238 |
if (!is.null(models$hk)) {
|
| 1239 |
+
qtls <- detect_comprehensive_qtls(models$hk, input$lod_threshold,
|
| 1240 |
+
cross, input$pheno_trait)
|
| 1241 |
qtl_results(qtls)
|
| 1242 |
|
| 1243 |
if (nrow(qtls) > 0) {
|
| 1244 |
+
qtl_choices <- paste0("Chr", qtls$Chromosome, "_",
|
| 1245 |
+
qtls$Peak_Position_cM, "cM_LOD", qtls$LOD_Score)
|
| 1246 |
+
updateSelectInput(session, "violin_qtl", choices = qtl_choices)
|
| 1247 |
}
|
| 1248 |
}
|
| 1249 |
|
| 1250 |
+
incProgress(0.2, detail = "Comparing models...")
|
| 1251 |
+
comp <- create_model_comparison(models, input$lod_threshold)
|
| 1252 |
+
model_comparison(comp)
|
| 1253 |
+
|
| 1254 |
if (input$n_permutations > 0 && !is.null(models$hk)) {
|
| 1255 |
+
incProgress(0.2, detail = paste(input$n_permutations, "permutations..."))
|
| 1256 |
+
perms <- run_permutation_analysis(cross, pheno_idx, input$n_permutations)
|
| 1257 |
+
permutation_results(perms)
|
|
|
|
| 1258 |
}
|
| 1259 |
|
| 1260 |
+
analysis_complete(TRUE)
|
| 1261 |
+
|
| 1262 |
n_success <- sum(!sapply(models, is.null))
|
| 1263 |
+
safe_notify(paste("Complete!", n_success, "models succeeded"), "success")
|
| 1264 |
})
|
| 1265 |
})
|
| 1266 |
|
| 1267 |
+
# Model status output
|
| 1268 |
+
output$model_status <- renderPrint({
|
| 1269 |
+
cat(model_status_text())
|
| 1270 |
+
})
|
| 1271 |
+
|
| 1272 |
+
# Data summary
|
| 1273 |
output$data_summary <- renderPrint({
|
| 1274 |
cross <- cross_data()
|
| 1275 |
if (is.null(cross)) {
|
| 1276 |
+
cat("No data loaded. Please upload CSV files.")
|
| 1277 |
} else {
|
| 1278 |
+
cat("=== QTL Data Summary ===\n\n")
|
| 1279 |
cat("Cross Type:", class(cross)[1], "\n")
|
| 1280 |
cat("Individuals:", nind(cross), "\n")
|
| 1281 |
cat("Markers:", totmar(cross), "\n")
|
| 1282 |
+
cat("Chromosomes:", nchr(cross), "\n\n")
|
| 1283 |
+
cat("Phenotypes:", paste(names(cross$pheno), collapse = ", "), "\n\n")
|
| 1284 |
+
|
| 1285 |
+
missing_total <- sum(is.na(pull.geno(cross))) / (nind(cross) * totmar(cross)) * 100
|
| 1286 |
+
cat("Missing Data Rate:", round(missing_total, 2), "%\n")
|
| 1287 |
+
cat("Heterozygosity:", round(mean(calc.heterozyg(cross), na.rm=TRUE), 4), "\n")
|
| 1288 |
}
|
| 1289 |
})
|
| 1290 |
|
| 1291 |
output$analysis_status <- renderPrint({
|
| 1292 |
+
if (!analysis_complete()) {
|
| 1293 |
+
cat("Analysis not run. Click 'RUN ALL 10 MODELS' to start.")
|
| 1294 |
} else {
|
| 1295 |
models <- all_models()
|
| 1296 |
n_success <- sum(!sapply(models, is.null))
|
| 1297 |
+
qtls <- qtl_results()
|
| 1298 |
+
|
| 1299 |
+
cat("=== Analysis Complete ===\n\n")
|
| 1300 |
+
cat("Models succeeded:", n_success, "/10\n")
|
| 1301 |
+
cat("QTLs detected:", ifelse(is.null(qtls), 0, nrow(qtls)), "\n")
|
| 1302 |
+
cat("LOD threshold:", input$lod_threshold, "\n")
|
| 1303 |
+
cat("Permutations:", input$n_permutations, "\n\n")
|
| 1304 |
+
|
| 1305 |
+
if (!is.null(qtls) && nrow(qtls) > 0) {
|
| 1306 |
+
cat("Top QTL:\n")
|
| 1307 |
+
top_qtl <- qtls[which.max(qtls$LOD_Score), ]
|
| 1308 |
+
cat(" Chromosome:", top_qtl$Chromosome, "\n")
|
| 1309 |
+
cat(" Position:", top_qtl$Peak_Position_cM, "cM\n")
|
| 1310 |
+
cat(" LOD:", top_qtl$LOD_Score, "\n")
|
| 1311 |
+
cat(" PVE:", top_qtl$PVE_Percent, "%\n")
|
| 1312 |
+
}
|
| 1313 |
}
|
| 1314 |
})
|
| 1315 |
|
| 1316 |
+
# Model comparison table
|
| 1317 |
+
output$model_comparison_table <- renderDT({
|
| 1318 |
+
comp <- model_comparison()
|
| 1319 |
+
req(comp, nrow(comp) > 0)
|
| 1320 |
+
|
| 1321 |
+
datatable(comp, options = list(pageLength = 10, scrollX = TRUE), rownames = FALSE) %>%
|
| 1322 |
+
formatStyle("Max_LOD", background = styleColorBar(c(0, max(comp$Max_LOD)), "lightblue"))
|
| 1323 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1324 |
|
| 1325 |
+
# QTL results table with all details
|
| 1326 |
+
output$qtl_table <- renderDT({
|
| 1327 |
qtls <- qtl_results()
|
| 1328 |
+
req(qtls, nrow(qtls) > 0)
|
| 1329 |
+
|
| 1330 |
+
datatable(qtls,
|
| 1331 |
+
options = list(pageLength = 15, scrollX = TRUE,
|
| 1332 |
+
dom = 'Bfrtip',
|
| 1333 |
+
buttons = c('copy', 'csv', 'excel', 'pdf')),
|
| 1334 |
+
rownames = FALSE,
|
| 1335 |
+
class = 'display nowrap') %>%
|
| 1336 |
+
formatStyle("LOD_Score",
|
| 1337 |
+
background = styleColorBar(c(0, max(qtls$LOD_Score)), "lightblue")) %>%
|
| 1338 |
+
formatStyle("PVE_Percent",
|
| 1339 |
+
background = styleColorBar(c(0, max(qtls$PVE_Percent)), "lightgreen")) %>%
|
| 1340 |
+
formatStyle("CI_1.5LOD_Width_cM",
|
| 1341 |
+
background = styleColorBar(c(0, max(qtls$CI_1.5LOD_Width_cM)), "lightcoral"))
|
| 1342 |
+
})
|
| 1343 |
+
|
| 1344 |
+
# Permutation table
|
| 1345 |
+
output$permutation_table <- renderDT({
|
| 1346 |
+
perms <- permutation_results()
|
| 1347 |
+
req(perms)
|
| 1348 |
+
|
| 1349 |
+
datatable(perms$thresholds,
|
| 1350 |
+
options = list(pageLength = 5, dom = 't'),
|
| 1351 |
+
rownames = FALSE) %>%
|
| 1352 |
+
formatStyle("LOD_Threshold",
|
| 1353 |
+
background = styleColorBar(c(0, max(perms$thresholds$LOD_Threshold)), "lightyellow"))
|
| 1354 |
+
})
|
| 1355 |
+
|
| 1356 |
+
# All plots
|
| 1357 |
+
output$genetic_map <- renderPlot({
|
| 1358 |
cross <- cross_data()
|
| 1359 |
+
req(cross)
|
| 1360 |
+
plot_genetic_density(cross)
|
| 1361 |
+
})
|
| 1362 |
+
|
| 1363 |
+
output$missing_plot <- renderPlot({
|
| 1364 |
+
cross <- cross_data()
|
| 1365 |
+
req(cross)
|
| 1366 |
+
plot_missing_data(cross)
|
| 1367 |
+
})
|
| 1368 |
+
|
| 1369 |
+
output$phenotype_dist <- renderPlot({
|
| 1370 |
+
cross <- cross_data()
|
| 1371 |
+
req(cross, input$pheno_trait)
|
| 1372 |
|
| 1373 |
+
pheno <- cross$pheno[[input$pheno_trait]]
|
| 1374 |
+
df <- data.frame(Phenotype = pheno[!is.na(pheno)])
|
| 1375 |
+
|
| 1376 |
+
p1 <- ggplot(df, aes(x = Phenotype)) +
|
| 1377 |
+
geom_histogram(aes(y = after_stat(density)), fill = "steelblue", alpha = 0.7, bins = 30) +
|
| 1378 |
+
geom_density(color = "red", size = 1) +
|
| 1379 |
+
labs(title = paste(input$pheno_trait, "- Distribution"), x = "Value", y = "Density") +
|
| 1380 |
+
theme_minimal()
|
| 1381 |
+
|
| 1382 |
+
p2 <- ggplot(df, aes(sample = Phenotype)) +
|
| 1383 |
+
stat_qq(color = "steelblue", alpha = 0.7) +
|
| 1384 |
+
stat_qq_line(color = "red", size = 1) +
|
| 1385 |
+
labs(title = "Q-Q Plot", x = "Theoretical", y = "Sample") +
|
| 1386 |
+
theme_minimal()
|
| 1387 |
|
| 1388 |
+
plot_grid(p1, p2, ncol = 2)
|
| 1389 |
})
|
| 1390 |
|
| 1391 |
+
output$manhattan_plot <- renderPlot({
|
| 1392 |
+
models <- all_models()
|
| 1393 |
+
qtls <- qtl_results()
|
| 1394 |
+
req(models, !is.null(models$hk))
|
| 1395 |
+
plot_manhattan_enhanced(models$hk, qtls, input$lod_threshold)
|
| 1396 |
+
})
|
| 1397 |
+
|
| 1398 |
+
output$surface_3d <- renderPlotly({
|
| 1399 |
+
models <- all_models()
|
| 1400 |
+
req(models, !is.null(models$hk))
|
| 1401 |
+
plot_3d_surface(models$hk)
|
| 1402 |
+
})
|
| 1403 |
+
|
| 1404 |
+
output$contour_plot <- renderPlot({
|
| 1405 |
+
models <- all_models()
|
| 1406 |
+
req(models, !is.null(models$hk))
|
| 1407 |
+
plot_lod_contour(models$hk)
|
| 1408 |
+
})
|
| 1409 |
+
|
| 1410 |
+
output$ridge_plot <- renderPlot({
|
| 1411 |
+
models <- all_models()
|
| 1412 |
+
req(models, !is.null(models$hk))
|
| 1413 |
+
plot_ridge_distribution(models$hk)
|
| 1414 |
+
})
|
| 1415 |
+
|
| 1416 |
+
output$bubble_plot <- renderPlot({
|
| 1417 |
qtls <- qtl_results()
|
| 1418 |
req(qtls, nrow(qtls) > 0)
|
| 1419 |
+
plot_qtl_bubble(qtls)
|
| 1420 |
})
|
| 1421 |
|
| 1422 |
+
output$chromosome_heatmap <- renderPlot({
|
| 1423 |
models <- all_models()
|
| 1424 |
+
req(models, !is.null(models$hk))
|
| 1425 |
+
plot_chromosome_heatmap(models$hk)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1426 |
})
|
| 1427 |
|
| 1428 |
+
output$violin_plot <- renderPlot({
|
| 1429 |
+
req(input$violin_qtl, input$violin_qtl != "")
|
| 1430 |
+
qtls <- qtl_results()
|
| 1431 |
+
cross <- cross_data()
|
| 1432 |
+
req(qtls, nrow(qtls) > 0, cross, input$pheno_trait)
|
| 1433 |
|
| 1434 |
+
qtl_idx <- which(paste0("Chr", qtls$Chromosome, "_",
|
| 1435 |
+
qtls$Peak_Position_cM, "cM_LOD", qtls$LOD_Score) == input$violin_qtl)
|
| 1436 |
+
if (length(qtl_idx) == 0) return(NULL)
|
| 1437 |
+
|
| 1438 |
+
plot_genotype_violin(cross, qtls[qtl_idx[1], , drop = FALSE], input$pheno_trait)
|
| 1439 |
+
})
|
| 1440 |
+
|
| 1441 |
+
output$correlation_plot <- renderPlot({
|
| 1442 |
+
cross <- cross_data()
|
| 1443 |
+
req(cross)
|
| 1444 |
+
plot_phenotype_correlation(cross)
|
| 1445 |
+
})
|
| 1446 |
+
|
| 1447 |
+
output$density_plot <- renderPlot({
|
| 1448 |
+
cross <- cross_data()
|
| 1449 |
+
req(cross)
|
| 1450 |
+
plot_genetic_density(cross)
|
| 1451 |
+
})
|
| 1452 |
+
|
| 1453 |
+
output$model_barplot <- renderPlot({
|
| 1454 |
+
comp <- model_comparison()
|
| 1455 |
+
req(comp, nrow(comp) > 0)
|
| 1456 |
+
plot_model_comparison(comp)
|
| 1457 |
+
})
|
| 1458 |
+
|
| 1459 |
+
output$parallel_plot <- renderPlot({
|
| 1460 |
+
models <- all_models()
|
| 1461 |
+
req(models, length(models) > 0)
|
| 1462 |
+
plot_parallel_coords(models)
|
| 1463 |
+
})
|
| 1464 |
+
|
| 1465 |
+
output$qq_plot <- renderPlot({
|
| 1466 |
+
models <- all_models()
|
| 1467 |
+
req(models, !is.null(models$hk))
|
| 1468 |
+
plot_qq_plot(models$hk)
|
| 1469 |
+
})
|
| 1470 |
+
|
| 1471 |
+
output$qtl_intervals <- renderPlot({
|
| 1472 |
+
qtls <- qtl_results()
|
| 1473 |
+
req(qtls, nrow(qtls) > 0)
|
| 1474 |
+
plot_qtl_intervals(qtls)
|
| 1475 |
})
|
| 1476 |
|
| 1477 |
+
output$pve_distribution <- renderPlot({
|
| 1478 |
+
qtls <- qtl_results()
|
| 1479 |
+
req(qtls, nrow(qtls) > 0)
|
| 1480 |
+
plot_pve_distribution(qtls)
|
| 1481 |
+
})
|
| 1482 |
+
|
| 1483 |
+
# Download handlers - WORKING
|
| 1484 |
+
output$download_qtl_results <- downloadHandler(
|
| 1485 |
filename = function() paste0("qtl_results_", Sys.Date(), ".csv"),
|
| 1486 |
content = function(file) {
|
| 1487 |
qtls <- qtl_results()
|
| 1488 |
if (!is.null(qtls) && nrow(qtls) > 0) {
|
| 1489 |
write.csv(qtls, file, row.names = FALSE)
|
| 1490 |
} else {
|
| 1491 |
+
write.csv(data.frame(Message = "No significant QTLs found. Try lowering LOD threshold."),
|
| 1492 |
+
file, row.names = FALSE)
|
| 1493 |
+
}
|
| 1494 |
+
}
|
| 1495 |
+
)
|
| 1496 |
+
|
| 1497 |
+
output$download_model_comparison <- downloadHandler(
|
| 1498 |
+
filename = function() paste0("model_comparison_", Sys.Date(), ".csv"),
|
| 1499 |
+
content = function(file) {
|
| 1500 |
+
comp <- model_comparison()
|
| 1501 |
+
if (!is.null(comp) && nrow(comp) > 0) {
|
| 1502 |
+
write.csv(comp, file, row.names = FALSE)
|
| 1503 |
+
} else {
|
| 1504 |
+
write.csv(data.frame(Message = "Run analysis first to get model comparison."),
|
| 1505 |
+
file, row.names = FALSE)
|
| 1506 |
+
}
|
| 1507 |
+
}
|
| 1508 |
+
)
|
| 1509 |
+
|
| 1510 |
+
output$download_full_report <- downloadHandler(
|
| 1511 |
+
filename = function() paste0("full_report_", Sys.Date(), ".csv"),
|
| 1512 |
+
content = function(file) {
|
| 1513 |
+
qtls <- qtl_results()
|
| 1514 |
+
comp <- model_comparison()
|
| 1515 |
+
perms <- permutation_results()
|
| 1516 |
+
|
| 1517 |
+
# Create comprehensive report
|
| 1518 |
+
report_lines <- c()
|
| 1519 |
+
report_lines <- c(report_lines, "# QTL Analysis Full Report")
|
| 1520 |
+
report_lines <- c(report_lines, paste("# Generated:", Sys.time()))
|
| 1521 |
+
report_lines <- c(report_lines, "")
|
| 1522 |
+
report_lines <- c(report_lines, "## Analysis Parameters")
|
| 1523 |
+
report_lines <- c(report_lines, paste("Cross Type:", input$cross_type))
|
| 1524 |
+
report_lines <- c(report_lines, paste("Phenotype Trait:", input$pheno_trait))
|
| 1525 |
+
report_lines <- c(report_lines, paste("LOD Threshold:", input$lod_threshold))
|
| 1526 |
+
report_lines <- c(report_lines, paste("Permutations:", input$n_permutations))
|
| 1527 |
+
report_lines <- c(report_lines, "")
|
| 1528 |
+
|
| 1529 |
+
if (!is.null(comp) && nrow(comp) > 0) {
|
| 1530 |
+
report_lines <- c(report_lines, "## Model Comparison")
|
| 1531 |
+
report_lines <- c(report_lines, capture.output(write.csv(comp, row.names = FALSE)))
|
| 1532 |
+
report_lines <- c(report_lines, "")
|
| 1533 |
}
|
| 1534 |
+
|
| 1535 |
+
if (!is.null(qtls) && nrow(qtls) > 0) {
|
| 1536 |
+
report_lines <- c(report_lines, "## Significant QTLs")
|
| 1537 |
+
report_lines <- c(report_lines, capture.output(write.csv(qtls, row.names = FALSE)))
|
| 1538 |
+
report_lines <- c(report_lines, "")
|
| 1539 |
+
}
|
| 1540 |
+
|
| 1541 |
+
if (!is.null(perms)) {
|
| 1542 |
+
report_lines <- c(report_lines, "## Permutation Thresholds")
|
| 1543 |
+
report_lines <- c(report_lines, capture.output(write.csv(perms$thresholds, row.names = FALSE)))
|
| 1544 |
+
}
|
| 1545 |
+
|
| 1546 |
+
writeLines(report_lines, file)
|
| 1547 |
+
}
|
| 1548 |
+
)
|
| 1549 |
+
|
| 1550 |
+
output$download_all_plots <- downloadHandler(
|
| 1551 |
+
filename = function() paste0("all_plots_", Sys.Date(), ".pdf"),
|
| 1552 |
+
content = function(file) {
|
| 1553 |
+
pdf(file, width = 12, height = 8)
|
| 1554 |
+
|
| 1555 |
+
# Generate and save each plot
|
| 1556 |
+
models <- all_models()
|
| 1557 |
+
qtls <- qtl_results()
|
| 1558 |
+
cross <- cross_data()
|
| 1559 |
+
comp <- model_comparison()
|
| 1560 |
+
|
| 1561 |
+
if (!is.null(cross)) {
|
| 1562 |
+
print(plot_genetic_density(cross))
|
| 1563 |
+
print(plot_missing_data(cross))
|
| 1564 |
+
}
|
| 1565 |
+
|
| 1566 |
+
if (!is.null(models) && !is.null(models$hk)) {
|
| 1567 |
+
print(plot_manhattan_enhanced(models$hk, qtls, input$lod_threshold))
|
| 1568 |
+
print(plot_lod_contour(models$hk))
|
| 1569 |
+
print(plot_ridge_distribution(models$hk))
|
| 1570 |
+
print(plot_qq_plot(models$hk))
|
| 1571 |
+
print(plot_chromosome_heatmap(models$hk))
|
| 1572 |
+
}
|
| 1573 |
+
|
| 1574 |
+
if (!is.null(qtls) && nrow(qtls) > 0) {
|
| 1575 |
+
print(plot_qtl_bubble(qtls))
|
| 1576 |
+
print(plot_qtl_intervals(qtls))
|
| 1577 |
+
print(plot_pve_distribution(qtls))
|
| 1578 |
+
}
|
| 1579 |
+
|
| 1580 |
+
if (!is.null(comp) && nrow(comp) > 0) {
|
| 1581 |
+
print(plot_model_comparison(comp))
|
| 1582 |
+
}
|
| 1583 |
+
|
| 1584 |
+
if (!is.null(cross) && ncol(cross$pheno) >= 2) {
|
| 1585 |
+
plot_phenotype_correlation(cross)
|
| 1586 |
+
}
|
| 1587 |
+
|
| 1588 |
+
dev.off()
|
| 1589 |
}
|
| 1590 |
)
|
| 1591 |
}
|