Spaces:
Sleeping
Sleeping
Update app.R
Browse files
app.R
CHANGED
|
@@ -1,9 +1,10 @@
|
|
| 1 |
# ============================================================================
|
| 2 |
-
#
|
| 3 |
-
#
|
|
|
|
| 4 |
# ============================================================================
|
| 5 |
|
| 6 |
-
# Load libraries
|
| 7 |
suppressPackageStartupMessages({
|
| 8 |
library(shiny)
|
| 9 |
library(shinydashboard)
|
|
@@ -18,21 +19,11 @@ suppressPackageStartupMessages({
|
|
| 18 |
library(RColorBrewer)
|
| 19 |
library(ggrepel)
|
| 20 |
library(cowplot)
|
| 21 |
-
library(pheatmap)
|
| 22 |
library(viridis)
|
| 23 |
library(ggridges)
|
| 24 |
-
library(
|
| 25 |
-
library(foreach)
|
| 26 |
-
library(doParallel)
|
| 27 |
-
library(igraph)
|
| 28 |
-
library(data.table)
|
| 29 |
-
})
|
| 30 |
-
|
| 31 |
-
# Check for corrplot
|
| 32 |
-
if (!require("corrplot", quietly = TRUE)) {
|
| 33 |
-
install.packages("corrplot", repos = "https://cloud.r-project.org/")
|
| 34 |
library(corrplot)
|
| 35 |
-
}
|
| 36 |
|
| 37 |
# ============================================================================
|
| 38 |
# DATA PARSER
|
|
@@ -40,103 +31,71 @@ if (!require("corrplot", quietly = TRUE)) {
|
|
| 40 |
|
| 41 |
parse_custom_cross <- function(geno_path, pheno_path, cross_type = "f2") {
|
| 42 |
|
| 43 |
-
# Read genotype file
|
| 44 |
geno_raw <- read.csv(geno_path, header = FALSE, stringsAsFactors = FALSE,
|
| 45 |
check.names = FALSE, na.strings = c("NA", "", "N/A", "-9", "0"))
|
| 46 |
|
| 47 |
if (nrow(geno_raw) < 4) {
|
| 48 |
-
stop("Genotype file must have at least 4 rows
|
| 49 |
}
|
| 50 |
|
| 51 |
-
# Parse header rows
|
| 52 |
col_names <- as.character(geno_raw[1, ])
|
| 53 |
chr_row <- as.character(geno_raw[2, ])
|
| 54 |
pos_row <- as.character(geno_raw[3, ])
|
| 55 |
ind_data <- geno_raw[4:nrow(geno_raw), , drop = FALSE]
|
| 56 |
|
| 57 |
-
# Identify marker columns
|
| 58 |
marker_cols <- 2:ncol(geno_raw)
|
| 59 |
-
if (length(marker_cols) == 0) {
|
| 60 |
-
stop("No marker columns found in genotype file")
|
| 61 |
-
}
|
| 62 |
-
|
| 63 |
marker_names <- col_names[marker_cols]
|
| 64 |
|
| 65 |
-
# Clean chromosome names
|
| 66 |
chromosomes <- chr_row[marker_cols]
|
| 67 |
chromosomes <- gsub("^[Cc]hr", "", chromosomes)
|
| 68 |
-
chromosomes <- gsub("^0+", "", chromosomes)
|
| 69 |
chromosomes[chromosomes == ""] <- "unknown"
|
| 70 |
-
chromosomes[is.na(chromosomes)] <- "unknown"
|
| 71 |
|
| 72 |
-
# Parse positions
|
| 73 |
positions <- suppressWarnings(as.numeric(pos_row[marker_cols]))
|
| 74 |
positions[is.na(positions)] <- 0
|
| 75 |
|
| 76 |
-
# Individual IDs
|
| 77 |
ind_ids <- as.character(ind_data[, 1])
|
| 78 |
-
if (any(duplicated(ind_ids)))
|
| 79 |
-
ind_ids <- make.unique(ind_ids)
|
| 80 |
-
}
|
| 81 |
|
| 82 |
-
# Convert genotype matrix
|
| 83 |
geno_mat <- as.matrix(ind_data[, marker_cols, drop = FALSE])
|
| 84 |
mode(geno_mat) <- "numeric"
|
| 85 |
rownames(geno_mat) <- ind_ids
|
| 86 |
colnames(geno_mat) <- marker_names
|
| 87 |
|
| 88 |
-
#
|
| 89 |
missing_rate <- rowMeans(is.na(geno_mat))
|
| 90 |
valid_inds <- missing_rate <= 0.5
|
| 91 |
-
if (sum(valid_inds) == 0) {
|
| 92 |
-
stop("No individuals with less than 50% missing data")
|
| 93 |
-
}
|
| 94 |
geno_mat <- geno_mat[valid_inds, , drop = FALSE]
|
| 95 |
ind_ids <- ind_ids[valid_inds]
|
| 96 |
|
| 97 |
-
# Read
|
| 98 |
pheno_raw <- read.csv(pheno_path, header = TRUE, stringsAsFactors = FALSE,
|
| 99 |
check.names = FALSE, na.strings = c("NA", "", "N/A"))
|
| 100 |
|
| 101 |
-
if (ncol(pheno_raw) < 2) {
|
| 102 |
-
stop("Phenotype file must have at least 2 columns (ID and one trait)")
|
| 103 |
-
}
|
| 104 |
-
|
| 105 |
pheno_ids <- as.character(pheno_raw[, 1])
|
| 106 |
-
if (any(duplicated(pheno_ids)))
|
| 107 |
-
pheno_ids <- make.unique(pheno_ids)
|
| 108 |
-
}
|
| 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
|
| 118 |
common_ids <- intersect(ind_ids, pheno_ids)
|
| 119 |
-
if (length(common_ids) == 0)
|
| 120 |
-
stop(paste("No matching individual IDs. Genotype IDs:",
|
| 121 |
-
paste(head(ind_ids, 3), collapse=", "),
|
| 122 |
-
"Phenotype IDs:", paste(head(pheno_ids, 3), collapse=", ")))
|
| 123 |
-
}
|
| 124 |
|
| 125 |
geno_mat <- geno_mat[common_ids, , drop = FALSE]
|
| 126 |
pheno_df <- pheno_df[common_ids, , drop = FALSE]
|
| 127 |
|
| 128 |
-
#
|
| 129 |
marker_missing <- colMeans(is.na(geno_mat))
|
| 130 |
valid_markers <- marker_missing <= 0.5
|
| 131 |
-
if (sum(valid_markers) == 0) {
|
| 132 |
-
stop("No markers with less than 50% missing data")
|
| 133 |
-
}
|
| 134 |
geno_mat <- geno_mat[, valid_markers, drop = FALSE]
|
| 135 |
chromosomes <- chromosomes[valid_markers]
|
| 136 |
positions <- positions[valid_markers]
|
| 137 |
marker_names <- marker_names[valid_markers]
|
| 138 |
|
| 139 |
-
# Build
|
| 140 |
unique_chrs <- unique(chromosomes)
|
| 141 |
chr_numeric <- suppressWarnings(as.numeric(unique_chrs))
|
| 142 |
chr_numeric[is.na(chr_numeric)] <- 999
|
|
@@ -152,16 +111,13 @@ parse_custom_cross <- function(geno_path, pheno_path, cross_type = "f2") {
|
|
| 152 |
chr_positions <- positions[chr_idx]
|
| 153 |
names(chr_positions) <- chr_markers
|
| 154 |
|
| 155 |
-
# Sort by position
|
| 156 |
ord <- order(chr_positions)
|
| 157 |
chr_positions <- chr_positions[ord]
|
| 158 |
chr_markers <- chr_markers[ord]
|
| 159 |
-
|
| 160 |
-
# Remove duplicate positions
|
| 161 |
chr_positions <- chr_positions[!duplicated(chr_positions)]
|
| 162 |
chr_markers <- chr_markers[!duplicated(chr_markers)]
|
| 163 |
|
| 164 |
-
if (length(chr_positions) >= 1) {
|
| 165 |
map_list[[ch]] <- chr_positions
|
| 166 |
geno_list[[ch]] <- list(
|
| 167 |
data = geno_mat[, chr_markers, drop = FALSE],
|
|
@@ -172,374 +128,400 @@ parse_custom_cross <- function(geno_path, pheno_path, cross_type = "f2") {
|
|
| 172 |
}
|
| 173 |
}
|
| 174 |
|
| 175 |
-
if (length(geno_list) == 0) {
|
| 176 |
-
stop("No valid chromosomes found")
|
| 177 |
-
}
|
| 178 |
-
|
| 179 |
-
# Create cross object
|
| 180 |
cross <- list(geno = geno_list, pheno = pheno_df)
|
| 181 |
class(cross) <- c(cross_type, "cross")
|
| 182 |
-
|
| 183 |
-
# Run basic checks
|
| 184 |
cross <- jittermap(cross)
|
| 185 |
|
| 186 |
return(cross)
|
| 187 |
}
|
| 188 |
|
| 189 |
# ============================================================================
|
| 190 |
-
#
|
| 191 |
# ============================================================================
|
| 192 |
|
| 193 |
-
|
|
|
|
|
|
|
|
|
|
| 194 |
tryCatch({
|
| 195 |
-
scanone(cross, pheno.col = pheno_col, method =
|
| 196 |
-
}, error = function(e)
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
})
|
| 200 |
-
}
|
| 201 |
-
|
| 202 |
-
run_permutations <- function(cross, pheno_col, n_perm = 100, method = "hk") {
|
| 203 |
tryCatch({
|
| 204 |
-
scanone(cross, pheno.col = pheno_col,
|
| 205 |
-
}, error = function(e)
|
| 206 |
-
message(paste("Permutation failed:", e$message))
|
| 207 |
-
return(NULL)
|
| 208 |
-
})
|
| 209 |
-
}
|
| 210 |
-
|
| 211 |
-
find_qtls <- function(scan, threshold) {
|
| 212 |
-
if (is.null(scan) || nrow(scan) == 0) {
|
| 213 |
-
return(data.frame())
|
| 214 |
-
}
|
| 215 |
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
}
|
| 220 |
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
}
|
| 249 |
-
|
| 250 |
-
# Calculate PVE
|
| 251 |
-
n <- nrow(scan)
|
| 252 |
-
pve <- (1 - 10^(-2 * peak_row$lod / n)) * 100
|
| 253 |
-
|
| 254 |
-
qtls <- rbind(qtls, data.frame(
|
| 255 |
-
Chromosome = as.character(chr),
|
| 256 |
-
Position_cM = round(peak_row$pos, 2),
|
| 257 |
-
LOD = round(peak_row$lod, 3),
|
| 258 |
-
PVE_percent = round(pve, 2),
|
| 259 |
-
CI_left = round(left_pos, 2),
|
| 260 |
-
CI_right = round(right_pos, 2),
|
| 261 |
-
CI_width = round(right_pos - left_pos, 2),
|
| 262 |
-
Marker = rownames(peak_row),
|
| 263 |
-
stringsAsFactors = FALSE
|
| 264 |
-
))
|
| 265 |
-
}
|
| 266 |
}
|
| 267 |
-
}
|
| 268 |
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
|
|
|
|
|
|
| 276 |
|
| 277 |
-
return(
|
| 278 |
}
|
| 279 |
|
| 280 |
# ============================================================================
|
| 281 |
-
#
|
| 282 |
# ============================================================================
|
| 283 |
|
| 284 |
-
# 1
|
| 285 |
-
|
| 286 |
-
if (is.null(
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
chr_midpoints <- sapply(chrs, function(ch) mean(df$pos_cum[df$chr == ch], na.rm = TRUE))
|
| 300 |
|
| 301 |
-
|
| 302 |
-
colors <- rep(brewer.pal(min(8, n_chrs), "Set1"), length.out = n_chrs)
|
| 303 |
-
names(colors) <- chrs
|
| 304 |
|
| 305 |
-
|
| 306 |
-
|
| 307 |
geom_hline(yintercept = threshold, color = "red", linetype = "dashed", size = 1) +
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
labs(title = "
|
| 311 |
-
x = "
|
| 312 |
theme_minimal() +
|
| 313 |
-
theme(legend.position = "
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
}
|
| 324 |
|
| 325 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 326 |
}
|
| 327 |
|
| 328 |
-
#
|
| 329 |
-
|
| 330 |
if (is.null(scan)) return(NULL)
|
| 331 |
|
| 332 |
df <- as.data.frame(scan)
|
| 333 |
-
df$
|
| 334 |
-
|
| 335 |
-
df
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
type = "scatter", mode = "markers",
|
| 344 |
-
marker = list(size = 5, opacity = 0.7)) %>%
|
| 345 |
-
layout(title = "Interactive Manhattan Plot",
|
| 346 |
-
xaxis = list(title = "Position (cM)"),
|
| 347 |
-
yaxis = list(title = "LOD Score"),
|
| 348 |
-
showlegend = TRUE) %>%
|
| 349 |
-
add_lines(x = ~pos, y = threshold, line = list(color = "orange", dash = "dash"),
|
| 350 |
-
name = "Threshold", showlegend = TRUE)
|
| 351 |
-
}
|
| 352 |
-
|
| 353 |
-
# 3. Phenotype Distribution
|
| 354 |
-
create_phenotype_plot <- function(cross, trait) {
|
| 355 |
-
if (is.null(cross) || is.null(trait) || !trait %in% names(cross$pheno)) {
|
| 356 |
-
return(NULL)
|
| 357 |
-
}
|
| 358 |
-
|
| 359 |
-
pheno <- cross$pheno[[trait]]
|
| 360 |
-
pheno <- pheno[!is.na(pheno)]
|
| 361 |
-
|
| 362 |
-
if (length(pheno) == 0) return(NULL)
|
| 363 |
-
|
| 364 |
-
df <- data.frame(value = pheno)
|
| 365 |
-
|
| 366 |
-
p1 <- ggplot(df, aes(x = value)) +
|
| 367 |
-
geom_histogram(aes(y = after_stat(density)), fill = "steelblue", alpha = 0.7, bins = 30) +
|
| 368 |
-
geom_density(color = "darkorange", size = 1) +
|
| 369 |
-
labs(title = paste(trait, "- Distribution"), x = "Value", y = "Density") +
|
| 370 |
-
theme_minimal()
|
| 371 |
-
|
| 372 |
-
p2 <- ggplot(df, aes(sample = value)) +
|
| 373 |
-
stat_qq(color = "steelblue", alpha = 0.7) +
|
| 374 |
-
stat_qq_line(color = "red", size = 1) +
|
| 375 |
-
labs(title = "Q-Q Plot", x = "Theoretical Quantiles", y = "Sample Quantiles") +
|
| 376 |
-
theme_minimal()
|
| 377 |
-
|
| 378 |
-
cowplot::plot_grid(p1, p2, ncol = 2)
|
| 379 |
}
|
| 380 |
|
| 381 |
-
# 4
|
| 382 |
-
|
| 383 |
-
if (is.null(
|
| 384 |
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 390 |
}
|
| 391 |
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
labs(title = "Genetic Map", x = "Chromosome", y = "Position (cM)") +
|
| 399 |
theme_minimal() +
|
| 400 |
-
theme(
|
| 401 |
}
|
| 402 |
|
| 403 |
-
# 5
|
| 404 |
-
|
| 405 |
if (is.null(scan)) return(NULL)
|
| 406 |
|
| 407 |
df <- as.data.frame(scan)
|
| 408 |
df$chr <- factor(df$chr, levels = unique(df$chr))
|
| 409 |
|
| 410 |
ggplot(df, aes(x = lod, y = chr, fill = after_stat(x))) +
|
| 411 |
-
|
| 412 |
scale_fill_viridis_c(option = "C", name = "LOD Score") +
|
| 413 |
labs(title = "LOD Score Distribution by Chromosome",
|
| 414 |
x = "LOD Score", y = "Chromosome") +
|
| 415 |
-
|
| 416 |
theme(legend.position = "right")
|
| 417 |
}
|
| 418 |
|
| 419 |
-
# 6
|
| 420 |
-
|
| 421 |
-
if (is.null(
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
|
|
|
|
|
|
|
|
|
| 433 |
|
| 434 |
-
|
| 435 |
-
geom_bar(stat = "identity", fill = "darkorange", alpha = 0.7) +
|
| 436 |
-
labs(title = "Missing Data per Marker", x = "Marker Index", y = "Missing Count") +
|
| 437 |
-
theme_minimal()
|
| 438 |
|
| 439 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 440 |
}
|
| 441 |
|
| 442 |
-
#
|
| 443 |
-
|
| 444 |
if (is.null(cross)) return(NULL)
|
| 445 |
|
| 446 |
-
map <-
|
| 447 |
-
|
|
|
|
| 448 |
for (chr in names(map)) {
|
| 449 |
positions <- map[[chr]]
|
| 450 |
if (length(positions) > 1) {
|
| 451 |
gaps <- diff(positions)
|
| 452 |
-
|
|
|
|
| 453 |
}
|
| 454 |
}
|
| 455 |
|
| 456 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 457 |
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 463 |
theme_minimal() +
|
| 464 |
theme(legend.position = "bottom")
|
| 465 |
}
|
| 466 |
|
| 467 |
-
#
|
| 468 |
-
|
| 469 |
if (is.null(cross) || is.null(qtl_row) || nrow(qtl_row) == 0) return(NULL)
|
| 470 |
|
|
|
|
| 471 |
chr_name <- as.character(qtl_row$Chromosome[1])
|
| 472 |
if (!chr_name %in% names(cross$geno)) return(NULL)
|
| 473 |
|
| 474 |
-
map <-
|
| 475 |
marker_pos <- which.min(abs(map - qtl_row$Position_cM[1]))
|
| 476 |
marker_name <- names(map)[marker_pos]
|
| 477 |
|
| 478 |
-
|
| 479 |
-
|
|
|
|
| 480 |
phenotypes <- cross$pheno[[trait]]
|
| 481 |
|
| 482 |
df <- data.frame(
|
| 483 |
-
|
| 484 |
-
|
| 485 |
)
|
| 486 |
-
df <- df[!is.na(df$
|
| 487 |
|
| 488 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 489 |
|
| 490 |
-
ggplot(df, aes(x =
|
| 491 |
-
geom_violin(alpha = 0.
|
| 492 |
-
geom_boxplot(width = 0.2, alpha = 0.
|
| 493 |
stat_summary(fun = mean, geom = "point", shape = 18, size = 4, color = "red") +
|
|
|
|
|
|
|
| 494 |
scale_fill_brewer(palette = "Set2") +
|
| 495 |
-
labs(title = paste("
|
| 496 |
x = "Genotype", y = trait) +
|
| 497 |
theme_minimal() +
|
| 498 |
theme(legend.position = "none")
|
| 499 |
}
|
| 500 |
|
| 501 |
-
#
|
| 502 |
-
|
| 503 |
-
if (is.null(
|
| 504 |
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
if (!is.null(scan)) {
|
| 509 |
-
comparison_df <- rbind(comparison_df,
|
| 510 |
-
data.frame(Method = toupper(method),
|
| 511 |
-
MaxLOD = max(scan$lod, na.rm = TRUE)))
|
| 512 |
-
}
|
| 513 |
-
}
|
| 514 |
|
| 515 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 516 |
|
| 517 |
-
ggplot(
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
labs(title = "
|
| 522 |
-
x = "
|
| 523 |
-
theme_minimal()
|
| 524 |
-
theme(legend.position = "none",
|
| 525 |
-
axis.text.x = element_text(angle = 45, hjust = 1))
|
| 526 |
}
|
| 527 |
|
| 528 |
-
#
|
| 529 |
-
|
| 530 |
-
if (is.null(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 531 |
|
| 532 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 533 |
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 543 |
}
|
| 544 |
|
| 545 |
# ============================================================================
|
|
@@ -547,59 +529,42 @@ create_correlation_plot <- function(cross) {
|
|
| 547 |
# ============================================================================
|
| 548 |
|
| 549 |
ui <- dashboardPage(
|
| 550 |
-
dashboardHeader(title = "QTL Analysis
|
| 551 |
|
| 552 |
-
dashboardSidebar(width =
|
| 553 |
sidebarMenu(
|
| 554 |
menuItem("Data Input", tabName = "data", icon = icon("upload")),
|
| 555 |
-
menuItem("Analysis", tabName = "
|
| 556 |
-
menuItem("Visualizations", tabName = "viz", icon = icon("chart-bar")),
|
| 557 |
menuItem("QTL Results", tabName = "results", icon = icon("table")),
|
|
|
|
| 558 |
menuItem("Documentation", tabName = "help", icon = icon("question-circle"))
|
| 559 |
),
|
| 560 |
|
| 561 |
div(style = "padding: 15px;",
|
| 562 |
h4("Data Upload", style = "color: #3c8dbc;"),
|
| 563 |
-
fileInput("geno_file", "Genotype CSV",
|
| 564 |
-
|
| 565 |
-
buttonLabel = "Browse",
|
| 566 |
-
placeholder = "genotypes.csv"),
|
| 567 |
-
fileInput("pheno_file", "Phenotype CSV",
|
| 568 |
-
accept = ".csv",
|
| 569 |
-
buttonLabel = "Browse",
|
| 570 |
-
placeholder = "phenotypes.csv"),
|
| 571 |
-
|
| 572 |
-
hr(),
|
| 573 |
|
| 574 |
-
h4("Analysis Settings", style = "color: #3c8dbc;"),
|
| 575 |
selectInput("cross_type", "Cross Type",
|
| 576 |
-
choices = c("F2 Intercross" = "f2",
|
| 577 |
-
"Backcross" = "bc",
|
| 578 |
-
"RIL (Selfing)" = "riself"),
|
| 579 |
selected = "f2"),
|
| 580 |
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
selectInput("scan_method", "Mapping Method",
|
| 584 |
-
choices = c("Haley-Knott" = "hk",
|
| 585 |
-
"EM Algorithm" = "em",
|
| 586 |
-
"Marker Regression" = "mr"),
|
| 587 |
-
selected = "hk"),
|
| 588 |
|
| 589 |
-
|
| 590 |
-
|
| 591 |
|
| 592 |
-
numericInput("
|
| 593 |
-
|
| 594 |
|
| 595 |
-
actionButton("run_analysis", "RUN
|
| 596 |
icon = icon("play"),
|
| 597 |
style = "background-color: #3c8dbc; color: white; width: 100%; margin-top: 15px;"),
|
| 598 |
|
| 599 |
br(), br(),
|
| 600 |
|
| 601 |
-
downloadButton("download_results", "Download Results",
|
| 602 |
-
style = "width: 100%;")
|
| 603 |
)
|
| 604 |
),
|
| 605 |
|
|
@@ -607,13 +572,9 @@ ui <- dashboardPage(
|
|
| 607 |
useShinyjs(),
|
| 608 |
tags$head(
|
| 609 |
tags$style(HTML("
|
| 610 |
-
.content-wrapper, .right-side {
|
| 611 |
-
|
| 612 |
-
}
|
| 613 |
-
.box {
|
| 614 |
-
border-radius: 10px;
|
| 615 |
-
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
| 616 |
-
}
|
| 617 |
"))
|
| 618 |
),
|
| 619 |
|
|
@@ -631,37 +592,32 @@ ui <- dashboardPage(
|
|
| 631 |
)
|
| 632 |
),
|
| 633 |
fluidRow(
|
| 634 |
-
box(title = "
|
| 635 |
-
plotOutput("qc_plot", height = "500px") %>% withSpinner()
|
| 636 |
-
)
|
| 637 |
-
),
|
| 638 |
-
fluidRow(
|
| 639 |
-
box(title = "Genetic Map", status = "success", width = 12,
|
| 640 |
plotOutput("genetic_map", height = "500px") %>% withSpinner()
|
| 641 |
)
|
| 642 |
)
|
| 643 |
),
|
| 644 |
|
| 645 |
-
#
|
| 646 |
-
tabItem(tabName = "
|
| 647 |
fluidRow(
|
| 648 |
box(title = "Analysis Status", status = "primary", solidHeader = TRUE, width = 12,
|
| 649 |
verbatimTextOutput("analysis_status")
|
| 650 |
)
|
| 651 |
),
|
| 652 |
fluidRow(
|
| 653 |
-
box(title = "Manhattan Plot", status = "info", width = 12,
|
| 654 |
-
plotOutput("
|
| 655 |
)
|
| 656 |
),
|
| 657 |
fluidRow(
|
| 658 |
-
box(title = "
|
| 659 |
-
|
| 660 |
)
|
| 661 |
),
|
| 662 |
fluidRow(
|
| 663 |
-
box(title = "
|
| 664 |
-
plotOutput("
|
| 665 |
)
|
| 666 |
)
|
| 667 |
),
|
|
@@ -669,25 +625,45 @@ ui <- dashboardPage(
|
|
| 669 |
# Visualizations Tab
|
| 670 |
tabItem(tabName = "viz",
|
| 671 |
fluidRow(
|
| 672 |
-
box(title = "
|
| 673 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 674 |
),
|
| 675 |
-
box(title = "
|
| 676 |
-
plotOutput("
|
| 677 |
)
|
| 678 |
),
|
| 679 |
fluidRow(
|
| 680 |
-
box(title = "
|
| 681 |
-
plotOutput("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 682 |
),
|
| 683 |
-
box(title = "
|
| 684 |
-
plotOutput("
|
| 685 |
)
|
| 686 |
),
|
| 687 |
fluidRow(
|
| 688 |
-
box(title = "
|
| 689 |
-
selectInput("effect_qtl", "Select QTL
|
| 690 |
-
plotOutput("
|
| 691 |
)
|
| 692 |
)
|
| 693 |
),
|
|
@@ -700,13 +676,27 @@ ui <- dashboardPage(
|
|
| 700 |
)
|
| 701 |
),
|
| 702 |
fluidRow(
|
| 703 |
-
box(title = "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 704 |
DTOutput("permutation_table") %>% withSpinner()
|
| 705 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 706 |
),
|
| 707 |
fluidRow(
|
| 708 |
-
box(title = "
|
| 709 |
-
|
| 710 |
)
|
| 711 |
)
|
| 712 |
),
|
|
@@ -714,62 +704,52 @@ ui <- dashboardPage(
|
|
| 714 |
# Help Tab
|
| 715 |
tabItem(tabName = "help",
|
| 716 |
fluidRow(
|
| 717 |
-
box(title = "Documentation", status = "primary", width = 12,
|
| 718 |
HTML("
|
| 719 |
-
<h3>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 720 |
|
| 721 |
-
<
|
| 722 |
-
<
|
| 723 |
-
|
| 724 |
-
|
| 725 |
-
|
| 726 |
-
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
|
| 730 |
-
|
| 731 |
-
<li>
|
| 732 |
-
<li>
|
| 733 |
-
<li>
|
| 734 |
-
<li>
|
| 735 |
-
</
|
| 736 |
|
| 737 |
-
<
|
|
|
|
| 738 |
<pre>
|
| 739 |
-
id,
|
| 740 |
-
|
| 741 |
-
|
| 742 |
-
|
| 743 |
-
|
| 744 |
-
<li>First column: Individual IDs (must match genotype file)</li>
|
| 745 |
-
<li>Remaining columns: Phenotypic traits</li>
|
| 746 |
-
</ul>
|
| 747 |
-
|
| 748 |
-
<h4>2. Run Analysis</h4>
|
| 749 |
-
<ul>
|
| 750 |
-
<li>Select cross type (F2, Backcross, or RIL)</li>
|
| 751 |
-
<li>Choose phenotype trait to analyze</li>
|
| 752 |
-
<li>Select mapping method (HK, EM, or MR)</li>
|
| 753 |
-
<li>Set LOD threshold (default 3.0)</li>
|
| 754 |
-
<li>Set number of permutations for significance testing</li>
|
| 755 |
-
<li>Click 'RUN ANALYSIS'</li>
|
| 756 |
-
</ul>
|
| 757 |
|
| 758 |
-
<
|
| 759 |
-
<
|
| 760 |
-
|
| 761 |
-
|
| 762 |
-
|
| 763 |
-
<li><strong>Threshold:</strong> Significance level from permutation test</li>
|
| 764 |
-
</ul>
|
| 765 |
-
|
| 766 |
-
<h4>Troubleshooting</h4>
|
| 767 |
-
<ul>
|
| 768 |
-
<li>Ensure IDs match between genotype and phenotype files</li>
|
| 769 |
-
<li>Check for missing data (>50% may cause issues)</li>
|
| 770 |
-
<li>Verify chromosome and position formats</li>
|
| 771 |
-
<li>Try different mapping methods if one fails</li>
|
| 772 |
-
</ul>
|
| 773 |
")
|
| 774 |
)
|
| 775 |
)
|
|
@@ -786,37 +766,34 @@ server <- function(input, output, session) {
|
|
| 786 |
|
| 787 |
# Reactive values
|
| 788 |
cross_data <- reactiveVal(NULL)
|
| 789 |
-
|
| 790 |
-
|
| 791 |
-
|
| 792 |
analysis_done <- reactiveVal(FALSE)
|
| 793 |
|
| 794 |
# Value boxes
|
| 795 |
output$n_individuals <- renderValueBox({
|
| 796 |
cross <- cross_data()
|
| 797 |
-
|
| 798 |
-
|
| 799 |
-
|
| 800 |
-
|
| 801 |
-
}
|
| 802 |
})
|
| 803 |
|
| 804 |
output$n_markers <- renderValueBox({
|
| 805 |
cross <- cross_data()
|
| 806 |
-
|
| 807 |
-
|
| 808 |
-
|
| 809 |
-
|
| 810 |
-
}
|
| 811 |
})
|
| 812 |
|
| 813 |
output$n_chromosomes <- renderValueBox({
|
| 814 |
cross <- cross_data()
|
| 815 |
-
|
| 816 |
-
|
| 817 |
-
|
| 818 |
-
|
| 819 |
-
}
|
| 820 |
})
|
| 821 |
|
| 822 |
# Load data
|
|
@@ -833,8 +810,6 @@ server <- function(input, output, session) {
|
|
| 833 |
)
|
| 834 |
|
| 835 |
cross_data(cross)
|
| 836 |
-
|
| 837 |
-
# Update phenotype dropdown
|
| 838 |
updateSelectInput(session, "pheno_trait",
|
| 839 |
choices = names(cross$pheno),
|
| 840 |
selected = names(cross$pheno)[1])
|
|
@@ -846,50 +821,46 @@ server <- function(input, output, session) {
|
|
| 846 |
})
|
| 847 |
})
|
| 848 |
|
| 849 |
-
# Run
|
| 850 |
observeEvent(input$run_analysis, {
|
| 851 |
cross <- cross_data()
|
| 852 |
req(cross, input$pheno_trait)
|
| 853 |
|
| 854 |
-
showNotification("Running
|
| 855 |
|
| 856 |
-
withProgress(message = "QTL
|
| 857 |
|
| 858 |
-
incProgress(0.
|
| 859 |
pheno_idx <- which(names(cross$pheno) == input$pheno_trait)
|
| 860 |
-
|
| 861 |
-
|
| 862 |
|
| 863 |
-
|
| 864 |
-
|
| 865 |
-
|
| 866 |
-
|
| 867 |
|
| 868 |
-
|
| 869 |
-
|
| 870 |
-
|
| 871 |
-
if (!is.na(thr) && thr > 0) {
|
| 872 |
-
updateNumericInput(session, "lod_threshold", value = round(thr, 2))
|
| 873 |
-
}
|
| 874 |
}
|
| 875 |
}
|
| 876 |
|
| 877 |
-
|
| 878 |
-
|
| 879 |
-
|
| 880 |
-
|
| 881 |
-
|
| 882 |
-
|
| 883 |
-
qtl_choices <- paste0("Chr", qtls$Chromosome, "_", qtls$Position_cM, "cM_LOD", qtls$LOD)
|
| 884 |
-
updateSelectInput(session, "effect_qtl", choices = qtl_choices)
|
| 885 |
}
|
| 886 |
|
| 887 |
analysis_done(TRUE)
|
| 888 |
-
showNotification("Analysis complete!",
|
|
|
|
| 889 |
})
|
| 890 |
})
|
| 891 |
|
| 892 |
-
#
|
| 893 |
output$data_summary <- renderPrint({
|
| 894 |
cross <- cross_data()
|
| 895 |
if (is.null(cross)) {
|
|
@@ -900,166 +871,263 @@ server <- function(input, output, session) {
|
|
| 900 |
cat("Individuals:", nind(cross), "\n")
|
| 901 |
cat("Markers:", totmar(cross), "\n")
|
| 902 |
cat("Chromosomes:", nchr(cross), "\n\n")
|
| 903 |
-
cat("Phenotypes:", paste(names(cross$pheno), collapse = ", "), "\n
|
| 904 |
|
| 905 |
-
|
| 906 |
-
|
| 907 |
-
cat("Missing data rate:", round(missing_total, 2), "%\n")
|
| 908 |
}
|
| 909 |
})
|
| 910 |
|
| 911 |
output$analysis_status <- renderPrint({
|
|
|
|
| 912 |
if (!analysis_done()) {
|
| 913 |
-
cat("Analysis not run. Click 'RUN
|
| 914 |
} else {
|
| 915 |
-
qtls <- qtl_result()
|
| 916 |
-
scan <- scan_result()
|
| 917 |
cat("=== Analysis Complete ===\n\n")
|
| 918 |
-
cat("
|
| 919 |
-
cat("
|
| 920 |
-
cat("QTLs found:", ifelse(is.null(qtls), 0, nrow(qtls)), "\n")
|
| 921 |
-
if (!is.null(scan)) {
|
| 922 |
-
cat("Max LOD:", round(max(scan$lod, na.rm = TRUE), 3), "\n")
|
| 923 |
-
}
|
| 924 |
-
}
|
| 925 |
-
})
|
| 926 |
-
|
| 927 |
-
output$qtl_statistics <- renderPrint({
|
| 928 |
-
qtls <- qtl_result()
|
| 929 |
-
if (is.null(qtls) || nrow(qtls) == 0) {
|
| 930 |
-
cat("No significant QTLs found.")
|
| 931 |
-
} else {
|
| 932 |
-
cat("=== QTL Statistics ===\n\n")
|
| 933 |
-
cat("Total QTLs:", nrow(qtls), "\n")
|
| 934 |
-
cat("Average LOD:", round(mean(qtls$LOD), 3), "\n")
|
| 935 |
-
cat("Average PVE:", round(mean(qtls$PVE_percent), 2), "%\n")
|
| 936 |
-
cat("Average CI width:", round(mean(qtls$CI_width), 2), "cM\n\n")
|
| 937 |
|
| 938 |
-
#
|
| 939 |
-
|
| 940 |
-
|
| 941 |
-
for (
|
| 942 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 943 |
}
|
|
|
|
| 944 |
}
|
| 945 |
})
|
| 946 |
|
| 947 |
-
#
|
| 948 |
-
output$
|
| 949 |
cross <- cross_data()
|
| 950 |
req(cross)
|
| 951 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 952 |
})
|
| 953 |
|
| 954 |
-
|
| 955 |
-
|
| 956 |
-
|
| 957 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 958 |
})
|
| 959 |
|
| 960 |
-
|
| 961 |
-
|
| 962 |
-
|
| 963 |
-
req(
|
| 964 |
-
|
| 965 |
})
|
| 966 |
|
| 967 |
-
|
| 968 |
-
|
| 969 |
-
|
| 970 |
-
|
|
|
|
| 971 |
})
|
| 972 |
|
|
|
|
| 973 |
output$ridge_plot <- renderPlot({
|
| 974 |
-
|
| 975 |
-
req(
|
| 976 |
-
|
| 977 |
})
|
| 978 |
|
| 979 |
-
|
| 980 |
-
|
| 981 |
-
|
| 982 |
-
|
|
|
|
| 983 |
})
|
| 984 |
|
| 985 |
-
|
| 986 |
-
|
| 987 |
-
|
| 988 |
-
|
|
|
|
| 989 |
})
|
| 990 |
|
| 991 |
-
|
| 992 |
-
|
| 993 |
-
|
| 994 |
-
req(
|
| 995 |
-
|
| 996 |
-
create_comparison_plot(scan_list)
|
| 997 |
})
|
| 998 |
|
| 999 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1000 |
cross <- cross_data()
|
| 1001 |
req(cross)
|
| 1002 |
-
|
| 1003 |
})
|
| 1004 |
|
| 1005 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1006 |
req(input$effect_qtl, input$effect_qtl != "")
|
| 1007 |
-
qtls <-
|
| 1008 |
cross <- cross_data()
|
| 1009 |
req(qtls, nrow(qtls) > 0, cross, input$pheno_trait)
|
| 1010 |
|
| 1011 |
-
|
| 1012 |
-
qtl_idx <- which(paste0("Chr", qtls$Chromosome, "_", qtls$Position_cM, "cM_LOD", qtls$LOD) == input$effect_qtl)
|
| 1013 |
if (length(qtl_idx) == 0) return(NULL)
|
| 1014 |
|
| 1015 |
-
|
| 1016 |
})
|
| 1017 |
|
| 1018 |
-
#
|
| 1019 |
output$qtl_table <- renderDT({
|
| 1020 |
-
qtls <-
|
| 1021 |
req(qtls, nrow(qtls) > 0)
|
| 1022 |
|
| 1023 |
-
datatable(qtls,
|
| 1024 |
-
|
| 1025 |
-
pageLength = 15,
|
| 1026 |
-
scrollX = TRUE,
|
| 1027 |
-
dom = 'Bfrtip'
|
| 1028 |
-
),
|
| 1029 |
-
rownames = FALSE,
|
| 1030 |
-
class = 'display nowrap') %>%
|
| 1031 |
-
formatStyle("LOD",
|
| 1032 |
-
background = styleColorBar(range(qtls$LOD), "lightblue")) %>%
|
| 1033 |
-
formatStyle("PVE_percent",
|
| 1034 |
-
background = styleColorBar(c(0, max(qtls$PVE_percent, na.rm = TRUE)), "lightgreen"))
|
| 1035 |
})
|
| 1036 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1037 |
output$permutation_table <- renderDT({
|
| 1038 |
-
|
| 1039 |
-
req(
|
| 1040 |
|
| 1041 |
-
# Calculate thresholds for different alpha levels
|
| 1042 |
alphas <- c(0.05, 0.01, 0.001)
|
| 1043 |
thresholds <- sapply(alphas, function(a) {
|
| 1044 |
-
thr <- summary(
|
| 1045 |
if (is.na(thr)) NA else round(thr, 3)
|
| 1046 |
})
|
| 1047 |
|
| 1048 |
-
perm_df <- data.frame(
|
| 1049 |
-
|
| 1050 |
-
|
| 1051 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1052 |
|
| 1053 |
-
datatable(
|
| 1054 |
-
options = list(pageLength = 5, dom = 't'),
|
| 1055 |
-
rownames = FALSE)
|
| 1056 |
})
|
| 1057 |
|
| 1058 |
# Download handler
|
| 1059 |
output$download_results <- downloadHandler(
|
| 1060 |
-
filename = function() paste0("
|
| 1061 |
content = function(file) {
|
| 1062 |
-
qtls <-
|
| 1063 |
if (!is.null(qtls) && nrow(qtls) > 0) {
|
| 1064 |
write.csv(qtls, file, row.names = FALSE)
|
| 1065 |
} else {
|
|
|
|
| 1 |
# ============================================================================
|
| 2 |
+
# ADVANCED QTL ANALYSIS PIPELINE
|
| 3 |
+
# 10 Mapping Models + 10+ Unique Visualizations
|
| 4 |
+
# Complete Production-Ready Application
|
| 5 |
# ============================================================================
|
| 6 |
|
| 7 |
+
# Load libraries
|
| 8 |
suppressPackageStartupMessages({
|
| 9 |
library(shiny)
|
| 10 |
library(shinydashboard)
|
|
|
|
| 19 |
library(RColorBrewer)
|
| 20 |
library(ggrepel)
|
| 21 |
library(cowplot)
|
|
|
|
| 22 |
library(viridis)
|
| 23 |
library(ggridges)
|
| 24 |
+
library(pheatmap)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
library(corrplot)
|
| 26 |
+
})
|
| 27 |
|
| 28 |
# ============================================================================
|
| 29 |
# DATA PARSER
|
|
|
|
| 31 |
|
| 32 |
parse_custom_cross <- function(geno_path, pheno_path, cross_type = "f2") {
|
| 33 |
|
|
|
|
| 34 |
geno_raw <- read.csv(geno_path, header = FALSE, stringsAsFactors = FALSE,
|
| 35 |
check.names = FALSE, na.strings = c("NA", "", "N/A", "-9", "0"))
|
| 36 |
|
| 37 |
if (nrow(geno_raw) < 4) {
|
| 38 |
+
stop("Genotype file must have at least 4 rows")
|
| 39 |
}
|
| 40 |
|
|
|
|
| 41 |
col_names <- as.character(geno_raw[1, ])
|
| 42 |
chr_row <- as.character(geno_raw[2, ])
|
| 43 |
pos_row <- as.character(geno_raw[3, ])
|
| 44 |
ind_data <- geno_raw[4:nrow(geno_raw), , drop = FALSE]
|
| 45 |
|
|
|
|
| 46 |
marker_cols <- 2:ncol(geno_raw)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
marker_names <- col_names[marker_cols]
|
| 48 |
|
|
|
|
| 49 |
chromosomes <- chr_row[marker_cols]
|
| 50 |
chromosomes <- gsub("^[Cc]hr", "", chromosomes)
|
|
|
|
| 51 |
chromosomes[chromosomes == ""] <- "unknown"
|
|
|
|
| 52 |
|
|
|
|
| 53 |
positions <- suppressWarnings(as.numeric(pos_row[marker_cols]))
|
| 54 |
positions[is.na(positions)] <- 0
|
| 55 |
|
|
|
|
| 56 |
ind_ids <- as.character(ind_data[, 1])
|
| 57 |
+
if (any(duplicated(ind_ids))) ind_ids <- make.unique(ind_ids)
|
|
|
|
|
|
|
| 58 |
|
|
|
|
| 59 |
geno_mat <- as.matrix(ind_data[, marker_cols, drop = FALSE])
|
| 60 |
mode(geno_mat) <- "numeric"
|
| 61 |
rownames(geno_mat) <- ind_ids
|
| 62 |
colnames(geno_mat) <- marker_names
|
| 63 |
|
| 64 |
+
# Filter individuals
|
| 65 |
missing_rate <- rowMeans(is.na(geno_mat))
|
| 66 |
valid_inds <- missing_rate <= 0.5
|
|
|
|
|
|
|
|
|
|
| 67 |
geno_mat <- geno_mat[valid_inds, , drop = FALSE]
|
| 68 |
ind_ids <- ind_ids[valid_inds]
|
| 69 |
|
| 70 |
+
# Read phenotypes
|
| 71 |
pheno_raw <- read.csv(pheno_path, header = TRUE, stringsAsFactors = FALSE,
|
| 72 |
check.names = FALSE, na.strings = c("NA", "", "N/A"))
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
pheno_ids <- as.character(pheno_raw[, 1])
|
| 75 |
+
if (any(duplicated(pheno_ids))) pheno_ids <- make.unique(pheno_ids)
|
|
|
|
|
|
|
| 76 |
pheno_df <- pheno_raw[, -1, drop = FALSE]
|
| 77 |
|
|
|
|
| 78 |
for (i in 1:ncol(pheno_df)) {
|
| 79 |
pheno_df[, i] <- suppressWarnings(as.numeric(pheno_df[, i]))
|
| 80 |
}
|
| 81 |
rownames(pheno_df) <- pheno_ids
|
| 82 |
|
| 83 |
+
# Align
|
| 84 |
common_ids <- intersect(ind_ids, pheno_ids)
|
| 85 |
+
if (length(common_ids) == 0) stop("No matching IDs")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
geno_mat <- geno_mat[common_ids, , drop = FALSE]
|
| 88 |
pheno_df <- pheno_df[common_ids, , drop = FALSE]
|
| 89 |
|
| 90 |
+
# Filter markers
|
| 91 |
marker_missing <- colMeans(is.na(geno_mat))
|
| 92 |
valid_markers <- marker_missing <= 0.5
|
|
|
|
|
|
|
|
|
|
| 93 |
geno_mat <- geno_mat[, valid_markers, drop = FALSE]
|
| 94 |
chromosomes <- chromosomes[valid_markers]
|
| 95 |
positions <- positions[valid_markers]
|
| 96 |
marker_names <- marker_names[valid_markers]
|
| 97 |
|
| 98 |
+
# Build map
|
| 99 |
unique_chrs <- unique(chromosomes)
|
| 100 |
chr_numeric <- suppressWarnings(as.numeric(unique_chrs))
|
| 101 |
chr_numeric[is.na(chr_numeric)] <- 999
|
|
|
|
| 111 |
chr_positions <- positions[chr_idx]
|
| 112 |
names(chr_positions) <- chr_markers
|
| 113 |
|
|
|
|
| 114 |
ord <- order(chr_positions)
|
| 115 |
chr_positions <- chr_positions[ord]
|
| 116 |
chr_markers <- chr_markers[ord]
|
|
|
|
|
|
|
| 117 |
chr_positions <- chr_positions[!duplicated(chr_positions)]
|
| 118 |
chr_markers <- chr_markers[!duplicated(chr_markers)]
|
| 119 |
|
| 120 |
+
if (length(chr_positions) >= 1) {
|
| 121 |
map_list[[ch]] <- chr_positions
|
| 122 |
geno_list[[ch]] <- list(
|
| 123 |
data = geno_mat[, chr_markers, drop = FALSE],
|
|
|
|
| 128 |
}
|
| 129 |
}
|
| 130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
cross <- list(geno = geno_list, pheno = pheno_df)
|
| 132 |
class(cross) <- c(cross_type, "cross")
|
|
|
|
|
|
|
| 133 |
cross <- jittermap(cross)
|
| 134 |
|
| 135 |
return(cross)
|
| 136 |
}
|
| 137 |
|
| 138 |
# ============================================================================
|
| 139 |
+
# 10 DIFFERENT MAPPING MODELS
|
| 140 |
# ============================================================================
|
| 141 |
|
| 142 |
+
run_all_models <- function(cross, pheno_col) {
|
| 143 |
+
models <- list()
|
| 144 |
+
|
| 145 |
+
# Model 1: Haley-Knott Regression
|
| 146 |
tryCatch({
|
| 147 |
+
models$hk <- scanone(cross, pheno.col = pheno_col, method = "hk")
|
| 148 |
+
}, error = function(e) message("HK failed: ", e$message))
|
| 149 |
+
|
| 150 |
+
# Model 2: EM Algorithm
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
tryCatch({
|
| 152 |
+
models$em <- scanone(cross, pheno.col = pheno_col, method = "em")
|
| 153 |
+
}, error = function(e) message("EM failed: ", e$message))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
+
# Model 3: Extended Haley-Knott
|
| 156 |
+
tryCatch({
|
| 157 |
+
models$ehk <- scanone(cross, pheno.col = pheno_col, method = "ehk")
|
| 158 |
+
}, error = function(e) message("EHK failed: ", e$message))
|
| 159 |
|
| 160 |
+
# Model 4: Multiple Imputation
|
| 161 |
+
tryCatch({
|
| 162 |
+
cross_sim <- sim.geno(cross, n.draws = 32, step = 1)
|
| 163 |
+
models$imp <- scanone(cross_sim, pheno.col = pheno_col, method = "imp")
|
| 164 |
+
}, error = function(e) message("IMP failed: ", e$message))
|
| 165 |
+
|
| 166 |
+
# Model 5: Marker Regression
|
| 167 |
+
tryCatch({
|
| 168 |
+
models$mr <- scanone(cross, pheno.col = pheno_col, method = "mr")
|
| 169 |
+
}, error = function(e) message("MR failed: ", e$message))
|
| 170 |
+
|
| 171 |
+
# Model 6: Non-parametric
|
| 172 |
+
tryCatch({
|
| 173 |
+
models$np <- scanone(cross, pheno.col = pheno_col, method = "np")
|
| 174 |
+
}, error = function(e) message("NP failed: ", e$message))
|
| 175 |
+
|
| 176 |
+
# Model 7: Composite Interval Mapping
|
| 177 |
+
tryCatch({
|
| 178 |
+
models$cim <- cim(cross, pheno.col = pheno_col, method = "hk", window = 10)
|
| 179 |
+
}, error = function(e) message("CIM failed: ", e$message))
|
| 180 |
+
|
| 181 |
+
# Model 8: Multiple QTL Mapping
|
| 182 |
+
tryCatch({
|
| 183 |
+
scan_hk <- scanone(cross, pheno.col = pheno_col, method = "hk")
|
| 184 |
+
cofactors <- which(scan_hk$lod > 2)
|
| 185 |
+
if (length(cofactors) > 0) {
|
| 186 |
+
models$mqm <- mqm(cross, pheno.col = pheno_col, cofactors = cofactors)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
}
|
| 188 |
+
}, error = function(e) message("MQM failed: ", e$message))
|
| 189 |
|
| 190 |
+
# Model 9: Binary Trait
|
| 191 |
+
tryCatch({
|
| 192 |
+
models$binary <- scanone(cross, pheno.col = pheno_col, method = "binary")
|
| 193 |
+
}, error = function(e) message("Binary failed: ", e$message))
|
| 194 |
+
|
| 195 |
+
# Model 10: Normal Model
|
| 196 |
+
tryCatch({
|
| 197 |
+
models$normal <- scanone(cross, pheno.col = pheno_col, method = "normal")
|
| 198 |
+
}, error = function(e) message("Normal failed: ", e$message))
|
| 199 |
|
| 200 |
+
return(models)
|
| 201 |
}
|
| 202 |
|
| 203 |
# ============================================================================
|
| 204 |
+
# 10+ UNIQUE VISUALIZATIONS
|
| 205 |
# ============================================================================
|
| 206 |
|
| 207 |
+
# Plot 1: Multi-Model Manhattan Comparison
|
| 208 |
+
plot_multi_manhattan <- function(models, threshold = 3) {
|
| 209 |
+
if (is.null(models) || length(models) == 0) return(NULL)
|
| 210 |
+
|
| 211 |
+
# Combine all models
|
| 212 |
+
combined <- data.frame()
|
| 213 |
+
for (name in names(models)) {
|
| 214 |
+
scan <- models[[name]]
|
| 215 |
+
if (!is.null(scan)) {
|
| 216 |
+
df <- as.data.frame(scan)
|
| 217 |
+
df$model <- toupper(name)
|
| 218 |
+
df$pos_cum <- df$pos
|
| 219 |
+
combined <- rbind(combined, df)
|
| 220 |
+
}
|
| 221 |
+
}
|
|
|
|
| 222 |
|
| 223 |
+
if (nrow(combined) == 0) return(NULL)
|
|
|
|
|
|
|
| 224 |
|
| 225 |
+
ggplot(combined, aes(x = pos, y = lod, color = model)) +
|
| 226 |
+
geom_line(size = 0.8, alpha = 0.7) +
|
| 227 |
geom_hline(yintercept = threshold, color = "red", linetype = "dashed", size = 1) +
|
| 228 |
+
facet_wrap(~chr, scales = "free_x", ncol = 3) +
|
| 229 |
+
scale_color_brewer(palette = "Set1") +
|
| 230 |
+
labs(title = "Multi-Model QTL Comparison Across Chromosomes",
|
| 231 |
+
x = "Position (cM)", y = "LOD Score", color = "Model") +
|
| 232 |
theme_minimal() +
|
| 233 |
+
theme(legend.position = "bottom",
|
| 234 |
+
strip.background = element_rect(fill = "lightblue", color = NA),
|
| 235 |
+
strip.text = element_text(face = "bold"))
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
# Plot 2: Model Performance Heatmap
|
| 239 |
+
plot_model_heatmap <- function(models) {
|
| 240 |
+
if (is.null(models) || length(models) == 0) return(NULL)
|
| 241 |
+
|
| 242 |
+
# Extract peak LOD per chromosome per model
|
| 243 |
+
perf_matrix <- matrix(0, nrow = length(models), ncol = 0)
|
| 244 |
+
model_names <- c()
|
| 245 |
+
|
| 246 |
+
for (name in names(models)) {
|
| 247 |
+
scan <- models[[name]]
|
| 248 |
+
if (!is.null(scan)) {
|
| 249 |
+
model_names <- c(model_names, toupper(name))
|
| 250 |
+
chr_lods <- tapply(scan$lod, scan$chr, max)
|
| 251 |
+
if (length(perf_matrix) == 0) {
|
| 252 |
+
perf_matrix <- matrix(chr_lods, nrow = 1)
|
| 253 |
+
} else {
|
| 254 |
+
perf_matrix <- rbind(perf_matrix, chr_lods[1:ncol(perf_matrix)])
|
| 255 |
+
}
|
| 256 |
+
}
|
| 257 |
}
|
| 258 |
|
| 259 |
+
rownames(perf_matrix) <- model_names
|
| 260 |
+
colnames(perf_matrix) <- names(chr_lods)
|
| 261 |
+
|
| 262 |
+
pheatmap(perf_matrix,
|
| 263 |
+
color = viridis(100),
|
| 264 |
+
main = "Model Performance Heatmap\n(Peak LOD per Chromosome)",
|
| 265 |
+
cluster_rows = TRUE,
|
| 266 |
+
cluster_cols = TRUE,
|
| 267 |
+
display_numbers = TRUE,
|
| 268 |
+
fontsize = 10,
|
| 269 |
+
angle_col = 45)
|
| 270 |
}
|
| 271 |
|
| 272 |
+
# Plot 3: 3D Surface Plot of LOD Scores
|
| 273 |
+
plot_3d_surface <- function(scan) {
|
| 274 |
if (is.null(scan)) return(NULL)
|
| 275 |
|
| 276 |
df <- as.data.frame(scan)
|
| 277 |
+
df$chr_num <- as.numeric(factor(df$chr, levels = unique(df$chr)))
|
| 278 |
+
|
| 279 |
+
plot_ly(df, x = ~chr_num, y = ~pos, z = ~lod, type = "mesh3d",
|
| 280 |
+
intensity = ~lod, colorscale = "Viridis",
|
| 281 |
+
text = ~paste("Chr:", chr, "<br>Pos:", pos, "<br>LOD:", round(lod, 3)),
|
| 282 |
+
hoverinfo = "text") %>%
|
| 283 |
+
layout(title = "3D LOD Surface Plot",
|
| 284 |
+
scene = list(xaxis = list(title = "Chromosome"),
|
| 285 |
+
yaxis = list(title = "Position (cM)"),
|
| 286 |
+
zaxis = list(title = "LOD Score")))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
}
|
| 288 |
|
| 289 |
+
# Plot 4: QQ Plot for Model Comparison
|
| 290 |
+
plot_qq_comparison <- function(models) {
|
| 291 |
+
if (is.null(models) || length(models) == 0) return(NULL)
|
| 292 |
|
| 293 |
+
qq_data <- data.frame()
|
| 294 |
+
for (name in names(models)) {
|
| 295 |
+
scan <- models[[name]]
|
| 296 |
+
if (!is.null(scan)) {
|
| 297 |
+
expected <- -log10(ppoints(length(scan$lod)))
|
| 298 |
+
observed <- -log10(rank(scan$lod, ties.method = "first") / (length(scan$lod) + 1))
|
| 299 |
+
qq_data <- rbind(qq_data, data.frame(
|
| 300 |
+
Model = toupper(name),
|
| 301 |
+
Expected = expected,
|
| 302 |
+
Observed = observed
|
| 303 |
+
))
|
| 304 |
+
}
|
| 305 |
}
|
| 306 |
|
| 307 |
+
ggplot(qq_data, aes(x = Expected, y = Observed, color = Model)) +
|
| 308 |
+
geom_point(size = 1, alpha = 0.6) +
|
| 309 |
+
geom_abline(intercept = 0, slope = 1, color = "red", linetype = "dashed", size = 1) +
|
| 310 |
+
scale_color_brewer(palette = "Set2") +
|
| 311 |
+
labs(title = "Q-Q Plot: Model Comparison",
|
| 312 |
+
x = "Expected -log10(p)", y = "Observed -log10(p)") +
|
|
|
|
| 313 |
theme_minimal() +
|
| 314 |
+
theme(legend.position = "bottom")
|
| 315 |
}
|
| 316 |
|
| 317 |
+
# Plot 5: Ridge Plot of LOD Distributions
|
| 318 |
+
plot_lod_ridges <- function(scan) {
|
| 319 |
if (is.null(scan)) return(NULL)
|
| 320 |
|
| 321 |
df <- as.data.frame(scan)
|
| 322 |
df$chr <- factor(df$chr, levels = unique(df$chr))
|
| 323 |
|
| 324 |
ggplot(df, aes(x = lod, y = chr, fill = after_stat(x))) +
|
| 325 |
+
geom_density_ridges_gradient(scale = 2, rel_min_height = 0.01) +
|
| 326 |
scale_fill_viridis_c(option = "C", name = "LOD Score") +
|
| 327 |
labs(title = "LOD Score Distribution by Chromosome",
|
| 328 |
x = "LOD Score", y = "Chromosome") +
|
| 329 |
+
theme_ridges() +
|
| 330 |
theme(legend.position = "right")
|
| 331 |
}
|
| 332 |
|
| 333 |
+
# Plot 6: QTL Effect Size Bubble Plot
|
| 334 |
+
plot_qtl_bubbles <- function(qtls) {
|
| 335 |
+
if (is.null(qtls) || nrow(qtls) == 0) return(NULL)
|
| 336 |
+
|
| 337 |
+
ggplot(qtls, aes(x = Chromosome, y = Position_cM, size = LOD, color = PVE_percent)) +
|
| 338 |
+
geom_point(alpha = 0.7) +
|
| 339 |
+
scale_size_continuous(range = c(3, 15), name = "LOD Score") +
|
| 340 |
+
scale_color_gradientn(colors = c("blue", "green", "yellow", "red"), name = "PVE (%)") +
|
| 341 |
+
labs(title = "QTL Effect Size Bubble Plot",
|
| 342 |
+
x = "Chromosome", y = "Position (cM)") +
|
| 343 |
+
theme_minimal() +
|
| 344 |
+
theme(axis.text.x = element_text(angle = 45, hjust = 1))
|
| 345 |
+
}
|
| 346 |
+
|
| 347 |
+
# Plot 7: Correlation Circle of Traits
|
| 348 |
+
plot_trait_circle <- function(cross) {
|
| 349 |
+
if (is.null(cross) || ncol(cross$pheno) < 2) return(NULL)
|
| 350 |
|
| 351 |
+
cor_matrix <- cor(cross$pheno, use = "complete.obs")
|
|
|
|
|
|
|
|
|
|
| 352 |
|
| 353 |
+
corrplot(cor_matrix,
|
| 354 |
+
method = "circle",
|
| 355 |
+
type = "upper",
|
| 356 |
+
order = "hclust",
|
| 357 |
+
addCoef.col = "black",
|
| 358 |
+
tl.col = "black",
|
| 359 |
+
tl.srt = 45,
|
| 360 |
+
title = "Trait Correlation Circle",
|
| 361 |
+
mar = c(0, 0, 2, 0))
|
| 362 |
}
|
| 363 |
|
| 364 |
+
# Plot 8: Genetic Map Density
|
| 365 |
+
plot_map_density <- function(cross) {
|
| 366 |
if (is.null(cross)) return(NULL)
|
| 367 |
|
| 368 |
+
map <- pull.map(cross)
|
| 369 |
+
density_data <- data.frame()
|
| 370 |
+
|
| 371 |
for (chr in names(map)) {
|
| 372 |
positions <- map[[chr]]
|
| 373 |
if (length(positions) > 1) {
|
| 374 |
gaps <- diff(positions)
|
| 375 |
+
density_data <- rbind(density_data,
|
| 376 |
+
data.frame(Chromosome = chr, Gap = gaps))
|
| 377 |
}
|
| 378 |
}
|
| 379 |
|
| 380 |
+
ggplot(density_data, aes(x = Gap, fill = Chromosome)) +
|
| 381 |
+
geom_histogram(bins = 30, alpha = 0.6, position = "stack") +
|
| 382 |
+
scale_fill_brewer(palette = "Set3") +
|
| 383 |
+
labs(title = "Genetic Map Density",
|
| 384 |
+
x = "Inter-marker Distance (cM)", y = "Count") +
|
| 385 |
+
theme_minimal() +
|
| 386 |
+
theme(legend.position = "bottom")
|
| 387 |
+
}
|
| 388 |
+
|
| 389 |
+
# Plot 9: Parallel Coordinates for Models
|
| 390 |
+
plot_parallel_models <- function(models) {
|
| 391 |
+
if (is.null(models) || length(models) == 0) return(NULL)
|
| 392 |
+
|
| 393 |
+
# Extract top 50 markers
|
| 394 |
+
first_model <- models[[1]]
|
| 395 |
+
if (is.null(first_model)) return(NULL)
|
| 396 |
+
|
| 397 |
+
top_markers <- order(first_model$lod, decreasing = TRUE)[1:min(50, nrow(first_model))]
|
| 398 |
+
|
| 399 |
+
parallel_data <- data.frame(Position = first_model$pos[top_markers])
|
| 400 |
+
for (name in names(models)) {
|
| 401 |
+
scan <- models[[name]]
|
| 402 |
+
if (!is.null(scan)) {
|
| 403 |
+
parallel_data[[toupper(name)]] <- scan$lod[top_markers]
|
| 404 |
+
}
|
| 405 |
+
}
|
| 406 |
|
| 407 |
+
# Reshape for plotting
|
| 408 |
+
plot_data <- pivot_longer(parallel_data, cols = -Position,
|
| 409 |
+
names_to = "Model", values_to = "LOD")
|
| 410 |
+
|
| 411 |
+
ggplot(plot_data, aes(x = Position, y = LOD, color = Model, group = Model)) +
|
| 412 |
+
geom_line(size = 1) +
|
| 413 |
+
scale_color_brewer(palette = "Set1") +
|
| 414 |
+
labs(title = "Model Comparison at Top Markers",
|
| 415 |
+
x = "Position (cM)", y = "LOD Score") +
|
| 416 |
theme_minimal() +
|
| 417 |
theme(legend.position = "bottom")
|
| 418 |
}
|
| 419 |
|
| 420 |
+
# Plot 10: Violin Plot of Phenotype by Genotype
|
| 421 |
+
plot_phenotype_genotype <- function(cross, qtl_row, trait) {
|
| 422 |
if (is.null(cross) || is.null(qtl_row) || nrow(qtl_row) == 0) return(NULL)
|
| 423 |
|
| 424 |
+
# Find nearest marker
|
| 425 |
chr_name <- as.character(qtl_row$Chromosome[1])
|
| 426 |
if (!chr_name %in% names(cross$geno)) return(NULL)
|
| 427 |
|
| 428 |
+
map <- pull.map(cross)[[chr_name]]
|
| 429 |
marker_pos <- which.min(abs(map - qtl_row$Position_cM[1]))
|
| 430 |
marker_name <- names(map)[marker_pos]
|
| 431 |
|
| 432 |
+
# Get data
|
| 433 |
+
geno_idx <- find.marker(cross, chr_name, marker_name)
|
| 434 |
+
genotypes <- pull.geno(cross)[, geno_idx]
|
| 435 |
phenotypes <- cross$pheno[[trait]]
|
| 436 |
|
| 437 |
df <- data.frame(
|
| 438 |
+
Genotype = factor(genotypes, levels = sort(unique(genotypes))),
|
| 439 |
+
Phenotype = phenotypes
|
| 440 |
)
|
| 441 |
+
df <- df[!is.na(df$Genotype) & !is.na(df$Phenotype), ]
|
| 442 |
|
| 443 |
+
# Recode genotypes for better labels
|
| 444 |
+
if (length(unique(df$Genotype)) == 3) {
|
| 445 |
+
df$Genotype <- recode(df$Genotype, `1` = "AA", `2` = "AB", `3` = "BB")
|
| 446 |
+
} else if (length(unique(df$Genotype)) == 2) {
|
| 447 |
+
df$Genotype <- recode(df$Genotype, `1` = "A", `2` = "B")
|
| 448 |
+
}
|
| 449 |
|
| 450 |
+
ggplot(df, aes(x = Genotype, y = Phenotype, fill = Genotype)) +
|
| 451 |
+
geom_violin(alpha = 0.6, trim = FALSE) +
|
| 452 |
+
geom_boxplot(width = 0.2, alpha = 0.8) +
|
| 453 |
stat_summary(fun = mean, geom = "point", shape = 18, size = 4, color = "red") +
|
| 454 |
+
stat_summary(fun = mean, geom = "text", aes(label = round(..y.., 2)),
|
| 455 |
+
vjust = -1, size = 3) +
|
| 456 |
scale_fill_brewer(palette = "Set2") +
|
| 457 |
+
labs(title = paste("Phenotype Distribution by Genotype at", marker_name),
|
| 458 |
x = "Genotype", y = trait) +
|
| 459 |
theme_minimal() +
|
| 460 |
theme(legend.position = "none")
|
| 461 |
}
|
| 462 |
|
| 463 |
+
# Plot 11: LOD Contour Plot
|
| 464 |
+
plot_lod_contour <- function(scan) {
|
| 465 |
+
if (is.null(scan)) return(NULL)
|
| 466 |
|
| 467 |
+
# Create grid for contour
|
| 468 |
+
chrs <- unique(scan$chr)
|
| 469 |
+
contour_data <- data.frame()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 470 |
|
| 471 |
+
for (chr in chrs) {
|
| 472 |
+
chr_data <- scan[scan$chr == chr, ]
|
| 473 |
+
chr_data$chr_num <- as.numeric(factor(chr, levels = chrs))
|
| 474 |
+
contour_data <- rbind(contour_data, chr_data)
|
| 475 |
+
}
|
| 476 |
|
| 477 |
+
ggplot(contour_data, aes(x = pos, y = chr_num, z = lod)) +
|
| 478 |
+
geom_contour_filled(bins = 10) +
|
| 479 |
+
scale_fill_viridis_d(name = "LOD") +
|
| 480 |
+
scale_y_continuous(breaks = 1:length(chrs), labels = chrs) +
|
| 481 |
+
labs(title = "LOD Contour Plot Across Genome",
|
| 482 |
+
x = "Position (cM)", y = "Chromosome") +
|
| 483 |
+
theme_minimal()
|
|
|
|
|
|
|
| 484 |
}
|
| 485 |
|
| 486 |
+
# Plot 12: Radar Chart for Model Metrics
|
| 487 |
+
plot_model_radar <- function(models) {
|
| 488 |
+
if (is.null(models) || length(models) == 0) return(NULL)
|
| 489 |
+
|
| 490 |
+
# Calculate metrics
|
| 491 |
+
metrics <- data.frame()
|
| 492 |
+
for (name in names(models)) {
|
| 493 |
+
scan <- models[[name]]
|
| 494 |
+
if (!is.null(scan)) {
|
| 495 |
+
metrics <- rbind(metrics, data.frame(
|
| 496 |
+
Model = toupper(name),
|
| 497 |
+
Max_LOD = max(scan$lod, na.rm = TRUE),
|
| 498 |
+
Mean_LOD = mean(scan$lod, na.rm = TRUE),
|
| 499 |
+
N_Peaks = sum(scan$lod > 3, na.rm = TRUE),
|
| 500 |
+
Max_Position = scan$pos[which.max(scan$lod)]
|
| 501 |
+
))
|
| 502 |
+
}
|
| 503 |
+
}
|
| 504 |
|
| 505 |
+
# Normalize for radar
|
| 506 |
+
metrics_norm <- metrics
|
| 507 |
+
for (col in c("Max_LOD", "Mean_LOD", "N_Peaks")) {
|
| 508 |
+
metrics_norm[[col]] <- (metrics_norm[[col]] - min(metrics_norm[[col]])) /
|
| 509 |
+
(max(metrics_norm[[col]]) - min(metrics_norm[[col]]))
|
| 510 |
+
}
|
| 511 |
|
| 512 |
+
# Reshape for plotting
|
| 513 |
+
radar_data <- pivot_longer(metrics_norm,
|
| 514 |
+
cols = c("Max_LOD", "Mean_LOD", "N_Peaks"),
|
| 515 |
+
names_to = "Metric", values_to = "Value")
|
| 516 |
+
|
| 517 |
+
ggplot(radar_data, aes(x = Metric, y = Value, color = Model, group = Model)) +
|
| 518 |
+
geom_line(size = 1) +
|
| 519 |
+
geom_point(size = 3) +
|
| 520 |
+
coord_polar() +
|
| 521 |
+
scale_color_brewer(palette = "Set1") +
|
| 522 |
+
labs(title = "Model Performance Radar Chart",
|
| 523 |
+
y = "Normalized Score") +
|
| 524 |
+
theme_minimal()
|
| 525 |
}
|
| 526 |
|
| 527 |
# ============================================================================
|
|
|
|
| 529 |
# ============================================================================
|
| 530 |
|
| 531 |
ui <- dashboardPage(
|
| 532 |
+
dashboardHeader(title = "QTL Analysis Pipeline - 10 Models"),
|
| 533 |
|
| 534 |
+
dashboardSidebar(width = 320,
|
| 535 |
sidebarMenu(
|
| 536 |
menuItem("Data Input", tabName = "data", icon = icon("upload")),
|
| 537 |
+
menuItem("10 Models Analysis", tabName = "models", icon = icon("chart-line")),
|
| 538 |
+
menuItem("10+ Visualizations", tabName = "viz", icon = icon("chart-bar")),
|
| 539 |
menuItem("QTL Results", tabName = "results", icon = icon("table")),
|
| 540 |
+
menuItem("Model Comparison", tabName = "comparison", icon = icon("compare")),
|
| 541 |
menuItem("Documentation", tabName = "help", icon = icon("question-circle"))
|
| 542 |
),
|
| 543 |
|
| 544 |
div(style = "padding: 15px;",
|
| 545 |
h4("Data Upload", style = "color: #3c8dbc;"),
|
| 546 |
+
fileInput("geno_file", "Genotype CSV", accept = ".csv"),
|
| 547 |
+
fileInput("pheno_file", "Phenotype CSV", accept = ".csv"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 548 |
|
|
|
|
| 549 |
selectInput("cross_type", "Cross Type",
|
| 550 |
+
choices = c("F2 Intercross" = "f2", "Backcross" = "bc", "RIL" = "riself"),
|
|
|
|
|
|
|
| 551 |
selected = "f2"),
|
| 552 |
|
| 553 |
+
hr(),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 554 |
|
| 555 |
+
h4("Analysis Settings", style = "color: #3c8dbc;"),
|
| 556 |
+
selectInput("pheno_trait", "Phenotype Trait", choices = NULL),
|
| 557 |
|
| 558 |
+
numericInput("lod_threshold", "LOD Threshold", value = 3.0, min = 0, max = 20, step = 0.5),
|
| 559 |
+
numericInput("n_permutations", "Permutations", value = 100, min = 0, max = 1000),
|
| 560 |
|
| 561 |
+
actionButton("run_analysis", "RUN 10 MODELS",
|
| 562 |
icon = icon("play"),
|
| 563 |
style = "background-color: #3c8dbc; color: white; width: 100%; margin-top: 15px;"),
|
| 564 |
|
| 565 |
br(), br(),
|
| 566 |
|
| 567 |
+
downloadButton("download_results", "Download Results", style = "width: 100%;")
|
|
|
|
| 568 |
)
|
| 569 |
),
|
| 570 |
|
|
|
|
| 572 |
useShinyjs(),
|
| 573 |
tags$head(
|
| 574 |
tags$style(HTML("
|
| 575 |
+
.content-wrapper, .right-side { background-color: #f4f4f4; }
|
| 576 |
+
.box { border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
|
| 577 |
+
.small-box { border-radius: 10px; }
|
|
|
|
|
|
|
|
|
|
|
|
|
| 578 |
"))
|
| 579 |
),
|
| 580 |
|
|
|
|
| 592 |
)
|
| 593 |
),
|
| 594 |
fluidRow(
|
| 595 |
+
box(title = "Genetic Map", status = "info", width = 12,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 596 |
plotOutput("genetic_map", height = "500px") %>% withSpinner()
|
| 597 |
)
|
| 598 |
)
|
| 599 |
),
|
| 600 |
|
| 601 |
+
# 10 Models Tab
|
| 602 |
+
tabItem(tabName = "models",
|
| 603 |
fluidRow(
|
| 604 |
box(title = "Analysis Status", status = "primary", solidHeader = TRUE, width = 12,
|
| 605 |
verbatimTextOutput("analysis_status")
|
| 606 |
)
|
| 607 |
),
|
| 608 |
fluidRow(
|
| 609 |
+
box(title = "Multi-Model Manhattan Plot", status = "info", width = 12,
|
| 610 |
+
plotOutput("multi_manhattan", height = "600px") %>% withSpinner()
|
| 611 |
)
|
| 612 |
),
|
| 613 |
fluidRow(
|
| 614 |
+
box(title = "Model Performance Heatmap", status = "success", width = 12,
|
| 615 |
+
plotOutput("model_heatmap", height = "600px") %>% withSpinner()
|
| 616 |
)
|
| 617 |
),
|
| 618 |
fluidRow(
|
| 619 |
+
box(title = "Model Radar Chart", status = "warning", width = 12,
|
| 620 |
+
plotOutput("radar_chart", height = "500px") %>% withSpinner()
|
| 621 |
)
|
| 622 |
)
|
| 623 |
),
|
|
|
|
| 625 |
# Visualizations Tab
|
| 626 |
tabItem(tabName = "viz",
|
| 627 |
fluidRow(
|
| 628 |
+
box(title = "3D LOD Surface Plot", status = "primary", width = 12,
|
| 629 |
+
plotlyOutput("surface_3d", height = "600px") %>% withSpinner()
|
| 630 |
+
)
|
| 631 |
+
),
|
| 632 |
+
fluidRow(
|
| 633 |
+
box(title = "LOD Ridge Plot", status = "info", width = 6,
|
| 634 |
+
plotOutput("ridge_plot", height = "500px") %>% withSpinner()
|
| 635 |
),
|
| 636 |
+
box(title = "LOD Contour Plot", status = "success", width = 6,
|
| 637 |
+
plotOutput("contour_plot", height = "500px") %>% withSpinner()
|
| 638 |
)
|
| 639 |
),
|
| 640 |
fluidRow(
|
| 641 |
+
box(title = "QTL Effect Bubble Plot", status = "warning", width = 12,
|
| 642 |
+
plotOutput("bubble_plot", height = "500px") %>% withSpinner()
|
| 643 |
+
)
|
| 644 |
+
),
|
| 645 |
+
fluidRow(
|
| 646 |
+
box(title = "Parallel Coordinates Comparison", status = "danger", width = 12,
|
| 647 |
+
plotOutput("parallel_plot", height = "500px") %>% withSpinner()
|
| 648 |
+
)
|
| 649 |
+
),
|
| 650 |
+
fluidRow(
|
| 651 |
+
box(title = "QQ Plot Model Comparison", status = "primary", width = 12,
|
| 652 |
+
plotOutput("qq_plot", height = "500px") %>% withSpinner()
|
| 653 |
+
)
|
| 654 |
+
),
|
| 655 |
+
fluidRow(
|
| 656 |
+
box(title = "Trait Correlation Circle", status = "info", width = 6,
|
| 657 |
+
plotOutput("correlation_circle", height = "500px") %>% withSpinner()
|
| 658 |
),
|
| 659 |
+
box(title = "Map Density", status = "success", width = 6,
|
| 660 |
+
plotOutput("map_density", height = "500px") %>% withSpinner()
|
| 661 |
)
|
| 662 |
),
|
| 663 |
fluidRow(
|
| 664 |
+
box(title = "Genotype-Phenotype Association", status = "warning", width = 12,
|
| 665 |
+
selectInput("effect_qtl", "Select QTL:", choices = NULL),
|
| 666 |
+
plotOutput("violin_plot", height = "500px") %>% withSpinner()
|
| 667 |
)
|
| 668 |
)
|
| 669 |
),
|
|
|
|
| 676 |
)
|
| 677 |
),
|
| 678 |
fluidRow(
|
| 679 |
+
box(title = "Model Performance Summary", status = "info", width = 12,
|
| 680 |
+
DTOutput("model_summary") %>% withSpinner()
|
| 681 |
+
)
|
| 682 |
+
),
|
| 683 |
+
fluidRow(
|
| 684 |
+
box(title = "Permutation Results", status = "success", width = 12,
|
| 685 |
DTOutput("permutation_table") %>% withSpinner()
|
| 686 |
)
|
| 687 |
+
)
|
| 688 |
+
),
|
| 689 |
+
|
| 690 |
+
# Model Comparison Tab
|
| 691 |
+
tabItem(tabName = "comparison",
|
| 692 |
+
fluidRow(
|
| 693 |
+
box(title = "Model Performance Metrics", status = "primary", width = 12,
|
| 694 |
+
DTOutput("comparison_table") %>% withSpinner()
|
| 695 |
+
)
|
| 696 |
),
|
| 697 |
fluidRow(
|
| 698 |
+
box(title = "Best Model per Chromosome", status = "info", width = 12,
|
| 699 |
+
DTOutput("best_model_table") %>% withSpinner()
|
| 700 |
)
|
| 701 |
)
|
| 702 |
),
|
|
|
|
| 704 |
# Help Tab
|
| 705 |
tabItem(tabName = "help",
|
| 706 |
fluidRow(
|
| 707 |
+
box(title = "Documentation - 10 Models & 10+ Plots", status = "primary", width = 12,
|
| 708 |
HTML("
|
| 709 |
+
<h3>10 Mapping Models Included:</h3>
|
| 710 |
+
<ol>
|
| 711 |
+
<li><strong>Haley-Knott (HK)</strong> - Fast regression-based method</li>
|
| 712 |
+
<li><strong>EM Algorithm</strong> - Expectation-maximization for missing data</li>
|
| 713 |
+
<li><strong>Extended Haley-Knott (EHK)</strong> - Improved HK method</li>
|
| 714 |
+
<li><strong>Multiple Imputation (IMP)</strong> - Handles missing genotypes</li>
|
| 715 |
+
<li><strong>Marker Regression (MR)</strong> - Simple marker-by-marker regression</li>
|
| 716 |
+
<li><strong>Non-parametric (NP)</strong> - Distribution-free method</li>
|
| 717 |
+
<li><strong>Composite Interval Mapping (CIM)</strong> - Controls for background QTLs</li>
|
| 718 |
+
<li><strong>Multiple QTL Mapping (MQM)</strong> - Uses cofactors</li>
|
| 719 |
+
<li><strong>Binary Trait</strong> - For case/control studies</li>
|
| 720 |
+
<li><strong>Normal Model</strong> - Assumes normal distribution</li>
|
| 721 |
+
</ol>
|
| 722 |
|
| 723 |
+
<h3>10+ Unique Visualizations:</h3>
|
| 724 |
+
<ol>
|
| 725 |
+
<li>Multi-Model Manhattan Plot</li>
|
| 726 |
+
<li>Model Performance Heatmap</li>
|
| 727 |
+
<li>3D LOD Surface Plot</li>
|
| 728 |
+
<li>QQ Plot for Model Comparison</li>
|
| 729 |
+
<li>LOD Score Ridge Plot</li>
|
| 730 |
+
<li>QTL Effect Bubble Plot</li>
|
| 731 |
+
<li>Trait Correlation Circle</li>
|
| 732 |
+
<li>Genetic Map Density</li>
|
| 733 |
+
<li>Parallel Coordinates Comparison</li>
|
| 734 |
+
<li>Violin Plot by Genotype</li>
|
| 735 |
+
<li>LOD Contour Plot</li>
|
| 736 |
+
<li>Model Radar Chart</li>
|
| 737 |
+
</ol>
|
| 738 |
|
| 739 |
+
<h3>File Format:</h3>
|
| 740 |
+
<p><strong>Genotype CSV:</strong></p>
|
| 741 |
<pre>
|
| 742 |
+
id,marker1,marker2,marker3
|
| 743 |
+
,chr1,chr1,chr2
|
| 744 |
+
,0,5.2,10.1
|
| 745 |
+
sample1,1,2,1
|
| 746 |
+
sample2,2,1,3</pre>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 747 |
|
| 748 |
+
<p><strong>Phenotype CSV:</strong></p>
|
| 749 |
+
<pre>
|
| 750 |
+
id,trait1,trait2
|
| 751 |
+
sample1,12.5,175
|
| 752 |
+
sample2,13.2,180</pre>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 753 |
")
|
| 754 |
)
|
| 755 |
)
|
|
|
|
| 766 |
|
| 767 |
# Reactive values
|
| 768 |
cross_data <- reactiveVal(NULL)
|
| 769 |
+
all_models <- reactiveVal(NULL)
|
| 770 |
+
qtl_results <- reactiveVal(NULL)
|
| 771 |
+
perm_results <- reactiveVal(NULL)
|
| 772 |
analysis_done <- reactiveVal(FALSE)
|
| 773 |
|
| 774 |
# Value boxes
|
| 775 |
output$n_individuals <- renderValueBox({
|
| 776 |
cross <- cross_data()
|
| 777 |
+
valueBox(
|
| 778 |
+
ifelse(is.null(cross), 0, nind(cross)),
|
| 779 |
+
"Individuals", icon = icon("users"), color = "blue"
|
| 780 |
+
)
|
|
|
|
| 781 |
})
|
| 782 |
|
| 783 |
output$n_markers <- renderValueBox({
|
| 784 |
cross <- cross_data()
|
| 785 |
+
valueBox(
|
| 786 |
+
ifelse(is.null(cross), 0, totmar(cross)),
|
| 787 |
+
"Markers", icon = icon("dna"), color = "green"
|
| 788 |
+
)
|
|
|
|
| 789 |
})
|
| 790 |
|
| 791 |
output$n_chromosomes <- renderValueBox({
|
| 792 |
cross <- cross_data()
|
| 793 |
+
valueBox(
|
| 794 |
+
ifelse(is.null(cross), 0, nchr(cross)),
|
| 795 |
+
"Chromosomes", icon = icon("chromosome"), color = "yellow"
|
| 796 |
+
)
|
|
|
|
| 797 |
})
|
| 798 |
|
| 799 |
# Load data
|
|
|
|
| 810 |
)
|
| 811 |
|
| 812 |
cross_data(cross)
|
|
|
|
|
|
|
| 813 |
updateSelectInput(session, "pheno_trait",
|
| 814 |
choices = names(cross$pheno),
|
| 815 |
selected = names(cross$pheno)[1])
|
|
|
|
| 821 |
})
|
| 822 |
})
|
| 823 |
|
| 824 |
+
# Run all 10 models
|
| 825 |
observeEvent(input$run_analysis, {
|
| 826 |
cross <- cross_data()
|
| 827 |
req(cross, input$pheno_trait)
|
| 828 |
|
| 829 |
+
showNotification("Running 10 mapping models...", type = "info", duration = 5)
|
| 830 |
|
| 831 |
+
withProgress(message = "Running 10 QTL Models", value = 0, {
|
| 832 |
|
| 833 |
+
incProgress(0.2, detail = "Running all mapping algorithms...")
|
| 834 |
pheno_idx <- which(names(cross$pheno) == input$pheno_trait)
|
| 835 |
+
models <- run_all_models(cross, pheno_idx)
|
| 836 |
+
all_models(models)
|
| 837 |
|
| 838 |
+
# Find QTLs from best model (use HK if available)
|
| 839 |
+
if (!is.null(models$hk)) {
|
| 840 |
+
qtls <- find_qtls(models$hk, input$lod_threshold)
|
| 841 |
+
qtl_results(qtls)
|
| 842 |
|
| 843 |
+
if (nrow(qtls) > 0) {
|
| 844 |
+
qtl_choices <- paste0("Chr", qtls$Chromosome, "_", qtls$Position_cM, "cM")
|
| 845 |
+
updateSelectInput(session, "effect_qtl", choices = qtl_choices)
|
|
|
|
|
|
|
|
|
|
| 846 |
}
|
| 847 |
}
|
| 848 |
|
| 849 |
+
# Run permutations if requested
|
| 850 |
+
if (input$n_permutations > 0 && !is.null(models$hk)) {
|
| 851 |
+
incProgress(0.2, detail = paste("Running", input$n_permutations, "permutations..."))
|
| 852 |
+
perms <- scanone(cross, pheno.col = pheno_idx,
|
| 853 |
+
n.perm = input$n_permutations, method = "hk")
|
| 854 |
+
perm_results(perms)
|
|
|
|
|
|
|
| 855 |
}
|
| 856 |
|
| 857 |
analysis_done(TRUE)
|
| 858 |
+
showNotification(paste("Analysis complete!", length(models), "models run successfully"),
|
| 859 |
+
type = "success", duration = 5)
|
| 860 |
})
|
| 861 |
})
|
| 862 |
|
| 863 |
+
# Data summary
|
| 864 |
output$data_summary <- renderPrint({
|
| 865 |
cross <- cross_data()
|
| 866 |
if (is.null(cross)) {
|
|
|
|
| 871 |
cat("Individuals:", nind(cross), "\n")
|
| 872 |
cat("Markers:", totmar(cross), "\n")
|
| 873 |
cat("Chromosomes:", nchr(cross), "\n\n")
|
| 874 |
+
cat("Phenotypes:", paste(names(cross$pheno), collapse = ", "), "\n")
|
| 875 |
|
| 876 |
+
missing_total <- sum(is.na(pull.geno(cross))) / (nind(cross) * totmar(cross)) * 100
|
| 877 |
+
cat("\nMissing data rate:", round(missing_total, 2), "%\n")
|
|
|
|
| 878 |
}
|
| 879 |
})
|
| 880 |
|
| 881 |
output$analysis_status <- renderPrint({
|
| 882 |
+
models <- all_models()
|
| 883 |
if (!analysis_done()) {
|
| 884 |
+
cat("Analysis not run. Click 'RUN 10 MODELS' to start.")
|
| 885 |
} else {
|
|
|
|
|
|
|
| 886 |
cat("=== Analysis Complete ===\n\n")
|
| 887 |
+
cat("Models run:", length(models), "/10\n")
|
| 888 |
+
cat("Models:", paste(toupper(names(models)), collapse = ", "), "\n\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 889 |
|
| 890 |
+
# Best model
|
| 891 |
+
best_lod <- 0
|
| 892 |
+
best_model <- ""
|
| 893 |
+
for (name in names(models)) {
|
| 894 |
+
if (!is.null(models[[name]])) {
|
| 895 |
+
max_lod <- max(models[[name]]$lod, na.rm = TRUE)
|
| 896 |
+
if (max_lod > best_lod) {
|
| 897 |
+
best_lod <- max_lod
|
| 898 |
+
best_model <- name
|
| 899 |
+
}
|
| 900 |
+
}
|
| 901 |
}
|
| 902 |
+
cat("Best model:", toupper(best_model), "(Max LOD =", round(best_lod, 3), ")\n")
|
| 903 |
}
|
| 904 |
})
|
| 905 |
|
| 906 |
+
# Genetic map
|
| 907 |
+
output$genetic_map <- renderPlot({
|
| 908 |
cross <- cross_data()
|
| 909 |
req(cross)
|
| 910 |
+
|
| 911 |
+
map <- pull.map(cross)
|
| 912 |
+
df <- data.frame()
|
| 913 |
+
for (chr in names(map)) {
|
| 914 |
+
positions <- map[[chr]]
|
| 915 |
+
df <- rbind(df, data.frame(Chromosome = chr, Position = positions))
|
| 916 |
+
}
|
| 917 |
+
|
| 918 |
+
df$Chromosome <- factor(df$Chromosome, levels = names(map))
|
| 919 |
+
|
| 920 |
+
ggplot(df, aes(x = Chromosome, y = Position)) +
|
| 921 |
+
geom_segment(aes(xend = Chromosome, yend = 0), color = "gray70", size = 1) +
|
| 922 |
+
geom_point(color = "steelblue", size = 2, alpha = 0.7) +
|
| 923 |
+
labs(title = "Genetic Map", x = "Chromosome", y = "Position (cM)") +
|
| 924 |
+
theme_minimal() +
|
| 925 |
+
theme(axis.text.x = element_text(angle = 45, hjust = 1))
|
| 926 |
})
|
| 927 |
|
| 928 |
+
# Multi-model Manhattan
|
| 929 |
+
output$multi_manhattan <- renderPlot({
|
| 930 |
+
models <- all_models()
|
| 931 |
+
req(models, length(models) > 0)
|
| 932 |
+
plot_multi_manhattan(models, input$lod_threshold)
|
| 933 |
+
})
|
| 934 |
+
|
| 935 |
+
# Model heatmap
|
| 936 |
+
output$model_heatmap <- renderPlot({
|
| 937 |
+
models <- all_models()
|
| 938 |
+
req(models)
|
| 939 |
+
plot_model_heatmap(models)
|
| 940 |
})
|
| 941 |
|
| 942 |
+
# Radar chart
|
| 943 |
+
output$radar_chart <- renderPlot({
|
| 944 |
+
models <- all_models()
|
| 945 |
+
req(models)
|
| 946 |
+
plot_model_radar(models)
|
| 947 |
})
|
| 948 |
|
| 949 |
+
# 3D surface
|
| 950 |
+
output$surface_3d <- renderPlotly({
|
| 951 |
+
models <- all_models()
|
| 952 |
+
req(models, !is.null(models$hk))
|
| 953 |
+
plot_3d_surface(models$hk)
|
| 954 |
})
|
| 955 |
|
| 956 |
+
# Ridge plot
|
| 957 |
output$ridge_plot <- renderPlot({
|
| 958 |
+
models <- all_models()
|
| 959 |
+
req(models, !is.null(models$hk))
|
| 960 |
+
plot_lod_ridges(models$hk)
|
| 961 |
})
|
| 962 |
|
| 963 |
+
# Contour plot
|
| 964 |
+
output$contour_plot <- renderPlot({
|
| 965 |
+
models <- all_models()
|
| 966 |
+
req(models, !is.null(models$hk))
|
| 967 |
+
plot_lod_contour(models$hk)
|
| 968 |
})
|
| 969 |
|
| 970 |
+
# Bubble plot
|
| 971 |
+
output$bubble_plot <- renderPlot({
|
| 972 |
+
qtls <- qtl_results()
|
| 973 |
+
req(qtls, nrow(qtls) > 0)
|
| 974 |
+
plot_qtl_bubbles(qtls)
|
| 975 |
})
|
| 976 |
|
| 977 |
+
# Parallel plot
|
| 978 |
+
output$parallel_plot <- renderPlot({
|
| 979 |
+
models <- all_models()
|
| 980 |
+
req(models, length(models) > 0)
|
| 981 |
+
plot_parallel_models(models)
|
|
|
|
| 982 |
})
|
| 983 |
|
| 984 |
+
# QQ plot
|
| 985 |
+
output$qq_plot <- renderPlot({
|
| 986 |
+
models <- all_models()
|
| 987 |
+
req(models, length(models) > 0)
|
| 988 |
+
plot_qq_comparison(models)
|
| 989 |
+
})
|
| 990 |
+
|
| 991 |
+
# Correlation circle
|
| 992 |
+
output$correlation_circle <- renderPlot({
|
| 993 |
cross <- cross_data()
|
| 994 |
req(cross)
|
| 995 |
+
plot_trait_circle(cross)
|
| 996 |
})
|
| 997 |
|
| 998 |
+
# Map density
|
| 999 |
+
output$map_density <- renderPlot({
|
| 1000 |
+
cross <- cross_data()
|
| 1001 |
+
req(cross)
|
| 1002 |
+
plot_map_density(cross)
|
| 1003 |
+
})
|
| 1004 |
+
|
| 1005 |
+
# Violin plot
|
| 1006 |
+
output$violin_plot <- renderPlot({
|
| 1007 |
req(input$effect_qtl, input$effect_qtl != "")
|
| 1008 |
+
qtls <- qtl_results()
|
| 1009 |
cross <- cross_data()
|
| 1010 |
req(qtls, nrow(qtls) > 0, cross, input$pheno_trait)
|
| 1011 |
|
| 1012 |
+
qtl_idx <- which(paste0("Chr", qtls$Chromosome, "_", qtls$Position_cM, "cM") == input$effect_qtl)
|
|
|
|
| 1013 |
if (length(qtl_idx) == 0) return(NULL)
|
| 1014 |
|
| 1015 |
+
plot_phenotype_genotype(cross, qtls[qtl_idx[1], , drop = FALSE], input$pheno_trait)
|
| 1016 |
})
|
| 1017 |
|
| 1018 |
+
# QTL table
|
| 1019 |
output$qtl_table <- renderDT({
|
| 1020 |
+
qtls <- qtl_results()
|
| 1021 |
req(qtls, nrow(qtls) > 0)
|
| 1022 |
|
| 1023 |
+
datatable(qtls, options = list(pageLength = 15, scrollX = TRUE), rownames = FALSE) %>%
|
| 1024 |
+
formatStyle("LOD", background = styleColorBar(range(qtls$LOD), "lightblue"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1025 |
})
|
| 1026 |
|
| 1027 |
+
# Model summary
|
| 1028 |
+
output$model_summary <- renderDT({
|
| 1029 |
+
models <- all_models()
|
| 1030 |
+
req(models)
|
| 1031 |
+
|
| 1032 |
+
summary_df <- data.frame()
|
| 1033 |
+
for (name in names(models)) {
|
| 1034 |
+
scan <- models[[name]]
|
| 1035 |
+
if (!is.null(scan)) {
|
| 1036 |
+
summary_df <- rbind(summary_df, data.frame(
|
| 1037 |
+
Model = toupper(name),
|
| 1038 |
+
Max_LOD = round(max(scan$lod, na.rm = TRUE), 3),
|
| 1039 |
+
Mean_LOD = round(mean(scan$lod, na.rm = TRUE), 3),
|
| 1040 |
+
N_Peaks = sum(scan$lod > input$lod_threshold, na.rm = TRUE),
|
| 1041 |
+
Chromosome = scan$chr[which.max(scan$lod)],
|
| 1042 |
+
Position = round(scan$pos[which.max(scan$lod)], 2)
|
| 1043 |
+
))
|
| 1044 |
+
}
|
| 1045 |
+
}
|
| 1046 |
+
|
| 1047 |
+
datatable(summary_df, options = list(pageLength = 10), rownames = FALSE)
|
| 1048 |
+
})
|
| 1049 |
+
|
| 1050 |
+
# Permutation table
|
| 1051 |
output$permutation_table <- renderDT({
|
| 1052 |
+
perms <- perm_results()
|
| 1053 |
+
req(perms)
|
| 1054 |
|
|
|
|
| 1055 |
alphas <- c(0.05, 0.01, 0.001)
|
| 1056 |
thresholds <- sapply(alphas, function(a) {
|
| 1057 |
+
thr <- summary(perms, alpha = a)[1]
|
| 1058 |
if (is.na(thr)) NA else round(thr, 3)
|
| 1059 |
})
|
| 1060 |
|
| 1061 |
+
perm_df <- data.frame(Alpha = alphas, Threshold = thresholds)
|
| 1062 |
+
datatable(perm_df, options = list(pageLength = 5, dom = 't'), rownames = FALSE)
|
| 1063 |
+
})
|
| 1064 |
+
|
| 1065 |
+
# Comparison table
|
| 1066 |
+
output$comparison_table <- renderDT({
|
| 1067 |
+
models <- all_models()
|
| 1068 |
+
req(models)
|
| 1069 |
+
|
| 1070 |
+
comparison_df <- data.frame()
|
| 1071 |
+
for (name in names(models)) {
|
| 1072 |
+
scan <- models[[name]]
|
| 1073 |
+
if (!is.null(scan)) {
|
| 1074 |
+
comparison_df <- rbind(comparison_df, data.frame(
|
| 1075 |
+
Model = toupper(name),
|
| 1076 |
+
Max_LOD = round(max(scan$lod, na.rm = TRUE), 3),
|
| 1077 |
+
Mean_LOD = round(mean(scan$lod, na.rm = TRUE), 3),
|
| 1078 |
+
Median_LOD = round(median(scan$lod, na.rm = TRUE), 3),
|
| 1079 |
+
SD_LOD = round(sd(scan$lod, na.rm = TRUE), 3),
|
| 1080 |
+
CV_LOD = round(sd(scan$lod, na.rm = TRUE) / mean(scan$lod, na.rm = TRUE) * 100, 2),
|
| 1081 |
+
Top_Chromosome = scan$chr[which.max(scan$lod)],
|
| 1082 |
+
Top_Position = round(scan$pos[which.max(scan$lod)], 2)
|
| 1083 |
+
))
|
| 1084 |
+
}
|
| 1085 |
+
}
|
| 1086 |
+
|
| 1087 |
+
datatable(comparison_df, options = list(pageLength = 10, scrollX = TRUE), rownames = FALSE) %>%
|
| 1088 |
+
formatStyle("Max_LOD", background = styleColorBar(range(comparison_df$Max_LOD), "lightgreen"))
|
| 1089 |
+
})
|
| 1090 |
+
|
| 1091 |
+
# Best model table
|
| 1092 |
+
output$best_model_table <- renderDT({
|
| 1093 |
+
models <- all_models()
|
| 1094 |
+
req(models)
|
| 1095 |
+
|
| 1096 |
+
# Get all chromosomes
|
| 1097 |
+
first_model <- models[[1]]
|
| 1098 |
+
if (is.null(first_model)) return(NULL)
|
| 1099 |
+
|
| 1100 |
+
chrs <- unique(first_model$chr)
|
| 1101 |
+
best_df <- data.frame()
|
| 1102 |
+
|
| 1103 |
+
for (chr in chrs) {
|
| 1104 |
+
best_lod <- -Inf
|
| 1105 |
+
best_model <- ""
|
| 1106 |
+
for (name in names(models)) {
|
| 1107 |
+
scan <- models[[name]]
|
| 1108 |
+
if (!is.null(scan)) {
|
| 1109 |
+
chr_max <- max(scan$lod[scan$chr == chr], na.rm = TRUE)
|
| 1110 |
+
if (chr_max > best_lod) {
|
| 1111 |
+
best_lod <- chr_max
|
| 1112 |
+
best_model <- name
|
| 1113 |
+
}
|
| 1114 |
+
}
|
| 1115 |
+
}
|
| 1116 |
+
best_df <- rbind(best_df, data.frame(
|
| 1117 |
+
Chromosome = chr,
|
| 1118 |
+
Best_Model = toupper(best_model),
|
| 1119 |
+
Peak_LOD = round(best_lod, 3)
|
| 1120 |
+
))
|
| 1121 |
+
}
|
| 1122 |
|
| 1123 |
+
datatable(best_df, options = list(pageLength = 15), rownames = FALSE)
|
|
|
|
|
|
|
| 1124 |
})
|
| 1125 |
|
| 1126 |
# Download handler
|
| 1127 |
output$download_results <- downloadHandler(
|
| 1128 |
+
filename = function() paste0("qtl_10models_", Sys.Date(), ".csv"),
|
| 1129 |
content = function(file) {
|
| 1130 |
+
qtls <- qtl_results()
|
| 1131 |
if (!is.null(qtls) && nrow(qtls) > 0) {
|
| 1132 |
write.csv(qtls, file, row.names = FALSE)
|
| 1133 |
} else {
|