softwareDevelopment commited on
Commit
1ffaadc
·
verified ·
1 Parent(s): 4f8c6da

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +32 -13
app.R CHANGED
@@ -8,6 +8,7 @@
8
  # - Fallback to top SIM markers when stepwise yields < 2 cofactors
9
  # - Permutation threshold uses full method (not just SIM)
10
  # - QTL plot: idiogram + LOD curve + auto QTL labels (qTrait_ChrN)
 
11
  # ============================================================
12
 
13
  options(repos = c(CRAN = "https://cloud.r-project.org"))
@@ -107,20 +108,30 @@ estimate_effects <- function(geno_v, pheno_v) {
107
  if (has_het && length(unique(d_code)) > 1) {
108
  fit <- tryCatch(lm(pheno_v ~ a_code + d_code), error = function(e) NULL)
109
  if (!is.null(fit)) {
110
- cf <- coef(fit); se <- coef(summary(fit))
111
- a_eff <- cf["a_code"]; d_eff <- cf["d_code"]
112
- se_a <- if ("a_code" %in% rownames(se)) se["a_code", "Std. Error"] else NA_real_
113
- se_d <- if ("d_code" %in% rownames(se)) se["d_code", "Std. Error"] else NA_real_
 
 
 
 
114
  if (!is.na(a_eff) && abs(a_eff) > 1e-10) dom_ratio <- abs(d_eff / a_eff)
115
  dom_cl <- dom_class(a_eff, d_eff)
116
  }
117
  } else {
118
  fit <- tryCatch(lm(pheno_v ~ a_code), error = function(e) NULL)
119
  if (!is.null(fit)) {
120
- cf <- coef(fit); se <- coef(summary(fit))
 
121
  a_eff <- cf["a_code"]
122
- se_a <- if ("a_code" %in% rownames(se)) se["a_code", "Std. Error"] else NA_real_
123
- d_eff <- 0; se_d <- 0; dom_ratio <- 0; dom_cl <- "Additive"
 
 
 
 
 
124
  }
125
  }
126
  list(a_eff = a_eff, se_a = se_a,
@@ -506,7 +517,8 @@ qtl_scan_cim <- function(X, y, marker_info, n_cofactors = 10, window_size = 10)
506
  lod <- calc_lod(fit_full, fit_null)
507
  pve <- calc_pve(fit_full, fit_null)
508
 
509
- ct <- coef(summary(fit_full))
 
510
  if (!"focal" %in% rownames(ct)) next
511
  pval <- ct["focal", "Pr(>|t|)"]
512
  eff <- estimate_effects(gv, pv)
@@ -672,7 +684,8 @@ qtl_scan_mqm <- function(X, y, marker_info, n_cofactors = 10, window_size = 10)
672
  lod <- calc_lod(fit_full, fit_null)
673
  pve <- calc_pve(fit_full, fit_null)
674
 
675
- ct <- coef(summary(fit_full))
 
676
  if (!"focal" %in% rownames(ct)) next
677
  pval <- ct["focal", "Pr(>|t|)"]
678
  eff <- estimate_effects(gv, pv)
@@ -866,11 +879,11 @@ qtl_scan_rf <- function(X, y, marker_info) {
866
  }
867
 
868
  # ============================================================
869
- # DISPATCHER
870
  # ============================================================
871
  qtl_scan <- function(X, y, marker_info, method = "CIM",
872
  n_cofactors = 10, window_size = 10) {
873
- cat("\n" + paste(rep("=", 60), collapse = ""), "\n")
874
  cat("QTL SCAN | Method:", method, "\n")
875
  cat(paste(rep("=", 60), collapse = ""), "\n")
876
 
@@ -1063,6 +1076,7 @@ make_chr_lod_plot <- function(scan_df, marker_info, lod_threshold = 2.0,
1063
  }))
1064
 
1065
  global_lod_max <- max(pd$LOD, na.rm = TRUE) * 1.15
 
1066
 
1067
  # Build per-chromosome panels
1068
  plot_list <- lapply(chroms, function(ch) {
@@ -1070,6 +1084,7 @@ make_chr_lod_plot <- function(scan_df, marker_info, lod_threshold = 2.0,
1070
  ch_pd <- ch_pd[order(ch_pd$Position), ]
1071
  ci <- chr_info[chr_info$Chromosome == ch, ]
1072
  chr_len <- ci$chr_end - ci$chr_start
 
1073
  ch_sig <- ch_pd[ch_pd$LOD >= lod_threshold & nchar(ch_pd$QTL_Label) > 0, ]
1074
 
1075
  chr_color <- "#4DB6AC" # Teal color
@@ -1298,6 +1313,7 @@ make_idiogram_ggplot <- function(qtl_list, all_markers_df, lod_threshold = 2.0)
1298
  }))
1299
 
1300
  max_pos <- max(all_markers_df$Position, na.rm = TRUE)
 
1301
 
1302
  p <- ggplot() +
1303
  geom_rect(data = chr_summary,
@@ -1431,7 +1447,7 @@ calc_selection_metrics <- function(gmat, pop_df, marker_info) {
1431
  }
1432
 
1433
  # ============================================================
1434
- # UI - Simplified for space (full UI from v10 with updated description)
1435
  # ============================================================
1436
  ui <- fluidPage(
1437
  shinyjs::useShinyjs(),
@@ -1778,7 +1794,10 @@ ui <- fluidPage(
1778
  tags$span(class = "fix-badge", "CIM FIX #3"),
1779
  " Fallback to top SIM markers if stepwise yields < 2 cofactors", tags$br(),
1780
  tags$span(class = "fix-badge", "QTL PLOT"),
1781
- " Per-chromosome idiogram + LOD curve with auto QTL labels"
 
 
 
1782
  ),
1783
  tags$h5(style = "margin-top:18px;font-weight:600;color:#3A86FF;", "References"),
1784
  tags$ul(
 
8
  # - Fallback to top SIM markers when stepwise yields < 2 cofactors
9
  # - Permutation threshold uses full method (not just SIM)
10
  # - QTL plot: idiogram + LOD curve + auto QTL labels (qTrait_ChrN)
11
+ # - FIXED: string concatenation error in qtl_scan dispatcher
12
  # ============================================================
13
 
14
  options(repos = c(CRAN = "https://cloud.r-project.org"))
 
108
  if (has_het && length(unique(d_code)) > 1) {
109
  fit <- tryCatch(lm(pheno_v ~ a_code + d_code), error = function(e) NULL)
110
  if (!is.null(fit)) {
111
+ cf <- coef(fit)
112
+ se <- tryCatch(coef(summary(fit)), error = function(e) NULL)
113
+ a_eff <- cf["a_code"]
114
+ d_eff <- cf["d_code"]
115
+ if (!is.null(se)) {
116
+ se_a <- if ("a_code" %in% rownames(se)) se["a_code", "Std. Error"] else NA_real_
117
+ se_d <- if ("d_code" %in% rownames(se)) se["d_code", "Std. Error"] else NA_real_
118
+ }
119
  if (!is.na(a_eff) && abs(a_eff) > 1e-10) dom_ratio <- abs(d_eff / a_eff)
120
  dom_cl <- dom_class(a_eff, d_eff)
121
  }
122
  } else {
123
  fit <- tryCatch(lm(pheno_v ~ a_code), error = function(e) NULL)
124
  if (!is.null(fit)) {
125
+ cf <- coef(fit)
126
+ se <- tryCatch(coef(summary(fit)), error = function(e) NULL)
127
  a_eff <- cf["a_code"]
128
+ if (!is.null(se)) {
129
+ se_a <- if ("a_code" %in% rownames(se)) se["a_code", "Std. Error"] else NA_real_
130
+ }
131
+ d_eff <- 0
132
+ se_d <- 0
133
+ dom_ratio <- 0
134
+ dom_cl <- "Additive"
135
  }
136
  }
137
  list(a_eff = a_eff, se_a = se_a,
 
517
  lod <- calc_lod(fit_full, fit_null)
518
  pve <- calc_pve(fit_full, fit_null)
519
 
520
+ ct <- tryCatch(coef(summary(fit_full)), error = function(e) NULL)
521
+ if (is.null(ct)) next
522
  if (!"focal" %in% rownames(ct)) next
523
  pval <- ct["focal", "Pr(>|t|)"]
524
  eff <- estimate_effects(gv, pv)
 
684
  lod <- calc_lod(fit_full, fit_null)
685
  pve <- calc_pve(fit_full, fit_null)
686
 
687
+ ct <- tryCatch(coef(summary(fit_full)), error = function(e) NULL)
688
+ if (is.null(ct)) next
689
  if (!"focal" %in% rownames(ct)) next
690
  pval <- ct["focal", "Pr(>|t|)"]
691
  eff <- estimate_effects(gv, pv)
 
879
  }
880
 
881
  # ============================================================
882
+ # DISPATCHER - FIXED: removed incorrect + operator
883
  # ============================================================
884
  qtl_scan <- function(X, y, marker_info, method = "CIM",
885
  n_cofactors = 10, window_size = 10) {
886
+ cat("\n", paste(rep("=", 60), collapse = ""), "\n")
887
  cat("QTL SCAN | Method:", method, "\n")
888
  cat(paste(rep("=", 60), collapse = ""), "\n")
889
 
 
1076
  }))
1077
 
1078
  global_lod_max <- max(pd$LOD, na.rm = TRUE) * 1.15
1079
+ if (is.na(global_lod_max) || global_lod_max <= 0) global_lod_max <- 5
1080
 
1081
  # Build per-chromosome panels
1082
  plot_list <- lapply(chroms, function(ch) {
 
1084
  ch_pd <- ch_pd[order(ch_pd$Position), ]
1085
  ci <- chr_info[chr_info$Chromosome == ch, ]
1086
  chr_len <- ci$chr_end - ci$chr_start
1087
+ if (chr_len <= 0) chr_len <- 100
1088
  ch_sig <- ch_pd[ch_pd$LOD >= lod_threshold & nchar(ch_pd$QTL_Label) > 0, ]
1089
 
1090
  chr_color <- "#4DB6AC" # Teal color
 
1313
  }))
1314
 
1315
  max_pos <- max(all_markers_df$Position, na.rm = TRUE)
1316
+ if (is.na(max_pos)) max_pos <- 100
1317
 
1318
  p <- ggplot() +
1319
  geom_rect(data = chr_summary,
 
1447
  }
1448
 
1449
  # ============================================================
1450
+ # UI - Complete UI (same as v10 but with updated version)
1451
  # ============================================================
1452
  ui <- fluidPage(
1453
  shinyjs::useShinyjs(),
 
1794
  tags$span(class = "fix-badge", "CIM FIX #3"),
1795
  " Fallback to top SIM markers if stepwise yields < 2 cofactors", tags$br(),
1796
  tags$span(class = "fix-badge", "QTL PLOT"),
1797
+ " Per-chromosome idiogram + LOD curve with auto QTL labels",
1798
+ tags$br(),
1799
+ tags$span(class = "fix-badge", "BUG FIX"),
1800
+ " Fixed string concatenation error in qtl_scan dispatcher"
1801
  ),
1802
  tags$h5(style = "margin-top:18px;font-weight:600;color:#3A86FF;", "References"),
1803
  tags$ul(