International Rice Research Institute commited on
Commit
94bb606
·
verified ·
1 Parent(s): 78aa911

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +60 -46
app.R CHANGED
@@ -115,15 +115,39 @@ ui <- fluidPage(
115
  )
116
  ))
117
  )),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  tabPanel("Visualization", icon = icon("chart-bar"), fluidRow(
119
  column(12, wellPanel(
120
  h3("Chromosome Map Visualization", icon("image")),
121
  conditionalPanel(
122
  condition = "output.analysis_run",
123
  selectInput("chromosome_select", "Select Chromosomes:", choices = NULL, multiple = TRUE),
124
- #checkboxInput("show_density", "Show Marker Density", value = FALSE),
125
- sliderInput("marker_label_size", "Marker Label Size:", min = 2, max = 10, value = 3, step = 0.5),
126
- sliderInput("plot_height", "Plot Height (px):", min = 400, max = 1200, value = 600, step = 50),
127
  downloadButton("download_plot", "Download Plot as PNG", class = "btn-success"),
128
  br(), br(),
129
  plotOutput("chromosome_plot", height = "auto")
@@ -427,16 +451,11 @@ server <- function(input, output, session) {
427
  return(list(original_data = df, recombination_data = map_distance_df, linkage_map = scaled_results$scaled_map, chrom_lengths_before = scaled_results$chrom_lengths_before, chrom_lengths_after = scaled_results$chrom_lengths_after))
428
  }
429
 
430
- create_multi_chromosome_plot <- function(linkage_data, selected_chromosomes,
431
- label_size = 3, show_density = FALSE,
432
- plot_width = 1200, plot_height = 800,
433
- chrom_width = 0.5, point_size = 2,
434
- chrom_spacing = 5.0) {
435
  library(ggplot2)
436
  library(dplyr)
437
  library(RColorBrewer)
438
 
439
- # Filter data
440
  plot_data <- linkage_data %>%
441
  filter(Chromosome %in% selected_chromosomes, !is.na(Position)) %>%
442
  arrange(Chromosome, Position)
@@ -449,7 +468,6 @@ server <- function(input, output, session) {
449
  theme_void())
450
  }
451
 
452
- # Chromosome summary
453
  chrom_stats <- plot_data %>%
454
  group_by(Chromosome) %>%
455
  summarise(
@@ -467,29 +485,23 @@ server <- function(input, output, session) {
467
  plot_data <- plot_data %>%
468
  left_join(chrom_stats %>% select(Chromosome, chrom_x, chrom_center), by = "Chromosome")
469
 
470
- # Distinct colors
471
  n_chrom <- nrow(chrom_stats)
472
  chrom_colors <- if (n_chrom <= 8) {
473
- brewer.pal(max(3, n_chrom), "Set2")
474
  } else {
475
- colorRampPalette(brewer.pal(20, "Set2"))(n_chrom)
476
  }
477
 
478
- # Build plot
479
  p <- ggplot() +
480
-
481
- # Chromosome lines (vertical lines instead of rectangles)
482
  geom_segment(
483
  data = chrom_stats,
484
  aes(x = chrom_center, xend = chrom_center,
485
  y = min_pos, yend = max_pos,
486
  color = as.factor(Chromosome)),
487
- size = chrom_width * 20, # Thicker line for chromosome
488
  alpha = 0.7,
489
  lineend = "round"
490
  ) +
491
-
492
- # Marker points (instead of ticks)
493
  geom_point(
494
  data = plot_data,
495
  aes(x = chrom_center, y = Position,
@@ -497,9 +509,7 @@ server <- function(input, output, session) {
497
  size = point_size,
498
  alpha = 0.8
499
  ) +
500
-
501
- # Optional: Add marker names if available
502
- {if("Marker" %in% names(plot_data))
503
  geom_text(
504
  data = plot_data,
505
  aes(x = chrom_center + chrom_width * 0.6,
@@ -513,8 +523,6 @@ server <- function(input, output, session) {
513
  alpha = 0.7
514
  )
515
  } +
516
-
517
- # Chromosome labels at the top
518
  geom_text(
519
  data = chrom_stats,
520
  aes(x = chrom_center, y = max_pos * 1.05,
@@ -524,22 +532,18 @@ server <- function(input, output, session) {
524
  fontface = "bold",
525
  vjust = 0
526
  ) +
527
-
528
  scale_color_manual(values = chrom_colors) +
529
  scale_fill_manual(values = chrom_colors) +
530
-
531
  scale_y_reverse(
532
  name = "Genetic Position (cM)",
533
- expand = expansion(mult = c(0.05, 0.1)) # More space at top for labels
534
  ) +
535
-
536
  scale_x_continuous(
537
  breaks = chrom_stats$chrom_center,
538
  labels = chrom_stats$Chromosome,
539
  name = "Chromosome",
540
  expand = expansion(mult = 0.1)
541
  ) +
542
-
543
  theme_minimal() +
544
  theme(
545
  legend.position = "none",
@@ -554,28 +558,17 @@ server <- function(input, output, session) {
554
  plot.margin = margin(20, 20, 20, 20)
555
  )
556
 
557
- # Add density plot if requested
558
- if (show_density) {
559
- p <- p +
560
- geom_density_2d_filled(
561
- data = plot_data,
562
- aes(x = chrom_center, y = Position),
563
- alpha = 0.2,
564
- show.legend = FALSE
565
- )
566
- }
567
-
568
  return(p)
569
  }
570
 
571
-
572
- create_single_chromosome_plot <- function(linkage_data, selected_chromosome, label_size = 3) {
573
  plot_data <- linkage_data %>% filter(Chromosome == selected_chromosome, !is.na(Position)) %>% arrange(Position)
574
  if (nrow(plot_data) == 0) return(ggplot() + annotate("text", x = 0.5, y = 0.5, label = paste("No valid marker positions available for Chromosome", selected_chromosome), size = 6, color = "red") + theme_void())
 
575
  p <- ggplot(plot_data, aes(x = Position, y = 1)) +
576
- geom_segment(aes(x = 0, xend = max(Position, na.rm = TRUE), y = 1, yend = 1), color = "darkblue", size = 2) +
577
  geom_point(aes(x = Position, y = 1), color = "red", size = 3) +
578
- geom_text_repel(aes(label = Marker), size = label_size, box.padding = 0.5, segment.color = "gray") +
579
  labs(title = paste("Chromosome", selected_chromosome, "Linkage Map"), x = "Position (cM)", y = "") +
580
  theme_minimal() +
581
  theme(
@@ -590,6 +583,25 @@ server <- function(input, output, session) {
590
  return(p)
591
  }
592
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
593
  observeEvent(input$run_analysis, {
594
  req(uploaded_data())
595
  showModal(modalDialog(title = "Processing", "Creating and scaling linkage map... This may take a few moments.", footer = NULL))
@@ -672,7 +684,8 @@ server <- function(input, output, session) {
672
  linkage_map,
673
  input$chromosome_select,
674
  label_size = input$marker_label_size,
675
- #show_density = input$show_density,
 
676
  plot_width = input$plot_width,
677
  plot_height = input$plot_height
678
  )
@@ -702,7 +715,8 @@ server <- function(input, output, session) {
702
  linkage_map,
703
  input$chromosome_select,
704
  label_size = input$marker_label_size,
705
- show_density = input$show_density,
 
706
  plot_width = input$plot_width,
707
  plot_height = input$plot_height
708
  )
 
115
  )
116
  ))
117
  )),
118
+ tabPanel("Plot Customization", icon = icon("palette"), sidebarLayout(
119
+ sidebarPanel(width = 4, wellPanel(
120
+ h3("Plot Customization", icon("paint-brush")),
121
+ selectInput("color_palette", "Color Palette:",
122
+ choices = c("Set1", "Set2", "Set3", "Paired", "Dark2", "Accent",
123
+ "Blues", "Reds", "Greens", "Purples", "Oranges",
124
+ "Greys", "Spectral", "RdBu"),
125
+ selected = "Set2"),
126
+ checkboxInput("show_labels", "Show Marker Labels", value = TRUE),
127
+ sliderInput("marker_label_size", "Marker Label Size:", min = 2, max = 10, value = 3, step = 0.5),
128
+ sliderInput("plot_width", "Plot Width (px):", min = 600, max = 1600, value = 1200, step = 50),
129
+ sliderInput("plot_height", "Plot Height (px):", min = 400, max = 1200, value = 600, step = 50),
130
+ helpText("Select a color palette and adjust plot settings. Preview updates in the Visualization tab.")
131
+ )),
132
+ mainPanel(width = 8, wellPanel(
133
+ h3("Color Palette Preview"),
134
+ p("The selected palette will be applied to chromosomes in the visualization. Below is a preview of the colors:"),
135
+ plotOutput("palette_preview", height = "200px"),
136
+ h4("Available Palettes"),
137
+ tags$ul(
138
+ tags$li(tags$b("Set1, Set2, Set3, Paired, Dark2, Accent:"), "Qualitative palettes for distinct categories"),
139
+ tags$li(tags$b("Blues, Reds, Greens, Purples, Oranges, Greys:"), "Sequential palettes for gradients"),
140
+ tags$li(tags$b("Spectral, RdBu:"), "Diverging palettes for contrasting values"),
141
+ tags$li(tags$b("Viridis, Magma, Inferno, Plasma:"), "Perceptually uniform sequential palettes")
142
+ )
143
+ ))
144
+ )),
145
  tabPanel("Visualization", icon = icon("chart-bar"), fluidRow(
146
  column(12, wellPanel(
147
  h3("Chromosome Map Visualization", icon("image")),
148
  conditionalPanel(
149
  condition = "output.analysis_run",
150
  selectInput("chromosome_select", "Select Chromosomes:", choices = NULL, multiple = TRUE),
 
 
 
151
  downloadButton("download_plot", "Download Plot as PNG", class = "btn-success"),
152
  br(), br(),
153
  plotOutput("chromosome_plot", height = "auto")
 
451
  return(list(original_data = df, recombination_data = map_distance_df, linkage_map = scaled_results$scaled_map, chrom_lengths_before = scaled_results$chrom_lengths_before, chrom_lengths_after = scaled_results$chrom_lengths_after))
452
  }
453
 
454
+ create_multi_chromosome_plot <- function(linkage_data, selected_chromosomes, label_size = 3, show_labels = TRUE, color_palette = "Set2", plot_width = 1200, plot_height = 800, chrom_width = 0.5, point_size = 2, chrom_spacing = 5.0) {
 
 
 
 
455
  library(ggplot2)
456
  library(dplyr)
457
  library(RColorBrewer)
458
 
 
459
  plot_data <- linkage_data %>%
460
  filter(Chromosome %in% selected_chromosomes, !is.na(Position)) %>%
461
  arrange(Chromosome, Position)
 
468
  theme_void())
469
  }
470
 
 
471
  chrom_stats <- plot_data %>%
472
  group_by(Chromosome) %>%
473
  summarise(
 
485
  plot_data <- plot_data %>%
486
  left_join(chrom_stats %>% select(Chromosome, chrom_x, chrom_center), by = "Chromosome")
487
 
 
488
  n_chrom <- nrow(chrom_stats)
489
  chrom_colors <- if (n_chrom <= 8) {
490
+ brewer.pal(max(3, n_chrom), color_palette)
491
  } else {
492
+ colorRampPalette(brewer.pal(8, color_palette))(n_chrom)
493
  }
494
 
 
495
  p <- ggplot() +
 
 
496
  geom_segment(
497
  data = chrom_stats,
498
  aes(x = chrom_center, xend = chrom_center,
499
  y = min_pos, yend = max_pos,
500
  color = as.factor(Chromosome)),
501
+ size = chrom_width * 20,
502
  alpha = 0.7,
503
  lineend = "round"
504
  ) +
 
 
505
  geom_point(
506
  data = plot_data,
507
  aes(x = chrom_center, y = Position,
 
509
  size = point_size,
510
  alpha = 0.8
511
  ) +
512
+ {if (show_labels && "Marker" %in% names(plot_data))
 
 
513
  geom_text(
514
  data = plot_data,
515
  aes(x = chrom_center + chrom_width * 0.6,
 
523
  alpha = 0.7
524
  )
525
  } +
 
 
526
  geom_text(
527
  data = chrom_stats,
528
  aes(x = chrom_center, y = max_pos * 1.05,
 
532
  fontface = "bold",
533
  vjust = 0
534
  ) +
 
535
  scale_color_manual(values = chrom_colors) +
536
  scale_fill_manual(values = chrom_colors) +
 
537
  scale_y_reverse(
538
  name = "Genetic Position (cM)",
539
+ expand = expansion(mult = c(0.05, 0.1))
540
  ) +
 
541
  scale_x_continuous(
542
  breaks = chrom_stats$chrom_center,
543
  labels = chrom_stats$Chromosome,
544
  name = "Chromosome",
545
  expand = expansion(mult = 0.1)
546
  ) +
 
547
  theme_minimal() +
548
  theme(
549
  legend.position = "none",
 
558
  plot.margin = margin(20, 20, 20, 20)
559
  )
560
 
 
 
 
 
 
 
 
 
 
 
 
561
  return(p)
562
  }
563
 
564
+ create_single_chromosome_plot <- function(linkage_data, selected_chromosome, label_size = 3, show_labels = TRUE, color_palette = "Set2") {
 
565
  plot_data <- linkage_data %>% filter(Chromosome == selected_chromosome, !is.na(Position)) %>% arrange(Position)
566
  if (nrow(plot_data) == 0) return(ggplot() + annotate("text", x = 0.5, y = 0.5, label = paste("No valid marker positions available for Chromosome", selected_chromosome), size = 6, color = "red") + theme_void())
567
+ chrom_color <- brewer.pal(3, color_palette)[1]
568
  p <- ggplot(plot_data, aes(x = Position, y = 1)) +
569
+ geom_segment(aes(x = 0, xend = max(Position, na.rm = TRUE), y = 1, yend = 1), color = chrom_color, size = 2) +
570
  geom_point(aes(x = Position, y = 1), color = "red", size = 3) +
571
+ {if (show_labels) geom_text_repel(aes(label = Marker), size = label_size, box.padding = 0.5, segment.color = "gray")} +
572
  labs(title = paste("Chromosome", selected_chromosome, "Linkage Map"), x = "Position (cM)", y = "") +
573
  theme_minimal() +
574
  theme(
 
583
  return(p)
584
  }
585
 
586
+ output$palette_preview <- renderPlot({
587
+ req(input$color_palette)
588
+ colors <- brewer.pal(8, input$color_palette)
589
+ n <- length(colors)
590
+ df <- data.frame(x = 1:n, y = rep(1, n), color = colors)
591
+ ggplot(df, aes(x = x, y = y, fill = I(color))) +
592
+ geom_tile(width = 0.9, height = 0.9) +
593
+ scale_x_continuous(breaks = 1:n, labels = 1:n) +
594
+ labs(title = paste("Preview of", input$color_palette, "Palette")) +
595
+ theme_minimal() +
596
+ theme(
597
+ axis.text.y = element_blank(),
598
+ axis.ticks.y = element_blank(),
599
+ axis.title = element_blank(),
600
+ panel.grid = element_blank(),
601
+ plot.title = element_text(hjust = 0.5, size = 14, face = "bold")
602
+ )
603
+ }, height = 200)
604
+
605
  observeEvent(input$run_analysis, {
606
  req(uploaded_data())
607
  showModal(modalDialog(title = "Processing", "Creating and scaling linkage map... This may take a few moments.", footer = NULL))
 
684
  linkage_map,
685
  input$chromosome_select,
686
  label_size = input$marker_label_size,
687
+ show_labels = input$show_labels,
688
+ color_palette = input$color_palette,
689
  plot_width = input$plot_width,
690
  plot_height = input$plot_height
691
  )
 
715
  linkage_map,
716
  input$chromosome_select,
717
  label_size = input$marker_label_size,
718
+ show_labels = input$show_labels,
719
+ color_palette = input$color_palette,
720
  plot_width = input$plot_width,
721
  plot_height = input$plot_height
722
  )