Spaces:
Sleeping
Sleeping
Update app.R
Browse files
app.R
CHANGED
|
@@ -1374,20 +1374,29 @@ server <- function(input, output, session) {
|
|
| 1374 |
# ============================================================
|
| 1375 |
# DATA LOADING
|
| 1376 |
# ============================================================
|
|
|
|
|
|
|
|
|
|
| 1377 |
observeEvent(input$genotype_file, {
|
| 1378 |
req(input$genotype_file)
|
| 1379 |
tryCatch({
|
| 1380 |
all_data <- readLines(input$genotype_file$datapath)
|
|
|
|
|
|
|
| 1381 |
if (length(all_data) < 4) { showNotification("Need β₯4 rows", type = "error"); return() }
|
| 1382 |
-
header <- strsplit(all_data[1], ",")[[1]]
|
| 1383 |
-
chromosome_row <- strsplit(all_data[2], ",")[[1]]
|
| 1384 |
-
position_row <- strsplit(all_data[3], ",")[[1]]
|
| 1385 |
-
genotype_data <- read.csv(text = paste(all_data[-(1:3)], collapse = "\n"),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1386 |
colnames(genotype_data) <- header
|
| 1387 |
-
rownames(genotype_data) <- genotype_data[, 1]
|
| 1388 |
-
genotype_data <- genotype_data[, -1]
|
| 1389 |
for (col in colnames(genotype_data))
|
| 1390 |
-
genotype_data[[col]] <- as.numeric(as.character(genotype_data[[col]]))
|
| 1391 |
values$genotype_data <- genotype_data
|
| 1392 |
values$chromosome_info <- chromosome_row[-1]
|
| 1393 |
values$position_info <- suppressWarnings(as.numeric(position_row[-1]))
|
|
@@ -1396,6 +1405,8 @@ server <- function(input, output, session) {
|
|
| 1396 |
Chromosome = chromosome_row[-1],
|
| 1397 |
Position = suppressWarnings(as.numeric(position_row[-1])),
|
| 1398 |
stringsAsFactors = FALSE)
|
|
|
|
|
|
|
| 1399 |
showNotification(paste("Loaded", ncol(genotype_data), "markers,", nrow(genotype_data), "samples"), type = "message")
|
| 1400 |
add_message("assistant", paste0("β
Genotype: **", ncol(genotype_data), " markers**, **", nrow(genotype_data), " samples**"))
|
| 1401 |
}, error = function(e) showNotification(paste("Geno error:", e$message), type = "error"))
|
|
@@ -1427,52 +1438,31 @@ server <- function(input, output, session) {
|
|
| 1427 |
observeEvent(input$test_genotype_file, {
|
| 1428 |
req(input$test_genotype_file)
|
| 1429 |
tryCatch({
|
| 1430 |
-
# Read raw lines, strip Windows carriage returns
|
| 1431 |
all_data <- readLines(input$test_genotype_file$datapath)
|
| 1432 |
all_data <- gsub("\r", "", all_data)
|
| 1433 |
-
all_data <- all_data[nchar(trimws(all_data)) > 0]
|
| 1434 |
-
|
| 1435 |
if (length(all_data) < 4) {
|
| 1436 |
-
showNotification("Test genotype needs β₯4 rows (header + chr
|
| 1437 |
-
return()
|
| 1438 |
}
|
| 1439 |
-
|
| 1440 |
-
|
| 1441 |
-
|
| 1442 |
-
|
| 1443 |
-
# Rows 4+ are sample data
|
| 1444 |
-
sample_lines <- all_data[-(1:3)]
|
| 1445 |
-
gd <- read.csv(text = paste(sample_lines, collapse = "\n"),
|
| 1446 |
-
header = FALSE, stringsAsFactors = FALSE,
|
| 1447 |
-
strip.white = TRUE, quote = '"')
|
| 1448 |
-
|
| 1449 |
if (ncol(gd) != length(header)) {
|
| 1450 |
-
showNotification(
|
| 1451 |
-
paste0("Column count mismatch: header has ", length(header),
|
| 1452 |
-
" cols but data has ", ncol(gd), " cols. Check CSV format."),
|
| 1453 |
-
type = "error", duration = 10)
|
| 1454 |
-
return()
|
| 1455 |
}
|
| 1456 |
-
|
| 1457 |
colnames(gd) <- header
|
| 1458 |
-
rownames(gd) <-
|
| 1459 |
gd <- gd[, -1, drop = FALSE]
|
| 1460 |
-
|
| 1461 |
-
# Convert all to numeric
|
| 1462 |
for (col in colnames(gd))
|
| 1463 |
gd[[col]] <- suppressWarnings(as.numeric(as.character(gd[[col]])))
|
| 1464 |
-
|
| 1465 |
values$test_genotype_data <- gd
|
| 1466 |
-
|
| 1467 |
cat("TEST GENO LOADED:", nrow(gd), "samples,", ncol(gd), "markers\n")
|
| 1468 |
-
cat("TEST
|
| 1469 |
-
|
| 1470 |
-
showNotification(
|
| 1471 |
-
paste0("β
Test genotype loaded: ", nrow(gd), " samples, ", ncol(gd), " markers"),
|
| 1472 |
-
type = "message")
|
| 1473 |
}, error = function(e) {
|
| 1474 |
-
showNotification(paste("Test geno
|
| 1475 |
-
cat("TEST GENO
|
| 1476 |
})
|
| 1477 |
})
|
| 1478 |
|
|
@@ -1621,153 +1611,159 @@ server <- function(input, output, session) {
|
|
| 1621 |
# PREDICT TEST PHENOTYPES
|
| 1622 |
# ============================================================
|
| 1623 |
observeEvent(input$predict_test_phenotype, {
|
| 1624 |
-
req(values$model_fit, values$test_genotype_data)
|
| 1625 |
|
| 1626 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1627 |
|
| 1628 |
-
|
| 1629 |
-
incProgress(0.05, detail = "Validating inputs...")
|
| 1630 |
-
Sys.sleep(0.08)
|
| 1631 |
|
| 1632 |
-
|
| 1633 |
-
|
|
|
|
| 1634 |
|
| 1635 |
-
|
| 1636 |
-
|
| 1637 |
-
|
| 1638 |
-
|
| 1639 |
-
|
| 1640 |
-
|
| 1641 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1642 |
}
|
| 1643 |
|
| 1644 |
-
|
| 1645 |
-
|
| 1646 |
-
|
| 1647 |
-
|
| 1648 |
|
| 1649 |
-
|
| 1650 |
-
|
| 1651 |
-
|
| 1652 |
-
|
|
|
|
|
|
|
| 1653 |
|
| 1654 |
-
|
| 1655 |
-
incProgress(0.10, detail = paste0("Matched ", length(common_m), " / ", length(train_cols), " markers..."))
|
| 1656 |
-
Sys.sleep(0.08)
|
| 1657 |
|
| 1658 |
if (length(common_m) == 0) {
|
| 1659 |
showNotification(
|
| 1660 |
-
paste0("No markers matched
|
| 1661 |
-
"
|
| 1662 |
-
type = "error", duration =
|
| 1663 |
add_message("assistant", paste0(
|
| 1664 |
-
"β **Prediction failed β
|
| 1665 |
-
"
|
| 1666 |
-
"
|
| 1667 |
-
"
|
| 1668 |
return()
|
| 1669 |
}
|
| 1670 |
|
| 1671 |
-
if (length(missing_m) > 0)
|
| 1672 |
-
showNotification(
|
| 1673 |
-
|
| 1674 |
-
type = "warning", duration = 6)
|
| 1675 |
-
}
|
| 1676 |
-
|
| 1677 |
-
# ββ Step 3: Build aligned data.frame (caret requires data.frame) ββ
|
| 1678 |
-
incProgress(0.20, detail = paste0("Building aligned matrix (", nrow(test_raw), " samples)..."))
|
| 1679 |
-
Sys.sleep(0.08)
|
| 1680 |
|
| 1681 |
-
#
|
| 1682 |
-
|
|
|
|
| 1683 |
|
| 1684 |
-
#
|
| 1685 |
-
|
| 1686 |
-
|
| 1687 |
-
matrix(0L, nrow = nrow(X_test_df), ncol = length(missing_m),
|
| 1688 |
-
dimnames = list(rownames(X_test_df), missing_m))
|
| 1689 |
-
)
|
| 1690 |
-
X_test_df <- cbind(X_test_df, fill_df)
|
| 1691 |
-
}
|
| 1692 |
|
| 1693 |
-
#
|
| 1694 |
-
|
|
|
|
|
|
|
|
|
|
| 1695 |
|
| 1696 |
-
#
|
| 1697 |
-
for (col in
|
| 1698 |
-
|
| 1699 |
-
|
| 1700 |
-
|
| 1701 |
-
X_test_df[[col]][na_idx] <- if (is.finite(col_mean)) col_mean else 0
|
| 1702 |
-
}
|
| 1703 |
}
|
| 1704 |
|
| 1705 |
-
|
| 1706 |
-
|
| 1707 |
-
|
| 1708 |
|
| 1709 |
-
|
| 1710 |
-
|
| 1711 |
-
|
| 1712 |
-
# ββ Step 4: Run prediction βββββββββββββββββββββββββββββββββββ
|
| 1713 |
-
incProgress(0.35, detail = paste0("Running RF model on ", nrow(X_test_df), " samples..."))
|
| 1714 |
Sys.sleep(0.05)
|
| 1715 |
|
| 1716 |
preds <- tryCatch({
|
| 1717 |
-
predict(
|
| 1718 |
-
|
| 1719 |
-
|
| 1720 |
-
|
| 1721 |
tryCatch({
|
| 1722 |
-
|
|
|
|
| 1723 |
}, error = function(e2) {
|
| 1724 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1725 |
})
|
| 1726 |
})
|
| 1727 |
|
| 1728 |
-
# ββ Step 5:
|
| 1729 |
-
incProgress(0.
|
| 1730 |
Sys.sleep(0.05)
|
| 1731 |
|
| 1732 |
-
|
| 1733 |
-
|
| 1734 |
-
|
| 1735 |
-
}
|
| 1736 |
-
|
| 1737 |
-
values$predicted_test_phenotype_data <- data.frame(
|
| 1738 |
-
id = sample_ids,
|
| 1739 |
-
Predicted_Phenotype = round(as.numeric(preds), 4),
|
| 1740 |
stringsAsFactors = FALSE
|
| 1741 |
)
|
| 1742 |
|
| 1743 |
-
|
| 1744 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1745 |
|
| 1746 |
}) # end withProgress
|
| 1747 |
|
| 1748 |
-
# ββ
|
| 1749 |
-
|
| 1750 |
-
|
| 1751 |
-
|
| 1752 |
-
missing_m <- setdiff(train_cols, colnames(values$test_genotype_data))
|
| 1753 |
|
| 1754 |
-
showNotification(
|
| 1755 |
-
|
| 1756 |
-
type = "message", duration = 5
|
| 1757 |
-
)
|
| 1758 |
|
| 1759 |
add_message("assistant", paste0(
|
| 1760 |
-
"β
**Test phenotypes predicted
|
| 1761 |
-
"β’ **",
|
| 1762 |
-
"β’
|
| 1763 |
-
|
| 1764 |
-
"
|
| 1765 |
-
"
|
| 1766 |
-
"\nβ’ Preview: Data Upload β 'Predicted Test Phenotype' tab\n",
|
| 1767 |
"β’ Save: click **Download Predicted Phenotypes (.xlsx)**"
|
| 1768 |
))
|
| 1769 |
})
|
| 1770 |
|
|
|
|
| 1771 |
# ============================================================
|
| 1772 |
# DOMESTICATION
|
| 1773 |
# ============================================================
|
|
@@ -2207,4 +2203,4 @@ server <- function(input, output, session) {
|
|
| 2207 |
})
|
| 2208 |
}
|
| 2209 |
|
| 2210 |
-
shinyApp(ui = ui, server = server)
|
|
|
|
| 1374 |
# ============================================================
|
| 1375 |
# DATA LOADING
|
| 1376 |
# ============================================================
|
| 1377 |
+
# Shared name-cleaner: strip \r, quotes, leading/trailing spaces
|
| 1378 |
+
cn <- function(x) trimws(gsub('[\r\n\"]', '', as.character(x)))
|
| 1379 |
+
|
| 1380 |
observeEvent(input$genotype_file, {
|
| 1381 |
req(input$genotype_file)
|
| 1382 |
tryCatch({
|
| 1383 |
all_data <- readLines(input$genotype_file$datapath)
|
| 1384 |
+
all_data <- gsub("\r", "", all_data)
|
| 1385 |
+
all_data <- all_data[nchar(trimws(all_data)) > 0]
|
| 1386 |
if (length(all_data) < 4) { showNotification("Need β₯4 rows", type = "error"); return() }
|
| 1387 |
+
header <- cn(strsplit(all_data[1], ",")[[1]])
|
| 1388 |
+
chromosome_row <- cn(strsplit(all_data[2], ",")[[1]])
|
| 1389 |
+
position_row <- cn(strsplit(all_data[3], ",")[[1]])
|
| 1390 |
+
genotype_data <- read.csv(text = paste(all_data[-(1:3)], collapse = "\n"),
|
| 1391 |
+
header = FALSE, stringsAsFactors = FALSE, quote = '"')
|
| 1392 |
+
if (ncol(genotype_data) != length(header)) {
|
| 1393 |
+
showNotification(paste0("Header has ", length(header), " cols but data has ", ncol(genotype_data)), type = "error"); return()
|
| 1394 |
+
}
|
| 1395 |
colnames(genotype_data) <- header
|
| 1396 |
+
rownames(genotype_data) <- cn(as.character(genotype_data[, 1]))
|
| 1397 |
+
genotype_data <- genotype_data[, -1, drop = FALSE]
|
| 1398 |
for (col in colnames(genotype_data))
|
| 1399 |
+
genotype_data[[col]] <- suppressWarnings(as.numeric(as.character(genotype_data[[col]])))
|
| 1400 |
values$genotype_data <- genotype_data
|
| 1401 |
values$chromosome_info <- chromosome_row[-1]
|
| 1402 |
values$position_info <- suppressWarnings(as.numeric(position_row[-1]))
|
|
|
|
| 1405 |
Chromosome = chromosome_row[-1],
|
| 1406 |
Position = suppressWarnings(as.numeric(position_row[-1])),
|
| 1407 |
stringsAsFactors = FALSE)
|
| 1408 |
+
cat("TRAIN GENO LOADED:", nrow(genotype_data), "samples,", ncol(genotype_data), "markers\n")
|
| 1409 |
+
cat("TRAIN first 3 cols:", paste(colnames(genotype_data)[1:3], collapse="|"), "\n")
|
| 1410 |
showNotification(paste("Loaded", ncol(genotype_data), "markers,", nrow(genotype_data), "samples"), type = "message")
|
| 1411 |
add_message("assistant", paste0("β
Genotype: **", ncol(genotype_data), " markers**, **", nrow(genotype_data), " samples**"))
|
| 1412 |
}, error = function(e) showNotification(paste("Geno error:", e$message), type = "error"))
|
|
|
|
| 1438 |
observeEvent(input$test_genotype_file, {
|
| 1439 |
req(input$test_genotype_file)
|
| 1440 |
tryCatch({
|
|
|
|
| 1441 |
all_data <- readLines(input$test_genotype_file$datapath)
|
| 1442 |
all_data <- gsub("\r", "", all_data)
|
| 1443 |
+
all_data <- all_data[nchar(trimws(all_data)) > 0]
|
|
|
|
| 1444 |
if (length(all_data) < 4) {
|
| 1445 |
+
showNotification("Test genotype needs β₯4 rows (header + chr + pos + β₯1 sample)", type = "error"); return()
|
|
|
|
| 1446 |
}
|
| 1447 |
+
# Use SAME cn() cleaner as training loader
|
| 1448 |
+
header <- cn(strsplit(all_data[1], ",")[[1]])
|
| 1449 |
+
gd <- read.csv(text = paste(all_data[-(1:3)], collapse = "\n"),
|
| 1450 |
+
header = FALSE, stringsAsFactors = FALSE, quote = '"')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1451 |
if (ncol(gd) != length(header)) {
|
| 1452 |
+
showNotification(paste0("Header has ", length(header), " cols but data has ", ncol(gd)), type = "error"); return()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1453 |
}
|
|
|
|
| 1454 |
colnames(gd) <- header
|
| 1455 |
+
rownames(gd) <- cn(as.character(gd[, 1]))
|
| 1456 |
gd <- gd[, -1, drop = FALSE]
|
|
|
|
|
|
|
| 1457 |
for (col in colnames(gd))
|
| 1458 |
gd[[col]] <- suppressWarnings(as.numeric(as.character(gd[[col]])))
|
|
|
|
| 1459 |
values$test_genotype_data <- gd
|
|
|
|
| 1460 |
cat("TEST GENO LOADED:", nrow(gd), "samples,", ncol(gd), "markers\n")
|
| 1461 |
+
cat("TEST first 3 cols:", paste(colnames(gd)[1:3], collapse="|"), "\n")
|
| 1462 |
+
showNotification(paste0("β
Test genotype: ", nrow(gd), " samples, ", ncol(gd), " markers"), type = "message")
|
|
|
|
|
|
|
|
|
|
| 1463 |
}, error = function(e) {
|
| 1464 |
+
showNotification(paste("Test geno error:", e$message), type = "error", duration = 10)
|
| 1465 |
+
cat("TEST GENO ERROR:", e$message, "\n")
|
| 1466 |
})
|
| 1467 |
})
|
| 1468 |
|
|
|
|
| 1611 |
# PREDICT TEST PHENOTYPES
|
| 1612 |
# ============================================================
|
| 1613 |
observeEvent(input$predict_test_phenotype, {
|
|
|
|
| 1614 |
|
| 1615 |
+
# ββ Guard: need model + test data βββββββββββββββββββββββββββ
|
| 1616 |
+
if (is.null(values$model_fit)) {
|
| 1617 |
+
showNotification("β Run CIM Analysis first to train the model.", type = "error", duration = 8)
|
| 1618 |
+
return()
|
| 1619 |
+
}
|
| 1620 |
+
if (is.null(values$test_genotype_data)) {
|
| 1621 |
+
showNotification("β Upload a Test Genotype CSV first.", type = "error", duration = 8)
|
| 1622 |
+
return()
|
| 1623 |
+
}
|
| 1624 |
|
| 1625 |
+
withProgress(message = "Predicting test phenotypes...", value = 0, {
|
|
|
|
|
|
|
| 1626 |
|
| 1627 |
+
# ββ Step 1: Setup ββββββββββββββββββββββββββββββββββββββββ
|
| 1628 |
+
incProgress(0.05, detail = "Loading model and test data...")
|
| 1629 |
+
Sys.sleep(0.05)
|
| 1630 |
|
| 1631 |
+
model <- values$model_fit
|
| 1632 |
+
test_raw <- values$test_genotype_data # data.frame: rows=samples, cols=markers
|
| 1633 |
+
|
| 1634 |
+
# Get the exact predictor column names the model was trained on
|
| 1635 |
+
train_cols <- values$model_train_cols
|
| 1636 |
+
if (is.null(train_cols)) {
|
| 1637 |
+
# Fallback: extract from caret model object
|
| 1638 |
+
train_cols <- tryCatch(model$finalModel$xNames, error = function(e) NULL)
|
| 1639 |
+
if (is.null(train_cols))
|
| 1640 |
+
train_cols <- tryCatch(colnames(model$trainingData)[-ncol(model$trainingData)], error = function(e) NULL)
|
| 1641 |
+
if (is.null(train_cols))
|
| 1642 |
+
train_cols <- colnames(values$genotype_data)
|
| 1643 |
}
|
| 1644 |
|
| 1645 |
+
cat("PREDICT: train_cols =", length(train_cols),
|
| 1646 |
+
"| test cols =", ncol(test_raw), "\n")
|
| 1647 |
+
cat("PREDICT: train[1:3] =", paste(train_cols[1:3], collapse="|"), "\n")
|
| 1648 |
+
cat("PREDICT: test[1:3] =", paste(colnames(test_raw)[1:3], collapse="|"), "\n")
|
| 1649 |
|
| 1650 |
+
# ββ Step 2: Marker overlap βββββββββββββββββββββββββββββββ
|
| 1651 |
+
incProgress(0.10, detail = "Matching markers...")
|
| 1652 |
+
Sys.sleep(0.05)
|
| 1653 |
+
|
| 1654 |
+
common_m <- intersect(train_cols, colnames(test_raw))
|
| 1655 |
+
missing_m <- setdiff(train_cols, colnames(test_raw))
|
| 1656 |
|
| 1657 |
+
cat("PREDICT: common =", length(common_m), "| missing =", length(missing_m), "\n")
|
|
|
|
|
|
|
| 1658 |
|
| 1659 |
if (length(common_m) == 0) {
|
| 1660 |
showNotification(
|
| 1661 |
+
paste0("No markers matched! Train has ", length(train_cols),
|
| 1662 |
+
" markers, test has ", ncol(test_raw), ". Check marker names match exactly."),
|
| 1663 |
+
type = "error", duration = 15)
|
| 1664 |
add_message("assistant", paste0(
|
| 1665 |
+
"β **Prediction failed β zero marker overlap**\n\n",
|
| 1666 |
+
"Train markers (first 5): ", paste(head(train_cols, 5), collapse=", "), "\n",
|
| 1667 |
+
"Test markers (first 5): ", paste(head(colnames(test_raw), 5), collapse=", "), "\n\n",
|
| 1668 |
+
"Both CSVs must have identical marker column names."))
|
| 1669 |
return()
|
| 1670 |
}
|
| 1671 |
|
| 1672 |
+
if (length(missing_m) > 0)
|
| 1673 |
+
showNotification(paste0(length(missing_m), " markers missing from test β imputing with 0"),
|
| 1674 |
+
type = "warning", duration = 5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1675 |
|
| 1676 |
+
# ββ Step 3: Build aligned data.frame ββββββββββββββββββββ
|
| 1677 |
+
incProgress(0.25, detail = paste0("Aligning ", nrow(test_raw), " samples Γ ", length(train_cols), " markers..."))
|
| 1678 |
+
Sys.sleep(0.05)
|
| 1679 |
|
| 1680 |
+
# Build complete data.frame with ALL training columns in training order
|
| 1681 |
+
n_test <- nrow(test_raw)
|
| 1682 |
+
ids <- rownames(test_raw)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1683 |
|
| 1684 |
+
# Pre-allocate with zeros
|
| 1685 |
+
X_pred <- as.data.frame(
|
| 1686 |
+
matrix(0, nrow = n_test, ncol = length(train_cols),
|
| 1687 |
+
dimnames = list(ids, train_cols))
|
| 1688 |
+
)
|
| 1689 |
|
| 1690 |
+
# Fill in the columns we have
|
| 1691 |
+
for (col in common_m) {
|
| 1692 |
+
vals <- suppressWarnings(as.numeric(as.character(test_raw[[col]])))
|
| 1693 |
+
vals[is.na(vals)] <- 0
|
| 1694 |
+
X_pred[[col]] <- vals
|
|
|
|
|
|
|
| 1695 |
}
|
| 1696 |
|
| 1697 |
+
cat("PREDICT: X_pred dims =", nrow(X_pred), "x", ncol(X_pred), "\n")
|
| 1698 |
+
cat("PREDICT: col order match =", identical(colnames(X_pred), train_cols), "\n")
|
| 1699 |
+
cat("PREDICT: any NA =", any(is.na(X_pred)), "\n")
|
| 1700 |
|
| 1701 |
+
# ββ Step 4: Predict ββββββββββββββββββββββββββββββββββββββ
|
| 1702 |
+
incProgress(0.40, detail = paste0("Running model on ", n_test, " samples..."))
|
|
|
|
|
|
|
|
|
|
| 1703 |
Sys.sleep(0.05)
|
| 1704 |
|
| 1705 |
preds <- tryCatch({
|
| 1706 |
+
# Try caret predict first (works if column names exactly match)
|
| 1707 |
+
as.numeric(predict(model, newdata = X_pred))
|
| 1708 |
+
}, error = function(e1) {
|
| 1709 |
+
cat("PREDICT caret failed:", e1$message, "\n")
|
| 1710 |
tryCatch({
|
| 1711 |
+
# Fallback 1: use randomForest finalModel directly
|
| 1712 |
+
as.numeric(predict(model$finalModel, newdata = X_pred))
|
| 1713 |
}, error = function(e2) {
|
| 1714 |
+
cat("PREDICT finalModel failed:", e2$message, "\n")
|
| 1715 |
+
tryCatch({
|
| 1716 |
+
# Fallback 2: use randomForest finalModel with matrix
|
| 1717 |
+
as.numeric(predict(model$finalModel, newdata = as.matrix(X_pred)))
|
| 1718 |
+
}, error = function(e3) {
|
| 1719 |
+
stop(paste("All predict methods failed.\n",
|
| 1720 |
+
"caret:", e1$message, "\n",
|
| 1721 |
+
"finalModel df:", e2$message, "\n",
|
| 1722 |
+
"finalModel mat:", e3$message))
|
| 1723 |
+
})
|
| 1724 |
})
|
| 1725 |
})
|
| 1726 |
|
| 1727 |
+
# ββ Step 5: Save results βββββββββββββββββββββββββββββββββ
|
| 1728 |
+
incProgress(0.15, detail = "Saving results...")
|
| 1729 |
Sys.sleep(0.05)
|
| 1730 |
|
| 1731 |
+
result_df <- data.frame(
|
| 1732 |
+
Sample_ID = ids,
|
| 1733 |
+
Predicted_Phenotype = round(preds, 4),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1734 |
stringsAsFactors = FALSE
|
| 1735 |
)
|
| 1736 |
|
| 1737 |
+
values$predicted_test_phenotype_data <- result_df
|
| 1738 |
+
|
| 1739 |
+
cat("PREDICT: SUCCESS β", nrow(result_df), "predictions\n")
|
| 1740 |
+
cat("PREDICT: range", round(min(preds),3), "to", round(max(preds),3), "\n")
|
| 1741 |
+
|
| 1742 |
+
incProgress(0.05, detail = "Done!")
|
| 1743 |
+
Sys.sleep(0.05)
|
| 1744 |
|
| 1745 |
}) # end withProgress
|
| 1746 |
|
| 1747 |
+
# ββ Success notification βββββββββββββββββββββββββββββββββ
|
| 1748 |
+
res <- values$predicted_test_phenotype_data
|
| 1749 |
+
n <- nrow(res)
|
| 1750 |
+
rng <- paste0(round(min(res$Predicted_Phenotype),3), " β ", round(max(res$Predicted_Phenotype),3))
|
|
|
|
| 1751 |
|
| 1752 |
+
showNotification(paste0("β
Predicted ", n, " test samples (", rng, ")"),
|
| 1753 |
+
type = "message", duration = 6)
|
|
|
|
|
|
|
| 1754 |
|
| 1755 |
add_message("assistant", paste0(
|
| 1756 |
+
"β
**Test phenotypes predicted!**\n\n",
|
| 1757 |
+
"β’ **", n, "** samples predicted\n",
|
| 1758 |
+
"β’ Predicted range: **", rng, "**\n",
|
| 1759 |
+
"β’ Markers matched: **", length(intersect(values$model_train_cols, colnames(values$test_genotype_data))),
|
| 1760 |
+
"** / ", length(values$model_train_cols), "\n",
|
| 1761 |
+
"β’ Preview: *Data Upload β Predicted Test Phenotype* tab\n",
|
|
|
|
| 1762 |
"β’ Save: click **Download Predicted Phenotypes (.xlsx)**"
|
| 1763 |
))
|
| 1764 |
})
|
| 1765 |
|
| 1766 |
+
|
| 1767 |
# ============================================================
|
| 1768 |
# DOMESTICATION
|
| 1769 |
# ============================================================
|
|
|
|
| 2203 |
})
|
| 2204 |
}
|
| 2205 |
|
| 2206 |
+
shinyApp(ui = ui, server = server)
|