Spaces:
Sleeping
Sleeping
Update app.R
Browse files
app.R
CHANGED
|
@@ -3,74 +3,75 @@ library(htmltools)
|
|
| 3 |
|
| 4 |
# ============================================================
|
| 5 |
# HELPER: Parse uploaded genotype CSV (handles your format)
|
| 6 |
-
# Expected format:
|
| 7 |
-
# Row 1 (header): empty, marker1, marker2, ...
|
| 8 |
-
# Each data row: sample_id, genotype_code, ...
|
| 9 |
-
# Genotype codes: 1 = AA, 2 = BB, 0 = AB (het), -1 or NA = missing
|
| 10 |
# ============================================================
|
| 11 |
parse_geno_csv <- function(path) {
|
|
|
|
|
|
|
| 12 |
df <- tryCatch({
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
# Remove any completely empty rows
|
| 21 |
-
df_raw <- df_raw[!apply(df_raw[,1, drop=FALSE], 1, function(x) is.na(x) | x == ""), ]
|
| 22 |
-
|
| 23 |
-
# Convert genotype columns to numeric
|
| 24 |
-
for (i in 2:ncol(df_raw)) {
|
| 25 |
-
df_raw[[i]] <- suppressWarnings(as.numeric(df_raw[[i]]))
|
| 26 |
-
}
|
| 27 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
df_raw
|
| 29 |
}, error = function(e) {
|
| 30 |
print(paste("Error reading genotype file:", e$message))
|
| 31 |
NULL
|
| 32 |
})
|
| 33 |
|
| 34 |
-
if (is.null(df)) return(NULL)
|
| 35 |
-
if (nrow(df) < 1) return(NULL)
|
| 36 |
-
|
| 37 |
-
# Ensure at least one marker column exists
|
| 38 |
-
if (ncol(df) < 2) return(NULL)
|
| 39 |
-
|
| 40 |
df
|
| 41 |
}
|
| 42 |
|
| 43 |
# ============================================================
|
| 44 |
# HELPER: Parse uploaded phenotype CSV
|
| 45 |
-
# Expected format:
|
| 46 |
-
# Row 1 (header): id, trait1, trait2, ...
|
| 47 |
-
# Each data row: sample_id, numeric_value, ...
|
| 48 |
# ============================================================
|
| 49 |
parse_pheno_csv <- function(path) {
|
|
|
|
|
|
|
| 50 |
df <- tryCatch({
|
| 51 |
df_raw <- read.csv(path, stringsAsFactors = FALSE, check.names = FALSE)
|
|
|
|
| 52 |
# Ensure first column is id
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
}
|
| 56 |
# Convert trait columns to numeric
|
| 57 |
for (i in 2:ncol(df_raw)) {
|
| 58 |
-
df_raw[[i]] <- suppressWarnings(as.numeric(df_raw[[i]]))
|
| 59 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
df_raw
|
| 61 |
}, error = function(e) {
|
| 62 |
print(paste("Error reading phenotype file:", e$message))
|
| 63 |
NULL
|
| 64 |
})
|
|
|
|
| 65 |
df
|
| 66 |
}
|
| 67 |
|
| 68 |
# ============================================================
|
| 69 |
# HELPER: Parse uploaded genetic map CSV
|
| 70 |
-
# Expected format:
|
| 71 |
-
# marker, chromosome, position_cM
|
| 72 |
# ============================================================
|
| 73 |
parse_map_csv <- function(path) {
|
|
|
|
|
|
|
| 74 |
df <- tryCatch(read.csv(path, stringsAsFactors = FALSE, check.names = FALSE),
|
| 75 |
error = function(e) NULL)
|
| 76 |
if (is.null(df)) return(NULL)
|
|
@@ -79,20 +80,20 @@ parse_map_csv <- function(path) {
|
|
| 79 |
names(df) <- tolower(trimws(names(df)))
|
| 80 |
|
| 81 |
# Try to find marker, chr, pos columns
|
| 82 |
-
mk_col
|
| 83 |
chr_col <- names(df)[grepl("chr|chrom|scaffold|linkage", names(df), ignore.case=TRUE)][1]
|
| 84 |
pos_col <- names(df)[grepl("pos|cM|cm|map|dist|bp|location", names(df), ignore.case=TRUE)][1]
|
| 85 |
|
| 86 |
if (is.na(mk_col) || is.na(pos_col)) return(NULL)
|
| 87 |
|
| 88 |
result <- data.frame(
|
| 89 |
-
marker
|
| 90 |
chromosome = if (!is.na(chr_col)) as.character(df[[chr_col]]) else "1",
|
| 91 |
position = suppressWarnings(as.numeric(df[[pos_col]])),
|
| 92 |
stringsAsFactors = FALSE
|
| 93 |
)
|
| 94 |
|
| 95 |
-
result <- result[!is.na(result$position) & !is.na(result$marker), ]
|
| 96 |
result
|
| 97 |
}
|
| 98 |
|
|
@@ -101,7 +102,7 @@ parse_map_csv <- function(path) {
|
|
| 101 |
# ============================================================
|
| 102 |
prepare_cross <- function(geno_df, pheno_df, trait) {
|
| 103 |
# Merge by id
|
| 104 |
-
merged <- merge(geno_df, pheno_df[, c("id", trait), drop=FALSE], by = "id", all=FALSE)
|
| 105 |
|
| 106 |
if (nrow(merged) == 0) {
|
| 107 |
return(NULL)
|
|
@@ -123,7 +124,11 @@ impute_markers <- function(X) {
|
|
| 123 |
nas <- is.na(X[, j])
|
| 124 |
if (any(nas)) {
|
| 125 |
vals <- X[!nas, j]
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
}
|
| 128 |
}
|
| 129 |
X
|
|
@@ -134,10 +139,11 @@ encode_additive <- function(X) {
|
|
| 134 |
for (j in seq_len(ncol(X))) {
|
| 135 |
u <- sort(unique(na.omit(X[, j])))
|
| 136 |
if (length(u) >= 2 && diff(range(u)) > 0) {
|
| 137 |
-
mn <- min(u)
|
|
|
|
| 138 |
X2[, j] <- (X[, j] - (mn + mx) / 2) / ((mx - mn) / 2)
|
| 139 |
} else {
|
| 140 |
-
X2[, j] <- X[, j] - 1.5
|
| 141 |
}
|
| 142 |
}
|
| 143 |
X2
|
|
@@ -146,13 +152,21 @@ encode_additive <- function(X) {
|
|
| 146 |
# ---- 1. Single Marker Regression ----
|
| 147 |
run_smr <- function(y, X, mpos, mchr) {
|
| 148 |
n_m <- ncol(X)
|
| 149 |
-
results <- data.frame(
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
for (j in seq_len(n_m)) {
|
| 155 |
-
g <- X[, j]
|
|
|
|
| 156 |
if (sum(valid) < 3) next
|
| 157 |
|
| 158 |
gv <- as.factor(g[valid])
|
|
@@ -170,25 +184,33 @@ run_smr <- function(y, X, mpos, mchr) {
|
|
| 170 |
if (rss1 > 0 && rss0 > 0) {
|
| 171 |
results$LOD[j] <- max(0, (n/2) * log10(rss0/rss1))
|
| 172 |
}
|
| 173 |
-
results$pvalue[j] <- tryCatch(anova(fit0, fit1)$`Pr(>F)`[2], error=function(e) NA)
|
| 174 |
results$R2[j] <- summary(fit1)$r.squared
|
| 175 |
coefs <- coef(fit1)
|
| 176 |
results$additive[j] <- if (length(coefs) > 1) coefs[2] else 0
|
| 177 |
-
}, error=function(e) NULL)
|
| 178 |
}
|
| 179 |
results
|
| 180 |
}
|
| 181 |
|
| 182 |
-
# ---- 2. Interval Mapping
|
| 183 |
run_im <- function(y, X, mpos, mchr) {
|
| 184 |
Xa <- encode_additive(X)
|
| 185 |
-
results <- data.frame(
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
for (j in seq_len(ncol(Xa))) {
|
| 191 |
-
g <- Xa[, j]
|
|
|
|
| 192 |
if (sum(valid) < 3) next
|
| 193 |
|
| 194 |
tryCatch({
|
|
@@ -201,10 +223,10 @@ run_im <- function(y, X, mpos, mchr) {
|
|
| 201 |
if (rss1 > 0 && rss0 > 0) {
|
| 202 |
results$LOD[j] <- max(0, (n/2) * log10(rss0/rss1))
|
| 203 |
}
|
| 204 |
-
results$pvalue[j] <- tryCatch(summary(fit1)$coefficients[2, 4], error=function(e) NA)
|
| 205 |
results$R2[j] <- summary(fit1)$r.squared
|
| 206 |
-
results$additive[j] <- tryCatch(coef(fit1)[2], error=function(e) 0)
|
| 207 |
-
}, error=function(e) NULL)
|
| 208 |
}
|
| 209 |
results
|
| 210 |
}
|
|
@@ -212,10 +234,17 @@ run_im <- function(y, X, mpos, mchr) {
|
|
| 212 |
# ---- 3. Composite Interval Mapping ----
|
| 213 |
run_cim <- function(y, X, mpos, mchr) {
|
| 214 |
Xa <- encode_additive(X)
|
| 215 |
-
results <- data.frame(
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
|
| 220 |
for (j in seq_len(ncol(Xa))) {
|
| 221 |
cofactors <- setdiff(seq_len(ncol(Xa)), j)
|
|
@@ -225,12 +254,12 @@ run_cim <- function(y, X, mpos, mchr) {
|
|
| 225 |
if (sum(valid) < 4) next
|
| 226 |
|
| 227 |
tryCatch({
|
| 228 |
-
cof_df <- as.data.frame(Xa[valid, cofactors, drop=FALSE])
|
| 229 |
cof_df$g_focal <- Xa[valid, j]
|
| 230 |
yv <- y[valid]
|
| 231 |
|
| 232 |
-
fit0 <- lm(yv ~ ., data=cof_df[, -ncol(cof_df), drop=FALSE])
|
| 233 |
-
fit1 <- lm(yv ~ ., data=cof_df)
|
| 234 |
n <- sum(valid)
|
| 235 |
rss0 <- sum(residuals(fit0)^2)
|
| 236 |
rss1 <- sum(residuals(fit1)^2)
|
|
@@ -238,230 +267,16 @@ run_cim <- function(y, X, mpos, mchr) {
|
|
| 238 |
if (rss1 > 0 && rss0 > 0) {
|
| 239 |
results$LOD[j] <- max(0, (n/2) * log10(rss0/rss1))
|
| 240 |
}
|
| 241 |
-
results$pvalue[j] <- tryCatch(anova(fit0, fit1)$`Pr(>F)`[2], error=function(e) NA)
|
| 242 |
results$R2[j] <- summary(fit1)$r.squared
|
| 243 |
coefs <- coef(fit1)
|
| 244 |
-
results$additive[j] <- tryCatch(coefs["g_focal"], error=function(e) 0)
|
| 245 |
-
}, error=function(e) NULL)
|
| 246 |
}
|
| 247 |
results
|
| 248 |
}
|
| 249 |
|
| 250 |
-
# ----
|
| 251 |
-
run_mqm <- function(y, X, mpos, mchr) {
|
| 252 |
-
res <- run_cim(y, X, mpos, mchr)
|
| 253 |
-
res$model <- "Multiple QTL Model (MQM)"
|
| 254 |
-
res$LOD <- res$LOD * runif(nrow(res), 0.92, 1.08)
|
| 255 |
-
res
|
| 256 |
-
}
|
| 257 |
-
|
| 258 |
-
# ---- 5. Bayesian Interval Mapping ----
|
| 259 |
-
run_bim <- function(y, X, mpos, mchr) {
|
| 260 |
-
results <- run_im(y, X, mpos, mchr)
|
| 261 |
-
results$model <- "Bayesian Interval Mapping"
|
| 262 |
-
lambda <- 0.1
|
| 263 |
-
for (j in seq_len(nrow(results))) {
|
| 264 |
-
if (!is.na(results$LOD[j])) {
|
| 265 |
-
results$LOD[j] <- results$LOD[j] * (1/(1+lambda))
|
| 266 |
-
if (!is.na(results$additive[j])) results$additive[j] <- results$additive[j]*(1/(1+lambda))
|
| 267 |
-
}
|
| 268 |
-
}
|
| 269 |
-
results
|
| 270 |
-
}
|
| 271 |
-
|
| 272 |
-
# ---- 6. LASSO QTL ----
|
| 273 |
-
run_lasso_qtl <- function(y, X, mpos, mchr) {
|
| 274 |
-
Xa <- encode_additive(impute_markers(X))
|
| 275 |
-
valid <- complete.cases(cbind(y, Xa))
|
| 276 |
-
yv <- y[valid]
|
| 277 |
-
Xv <- Xa[valid, , drop=FALSE]
|
| 278 |
-
n <- nrow(Xv)
|
| 279 |
-
p <- ncol(Xv)
|
| 280 |
-
|
| 281 |
-
if (n < 2 || p < 1) {
|
| 282 |
-
return(data.frame(marker=colnames(X), position=mpos[seq_len(p)],
|
| 283 |
-
chromosome=mchr[seq_len(p)], LOD=NA, pvalue=NA,
|
| 284 |
-
R2=NA, additive=0, model="LASSO QTL", stringsAsFactors=FALSE))
|
| 285 |
-
}
|
| 286 |
-
|
| 287 |
-
lambda <- 0.5
|
| 288 |
-
beta <- rep(0, p)
|
| 289 |
-
|
| 290 |
-
for (iter in 1:50) {
|
| 291 |
-
for (j in 1:p) {
|
| 292 |
-
r <- yv - Xv %*% beta + Xv[,j] * beta[j]
|
| 293 |
-
rho <- sum(Xv[,j] * r) / n
|
| 294 |
-
z <- sum(Xv[,j]^2) / n
|
| 295 |
-
if (z > 0) {
|
| 296 |
-
beta[j] <- sign(rho) * max(0, abs(rho) - lambda) / z
|
| 297 |
-
}
|
| 298 |
-
}
|
| 299 |
-
}
|
| 300 |
-
|
| 301 |
-
results <- data.frame(marker=colnames(X), position=mpos[seq_len(p)],
|
| 302 |
-
chromosome=mchr[seq_len(p)], LOD=NA_real_, pvalue=NA_real_,
|
| 303 |
-
R2=NA_real_, additive=beta, model="LASSO QTL", stringsAsFactors=FALSE)
|
| 304 |
-
|
| 305 |
-
for (j in seq_len(p)) {
|
| 306 |
-
if (var(yv) > 0) {
|
| 307 |
-
results$R2[j] <- min(1, max(0, (beta[j]^2 * var(Xv[,j])) / var(yv)))
|
| 308 |
-
results$LOD[j] <- results$R2[j] * (n/2) * log10(exp(1))
|
| 309 |
-
results$pvalue[j] <- tryCatch(pf((results$R2[j]/1)/((1-results$R2[j])/(n-2)), 1, n-2, lower.tail=FALSE), error=function(e) NA)
|
| 310 |
-
}
|
| 311 |
-
}
|
| 312 |
-
results
|
| 313 |
-
}
|
| 314 |
-
|
| 315 |
-
# ---- 7. Ridge Regression QTL ----
|
| 316 |
-
run_ridge_qtl <- function(y, X, mpos, mchr) {
|
| 317 |
-
Xa <- encode_additive(impute_markers(X))
|
| 318 |
-
valid <- complete.cases(cbind(y, Xa))
|
| 319 |
-
yv <- y[valid]
|
| 320 |
-
Xv <- Xa[valid, , drop=FALSE]
|
| 321 |
-
n <- nrow(Xv)
|
| 322 |
-
p <- ncol(Xv)
|
| 323 |
-
|
| 324 |
-
if (n < 2 || p < 1) {
|
| 325 |
-
return(data.frame(marker=colnames(X), position=mpos[seq_len(p)],
|
| 326 |
-
chromosome=mchr[seq_len(p)], LOD=NA, pvalue=NA,
|
| 327 |
-
R2=NA, additive=0, model="Ridge Regression QTL", stringsAsFactors=FALSE))
|
| 328 |
-
}
|
| 329 |
-
|
| 330 |
-
lambda <- 1.0
|
| 331 |
-
XtX <- t(Xv) %*% Xv + lambda * diag(p)
|
| 332 |
-
Xty <- t(Xv) %*% yv
|
| 333 |
-
beta <- tryCatch(solve(XtX, Xty), error=function(e) rep(0, p))
|
| 334 |
-
|
| 335 |
-
results <- data.frame(marker=colnames(X), position=mpos[seq_len(p)],
|
| 336 |
-
chromosome=mchr[seq_len(p)], LOD=NA_real_, pvalue=NA_real_,
|
| 337 |
-
R2=NA_real_, additive=as.numeric(beta), model="Ridge Regression QTL",
|
| 338 |
-
stringsAsFactors=FALSE)
|
| 339 |
-
|
| 340 |
-
for (j in seq_len(p)) {
|
| 341 |
-
if (var(yv) > 0) {
|
| 342 |
-
r2 <- min(1, max(0, (beta[j]^2 * var(Xv[,j])) / var(yv)))
|
| 343 |
-
results$R2[j] <- r2
|
| 344 |
-
results$LOD[j] <- max(0, r2 * (n/2) * log10(exp(1)))
|
| 345 |
-
results$pvalue[j] <- tryCatch(pf((r2/1)/((1-r2)/(n-2)), 1, n-2, lower.tail=FALSE), error=function(e) NA)
|
| 346 |
-
}
|
| 347 |
-
}
|
| 348 |
-
results
|
| 349 |
-
}
|
| 350 |
-
|
| 351 |
-
# ---- 8. Two-Way Epistasis ----
|
| 352 |
-
run_epistasis <- function(y, X, mpos, mchr) {
|
| 353 |
-
Xa <- encode_additive(impute_markers(X))
|
| 354 |
-
n_m <- ncol(Xa)
|
| 355 |
-
|
| 356 |
-
results <- data.frame(marker=colnames(X), position=mpos[seq_len(n_m)],
|
| 357 |
-
chromosome=mchr[seq_len(n_m)], LOD=NA_real_, pvalue=NA_real_,
|
| 358 |
-
R2=NA_real_, additive=NA_real_, model="Epistasis (2-way)",
|
| 359 |
-
stringsAsFactors=FALSE)
|
| 360 |
-
|
| 361 |
-
for (j in seq_len(n_m)) {
|
| 362 |
-
partners <- setdiff(seq_len(n_m), j)
|
| 363 |
-
lods <- c()
|
| 364 |
-
|
| 365 |
-
for (k in partners[1:min(10, length(partners))]) { # Limit to 10 partners for speed
|
| 366 |
-
tryCatch({
|
| 367 |
-
g1 <- Xa[, j]
|
| 368 |
-
g2 <- Xa[, k]
|
| 369 |
-
valid <- complete.cases(cbind(y, g1, g2))
|
| 370 |
-
|
| 371 |
-
if (sum(valid) < 4) next
|
| 372 |
-
|
| 373 |
-
fit1 <- lm(y[valid] ~ g1[valid] + g2[valid])
|
| 374 |
-
fit2 <- lm(y[valid] ~ g1[valid] * g2[valid])
|
| 375 |
-
n <- sum(valid)
|
| 376 |
-
rss1 <- sum(residuals(fit1)^2)
|
| 377 |
-
rss2 <- sum(residuals(fit2)^2)
|
| 378 |
-
|
| 379 |
-
if (rss2 > 0 && rss1 > 0) {
|
| 380 |
-
lods <- c(lods, max(0, (n/2) * log10(rss1/rss2)))
|
| 381 |
-
}
|
| 382 |
-
}, error=function(e) NULL)
|
| 383 |
-
}
|
| 384 |
-
|
| 385 |
-
if (length(lods) > 0) {
|
| 386 |
-
results$LOD[j] <- max(lods, na.rm=TRUE)
|
| 387 |
-
if (!is.na(results$LOD[j]) && n_m > 0) {
|
| 388 |
-
results$R2[j] <- min(1, 1 - exp(-2 * results$LOD[j] / n_m))
|
| 389 |
-
}
|
| 390 |
-
results$pvalue[j] <- tryCatch(pchisq(results$LOD[j] * 2 * log(10), 1, lower.tail=FALSE), error=function(e) NA)
|
| 391 |
-
results$additive[j] <- mean(Xa[, j] * y, na.rm=TRUE)
|
| 392 |
-
}
|
| 393 |
-
}
|
| 394 |
-
results
|
| 395 |
-
}
|
| 396 |
-
|
| 397 |
-
# ---- 9. Mixed Linear Model ----
|
| 398 |
-
run_mlm <- function(y, X, mpos, mchr) {
|
| 399 |
-
Xa <- encode_additive(impute_markers(X))
|
| 400 |
-
n <- length(y)
|
| 401 |
-
|
| 402 |
-
results <- data.frame(marker=colnames(X), position=mpos[seq_len(ncol(X))],
|
| 403 |
-
chromosome=mchr[seq_len(ncol(X))], LOD=NA_real_, pvalue=NA_real_,
|
| 404 |
-
R2=NA_real_, additive=NA_real_, model="Mixed Linear Model",
|
| 405 |
-
stringsAsFactors=FALSE)
|
| 406 |
-
|
| 407 |
-
for (j in seq_len(ncol(Xa))) {
|
| 408 |
-
g <- Xa[, j]
|
| 409 |
-
valid <- complete.cases(cbind(y, g))
|
| 410 |
-
if (sum(valid) < 3) next
|
| 411 |
-
|
| 412 |
-
tryCatch({
|
| 413 |
-
yv <- y[valid]
|
| 414 |
-
gv <- g[valid]
|
| 415 |
-
n_v <- sum(valid)
|
| 416 |
-
|
| 417 |
-
fit1 <- lm(yv ~ gv)
|
| 418 |
-
rss0 <- sum((yv - mean(yv))^2)
|
| 419 |
-
rss1 <- sum(residuals(fit1)^2)
|
| 420 |
-
|
| 421 |
-
if (rss1 > 0 && rss0 > 0) {
|
| 422 |
-
results$LOD[j] <- max(0, (n_v/2) * log10(rss0/rss1))
|
| 423 |
-
}
|
| 424 |
-
results$pvalue[j] <- tryCatch(summary(fit1)$coefficients[2, 4], error=function(e) NA)
|
| 425 |
-
results$R2[j] <- summary(fit1)$r.squared
|
| 426 |
-
results$additive[j] <- tryCatch(coef(fit1)[2], error=function(e) 0)
|
| 427 |
-
}, error=function(e) NULL)
|
| 428 |
-
}
|
| 429 |
-
results
|
| 430 |
-
}
|
| 431 |
-
|
| 432 |
-
# ---- 10. Kruskal-Wallis Non-Parametric ----
|
| 433 |
-
run_kruskal <- function(y, X, mpos, mchr) {
|
| 434 |
-
results <- data.frame(marker=colnames(X), position=mpos[seq_len(ncol(X))],
|
| 435 |
-
chromosome=mchr[seq_len(ncol(X))], LOD=NA_real_, pvalue=NA_real_,
|
| 436 |
-
R2=NA_real_, additive=NA_real_, model="Kruskal-Wallis (Non-Parametric)",
|
| 437 |
-
stringsAsFactors=FALSE)
|
| 438 |
-
|
| 439 |
-
for (j in seq_len(ncol(X))) {
|
| 440 |
-
g <- X[, j]
|
| 441 |
-
valid <- !is.na(g)
|
| 442 |
-
if (sum(valid) < 3) next
|
| 443 |
-
|
| 444 |
-
gv <- as.factor(g[valid])
|
| 445 |
-
yv <- y[valid]
|
| 446 |
-
|
| 447 |
-
if (nlevels(gv) < 2) next
|
| 448 |
-
|
| 449 |
-
tryCatch({
|
| 450 |
-
kw <- kruskal.test(yv ~ gv)
|
| 451 |
-
results$pvalue[j] <- kw$p.value
|
| 452 |
-
results$LOD[j] <- max(0, -log10(kw$p.value + 1e-10) * log10(exp(1)) * 2)
|
| 453 |
-
results$R2[j] <- min(1, kw$statistic / (sum(valid) - 1))
|
| 454 |
-
|
| 455 |
-
means <- tapply(yv, gv, mean, na.rm=TRUE)
|
| 456 |
-
if (length(means) >= 2) {
|
| 457 |
-
results$additive[j] <- diff(means)[1]
|
| 458 |
-
}
|
| 459 |
-
}, error=function(e) NULL)
|
| 460 |
-
}
|
| 461 |
-
results
|
| 462 |
-
}
|
| 463 |
-
|
| 464 |
-
# ---- Run all models ----
|
| 465 |
run_all_models <- function(geno_df, pheno_df, trait, map_df) {
|
| 466 |
cross <- prepare_cross(geno_df, pheno_df, trait)
|
| 467 |
if (is.null(cross)) return(NULL)
|
|
@@ -487,38 +302,15 @@ run_all_models <- function(geno_df, pheno_df, trait, map_df) {
|
|
| 487 |
mpos[is.na(mpos)] <- seq_pos[is.na(mpos)]
|
| 488 |
}
|
| 489 |
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
run_cim(y, X, mpos, mchr),
|
| 494 |
-
run_mqm(y, X, mpos, mchr),
|
| 495 |
-
run_bim(y, X, mpos, mchr),
|
| 496 |
-
run_lasso_qtl(y, X, mpos, mchr),
|
| 497 |
-
run_ridge_qtl(y, X, mpos, mchr),
|
| 498 |
-
run_epistasis(y, X, mpos, mchr),
|
| 499 |
-
run_mlm(y, X, mpos, mchr),
|
| 500 |
-
run_kruskal(y, X, mpos, mchr)
|
| 501 |
-
)
|
| 502 |
-
|
| 503 |
-
do.call(rbind, models)
|
| 504 |
-
}
|
| 505 |
-
|
| 506 |
-
# Permutation threshold
|
| 507 |
-
permutation_threshold <- function(y, X, mpos, mchr, n_perm=100, alpha=0.05) {
|
| 508 |
-
X_imp <- impute_markers(X)
|
| 509 |
-
lod_max <- numeric(n_perm)
|
| 510 |
-
|
| 511 |
-
for (i in seq_len(n_perm)) {
|
| 512 |
-
yp <- sample(y)
|
| 513 |
-
res <- run_smr(yp, X_imp, mpos, mchr)
|
| 514 |
-
lod_max[i] <- max(res$LOD, na.rm=TRUE)
|
| 515 |
-
}
|
| 516 |
|
| 517 |
-
|
| 518 |
}
|
| 519 |
|
| 520 |
# ============================================================
|
| 521 |
-
# TEMPLATE GENERATION
|
| 522 |
# ============================================================
|
| 523 |
make_geno_template <- function() {
|
| 524 |
data.frame(
|
|
@@ -554,438 +346,410 @@ make_map_template <- function() {
|
|
| 554 |
# ============================================================
|
| 555 |
ui <- fluidPage(
|
| 556 |
tags$head(
|
| 557 |
-
tags$link(rel="preconnect", href="https://fonts.googleapis.com"),
|
| 558 |
-
tags$link(rel="preconnect", href="https://fonts.gstatic.com", crossorigin=NA),
|
| 559 |
-
tags$link(href="https://fonts.googleapis.com/css2?family=Space+Mono:wght@400;700&family=Crimson+Pro:ital,wght@0,300;0,600;1,300&display=swap", rel="stylesheet"),
|
| 560 |
tags$style(HTML("
|
| 561 |
:root {
|
| 562 |
--bg:#0a0e1a; --bg2:#0f1526; --bg3:#151d35; --panel:#1a2240;
|
| 563 |
--accent:#00d4ff; --accent2:#ff6b35; --accent3:#39ff14; --gold:#ffd700;
|
| 564 |
--text:#e8edf5; --text-dim:#7a8aaa; --border:rgba(0,212,255,0.2);
|
| 565 |
-
--font-mono:'
|
| 566 |
-
|
| 567 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 568 |
}
|
| 569 |
-
*{box-sizing:border-box;margin:0;padding:0;}
|
| 570 |
-
body{background:var(--bg);color:var(--text);font-family:var(--font-mono);min-height:100vh;overflow-x:hidden;}
|
| 571 |
-
body::before{content:'';position:fixed;top:0;left:0;right:0;bottom:0;
|
| 572 |
-
background:radial-gradient(ellipse at 20% 20%,rgba(0,212,255,0.04) 0%,transparent 60%),
|
| 573 |
-
radial-gradient(ellipse at 80% 80%,rgba(255,107,53,0.03) 0%,transparent 60%);
|
| 574 |
-
pointer-events:none;z-index:0;}
|
| 575 |
-
.header-bar{background:linear-gradient(135deg,var(--bg2) 0%,var(--bg3) 100%);
|
| 576 |
-
border-bottom:1px solid var(--border);padding:18px 32px;
|
| 577 |
-
display:flex;align-items:center;gap:20px;position:relative;z-index:10;}
|
| 578 |
-
.header-bar::after{content:'';position:absolute;bottom:0;left:0;right:0;height:1px;
|
| 579 |
-
background:linear-gradient(90deg,transparent,var(--accent),var(--accent2),var(--accent3),transparent);}
|
| 580 |
-
.header-title{font-family:var(--font-mono);font-size:1.3rem;font-weight:700;
|
| 581 |
-
color:var(--accent);letter-spacing:.15em;text-transform:uppercase;}
|
| 582 |
-
.header-sub{font-family:var(--font-serif);font-style:italic;font-size:.95rem;color:var(--text-dim);}
|
| 583 |
-
.dna-icon{font-size:2rem;filter:drop-shadow(0 0 8px var(--accent));}
|
| 584 |
-
.main-layout{display:flex;min-height:calc(100vh - 65px);position:relative;z-index:1;}
|
| 585 |
-
.sidebar{width:270px;min-width:270px;background:var(--bg2);border-right:1px solid var(--border);
|
| 586 |
-
padding:20px 16px;display:flex;flex-direction:column;gap:14px;overflow-y:auto;}
|
| 587 |
-
.sidebar-section{background:var(--panel);border:1px solid var(--border);border-radius:8px;padding:14px;}
|
| 588 |
-
.sidebar-label{font-size:.65rem;letter-spacing:.2em;text-transform:uppercase;color:var(--accent);
|
| 589 |
-
margin-bottom:10px;border-bottom:1px solid var(--border);padding-bottom:6px;}
|
| 590 |
-
.control-group{margin-bottom:12px;}
|
| 591 |
-
.control-group label{display:block;font-size:.7rem;color:var(--text-dim);margin-bottom:5px;letter-spacing:.08em;}
|
| 592 |
-
select,input[type=range]{width:100%;background:var(--bg);color:var(--text);
|
| 593 |
-
border:1px solid var(--border);border-radius:4px;padding:6px 8px;
|
| 594 |
-
font-family:var(--font-mono);font-size:.75rem;outline:none;transition:border-color .2s;}
|
| 595 |
-
select:focus{border-color:var(--accent);box-shadow:var(--glow);}
|
| 596 |
-
.run-btn{width:100%;padding:12px;background:linear-gradient(135deg,var(--accent) 0%,#0099bb 100%);
|
| 597 |
-
color:var(--bg);border:none;border-radius:6px;font-family:var(--font-mono);font-size:.8rem;
|
| 598 |
-
font-weight:700;letter-spacing:.12em;text-transform:uppercase;cursor:pointer;
|
| 599 |
-
transition:all .2s;box-shadow:0 4px 15px rgba(0,212,255,.3);}
|
| 600 |
-
.run-btn:hover{transform:translateY(-1px);box-shadow:0 6px 25px rgba(0,212,255,.5);}
|
| 601 |
-
.perm-btn{width:100%;padding:10px;background:linear-gradient(135deg,var(--accent2) 0%,#cc4400 100%);
|
| 602 |
-
color:var(--text);border:none;border-radius:6px;font-family:var(--font-mono);font-size:.75rem;
|
| 603 |
-
font-weight:700;letter-spacing:.1em;text-transform:uppercase;cursor:pointer;
|
| 604 |
-
transition:all .2s;margin-top:6px;box-shadow:0 4px 15px rgba(255,107,53,.3);}
|
| 605 |
-
.perm-btn:hover{transform:translateY(-1px);box-shadow:0 6px 25px rgba(255,107,53,.5);}
|
| 606 |
-
.stat-box{background:var(--bg);border:1px solid var(--border);border-radius:6px;
|
| 607 |
-
padding:10px;text-align:center;margin-bottom:8px;}
|
| 608 |
-
.stat-val{font-size:1.4rem;color:var(--accent);font-weight:700;}
|
| 609 |
-
.stat-lbl{font-size:.6rem;color:var(--text-dim);letter-spacing:.15em;text-transform:uppercase;}
|
| 610 |
-
.content-area{flex:1;padding:20px;overflow-y:auto;display:flex;flex-direction:column;gap:20px;}
|
| 611 |
-
.tab-bar{display:flex;gap:4px;background:var(--bg2);border:1px solid var(--border);
|
| 612 |
-
border-radius:8px;padding:4px;flex-wrap:wrap;}
|
| 613 |
-
.tab-btn{padding:8px 14px;background:transparent;color:var(--text-dim);border:none;border-radius:5px;
|
| 614 |
-
font-family:var(--font-mono);font-size:.7rem;letter-spacing:.1em;text-transform:uppercase;
|
| 615 |
-
cursor:pointer;transition:all .2s;white-space:nowrap;}
|
| 616 |
-
.tab-btn.active{background:var(--panel);color:var(--accent);border:1px solid var(--border);box-shadow:var(--glow);}
|
| 617 |
-
.tab-btn:hover:not(.active){color:var(--text);}
|
| 618 |
-
.panel-card{background:var(--bg2);border:1px solid var(--border);border-radius:10px;overflow:hidden;}
|
| 619 |
-
.panel-card-header{background:var(--panel);padding:12px 20px;border-bottom:1px solid var(--border);
|
| 620 |
-
display:flex;align-items:center;justify-content:space-between;}
|
| 621 |
-
.panel-card-title{font-size:.75rem;letter-spacing:.18em;text-transform:uppercase;color:var(--accent);}
|
| 622 |
-
.panel-card-body{padding:16px;}
|
| 623 |
-
.plot-container{width:100%;background:var(--bg);border-radius:6px;overflow:hidden;position:relative;}
|
| 624 |
-
.results-grid{display:grid;grid-template-columns:1fr 1fr;gap:16px;}
|
| 625 |
-
.results-grid-3{display:grid;grid-template-columns:1fr 1fr 1fr;gap:16px;}
|
| 626 |
-
table.qtl-table{width:100%;border-collapse:collapse;font-size:.7rem;}
|
| 627 |
-
table.qtl-table thead th{background:var(--panel);color:var(--accent);padding:8px 10px;
|
| 628 |
-
text-align:left;border-bottom:1px solid var(--border);letter-spacing:.1em;
|
| 629 |
-
text-transform:uppercase;position:sticky;top:0;}
|
| 630 |
-
table.qtl-table tbody tr{border-bottom:1px solid rgba(255,255,255,.04);}
|
| 631 |
-
table.qtl-table tbody tr:hover{background:var(--panel);}
|
| 632 |
-
table.qtl-table tbody td{padding:7px 10px;color:var(--text-dim);}
|
| 633 |
-
table.qtl-table tbody td:nth-child(4){color:var(--accent3);font-weight:700;}
|
| 634 |
-
.sig-row td{color:var(--text)!important;}.sig-row td:nth-child(4){color:var(--gold)!important;}
|
| 635 |
-
.badge{display:inline-block;padding:2px 7px;border-radius:3px;font-size:.6rem;
|
| 636 |
-
letter-spacing:.1em;text-transform:uppercase;font-weight:700;}
|
| 637 |
-
.badge-sig{background:rgba(57,255,20,.15);color:var(--accent3);border:1px solid rgba(57,255,20,.3);}
|
| 638 |
-
.badge-ns{background:rgba(255,255,255,.05);color:var(--text-dim);border:1px solid rgba(255,255,255,.1);}
|
| 639 |
-
.threshold-badge{display:inline-block;padding:3px 10px;background:rgba(255,215,0,.1);
|
| 640 |
-
border:1px solid rgba(255,215,0,.3);color:var(--gold);border-radius:4px;
|
| 641 |
-
font-size:.7rem;letter-spacing:.1em;}
|
| 642 |
-
.export-btn{padding:7px 14px;background:var(--bg);color:var(--accent);
|
| 643 |
-
border:1px solid var(--border);border-radius:4px;font-family:var(--font-mono);
|
| 644 |
-
font-size:.65rem;letter-spacing:.1em;cursor:pointer;transition:all .2s;}
|
| 645 |
-
.export-btn:hover{border-color:var(--accent);box-shadow:var(--glow);}
|
| 646 |
-
.scrollable-table{max-height:400px;overflow-y:auto;}
|
| 647 |
-
.env-tag{display:inline-block;padding:2px 8px;border-radius:3px;font-size:.65rem;font-weight:700;letter-spacing:.08em;}
|
| 648 |
-
.env1{background:rgba(0,212,255,.15);color:var(--accent);border:1px solid rgba(0,212,255,.3);}
|
| 649 |
-
.env2{background:rgba(255,107,53,.15);color:var(--accent2);border:1px solid rgba(255,107,53,.3);}
|
| 650 |
-
|
| 651 |
-
/* ====== UPLOAD STYLES ====== */
|
| 652 |
-
.upload-zone{border:2px dashed var(--border);border-radius:8px;padding:18px 14px;
|
| 653 |
-
text-align:center;cursor:pointer;transition:all .3s;background:var(--bg);position:relative;}
|
| 654 |
-
.upload-zone:hover{border-color:var(--accent);background:rgba(0,212,255,.04);}
|
| 655 |
-
.upload-zone input[type=file]{position:absolute;inset:0;opacity:0;cursor:pointer;width:100%;height:100%;}
|
| 656 |
-
.upload-icon{font-size:1.6rem;display:block;margin-bottom:6px;}
|
| 657 |
-
.upload-label{font-size:.68rem;color:var(--text-dim);letter-spacing:.06em;}
|
| 658 |
-
.upload-label strong{color:var(--accent);}
|
| 659 |
-
.file-status{margin-top:8px;font-size:.65rem;padding:5px 8px;border-radius:4px;}
|
| 660 |
-
.file-ok{background:var(--ok);color:var(--accent3);border:1px solid rgba(57,255,20,.3);}
|
| 661 |
-
.file-warn{background:var(--warn);color:var(--accent2);border:1px solid rgba(255,107,53,.3);}
|
| 662 |
-
.file-info{background:rgba(0,212,255,.08);color:var(--accent);border:1px solid var(--border);}
|
| 663 |
-
.upload-sep{text-align:center;font-size:.6rem;color:var(--text-dim);margin:6px 0;letter-spacing:.15em;}
|
| 664 |
-
.tmpl-btn{width:100%;padding:7px;background:transparent;color:var(--text-dim);
|
| 665 |
-
border:1px solid var(--border);border-radius:4px;font-family:var(--font-mono);
|
| 666 |
-
font-size:.65rem;letter-spacing:.08em;cursor:pointer;transition:all .2s;margin-top:4px;}
|
| 667 |
-
.tmpl-btn:hover{color:var(--accent);border-color:var(--accent);}
|
| 668 |
-
.data-preview{background:var(--bg);border:1px solid var(--border);border-radius:6px;
|
| 669 |
-
padding:12px;font-size:.65rem;color:var(--text-dim);overflow-x:auto;max-height:180px;overflow-y:auto;}
|
| 670 |
-
.data-preview table{width:100%;border-collapse:collapse;}
|
| 671 |
-
.data-preview th{color:var(--accent);padding:4px 8px;border-bottom:1px solid var(--border);
|
| 672 |
-
text-align:left;font-size:.62rem;letter-spacing:.08em;white-space:nowrap;}
|
| 673 |
-
.data-preview td{padding:3px 8px;border-bottom:1px solid rgba(255,255,255,.04);white-space:nowrap;}
|
| 674 |
-
.format-hint{background:rgba(0,212,255,.06);border:1px solid var(--border);border-radius:6px;
|
| 675 |
-
padding:10px;font-size:.65rem;color:var(--text-dim);line-height:1.7;margin-top:8px;}
|
| 676 |
-
.format-hint code{color:var(--accent3);font-family:var(--font-mono);}
|
| 677 |
-
.spinner{width:36px;height:36px;border:3px solid var(--border);border-top-color:var(--accent);
|
| 678 |
-
border-radius:50%;animation:spin .8s linear infinite;margin:0 auto;}
|
| 679 |
-
@keyframes spin{to{transform:rotate(360deg);}}
|
| 680 |
-
#loadingScreen{position:fixed;top:0;left:0;right:0;bottom:0;background:rgba(10,14,26,.95);
|
| 681 |
-
display:flex;flex-direction:column;align-items:center;justify-content:center;z-index:9999;gap:16px;}
|
| 682 |
-
.loading-text{font-size:.8rem;color:var(--accent);letter-spacing:.2em;text-transform:uppercase;}
|
| 683 |
-
#mainContent{display:none;}
|
| 684 |
"))
|
| 685 |
),
|
| 686 |
-
|
| 687 |
-
#
|
| 688 |
-
tags$div(
|
| 689 |
-
tags$
|
| 690 |
-
tags$div(
|
| 691 |
-
|
| 692 |
-
|
| 693 |
-
|
| 694 |
-
document.getElementById('mainContent').style.display='block';
|
| 695 |
-
});
|
| 696 |
-
"))
|
| 697 |
),
|
| 698 |
-
|
| 699 |
-
tags$div(
|
| 700 |
-
#
|
| 701 |
-
tags$div(class="
|
| 702 |
-
tags$
|
| 703 |
-
|
| 704 |
-
|
| 705 |
-
tags$div(class="
|
| 706 |
-
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
|
| 710 |
-
)
|
| 711 |
-
),
|
| 712 |
-
|
| 713 |
-
tags$div(class="main-layout",
|
| 714 |
-
# Sidebar
|
| 715 |
-
tags$div(class="sidebar",
|
| 716 |
-
|
| 717 |
-
# ---- DATA UPLOAD SECTION ----
|
| 718 |
-
tags$div(class="sidebar-section",
|
| 719 |
-
tags$div(class="sidebar-label","📂 Upload Your Data"),
|
| 720 |
-
|
| 721 |
-
# Genotype file
|
| 722 |
-
tags$div(class="control-group",
|
| 723 |
-
tags$label("Genotype File (CSV)"),
|
| 724 |
-
tags$div(class="upload-zone",
|
| 725 |
-
fileInput("geno_file", NULL, accept=".csv", buttonLabel="", placeholder=""),
|
| 726 |
-
tags$span(class="upload-icon","🧬"),
|
| 727 |
-
tags$div(class="upload-label", HTML("<strong>Click</strong> to upload genotype CSV"))
|
| 728 |
-
),
|
| 729 |
-
uiOutput("geno_status")
|
| 730 |
-
),
|
| 731 |
-
|
| 732 |
-
# Phenotype file
|
| 733 |
-
tags$div(class="control-group",
|
| 734 |
-
tags$label("Phenotype File (CSV)"),
|
| 735 |
-
tags$div(class="upload-zone",
|
| 736 |
-
fileInput("pheno_file", NULL, accept=".csv", buttonLabel="", placeholder=""),
|
| 737 |
-
tags$span(class="upload-icon","🌾"),
|
| 738 |
-
tags$div(class="upload-label", HTML("<strong>Click</strong> to upload phenotype CSV"))
|
| 739 |
-
),
|
| 740 |
-
uiOutput("pheno_status")
|
| 741 |
-
),
|
| 742 |
-
|
| 743 |
-
# Map file
|
| 744 |
-
tags$div(class="control-group",
|
| 745 |
-
tags$label("Genetic Map File (CSV)"),
|
| 746 |
-
tags$div(class="upload-zone",
|
| 747 |
-
fileInput("map_file", NULL, accept=".csv", buttonLabel="", placeholder=""),
|
| 748 |
-
tags$span(class="upload-icon","🗺️"),
|
| 749 |
-
tags$div(class="upload-label", HTML("<strong>Click</strong> to upload map CSV"))
|
| 750 |
-
),
|
| 751 |
-
uiOutput("map_status")
|
| 752 |
-
),
|
| 753 |
-
|
| 754 |
-
tags$div(class="upload-sep","— OR DOWNLOAD TEMPLATES —"),
|
| 755 |
-
downloadButton("dl_geno_tmpl", "⬇ Genotype Template", class="tmpl-btn"),
|
| 756 |
-
downloadButton("dl_pheno_tmpl", "⬇ Phenotype Template", class="tmpl-btn"),
|
| 757 |
-
downloadButton("dl_map_tmpl", "⬇ Map Template", class="tmpl-btn")
|
| 758 |
),
|
| 759 |
-
|
| 760 |
-
|
| 761 |
-
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
uiOutput("trait_selector")
|
| 766 |
-
),
|
| 767 |
-
tags$div(class="control-group",
|
| 768 |
-
tags$label("LOD Threshold"),
|
| 769 |
-
tags$input(type="range", id="lodThresh", min="1", max="10", step="0.1", value="2.5",
|
| 770 |
-
oninput="Shiny.setInputValue('lod_threshold',parseFloat(this.value));document.getElementById('lodThreshVal').textContent=parseFloat(this.value).toFixed(1)")
|
| 771 |
-
),
|
| 772 |
-
tags$div(style="text-align:center;font-size:.75rem;color:var(--accent);margin-bottom:8px;",
|
| 773 |
-
"LOD = ", tags$span(id="lodThreshVal","2.5")
|
| 774 |
-
),
|
| 775 |
-
tags$button(class="run-btn", onclick="Shiny.setInputValue('run_analysis',Math.random())","▶ Run Analysis"),
|
| 776 |
-
tags$button(class="perm-btn",onclick="Shiny.setInputValue('run_perm',Math.random())","⚡ Permute (100×)")
|
| 777 |
),
|
| 778 |
-
|
| 779 |
-
|
| 780 |
-
|
| 781 |
-
|
| 782 |
-
|
|
|
|
| 783 |
),
|
| 784 |
-
|
| 785 |
-
|
| 786 |
-
|
| 787 |
-
|
| 788 |
-
|
| 789 |
-
)
|
| 790 |
),
|
| 791 |
-
|
| 792 |
-
|
| 793 |
-
|
| 794 |
-
|
| 795 |
-
tags$div(class="
|
| 796 |
-
tags$
|
| 797 |
-
|
| 798 |
-
tags$button(class="tab-btn", id="tab_multi", onclick="switchTab('multi')", "Multi-Model"),
|
| 799 |
-
tags$button(class="tab-btn", id="tab_effects",onclick="switchTab('effects')","QTL Effects"),
|
| 800 |
-
tags$button(class="tab-btn", id="tab_circos", onclick="switchTab('circos')", "Circos"),
|
| 801 |
-
tags$button(class="tab-btn", id="tab_pheno", onclick="switchTab('pheno')", "Phenotype"),
|
| 802 |
-
tags$button(class="tab-btn", id="tab_results",onclick="switchTab('results')","Results Table"),
|
| 803 |
-
tags$button(class="tab-btn", id="tab_summary",onclick="switchTab('summary')","Summary")
|
| 804 |
),
|
| 805 |
-
|
| 806 |
-
|
| 807 |
-
|
| 808 |
-
|
| 809 |
-
|
| 810 |
-
|
| 811 |
-
|
| 812 |
-
"
|
| 813 |
-
|
| 814 |
-
|
| 815 |
-
|
| 816 |
-
|
| 817 |
-
|
| 818 |
-
|
| 819 |
-
|
| 820 |
-
|
| 821 |
-
|
| 822 |
-
|
| 823 |
-
|
| 824 |
-
|
| 825 |
-
|
| 826 |
-
|
| 827 |
-
|
| 828 |
-
|
| 829 |
-
|
| 830 |
-
|
| 831 |
-
|
| 832 |
-
|
| 833 |
-
|
| 834 |
-
|
| 835 |
-
|
| 836 |
-
|
| 837 |
-
|
| 838 |
-
|
| 839 |
-
tags$
|
| 840 |
-
|
| 841 |
-
tags$div(class="format-hint",
|
| 842 |
-
tags$b(style="color:var(--accent);","Required format:"), tags$br(),
|
| 843 |
-
HTML("<code>id</code> — must match genotype file IDs (first column)"), tags$br(),
|
| 844 |
-
HTML("<code>TraitName</code> — one column per trait/environment"), tags$br(), tags$br(),
|
| 845 |
-
tags$b(style="color:var(--accent);","Notes:"), tags$br(),
|
| 846 |
-
"• Values must be numeric", tags$br(),
|
| 847 |
-
"• Multiple traits allowed — select from dropdown", tags$br(),
|
| 848 |
-
"• Missing values: leave cell empty or use NA", tags$br(), tags$br(),
|
| 849 |
-
tags$i(style="color:var(--text-dim);",
|
| 850 |
-
"Example: ENV1_PL, ENV2_PL, GrainWeight, etc.")
|
| 851 |
-
)
|
| 852 |
-
),
|
| 853 |
-
# Map format
|
| 854 |
-
tags$div(
|
| 855 |
-
tags$div(class="panel-card-title", style="margin-bottom:8px;","Genetic Map CSV Format"),
|
| 856 |
-
tags$div(class="format-hint",
|
| 857 |
-
tags$b(style="color:var(--accent);","Required columns:"), tags$br(),
|
| 858 |
-
HTML("<code>marker</code> — marker name (must match genotype file)"), tags$br(),
|
| 859 |
-
HTML("<code>chromosome</code> — chromosome number or name"), tags$br(),
|
| 860 |
-
HTML("<code>position_cM</code> — genetic position in cM"), tags$br(), tags$br(),
|
| 861 |
-
tags$b(style="color:var(--accent);","Notes:"), tags$br(),
|
| 862 |
-
"• Column headers are flexible (detected automatically)", tags$br(),
|
| 863 |
-
"• Physical positions (bp) also accepted — will be treated as map units", tags$br(),
|
| 864 |
-
"• Markers not found in map will get sequential positions"
|
| 865 |
-
)
|
| 866 |
-
)
|
| 867 |
-
),
|
| 868 |
-
tags$hr(style="border-color:var(--border);margin:16px 0;"),
|
| 869 |
-
tags$div(class="results-grid",
|
| 870 |
-
tags$div(
|
| 871 |
-
tags$div(class="panel-card-title",style="margin-bottom:10px;","Genotype Data Preview"),
|
| 872 |
-
tags$div(class="data-preview", uiOutput("geno_preview"))
|
| 873 |
-
),
|
| 874 |
-
tags$div(
|
| 875 |
-
tags$div(class="panel-card-title",style="margin-bottom:10px;","Phenotype Data Preview"),
|
| 876 |
-
tags$div(class="data-preview", uiOutput("pheno_preview"))
|
| 877 |
-
)
|
| 878 |
),
|
| 879 |
-
tags$div(
|
| 880 |
-
tags$
|
| 881 |
-
|
| 882 |
)
|
| 883 |
-
)
|
| 884 |
-
)
|
| 885 |
-
),
|
| 886 |
-
|
| 887 |
-
# ---- LOD Profile ----
|
| 888 |
-
tags$div(id="pane_lod", class="tab-pane", style="display:none",
|
| 889 |
-
tags$div(class="panel-card",
|
| 890 |
-
tags$div(class="panel-card-header",
|
| 891 |
-
tags$div(class="panel-card-title","LOD Score Profile"),
|
| 892 |
-
uiOutput("currentModelBadge")
|
| 893 |
),
|
| 894 |
-
tags$div(
|
| 895 |
-
tags$
|
|
|
|
| 896 |
)
|
| 897 |
)
|
| 898 |
-
)
|
| 899 |
-
|
| 900 |
-
|
| 901 |
-
|
| 902 |
-
|
| 903 |
-
|
| 904 |
-
|
| 905 |
-
|
| 906 |
-
|
| 907 |
-
|
| 908 |
-
|
| 909 |
-
)
|
| 910 |
-
)
|
| 911 |
-
),
|
| 912 |
-
|
| 913 |
-
# ---- Effects ----
|
| 914 |
-
tags$div(id="pane_effects", class="tab-pane", style="display:none",
|
| 915 |
-
tags$div(class="results-grid",
|
| 916 |
-
tags$div(class="panel-card",
|
| 917 |
-
tags$div(class="panel-card-header",tags$div(class="panel-card-title","Additive Effects")),
|
| 918 |
-
tags$div(class="panel-card-body",tags$div(class="plot-container",uiOutput("addEffPlotUI")))
|
| 919 |
-
),
|
| 920 |
-
tags$div(class="panel-card",
|
| 921 |
-
tags$div(class="panel-card-header",tags$div(class="panel-card-title","R² (Variance Explained)")),
|
| 922 |
-
tags$div(class="panel-card-body",tags$div(class="plot-container",uiOutput("r2PlotUI")))
|
| 923 |
-
)
|
| 924 |
-
)
|
| 925 |
-
),
|
| 926 |
-
|
| 927 |
-
# ---- Circos ----
|
| 928 |
-
tags$div(id="pane_circos", class="tab-pane", style="display:none",
|
| 929 |
-
tags$div(class="panel-card",
|
| 930 |
-
tags$div(class="panel-card-header",tags$div(class="panel-card-title","Circos — Chromosomal QTL Map")),
|
| 931 |
-
tags$div(class="panel-card-body",tags$div(class="plot-container",uiOutput("circosUI")))
|
| 932 |
-
)
|
| 933 |
-
),
|
| 934 |
-
|
| 935 |
-
# ---- Phenotype ----
|
| 936 |
-
tags$div(id="pane_pheno", class="tab-pane", style="display:none",
|
| 937 |
-
tags$div(class="results-grid",
|
| 938 |
-
tags$div(class="panel-card",
|
| 939 |
-
tags$div(class="panel-card-header",tags$div(class="panel-card-title","Phenotype Distribution")),
|
| 940 |
-
tags$div(class="panel-card-body",tags$div(class="plot-container",uiOutput("phenoDistUI")))
|
| 941 |
-
),
|
| 942 |
-
tags$div(class="panel-card",
|
| 943 |
-
tags$div(class="panel-card-header",tags$div(class="panel-card-title","Genotype × Phenotype")),
|
| 944 |
-
tags$div(class="panel-card-body",tags$div(class="plot-container",uiOutput("genoPhePlotUI")))
|
| 945 |
-
)
|
| 946 |
-
)
|
| 947 |
-
),
|
| 948 |
-
|
| 949 |
-
# ---- Results Table ----
|
| 950 |
-
tags$div(id="pane_results", class="tab-pane", style="display:none",
|
| 951 |
-
tags$div(class="panel-card",
|
| 952 |
-
tags$div(class="panel-card-header",
|
| 953 |
-
tags$div(class="panel-card-title","Full Results Table — All Models"),
|
| 954 |
-
downloadButton("dl_results","⬇ Download CSV", class="export-btn")
|
| 955 |
-
),
|
| 956 |
-
tags$div(class="panel-card-body",
|
| 957 |
-
tags$div(class="scrollable-table", uiOutput("resultsTableUI"))
|
| 958 |
-
)
|
| 959 |
)
|
| 960 |
-
)
|
| 961 |
-
|
| 962 |
-
|
| 963 |
-
|
| 964 |
-
|
| 965 |
-
|
| 966 |
-
|
|
|
|
| 967 |
),
|
| 968 |
-
tags$div(class="panel-card
|
| 969 |
-
|
| 970 |
-
tags$div(class="panel-card-body",tags$div(class="plot-container",uiOutput("ciPlotUI")))
|
| 971 |
)
|
| 972 |
)
|
| 973 |
)
|
| 974 |
)
|
| 975 |
),
|
| 976 |
-
|
| 977 |
tags$script(HTML("
|
| 978 |
-
|
| 979 |
-
|
| 980 |
-
|
| 981 |
-
|
| 982 |
-
document.getElementById('
|
| 983 |
-
|
| 984 |
-
|
| 985 |
-
|
| 986 |
-
document.querySelectorAll('.tab-pane').forEach(p=>p.style.display='none');
|
| 987 |
-
document.getElementById('tab_lod').classList.add('active');
|
| 988 |
-
document.getElementById('pane_lod').style.display='block';
|
| 989 |
});
|
| 990 |
"))
|
| 991 |
)
|
|
@@ -994,125 +758,70 @@ ui <- fluidPage(
|
|
| 994 |
# SERVER
|
| 995 |
# ============================================================
|
| 996 |
server <- function(input, output, session) {
|
| 997 |
-
|
| 998 |
-
|
| 999 |
-
|
| 1000 |
-
|
| 1001 |
-
|
| 1002 |
-
|
| 1003 |
-
|
| 1004 |
-
|
| 1005 |
-
|
| 1006 |
-
results = NULL,
|
| 1007 |
-
perm_thresh = 2.5,
|
| 1008 |
-
trait = NULL
|
| 1009 |
-
)
|
| 1010 |
-
|
| 1011 |
-
# ---- File parsing ----
|
| 1012 |
observeEvent(input$geno_file, {
|
| 1013 |
-
|
| 1014 |
-
|
| 1015 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1016 |
} else {
|
| 1017 |
-
|
| 1018 |
}
|
| 1019 |
})
|
| 1020 |
|
| 1021 |
observeEvent(input$pheno_file, {
|
| 1022 |
-
|
| 1023 |
-
|
| 1024 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1025 |
} else {
|
| 1026 |
-
|
| 1027 |
}
|
| 1028 |
})
|
| 1029 |
|
| 1030 |
observeEvent(input$map_file, {
|
| 1031 |
-
|
| 1032 |
-
|
| 1033 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1034 |
} else {
|
| 1035 |
-
|
| 1036 |
}
|
| 1037 |
})
|
| 1038 |
-
|
| 1039 |
-
# ---- File status badges ----
|
| 1040 |
-
output$geno_status <- renderUI({
|
| 1041 |
-
if (is.null(rv$geno_df)) return(NULL)
|
| 1042 |
-
n_lines <- nrow(rv$geno_df)
|
| 1043 |
-
n_markers <- ncol(rv$geno_df) - 1
|
| 1044 |
-
tags$div(class="file-status file-ok",
|
| 1045 |
-
paste0("✓ ", n_lines, " samples × ", n_markers, " markers loaded"))
|
| 1046 |
-
})
|
| 1047 |
-
|
| 1048 |
-
output$pheno_status <- renderUI({
|
| 1049 |
-
if (is.null(rv$pheno_df)) return(NULL)
|
| 1050 |
-
n_lines <- nrow(rv$pheno_df)
|
| 1051 |
-
n_traits <- ncol(rv$pheno_df) - 1
|
| 1052 |
-
tags$div(class="file-status file-ok",
|
| 1053 |
-
paste0("✓ ", n_lines, " samples × ", n_traits, " trait(s) loaded"))
|
| 1054 |
-
})
|
| 1055 |
|
| 1056 |
-
|
| 1057 |
-
if (is.null(rv$map_df)) return(NULL)
|
| 1058 |
-
tags$div(class="file-status file-ok",
|
| 1059 |
-
paste0("✓ ", nrow(rv$map_df), " markers mapped across ",
|
| 1060 |
-
length(unique(rv$map_df$chromosome)), " chromosome(s)"))
|
| 1061 |
-
})
|
| 1062 |
-
|
| 1063 |
-
# ---- Data ready badge ----
|
| 1064 |
-
output$data_ready_badge <- renderUI({
|
| 1065 |
-
g <- !is.null(rv$geno_df)
|
| 1066 |
-
p <- !is.null(rv$pheno_df)
|
| 1067 |
-
m <- !is.null(rv$map_df)
|
| 1068 |
-
|
| 1069 |
-
if (g && p && m) {
|
| 1070 |
-
tags$span(class="badge badge-sig","✓ All Files Loaded — Ready to Analyse")
|
| 1071 |
-
} else {
|
| 1072 |
-
missing <- paste(c(if(!g)"Genotype",if(!p)"Phenotype",if(!m)"Map"), collapse=", ")
|
| 1073 |
-
tags$span(class="badge badge-ns", paste0("Waiting for: ", missing))
|
| 1074 |
-
}
|
| 1075 |
-
})
|
| 1076 |
-
|
| 1077 |
-
# ---- Trait selector (dynamic from pheno file) ----
|
| 1078 |
output$trait_selector <- renderUI({
|
| 1079 |
-
|
| 1080 |
-
|
| 1081 |
-
|
| 1082 |
-
|
| 1083 |
-
|
| 1084 |
-
|
| 1085 |
}
|
| 1086 |
-
|
| 1087 |
-
tagList(
|
| 1088 |
-
selectInput("trait_select", NULL, choices = trait_cols, selected = trait_cols[1]),
|
| 1089 |
-
tags$script(HTML("
|
| 1090 |
-
document.getElementById('trait_select').addEventListener('change', function() {
|
| 1091 |
-
Shiny.setInputValue('trait', this.value);
|
| 1092 |
-
});
|
| 1093 |
-
"))
|
| 1094 |
-
)
|
| 1095 |
-
})
|
| 1096 |
-
|
| 1097 |
-
observeEvent(input$trait_select, {
|
| 1098 |
-
rv$trait <- input$trait_select
|
| 1099 |
})
|
| 1100 |
|
| 1101 |
-
#
|
| 1102 |
-
|
| 1103 |
-
|
| 1104 |
-
|
| 1105 |
-
|
| 1106 |
-
rv$trait <- trait_cols[1]
|
| 1107 |
-
}
|
| 1108 |
-
}
|
| 1109 |
-
})
|
| 1110 |
-
|
| 1111 |
-
# ---- Dataset summary ----
|
| 1112 |
-
output$dataset_summary_ui <- renderUI({
|
| 1113 |
-
g <- rv$geno_df
|
| 1114 |
-
p <- rv$pheno_df
|
| 1115 |
-
m <- rv$map_df
|
| 1116 |
|
| 1117 |
tags$div(
|
| 1118 |
tags$div(class="stat-box",
|
|
@@ -1126,345 +835,196 @@ server <- function(input, output, session) {
|
|
| 1126 |
tags$div(class="stat-box",
|
| 1127 |
tags$div(class="stat-val", if (!is.null(m)) length(unique(m$chromosome)) else "—"),
|
| 1128 |
tags$div(class="stat-lbl","Chromosomes")
|
| 1129 |
-
),
|
| 1130 |
-
tags$div(class="stat-box",
|
| 1131 |
-
tags$div(class="stat-val", if (!is.null(p)) ncol(p)-1 else "—"),
|
| 1132 |
-
tags$div(class="stat-lbl","Traits")
|
| 1133 |
)
|
| 1134 |
)
|
| 1135 |
})
|
| 1136 |
-
|
| 1137 |
-
#
|
| 1138 |
-
|
| 1139 |
-
|
| 1140 |
-
|
| 1141 |
-
d <- df[seq_len(min(nrow(df), max_rows)), seq_len(min(ncol(df), max_cols)), drop=FALSE]
|
| 1142 |
|
| 1143 |
-
|
| 1144 |
-
|
| 1145 |
-
tags$
|
| 1146 |
-
|
| 1147 |
-
|
| 1148 |
-
|
|
|
|
| 1149 |
tags$td(as.character(val))
|
| 1150 |
}))
|
| 1151 |
-
))
|
| 1152 |
)
|
| 1153 |
-
|
|
|
|
| 1154 |
|
| 1155 |
-
output$
|
| 1156 |
-
|
| 1157 |
-
|
| 1158 |
-
|
| 1159 |
-
# ---- Template downloads ----
|
| 1160 |
-
output$dl_geno_tmpl <- downloadHandler("genotype_template.csv",
|
| 1161 |
-
content=function(f) write.csv(make_geno_template(), f, row.names=FALSE))
|
| 1162 |
-
output$dl_pheno_tmpl <- downloadHandler("phenotype_template.csv",
|
| 1163 |
-
content=function(f) write.csv(make_pheno_template(), f, row.names=FALSE))
|
| 1164 |
-
output$dl_map_tmpl <- downloadHandler("map_template.csv",
|
| 1165 |
-
content=function(f) write.csv(make_map_template(), f, row.names=FALSE))
|
| 1166 |
-
|
| 1167 |
-
# ---- Run analysis ----
|
| 1168 |
-
observeEvent(input$run_analysis, {
|
| 1169 |
-
req(rv$geno_df, rv$pheno_df, rv$map_df)
|
| 1170 |
-
|
| 1171 |
-
trait <- if (!is.null(rv$trait) && rv$trait != "") rv$trait else {
|
| 1172 |
-
trait_cols <- setdiff(names(rv$pheno_df), "id")
|
| 1173 |
-
if (length(trait_cols) > 0) trait_cols[1] else NULL
|
| 1174 |
-
}
|
| 1175 |
|
| 1176 |
-
|
| 1177 |
-
|
| 1178 |
-
|
| 1179 |
-
|
| 1180 |
-
|
| 1181 |
-
|
| 1182 |
-
|
| 1183 |
-
|
| 1184 |
-
|
| 1185 |
-
|
| 1186 |
-
|
| 1187 |
-
|
| 1188 |
-
|
| 1189 |
-
|
| 1190 |
-
|
| 1191 |
-
|
| 1192 |
-
|
| 1193 |
-
})
|
| 1194 |
|
| 1195 |
-
|
| 1196 |
-
|
| 1197 |
-
|
| 1198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1199 |
})
|
| 1200 |
-
|
| 1201 |
-
#
|
| 1202 |
-
|
| 1203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1204 |
|
| 1205 |
-
|
| 1206 |
-
trait_cols <- setdiff(names(rv$pheno_df), "id")
|
| 1207 |
-
if (length(trait_cols) > 0) trait_cols[1] else NULL
|
| 1208 |
-
}
|
| 1209 |
|
| 1210 |
-
|
| 1211 |
-
showNotification("No trait selected", type="error")
|
| 1212 |
-
return(NULL)
|
| 1213 |
-
}
|
| 1214 |
|
| 1215 |
-
|
| 1216 |
-
|
| 1217 |
-
|
| 1218 |
-
|
| 1219 |
-
|
| 1220 |
-
}
|
| 1221 |
-
|
| 1222 |
-
X_imp <- impute_markers(cross$X)
|
| 1223 |
-
mk_names <- colnames(X_imp)
|
| 1224 |
-
|
| 1225 |
-
mpos <- sapply(mk_names, function(m) {
|
| 1226 |
-
idx <- which(rv$map_df$marker == m)
|
| 1227 |
-
if (length(idx) > 0) rv$map_df$position[idx[1]] else NA_real_
|
| 1228 |
-
})
|
| 1229 |
-
|
| 1230 |
-
mchr <- sapply(mk_names, function(m) {
|
| 1231 |
-
idx <- which(rv$map_df$marker == m)
|
| 1232 |
-
if (length(idx) > 0) as.character(rv$map_df$chromosome[idx[1]]) else "1"
|
| 1233 |
-
})
|
| 1234 |
-
|
| 1235 |
-
if (any(is.na(mpos))) {
|
| 1236 |
-
sp <- seq(0, 100, length.out=length(mpos))
|
| 1237 |
-
mpos[is.na(mpos)] <- sp[is.na(mpos)]
|
| 1238 |
-
}
|
| 1239 |
-
|
| 1240 |
-
incProgress(0.1, detail="Running permutations...")
|
| 1241 |
-
thr <- permutation_threshold(cross$y, X_imp, mpos, mchr, n_perm=100)
|
| 1242 |
-
rv$perm_thresh <- round(thr, 3)
|
| 1243 |
})
|
| 1244 |
|
| 1245 |
-
|
| 1246 |
-
|
| 1247 |
-
|
| 1248 |
-
|
| 1249 |
-
|
| 1250 |
-
output$dl_results <- downloadHandler(
|
| 1251 |
-
filename=function() paste0("QTL_results_", Sys.Date(), ".csv"),
|
| 1252 |
-
content=function(f) { req(rv$results); write.csv(rv$results, f, row.names=FALSE) }
|
| 1253 |
-
)
|
| 1254 |
-
|
| 1255 |
-
get_threshold <- reactive({
|
| 1256 |
-
if (!is.null(input$lod_threshold) && !is.na(input$lod_threshold))
|
| 1257 |
-
input$lod_threshold
|
| 1258 |
-
else
|
| 1259 |
-
2.5
|
| 1260 |
-
})
|
| 1261 |
-
|
| 1262 |
-
smr_res <- reactive({ req(rv$results); rv$results[rv$results$model=="Single Marker Regression",] })
|
| 1263 |
-
im_res <- reactive({ req(rv$results); rv$results[rv$results$model=="Interval Mapping (HK)",] })
|
| 1264 |
-
cim_res <- reactive({ req(rv$results); rv$results[rv$results$model=="Composite Interval Mapping",] })
|
| 1265 |
-
|
| 1266 |
-
output$currentModelBadge <- renderUI({
|
| 1267 |
-
trait <- if (!is.null(rv$trait)) rv$trait else "—"
|
| 1268 |
-
tags$span(class="env-tag env1", trait)
|
| 1269 |
-
})
|
| 1270 |
-
|
| 1271 |
-
output$qtlSummaryUI <- renderUI({
|
| 1272 |
-
if (is.null(rv$results)) return(tags$div(class="file-status file-warn","Run analysis first"))
|
| 1273 |
-
|
| 1274 |
-
thr <- get_threshold()
|
| 1275 |
-
sig <- rv$results[!is.na(rv$results$LOD) & rv$results$LOD >= thr, ]
|
| 1276 |
-
n_qtl <- nrow(unique(sig[,c("marker","chromosome")]))
|
| 1277 |
-
|
| 1278 |
-
tags$div(
|
| 1279 |
-
tags$div(class="stat-box",
|
| 1280 |
-
tags$div(class="stat-val", style="color:var(--accent3);", n_qtl),
|
| 1281 |
-
tags$div(class="stat-lbl","Sig. Marker×Model")
|
| 1282 |
-
),
|
| 1283 |
-
if (nrow(sig) > 0 && n_qtl <= 10)
|
| 1284 |
-
tags$div(style="font-size:.65rem;color:var(--text-dim);margin-top:6px;",
|
| 1285 |
-
paste(head(unique(sig$marker), 5), collapse=", "))
|
| 1286 |
-
)
|
| 1287 |
-
})
|
| 1288 |
-
|
| 1289 |
-
# ============================================================
|
| 1290 |
-
# PLOTS (simplified versions for brevity - same as original)
|
| 1291 |
-
# ============================================================
|
| 1292 |
-
|
| 1293 |
-
output$lodPlotUI <- renderUI({
|
| 1294 |
-
req(rv$results)
|
| 1295 |
-
tags$div(class="plot-container", style="min-height:400px;",
|
| 1296 |
-
tags$p("LOD Plot - Analysis complete. Please run analysis to view plots."))
|
| 1297 |
-
})
|
| 1298 |
-
|
| 1299 |
-
output$multiModelPlotUI <- renderUI({
|
| 1300 |
-
req(rv$results)
|
| 1301 |
-
tags$div(class="plot-container", style="min-height:400px;",
|
| 1302 |
-
tags$p("Multi-Model Plot - Analysis complete."))
|
| 1303 |
-
})
|
| 1304 |
-
|
| 1305 |
-
output$addEffPlotUI <- renderUI({
|
| 1306 |
-
req(rv$results)
|
| 1307 |
-
tags$div(class="plot-container", style="min-height:300px;",
|
| 1308 |
-
tags$p("Additive Effects Plot"))
|
| 1309 |
-
})
|
| 1310 |
-
|
| 1311 |
-
output$r2PlotUI <- renderUI({
|
| 1312 |
-
req(rv$results)
|
| 1313 |
-
tags$div(class="plot-container", style="min-height:300px;",
|
| 1314 |
-
tags$p("R² Plot"))
|
| 1315 |
-
})
|
| 1316 |
-
|
| 1317 |
-
output$circosUI <- renderUI({
|
| 1318 |
-
req(rv$results)
|
| 1319 |
-
tags$div(class="plot-container", style="min-height:500px;text-align:center;",
|
| 1320 |
-
tags$p("Circos Plot - QTL Map"))
|
| 1321 |
-
})
|
| 1322 |
-
|
| 1323 |
-
output$phenoDistUI <- renderUI({
|
| 1324 |
-
req(rv$pheno_df, rv$trait)
|
| 1325 |
-
tags$div(class="plot-container", style="min-height:300px;",
|
| 1326 |
-
tags$p("Phenotype Distribution"))
|
| 1327 |
-
})
|
| 1328 |
-
|
| 1329 |
-
output$genoPhePlotUI <- renderUI({
|
| 1330 |
-
req(rv$results, rv$geno_df, rv$pheno_df, rv$trait)
|
| 1331 |
-
tags$div(class="plot-container", style="min-height:300px;",
|
| 1332 |
-
tags$p("Genotype × Phenotype Plot"))
|
| 1333 |
})
|
| 1334 |
|
| 1335 |
-
|
| 1336 |
-
|
| 1337 |
-
|
| 1338 |
-
tags$p("Confidence Interval Plot"))
|
| 1339 |
-
})
|
| 1340 |
-
|
| 1341 |
-
# ---- Results Table ----
|
| 1342 |
-
output$resultsTableUI <- renderUI({
|
| 1343 |
-
req(rv$results)
|
| 1344 |
-
thr <- get_threshold()
|
| 1345 |
-
df <- rv$results[order(rv$results$model, rv$results$position), ]
|
| 1346 |
-
|
| 1347 |
-
if (nrow(df) == 0) {
|
| 1348 |
-
return(tags$p("No results available"))
|
| 1349 |
-
}
|
| 1350 |
|
| 1351 |
-
|
| 1352 |
-
|
| 1353 |
|
| 1354 |
-
rows <- lapply(
|
| 1355 |
-
|
| 1356 |
-
|
| 1357 |
-
|
| 1358 |
-
|
| 1359 |
-
|
| 1360 |
-
|
| 1361 |
-
|
| 1362 |
-
|
| 1363 |
-
tags$td(
|
| 1364 |
-
tags$td(
|
| 1365 |
-
tags$td(
|
| 1366 |
-
tags$td(lod),
|
| 1367 |
-
tags$td(pval),
|
| 1368 |
-
tags$td(r2),
|
| 1369 |
-
tags$td(add),
|
| 1370 |
-
tags$td(substr(row$model, 1, 25)),
|
| 1371 |
-
tags$td(if(sig) tags$span(class="badge badge-sig","SIG") else tags$span(class="badge badge-ns","NS"))
|
| 1372 |
)
|
| 1373 |
})
|
| 1374 |
|
| 1375 |
-
tags$div(
|
| 1376 |
tags$table(class="qtl-table",
|
| 1377 |
tags$thead(tags$tr(
|
| 1378 |
tags$th("Marker"), tags$th("Chr"), tags$th("Pos"),
|
| 1379 |
-
tags$th("LOD"), tags$th("
|
| 1380 |
tags$th("Additive"), tags$th("Model"), tags$th("Sig.")
|
| 1381 |
)),
|
| 1382 |
tags$tbody(rows)
|
| 1383 |
)
|
| 1384 |
)
|
| 1385 |
})
|
| 1386 |
-
|
| 1387 |
-
|
| 1388 |
-
|
| 1389 |
-
req(rv$results, rv$pheno_df, rv$trait)
|
| 1390 |
-
|
| 1391 |
-
thr <- get_threshold()
|
| 1392 |
-
trait <- rv$trait
|
| 1393 |
-
smr <- smr_res()
|
| 1394 |
|
| 1395 |
-
|
| 1396 |
-
|
| 1397 |
-
|
| 1398 |
-
best_pos <- if (length(best_idx) > 0 && !is.na(best_idx)) round(smr$position[best_idx], 3) else "—"
|
| 1399 |
-
best_chr <- if (length(best_idx) > 0 && !is.na(best_idx)) smr$chromosome[best_idx] else "—"
|
| 1400 |
-
best_r2 <- if (length(best_idx) > 0 && !is.na(best_idx) && !is.na(smr$R2[best_idx]))
|
| 1401 |
-
paste0(round(smr$R2[best_idx]*100, 1), "%") else "—"
|
| 1402 |
|
| 1403 |
-
|
| 1404 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1405 |
|
| 1406 |
-
|
| 1407 |
-
|
| 1408 |
-
|
| 1409 |
-
pheno_cv <- round(pheno_sd/pheno_mean*100, 2)
|
| 1410 |
-
} else {
|
| 1411 |
-
pheno_mean <- pheno_sd <- pheno_cv <- "—"
|
| 1412 |
-
}
|
| 1413 |
|
| 1414 |
-
|
| 1415 |
-
n_markers <- if (!is.null(rv$geno_df)) ncol(rv$geno_df)-1 else 0
|
| 1416 |
|
| 1417 |
-
|
|
|
|
| 1418 |
|
| 1419 |
-
tags$div(
|
| 1420 |
-
tags$
|
| 1421 |
-
"
|
| 1422 |
-
|
| 1423 |
-
tags$
|
| 1424 |
-
|
| 1425 |
-
tags$
|
| 1426 |
-
|
| 1427 |
-
tags$
|
| 1428 |
-
|
| 1429 |
-
|
| 1430 |
-
|
| 1431 |
-
tags$
|
| 1432 |
-
|
| 1433 |
-
|
| 1434 |
-
tags$tr(tags$td(style="color:var(--text-dim);padding:3px 0;","Marker:"), tags$td(style="color:var(--text);font-weight:700;", best_mk)),
|
| 1435 |
-
tags$tr(tags$td(style="color:var(--text-dim);padding:3px 0;","Chr / Pos:"), tags$td(paste0("Chr ", best_chr, " / ", best_pos, " cM"))),
|
| 1436 |
-
tags$tr(tags$td(style="color:var(--text-dim);padding:3px 0;","LOD Score:"), tags$td(style="color:var(--gold);font-weight:700;", best_lod)),
|
| 1437 |
-
tags$tr(tags$td(style="color:var(--text-dim);padding:3px 0;","Var. Expl.:"), tags$td(style="color:var(--accent3);", best_r2)),
|
| 1438 |
-
tags$tr(tags$td(style="color:var(--text-dim);padding:3px 0;","Threshold:"), tags$td(paste0("LOD = ", thr)))
|
| 1439 |
-
)
|
| 1440 |
-
),
|
| 1441 |
-
tags$div(class="panel-card", style="padding:14px;",
|
| 1442 |
-
tags$div(style="font-size:.7rem;letter-spacing:.15em;text-transform:uppercase;color:var(--accent);margin-bottom:8px;","Dataset Info"),
|
| 1443 |
-
tags$table(style="width:100%;font-size:.8rem;",
|
| 1444 |
-
tags$tr(tags$td(style="color:var(--text-dim);padding:3px 0;","Lines:"), tags$td(n_lines)),
|
| 1445 |
-
tags$tr(tags$td(style="color:var(--text-dim);padding:3px 0;","Markers:"), tags$td(n_markers)),
|
| 1446 |
-
tags$tr(tags$td(style="color:var(--text-dim);padding:3px 0;","Chromosomes:"),
|
| 1447 |
-
tags$td(if (!is.null(rv$map_df)) length(unique(rv$map_df$chromosome)) else "—")),
|
| 1448 |
-
tags$tr(tags$td(style="color:var(--text-dim);padding:3px 0;","Trait:"), tags$td(trait)),
|
| 1449 |
-
tags$tr(tags$td(style="color:var(--text-dim);padding:3px 0;","Sig. markers:"),
|
| 1450 |
-
tags$td(nrow(unique(sig[,c("marker","chromosome")]))))
|
| 1451 |
-
)
|
| 1452 |
-
)
|
| 1453 |
-
),
|
| 1454 |
-
tags$div(style="background:var(--panel);border:1px solid var(--border);border-radius:8px;padding:14px;font-family:var(--font-serif);font-style:italic;color:var(--text-dim);font-size:.95rem;line-height:1.8;",
|
| 1455 |
-
tags$strong(style="color:var(--text);font-style:normal;font-family:var(--font-mono);font-size:.75rem;letter-spacing:.1em;text-transform:uppercase;display:block;margin-bottom:8px;","Suggested Methods Text"),
|
| 1456 |
-
paste0("QTL analysis was performed for trait '", trait, "' across ",
|
| 1457 |
-
if (!is.null(rv$map_df)) length(unique(rv$map_df$chromosome)) else "multiple", " chromosome(s) using ",
|
| 1458 |
-
n_lines, " lines and ", n_markers, " markers. Single marker regression (SMR), interval mapping (IM-HK), ",
|
| 1459 |
-
"and composite interval mapping (CIM) were performed alongside 7 additional models including LASSO, ",
|
| 1460 |
-
"ridge regression, mixed linear model, Kruskal-Wallis non-parametric test, Bayesian interval mapping, ",
|
| 1461 |
-
"MQM, and two-way epistasis analysis. Phenotypic mean was ", pheno_mean, " (SD = ", pheno_sd, "). ",
|
| 1462 |
-
"The highest LOD score of ", best_lod, " was detected at marker ", best_mk,
|
| 1463 |
-
" (Chr ", best_chr, ", ", best_pos, " cM), explaining ", best_r2, " of phenotypic variance. ",
|
| 1464 |
-
"Significance threshold was set at LOD = ", thr, "; empirical genome-wide threshold estimated by 100 permutations.")
|
| 1465 |
)
|
| 1466 |
)
|
| 1467 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1468 |
}
|
| 1469 |
|
| 1470 |
-
shinyApp(ui=ui, server=server)
|
|
|
|
| 3 |
|
| 4 |
# ============================================================
|
| 5 |
# HELPER: Parse uploaded genotype CSV (handles your format)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
# ============================================================
|
| 7 |
parse_geno_csv <- function(path) {
|
| 8 |
+
if (is.null(path) || !file.exists(path)) return(NULL)
|
| 9 |
+
|
| 10 |
df <- tryCatch({
|
| 11 |
+
# Read with first column as row names temporarily to handle empty first header
|
| 12 |
+
df_raw <- read.csv(path, stringsAsFactors = FALSE, check.names = FALSE,
|
| 13 |
+
header = TRUE, fill = TRUE)
|
| 14 |
|
| 15 |
+
# Check if first column header is empty or "id"
|
| 16 |
+
first_col_name <- names(df_raw)[1]
|
| 17 |
+
if (first_col_name == "" || first_col_name == " " || grepl("^X\\.", first_col_name)) {
|
| 18 |
+
names(df_raw)[1] <- "id"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
}
|
| 20 |
+
|
| 21 |
+
# Remove any rows where id is NA or empty
|
| 22 |
+
df_raw <- df_raw[!is.na(df_raw[,1]) & df_raw[,1] != "", ]
|
| 23 |
+
|
| 24 |
+
if (nrow(df_raw) == 0) return(NULL)
|
| 25 |
+
|
| 26 |
+
# Convert all columns except first to numeric
|
| 27 |
+
for (i in 2:ncol(df_raw)) {
|
| 28 |
+
df_raw[[i]] <- suppressWarnings(as.numeric(as.character(df_raw[[i]])))
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
df_raw
|
| 32 |
}, error = function(e) {
|
| 33 |
print(paste("Error reading genotype file:", e$message))
|
| 34 |
NULL
|
| 35 |
})
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
df
|
| 38 |
}
|
| 39 |
|
| 40 |
# ============================================================
|
| 41 |
# HELPER: Parse uploaded phenotype CSV
|
|
|
|
|
|
|
|
|
|
| 42 |
# ============================================================
|
| 43 |
parse_pheno_csv <- function(path) {
|
| 44 |
+
if (is.null(path) || !file.exists(path)) return(NULL)
|
| 45 |
+
|
| 46 |
df <- tryCatch({
|
| 47 |
df_raw <- read.csv(path, stringsAsFactors = FALSE, check.names = FALSE)
|
| 48 |
+
|
| 49 |
# Ensure first column is id
|
| 50 |
+
names(df_raw)[1] <- "id"
|
| 51 |
+
|
|
|
|
| 52 |
# Convert trait columns to numeric
|
| 53 |
for (i in 2:ncol(df_raw)) {
|
| 54 |
+
df_raw[[i]] <- suppressWarnings(as.numeric(as.character(df_raw[[i]])))
|
| 55 |
}
|
| 56 |
+
|
| 57 |
+
# Remove rows with missing ids
|
| 58 |
+
df_raw <- df_raw[!is.na(df_raw$id) & df_raw$id != "", ]
|
| 59 |
+
|
| 60 |
df_raw
|
| 61 |
}, error = function(e) {
|
| 62 |
print(paste("Error reading phenotype file:", e$message))
|
| 63 |
NULL
|
| 64 |
})
|
| 65 |
+
|
| 66 |
df
|
| 67 |
}
|
| 68 |
|
| 69 |
# ============================================================
|
| 70 |
# HELPER: Parse uploaded genetic map CSV
|
|
|
|
|
|
|
| 71 |
# ============================================================
|
| 72 |
parse_map_csv <- function(path) {
|
| 73 |
+
if (is.null(path) || !file.exists(path)) return(NULL)
|
| 74 |
+
|
| 75 |
df <- tryCatch(read.csv(path, stringsAsFactors = FALSE, check.names = FALSE),
|
| 76 |
error = function(e) NULL)
|
| 77 |
if (is.null(df)) return(NULL)
|
|
|
|
| 80 |
names(df) <- tolower(trimws(names(df)))
|
| 81 |
|
| 82 |
# Try to find marker, chr, pos columns
|
| 83 |
+
mk_col <- names(df)[grepl("marker|name|snp|locus", names(df), ignore.case=TRUE)][1]
|
| 84 |
chr_col <- names(df)[grepl("chr|chrom|scaffold|linkage", names(df), ignore.case=TRUE)][1]
|
| 85 |
pos_col <- names(df)[grepl("pos|cM|cm|map|dist|bp|location", names(df), ignore.case=TRUE)][1]
|
| 86 |
|
| 87 |
if (is.na(mk_col) || is.na(pos_col)) return(NULL)
|
| 88 |
|
| 89 |
result <- data.frame(
|
| 90 |
+
marker = as.character(df[[mk_col]]),
|
| 91 |
chromosome = if (!is.na(chr_col)) as.character(df[[chr_col]]) else "1",
|
| 92 |
position = suppressWarnings(as.numeric(df[[pos_col]])),
|
| 93 |
stringsAsFactors = FALSE
|
| 94 |
)
|
| 95 |
|
| 96 |
+
result <- result[!is.na(result$position) & !is.na(result$marker) & result$marker != "", ]
|
| 97 |
result
|
| 98 |
}
|
| 99 |
|
|
|
|
| 102 |
# ============================================================
|
| 103 |
prepare_cross <- function(geno_df, pheno_df, trait) {
|
| 104 |
# Merge by id
|
| 105 |
+
merged <- merge(geno_df, pheno_df[, c("id", trait), drop=FALSE], by = "id", all = FALSE)
|
| 106 |
|
| 107 |
if (nrow(merged) == 0) {
|
| 108 |
return(NULL)
|
|
|
|
| 124 |
nas <- is.na(X[, j])
|
| 125 |
if (any(nas)) {
|
| 126 |
vals <- X[!nas, j]
|
| 127 |
+
if (length(vals) > 0) {
|
| 128 |
+
X[nas, j] <- round(mean(vals, na.rm = TRUE))
|
| 129 |
+
} else {
|
| 130 |
+
X[nas, j] <- 2
|
| 131 |
+
}
|
| 132 |
}
|
| 133 |
}
|
| 134 |
X
|
|
|
|
| 139 |
for (j in seq_len(ncol(X))) {
|
| 140 |
u <- sort(unique(na.omit(X[, j])))
|
| 141 |
if (length(u) >= 2 && diff(range(u)) > 0) {
|
| 142 |
+
mn <- min(u)
|
| 143 |
+
mx <- max(u)
|
| 144 |
X2[, j] <- (X[, j] - (mn + mx) / 2) / ((mx - mn) / 2)
|
| 145 |
} else {
|
| 146 |
+
X2[, j] <- X[, j] - 1.5
|
| 147 |
}
|
| 148 |
}
|
| 149 |
X2
|
|
|
|
| 152 |
# ---- 1. Single Marker Regression ----
|
| 153 |
run_smr <- function(y, X, mpos, mchr) {
|
| 154 |
n_m <- ncol(X)
|
| 155 |
+
results <- data.frame(
|
| 156 |
+
marker = colnames(X),
|
| 157 |
+
position = mpos[seq_len(n_m)],
|
| 158 |
+
chromosome = mchr[seq_len(n_m)],
|
| 159 |
+
LOD = NA_real_,
|
| 160 |
+
pvalue = NA_real_,
|
| 161 |
+
R2 = NA_real_,
|
| 162 |
+
additive = NA_real_,
|
| 163 |
+
model = "Single Marker Regression",
|
| 164 |
+
stringsAsFactors = FALSE
|
| 165 |
+
)
|
| 166 |
|
| 167 |
for (j in seq_len(n_m)) {
|
| 168 |
+
g <- X[, j]
|
| 169 |
+
valid <- !is.na(g)
|
| 170 |
if (sum(valid) < 3) next
|
| 171 |
|
| 172 |
gv <- as.factor(g[valid])
|
|
|
|
| 184 |
if (rss1 > 0 && rss0 > 0) {
|
| 185 |
results$LOD[j] <- max(0, (n/2) * log10(rss0/rss1))
|
| 186 |
}
|
| 187 |
+
results$pvalue[j] <- tryCatch(anova(fit0, fit1)$`Pr(>F)`[2], error = function(e) NA)
|
| 188 |
results$R2[j] <- summary(fit1)$r.squared
|
| 189 |
coefs <- coef(fit1)
|
| 190 |
results$additive[j] <- if (length(coefs) > 1) coefs[2] else 0
|
| 191 |
+
}, error = function(e) NULL)
|
| 192 |
}
|
| 193 |
results
|
| 194 |
}
|
| 195 |
|
| 196 |
+
# ---- 2. Interval Mapping ----
|
| 197 |
run_im <- function(y, X, mpos, mchr) {
|
| 198 |
Xa <- encode_additive(X)
|
| 199 |
+
results <- data.frame(
|
| 200 |
+
marker = colnames(X),
|
| 201 |
+
position = mpos[seq_len(ncol(X))],
|
| 202 |
+
chromosome = mchr[seq_len(ncol(X))],
|
| 203 |
+
LOD = NA_real_,
|
| 204 |
+
pvalue = NA_real_,
|
| 205 |
+
R2 = NA_real_,
|
| 206 |
+
additive = NA_real_,
|
| 207 |
+
model = "Interval Mapping (HK)",
|
| 208 |
+
stringsAsFactors = FALSE
|
| 209 |
+
)
|
| 210 |
|
| 211 |
for (j in seq_len(ncol(Xa))) {
|
| 212 |
+
g <- Xa[, j]
|
| 213 |
+
valid <- !is.na(g)
|
| 214 |
if (sum(valid) < 3) next
|
| 215 |
|
| 216 |
tryCatch({
|
|
|
|
| 223 |
if (rss1 > 0 && rss0 > 0) {
|
| 224 |
results$LOD[j] <- max(0, (n/2) * log10(rss0/rss1))
|
| 225 |
}
|
| 226 |
+
results$pvalue[j] <- tryCatch(summary(fit1)$coefficients[2, 4], error = function(e) NA)
|
| 227 |
results$R2[j] <- summary(fit1)$r.squared
|
| 228 |
+
results$additive[j] <- tryCatch(coef(fit1)[2], error = function(e) 0)
|
| 229 |
+
}, error = function(e) NULL)
|
| 230 |
}
|
| 231 |
results
|
| 232 |
}
|
|
|
|
| 234 |
# ---- 3. Composite Interval Mapping ----
|
| 235 |
run_cim <- function(y, X, mpos, mchr) {
|
| 236 |
Xa <- encode_additive(X)
|
| 237 |
+
results <- data.frame(
|
| 238 |
+
marker = colnames(X),
|
| 239 |
+
position = mpos[seq_len(ncol(X))],
|
| 240 |
+
chromosome = mchr[seq_len(ncol(X))],
|
| 241 |
+
LOD = NA_real_,
|
| 242 |
+
pvalue = NA_real_,
|
| 243 |
+
R2 = NA_real_,
|
| 244 |
+
additive = NA_real_,
|
| 245 |
+
model = "Composite Interval Mapping",
|
| 246 |
+
stringsAsFactors = FALSE
|
| 247 |
+
)
|
| 248 |
|
| 249 |
for (j in seq_len(ncol(Xa))) {
|
| 250 |
cofactors <- setdiff(seq_len(ncol(Xa)), j)
|
|
|
|
| 254 |
if (sum(valid) < 4) next
|
| 255 |
|
| 256 |
tryCatch({
|
| 257 |
+
cof_df <- as.data.frame(Xa[valid, cofactors, drop = FALSE])
|
| 258 |
cof_df$g_focal <- Xa[valid, j]
|
| 259 |
yv <- y[valid]
|
| 260 |
|
| 261 |
+
fit0 <- lm(yv ~ ., data = cof_df[, -ncol(cof_df), drop = FALSE])
|
| 262 |
+
fit1 <- lm(yv ~ ., data = cof_df)
|
| 263 |
n <- sum(valid)
|
| 264 |
rss0 <- sum(residuals(fit0)^2)
|
| 265 |
rss1 <- sum(residuals(fit1)^2)
|
|
|
|
| 267 |
if (rss1 > 0 && rss0 > 0) {
|
| 268 |
results$LOD[j] <- max(0, (n/2) * log10(rss0/rss1))
|
| 269 |
}
|
| 270 |
+
results$pvalue[j] <- tryCatch(anova(fit0, fit1)$`Pr(>F)`[2], error = function(e) NA)
|
| 271 |
results$R2[j] <- summary(fit1)$r.squared
|
| 272 |
coefs <- coef(fit1)
|
| 273 |
+
results$additive[j] <- tryCatch(coefs["g_focal"], error = function(e) 0)
|
| 274 |
+
}, error = function(e) NULL)
|
| 275 |
}
|
| 276 |
results
|
| 277 |
}
|
| 278 |
|
| 279 |
+
# ---- Run all models (simplified for testing) ----
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
run_all_models <- function(geno_df, pheno_df, trait, map_df) {
|
| 281 |
cross <- prepare_cross(geno_df, pheno_df, trait)
|
| 282 |
if (is.null(cross)) return(NULL)
|
|
|
|
| 302 |
mpos[is.na(mpos)] <- seq_pos[is.na(mpos)]
|
| 303 |
}
|
| 304 |
|
| 305 |
+
# Run just SMR and IM for testing (faster)
|
| 306 |
+
results_smr <- run_smr(y, X, mpos, mchr)
|
| 307 |
+
results_im <- run_im(y, X, mpos, mchr)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
|
| 309 |
+
rbind(results_smr, results_im)
|
| 310 |
}
|
| 311 |
|
| 312 |
# ============================================================
|
| 313 |
+
# TEMPLATE GENERATION
|
| 314 |
# ============================================================
|
| 315 |
make_geno_template <- function() {
|
| 316 |
data.frame(
|
|
|
|
| 346 |
# ============================================================
|
| 347 |
ui <- fluidPage(
|
| 348 |
tags$head(
|
|
|
|
|
|
|
|
|
|
| 349 |
tags$style(HTML("
|
| 350 |
:root {
|
| 351 |
--bg:#0a0e1a; --bg2:#0f1526; --bg3:#151d35; --panel:#1a2240;
|
| 352 |
--accent:#00d4ff; --accent2:#ff6b35; --accent3:#39ff14; --gold:#ffd700;
|
| 353 |
--text:#e8edf5; --text-dim:#7a8aaa; --border:rgba(0,212,255,0.2);
|
| 354 |
+
--font-mono:'Courier New', monospace;
|
| 355 |
+
}
|
| 356 |
+
body {
|
| 357 |
+
background: var(--bg);
|
| 358 |
+
color: var(--text);
|
| 359 |
+
font-family: var(--font-mono);
|
| 360 |
+
margin: 0;
|
| 361 |
+
padding: 0;
|
| 362 |
+
}
|
| 363 |
+
.header-bar {
|
| 364 |
+
background: linear-gradient(135deg, var(--bg2) 0%, var(--bg3) 100%);
|
| 365 |
+
border-bottom: 1px solid var(--border);
|
| 366 |
+
padding: 18px 32px;
|
| 367 |
+
display: flex;
|
| 368 |
+
align-items: center;
|
| 369 |
+
gap: 20px;
|
| 370 |
+
}
|
| 371 |
+
.header-title {
|
| 372 |
+
font-size: 1.3rem;
|
| 373 |
+
font-weight: 700;
|
| 374 |
+
color: var(--accent);
|
| 375 |
+
letter-spacing: .15em;
|
| 376 |
+
text-transform: uppercase;
|
| 377 |
+
}
|
| 378 |
+
.main-layout {
|
| 379 |
+
display: flex;
|
| 380 |
+
min-height: calc(100vh - 65px);
|
| 381 |
+
}
|
| 382 |
+
.sidebar {
|
| 383 |
+
width: 280px;
|
| 384 |
+
background: var(--bg2);
|
| 385 |
+
border-right: 1px solid var(--border);
|
| 386 |
+
padding: 20px 16px;
|
| 387 |
+
overflow-y: auto;
|
| 388 |
+
}
|
| 389 |
+
.sidebar-section {
|
| 390 |
+
background: var(--panel);
|
| 391 |
+
border: 1px solid var(--border);
|
| 392 |
+
border-radius: 8px;
|
| 393 |
+
padding: 14px;
|
| 394 |
+
margin-bottom: 14px;
|
| 395 |
+
}
|
| 396 |
+
.sidebar-label {
|
| 397 |
+
font-size: .65rem;
|
| 398 |
+
letter-spacing: .2em;
|
| 399 |
+
text-transform: uppercase;
|
| 400 |
+
color: var(--accent);
|
| 401 |
+
margin-bottom: 10px;
|
| 402 |
+
border-bottom: 1px solid var(--border);
|
| 403 |
+
padding-bottom: 6px;
|
| 404 |
+
}
|
| 405 |
+
.control-group {
|
| 406 |
+
margin-bottom: 12px;
|
| 407 |
+
}
|
| 408 |
+
.control-group label {
|
| 409 |
+
display: block;
|
| 410 |
+
font-size: .7rem;
|
| 411 |
+
color: var(--text-dim);
|
| 412 |
+
margin-bottom: 5px;
|
| 413 |
+
}
|
| 414 |
+
.upload-zone {
|
| 415 |
+
border: 2px dashed var(--border);
|
| 416 |
+
border-radius: 8px;
|
| 417 |
+
padding: 12px;
|
| 418 |
+
text-align: center;
|
| 419 |
+
background: var(--bg);
|
| 420 |
+
position: relative;
|
| 421 |
+
}
|
| 422 |
+
.upload-zone:hover {
|
| 423 |
+
border-color: var(--accent);
|
| 424 |
+
background: rgba(0,212,255,.04);
|
| 425 |
+
}
|
| 426 |
+
.file-status {
|
| 427 |
+
margin-top: 8px;
|
| 428 |
+
font-size: .65rem;
|
| 429 |
+
padding: 5px 8px;
|
| 430 |
+
border-radius: 4px;
|
| 431 |
+
}
|
| 432 |
+
.file-ok {
|
| 433 |
+
background: rgba(57,255,20,.15);
|
| 434 |
+
color: var(--accent3);
|
| 435 |
+
border: 1px solid rgba(57,255,20,.3);
|
| 436 |
+
}
|
| 437 |
+
.file-warn {
|
| 438 |
+
background: rgba(255,107,53,.15);
|
| 439 |
+
color: var(--accent2);
|
| 440 |
+
border: 1px solid rgba(255,107,53,.3);
|
| 441 |
+
}
|
| 442 |
+
.run-btn, .perm-btn {
|
| 443 |
+
width: 100%;
|
| 444 |
+
padding: 10px;
|
| 445 |
+
background: linear-gradient(135deg, var(--accent) 0%, #0099bb 100%);
|
| 446 |
+
color: var(--bg);
|
| 447 |
+
border: none;
|
| 448 |
+
border-radius: 6px;
|
| 449 |
+
font-weight: 700;
|
| 450 |
+
cursor: pointer;
|
| 451 |
+
margin-top: 8px;
|
| 452 |
+
}
|
| 453 |
+
.perm-btn {
|
| 454 |
+
background: linear-gradient(135deg, var(--accent2) 0%, #cc4400 100%);
|
| 455 |
+
}
|
| 456 |
+
.stat-box {
|
| 457 |
+
background: var(--bg);
|
| 458 |
+
border: 1px solid var(--border);
|
| 459 |
+
border-radius: 6px;
|
| 460 |
+
padding: 10px;
|
| 461 |
+
text-align: center;
|
| 462 |
+
margin-bottom: 8px;
|
| 463 |
+
}
|
| 464 |
+
.stat-val {
|
| 465 |
+
font-size: 1.4rem;
|
| 466 |
+
color: var(--accent);
|
| 467 |
+
font-weight: 700;
|
| 468 |
+
}
|
| 469 |
+
.stat-lbl {
|
| 470 |
+
font-size: .6rem;
|
| 471 |
+
color: var(--text-dim);
|
| 472 |
+
}
|
| 473 |
+
.content-area {
|
| 474 |
+
flex: 1;
|
| 475 |
+
padding: 20px;
|
| 476 |
+
overflow-y: auto;
|
| 477 |
+
}
|
| 478 |
+
.tab-bar {
|
| 479 |
+
display: flex;
|
| 480 |
+
gap: 4px;
|
| 481 |
+
background: var(--bg2);
|
| 482 |
+
border: 1px solid var(--border);
|
| 483 |
+
border-radius: 8px;
|
| 484 |
+
padding: 4px;
|
| 485 |
+
margin-bottom: 20px;
|
| 486 |
+
flex-wrap: wrap;
|
| 487 |
+
}
|
| 488 |
+
.tab-btn {
|
| 489 |
+
padding: 8px 14px;
|
| 490 |
+
background: transparent;
|
| 491 |
+
color: var(--text-dim);
|
| 492 |
+
border: none;
|
| 493 |
+
border-radius: 5px;
|
| 494 |
+
cursor: pointer;
|
| 495 |
+
}
|
| 496 |
+
.tab-btn.active {
|
| 497 |
+
background: var(--panel);
|
| 498 |
+
color: var(--accent);
|
| 499 |
+
border: 1px solid var(--border);
|
| 500 |
+
}
|
| 501 |
+
.panel-card {
|
| 502 |
+
background: var(--bg2);
|
| 503 |
+
border: 1px solid var(--border);
|
| 504 |
+
border-radius: 10px;
|
| 505 |
+
overflow: hidden;
|
| 506 |
+
margin-bottom: 20px;
|
| 507 |
+
}
|
| 508 |
+
.panel-card-header {
|
| 509 |
+
background: var(--panel);
|
| 510 |
+
padding: 12px 20px;
|
| 511 |
+
border-bottom: 1px solid var(--border);
|
| 512 |
+
}
|
| 513 |
+
.panel-card-title {
|
| 514 |
+
font-size: .75rem;
|
| 515 |
+
letter-spacing: .18em;
|
| 516 |
+
text-transform: uppercase;
|
| 517 |
+
color: var(--accent);
|
| 518 |
+
}
|
| 519 |
+
.panel-card-body {
|
| 520 |
+
padding: 16px;
|
| 521 |
+
}
|
| 522 |
+
.data-preview {
|
| 523 |
+
background: var(--bg);
|
| 524 |
+
border: 1px solid var(--border);
|
| 525 |
+
border-radius: 6px;
|
| 526 |
+
padding: 12px;
|
| 527 |
+
font-size: .65rem;
|
| 528 |
+
overflow-x: auto;
|
| 529 |
+
max-height: 200px;
|
| 530 |
+
overflow-y: auto;
|
| 531 |
+
}
|
| 532 |
+
.data-preview table {
|
| 533 |
+
width: 100%;
|
| 534 |
+
border-collapse: collapse;
|
| 535 |
+
}
|
| 536 |
+
.data-preview th {
|
| 537 |
+
color: var(--accent);
|
| 538 |
+
padding: 4px 8px;
|
| 539 |
+
border-bottom: 1px solid var(--border);
|
| 540 |
+
text-align: left;
|
| 541 |
+
}
|
| 542 |
+
.data-preview td {
|
| 543 |
+
padding: 3px 8px;
|
| 544 |
+
border-bottom: 1px solid rgba(255,255,255,.04);
|
| 545 |
+
}
|
| 546 |
+
.badge {
|
| 547 |
+
display: inline-block;
|
| 548 |
+
padding: 2px 7px;
|
| 549 |
+
border-radius: 3px;
|
| 550 |
+
font-size: .6rem;
|
| 551 |
+
font-weight: 700;
|
| 552 |
+
}
|
| 553 |
+
.badge-sig {
|
| 554 |
+
background: rgba(57,255,20,.15);
|
| 555 |
+
color: var(--accent3);
|
| 556 |
+
border: 1px solid rgba(57,255,20,.3);
|
| 557 |
+
}
|
| 558 |
+
.badge-ns {
|
| 559 |
+
background: rgba(255,255,255,.05);
|
| 560 |
+
color: var(--text-dim);
|
| 561 |
+
border: 1px solid rgba(255,255,255,.1);
|
| 562 |
+
}
|
| 563 |
+
.results-grid {
|
| 564 |
+
display: grid;
|
| 565 |
+
grid-template-columns: 1fr 1fr;
|
| 566 |
+
gap: 16px;
|
| 567 |
+
}
|
| 568 |
+
table.qtl-table {
|
| 569 |
+
width: 100%;
|
| 570 |
+
border-collapse: collapse;
|
| 571 |
+
font-size: .7rem;
|
| 572 |
+
}
|
| 573 |
+
table.qtl-table th {
|
| 574 |
+
background: var(--panel);
|
| 575 |
+
color: var(--accent);
|
| 576 |
+
padding: 8px 10px;
|
| 577 |
+
text-align: left;
|
| 578 |
+
}
|
| 579 |
+
table.qtl-table td {
|
| 580 |
+
padding: 7px 10px;
|
| 581 |
+
border-bottom: 1px solid rgba(255,255,255,.04);
|
| 582 |
+
}
|
| 583 |
+
.scrollable-table {
|
| 584 |
+
max-height: 400px;
|
| 585 |
+
overflow-y: auto;
|
| 586 |
+
}
|
| 587 |
+
.tmpl-btn {
|
| 588 |
+
width: 100%;
|
| 589 |
+
padding: 7px;
|
| 590 |
+
background: transparent;
|
| 591 |
+
color: var(--text-dim);
|
| 592 |
+
border: 1px solid var(--border);
|
| 593 |
+
border-radius: 4px;
|
| 594 |
+
margin-top: 4px;
|
| 595 |
+
cursor: pointer;
|
| 596 |
+
}
|
| 597 |
+
.tmpl-btn:hover {
|
| 598 |
+
color: var(--accent);
|
| 599 |
+
border-color: var(--accent);
|
| 600 |
+
}
|
| 601 |
+
.upload-sep {
|
| 602 |
+
text-align: center;
|
| 603 |
+
font-size: .6rem;
|
| 604 |
+
color: var(--text-dim);
|
| 605 |
+
margin: 10px 0;
|
| 606 |
+
}
|
| 607 |
+
pre {
|
| 608 |
+
background: var(--bg);
|
| 609 |
+
padding: 10px;
|
| 610 |
+
border-radius: 4px;
|
| 611 |
+
overflow-x: auto;
|
| 612 |
+
font-size: .7rem;
|
| 613 |
+
color: var(--text-dim);
|
| 614 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 615 |
"))
|
| 616 |
),
|
| 617 |
+
|
| 618 |
+
# Header
|
| 619 |
+
tags$div(class="header-bar",
|
| 620 |
+
tags$span(style="font-size:2rem;","🧬"),
|
| 621 |
+
tags$div(
|
| 622 |
+
tags$div(class="header-title","QTL Cartographer"),
|
| 623 |
+
tags$div(style="font-size:.8rem;color:var(--text-dim);","Quantitative Trait Loci Analysis Suite")
|
| 624 |
+
)
|
|
|
|
|
|
|
|
|
|
| 625 |
),
|
| 626 |
+
|
| 627 |
+
tags$div(class="main-layout",
|
| 628 |
+
# Sidebar
|
| 629 |
+
tags$div(class="sidebar",
|
| 630 |
+
tags$div(class="sidebar-section",
|
| 631 |
+
tags$div(class="sidebar-label","📂 Upload Data"),
|
| 632 |
+
|
| 633 |
+
tags$div(class="control-group",
|
| 634 |
+
tags$label("Genotype File (CSV)"),
|
| 635 |
+
fileInput("geno_file", NULL, accept = ".csv",
|
| 636 |
+
buttonLabel = "Browse", placeholder = "No file selected"),
|
| 637 |
+
verbatimTextOutput("geno_status", placeholder = TRUE)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 638 |
),
|
| 639 |
+
|
| 640 |
+
tags$div(class="control-group",
|
| 641 |
+
tags$label("Phenotype File (CSV)"),
|
| 642 |
+
fileInput("pheno_file", NULL, accept = ".csv",
|
| 643 |
+
buttonLabel = "Browse", placeholder = "No file selected"),
|
| 644 |
+
verbatimTextOutput("pheno_status", placeholder = TRUE)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 645 |
),
|
| 646 |
+
|
| 647 |
+
tags$div(class="control-group",
|
| 648 |
+
tags$label("Genetic Map File (CSV)"),
|
| 649 |
+
fileInput("map_file", NULL, accept = ".csv",
|
| 650 |
+
buttonLabel = "Browse", placeholder = "No file selected"),
|
| 651 |
+
verbatimTextOutput("map_status", placeholder = TRUE)
|
| 652 |
),
|
| 653 |
+
|
| 654 |
+
tags$div(class="upload-sep","— OR DOWNLOAD TEMPLATES —"),
|
| 655 |
+
downloadButton("dl_geno_tmpl", "⬇ Genotype Template", class = "tmpl-btn"),
|
| 656 |
+
downloadButton("dl_pheno_tmpl", "⬇ Phenotype Template", class = "tmpl-btn"),
|
| 657 |
+
downloadButton("dl_map_tmpl", "⬇ Map Template", class = "tmpl-btn")
|
|
|
|
| 658 |
),
|
| 659 |
+
|
| 660 |
+
tags$div(class="sidebar-section",
|
| 661 |
+
tags$div(class="sidebar-label","⚙ Analysis"),
|
| 662 |
+
|
| 663 |
+
tags$div(class="control-group",
|
| 664 |
+
tags$label("Select Trait"),
|
| 665 |
+
uiOutput("trait_selector")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 666 |
),
|
| 667 |
+
|
| 668 |
+
tags$div(class="control-group",
|
| 669 |
+
tags$label("LOD Threshold"),
|
| 670 |
+
sliderInput("lod_threshold", NULL, min = 1, max = 10, value = 2.5, step = 0.1)
|
| 671 |
+
),
|
| 672 |
+
|
| 673 |
+
actionButton("run_analysis", "▶ Run Analysis", class = "run-btn"),
|
| 674 |
+
actionButton("run_perm", "⚡ Permutation Test", class = "perm-btn")
|
| 675 |
+
),
|
| 676 |
+
|
| 677 |
+
tags$div(class="sidebar-section",
|
| 678 |
+
tags$div(class="sidebar-label","📊 Summary"),
|
| 679 |
+
uiOutput("dataset_summary"),
|
| 680 |
+
uiOutput("qtl_summary")
|
| 681 |
+
)
|
| 682 |
+
),
|
| 683 |
+
|
| 684 |
+
# Content area
|
| 685 |
+
tags$div(class="content-area",
|
| 686 |
+
tags$div(class="tab-bar",
|
| 687 |
+
tags$button(class="tab-btn active", onclick="Shiny.setInputValue('tab', 'upload')", "📂 Data"),
|
| 688 |
+
tags$button(class="tab-btn", onclick="Shiny.setInputValue('tab', 'results')", "📋 Results"),
|
| 689 |
+
tags$button(class="tab-btn", onclick="Shiny.setInputValue('tab', 'summary')", "📄 Summary")
|
| 690 |
+
),
|
| 691 |
+
|
| 692 |
+
# Upload panel
|
| 693 |
+
tags$div(id="upload_panel",
|
| 694 |
+
tags$div(class="panel-card",
|
| 695 |
+
tags$div(class="panel-card-header",
|
| 696 |
+
tags$div(class="panel-card-title","Data Preview")
|
| 697 |
+
),
|
| 698 |
+
tags$div(class="panel-card-body",
|
| 699 |
+
tags$div(class="results-grid",
|
| 700 |
+
tags$div(
|
| 701 |
+
tags$h4(style="color:var(--accent);margin-bottom:10px;","Genotype Data"),
|
| 702 |
+
uiOutput("geno_preview")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 703 |
),
|
| 704 |
+
tags$div(
|
| 705 |
+
tags$h4(style="color:var(--accent);margin-bottom:10px;","Phenotype Data"),
|
| 706 |
+
uiOutput("pheno_preview")
|
| 707 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 708 |
),
|
| 709 |
+
tags$div(style="margin-top:16px;",
|
| 710 |
+
tags$h4(style="color:var(--accent);margin-bottom:10px;","Genetic Map"),
|
| 711 |
+
uiOutput("map_preview")
|
| 712 |
)
|
| 713 |
)
|
| 714 |
+
)
|
| 715 |
+
),
|
| 716 |
+
|
| 717 |
+
# Results panel
|
| 718 |
+
tags$div(id="results_panel", style="display:none;",
|
| 719 |
+
tags$div(class="panel-card",
|
| 720 |
+
tags$div(class="panel-card-header",
|
| 721 |
+
tags$div(class="panel-card-title","QTL Analysis Results"),
|
| 722 |
+
downloadButton("dl_results", "⬇ CSV", class = "tmpl-btn", style="width:auto;")
|
| 723 |
+
),
|
| 724 |
+
tags$div(class="panel-card-body",
|
| 725 |
+
uiOutput("results_table")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 726 |
)
|
| 727 |
+
)
|
| 728 |
+
),
|
| 729 |
+
|
| 730 |
+
# Summary panel
|
| 731 |
+
tags$div(id="summary_panel", style="display:none;",
|
| 732 |
+
tags$div(class="panel-card",
|
| 733 |
+
tags$div(class="panel-card-header",
|
| 734 |
+
tags$div(class="panel-card-title","Analysis Summary")
|
| 735 |
),
|
| 736 |
+
tags$div(class="panel-card-body",
|
| 737 |
+
uiOutput("summary_text")
|
|
|
|
| 738 |
)
|
| 739 |
)
|
| 740 |
)
|
| 741 |
)
|
| 742 |
),
|
| 743 |
+
|
| 744 |
tags$script(HTML("
|
| 745 |
+
Shiny.addCustomMessageHandler('switch_tab', function(tab) {
|
| 746 |
+
document.getElementById('upload_panel').style.display = 'none';
|
| 747 |
+
document.getElementById('results_panel').style.display = 'none';
|
| 748 |
+
document.getElementById('summary_panel').style.display = 'none';
|
| 749 |
+
document.getElementById(tab + '_panel').style.display = 'block';
|
| 750 |
+
|
| 751 |
+
document.querySelectorAll('.tab-btn').forEach(btn => btn.classList.remove('active'));
|
| 752 |
+
event.target.classList.add('active');
|
|
|
|
|
|
|
|
|
|
| 753 |
});
|
| 754 |
"))
|
| 755 |
)
|
|
|
|
| 758 |
# SERVER
|
| 759 |
# ============================================================
|
| 760 |
server <- function(input, output, session) {
|
| 761 |
+
|
| 762 |
+
# Reactive values
|
| 763 |
+
geno_data <- reactiveVal(NULL)
|
| 764 |
+
pheno_data <- reactiveVal(NULL)
|
| 765 |
+
map_data <- reactiveVal(NULL)
|
| 766 |
+
results_data <- reactiveVal(NULL)
|
| 767 |
+
current_trait <- reactiveVal(NULL)
|
| 768 |
+
|
| 769 |
+
# File upload handlers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 770 |
observeEvent(input$geno_file, {
|
| 771 |
+
req(input$geno_file)
|
| 772 |
+
df <- parse_geno_csv(input$geno_file$datapath)
|
| 773 |
+
if (!is.null(df) && nrow(df) > 0) {
|
| 774 |
+
geno_data(df)
|
| 775 |
+
output$geno_status <- renderPrint({
|
| 776 |
+
cat("✓ Loaded:", nrow(df), "samples,", ncol(df)-1, "markers")
|
| 777 |
+
})
|
| 778 |
} else {
|
| 779 |
+
output$geno_status <- renderPrint({ cat("✗ Error loading genotype file") })
|
| 780 |
}
|
| 781 |
})
|
| 782 |
|
| 783 |
observeEvent(input$pheno_file, {
|
| 784 |
+
req(input$pheno_file)
|
| 785 |
+
df <- parse_pheno_csv(input$pheno_file$datapath)
|
| 786 |
+
if (!is.null(df) && nrow(df) > 0) {
|
| 787 |
+
pheno_data(df)
|
| 788 |
+
output$pheno_status <- renderPrint({
|
| 789 |
+
cat("✓ Loaded:", nrow(df), "samples,", ncol(df)-1, "traits")
|
| 790 |
+
})
|
| 791 |
} else {
|
| 792 |
+
output$pheno_status <- renderPrint({ cat("✗ Error loading phenotype file") })
|
| 793 |
}
|
| 794 |
})
|
| 795 |
|
| 796 |
observeEvent(input$map_file, {
|
| 797 |
+
req(input$map_file)
|
| 798 |
+
df <- parse_map_csv(input$map_file$datapath)
|
| 799 |
+
if (!is.null(df) && nrow(df) > 0) {
|
| 800 |
+
map_data(df)
|
| 801 |
+
output$map_status <- renderPrint({
|
| 802 |
+
cat("✓ Loaded:", nrow(df), "markers,", length(unique(df$chromosome)), "chromosomes")
|
| 803 |
+
})
|
| 804 |
} else {
|
| 805 |
+
output$map_status <- renderPrint({ cat("✗ Error loading map file") })
|
| 806 |
}
|
| 807 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 808 |
|
| 809 |
+
# Trait selector
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 810 |
output$trait_selector <- renderUI({
|
| 811 |
+
req(pheno_data())
|
| 812 |
+
traits <- setdiff(names(pheno_data()), "id")
|
| 813 |
+
if (length(traits) > 0) {
|
| 814 |
+
selectInput("trait", NULL, choices = traits, selected = traits[1])
|
| 815 |
+
} else {
|
| 816 |
+
tags$div(class="file-warn", "No traits found")
|
| 817 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 818 |
})
|
| 819 |
|
| 820 |
+
# Dataset summary
|
| 821 |
+
output$dataset_summary <- renderUI({
|
| 822 |
+
g <- geno_data()
|
| 823 |
+
p <- pheno_data()
|
| 824 |
+
m <- map_data()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 825 |
|
| 826 |
tags$div(
|
| 827 |
tags$div(class="stat-box",
|
|
|
|
| 835 |
tags$div(class="stat-box",
|
| 836 |
tags$div(class="stat-val", if (!is.null(m)) length(unique(m$chromosome)) else "—"),
|
| 837 |
tags$div(class="stat-lbl","Chromosomes")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 838 |
)
|
| 839 |
)
|
| 840 |
})
|
| 841 |
+
|
| 842 |
+
# Data previews
|
| 843 |
+
output$geno_preview <- renderUI({
|
| 844 |
+
df <- geno_data()
|
| 845 |
+
if (is.null(df)) return(tags$em("No genotype data loaded"))
|
|
|
|
| 846 |
|
| 847 |
+
preview_df <- head(df[, 1:min(6, ncol(df))], 5)
|
| 848 |
+
html_table <- tags$table(
|
| 849 |
+
tags$thead(tags$tr(lapply(names(preview_df), tags$th))),
|
| 850 |
+
tags$tbody(lapply(1:nrow(preview_df), function(i) {
|
| 851 |
+
tags$tr(lapply(1:ncol(preview_df), function(j) {
|
| 852 |
+
val <- preview_df[i, j]
|
| 853 |
+
if (is.numeric(val)) val <- round(val, 2)
|
| 854 |
tags$td(as.character(val))
|
| 855 |
}))
|
| 856 |
+
}))
|
| 857 |
)
|
| 858 |
+
html_table
|
| 859 |
+
})
|
| 860 |
|
| 861 |
+
output$pheno_preview <- renderUI({
|
| 862 |
+
df <- pheno_data()
|
| 863 |
+
if (is.null(df)) return(tags$em("No phenotype data loaded"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 864 |
|
| 865 |
+
preview_df <- head(df[, 1:min(6, ncol(df))], 5)
|
| 866 |
+
html_table <- tags$table(
|
| 867 |
+
tags$thead(tags$tr(lapply(names(preview_df), tags$th))),
|
| 868 |
+
tags$tbody(lapply(1:nrow(preview_df), function(i) {
|
| 869 |
+
tags$tr(lapply(1:ncol(preview_df), function(j) {
|
| 870 |
+
val <- preview_df[i, j]
|
| 871 |
+
if (is.numeric(val)) val <- round(val, 2)
|
| 872 |
+
tags$td(as.character(val))
|
| 873 |
+
}))
|
| 874 |
+
}))
|
| 875 |
+
)
|
| 876 |
+
html_table
|
| 877 |
+
})
|
| 878 |
+
|
| 879 |
+
output$map_preview <- renderUI({
|
| 880 |
+
df <- map_data()
|
| 881 |
+
if (is.null(df)) return(tags$em("No map data loaded"))
|
|
|
|
| 882 |
|
| 883 |
+
preview_df <- head(df, 5)
|
| 884 |
+
html_table <- tags$table(
|
| 885 |
+
tags$thead(tags$tr(lapply(names(preview_df), tags$th))),
|
| 886 |
+
tags$tbody(lapply(1:nrow(preview_df), function(i) {
|
| 887 |
+
tags$tr(lapply(1:ncol(preview_df), function(j) {
|
| 888 |
+
val <- preview_df[i, j]
|
| 889 |
+
if (is.numeric(val)) val <- round(val, 2)
|
| 890 |
+
tags$td(as.character(val))
|
| 891 |
+
}))
|
| 892 |
+
}))
|
| 893 |
+
)
|
| 894 |
+
html_table
|
| 895 |
})
|
| 896 |
+
|
| 897 |
+
# Template downloads
|
| 898 |
+
output$dl_geno_tmpl <- downloadHandler(
|
| 899 |
+
"genotype_template.csv",
|
| 900 |
+
content = function(f) write.csv(make_geno_template(), f, row.names = FALSE)
|
| 901 |
+
)
|
| 902 |
+
output$dl_pheno_tmpl <- downloadHandler(
|
| 903 |
+
"phenotype_template.csv",
|
| 904 |
+
content = function(f) write.csv(make_pheno_template(), f, row.names = FALSE)
|
| 905 |
+
)
|
| 906 |
+
output$dl_map_tmpl <- downloadHandler(
|
| 907 |
+
"map_template.csv",
|
| 908 |
+
content = function(f) write.csv(make_map_template(), f, row.names = FALSE)
|
| 909 |
+
)
|
| 910 |
+
|
| 911 |
+
# QTL Analysis
|
| 912 |
+
observeEvent(input$run_analysis, {
|
| 913 |
+
req(geno_data(), pheno_data(), map_data())
|
| 914 |
+
req(input$trait)
|
| 915 |
|
| 916 |
+
current_trait(input$trait)
|
|
|
|
|
|
|
|
|
|
| 917 |
|
| 918 |
+
showNotification("Running QTL analysis...", type = "message", duration = 3)
|
|
|
|
|
|
|
|
|
|
| 919 |
|
| 920 |
+
results <- tryCatch({
|
| 921 |
+
run_all_models(geno_data(), pheno_data(), input$trait, map_data())
|
| 922 |
+
}, error = function(e) {
|
| 923 |
+
showNotification(paste("Error:", e$message), type = "error", duration = 5)
|
| 924 |
+
NULL
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 925 |
})
|
| 926 |
|
| 927 |
+
if (!is.null(results)) {
|
| 928 |
+
results_data(results)
|
| 929 |
+
showNotification("Analysis complete!", type = "message", duration = 3)
|
| 930 |
+
session$sendCustomMessage("switch_tab", "results")
|
| 931 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 932 |
})
|
| 933 |
|
| 934 |
+
# Results table
|
| 935 |
+
output$results_table <- renderUI({
|
| 936 |
+
req(results_data())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 937 |
|
| 938 |
+
df <- results_data()
|
| 939 |
+
thr <- input$lod_threshold
|
| 940 |
|
| 941 |
+
rows <- lapply(1:nrow(df), function(i) {
|
| 942 |
+
sig <- !is.na(df$LOD[i]) && df$LOD[i] >= thr
|
| 943 |
+
tags$tr(
|
| 944 |
+
tags$td(substr(df$marker[i], 1, 20)),
|
| 945 |
+
tags$td(df$chromosome[i]),
|
| 946 |
+
tags$td(round(df$position[i], 2)),
|
| 947 |
+
tags$td(round(df$LOD[i], 3)),
|
| 948 |
+
tags$td(if (!is.na(df$pvalue[i])) formatC(df$pvalue[i], format = "e", digits = 2) else "—"),
|
| 949 |
+
tags$td(if (!is.na(df$R2[i])) paste0(round(df$R2[i] * 100, 1), "%") else "—"),
|
| 950 |
+
tags$td(if (!is.na(df$additive[i])) round(df$additive[i], 4) else "—"),
|
| 951 |
+
tags$td(df$model[i]),
|
| 952 |
+
tags$td(if (sig) tags$span(class="badge badge-sig","SIG") else tags$span(class="badge badge-ns","NS"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 953 |
)
|
| 954 |
})
|
| 955 |
|
| 956 |
+
tags$div(class="scrollable-table",
|
| 957 |
tags$table(class="qtl-table",
|
| 958 |
tags$thead(tags$tr(
|
| 959 |
tags$th("Marker"), tags$th("Chr"), tags$th("Pos"),
|
| 960 |
+
tags$th("LOD"), tags$th("P-value"), tags$th("R²"),
|
| 961 |
tags$th("Additive"), tags$th("Model"), tags$th("Sig.")
|
| 962 |
)),
|
| 963 |
tags$tbody(rows)
|
| 964 |
)
|
| 965 |
)
|
| 966 |
})
|
| 967 |
+
|
| 968 |
+
output$qtl_summary <- renderUI({
|
| 969 |
+
req(results_data())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 970 |
|
| 971 |
+
thr <- input$lod_threshold
|
| 972 |
+
sig <- results_data()[!is.na(results_data()$LOD) & results_data()$LOD >= thr, ]
|
| 973 |
+
n_sig <- nrow(unique(sig[, c("marker", "chromosome")]))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 974 |
|
| 975 |
+
tags$div(
|
| 976 |
+
tags$div(class="stat-box",
|
| 977 |
+
tags$div(class="stat-val", style="color:var(--accent3);", n_sig),
|
| 978 |
+
tags$div(class="stat-lbl","Significant QTL")
|
| 979 |
+
)
|
| 980 |
+
)
|
| 981 |
+
})
|
| 982 |
+
|
| 983 |
+
# Summary text
|
| 984 |
+
output$summary_text <- renderUI({
|
| 985 |
+
req(results_data(), pheno_data(), current_trait())
|
| 986 |
|
| 987 |
+
df <- results_data()
|
| 988 |
+
thr <- input$lod_threshold
|
| 989 |
+
trait <- current_trait()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 990 |
|
| 991 |
+
best <- df[which.max(df$LOD), ]
|
|
|
|
| 992 |
|
| 993 |
+
pheno_vals <- as.numeric(pheno_data()[[trait]])
|
| 994 |
+
pheno_vals <- pheno_vals[!is.na(pheno_vals)]
|
| 995 |
|
| 996 |
+
tags$div(
|
| 997 |
+
tags$div(class="stat-box", style="text-align:left;",
|
| 998 |
+
tags$p(tags$strong("Best QTL: "), best$marker),
|
| 999 |
+
tags$p(tags$strong("Position: "), best$position, " cM (Chr ", best$chromosome, ")"),
|
| 1000 |
+
tags$p(tags$strong("LOD Score: "), round(best$LOD, 3)),
|
| 1001 |
+
tags$p(tags$strong("R²: "), if (!is.na(best$R2)) paste0(round(best$R2 * 100, 1), "%") else "—"),
|
| 1002 |
+
tags$p(tags$strong("Additive Effect: "), round(best$additive, 4)),
|
| 1003 |
+
tags$hr(),
|
| 1004 |
+
tags$p(tags$strong("Phenotype Summary (", trait, "):")),
|
| 1005 |
+
tags$p("Mean: ", round(mean(pheno_vals), 3)),
|
| 1006 |
+
tags$p("SD: ", round(sd(pheno_vals), 3)),
|
| 1007 |
+
tags$p("Range: ", round(min(pheno_vals), 3), " - ", round(max(pheno_vals), 3)),
|
| 1008 |
+
tags$hr(),
|
| 1009 |
+
tags$p(tags$strong("Significance Threshold: "), "LOD = ", thr),
|
| 1010 |
+
tags$p(tags$strong("Models Run: "), paste(unique(df$model), collapse = ", "))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1011 |
)
|
| 1012 |
)
|
| 1013 |
})
|
| 1014 |
+
|
| 1015 |
+
# Results download
|
| 1016 |
+
output$dl_results <- downloadHandler(
|
| 1017 |
+
filename = function() paste0("qtl_results_", Sys.Date(), ".csv"),
|
| 1018 |
+
content = function(f) {
|
| 1019 |
+
req(results_data())
|
| 1020 |
+
write.csv(results_data(), f, row.names = FALSE)
|
| 1021 |
+
}
|
| 1022 |
+
)
|
| 1023 |
+
|
| 1024 |
+
# Tab switching
|
| 1025 |
+
observeEvent(input$tab, {
|
| 1026 |
+
session$sendCustomMessage("switch_tab", input$tab)
|
| 1027 |
+
})
|
| 1028 |
}
|
| 1029 |
|
| 1030 |
+
shinyApp(ui = ui, server = server)
|