Spaces:
Sleeping
Sleeping
File size: 34,639 Bytes
966efc7 6d06986 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 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 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 |
---
title: "Bioinformatics Dashboard v0.0 🧬 🦠 🧫"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
if (!requireNamespace("pacman", quietly = TRUE)) {
install.packages("pacman")
}
pacman::p_load(flexdashboard, shiny, ggplot2, plotly, clusterProfiler, readxl, tidyverse, DESeq2, biomaRt, tidyr, shinyjs, rentrez, dplyr, ggtext, cowplot, UpSetR)
library(flexdashboard)
library(shiny)
library(ggplot2)
library(plotly)
library(clusterProfiler)
library(readxl)
library(tidyverse)
library(DESeq2)
library(biomaRt)
library(tidyr)
library(shinyjs)
library(rentrez)
library(dplyr)
library(ggtext)
library(cowplot)
library(UpSetR)
```
Column {.tabset}
-----------------------------------------------------------------------
### RNAseq analysis
Analyzes RNAseq data using DESeq2 and GSEA. Visualizes using volcano plot, and other plots to show the GSEA analysis results. Please make sure the uploaded data is in xlsx format, has the first column with the gene names, and there is an even number of data columns. The control condition should be first, the mutant condition second.
```{r}
ui <- fluidPage(
titlePanel("Interactive Volcano Plot with Gene and GO Term Search"),
useShinyjs(),
passwordInput("password", "Enter Password:", value = "", placeholder = "Password"),
actionButton("submit_password", "Submit"),
uiOutput("main_ui"),
#if you define a side_ui --> absolute panel, it would have to be defined here
)
# Server logic
server <- function(input, output, session) {
# Password handling
correct_password <- "my_secret_password"
observeEvent(input$submit_password, {
if (input$password == correct_password) {
showModal(modalDialog(
title = "Access Granted",
"Welcome! You can now search for genes and view the volcano plot.",
easyClose = TRUE,
footer = NULL
))
# Hide the password input and button after validation
shinyjs::hide("password")
shinyjs::hide("submit_password")
# Main UI appears after password is correct
output$main_ui <- renderUI({
sidebarLayout(
sidebarPanel(
fluidRow(
fileInput("file", "Choose XLSX File", multiple = FALSE, accept = c(".xlsx", "text/xlsx")),
actionButton("analyze_button", "Analyze"),
tags$hr(),
textInput("gene_search", "Search for a gene or keyword (separate multiple genes with ';'):", ""),
actionButton("search_gene", "Search Gene"),
tags$hr(),
selectInput("GO_search", "Select a GO term:", choices = NULL),
actionButton("search_GO_term", "Search GO Term"),
tags$hr(),
selectInput("description_search", "Search for the name of a pathway:", choices = NULL),
actionButton("search_description", "Search Description"),
tags$hr(),
sliderInput("pvalue", "P-value: ",
min = 0, max = 1,
value = 0.01, step = 0.00001),
tags$hr(),
sliderInput("log2fc", "Log2FoldChange: ",
min = 0.0001, max = 100,
value = 2.5, step = 0.05),
tags$hr(),
actionButton("visualize_gse", "Visualize the GSEGO Results:")
)
),
mainPanel(
plotlyOutput("volcanoPlot"),
plotOutput("dotPlotTitle", width = "100%", height = "100px"),
plotOutput("dotPlot", width = "100%", height = "1000px"),
plotOutput("conceptNetworkTitle", width = "100%", height = "100px"),
plotOutput("conceptNetwork", width = "100%", height = "600px"),
plotOutput("heatMapTitle", width = "100%", height = "100px"),
plotOutput("heatMap", width = "100%", height = "400px"),
plotOutput("upsetPlotTitle", width = "100%", height = "100px"),
plotOutput("upsetPlot", width = "100%", height = "1000px"),
plotOutput("pubmedPathwayPlotTitle", width = "100%", height = "100px"),
plotOutput("pubmedPathwayPlot", width = "100%", height = "1500px")
)
)
})
# Reactive values to store results and search criteria
searchValues <- reactiveValues(
gene_search = "",
GO_search = "All",
description_search = "",
df_inverted = NULL # Store df_inverted here
)
# Process uploaded file and perform DESeq2 analysis
observeEvent(input$analyze_button, {
req(input$file) # Ensure a file is uploaded
# Read in gene counts data
genecounts <- tryCatch({
read_excel(input$file$datapath, sheet = 1, col_names = TRUE)
}, error = function(e) {
showModal(modalDialog(title = "Error", "Could not read the Excel file.", easyClose = TRUE))
return(NULL)
})
if (is.null(genecounts)) return(NULL) # Stop further processing if reading failed
genecounts <- as.data.frame(genecounts)
rownames(genecounts) <- genecounts[, 1]
genecounts$Gene_Name <- NULL
genecounts <- genecounts[, -1]
num_samples <- ncol(genecounts)
# Check if the number of samples is even
if (num_samples %% 2 != 0) {
showModal(modalDialog(
title = "Error",
"The number of samples must be even for proper grouping.",
easyClose = TRUE
))
return(NULL)
}
# Create the condition data frame
condition <- data.frame(genotype = rep(c('C', 'R'), each = num_samples / 2), row.names = colnames(genecounts))
# Create DESeq2 dataset
dds <- DESeqDataSetFromMatrix(countData = genecounts, colData = condition, design = ~genotype)
de <- DESeq(dds)
res_reactive <- reactiveVal()
res_reactive(results(de))
res <<- results(de)
# Create additional columns for plotting
res$pvalue_log10 <- -log10(res$pvalue)
pvalue_threshold <- 0.05
fold_change_threshold <- 2
res$significance <- ifelse(res$pvalue < pvalue_threshold, "Significant", "Not Significant")
res$new_column <- rownames(res)
res$diffexpressed <- ifelse(res$log2FoldChange > 0, "UP", ifelse(res$log2FoldChange < 0, "DOWN", "NO_CHANGE"))
# Generate gene list for GSEA
organism = "org.Hs.eg.db"
original_gene_list <- res$log2FoldChange
names(original_gene_list) <- res$new_column
gene_list <<- na.omit(original_gene_list)
gene_list = sort(gene_list, decreasing = TRUE)
# Perform GO enrichment analysis
gse <<- gseGO(geneList = gene_list,
ont = "ALL",
keyType = "SYMBOL",
minGSSize = 3,
maxGSSize = 800,
pvalueCutoff = 0.05,
verbose = TRUE,
OrgDb = organism,
pAdjustMethod = "none")
# Store inverted results for GO terms in reactive values
searchValues$df_inverted <- gse@result %>% separate_rows(core_enrichment, sep = "/")
# Update GO term and description choices in UI
updateSelectInput(session, "GO_search", choices = unique(searchValues$df_inverted$ID))
updateSelectInput(session, "description_search", choices = unique(searchValues$df_inverted$Description))
# Reactive filtering of results based on user input
filteredRes <- reactive({
data <- as.data.frame(res)
# Apply gene search filter
if (searchValues$gene_search != "") {
genes <- strsplit(searchValues$gene_search, ";")[[1]]
genes <- trimws(genes)
data <- data %>%
filter(rowSums(sapply(genes, function(gene) grepl(gene, new_column, ignore.case = TRUE))) > 0)
}
# Apply GO term filter
if (searchValues$GO_search != "All") {
selected_genes <- searchValues$df_inverted %>%
filter(ID == searchValues$GO_search) %>%
pull(core_enrichment)
data <- data %>%
filter(new_column %in% selected_genes)
}
# Apply description search filter
if (searchValues$description_search != "") {
selected_genes <- searchValues$df_inverted %>%
filter(Description == searchValues$description_search) %>%
pull(core_enrichment)
data <- data %>%
filter(new_column %in% selected_genes)
}
data
})
# Render volcano plot based on filtered results
output$volcanoPlot <- renderPlotly({
data_res <- filteredRes()
p_value <- input$pvalue #need to add slides here
log2fc <- input$log2fc #need to add slider here
p <- ggplot(data_res, aes(x = log2FoldChange, y = pvalue_log10,
text = paste("Gene:", new_column, "<br>Log2 Fold Change:", log2FoldChange,
"<br>P-value:", pvalue, "<br>Significance:", significance,
"<br>Differentially Expressed:", diffexpressed, "<br>-log10 Values:", pvalue_log10))) +
geom_point(aes(color = log2FoldChange, shape = diffexpressed)) +
geom_hline(yintercept = -log10(p_value), linetype = "dotted", color = "red") +
geom_vline(xintercept = c(-log2fc, log2fc), linetype = "dotted", color = "darkblue") +
xlim(-5, 5) +
xlab("Log2 Fold Change") +
ylab("-log10(P-value)") +
ggtitle("Volcano Plot") +
scale_color_gradient2(low = "green", mid = "pink", high = "blue", midpoint = 0,
name = "Log2 Fold Change") # Add a custom color scale for the color legends
ggplotly(p, tooltip = "text")
})
})
# Update search criteria based on user actions
observeEvent(input$search_gene, {
searchValues$gene_search <- input$gene_search
searchValues$GO_search <- "All"
searchValues$description_search <- ""
})
observeEvent(input$search_GO_term, {
searchValues$GO_search <- input$GO_search
searchValues$gene_search <- ""
searchValues$description_search <- ""
})
observeEvent(input$search_description, {
searchValues$description_search <- input$description_search
searchValues$GO_search <- "All"
searchValues$gene_search <- ""
})
observeEvent(input$visualize_gse, {
library(ggplot2)
library(ggtext)
library(gridExtra)
output$dotPlotTitle <- renderPlot({
txt <- "Dot Plot with 10 Pathways"
title_plot <- ggplot() +
geom_textbox(
aes(x = 0, y = 0, label = txt),
size = 18 / .pt,
width = unit(6, "inches")
) +
theme_void()
print(title_plot)
})
output$dotPlot <- renderPlot({
dot_plot <- dotplot(gse, showCategory = 10)
print(dot_plot)
})
output$conceptNetworkTitle <- renderPlot({
txt <- "Gene Concept Network"
title_plot <- ggplot() +
geom_textbox(
aes(x = 0, y = 0, label = txt),
size = 18 / .pt,
width = unit(6, "inches")
) +
theme_void()
print(title_plot)
})
output$conceptNetwork <- renderPlot({
gsex <- setReadable(gse, 'org.Hs.eg.db', 'ENTREZID')
geneList <- gse@geneList
p1 <- cnetplot(gsex, foldChange = geneList, max.overlaps = 100)
p2 <- cnetplot(gsex, categorySize = "pvalue", foldChange = geneList, max.overlaps = 100)
p3 <- cnetplot(gsex, foldChange = geneList, circular = TRUE, colorEdge = TRUE, max.overlaps = 100)
maingene_plot <- cowplot::plot_grid(p1, p2, p3, ncol = 3, labels = LETTERS[1:3], rel_widths = c(.8, .8, 1.2))
print(maingene_plot)
})
output$heatMapTitle <- renderPlot({
txt <- "Heatmap-Like Functional Classification"
title_plot <- ggplot() +
geom_textbox(
aes(x = 0, y = 0, label = txt),
size = 18 / .pt,
width = unit(6, "inches")
) +
theme_void()
print(title_plot)
})
output$heatMap <- renderPlot({
gsex <- setReadable(gse, 'org.Hs.eg.db', 'ENTREZID')
geneList <- gse@geneList
p1 <- heatplot(gsex, showCategory=5)
p2 <- heatplot(gsex, foldChange=geneList, showCategory=5)
mainheatmap_plot <- cowplot::plot_grid(p1, p2, ncol=1, labels=LETTERS[1:2])
print(mainheatmap_plot)
})
output$upsetPlotTitle <- renderPlot({
txt <- "UpSet Plot"
title_plot <- ggplot() +
geom_textbox(
aes(x = 0, y = 0, label = txt),
size = 18 / .pt,
width = unit(6, "inches")
) +
theme_void()
print(title_plot)
})
output$upsetPlot <- renderPlot({
gse_df <- gse@result
top_terms <- gse_df %>% arrange(pvalue) %>% head(10)
top_gene_sets <- strsplit(top_terms$core_enrichment, "/")
gene_sets_list <- lapply(top_gene_sets, function(x) unique(trimws(x)))
gene_sets_df <- fromList(setNames(gene_sets_list, top_terms$ID))
# Create the UpSet plot
upset_plot <- upset(gene_sets_df,
sets = names(gene_sets_df),
main.bar.color = "steelblue",
sets.bar.color = "darkred",
order.by = "freq",
matrix.color = "gray",
keep.order = TRUE)
print(upset_plot)
})
output$pubmedPathwayPlotTitle <- renderPlot({
txt <- "PubMed Pathway Enrichment"
title_plot <- ggplot() +
geom_textbox(
aes(x = 0, y = 0, label = txt),
size = 18 / .pt,
width = unit(6, "inches")
) +
theme_void()
print(title_plot)
})
output$pubmedPathwayPlot <- renderPlot({
results <- data.frame(Term = character(), Year = integer(), Count = integer(), stringsAsFactors = FALSE)
terms <- tail(gse$Description, n = 10)
results <- data.frame()
titles_2024 <- data.frame()
for (term in terms) {
for (year in 2014:2024) {
query <- paste(term, "[Title/Abstract] AND", year, "[PDAT]")
# Count results for each term and year
search_results <- entrez_search(db = "pubmed", term = query, retmax = 0)
results <- rbind(results, data.frame(Term = term, Year = year, Count = search_results$count))
# If the year is 2024, retrieve the first 10 article titles
if (year == 2024) {
search_results_2024 <- entrez_search(db = "pubmed", term = query, retmax = 10)
if (search_results_2024$count > 0) {
article_ids <- search_results_2024$ids
articles <- entrez_fetch(db = "pubmed", id = article_ids, rettype = "abstract", retmode = "text")
titles <- sapply(strsplit(articles, "\n"), function(x) x[1])
titles_2024 <- reactive({rbind(titles_2024, data.frame(Term = term, Title = titles, stringsAsFactors = FALSE))})
}
}
}
}
total_counts <- results %>%
group_by(Year) %>%
summarize(Total_Count = sum(Count), .groups = 'drop')
results <- results %>%
left_join(total_counts, by = "Year")
results <- results %>%
mutate(Ratio = Count / Total_Count)
print(results)
mainpubmed_plot <- ggplot(results, aes(x = Year, y = Ratio, color = Term)) +
geom_line() +
geom_point(size = 3, shape = 20, fill = "white", stroke = 1) + # Bolded dots
scale_x_continuous(limits = c(2013, 2025), breaks = seq(2013, 2025, by = 2.5)) + # 2.5-year breaks
labs(title = "Publication Ratio for Enriched Terms", x = "Year", y = "Publication Ratio") +
theme_minimal()
print(mainpubmed_plot)
})
})
} else {
showModal(modalDialog(
title = "Access Denied",
"Incorrect password. Please try again.",
easyClose = TRUE,
footer = NULL
))
}
})
}
shinyApp(ui = ui, server = server)
```
### Proteomics analysis
Analyzes proteomics data using DESeq2 and GSEA. Visualizes using volcano plot, and other plots to show the GSEA analysis results. Please make sure the uploaded data is in xlsx format, has the first column with the gene names, and the second column has the expanded name of each gene and there is an even number of data columns. The control condition should be first, the mutant condition second.
```{r}
ui <- fluidPage(
titlePanel("Interactive Volcano Plot with Gene and GO Term Search"),
useShinyjs(),
passwordInput("password", "Enter Password:", value = "", placeholder = "Password"),
actionButton("submit_password", "Submit"),
uiOutput("main_ui"),
#if you define a side_ui --> absolute panel, it would have to be defined here
)
# Server logic
server <- function(input, output, session) {
# Password handling
correct_password <- "my_secret_password"
observeEvent(input$submit_password, {
if (input$password == correct_password) {
showModal(modalDialog(
title = "Access Granted",
"Welcome! You can now search for genes and view the volcano plot.",
easyClose = TRUE,
footer = NULL
))
# Hide the password input and button after validation
shinyjs::hide("password")
shinyjs::hide("submit_password")
# Main UI appears after password is correct
output$main_ui <- renderUI({
sidebarLayout(
sidebarPanel(
fluidRow(
fileInput("file", "Choose XLSX File", multiple = FALSE, accept = c(".xlsx", "text/xlsx")),
actionButton("analyze_button", "Analyze"),
tags$hr(),
textInput("gene_search", "Search for a gene or keyword (separate multiple genes with ';'):", ""),
actionButton("search_gene", "Search Gene"),
tags$hr(),
selectInput("GO_search", "Select a GO term:", choices = NULL),
actionButton("search_GO_term", "Search GO Term"),
tags$hr(),
selectInput("description_search", "Search for the name of a pathway:", choices = NULL),
actionButton("search_description", "Search Description"),
tags$hr(),
sliderInput("pvalue", "P-value: ",
min = 0, max = 1,
value = 0.01, step = 0.00001),
tags$hr(),
sliderInput("log2fc", "Log2FoldChange: ",
min = 0.0001, max = 100,
value = 2.5, step = 0.05),
tags$hr(),
actionButton("visualize_gse", "Visualize the GSEGO Results:")
)
),
mainPanel(
plotlyOutput("volcanoPlot"),
plotOutput("dotPlotTitle", width = "100%", height = "100px"),
plotOutput("dotPlot", width = "100%", height = "1000px"),
plotOutput("conceptNetworkTitle", width = "100%", height = "100px"),
plotOutput("conceptNetwork", width = "100%", height = "600px"),
plotOutput("heatMapTitle", width = "100%", height = "100px"),
plotOutput("heatMap", width = "100%", height = "400px"),
plotOutput("upsetPlotTitle", width = "100%", height = "100px"),
plotOutput("upsetPlot", width = "100%", height = "1000px"),
plotOutput("pubmedPathwayPlotTitle", width = "100%", height = "100px"),
plotOutput("pubmedPathwayPlot", width = "100%", height = "1500px")
)
)
})
# Reactive values to store results and search criteria
searchValues <- reactiveValues(
gene_search = "",
GO_search = "All",
description_search = "",
df_inverted = NULL # Store df_inverted here
)
# Process uploaded file and perform DESeq2 analysis
observeEvent(input$analyze_button, {
req(input$file) # Ensure a file is uploaded
# Read in gene counts data
genecounts <- tryCatch({
read_excel(input$file$datapath, sheet = 1, col_names = TRUE)
}, error = function(e) {
showModal(modalDialog(title = "Error", "Could not read the Excel file.", easyClose = TRUE))
return(NULL)
})
if (is.null(genecounts)) return(NULL) # Stop further processing if reading failed
genecounts <- as.data.frame(genecounts)
rownames(genecounts) <- genecounts[, 1]
genecounts <- genecounts[, -1]
descriptions <<- data.frame(Description = genecounts[, 1])
rownames(descriptions) <- rownames(genecounts)
genecounts <- genecounts[, -1]
num_samples <- ncol(genecounts)
num_samples <- ncol(genecounts)
# Check if the number of samples is even
if (num_samples %% 2 != 0) {
showModal(modalDialog(
title = "Error",
"The number of samples must be even for proper grouping.",
easyClose = TRUE
))
return(NULL)
}
# Create the condition data frame
condition <- data.frame(genotype = rep(c('C', 'R'), each = num_samples / 2), row.names = colnames(genecounts))
# Create DESeq2 dataset
dds <- DESeqDataSetFromMatrix(countData = genecounts, colData = condition, design = ~genotype)
de <- DESeq(dds)
res_reactive <- reactiveVal()
res_reactive(results(de))
res <<- results(de)
# Create additional columns for plotting
res$pvalue_log10 <- -log10(res$pvalue)
pvalue_threshold <- 0.05
fold_change_threshold <- 2
res$significance <- ifelse(res$pvalue < pvalue_threshold, "Significant", "Not Significant")
res$new_column <- rownames(res)
res$diffexpressed <- ifelse(res$log2FoldChange > 0, "UP", ifelse(res$log2FoldChange < 0, "DOWN", "NO_CHANGE"))
# Generate gene list for GSEA
organism = "org.Hs.eg.db"
original_gene_list <- res$log2FoldChange
names(original_gene_list) <- res$new_column
gene_list <<- na.omit(original_gene_list)
gene_list = sort(gene_list, decreasing = TRUE)
# Perform GO enrichment analysis
gse <<- gseGO(geneList = gene_list,
ont = "ALL",
keyType = "SYMBOL",
minGSSize = 3,
maxGSSize = 800,
pvalueCutoff = 0.05,
verbose = TRUE,
OrgDb = organism,
pAdjustMethod = "none")
# Store inverted results for GO terms in reactive values
searchValues$df_inverted <- gse@result %>% separate_rows(core_enrichment, sep = "/")
# Update GO term and description choices in UI
updateSelectInput(session, "GO_search", choices = unique(searchValues$df_inverted$ID))
updateSelectInput(session, "description_search", choices = unique(searchValues$df_inverted$Description))
# Reactive filtering of results based on user input
filteredRes <- reactive({
data <- as.data.frame(res)
# Apply gene search filter
if (searchValues$gene_search != "") {
genes <- strsplit(searchValues$gene_search, ";")[[1]]
genes <- trimws(genes)
data <- data %>%
filter(rowSums(sapply(genes, function(gene) grepl(gene, new_column, ignore.case = TRUE))) > 0)
}
# Apply GO term filter
if (searchValues$GO_search != "All") {
selected_genes <- searchValues$df_inverted %>%
filter(ID == searchValues$GO_search) %>%
pull(core_enrichment)
data <- data %>%
filter(new_column %in% selected_genes)
}
# Apply description search filter
if (searchValues$description_search != "") {
selected_genes <- searchValues$df_inverted %>%
filter(Description == searchValues$description_search) %>%
pull(core_enrichment)
data <- data %>%
filter(new_column %in% selected_genes)
}
data
})
# Render volcano plot based on filtered results
output$volcanoPlot <- renderPlotly({
data_res <- filteredRes()
p_value <- input$pvalue #need to add slides here
log2fc <- input$log2fc #need to add slider here
p <- ggplot(data_res, aes(x = log2FoldChange, y = pvalue_log10,
text = paste("Gene:", new_column, "<br>Log2 Fold Change:", log2FoldChange,
"<br>P-value:", pvalue, "<br>Significance:", significance,
"<br>Differentially Expressed:", diffexpressed, "<br>-log10 Values:", pvalue_log10))) +
geom_point(aes(color = log2FoldChange, shape = diffexpressed)) +
geom_hline(yintercept = -log10(p_value), linetype = "dotted", color = "red") +
geom_vline(xintercept = c(-log2fc, log2fc), linetype = "dotted", color = "darkblue") +
xlim(-5, 5) +
xlab("Log2 Fold Change") +
ylab("-log10(P-value)") +
ggtitle("Volcano Plot") +
scale_color_gradient2(low = "green", mid = "pink", high = "blue", midpoint = 0,
name = "Log2 Fold Change") # Add a custom color scale for the color legends
ggplotly(p, tooltip = "text")
})
})
# Update search criteria based on user actions
observeEvent(input$search_gene, {
searchValues$gene_search <- input$gene_search
searchValues$GO_search <- "All"
searchValues$description_search <- ""
})
observeEvent(input$search_GO_term, {
searchValues$GO_search <- input$GO_search
searchValues$gene_search <- ""
searchValues$description_search <- ""
})
observeEvent(input$search_description, {
searchValues$description_search <- input$description_search
searchValues$GO_search <- "All"
searchValues$gene_search <- ""
})
observeEvent(input$visualize_gse, {
library(ggplot2)
library(ggtext)
library(gridExtra)
output$dotPlotTitle <- renderPlot({
txt <- "Dot Plot with 10 Pathways"
title_plot <- ggplot() +
geom_textbox(
aes(x = 0, y = 0, label = txt),
size = 18 / .pt,
width = unit(6, "inches")
) +
theme_void()
print(title_plot)
})
output$dotPlot <- renderPlot({
dot_plot <- dotplot(gse, showCategory = 10)
print(dot_plot)
})
output$conceptNetworkTitle <- renderPlot({
txt <- "Gene Concept Network"
title_plot <- ggplot() +
geom_textbox(
aes(x = 0, y = 0, label = txt),
size = 18 / .pt,
width = unit(6, "inches")
) +
theme_void()
print(title_plot)
})
output$conceptNetwork <- renderPlot({
gsex <- setReadable(gse, 'org.Hs.eg.db', 'ENTREZID')
geneList <- gse@geneList
p1 <- cnetplot(gsex, foldChange = geneList, max.overlaps = 100)
p2 <- cnetplot(gsex, categorySize = "pvalue", foldChange = geneList, max.overlaps = 100)
p3 <- cnetplot(gsex, foldChange = geneList, circular = TRUE, colorEdge = TRUE, max.overlaps = 100)
maingene_plot <- cowplot::plot_grid(p1, p2, p3, ncol = 3, labels = LETTERS[1:3], rel_widths = c(.8, .8, 1.2))
print(maingene_plot)
})
output$heatMapTitle <- renderPlot({
txt <- "Heatmap-Like Functional Classification"
title_plot <- ggplot() +
geom_textbox(
aes(x = 0, y = 0, label = txt),
size = 18 / .pt,
width = unit(6, "inches")
) +
theme_void()
print(title_plot)
})
output$heatMap <- renderPlot({
gsex <- setReadable(gse, 'org.Hs.eg.db', 'ENTREZID')
geneList <- gse@geneList
p1 <- heatplot(gsex, showCategory=5)
p2 <- heatplot(gsex, foldChange=geneList, showCategory=5)
mainheatmap_plot <- cowplot::plot_grid(p1, p2, ncol=1, labels=LETTERS[1:2])
print(mainheatmap_plot)
})
output$upsetPlotTitle <- renderPlot({
txt <- "UpSet Plot"
title_plot <- ggplot() +
geom_textbox(
aes(x = 0, y = 0, label = txt),
size = 18 / .pt,
width = unit(6, "inches")
) +
theme_void()
print(title_plot)
})
output$upsetPlot <- renderPlot({
gse_df <- gse@result
top_terms <- gse_df %>% arrange(pvalue) %>% head(10)
top_gene_sets <- strsplit(top_terms$core_enrichment, "/")
gene_sets_list <- lapply(top_gene_sets, function(x) unique(trimws(x)))
gene_sets_df <- fromList(setNames(gene_sets_list, top_terms$ID))
# Create the UpSet plot
upset_plot <- upset(gene_sets_df,
sets = names(gene_sets_df),
main.bar.color = "steelblue",
sets.bar.color = "darkred",
order.by = "freq",
matrix.color = "gray",
keep.order = TRUE)
print(upset_plot)
})
output$pubmedPathwayPlotTitle <- renderPlot({
txt <- "PubMed Pathway Enrichment"
title_plot <- ggplot() +
geom_textbox(
aes(x = 0, y = 0, label = txt),
size = 18 / .pt,
width = unit(6, "inches")
) +
theme_void()
print(title_plot)
})
output$pubmedPathwayPlot <- renderPlot({
results <- data.frame(Term = character(), Year = integer(), Count = integer(), stringsAsFactors = FALSE)
terms <- tail(gse$Description, n = 10)
results <- data.frame()
titles_2024 <- data.frame()
for (term in terms) {
for (year in 2014:2024) {
query <- paste(term, "[Title/Abstract] AND", year, "[PDAT]")
# Count results for each term and year
search_results <- entrez_search(db = "pubmed", term = query, retmax = 0)
results <- rbind(results, data.frame(Term = term, Year = year, Count = search_results$count))
# If the year is 2024, retrieve the first 10 article titles
if (year == 2024) {
search_results_2024 <- entrez_search(db = "pubmed", term = query, retmax = 10)
if (search_results_2024$count > 0) {
article_ids <- search_results_2024$ids
articles <- entrez_fetch(db = "pubmed", id = article_ids, rettype = "abstract", retmode = "text")
titles <- sapply(strsplit(articles, "\n"), function(x) x[1])
titles_2024 <- reactive({rbind(titles_2024, data.frame(Term = term, Title = titles, stringsAsFactors = FALSE))})
}
}
}
}
total_counts <- results %>%
group_by(Year) %>%
summarize(Total_Count = sum(Count), .groups = 'drop')
results <- results %>%
left_join(total_counts, by = "Year")
results <- results %>%
mutate(Ratio = Count / Total_Count)
print(results)
mainpubmed_plot <- ggplot(results, aes(x = Year, y = Ratio, color = Term)) +
geom_line() +
geom_point(size = 3, shape = 20, fill = "white", stroke = 1) + # Bolded dots
scale_x_continuous(limits = c(2013, 2025), breaks = seq(2013, 2025, by = 2.5)) + # 2.5-year breaks
labs(title = "Publication Ratio for Enriched Terms", x = "Year", y = "Publication Ratio") +
theme_minimal()
print(mainpubmed_plot)
})
})
} else {
showModal(modalDialog(
title = "Access Denied",
"Incorrect password. Please try again.",
easyClose = TRUE,
footer = NULL
))
}
})
}
shinyApp(ui = ui, server = server)
``` |