Spaces:
Sleeping
Sleeping
Update app.R
Browse files
app.R
CHANGED
|
@@ -8,604 +8,490 @@ library(shinycssloaders)
|
|
| 8 |
library(shinyjs)
|
| 9 |
library(plotly)
|
| 10 |
library(gridExtra)
|
|
|
|
| 11 |
|
| 12 |
-
#
|
| 13 |
parse_custom_cross <- function(geno_path, pheno_path, cross_type = "f2") {
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
list(data=g_chr, map=map_list[[ch]])
|
| 80 |
-
})
|
| 81 |
-
names(geno_list) <- unique_chrs
|
| 82 |
-
for (ch in unique_chrs) class(geno_list[[ch]]$map) <- "A"
|
| 83 |
-
|
| 84 |
-
cross_obj <- list(geno=geno_list, pheno=pheno_df)
|
| 85 |
-
class(cross_obj) <- c(cross_type, "cross")
|
| 86 |
-
cross_obj <- jittermap(cross_obj)
|
| 87 |
-
if (cross_type=="f2") {
|
| 88 |
-
cross_obj <- tryCatch(
|
| 89 |
-
convert2bcsft(cross_obj, BC.gen=0, F.gen=2, estimate.map=FALSE),
|
| 90 |
-
error=function(e) cross_obj
|
| 91 |
-
)
|
| 92 |
-
}
|
| 93 |
-
cross_obj
|
| 94 |
-
}
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
check.names=FALSE, na.strings=c("NA","","N/A"))
|
| 100 |
-
col_names <- as.character(geno_raw[1,])
|
| 101 |
-
chr_row <- as.character(geno_raw[2,])
|
| 102 |
-
pos_row <- as.character(geno_raw[3,])
|
| 103 |
-
ind_data <- geno_raw[4:nrow(geno_raw),,drop=FALSE]
|
| 104 |
-
n_cols <- ncol(geno_raw)
|
| 105 |
-
marker_cols <- 2:n_cols
|
| 106 |
-
marker_names <- col_names[marker_cols]
|
| 107 |
-
chromosomes <- chr_row[marker_cols]
|
| 108 |
-
positions <- suppressWarnings(as.numeric(pos_row[marker_cols]))
|
| 109 |
-
|
| 110 |
-
# Clean chromosome labels
|
| 111 |
-
chromosomes <- gsub("^[Cc]hr0*", "", chromosomes)
|
| 112 |
-
chr_numeric <- suppressWarnings(as.integer(chromosomes))
|
| 113 |
-
chromosomes <- ifelse(!is.na(chr_numeric), as.character(chr_numeric), chromosomes)
|
| 114 |
-
|
| 115 |
-
ind_ids <- as.character(ind_data[,1])
|
| 116 |
-
geno_mat <- as.matrix(ind_data[,marker_cols])
|
| 117 |
-
class(geno_mat) <- "numeric"
|
| 118 |
-
geno_mat <- apply(geno_mat, 2, function(x) suppressWarnings(as.numeric(x)))
|
| 119 |
-
rownames(geno_mat) <- ind_ids
|
| 120 |
-
colnames(geno_mat) <- marker_names
|
| 121 |
-
|
| 122 |
-
# Read phenotype
|
| 123 |
-
pheno_raw <- read.csv(pheno_path, header=TRUE, stringsAsFactors=FALSE,
|
| 124 |
-
check.names=FALSE, na.strings=c("NA","","N/A"))
|
| 125 |
-
pheno_ids <- as.character(pheno_raw[,1])
|
| 126 |
-
pheno_df <- pheno_raw[,-1,drop=FALSE]
|
| 127 |
-
pheno_df <- as.data.frame(lapply(pheno_df, function(x) suppressWarnings(as.numeric(x))))
|
| 128 |
-
rownames(pheno_df) <- pheno_ids
|
| 129 |
-
|
| 130 |
-
# Match individuals
|
| 131 |
-
common_ids <- intersect(ind_ids, pheno_ids)
|
| 132 |
-
if (length(common_ids)==0) stop("No matching individual IDs.")
|
| 133 |
-
geno_mat <- geno_mat[common_ids,]
|
| 134 |
-
pheno_df <- pheno_df[common_ids,]
|
| 135 |
-
|
| 136 |
-
# Map list
|
| 137 |
-
unique_chrs <- unique(chromosomes)
|
| 138 |
-
chr_num <- suppressWarnings(as.integer(unique_chrs))
|
| 139 |
-
ord <- order(ifelse(!is.na(chr_num), chr_num, 999), unique_chrs)
|
| 140 |
-
unique_chrs <- unique_chrs[ord]
|
| 141 |
-
map_list <- lapply(unique_chrs, function(ch) {
|
| 142 |
-
idx <- which(chromosomes==ch)
|
| 143 |
-
pos <- positions[idx]
|
| 144 |
-
mnames <- marker_names[idx]
|
| 145 |
-
ord2 <- order(pos)
|
| 146 |
-
mnames <- mnames[ord2]
|
| 147 |
-
mpos <- pos[ord2]
|
| 148 |
-
names(mpos) <- mnames
|
| 149 |
-
mpos
|
| 150 |
-
})
|
| 151 |
-
names(map_list) <- unique_chrs
|
| 152 |
-
|
| 153 |
-
# Build geno list without convert2bcsft
|
| 154 |
-
geno_list <- lapply(unique_chrs, function(ch) {
|
| 155 |
-
idx <- which(chromosomes==ch)
|
| 156 |
-
mnames <- marker_names[idx]
|
| 157 |
-
pos_ch <- positions[idx]
|
| 158 |
-
ord2 <- order(pos_ch)
|
| 159 |
-
mnames <- mnames[ord2]
|
| 160 |
-
g_chr <- geno_mat[, mnames, drop=FALSE]
|
| 161 |
-
g_chr[g_chr==0] <- NA
|
| 162 |
-
storage.mode(g_chr) <- "integer"
|
| 163 |
-
list(data=g_chr, map=map_list[[ch]])
|
| 164 |
-
})
|
| 165 |
-
names(geno_list) <- unique_chrs
|
| 166 |
-
for (ch in unique_chrs) class(geno_list[[ch]]$map) <- "A"
|
| 167 |
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
| 172 |
}
|
| 173 |
|
| 174 |
-
#
|
| 175 |
-
|
| 176 |
ui <- fluidPage(
|
| 177 |
useShinyjs(),
|
| 178 |
-
tags$head(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
div(class = "qtl-header",
|
| 180 |
div(class = "logo-mark", "QTL"),
|
| 181 |
-
|
| 182 |
-
div(class = "subtitle", "Quantitative Trait Loci Analysis Pipeline")),
|
| 183 |
-
div(class = "header-badges",
|
| 184 |
-
div(class = "badge-pill blue", "R/QTL ENGINE"),
|
| 185 |
-
div(class = "badge-pill green", "10+ MODELS"),
|
| 186 |
-
div(class = "badge-pill orange","MULTI-TRAIT"))
|
| 187 |
),
|
| 188 |
-
|
|
|
|
| 189 |
# Sidebar
|
| 190 |
-
div(
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
),
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
)
|
| 214 |
-
),
|
| 215 |
-
uiOutput("parse_preview_ui"),
|
| 216 |
-
div(class = "section-label", "Cross Type"),
|
| 217 |
-
div(class = "control-block",
|
| 218 |
-
selectInput("cross_type", NULL,
|
| 219 |
-
choices = c("Backcross" = "bc", "F2 Intercross" = "f2",
|
| 220 |
-
"RIL (selfing)" = "riself", "RIL (sib-mating)" = "risib"),
|
| 221 |
-
selected = "f2")
|
| 222 |
-
),
|
| 223 |
-
div(class = "section-label", "Phenotype"),
|
| 224 |
-
div(class = "control-block",
|
| 225 |
-
selectInput("pheno_col", NULL, choices = NULL)
|
| 226 |
-
),
|
| 227 |
-
div(class = "section-label", "Scan Models"),
|
| 228 |
-
checkboxGroupInput("models", NULL,
|
| 229 |
-
choices = c(
|
| 230 |
-
"EM Algorithm (em)" = "em",
|
| 231 |
-
"Haley-Knott (hk)" = "hk",
|
| 232 |
-
"Extended HK (ehk)" = "ehk",
|
| 233 |
-
"Multiple Imputation (imp)" = "imp",
|
| 234 |
-
"Composite Interval (cim)" = "cim",
|
| 235 |
-
"Non-Parametric (np)" = "np",
|
| 236 |
-
"Marker Regression (mr)" = "mr"
|
| 237 |
-
),
|
| 238 |
-
selected = c("hk","em","imp","np","mr")
|
| 239 |
-
),
|
| 240 |
-
div(class = "section-label", "Permutation"),
|
| 241 |
-
div(class = "control-block",
|
| 242 |
-
tags$label("Permutations (0 = skip)"),
|
| 243 |
-
numericInput("n_perm", NULL, value=100, min=0, max=1000, step=50)
|
| 244 |
-
),
|
| 245 |
-
div(class = "control-block",
|
| 246 |
-
tags$label("α threshold"),
|
| 247 |
-
numericInput("alpha_thresh", NULL, value=0.05, min=0.01, max=0.1, step=0.01)
|
| 248 |
-
),
|
| 249 |
-
div(class = "control-block",
|
| 250 |
-
tags$label("Fixed LOD threshold"),
|
| 251 |
-
numericInput("lod_thresh", NULL, value=3.0, min=1, max=10, step=0.5)
|
| 252 |
-
),
|
| 253 |
-
div(class = "section-label", "Genome Scan"),
|
| 254 |
-
div(class = "control-block",
|
| 255 |
-
tags$label("Step (cM)"),
|
| 256 |
-
numericInput("step_size", NULL, value=1, min=0.5, max=5, step=0.5)
|
| 257 |
-
),
|
| 258 |
-
div(class = "control-block",
|
| 259 |
-
tags$label("Error probability"),
|
| 260 |
-
numericInput("error_prob", NULL, value=0.001, min=0.0001, max=0.01, step=0.001)
|
| 261 |
-
),
|
| 262 |
-
actionButton("run_analysis", "▶ RUN ANALYSIS", class = "btn-run"),
|
| 263 |
-
downloadButton("download_results", "⬇ EXPORT CSV", class = "btn-export"),
|
| 264 |
div(class = "status-row",
|
| 265 |
-
div(id="status_dot",
|
| 266 |
-
textOutput("status_text", inline=TRUE)
|
| 267 |
)
|
| 268 |
),
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
)
|
| 274 |
-
)
|
| 275 |
-
tags$script(HTML('/* tab switching script */'))
|
| 276 |
)
|
| 277 |
|
| 278 |
-
#
|
| 279 |
server <- function(input, output, session) {
|
| 280 |
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
plot.background=element_rect(fill="#ffffff", color=NA),
|
| 284 |
-
panel.background=element_rect(fill="#f8f9fb", color=NA),
|
| 285 |
-
panel.grid.major=element_line(color="#e8ecf2", linewidth=0.45),
|
| 286 |
-
panel.grid.minor=element_line(color="#f1f4f8", linewidth=0.25),
|
| 287 |
-
panel.border=element_rect(color="#dde2ea", fill=NA, linewidth=0.8),
|
| 288 |
-
axis.text=element_text(color="#8a95a3", size=9, family="mono"),
|
| 289 |
-
axis.title=element_text(color="#4a5568", size=10),
|
| 290 |
-
plot.title=element_text(color="#1a202c", size=12, face="bold", family="mono"),
|
| 291 |
-
plot.subtitle=element_text(color="#8a95a3", size=9),
|
| 292 |
-
legend.background=element_rect(fill="#ffffff", color="#dde2ea", linewidth=0.5),
|
| 293 |
-
legend.text=element_text(color="#4a5568", size=9),
|
| 294 |
-
legend.title=element_text(color="#8a95a3", size=9),
|
| 295 |
-
strip.background=element_rect(fill="#f1f4f8", color="#dde2ea"),
|
| 296 |
-
strip.text=element_text(color="#1a6cf5", size=9, family="mono", face="bold"),
|
| 297 |
-
plot.margin=margin(12,16,12,16)
|
| 298 |
-
)
|
| 299 |
-
}
|
| 300 |
|
| 301 |
-
#
|
| 302 |
-
|
| 303 |
req(input$geno_file, input$pheno_file)
|
| 304 |
-
|
| 305 |
-
parse_custom_cross(input$geno_file$datapath, input$pheno_file$datapath, input$cross_type),
|
| 306 |
-
error=function(e) {
|
| 307 |
-
showNotification(paste("Error parsing data:", e$message), type="error")
|
| 308 |
-
NULL
|
| 309 |
-
}
|
| 310 |
-
)
|
| 311 |
})
|
| 312 |
|
| 313 |
-
#
|
| 314 |
-
|
| 315 |
-
cross <-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
output$status_text <- renderText(paste0(totmar(cross), " markers · ", nind(cross), " inds · ", nchr(cross), " chrs — Ready"))
|
| 319 |
-
}
|
| 320 |
})
|
| 321 |
|
| 322 |
-
#
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
observeEvent(input$run_analysis, {
|
| 326 |
-
cross <- cross_obj()
|
| 327 |
req(cross)
|
| 328 |
-
output$status_text <- renderText("Running analysis...")
|
| 329 |
-
runjs("document.getElementById('status_dot').classList.remove('active')")
|
| 330 |
-
|
| 331 |
-
withProgress(message="QTL Mapping in progress...", value=0, {
|
| 332 |
-
# calc.genoprob
|
| 333 |
-
cross <- tryCatch(
|
| 334 |
-
calc.genoprob(cross, step=input$step_size, error.prob=input$error_prob),
|
| 335 |
-
error=function(e) {showNotification(paste("calc.genoprob error:", e$message), type="error"); NULL}
|
| 336 |
-
)
|
| 337 |
-
req(cross); incProgress(0.2)
|
| 338 |
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
sim.geno(cross, n.draws=64, step=input$step_size, error.prob=input$error_prob),
|
| 342 |
-
error=function(e) cross
|
| 343 |
-
)
|
| 344 |
-
incProgress(0.2)
|
| 345 |
|
| 346 |
-
|
| 347 |
pheno_idx <- which(names(cross$pheno) == input$pheno_col)
|
| 348 |
-
if (length(pheno_idx)==0) pheno_idx <- 1
|
| 349 |
|
| 350 |
-
|
| 351 |
-
if (length(
|
| 352 |
|
| 353 |
scan_results <- list()
|
| 354 |
perm_results <- list()
|
| 355 |
|
| 356 |
-
for (mdl in
|
| 357 |
-
incProgress(0.
|
| 358 |
-
sc <- tryCatch(
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
)
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
scanone(cross, pheno.col=pheno_idx, method=mdl, n.perm=input$n_perm, verbose=FALSE),
|
| 367 |
-
error=function(e) NULL
|
| 368 |
-
)
|
| 369 |
-
}
|
| 370 |
}
|
| 371 |
}
|
| 372 |
-
incProgress(0.2)
|
| 373 |
|
| 374 |
-
|
| 375 |
-
lod_thresh <- input$lod_thresh
|
| 376 |
if (length(perm_results) > 0 && !is.null(perm_results[[1]])) {
|
| 377 |
-
thr <- tryCatch(summary(perm_results[[1]], alpha=
|
| 378 |
-
if (!is.null(thr)
|
| 379 |
}
|
| 380 |
|
| 381 |
-
|
| 382 |
-
qtl_table <- build_qtl_table(cross, scan_results, lod_thresh, pheno_idx)
|
| 383 |
-
|
| 384 |
-
# Save
|
| 385 |
-
analysis_res(list(
|
| 386 |
-
cross=cross,
|
| 387 |
-
scan_results=scan_results,
|
| 388 |
-
perm_results=perm_results,
|
| 389 |
-
lod_thr=lod_thresh,
|
| 390 |
-
qtl_table=qtl_table,
|
| 391 |
-
pheno_idx=pheno_idx
|
| 392 |
-
))
|
| 393 |
|
| 394 |
-
|
|
|
|
| 395 |
})
|
| 396 |
-
|
| 397 |
-
# Update dropdowns
|
| 398 |
-
res <- analysis_res()
|
| 399 |
-
if (!is.null(res)){
|
| 400 |
-
updateSelectInput(session, "manhattan_model", choices=names(res$scan_results), selected=names(res$scan_results)[1])
|
| 401 |
-
updateSelectInput(session, "chr_plot_model", choices=names(res$scan_results), selected=names(res$scan_results)[1])
|
| 402 |
-
qtl_choices <- if(nrow(res$qtl_table)>0) paste0(res$qtl_table$Chr, "@", res$qtl_table$Peak_Marker) else "none"
|
| 403 |
-
updateSelectInput(session, "effect_qtl", choices=qtl_choices)
|
| 404 |
-
}
|
| 405 |
-
|
| 406 |
-
runjs("document.getElementById('status_dot').classList.add('active')")
|
| 407 |
-
output$status_text <- renderText(paste0("Analysis complete"))
|
| 408 |
})
|
| 409 |
|
| 410 |
-
|
| 411 |
-
build_qtl_table <- function(cross, scan_results, lod_thresh, pheno_idx) {
|
| 412 |
rows <- list()
|
| 413 |
for (mdl in names(scan_results)) {
|
| 414 |
sc <- scan_results[[mdl]]
|
| 415 |
-
|
| 416 |
-
if (nrow(
|
| 417 |
-
for (ch in unique(
|
| 418 |
-
chr_sc <- sc[sc$chr==ch,]
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
Model=mdl, Chr=ch, Peak_Marker=peak_marker, Peak_pos_cM=round(peak_pos,2),
|
| 433 |
-
LOD=round(peak_lod,3), PVE_pct=pve,
|
| 434 |
-
Left_Marker=left_marker, Left_pos_cM=round(left_pos,2),
|
| 435 |
-
Right_Marker=right_marker, Right_pos_cM=round(right_pos,2),
|
| 436 |
-
CI_width_cM=round(right_pos-left_pos,2),
|
| 437 |
-
stringsAsFactors=FALSE
|
| 438 |
)
|
| 439 |
}
|
| 440 |
}
|
| 441 |
-
if (length(rows)==0) return(data.frame())
|
| 442 |
-
bind_rows(rows) %>% arrange(Chr,
|
| 443 |
}
|
| 444 |
|
| 445 |
-
#
|
| 446 |
-
|
| 447 |
-
cross <-
|
| 448 |
req(cross)
|
| 449 |
-
|
| 450 |
-
df <- do.call(rbind, lapply(names(map), function(ch) {
|
| 451 |
-
data.frame(chr=ch, pos=unname(map[[ch]]))
|
| 452 |
-
}))
|
| 453 |
-
df$chr <- factor(df$chr, levels=names(map))
|
| 454 |
-
ggplot(df, aes(x=chr,y=pos)) +
|
| 455 |
-
geom_segment(aes(xend=chr,yend=pos), color="#dde2ea", linewidth=8) +
|
| 456 |
-
geom_point(color="#1a6cf5", size=1.5, alpha=0.8) +
|
| 457 |
-
scale_y_reverse() +
|
| 458 |
-
labs(title="Genetic Map", x="Chromosome", y="Position (cM)") + theme_qtl()
|
| 459 |
})
|
| 460 |
|
| 461 |
-
output$
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
df <- data.frame(value=ph[!is.na(ph)])
|
| 466 |
-
p1 <- ggplot(df, aes(value)) +
|
| 467 |
-
geom_histogram(fill="#1a6cf5", color="#ffffff", bins=30, alpha=0.8) +
|
| 468 |
-
labs(title=input$pheno_col, x="Value", y="Count") + theme_qtl()
|
| 469 |
-
p2 <- ggplot(df, aes(sample=value)) +
|
| 470 |
-
stat_qq(color="#18a058", size=1.5, alpha=0.8) +
|
| 471 |
-
stat_qq_line(color="#c2185b", linewidth=0.8) +
|
| 472 |
-
labs(title="Q-Q Plot", x="Theoretical", y="Sample") + theme_qtl()
|
| 473 |
-
gridExtra::grid.arrange(p1, p2, ncol=2)
|
| 474 |
})
|
| 475 |
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 479 |
map <- pull.map(cross)
|
| 480 |
-
df <- do.call(rbind, lapply(names(map), function(ch)
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 493 |
})
|
| 494 |
|
| 495 |
output$plot_manhattan <- renderPlot({
|
| 496 |
-
res <-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 515 |
})
|
| 516 |
|
| 517 |
-
output$
|
| 518 |
-
res <-
|
| 519 |
-
|
| 520 |
-
mdl <- input$manhattan_model
|
| 521 |
-
sc <- res$scan_results[[mdl]]
|
| 522 |
-
req(sc)
|
| 523 |
chrs <- unique(sc$chr)
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 539 |
})
|
| 540 |
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
datatable(qt, rownames=FALSE,
|
| 551 |
-
options=list(pageLength=20, scrollX=TRUE, dom="Blfrtip"),
|
| 552 |
-
class="display nowrap") %>%
|
| 553 |
-
formatStyle("LOD", background=styleColorBar(range(qt$LOD), "#bcd0fc")) %>%
|
| 554 |
-
formatStyle("PVE_pct", background=styleColorBar(c(0,100), "#b3dfc7")) %>%
|
| 555 |
-
formatRound(c("Peak_pos_cM","LOD","PVE_pct","Left_pos_cM","Right_pos_cM","CI_width_cM"), 3)
|
| 556 |
})
|
| 557 |
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
)
|
| 569 |
-
datatable(df, rownames=FALSE, options=list(dom="t", pageLength=15), class="display") %>%
|
| 570 |
-
formatStyle("Max_LOD", color="#1a6cf5", fontWeight="bold") %>%
|
| 571 |
-
formatStyle("QTLs_Found", color="#c2185b", fontWeight="bold")
|
| 572 |
})
|
| 573 |
|
| 574 |
-
output$
|
| 575 |
-
res <-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 589 |
}
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 593 |
})
|
| 594 |
|
| 595 |
# Download
|
| 596 |
output$download_results <- downloadHandler(
|
| 597 |
-
filename=function() paste0("
|
| 598 |
-
content=function(file) {
|
| 599 |
-
res <-
|
| 600 |
-
if (!is.null(res) && nrow(res$qtl_table)>0) {
|
| 601 |
-
write.csv(res$qtl_table, file, row.names=FALSE)
|
| 602 |
} else {
|
| 603 |
-
write.csv(data.frame(
|
| 604 |
}
|
| 605 |
}
|
| 606 |
)
|
| 607 |
-
|
| 608 |
}
|
| 609 |
|
| 610 |
-
|
| 611 |
-
shinyApp(ui=ui, server=server)
|
|
|
|
| 8 |
library(shinyjs)
|
| 9 |
library(plotly)
|
| 10 |
library(gridExtra)
|
| 11 |
+
library(scales)
|
| 12 |
|
| 13 |
+
# ========================== CUSTOM PARSER ==========================
|
| 14 |
parse_custom_cross <- function(geno_path, pheno_path, cross_type = "f2") {
|
| 15 |
+
tryCatch({
|
| 16 |
+
geno_raw <- read.csv(geno_path, header = FALSE, stringsAsFactors = FALSE,
|
| 17 |
+
check.names = FALSE, na.strings = c("NA", "", "N/A"))
|
| 18 |
+
|
| 19 |
+
col_names <- as.character(geno_raw[1, ])
|
| 20 |
+
chr_row <- as.character(geno_raw[2, ])
|
| 21 |
+
pos_row <- as.character(geno_raw[3, ])
|
| 22 |
+
ind_data <- geno_raw[4:nrow(geno_raw), , drop = FALSE]
|
| 23 |
+
|
| 24 |
+
marker_cols <- 2:ncol(geno_raw)
|
| 25 |
+
marker_names <- col_names[marker_cols]
|
| 26 |
+
chromosomes <- gsub("^[Cc]hr0*", "", chr_row[marker_cols])
|
| 27 |
+
positions <- suppressWarnings(as.numeric(pos_row[marker_cols]))
|
| 28 |
+
|
| 29 |
+
chr_num <- suppressWarnings(as.integer(chromosomes))
|
| 30 |
+
chromosomes <- ifelse(!is.na(chr_num), as.character(chr_num), chromosomes)
|
| 31 |
+
|
| 32 |
+
ind_ids <- as.character(ind_data[, 1])
|
| 33 |
+
geno_mat <- as.matrix(ind_data[, marker_cols, drop = FALSE])
|
| 34 |
+
geno_mat <- apply(geno_mat, 2, function(x) suppressWarnings(as.numeric(x)))
|
| 35 |
+
rownames(geno_mat) <- ind_ids
|
| 36 |
+
colnames(geno_mat) <- marker_names
|
| 37 |
+
geno_mat[geno_mat == 0] <- NA
|
| 38 |
+
storage.mode(geno_mat) <- "integer"
|
| 39 |
+
|
| 40 |
+
# Phenotype
|
| 41 |
+
pheno_raw <- read.csv(pheno_path, header = TRUE, stringsAsFactors = FALSE,
|
| 42 |
+
check.names = FALSE, na.strings = c("NA", "", "N/A"))
|
| 43 |
+
pheno_ids <- as.character(pheno_raw[, 1])
|
| 44 |
+
pheno_df <- as.data.frame(lapply(pheno_raw[,-1, drop=FALSE],
|
| 45 |
+
function(x) suppressWarnings(as.numeric(x))))
|
| 46 |
+
rownames(pheno_df) <- pheno_ids
|
| 47 |
+
|
| 48 |
+
common_ids <- intersect(ind_ids, pheno_ids)
|
| 49 |
+
if (length(common_ids) == 0) stop("No matching IDs between genotype and phenotype files.")
|
| 50 |
+
|
| 51 |
+
geno_mat <- geno_mat[common_ids, , drop = FALSE]
|
| 52 |
+
pheno_df <- pheno_df[common_ids, , drop = FALSE]
|
| 53 |
+
|
| 54 |
+
# Map
|
| 55 |
+
unique_chrs <- unique(chromosomes)
|
| 56 |
+
chr_num <- suppressWarnings(as.integer(unique_chrs))
|
| 57 |
+
ord <- order(ifelse(!is.na(chr_num), chr_num, 999L), unique_chrs)
|
| 58 |
+
unique_chrs <- unique_chrs[ord]
|
| 59 |
+
|
| 60 |
+
map_list <- lapply(unique_chrs, function(ch) {
|
| 61 |
+
idx <- which(chromosomes == ch)
|
| 62 |
+
pos <- positions[idx]
|
| 63 |
+
mnames <- marker_names[idx]
|
| 64 |
+
ord2 <- order(pos)
|
| 65 |
+
m <- pos[ord2]; names(m) <- mnames[ord2]; m
|
| 66 |
+
})
|
| 67 |
+
names(map_list) <- unique_chrs
|
| 68 |
+
|
| 69 |
+
# Build cross
|
| 70 |
+
geno_list <- lapply(unique_chrs, function(ch) {
|
| 71 |
+
idx <- which(chromosomes == ch)
|
| 72 |
+
mnames <- marker_names[idx]
|
| 73 |
+
pos_ch <- positions[idx]
|
| 74 |
+
ord2 <- order(pos_ch)
|
| 75 |
+
mnames <- mnames[ord2]
|
| 76 |
+
g_chr <- geno_mat[, mnames, drop = FALSE]
|
| 77 |
+
list(data = g_chr, map = map_list[[ch]])
|
| 78 |
+
})
|
| 79 |
+
names(geno_list) <- unique_chrs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
+
cross_obj <- list(geno = geno_list, pheno = pheno_df)
|
| 82 |
+
class(cross_obj) <- c(cross_type, "cross")
|
| 83 |
+
cross_obj <- jittermap(cross_obj)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
+
if (cross_type == "f2") {
|
| 86 |
+
cross_obj <- tryCatch(convert2bcsft(cross_obj, BC.gen = 0, F.gen = 2, estimate.map = FALSE),
|
| 87 |
+
error = function(e) cross_obj)
|
| 88 |
+
}
|
| 89 |
+
cross_obj
|
| 90 |
+
}, error = function(e) stop(e$message))
|
| 91 |
}
|
| 92 |
|
| 93 |
+
# ========================== UI ==========================
|
|
|
|
| 94 |
ui <- fluidPage(
|
| 95 |
useShinyjs(),
|
| 96 |
+
tags$head(
|
| 97 |
+
tags$link(href = "https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600&family=IBM+Plex+Sans:wght@300;400;500;600&display=swap", rel = "stylesheet"),
|
| 98 |
+
tags$style(HTML("
|
| 99 |
+
body { font-family: 'IBM Plex Sans', sans-serif; background: #f8f9fb; }
|
| 100 |
+
.qtl-header { background: white; padding: 12px 24px; border-bottom: 2px solid #dde2ea; display: flex; align-items: center; gap: 15px; position: sticky; top: 0; z-index: 100; }
|
| 101 |
+
.logo-mark { width: 40px; height: 40px; background: #1a6cf5; color: white; border-radius: 8px; display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 16px; }
|
| 102 |
+
.btn-run { width: 100%; padding: 12px; background: #1a6cf5; color: white; border: none; border-radius: 8px; font-weight: 600; }
|
| 103 |
+
.btn-run:disabled { background: #94a3b8; }
|
| 104 |
+
.plot-card { background: white; border-radius: 12px; box-shadow: 0 4px 15px rgba(0,0,0,0.08); margin-bottom: 20px; overflow: hidden; }
|
| 105 |
+
.plot-card-header { padding: 14px 18px; background: #f1f4f8; border-bottom: 1px solid #dde2ea; font-weight: 600; }
|
| 106 |
+
.empty-state { padding: 80px 20px; text-align: center; color: #8a95a3; font-size: 18px; }
|
| 107 |
+
.status-row { padding: 10px; background: white; border: 1px solid #dde2ea; border-radius: 8px; margin-top: 10px; }
|
| 108 |
+
"))
|
| 109 |
+
),
|
| 110 |
+
|
| 111 |
div(class = "qtl-header",
|
| 112 |
div(class = "logo-mark", "QTL"),
|
| 113 |
+
h1("QTL Mapper Pro", style = "margin:0; font-size:20px;")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
),
|
| 115 |
+
|
| 116 |
+
div(style = "display: grid; grid-template-columns: 310px 1fr; height: calc(100vh - 65px);",
|
| 117 |
# Sidebar
|
| 118 |
+
div(style = "background: #f1f4f8; padding: 20px; overflow-y: auto; border-right: 2px solid #dde2ea;",
|
| 119 |
+
h4("Data Input"),
|
| 120 |
+
fileInput("geno_file", "Genotype CSV", accept = ".csv", width = "100%"),
|
| 121 |
+
fileInput("pheno_file", "Phenotype CSV", accept = ".csv", width = "100%"),
|
| 122 |
+
|
| 123 |
+
h4("Settings", style = "margin-top:25px;"),
|
| 124 |
+
selectInput("cross_type", "Cross Type",
|
| 125 |
+
choices = c("F2 Intercross"="f2", "Backcross"="bc", "RIL Self"="riself", "RIL Sib"="risib"),
|
| 126 |
+
selected = "f2"),
|
| 127 |
+
selectInput("pheno_col", "Select Trait", choices = NULL),
|
| 128 |
+
|
| 129 |
+
checkboxGroupInput("models", "Mapping Methods",
|
| 130 |
+
choices = c("hk","em","ehk","imp","cim","np","mr"),
|
| 131 |
+
selected = c("hk","em","imp"),
|
| 132 |
+
inline = FALSE),
|
| 133 |
+
|
| 134 |
+
numericInput("step_size", "Step (cM)", 2, min = 1, max = 10, step = 0.5),
|
| 135 |
+
numericInput("n_perm", "Permutations (0=skip)", 100, min = 0, max = 1000, step = 50),
|
| 136 |
+
numericInput("lod_thresh", "LOD Threshold", 3.0, min = 1, max = 15, step = 0.5),
|
| 137 |
+
|
| 138 |
+
actionButton("run_analysis", "▶ RUN ANALYSIS", class = "btn-run"),
|
| 139 |
+
downloadButton("download_results", "Download Results", style = "width:100%; margin-top:10px;"),
|
| 140 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
div(class = "status-row",
|
| 142 |
+
div(id = "status_dot", style = "display:inline-block;width:10px;height:10px;border-radius:50%;background:#94a3b8;"),
|
| 143 |
+
textOutput("status_text", inline = TRUE)
|
| 144 |
)
|
| 145 |
),
|
| 146 |
+
|
| 147 |
+
# Main Content
|
| 148 |
+
div(style = "padding: 20px; overflow-y: auto;",
|
| 149 |
+
tabsetPanel(
|
| 150 |
+
tabPanel("Overview", uiOutput("overview_content")),
|
| 151 |
+
tabPanel("Manhattan", uiOutput("manhattan_content")),
|
| 152 |
+
tabPanel("Genome Scan", uiOutput("genome_content")),
|
| 153 |
+
tabPanel("Chr Plots", uiOutput("chromosome_content")),
|
| 154 |
+
tabPanel("Model Compare", uiOutput("model_comp_content")),
|
| 155 |
+
tabPanel("QTL Effects", uiOutput("effects_content")),
|
| 156 |
+
tabPanel("2D Scan", uiOutput("pairwise_content")),
|
| 157 |
+
tabPanel("Diagnostics", uiOutput("diagnostics_content")),
|
| 158 |
+
tabPanel("Results", uiOutput("results_content"))
|
| 159 |
+
)
|
| 160 |
)
|
| 161 |
+
)
|
|
|
|
| 162 |
)
|
| 163 |
|
| 164 |
+
# ========================== SERVER ==========================
|
| 165 |
server <- function(input, output, session) {
|
| 166 |
|
| 167 |
+
model_colors <- c(hk="#e8500a", em="#1a6cf5", ehk="#18a058", imp="#c2185b",
|
| 168 |
+
cim="#7c3aed", np="#0891b2", mr="#dc2626")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
+
# Reactive Cross
|
| 171 |
+
cross_data <- reactive({
|
| 172 |
req(input$geno_file, input$pheno_file)
|
| 173 |
+
parse_custom_cross(input$geno_file$datapath, input$pheno_file$datapath, input$cross_type)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
})
|
| 175 |
|
| 176 |
+
# Pre-compute genotype probabilities (expensive, done once)
|
| 177 |
+
cross_with_probs <- reactive({
|
| 178 |
+
cross <- cross_data()
|
| 179 |
+
req(cross)
|
| 180 |
+
calc.genoprob(cross, step = input$step_size, error.prob = 0.001)
|
|
|
|
|
|
|
| 181 |
})
|
| 182 |
|
| 183 |
+
# Main Analysis
|
| 184 |
+
analysis_results <- eventReactive(input$run_analysis, {
|
| 185 |
+
cross <- cross_with_probs()
|
|
|
|
|
|
|
| 186 |
req(cross)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
+
shinyjs::disable("run_analysis")
|
| 189 |
+
on.exit(shinyjs::enable("run_analysis"), add = TRUE)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
+
withProgress(message = "QTL Analysis in progress...", value = 0, {
|
| 192 |
pheno_idx <- which(names(cross$pheno) == input$pheno_col)
|
| 193 |
+
if (length(pheno_idx) == 0) pheno_idx <- 1
|
| 194 |
|
| 195 |
+
models_to_run <- input$models
|
| 196 |
+
if (length(models_to_run) == 0) models_to_run <- "hk"
|
| 197 |
|
| 198 |
scan_results <- list()
|
| 199 |
perm_results <- list()
|
| 200 |
|
| 201 |
+
for (mdl in models_to_run) {
|
| 202 |
+
incProgress(0.6/length(models_to_run), detail = paste("Scanning with", mdl))
|
| 203 |
+
sc <- tryCatch(scanone(cross, pheno.col = pheno_idx, method = mdl), error = function(e) NULL)
|
| 204 |
+
if (!is.null(sc)) scan_results[[mdl]] <- sc
|
| 205 |
+
|
| 206 |
+
if (input$n_perm > 0) {
|
| 207 |
+
incProgress(0.3/length(models_to_run), detail = paste("Permuting", mdl))
|
| 208 |
+
perm <- tryCatch(scanone(cross, pheno.col = pheno_idx, method = mdl,
|
| 209 |
+
n.perm = input$n_perm, verbose = FALSE), error = function(e) NULL)
|
| 210 |
+
if (!is.null(perm)) perm_results[[mdl]] <- perm
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
}
|
| 212 |
}
|
|
|
|
| 213 |
|
| 214 |
+
lod_thr <- input$lod_thresh
|
|
|
|
| 215 |
if (length(perm_results) > 0 && !is.null(perm_results[[1]])) {
|
| 216 |
+
thr <- tryCatch(summary(perm_results[[1]], alpha = 0.05)[1], error = function(e) NULL)
|
| 217 |
+
if (!is.null(thr)) lod_thr <- thr
|
| 218 |
}
|
| 219 |
|
| 220 |
+
qtl_table <- build_qtl_table(cross, scan_results, lod_thr, pheno_idx)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
|
| 222 |
+
list(cross = cross, scan_results = scan_results, perm_results = perm_results,
|
| 223 |
+
lod_thr = lod_thr, qtl_table = qtl_table, pheno_idx = pheno_idx)
|
| 224 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
})
|
| 226 |
|
| 227 |
+
build_qtl_table <- function(cross, scan_results, lod_thr, pheno_idx) {
|
|
|
|
| 228 |
rows <- list()
|
| 229 |
for (mdl in names(scan_results)) {
|
| 230 |
sc <- scan_results[[mdl]]
|
| 231 |
+
sig <- sc[sc$lod >= lod_thr, , drop = FALSE]
|
| 232 |
+
if (nrow(sig) == 0) next
|
| 233 |
+
for (ch in unique(sig$chr)) {
|
| 234 |
+
chr_sc <- sc[sc$chr == ch, ]
|
| 235 |
+
peak <- chr_sc[which.max(chr_sc$lod), ]
|
| 236 |
+
ci <- tryCatch(lodint(sc, chr = ch, drop = 1.5), error = function(e) NULL)
|
| 237 |
+
|
| 238 |
+
rows[[length(rows) + 1]] <- data.frame(
|
| 239 |
+
Model = mdl,
|
| 240 |
+
Chr = ch,
|
| 241 |
+
Peak_Marker = rownames(peak),
|
| 242 |
+
Peak_cM = round(peak$pos, 2),
|
| 243 |
+
LOD = round(peak$lod, 3),
|
| 244 |
+
PVE = round((1 - 10^(-2 * peak$lod / nind(cross))) * 100, 1),
|
| 245 |
+
CI_Left_cM = ifelse(!is.null(ci), round(ci$pos[1], 2), NA),
|
| 246 |
+
CI_Right_cM = ifelse(!is.null(ci), round(ci$pos[nrow(ci)], 2), NA),
|
| 247 |
+
stringsAsFactors = FALSE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
)
|
| 249 |
}
|
| 250 |
}
|
| 251 |
+
if (length(rows) == 0) return(data.frame())
|
| 252 |
+
bind_rows(rows) %>% arrange(Chr, Peak_cM)
|
| 253 |
}
|
| 254 |
|
| 255 |
+
# Update phenotype dropdown
|
| 256 |
+
observe({
|
| 257 |
+
cross <- cross_data()
|
| 258 |
req(cross)
|
| 259 |
+
updateSelectInput(session, "pheno_col", choices = names(cross$pheno))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
})
|
| 261 |
|
| 262 |
+
output$status_text <- renderText({
|
| 263 |
+
if (is.null(cross_data())) "Upload files"
|
| 264 |
+
else if (is.null(analysis_results())) "Ready - Click RUN ANALYSIS"
|
| 265 |
+
else "Analysis Completed"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 266 |
})
|
| 267 |
|
| 268 |
+
# ====================== ALL TABS & PLOTS ======================
|
| 269 |
+
|
| 270 |
+
output$overview_content <- renderUI({
|
| 271 |
+
cross <- cross_data()
|
| 272 |
+
if (is.null(cross)) return(div(class = "empty-state", "📂 Please upload genotype and phenotype files"))
|
| 273 |
+
res <- analysis_results()
|
| 274 |
+
nqtl <- if(!is.null(res)) nrow(res$qtl_table) else 0
|
| 275 |
+
|
| 276 |
+
tagList(
|
| 277 |
+
fluidRow(
|
| 278 |
+
column(3, div(style="background:#eef3fe;padding:20px;border-radius:10px;text-align:center;", h3(totmar(cross)), p("Markers"))),
|
| 279 |
+
column(3, div(style="background:#fef4ee;padding:20px;border-radius:10px;text-align:center;", h3(nind(cross)), p("Individuals"))),
|
| 280 |
+
column(3, div(style="background:#edf7f2;padding:20px;border-radius:10px;text-align:center;", h3(nchr(cross)), p("Chromosomes"))),
|
| 281 |
+
column(3, div(style="background:#fce8f1;padding:20px;border-radius:10px;text-align:center;", h3(nqtl), p("Significant QTLs")))
|
| 282 |
+
),
|
| 283 |
+
div(class = "plot-card", div(class = "plot-card-header", "Genome Map"), plotOutput("plot_genome_map", height = "380px") %>% withSpinner())
|
| 284 |
+
)
|
| 285 |
+
})
|
| 286 |
+
|
| 287 |
+
output$plot_genome_map <- renderPlot({
|
| 288 |
+
cross <- cross_data(); req(cross)
|
| 289 |
map <- pull.map(cross)
|
| 290 |
+
df <- do.call(rbind, lapply(names(map), function(ch) data.frame(chr=ch, pos=unname(map[[ch]]))))
|
| 291 |
+
ggplot(df, aes(x=chr, y=pos)) +
|
| 292 |
+
geom_segment(aes(xend=chr, yend=0), color="#e0e7ff", linewidth=12) +
|
| 293 |
+
geom_point(color="#1a6cf5", size=2.5) +
|
| 294 |
+
scale_y_reverse() + labs(y="Position (cM)", title="Genetic Map") +
|
| 295 |
+
theme_minimal(base_size=13)
|
| 296 |
+
})
|
| 297 |
+
|
| 298 |
+
# Manhattan
|
| 299 |
+
output$manhattan_content <- renderUI({
|
| 300 |
+
res <- analysis_results()
|
| 301 |
+
if (is.null(res)) return(div(class="empty-state", "Run analysis first"))
|
| 302 |
+
tagList(
|
| 303 |
+
selectInput("manhattan_model", "Model", choices=names(res$scan_results)),
|
| 304 |
+
div(class="plot-card", div(class="plot-card-header", "Manhattan Plot"),
|
| 305 |
+
plotOutput("plot_manhattan", height="520px") %>% withSpinner())
|
| 306 |
+
)
|
| 307 |
})
|
| 308 |
|
| 309 |
output$plot_manhattan <- renderPlot({
|
| 310 |
+
res <- analysis_results(); req(res, input$manhattan_model)
|
| 311 |
+
sc <- res$scan_results[[input$manhattan_model]]
|
| 312 |
+
ggplot(sc, aes(x=pos, y=lod, color=factor(chr))) +
|
| 313 |
+
geom_point(size=1.3, alpha=0.85) +
|
| 314 |
+
geom_hline(yintercept=res$lod_thr, color="red", linetype="dashed", linewidth=0.8) +
|
| 315 |
+
facet_wrap(~chr, scales="free_x", ncol=4) +
|
| 316 |
+
labs(y="LOD Score", title=paste("Manhattan -", input$manhattan_model)) +
|
| 317 |
+
theme_minimal() + theme(legend.position="none")
|
| 318 |
+
})
|
| 319 |
+
|
| 320 |
+
# Genome Scan (All Models)
|
| 321 |
+
output$genome_content <- renderUI({
|
| 322 |
+
res <- analysis_results()
|
| 323 |
+
if (is.null(res)) return(div(class="empty-state", "Run analysis first"))
|
| 324 |
+
div(class="plot-card", div(class="plot-card-header", "Genome Scan - All Models"),
|
| 325 |
+
plotOutput("plot_scan_all", height="520px") %>% withSpinner())
|
| 326 |
+
})
|
| 327 |
+
|
| 328 |
+
output$plot_scan_all <- renderPlot({
|
| 329 |
+
res <- analysis_results(); req(res)
|
| 330 |
+
df <- bind_rows(lapply(names(res$scan_results), function(m) {
|
| 331 |
+
sc <- res$scan_results[[m]]
|
| 332 |
+
sc$model <- m
|
| 333 |
+
sc
|
| 334 |
+
}))
|
| 335 |
+
ggplot(df, aes(x=pos, y=lod, color=model)) +
|
| 336 |
+
geom_line(linewidth=0.7, alpha=0.9) +
|
| 337 |
+
geom_hline(yintercept=res$lod_thr, color="red", linetype="dashed") +
|
| 338 |
+
facet_wrap(~chr, scales="free_x", ncol=4) +
|
| 339 |
+
scale_color_manual(values=model_colors) +
|
| 340 |
+
labs(y="LOD", title="Genome Scan - All Models") + theme_minimal()
|
| 341 |
+
})
|
| 342 |
+
|
| 343 |
+
# Chromosome Grid
|
| 344 |
+
output$chromosome_content <- renderUI({
|
| 345 |
+
res <- analysis_results()
|
| 346 |
+
if (is.null(res)) return(div(class="empty-state", "Run analysis first"))
|
| 347 |
+
tagList(
|
| 348 |
+
selectInput("chr_model", "Model", choices=names(res$scan_results)),
|
| 349 |
+
div(class="plot-card", div(class="plot-card-header", "Per Chromosome LOD Profiles"),
|
| 350 |
+
plotOutput("plot_chr_grid", height="800px") %>% withSpinner())
|
| 351 |
+
)
|
| 352 |
})
|
| 353 |
|
| 354 |
+
output$plot_chr_grid <- renderPlot({
|
| 355 |
+
res <- analysis_results(); req(res, input$chr_model)
|
| 356 |
+
sc <- res$scan_results[[input$chr_model]]
|
|
|
|
|
|
|
|
|
|
| 357 |
chrs <- unique(sc$chr)
|
| 358 |
+
plots <- lapply(chrs, function(ch) {
|
| 359 |
+
p <- ggplot(sc[sc$chr==ch,], aes(x=pos, y=lod)) +
|
| 360 |
+
geom_line(color="#1a6cf5", linewidth=0.9) +
|
| 361 |
+
geom_hline(yintercept=res$lod_thr, color="red", linetype="dashed") +
|
| 362 |
+
labs(title=paste("Chr", ch), x="cM", y="LOD") + theme_minimal(base_size=10)
|
| 363 |
+
if (nrow(res$qtl_table) > 0) {
|
| 364 |
+
qt_chr <- res$qtl_table[res$qtl_table$Chr == ch & res$qtl_table$Model == input$chr_model, ]
|
| 365 |
+
if (nrow(qt_chr) > 0) p <- p + geom_point(data=qt_chr, aes(x=Peak_cM, y=LOD), color="red", size=4, shape=23, fill="yellow")
|
| 366 |
+
}
|
| 367 |
+
p
|
| 368 |
+
})
|
| 369 |
+
do.call(grid.arrange, c(plots, ncol = min(3, length(chrs))))
|
| 370 |
+
})
|
| 371 |
+
|
| 372 |
+
# Model Comparison
|
| 373 |
+
output$model_comp_content <- renderUI({
|
| 374 |
+
res <- analysis_results()
|
| 375 |
+
if (is.null(res)) return(div(class="empty-state", "Run analysis first"))
|
| 376 |
+
div(class="plot-card", div(class="plot-card-header", "Model Comparison"),
|
| 377 |
+
plotOutput("plot_model_bar", height="400px") %>% withSpinner())
|
| 378 |
})
|
| 379 |
|
| 380 |
+
output$plot_model_bar <- renderPlot({
|
| 381 |
+
res <- analysis_results(); req(res)
|
| 382 |
+
df <- data.frame(Model = names(res$scan_results),
|
| 383 |
+
MaxLOD = sapply(res$scan_results, function(x) max(x$lod, na.rm=TRUE)))
|
| 384 |
+
ggplot(df, aes(x=reorder(Model, MaxLOD), y=MaxLOD, fill=Model)) +
|
| 385 |
+
geom_col() + coord_flip() +
|
| 386 |
+
scale_fill_manual(values=model_colors) +
|
| 387 |
+
geom_hline(yintercept=res$lod_thr, color="red", linetype="dashed") +
|
| 388 |
+
labs(title="Maximum LOD per Model", y="Max LOD") + theme_minimal()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 389 |
})
|
| 390 |
|
| 391 |
+
# QTL Effects
|
| 392 |
+
output$effects_content <- renderUI({
|
| 393 |
+
res <- analysis_results()
|
| 394 |
+
if (is.null(res) || nrow(res$qtl_table)==0)
|
| 395 |
+
return(div(class="empty-state", "No significant QTLs found"))
|
| 396 |
+
qtl_choices <- paste0(res$qtl_table$Chr, "@", res$qtl_table$Peak_Marker)
|
| 397 |
+
tagList(
|
| 398 |
+
selectInput("effect_qtl", "Select QTL", choices=qtl_choices),
|
| 399 |
+
div(class="plot-card", div(class="plot-card-header", "QTL Effect Plot"),
|
| 400 |
+
plotOutput("plot_effects", height="380px") %>% withSpinner())
|
| 401 |
)
|
|
|
|
|
|
|
|
|
|
| 402 |
})
|
| 403 |
|
| 404 |
+
output$plot_effects <- renderPlot({
|
| 405 |
+
res <- analysis_results(); req(res, input$effect_qtl)
|
| 406 |
+
parts <- strsplit(input$effect_qtl, "@")[[1]]
|
| 407 |
+
marker <- parts[2]
|
| 408 |
+
tryCatch({
|
| 409 |
+
eff <- effectplot(res$cross, mname1=marker, pheno.col=res$pheno_idx, draw=FALSE)
|
| 410 |
+
df <- data.frame(Genotype=names(eff$Means), Mean=eff$Means, SE=eff$SEs)
|
| 411 |
+
ggplot(df, aes(x=Genotype, y=Mean, fill=Genotype)) +
|
| 412 |
+
geom_col(alpha=0.85) +
|
| 413 |
+
geom_errorbar(aes(ymin=Mean-SE, ymax=Mean+SE), width=0.3) +
|
| 414 |
+
labs(title=paste("Effect at", marker)) + theme_minimal()
|
| 415 |
+
}, error = function(e) ggplot() + annotate("text", x=0.5, y=0.5, label="Effect plot unavailable") + theme_void())
|
| 416 |
+
})
|
| 417 |
+
|
| 418 |
+
# 2D Scan (Epistasis)
|
| 419 |
+
output$pairwise_content <- renderUI({
|
| 420 |
+
res <- analysis_results()
|
| 421 |
+
if (is.null(res)) return(div(class="empty-state", "Run analysis first"))
|
| 422 |
+
div(class="plot-card", div(class="plot-card-header", "2D Genome Scan (Epistasis)"),
|
| 423 |
+
plotOutput("plot_2d", height="600px") %>% withSpinner())
|
| 424 |
+
})
|
| 425 |
+
|
| 426 |
+
output$plot_2d <- renderPlot({
|
| 427 |
+
res <- analysis_results(); req(res)
|
| 428 |
+
sc2 <- tryCatch(scantwo(res$cross, pheno.col=res$pheno_idx, method="hk", verbose=FALSE),
|
| 429 |
+
error = function(e) NULL)
|
| 430 |
+
if (is.null(sc2)) {
|
| 431 |
+
ggplot() + annotate("text", x=0.5, y=0.5, label="2D scan not available") + theme_void()
|
| 432 |
+
} else {
|
| 433 |
+
plot(sc2, main="Pairwise LOD Scan")
|
| 434 |
}
|
| 435 |
+
})
|
| 436 |
+
|
| 437 |
+
# Diagnostics
|
| 438 |
+
output$diagnostics_content <- renderUI({
|
| 439 |
+
cross <- cross_data()
|
| 440 |
+
if (is.null(cross)) return(div(class="empty-state", "Upload data"))
|
| 441 |
+
tagList(
|
| 442 |
+
div(class="plot-card", div(class="plot-card-header", "Missing Genotypes"),
|
| 443 |
+
plotOutput("plot_missing", height="320px")),
|
| 444 |
+
div(class="plot-card", div(class="plot-card-header", "Genotype Frequencies"),
|
| 445 |
+
plotOutput("plot_geno_freq", height="320px"))
|
| 446 |
+
)
|
| 447 |
+
})
|
| 448 |
+
|
| 449 |
+
output$plot_missing <- renderPlot({
|
| 450 |
+
cross <- cross_data(); req(cross)
|
| 451 |
+
miss <- nmissing(cross, what="mar")
|
| 452 |
+
df <- data.frame(Marker=names(miss), Missing=miss)
|
| 453 |
+
ggplot(df, aes(x=reorder(Marker, Missing), y=Missing)) +
|
| 454 |
+
geom_col(fill="#1a6cf5") + theme_minimal() +
|
| 455 |
+
theme(axis.text.x = element_blank()) + labs(x="Marker", y="Missing Count")
|
| 456 |
+
})
|
| 457 |
+
|
| 458 |
+
output$plot_geno_freq <- renderPlot({
|
| 459 |
+
cross <- cross_data(); req(cross)
|
| 460 |
+
gt <- pull.geno(cross)
|
| 461 |
+
freq <- colMeans(gt == 1, na.rm=TRUE)
|
| 462 |
+
df <- data.frame(Marker=names(freq), Freq_A=freq, Freq_B=1-freq)
|
| 463 |
+
df_long <- pivot_longer(df, cols=c(Freq_A, Freq_B))
|
| 464 |
+
ggplot(df_long, aes(x=Marker, y=value, fill=name)) +
|
| 465 |
+
geom_col(position="stack") + theme_minimal() +
|
| 466 |
+
theme(axis.text.x = element_blank()) + labs(y="Frequency")
|
| 467 |
+
})
|
| 468 |
+
|
| 469 |
+
# Results Table
|
| 470 |
+
output$results_content <- renderUI({
|
| 471 |
+
res <- analysis_results()
|
| 472 |
+
if (is.null(res) || nrow(res$qtl_table)==0)
|
| 473 |
+
return(div(class="empty-state", "No significant QTLs detected"))
|
| 474 |
+
div(class="plot-card", div(class="plot-card-header", "QTL Results Table"),
|
| 475 |
+
DTOutput("table_qtl") %>% withSpinner())
|
| 476 |
+
})
|
| 477 |
+
|
| 478 |
+
output$table_qtl <- renderDT({
|
| 479 |
+
res <- analysis_results()
|
| 480 |
+
datatable(res$qtl_table, options=list(pageLength=15, scrollX=TRUE), rownames=FALSE)
|
| 481 |
})
|
| 482 |
|
| 483 |
# Download
|
| 484 |
output$download_results <- downloadHandler(
|
| 485 |
+
filename = function() paste0("QTL_Results_", Sys.Date(), ".csv"),
|
| 486 |
+
content = function(file) {
|
| 487 |
+
res <- analysis_results()
|
| 488 |
+
if (!is.null(res) && nrow(res$qtl_table) > 0) {
|
| 489 |
+
write.csv(res$qtl_table, file, row.names = FALSE)
|
| 490 |
} else {
|
| 491 |
+
write.csv(data.frame(Note="No significant QTLs found"), file, row.names = FALSE)
|
| 492 |
}
|
| 493 |
}
|
| 494 |
)
|
|
|
|
| 495 |
}
|
| 496 |
|
| 497 |
+
shinyApp(ui = ui, server = server)
|
|
|