International Rice Research Institute commited on
Update app.R
Browse files
app.R
CHANGED
|
@@ -9,7 +9,7 @@ library(plotly)
|
|
| 9 |
library(shinyWidgets)
|
| 10 |
library(ggplot2)
|
| 11 |
|
| 12 |
-
# UI with updated styling
|
| 13 |
ui <- fluidPage(
|
| 14 |
useShinyjs(),
|
| 15 |
tags$head(
|
|
@@ -68,6 +68,7 @@ ui <- fluidPage(
|
|
| 68 |
background: var(--card-bg);
|
| 69 |
padding: 1.5rem;
|
| 70 |
border: 1px solid rgba(30, 58, 138, 0.2);
|
|
|
|
| 71 |
}
|
| 72 |
|
| 73 |
.insights {
|
|
@@ -76,6 +77,7 @@ ui <- fluidPage(
|
|
| 76 |
background: #F5F5F5;
|
| 77 |
border-radius: 8px;
|
| 78 |
border-left: 4px solid var(--secondary);
|
|
|
|
| 79 |
}
|
| 80 |
|
| 81 |
.upload-container {
|
|
@@ -281,6 +283,17 @@ ui <- fluidPage(
|
|
| 281 |
color: var(--dark);
|
| 282 |
margin: 0;
|
| 283 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
"))
|
| 285 |
),
|
| 286 |
tabsetPanel(
|
|
@@ -302,7 +315,7 @@ ui <- fluidPage(
|
|
| 302 |
div(id = "pheno_loader", class = "loader hidden")
|
| 303 |
),
|
| 304 |
radioButtons("pheno_plotType", "📊 Select Plot Type",
|
| 305 |
-
choices = c("2D Bar" = "2d", "3D
|
| 306 |
selected = "2d", width = "100%"),
|
| 307 |
actionButton("pheno_process", "🔍 Analyze Data", class = "btn-primary w-full"),
|
| 308 |
tabsetPanel(
|
|
@@ -311,7 +324,7 @@ ui <- fluidPage(
|
|
| 311 |
div(
|
| 312 |
class = "section",
|
| 313 |
h3("🔍 Missing Data Patterns by Environment & Trait", class = "text-2xl font-bold mb-6"),
|
| 314 |
-
div(class = "chart-container", plotlyOutput("pheno_missingPlot"))
|
| 315 |
)
|
| 316 |
),
|
| 317 |
tabPanel(
|
|
@@ -319,7 +332,7 @@ ui <- fluidPage(
|
|
| 319 |
div(
|
| 320 |
class = "section",
|
| 321 |
h3("✅ Post-Filtering Missing Data Patterns", class = "text-2xl font-bold mb-6"),
|
| 322 |
-
div(class = "chart-container", plotlyOutput("pheno_filteredMissingPlot"))
|
| 323 |
)
|
| 324 |
),
|
| 325 |
tabPanel(
|
|
@@ -344,7 +357,7 @@ ui <- fluidPage(
|
|
| 344 |
class = "sidebar-header",
|
| 345 |
h3("📊 Phenotypic data analysis", style = "margin: 0; font-weight: 600; color: white;")
|
| 346 |
),
|
| 347 |
-
selectInput("lme4_selected_trait", "Select Trait", choices = c()),
|
| 348 |
checkboxInput("lme4_include_interaction", "Include Environment:Genotype Interaction", value = FALSE),
|
| 349 |
div(
|
| 350 |
class = "sidebar-content",
|
|
@@ -425,7 +438,7 @@ ui <- fluidPage(
|
|
| 425 |
h2("📈 Predicted Means (BLUEs)"),
|
| 426 |
div(
|
| 427 |
id = "lme4_blues_content",
|
| 428 |
-
div(class = "chart-container", plotlyOutput("lme4_blues_plot")),
|
| 429 |
div(class = "table-container", DTOutput("lme4_blues_table")),
|
| 430 |
div(
|
| 431 |
class = "insights",
|
|
@@ -444,15 +457,15 @@ ui <- fluidPage(
|
|
| 444 |
class = "tab-pane fade", id = "lme4-heritability", role = "tabpanel",
|
| 445 |
div(
|
| 446 |
class = "section",
|
| 447 |
-
h2("🧬 Heritability"),
|
| 448 |
div(
|
| 449 |
id = "lme4_heritability_content",
|
| 450 |
-
div(class = "chart-container", plotlyOutput("lme4_heritability_plot")),
|
| 451 |
div(class = "table-container", DTOutput("lme4_heritability_table")),
|
| 452 |
div(
|
| 453 |
class = "insights",
|
| 454 |
h4("💡 Interpretation"),
|
| 455 |
-
p("Heritability (H²) measures
|
| 456 |
)
|
| 457 |
),
|
| 458 |
div(
|
|
@@ -469,12 +482,12 @@ ui <- fluidPage(
|
|
| 469 |
h2("🔗 Trait Correlations"),
|
| 470 |
div(
|
| 471 |
id = "lme4_correlations_content",
|
| 472 |
-
div(class = "chart-container", plotlyOutput("lme4_correlation_plot")),
|
| 473 |
div(class = "table-container", DTOutput("lme4_cor_table")),
|
| 474 |
div(
|
| 475 |
class = "insights",
|
| 476 |
h4("💡 Interpretation"),
|
| 477 |
-
p("Correlations reveal trait synergies or trade-offs. Positive correlations (near 1) suggest traits improve together; negative correlations (near -1) indicate trade-offs.
|
| 478 |
)
|
| 479 |
),
|
| 480 |
div(
|
|
@@ -491,7 +504,7 @@ ui <- fluidPage(
|
|
| 491 |
h2("⚖️ Stability Analysis"),
|
| 492 |
div(
|
| 493 |
id = "lme4_stability_content",
|
| 494 |
-
div(class = "chart-container", plotlyOutput("lme4_stability_plot")),
|
| 495 |
div(class = "table-container", DTOutput("lme4_stability_table")),
|
| 496 |
div(
|
| 497 |
class = "insights",
|
|
@@ -539,6 +552,18 @@ server <- function(input, output, session) {
|
|
| 539 |
debug_log = NULL
|
| 540 |
)
|
| 541 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 542 |
# Phenotypic: Process data
|
| 543 |
pheno_process_data <- function(path) {
|
| 544 |
message("Pheno: Reading Excel file: ", path)
|
|
@@ -655,16 +680,30 @@ server <- function(input, output, session) {
|
|
| 655 |
req(rv_pheno$missing_data)
|
| 656 |
data <- rv_pheno$missing_data %>% filter(!is.na(value))
|
| 657 |
if (nrow(data) == 0) {
|
| 658 |
-
return(plot_ly() %>%
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 659 |
}
|
| 660 |
if (input$pheno_plotType == "2d") {
|
| 661 |
p <- ggplot(data, aes(x = Environment, y = value, fill = Trait)) +
|
| 662 |
geom_bar(stat = "identity", position = "dodge", alpha = 0.85) +
|
| 663 |
-
labs(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 664 |
theme_minimal() +
|
| 665 |
-
scale_fill_manual(values =
|
| 666 |
theme(
|
| 667 |
-
|
|
|
|
| 668 |
axis.text.y = element_text(size = 12, color = "#2D3748"),
|
| 669 |
axis.title.x = element_text(size = 14, color = "#2D3748", face = "bold", margin = margin(t = 12)),
|
| 670 |
axis.title.y = element_text(size = 14, color = "#2D3748", face = "bold", margin = margin(r = 12)),
|
|
@@ -678,13 +717,11 @@ server <- function(input, output, session) {
|
|
| 678 |
) +
|
| 679 |
facet_wrap(~Trait, ncol = 2, scales = "free_y")
|
| 680 |
|
| 681 |
-
ggplotly(p) %>%
|
| 682 |
layout(
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
|
| 686 |
-
font = list(family = "Poppins", size = 12, color = "#2D3748"),
|
| 687 |
-
showlegend = TRUE
|
| 688 |
)
|
| 689 |
} else {
|
| 690 |
plot_ly(
|
|
@@ -695,16 +732,13 @@ server <- function(input, output, session) {
|
|
| 695 |
type = "heatmap",
|
| 696 |
colorscale = list(c(0, 0.5, 1), c("#FFB6C1", "#FFDAB9", "#B5EAD7")),
|
| 697 |
showscale = TRUE,
|
| 698 |
-
colorbar = list(title = "Missing Proportion")
|
| 699 |
) %>%
|
| 700 |
layout(
|
| 701 |
-
|
| 702 |
-
|
| 703 |
-
|
| 704 |
-
|
| 705 |
-
margin = list(l = 70, r = 70, t = 70, b = 140),
|
| 706 |
-
font = list(family = "Poppins", size = 12, color = "#2D3748"),
|
| 707 |
-
showlegend = FALSE
|
| 708 |
)
|
| 709 |
}
|
| 710 |
})
|
|
@@ -713,16 +747,30 @@ server <- function(input, output, session) {
|
|
| 713 |
req(rv_pheno$filtered_missing_data)
|
| 714 |
data <- rv_pheno$filtered_missing_data %>% filter(!is.na(value))
|
| 715 |
if (nrow(data) == 0) {
|
| 716 |
-
return(plot_ly() %>%
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 717 |
}
|
| 718 |
if (input$pheno_plotType == "2d") {
|
| 719 |
p <- ggplot(data, aes(x = Environment, y = value, fill = Trait)) +
|
| 720 |
geom_bar(stat = "identity", position = "dodge", alpha = 0.85) +
|
| 721 |
-
labs(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 722 |
theme_minimal() +
|
| 723 |
-
scale_fill_manual(values =
|
| 724 |
theme(
|
| 725 |
-
|
|
|
|
| 726 |
axis.text.y = element_text(size = 12, color = "#2D3748"),
|
| 727 |
axis.title.x = element_text(size = 14, color = "#2D3748", face = "bold", margin = margin(t = 12)),
|
| 728 |
axis.title.y = element_text(size = 14, color = "#2D3748", face = "bold", margin = margin(r = 12)),
|
|
@@ -736,13 +784,11 @@ server <- function(input, output, session) {
|
|
| 736 |
) +
|
| 737 |
facet_wrap(~Trait, ncol = 2, scales = "free_y")
|
| 738 |
|
| 739 |
-
ggplotly(p) %>%
|
| 740 |
layout(
|
| 741 |
-
|
| 742 |
-
|
| 743 |
-
|
| 744 |
-
font = list(family = "Poppins", size = 12, color = "#2D3748"),
|
| 745 |
-
showlegend = TRUE
|
| 746 |
)
|
| 747 |
} else {
|
| 748 |
plot_ly(
|
|
@@ -753,16 +799,13 @@ server <- function(input, output, session) {
|
|
| 753 |
type = "heatmap",
|
| 754 |
colorscale = list(c(0, 0.5, 1), c("#FFB6C1", "#FFDAB9", "#B5EAD7")),
|
| 755 |
showscale = TRUE,
|
| 756 |
-
colorbar = list(title = "Missing Proportion")
|
| 757 |
) %>%
|
| 758 |
layout(
|
| 759 |
-
|
| 760 |
-
|
| 761 |
-
|
| 762 |
-
|
| 763 |
-
margin = list(l = 70, r = 70, t = 70, b = 140),
|
| 764 |
-
font = list(family = "Poppins", size = 12, color = "#2D3748"),
|
| 765 |
-
showlegend = FALSE
|
| 766 |
)
|
| 767 |
}
|
| 768 |
})
|
|
@@ -782,7 +825,7 @@ server <- function(input, output, session) {
|
|
| 782 |
extensions = 'Buttons',
|
| 783 |
class = 'cell-border stripe',
|
| 784 |
caption = "Environment-Wise Descriptive Statistics"
|
| 785 |
-
)
|
| 786 |
})
|
| 787 |
|
| 788 |
output$pheno_downloadStats <- downloadHandler(
|
|
@@ -828,7 +871,7 @@ server <- function(input, output, session) {
|
|
| 828 |
message("LME4: Warning: ", zero_combinations, " Environment:Genotype combinations have no data")
|
| 829 |
}
|
| 830 |
if (zero_combinations > (nrow(combo_counts) * ncol(combo_counts) * 0.5)) {
|
| 831 |
-
|
| 832 |
}
|
| 833 |
|
| 834 |
missing_data <- data %>%
|
|
@@ -869,16 +912,21 @@ server <- function(input, output, session) {
|
|
| 869 |
}
|
| 870 |
formula <- as.formula(formula_str)
|
| 871 |
tryCatch({
|
| 872 |
-
model <- lmer(formula, data = data, na.action = na.omit,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 873 |
message("LME4: Successfully fitted model for ", trait)
|
| 874 |
return(model)
|
| 875 |
}, error = function(e) {
|
| 876 |
message("LME4: Error fitting model for ", trait, ": ", e$message)
|
| 877 |
-
showNotification(paste("Model fitting failed for
|
| 878 |
return(NULL)
|
| 879 |
}, warning = function(w) {
|
| 880 |
message("LME4: Warning in model fitting for ", trait, ": ", w$message)
|
| 881 |
-
showNotification(paste("Warning for
|
| 882 |
return(NULL)
|
| 883 |
})
|
| 884 |
}
|
|
@@ -887,7 +935,7 @@ server <- function(input, output, session) {
|
|
| 887 |
lme4_get_blue <- function(model, trait) {
|
| 888 |
if (is.null(model)) {
|
| 889 |
message("LME4: No model for ", trait)
|
| 890 |
-
return(data.frame(Trait = trait, Genotype = NA, Environment = NA, predicted.value = NA))
|
| 891 |
}
|
| 892 |
tryCatch({
|
| 893 |
emm_options(pbkrtest.limit = 5000, lmerTest.limit = 5000)
|
|
@@ -895,16 +943,19 @@ server <- function(input, output, session) {
|
|
| 895 |
blues_df <- as.data.frame(blues) %>%
|
| 896 |
rename(predicted.value = emmean) %>%
|
| 897 |
mutate(Trait = trait) %>%
|
| 898 |
-
select(Trait, Genotype, Environment, predicted.value) %>%
|
| 899 |
filter(!is.na(predicted.value))
|
|
|
|
|
|
|
|
|
|
| 900 |
return(blues_df)
|
| 901 |
}, error = function(e) {
|
| 902 |
message("LME4: Error in BLUEs for ", trait, ": ", e$message)
|
| 903 |
-
return(data.frame(Trait = trait, Genotype = NA, Environment = NA, predicted.value = NA))
|
| 904 |
})
|
| 905 |
}
|
| 906 |
|
| 907 |
-
# LME4: Calculate heritability
|
| 908 |
lme4_calculate_heritability <- function(model, trait) {
|
| 909 |
message("LME4: Calculating heritability for ", trait)
|
| 910 |
if (is.null(model)) {
|
|
@@ -913,17 +964,28 @@ server <- function(input, output, session) {
|
|
| 913 |
tryCatch({
|
| 914 |
varcomp <- as.data.frame(VarCorr(model))
|
| 915 |
vg <- varcomp$vcov[varcomp$grp == "Genotype"]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 916 |
if (length(vg) == 0 || is.na(vg) || vg <= 0) {
|
| 917 |
-
return(data.frame(Trait = trait, Heritability = NA, Error = "Invalid genetic variance"))
|
| 918 |
}
|
| 919 |
-
ve <- sigma(model)^2
|
| 920 |
if (is.na(ve) || ve <= 0) {
|
| 921 |
-
return(data.frame(Trait = trait, Heritability = NA, Error = "Invalid residual variance"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 922 |
}
|
| 923 |
-
|
| 924 |
-
if (h2 < 0 || h2 > 1) {
|
| 925 |
return(data.frame(Trait = trait, Heritability = NA, Error = "Heritability out of bounds"))
|
| 926 |
}
|
|
|
|
| 927 |
return(data.frame(Trait = trait, Heritability = round(h2, 3), Error = NA))
|
| 928 |
}, error = function(e) {
|
| 929 |
message("LME4: Error in heritability for ", trait, ": ", e$message)
|
|
@@ -959,8 +1021,8 @@ server <- function(input, output, session) {
|
|
| 959 |
data.frame(Slope = NA, R2 = NA, Error = "Model fitting failed")
|
| 960 |
} else {
|
| 961 |
data.frame(
|
| 962 |
-
Slope = coef(model)[2],
|
| 963 |
-
R2 = summary(model)$r.squared,
|
| 964 |
Error = NA
|
| 965 |
)
|
| 966 |
}
|
|
@@ -1031,7 +1093,8 @@ server <- function(input, output, session) {
|
|
| 1031 |
if (nrow(trait_data) > 0) {
|
| 1032 |
cor_matrix <- cor(trait_data, use = "pairwise.complete.obs")
|
| 1033 |
cor_table <- as.data.frame(as.table(cor_matrix)) %>%
|
| 1034 |
-
rename(Trait1 = Var1, Trait2 = Var2, Correlation = Freq)
|
|
|
|
| 1035 |
rv_lme4$cor_matrix <- cor_matrix
|
| 1036 |
rv_lme4$cor_table <- cor_table
|
| 1037 |
debug_log <- c(debug_log, "Correlations: Success")
|
|
@@ -1048,7 +1111,7 @@ server <- function(input, output, session) {
|
|
| 1048 |
|
| 1049 |
rv_lme4$lme4_models <- lme4_models
|
| 1050 |
rv_lme4$blues_all <- bind_rows(blues_list) %>% filter(!is.na(predicted.value))
|
| 1051 |
-
rv_lme4$heritability_table <- bind_rows(herit_list)
|
| 1052 |
rv_lme4$stability_table <- bind_rows(stability_list)
|
| 1053 |
rv_lme4$debug_log <- debug_log
|
| 1054 |
|
|
@@ -1081,11 +1144,6 @@ server <- function(input, output, session) {
|
|
| 1081 |
rv_lme4$blues_all %>% filter(Trait == rv_lme4$trait, !is.na(predicted.value))
|
| 1082 |
})
|
| 1083 |
|
| 1084 |
-
lme4_filtered_heritability <- reactive({
|
| 1085 |
-
req(rv_lme4$heritability_table, rv_lme4$trait)
|
| 1086 |
-
rv_lme4$heritability_table %>% filter(Trait == rv_lme4$trait)
|
| 1087 |
-
})
|
| 1088 |
-
|
| 1089 |
lme4_filtered_stability <- reactive({
|
| 1090 |
req(rv_lme4$stability_table, rv_lme4$trait)
|
| 1091 |
rv_lme4$stability_table %>% filter(Trait == rv_lme4$trait, !is.na(Slope))
|
|
@@ -1096,132 +1154,157 @@ server <- function(input, output, session) {
|
|
| 1096 |
req(lme4_filtered_blues())
|
| 1097 |
data <- lme4_filtered_blues()
|
| 1098 |
if (nrow(data) == 0 || all(is.na(data$predicted.value))) {
|
| 1099 |
-
plot_ly() %>%
|
| 1100 |
-
|
| 1101 |
-
|
| 1102 |
-
|
| 1103 |
-
|
| 1104 |
-
|
| 1105 |
-
|
| 1106 |
-
|
| 1107 |
-
|
| 1108 |
-
|
| 1109 |
-
colors = c('#FFB6C1', '#FFDAB9', '#B5EAD7', '#E6E6FA', '#FFFACD', '#FF9999')) %>%
|
| 1110 |
-
layout(
|
| 1111 |
-
title = paste("Predicted Means for", rv_lme4$trait),
|
| 1112 |
-
xaxis = list(title = "Environment"),
|
| 1113 |
-
yaxis = list(title = "Predicted Value"),
|
| 1114 |
-
plot_bgcolor = "transparent",
|
| 1115 |
-
paper_bgcolor = "transparent",
|
| 1116 |
-
font = list(family = "Poppins", size = 12, color = "#2D3748")
|
| 1117 |
-
)
|
| 1118 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1119 |
})
|
| 1120 |
|
| 1121 |
output$lme4_heritability_plot <- renderPlotly({
|
| 1122 |
-
req(
|
| 1123 |
-
data <-
|
| 1124 |
-
if (nrow(data) == 0
|
| 1125 |
-
plot_ly() %>%
|
| 1126 |
-
|
| 1127 |
-
|
| 1128 |
-
|
| 1129 |
-
|
| 1130 |
-
|
| 1131 |
-
|
| 1132 |
-
|
| 1133 |
-
|
| 1134 |
-
|
| 1135 |
-
layout(
|
| 1136 |
-
title = "Heritability",
|
| 1137 |
-
xaxis = list(title = "Trait"),
|
| 1138 |
-
yaxis = list(title = "Heritability", range = c(0, 1)),
|
| 1139 |
-
showlegend = FALSE,
|
| 1140 |
-
plot_bgcolor = "transparent",
|
| 1141 |
-
paper_bgcolor = "transparent",
|
| 1142 |
-
font = list(family = "Poppins", size = 12, color = "#2D3748")
|
| 1143 |
-
)
|
| 1144 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1145 |
})
|
| 1146 |
|
| 1147 |
output$lme4_correlation_plot <- renderPlotly({
|
| 1148 |
req(rv_lme4$cor_matrix)
|
| 1149 |
if (ncol(rv_lme4$cor_matrix) < 2 || all(is.na(rv_lme4$cor_matrix))) {
|
| 1150 |
-
plot_ly() %>%
|
| 1151 |
-
|
| 1152 |
-
|
| 1153 |
-
|
| 1154 |
-
|
| 1155 |
-
|
| 1156 |
-
|
| 1157 |
-
|
| 1158 |
-
|
| 1159 |
-
|
| 1160 |
-
y = rownames(rv_lme4$cor_matrix),
|
| 1161 |
-
z = rv_lme4$cor_matrix,
|
| 1162 |
-
type = "heatmap",
|
| 1163 |
-
colors = colorRamp(c('#E6E6FA', 'white', '#FFB6C1'))
|
| 1164 |
-
) %>%
|
| 1165 |
-
layout(
|
| 1166 |
-
title = "Trait Correlation Matrix",
|
| 1167 |
-
xaxis = list(title = ""),
|
| 1168 |
-
yaxis = list(title = ""),
|
| 1169 |
-
plot_bgcolor = "transparent",
|
| 1170 |
-
paper_bgcolor = "transparent",
|
| 1171 |
-
font = list(family = "Poppins", size = 12, color = "#2D3748")
|
| 1172 |
-
)
|
| 1173 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1174 |
})
|
| 1175 |
|
| 1176 |
output$lme4_stability_plot <- renderPlotly({
|
| 1177 |
req(lme4_filtered_stability())
|
| 1178 |
data <- lme4_filtered_stability()
|
| 1179 |
if (nrow(data) == 0 || all(is.na(data$Slope))) {
|
| 1180 |
-
plot_ly() %>%
|
| 1181 |
-
|
| 1182 |
-
|
| 1183 |
-
|
| 1184 |
-
|
| 1185 |
-
|
| 1186 |
-
|
| 1187 |
-
|
| 1188 |
-
|
| 1189 |
-
|
| 1190 |
-
textposition = 'top') %>%
|
| 1191 |
-
layout(
|
| 1192 |
-
title = paste("Stability Analysis for", rv_lme4$trait),
|
| 1193 |
-
xaxis = list(title = "Slope"),
|
| 1194 |
-
yaxis = list(title = "R²"),
|
| 1195 |
-
plot_bgcolor = "transparent",
|
| 1196 |
-
paper_bgcolor = "transparent",
|
| 1197 |
-
font = list(family = "Poppins", size = 12, color = "#2D3748")
|
| 1198 |
-
)
|
| 1199 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1200 |
})
|
| 1201 |
|
| 1202 |
# LME4: Table outputs
|
| 1203 |
output$lme4_blues_table <- renderDT({
|
| 1204 |
req(lme4_filtered_blues())
|
| 1205 |
datatable(
|
| 1206 |
-
lme4_filtered_blues(),
|
| 1207 |
extensions = 'Buttons',
|
| 1208 |
-
options = list(
|
| 1209 |
-
|
| 1210 |
-
|
| 1211 |
-
|
| 1212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1213 |
})
|
| 1214 |
|
| 1215 |
output$lme4_heritability_table <- renderDT({
|
| 1216 |
-
req(
|
| 1217 |
datatable(
|
| 1218 |
-
|
| 1219 |
extensions = 'Buttons',
|
| 1220 |
-
options = list(
|
| 1221 |
-
|
| 1222 |
-
|
| 1223 |
-
|
| 1224 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1225 |
})
|
| 1226 |
|
| 1227 |
output$lme4_cor_table <- renderDT({
|
|
@@ -1229,23 +1312,31 @@ server <- function(input, output, session) {
|
|
| 1229 |
datatable(
|
| 1230 |
rv_lme4$cor_table,
|
| 1231 |
extensions = 'Buttons',
|
| 1232 |
-
options = list(
|
| 1233 |
-
|
| 1234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1235 |
caption = "Correlation Between Traits"
|
| 1236 |
-
)
|
| 1237 |
})
|
| 1238 |
|
| 1239 |
output$lme4_stability_table <- renderDT({
|
| 1240 |
req(lme4_filtered_stability())
|
| 1241 |
datatable(
|
| 1242 |
-
lme4_filtered_stability(),
|
| 1243 |
extensions = 'Buttons',
|
| 1244 |
-
options = list(
|
| 1245 |
-
|
| 1246 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1247 |
caption = paste("Stability Analysis for", rv_lme4$trait)
|
| 1248 |
-
)
|
| 1249 |
})
|
| 1250 |
}
|
| 1251 |
|
|
|
|
| 9 |
library(shinyWidgets)
|
| 10 |
library(ggplot2)
|
| 11 |
|
| 12 |
+
# UI with updated styling and improved plot containers
|
| 13 |
ui <- fluidPage(
|
| 14 |
useShinyjs(),
|
| 15 |
tags$head(
|
|
|
|
| 68 |
background: var(--card-bg);
|
| 69 |
padding: 1.5rem;
|
| 70 |
border: 1px solid rgba(30, 58, 138, 0.2);
|
| 71 |
+
min-height: 400px;
|
| 72 |
}
|
| 73 |
|
| 74 |
.insights {
|
|
|
|
| 77 |
background: #F5F5F5;
|
| 78 |
border-radius: 8px;
|
| 79 |
border-left: 4px solid var(--secondary);
|
| 80 |
+
font-size: 14px;
|
| 81 |
}
|
| 82 |
|
| 83 |
.upload-container {
|
|
|
|
| 283 |
color: var(--dark);
|
| 284 |
margin: 0;
|
| 285 |
}
|
| 286 |
+
|
| 287 |
+
.plotly-title {
|
| 288 |
+
font-size: 18px;
|
| 289 |
+
font-weight: bold;
|
| 290 |
+
color: var(--primary);
|
| 291 |
+
}
|
| 292 |
+
|
| 293 |
+
.plotly-annotation {
|
| 294 |
+
font-size: 14px;
|
| 295 |
+
color: var(--dark);
|
| 296 |
+
}
|
| 297 |
"))
|
| 298 |
),
|
| 299 |
tabsetPanel(
|
|
|
|
| 315 |
div(id = "pheno_loader", class = "loader hidden")
|
| 316 |
),
|
| 317 |
radioButtons("pheno_plotType", "📊 Select Plot Type",
|
| 318 |
+
choices = c("2D Bar" = "2d", "3D Heatmap" = "3d"),
|
| 319 |
selected = "2d", width = "100%"),
|
| 320 |
actionButton("pheno_process", "🔍 Analyze Data", class = "btn-primary w-full"),
|
| 321 |
tabsetPanel(
|
|
|
|
| 324 |
div(
|
| 325 |
class = "section",
|
| 326 |
h3("🔍 Missing Data Patterns by Environment & Trait", class = "text-2xl font-bold mb-6"),
|
| 327 |
+
div(class = "chart-container", plotlyOutput("pheno_missingPlot", height = "500px"))
|
| 328 |
)
|
| 329 |
),
|
| 330 |
tabPanel(
|
|
|
|
| 332 |
div(
|
| 333 |
class = "section",
|
| 334 |
h3("✅ Post-Filtering Missing Data Patterns", class = "text-2xl font-bold mb-6"),
|
| 335 |
+
div(class = "chart-container", plotlyOutput("pheno_filteredMissingPlot", height = "500px"))
|
| 336 |
)
|
| 337 |
),
|
| 338 |
tabPanel(
|
|
|
|
| 357 |
class = "sidebar-header",
|
| 358 |
h3("📊 Phenotypic data analysis", style = "margin: 0; font-weight: 600; color: white;")
|
| 359 |
),
|
| 360 |
+
selectInput("lme4_selected_trait", "Select Trait for Detailed Analysis", choices = c()),
|
| 361 |
checkboxInput("lme4_include_interaction", "Include Environment:Genotype Interaction", value = FALSE),
|
| 362 |
div(
|
| 363 |
class = "sidebar-content",
|
|
|
|
| 438 |
h2("📈 Predicted Means (BLUEs)"),
|
| 439 |
div(
|
| 440 |
id = "lme4_blues_content",
|
| 441 |
+
div(class = "chart-container", plotlyOutput("lme4_blues_plot", height = "500px")),
|
| 442 |
div(class = "table-container", DTOutput("lme4_blues_table")),
|
| 443 |
div(
|
| 444 |
class = "insights",
|
|
|
|
| 457 |
class = "tab-pane fade", id = "lme4-heritability", role = "tabpanel",
|
| 458 |
div(
|
| 459 |
class = "section",
|
| 460 |
+
h2("🧬 Heritability Across All Traits"),
|
| 461 |
div(
|
| 462 |
id = "lme4_heritability_content",
|
| 463 |
+
div(class = "chart-container", plotlyOutput("lme4_heritability_plot", height = "500px")),
|
| 464 |
div(class = "table-container", DTOutput("lme4_heritability_table")),
|
| 465 |
div(
|
| 466 |
class = "insights",
|
| 467 |
h4("💡 Interpretation"),
|
| 468 |
+
p("Heritability (H²) measures the proportion of phenotypic variance due to genetics. High H² (>0.7) traits are easier to improve via breeding; low H² traits are more environment-driven.")
|
| 469 |
)
|
| 470 |
),
|
| 471 |
div(
|
|
|
|
| 482 |
h2("🔗 Trait Correlations"),
|
| 483 |
div(
|
| 484 |
id = "lme4_correlations_content",
|
| 485 |
+
div(class = "chart-container", plotlyOutput("lme4_correlation_plot", height = "500px")),
|
| 486 |
div(class = "table-container", DTOutput("lme4_cor_table")),
|
| 487 |
div(
|
| 488 |
class = "insights",
|
| 489 |
h4("💡 Interpretation"),
|
| 490 |
+
p("Correlations reveal trait synergies or trade-offs. Positive correlations (near 1) suggest traits improve together; negative correlations (near -1) indicate trade-offs.")
|
| 491 |
)
|
| 492 |
),
|
| 493 |
div(
|
|
|
|
| 504 |
h2("⚖️ Stability Analysis"),
|
| 505 |
div(
|
| 506 |
id = "lme4_stability_content",
|
| 507 |
+
div(class = "chart-container", plotlyOutput("lme4_stability_plot", height = "500px")),
|
| 508 |
div(class = "table-container", DTOutput("lme4_stability_table")),
|
| 509 |
div(
|
| 510 |
class = "insights",
|
|
|
|
| 552 |
debug_log = NULL
|
| 553 |
)
|
| 554 |
|
| 555 |
+
# Plot formatting constants
|
| 556 |
+
PLOT_FONT <- list(family = "Poppins", size = 14, color = "#2D3748")
|
| 557 |
+
PLOT_COLORS <- c("#FFB6C1", "#FFDAB9", "#B5EAD7", "#E6E6FA", "#FFFACD", "#FF9999")
|
| 558 |
+
PLOT_LAYOUT <- list(
|
| 559 |
+
plot_bgcolor = "transparent",
|
| 560 |
+
paper_bgcolor = "transparent",
|
| 561 |
+
margin = list(l = 80, r = 80, t = 100, b = 100),
|
| 562 |
+
font = PLOT_FONT,
|
| 563 |
+
showlegend = TRUE,
|
| 564 |
+
legend = list(orientation = "h", y = -0.2, x = 0.5, xanchor = "center")
|
| 565 |
+
)
|
| 566 |
+
|
| 567 |
# Phenotypic: Process data
|
| 568 |
pheno_process_data <- function(path) {
|
| 569 |
message("Pheno: Reading Excel file: ", path)
|
|
|
|
| 680 |
req(rv_pheno$missing_data)
|
| 681 |
data <- rv_pheno$missing_data %>% filter(!is.na(value))
|
| 682 |
if (nrow(data) == 0) {
|
| 683 |
+
return(plot_ly() %>%
|
| 684 |
+
layout(
|
| 685 |
+
title = list(text = "No Valid Data for Missing Data Plot", font = PLOT_FONT),
|
| 686 |
+
annotations = list(
|
| 687 |
+
text = "Upload valid data to visualize missing patterns.",
|
| 688 |
+
x = 0.5, y = 0.5, xref = "paper", yref = "paper",
|
| 689 |
+
showarrow = FALSE, font = list(size = 12, color = "#2D3748")
|
| 690 |
+
),
|
| 691 |
+
PLOT_LAYOUT
|
| 692 |
+
))
|
| 693 |
}
|
| 694 |
if (input$pheno_plotType == "2d") {
|
| 695 |
p <- ggplot(data, aes(x = Environment, y = value, fill = Trait)) +
|
| 696 |
geom_bar(stat = "identity", position = "dodge", alpha = 0.85) +
|
| 697 |
+
labs(
|
| 698 |
+
title = "Missing Data by Environment and Trait",
|
| 699 |
+
y = "Proportion of Missing Data",
|
| 700 |
+
x = "Environment"
|
| 701 |
+
) +
|
| 702 |
theme_minimal() +
|
| 703 |
+
scale_fill_manual(values = PLOT_COLORS) +
|
| 704 |
theme(
|
| 705 |
+
plot.title = element_text(size = 18, face = "bold", color = "#2D3748", hjust = 0.5),
|
| 706 |
+
axis.text.x = element_text(angle = 45, hjust = 1, size = 12, color = "#2D3748"),
|
| 707 |
axis.text.y = element_text(size = 12, color = "#2D3748"),
|
| 708 |
axis.title.x = element_text(size = 14, color = "#2D3748", face = "bold", margin = margin(t = 12)),
|
| 709 |
axis.title.y = element_text(size = 14, color = "#2D3748", face = "bold", margin = margin(r = 12)),
|
|
|
|
| 717 |
) +
|
| 718 |
facet_wrap(~Trait, ncol = 2, scales = "free_y")
|
| 719 |
|
| 720 |
+
ggplotly(p, tooltip = c("Environment", "Trait", "value")) %>%
|
| 721 |
layout(
|
| 722 |
+
PLOT_LAYOUT,
|
| 723 |
+
title = list(text = "Missing Data by Environment and Trait", font = list(size = 18, family = "Poppins", color = "#2D3748")),
|
| 724 |
+
hoverlabel = list(bgcolor = "white", font = PLOT_FONT)
|
|
|
|
|
|
|
| 725 |
)
|
| 726 |
} else {
|
| 727 |
plot_ly(
|
|
|
|
| 732 |
type = "heatmap",
|
| 733 |
colorscale = list(c(0, 0.5, 1), c("#FFB6C1", "#FFDAB9", "#B5EAD7")),
|
| 734 |
showscale = TRUE,
|
| 735 |
+
colorbar = list(title = "Missing Proportion", titlefont = PLOT_FONT, tickfont = PLOT_FONT)
|
| 736 |
) %>%
|
| 737 |
layout(
|
| 738 |
+
title = list(text = "Missing Data Heatmap", font = list(size = 18, family = "Poppins", color = "#2D3748")),
|
| 739 |
+
xaxis = list(title = "Environment", tickangle = 45, titlefont = PLOT_FONT, tickfont = PLOT_FONT),
|
| 740 |
+
yaxis = list(title = "Trait", titlefont = PLOT_FONT, tickfont = PLOT_FONT),
|
| 741 |
+
PLOT_LAYOUT
|
|
|
|
|
|
|
|
|
|
| 742 |
)
|
| 743 |
}
|
| 744 |
})
|
|
|
|
| 747 |
req(rv_pheno$filtered_missing_data)
|
| 748 |
data <- rv_pheno$filtered_missing_data %>% filter(!is.na(value))
|
| 749 |
if (nrow(data) == 0) {
|
| 750 |
+
return(plot_ly() %>%
|
| 751 |
+
layout(
|
| 752 |
+
title = list(text = "No Valid Filtered Data for Plot", font = PLOT_FONT),
|
| 753 |
+
annotations = list(
|
| 754 |
+
text = "No valid data after filtering environments with >20% missing data.",
|
| 755 |
+
x = 0.5, y = 0.5, xref = "paper", yref = "paper",
|
| 756 |
+
showarrow = FALSE, font = list(size = 12, color = "#2D3748")
|
| 757 |
+
),
|
| 758 |
+
PLOT_LAYOUT
|
| 759 |
+
))
|
| 760 |
}
|
| 761 |
if (input$pheno_plotType == "2d") {
|
| 762 |
p <- ggplot(data, aes(x = Environment, y = value, fill = Trait)) +
|
| 763 |
geom_bar(stat = "identity", position = "dodge", alpha = 0.85) +
|
| 764 |
+
labs(
|
| 765 |
+
title = "Filtered Missing Data by Environment and Trait",
|
| 766 |
+
y = "Proportion of Missing Data (Filtered)",
|
| 767 |
+
x = "Environment"
|
| 768 |
+
) +
|
| 769 |
theme_minimal() +
|
| 770 |
+
scale_fill_manual(values = PLOT_COLORS) +
|
| 771 |
theme(
|
| 772 |
+
plot.title = element_text(size = 18, face = "bold", color = "#2D3748", hjust = 0.5),
|
| 773 |
+
axis.text.x = element_text(angle = 45, hjust = 1, size = 12, color = "#2D3748"),
|
| 774 |
axis.text.y = element_text(size = 12, color = "#2D3748"),
|
| 775 |
axis.title.x = element_text(size = 14, color = "#2D3748", face = "bold", margin = margin(t = 12)),
|
| 776 |
axis.title.y = element_text(size = 14, color = "#2D3748", face = "bold", margin = margin(r = 12)),
|
|
|
|
| 784 |
) +
|
| 785 |
facet_wrap(~Trait, ncol = 2, scales = "free_y")
|
| 786 |
|
| 787 |
+
ggplotly(p, tooltip = c("Environment", "Trait", "value")) %>%
|
| 788 |
layout(
|
| 789 |
+
PLOT_LAYOUT,
|
| 790 |
+
title = list(text = "Filtered Missing Data by Environment and Trait", font = list(size = 18, family = "Poppins", color = "#2D3748")),
|
| 791 |
+
hoverlabel = list(bgcolor = "white", font = PLOT_FONT)
|
|
|
|
|
|
|
| 792 |
)
|
| 793 |
} else {
|
| 794 |
plot_ly(
|
|
|
|
| 799 |
type = "heatmap",
|
| 800 |
colorscale = list(c(0, 0.5, 1), c("#FFB6C1", "#FFDAB9", "#B5EAD7")),
|
| 801 |
showscale = TRUE,
|
| 802 |
+
colorbar = list(title = "Missing Proportion", titlefont = PLOT_FONT, tickfont = PLOT_FONT)
|
| 803 |
) %>%
|
| 804 |
layout(
|
| 805 |
+
title = list(text = "Filtered Missing Data Heatmap", font = list(size = 18, family = "Poppins", color = "#2D3748")),
|
| 806 |
+
xaxis = list(title = "Environment", tickangle = 45, titlefont = PLOT_FONT, tickfont = PLOT_FONT),
|
| 807 |
+
yaxis = list(title = "Trait", titlefont = PLOT_FONT, tickfont = PLOT_FONT),
|
| 808 |
+
PLOT_LAYOUT
|
|
|
|
|
|
|
|
|
|
| 809 |
)
|
| 810 |
}
|
| 811 |
})
|
|
|
|
| 825 |
extensions = 'Buttons',
|
| 826 |
class = 'cell-border stripe',
|
| 827 |
caption = "Environment-Wise Descriptive Statistics"
|
| 828 |
+
) %>% formatRound(columns = c("mean", "median", "sd", "min", "max", "cv"), digits = 2)
|
| 829 |
})
|
| 830 |
|
| 831 |
output$pheno_downloadStats <- downloadHandler(
|
|
|
|
| 871 |
message("LME4: Warning: ", zero_combinations, " Environment:Genotype combinations have no data")
|
| 872 |
}
|
| 873 |
if (zero_combinations > (nrow(combo_counts) * ncol(combo_counts) * 0.5)) {
|
| 874 |
+
showNotification("Warning: Over 50% of Environment:Genotype combinations are missing", type = "warning", duration = 10)
|
| 875 |
}
|
| 876 |
|
| 877 |
missing_data <- data %>%
|
|
|
|
| 912 |
}
|
| 913 |
formula <- as.formula(formula_str)
|
| 914 |
tryCatch({
|
| 915 |
+
model <- lmer(formula, data = data, na.action = na.omit,
|
| 916 |
+
control = lmerControl(check.rankX = "warn+drop.cols", optimizer = "bobyqa"))
|
| 917 |
+
if (isSingular(model)) {
|
| 918 |
+
message("LME4: Singular fit for ", trait)
|
| 919 |
+
showNotification(paste("Singular fit for", trait, "- results may be unreliable"), type = "warning", duration = 10)
|
| 920 |
+
}
|
| 921 |
message("LME4: Successfully fitted model for ", trait)
|
| 922 |
return(model)
|
| 923 |
}, error = function(e) {
|
| 924 |
message("LME4: Error fitting model for ", trait, ": ", e$message)
|
| 925 |
+
showNotification(paste("Model fitting failed for", trait, ": ", e$message), type = "error", duration = 10)
|
| 926 |
return(NULL)
|
| 927 |
}, warning = function(w) {
|
| 928 |
message("LME4: Warning in model fitting for ", trait, ": ", w$message)
|
| 929 |
+
showNotification(paste("Warning for", trait, ": ", w$message), type = "warning", duration = 10)
|
| 930 |
return(NULL)
|
| 931 |
})
|
| 932 |
}
|
|
|
|
| 935 |
lme4_get_blue <- function(model, trait) {
|
| 936 |
if (is.null(model)) {
|
| 937 |
message("LME4: No model for ", trait)
|
| 938 |
+
return(data.frame(Trait = trait, Genotype = NA, Environment = NA, predicted.value = NA, Error = "No model fitted"))
|
| 939 |
}
|
| 940 |
tryCatch({
|
| 941 |
emm_options(pbkrtest.limit = 5000, lmerTest.limit = 5000)
|
|
|
|
| 943 |
blues_df <- as.data.frame(blues) %>%
|
| 944 |
rename(predicted.value = emmean) %>%
|
| 945 |
mutate(Trait = trait) %>%
|
| 946 |
+
select(Trait, Genotype, Environment, predicted.value, SE = SE) %>%
|
| 947 |
filter(!is.na(predicted.value))
|
| 948 |
+
if (nrow(blues_df) == 0) {
|
| 949 |
+
return(data.frame(Trait = trait, Genotype = NA, Environment = NA, predicted.value = NA, SE = NA, Error = "No valid BLUEs"))
|
| 950 |
+
}
|
| 951 |
return(blues_df)
|
| 952 |
}, error = function(e) {
|
| 953 |
message("LME4: Error in BLUEs for ", trait, ": ", e$message)
|
| 954 |
+
return(data.frame(Trait = trait, Genotype = NA, Environment = NA, predicted.value = NA, SE = NA, Error = e$message))
|
| 955 |
})
|
| 956 |
}
|
| 957 |
|
| 958 |
+
# LME4: Calculate heritability (improved for robustness)
|
| 959 |
lme4_calculate_heritability <- function(model, trait) {
|
| 960 |
message("LME4: Calculating heritability for ", trait)
|
| 961 |
if (is.null(model)) {
|
|
|
|
| 964 |
tryCatch({
|
| 965 |
varcomp <- as.data.frame(VarCorr(model))
|
| 966 |
vg <- varcomp$vcov[varcomp$grp == "Genotype"]
|
| 967 |
+
vge <- varcomp$vcov[varcomp$grp == "Environment:Genotype"]
|
| 968 |
+
ve <- sigma(model)^2
|
| 969 |
+
|
| 970 |
+
# Check for valid variance components
|
| 971 |
if (length(vg) == 0 || is.na(vg) || vg <= 0) {
|
| 972 |
+
return(data.frame(Trait = trait, Heritability = NA, Error = "Invalid or zero genetic variance"))
|
| 973 |
}
|
|
|
|
| 974 |
if (is.na(ve) || ve <= 0) {
|
| 975 |
+
return(data.frame(Trait = trait, Heritability = NA, Error = "Invalid or zero residual variance"))
|
| 976 |
+
}
|
| 977 |
+
|
| 978 |
+
# Include GxE variance if interaction is included
|
| 979 |
+
if (input$lme4_include_interaction && !is.na(vge) && vge > 0) {
|
| 980 |
+
h2 <- (vg + vge) / (vg + vge + ve)
|
| 981 |
+
} else {
|
| 982 |
+
h2 <- vg / (vg + ve)
|
| 983 |
}
|
| 984 |
+
|
| 985 |
+
if (is.na(h2) || h2 < 0 || h2 > 1) {
|
| 986 |
return(data.frame(Trait = trait, Heritability = NA, Error = "Heritability out of bounds"))
|
| 987 |
}
|
| 988 |
+
|
| 989 |
return(data.frame(Trait = trait, Heritability = round(h2, 3), Error = NA))
|
| 990 |
}, error = function(e) {
|
| 991 |
message("LME4: Error in heritability for ", trait, ": ", e$message)
|
|
|
|
| 1021 |
data.frame(Slope = NA, R2 = NA, Error = "Model fitting failed")
|
| 1022 |
} else {
|
| 1023 |
data.frame(
|
| 1024 |
+
Slope = round(coef(model)[2], 3),
|
| 1025 |
+
R2 = round(summary(model)$r.squared, 3),
|
| 1026 |
Error = NA
|
| 1027 |
)
|
| 1028 |
}
|
|
|
|
| 1093 |
if (nrow(trait_data) > 0) {
|
| 1094 |
cor_matrix <- cor(trait_data, use = "pairwise.complete.obs")
|
| 1095 |
cor_table <- as.data.frame(as.table(cor_matrix)) %>%
|
| 1096 |
+
rename(Trait1 = Var1, Trait2 = Var2, Correlation = Freq) %>%
|
| 1097 |
+
mutate(Correlation = round(Correlation, 3))
|
| 1098 |
rv_lme4$cor_matrix <- cor_matrix
|
| 1099 |
rv_lme4$cor_table <- cor_table
|
| 1100 |
debug_log <- c(debug_log, "Correlations: Success")
|
|
|
|
| 1111 |
|
| 1112 |
rv_lme4$lme4_models <- lme4_models
|
| 1113 |
rv_lme4$blues_all <- bind_rows(blues_list) %>% filter(!is.na(predicted.value))
|
| 1114 |
+
rv_lme4$heritability_table <- bind_rows(herit_list) %>% filter(!is.na(Heritability) | !is.na(Error))
|
| 1115 |
rv_lme4$stability_table <- bind_rows(stability_list)
|
| 1116 |
rv_lme4$debug_log <- debug_log
|
| 1117 |
|
|
|
|
| 1144 |
rv_lme4$blues_all %>% filter(Trait == rv_lme4$trait, !is.na(predicted.value))
|
| 1145 |
})
|
| 1146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1147 |
lme4_filtered_stability <- reactive({
|
| 1148 |
req(rv_lme4$stability_table, rv_lme4$trait)
|
| 1149 |
rv_lme4$stability_table %>% filter(Trait == rv_lme4$trait, !is.na(Slope))
|
|
|
|
| 1154 |
req(lme4_filtered_blues())
|
| 1155 |
data <- lme4_filtered_blues()
|
| 1156 |
if (nrow(data) == 0 || all(is.na(data$predicted.value))) {
|
| 1157 |
+
return(plot_ly() %>%
|
| 1158 |
+
layout(
|
| 1159 |
+
title = list(text = paste("No Valid Predicted Means for", rv_lme4$trait), font = PLOT_FONT),
|
| 1160 |
+
annotations = list(
|
| 1161 |
+
text = "No valid data for plotting predicted means.",
|
| 1162 |
+
x = 0.5, y = 0.5, xref = "paper", yref = "paper",
|
| 1163 |
+
showarrow = FALSE, font = list(size = 12, color = "#2D3748")
|
| 1164 |
+
),
|
| 1165 |
+
PLOT_LAYOUT
|
| 1166 |
+
))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1167 |
}
|
| 1168 |
+
plot_ly(data, x = ~Environment, y = ~predicted.value, color = ~Genotype,
|
| 1169 |
+
type = 'scatter', mode = 'lines+markers',
|
| 1170 |
+
marker = list(size = 8),
|
| 1171 |
+
line = list(width = 2),
|
| 1172 |
+
colors = PLOT_COLORS,
|
| 1173 |
+
text = ~paste("Genotype:", Genotype, "<br>Value:", round(predicted.value, 2)),
|
| 1174 |
+
hoverinfo = "text") %>%
|
| 1175 |
+
layout(
|
| 1176 |
+
title = list(text = paste("Predicted Means (BLUEs) for", rv_lme4$trait), font = list(size = 18, family = "Poppins", color = "#2D3748")),
|
| 1177 |
+
xaxis = list(title = "Environment", tickangle = 45, titlefont = PLOT_FONT, tickfont = PLOT_FONT),
|
| 1178 |
+
yaxis = list(title = "Predicted Value", titlefont = PLOT_FONT, tickfont = PLOT_FONT),
|
| 1179 |
+
PLOT_LAYOUT,
|
| 1180 |
+
hoverlabel = list(bgcolor = "white", font = PLOT_FONT)
|
| 1181 |
+
)
|
| 1182 |
})
|
| 1183 |
|
| 1184 |
output$lme4_heritability_plot <- renderPlotly({
|
| 1185 |
+
req(rv_lme4$heritability_table)
|
| 1186 |
+
data <- rv_lme4$heritability_table %>% filter(!is.na(Heritability))
|
| 1187 |
+
if (nrow(data) == 0) {
|
| 1188 |
+
return(plot_ly() %>%
|
| 1189 |
+
layout(
|
| 1190 |
+
title = list(text = "No Valid Heritability Data", font = PLOT_FONT),
|
| 1191 |
+
annotations = list(
|
| 1192 |
+
text = paste("Heritability calculation failed for all traits.<br>Check debug log for details."),
|
| 1193 |
+
x = 0.5, y = 0.5, xref = "paper", yref = "paper",
|
| 1194 |
+
showarrow = FALSE, font = list(size = 12, color = "#2D3748")
|
| 1195 |
+
),
|
| 1196 |
+
PLOT_LAYOUT
|
| 1197 |
+
))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1198 |
}
|
| 1199 |
+
plot_ly(data, x = ~Trait, y = ~Heritability, type = 'bar',
|
| 1200 |
+
marker = list(color = PLOT_COLORS),
|
| 1201 |
+
text = ~paste("H²:", round(Heritability, 3)),
|
| 1202 |
+
hoverinfo = "text") %>%
|
| 1203 |
+
layout(
|
| 1204 |
+
title = list(text = "Broad-Sense Heritability Across All Traits", font = list(size = 18, family = "Poppins", color = "#2D3748")),
|
| 1205 |
+
xaxis = list(title = "Trait", tickangle = 45, titlefont = PLOT_FONT, tickfont = PLOT_FONT),
|
| 1206 |
+
yaxis = list(title = "Heritability (H²)", range = c(0, 1), titlefont = PLOT_FONT, tickfont = PLOT_FONT),
|
| 1207 |
+
PLOT_LAYOUT,
|
| 1208 |
+
showlegend = FALSE,
|
| 1209 |
+
hoverlabel = list(bgcolor = "white", font = PLOT_FONT)
|
| 1210 |
+
)
|
| 1211 |
})
|
| 1212 |
|
| 1213 |
output$lme4_correlation_plot <- renderPlotly({
|
| 1214 |
req(rv_lme4$cor_matrix)
|
| 1215 |
if (ncol(rv_lme4$cor_matrix) < 2 || all(is.na(rv_lme4$cor_matrix))) {
|
| 1216 |
+
return(plot_ly() %>%
|
| 1217 |
+
layout(
|
| 1218 |
+
title = list(text = "No Valid Correlation Data", font = PLOT_FONT),
|
| 1219 |
+
annotations = list(
|
| 1220 |
+
text = "Correlation plot requires at least two valid traits.",
|
| 1221 |
+
x = 0.5, y = 0.5, xref = "paper", yref = "paper",
|
| 1222 |
+
showarrow = FALSE, font = list(size = 12, color = "#2D3748")
|
| 1223 |
+
),
|
| 1224 |
+
PLOT_LAYOUT
|
| 1225 |
+
))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1226 |
}
|
| 1227 |
+
plot_ly(
|
| 1228 |
+
x = colnames(rv_lme4$cor_matrix),
|
| 1229 |
+
y = rownames(rv_lme4$cor_matrix),
|
| 1230 |
+
z = rv_lme4$cor_matrix,
|
| 1231 |
+
type = "heatmap",
|
| 1232 |
+
colorscale = list(c(-1, 0, 1), c("#FFB6C1", "white", "#B5EAD7")),
|
| 1233 |
+
showscale = TRUE,
|
| 1234 |
+
colorbar = list(title = "Correlation", titlefont = PLOT_FONT, tickfont = PLOT_FONT),
|
| 1235 |
+
text = ~round(rv_lme4$cor_matrix, 2),
|
| 1236 |
+
hoverinfo = "x+y+z"
|
| 1237 |
+
) %>%
|
| 1238 |
+
layout(
|
| 1239 |
+
title = list(text = "Trait Correlation Matrix", font = list(size = 18, family = "Poppins", color = "#2D3748")),
|
| 1240 |
+
xaxis = list(title = "", tickangle = 45, titlefont = PLOT_FONT, tickfont = PLOT_FONT),
|
| 1241 |
+
yaxis = list(title = "", titlefont = PLOT_FONT, tickfont = PLOT_FONT),
|
| 1242 |
+
PLOT_LAYOUT,
|
| 1243 |
+
hoverlabel = list(bgcolor = "white", font = PLOT_FONT)
|
| 1244 |
+
)
|
| 1245 |
})
|
| 1246 |
|
| 1247 |
output$lme4_stability_plot <- renderPlotly({
|
| 1248 |
req(lme4_filtered_stability())
|
| 1249 |
data <- lme4_filtered_stability()
|
| 1250 |
if (nrow(data) == 0 || all(is.na(data$Slope))) {
|
| 1251 |
+
return(plot_ly() %>%
|
| 1252 |
+
layout(
|
| 1253 |
+
title = list(text = paste("No Valid Stability Analysis for", rv_lme4$trait), font = PLOT_FONT),
|
| 1254 |
+
annotations = list(
|
| 1255 |
+
text = paste("Stability analysis failed for", rv_lme4$trait, ".<br>Check debug log for details."),
|
| 1256 |
+
x = 0.5, y = 0.5, xref = "paper", yref = "paper",
|
| 1257 |
+
showarrow = FALSE, font = list(size = 12, color = "#2D3748")
|
| 1258 |
+
),
|
| 1259 |
+
PLOT_LAYOUT
|
| 1260 |
+
))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1261 |
}
|
| 1262 |
+
plot_ly(data, x = ~Slope, y = ~R2, text = ~Genotype, type = 'scatter', mode = 'markers+text',
|
| 1263 |
+
marker = list(size = 12, color = PLOT_COLORS[1]),
|
| 1264 |
+
textposition = 'top',
|
| 1265 |
+
hoverinfo = "text",
|
| 1266 |
+
text = ~paste("Genotype:", Genotype, "<br>Slope:", round(Slope, 3), "<br>R²:", round(R2, 3))) %>%
|
| 1267 |
+
layout(
|
| 1268 |
+
title = list(text = paste("Finlay-Wilkinson Stability Analysis for", rv_lme4$trait), font = list(size = 18, family = "Poppins", color = "#2D3748")),
|
| 1269 |
+
xaxis = list(title = "Slope", titlefont = PLOT_FONT, tickfont = PLOT_FONT),
|
| 1270 |
+
yaxis = list(title = "R²", titlefont = PLOT_FONT, tickfont = PLOT_FONT, range = c(0, 1)),
|
| 1271 |
+
PLOT_LAYOUT,
|
| 1272 |
+
showlegend = FALSE,
|
| 1273 |
+
hoverlabel = list(bgcolor = "white", font = PLOT_FONT)
|
| 1274 |
+
)
|
| 1275 |
})
|
| 1276 |
|
| 1277 |
# LME4: Table outputs
|
| 1278 |
output$lme4_blues_table <- renderDT({
|
| 1279 |
req(lme4_filtered_blues())
|
| 1280 |
datatable(
|
| 1281 |
+
lme4_filtered_blues() %>% select(-Error),
|
| 1282 |
extensions = 'Buttons',
|
| 1283 |
+
options = list(
|
| 1284 |
+
scrollX = TRUE,
|
| 1285 |
+
dom = 'Bfrtip',
|
| 1286 |
+
buttons = c('copy', 'excel', 'pdf', 'print'),
|
| 1287 |
+
pageLength = 10,
|
| 1288 |
+
columnDefs = list(list(className = 'dt-center', targets = '_all'))
|
| 1289 |
+
),
|
| 1290 |
+
caption = paste("Predicted Means (BLUEs) for", rv_lme4$trait)
|
| 1291 |
+
) %>% formatRound(columns = c("predicted.value", "SE"), digits = 3)
|
| 1292 |
})
|
| 1293 |
|
| 1294 |
output$lme4_heritability_table <- renderDT({
|
| 1295 |
+
req(rv_lme4$heritability_table)
|
| 1296 |
datatable(
|
| 1297 |
+
rv_lme4$heritability_table,
|
| 1298 |
extensions = 'Buttons',
|
| 1299 |
+
options = list(
|
| 1300 |
+
scrollX = TRUE,
|
| 1301 |
+
dom = 'Bfrtip',
|
| 1302 |
+
buttons = c('copy', 'excel', 'pdf', 'print'),
|
| 1303 |
+
pageLength = 10,
|
| 1304 |
+
columnDefs = list(list(className = 'dt-center', targets = '_all'))
|
| 1305 |
+
),
|
| 1306 |
+
caption = "Broad-Sense Heritability for All Traits"
|
| 1307 |
+
) %>% formatRound(columns = "Heritability", digits = 3)
|
| 1308 |
})
|
| 1309 |
|
| 1310 |
output$lme4_cor_table <- renderDT({
|
|
|
|
| 1312 |
datatable(
|
| 1313 |
rv_lme4$cor_table,
|
| 1314 |
extensions = 'Buttons',
|
| 1315 |
+
options = list(
|
| 1316 |
+
scrollX = TRUE,
|
| 1317 |
+
dom = 'Bfrtip',
|
| 1318 |
+
buttons = c('copy', 'excel', 'pdf', 'print'),
|
| 1319 |
+
pageLength = 10,
|
| 1320 |
+
columnDefs = list(list(className = 'dt-center', targets = '_all'))
|
| 1321 |
+
),
|
| 1322 |
caption = "Correlation Between Traits"
|
| 1323 |
+
) %>% formatRound(columns = "Correlation", digits = 3)
|
| 1324 |
})
|
| 1325 |
|
| 1326 |
output$lme4_stability_table <- renderDT({
|
| 1327 |
req(lme4_filtered_stability())
|
| 1328 |
datatable(
|
| 1329 |
+
lme4_filtered_stability() %>% select(-Error),
|
| 1330 |
extensions = 'Buttons',
|
| 1331 |
+
options = list(
|
| 1332 |
+
scrollX = TRUE,
|
| 1333 |
+
dom = 'Bfrtip',
|
| 1334 |
+
buttons = c('copy', 'excel', 'pdf', 'print'),
|
| 1335 |
+
pageLength = 10,
|
| 1336 |
+
columnDefs = list(list(className = 'dt-center', targets = '_all'))
|
| 1337 |
+
),
|
| 1338 |
caption = paste("Stability Analysis for", rv_lme4$trait)
|
| 1339 |
+
) %>% formatRound(columns = c("Slope", "R2"), digits = 3)
|
| 1340 |
})
|
| 1341 |
}
|
| 1342 |
|