inst/shiny/app.R

#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(shiny)
library(MSEApdata)
#library(MSEAp)
library(clusterProfiler)
library(tibble)
library(BridgeDbR)

# Please download the file metabolites_20190509.bridge from https://www.bridgedb.org/mapping-databases/hmdb-metabolite-mappings/
# before running this app
mapper <- loadDatabase("./metabolites_20190509.bridge")
data(kusano)
data(mset_SMPDB_format_KEGG)

ui = fluidPage(
  titlePanel("MSEAp Shiny"),
  
  sidebarLayout(
    
    sidebarPanel(
      textAreaInput(inputId="caption", label="Put your metabolite IDs here", width="100%", height="400px", value=paste(kusano, collapse = "\n"), placeholder = "Placeholder"),
      radioButtons("pathway", "Pathway Database:",
                   c("SMPDB" = "smpdb", "KEGG" = "kegg")),
      textInput(inputId = "pvaluecutoff", label = "pvalueCutoff", value = 0.5),
      textInput(inputId = "qvaluecutoff", label = "qvalueCutoff", value = 0.5),
      textInput(inputId = "minGSSize", label = "minMSSize", value = 5),
      width = 1
    ),
    
    mainPanel(
      tabsetPanel(type = "tabs",
                  tabPanel("ID Table", DT::dataTableOutput("value")),
                  tabPanel("MSEA results", DT::dataTableOutput("mseaclprof"), DT::dataTableOutput("mseamseap"),
                           plotOutput("clprofbar"), plotOutput("mseapbar"),
                           plotOutput("clprofdot"), plotOutput("mseapdot"),
                           plotOutput("clprofheat"), plotOutput("clprofemap"), plotOutput("clprofupset"))
      )
    )
    
  )
)

server = function(input, output) {

  observeEvent({
      input$caption
      input$pvaluecutoff
      input$qvaluecutoff
      input$minGSSize
    }, {
    mids <- strsplit(input$caption, "\n")
    
    kegg <- c()
    cas <- c()
    chebi <- c()
    knapsack <- c()
    hmdb <- c()
    pubchem <- c()
    
    for(mid in mids[[1]]){
      # CAS
      tmp <- map(mapper, mid, source = "Ck", target = "Ca")
      cas <- c(cas, paste(tmp, collapse = " "))
      # CHEBI
      tmp <- map(mapper, mid, source = "Ck", target = "Ce")
      tmp <- unique(gsub("CHEBI:", "", tmp))
      tmp <- paste0('<a href="https://www.ebi.ac.uk/chebi/searchId.do?chebiId=CHEBI:', tmp, '" target="_blank">', tmp, "</a>")
      chebi <- c(chebi, paste(tmp, collapse = " "))
      # KNAPSACK
      tmp <- map(mapper, mid, source = "Ck", target = "Cks")
      tmp <- paste0('<a href="http://www.knapsackfamily.com/knapsack_jsp/information.jsp?sname=C_ID&word=', tmp, '" target="_blank">', tmp, "</a>")
      knapsack <- c(knapsack, paste(tmp, collapse = " "))
      # HMDB
      tmp <- map(mapper, mid, source = "Ck", target = "Ch")
      tmp <- paste0('<a href="http://www.hmdb.ca/metabolites/', tmp, '" target="_blank">', tmp, "</a>")
      hmdb <- c(hmdb, paste(tmp, collapse = " "))
      # PUBCHEM
      tmp <- map(mapper, mid, source = "Ck", target = "Cpc")
      tmp <- paste0('<a href="https://pubchem.ncbi.nlm.nih.gov/compound/', tmp, '" target="_blank">', tmp, "</a>")
      pubchem <- c(pubchem, paste(tmp, collapse = " "))
      
      # KEGG
      kegg <- c(kegg, paste0('<a href="https://www.genome.jp/dbget-bin/www_bget?cpd:', mid, '" target="_blank">', mid, "</a>"))
    }
    
    x <- data.frame("KEGG" = kegg, "CAS" = cas, "CHEBI" = chebi, "KNAPSACK" = knapsack, "HMDB" = hmdb, "PUBCHEM" = pubchem)
    
    output$value <- DT::renderDataTable(x, options= list(pageLength = 25), escape=FALSE)
    

    pathway <- character(0)
    cids <- list()
    
    for(i in 1:length(mset_SMPDB_format_KEGG)) {
      row = mset_SMPDB_format_KEGG[[i]]
      pathway <- c(pathway, row[[2]])
      cids[[i]] <- row[[3]]
    }
    
    # MSEA with clusterProfiler
    tb <- tibble(pathway=pathway, cID=cids)
    y <- enricher(kusano, pvalueCutoff=as.numeric(input$pvaluecutoff), TERM2GENE=tb, minGSSize=as.numeric(input$minGSSize), qvalueCutoff = as.numeric(input$qvaluecutoff))
    
    # MSEA with MSEAp
    res <- MSEAp::msea(mset_SMPDB_format_KEGG, kusano)
    
    output$mseaclprof <- DT::renderDataTable({
      as.data.frame(y)
    })
    
    output$mseamseap <- DT::renderDataTable({
      res
    })
    
    output$clprofbar <- renderPlot({
      barplot(y)
    })
    
    output$mseapbar <- renderPlot({
      MSEAp::barplot(res)
    })
    
    output$clprofdot <- renderPlot({
      dotplot(y)
    })
    
    output$mseapdot <- renderPlot({
      MSEAp::dotplot(res)
    })
    
    output$clprofheat <- renderPlot({
      heatplot(y)
    })
    
    output$clprofemap <- renderPlot({
      emapplot(y)
    })
    
    output$clprofupset <- renderPlot({
      upsetplot(y)
    })
    
  })

}

# Run the application 
shinyApp(ui = ui, server = server)
afukushima/MSEAp documentation built on Sept. 18, 2019, 7:12 p.m.