star
star

Reputation: 775

How define Server for Copy/Paste regions as input in R/Shiny?

I have a shiny which can be used to overlap between query regions and a given data table (e.g. DF). I put two options for uploading query data 1) as .bed format and 2) inserting data as copy/paste. The shiny works well with uploading .bed file but I am not sure how can I define Server for copy/paste (text) data.

Thank you in advance for any suggestion!

DF<- data.table(chr=c("chr1","chr2"),start=c(10,20),end=c(20,30))%>% setkey(chr, start, end) 
text<- data.table(chr=c("chr1","chr2"),start=c(15,25),end=c(15,30))%>% setkey(chr, start, end) 

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel (
      p(strong ("Find overlap between query file and data"),style = "color:blue;"),
      br(),
      selectInput("choose","Choose file source",choices = c("file","text"),selected = "file"),
      conditionalPanel("input.choose=='file'",
                   fileInput("query.file", "Upload genomic coordinates in .bed 
                             format:",multiple = F,accept = ".bed")),
      conditionalPanel("input.choose=='text'",
                   textAreaInput("query.text", "Enter genomic coordinates:")),
      actionButton("run", "run"),
      width = "2"),
  mainPanel(
    dataTableOutput("DFtable"),
    dataTableOutput("overlap_table"))
  )
)

server <- function (input, output, session) {

  ## Read user .bed file
  user_query.file <- reactive({
  req(input$query.file)
  ext <- tools::file_ext(input$query.file$name)
  switch(ext,
       bed = fread(input$query.file$datapath,header=F)%>% 
         dplyr::rename (chr =V1, start=V2, end=V3) %>% setkey(chr, start, end)%>% unique()
    )
  })

  ## overlapping between query file and table
  overlap <- eventReactive(input$run, {
  req(input$run)
  withProgress(message = 'Analysis in progress', value = 0, {
  query.overlap<- foverlaps(user_query.file() ,DF,  nomatch = 0) %>% 
    unique()
    })
  })

  ## output
  output$DFtable<- renderDataTable({ DF })
  output$overlap_table <- renderDT({overlap () }) 

}

shinyApp(ui, server)

Desire out put using TEXT input option:

chr start end i.start i.end

chr1    15  15      10    20 
chr2    25  30      20    30

Upvotes: 1

Views: 76

Answers (1)

GriffoGoes
GriffoGoes

Reputation: 734

Not sure on how your copied text looks like, but assuming something like:

chr,start,end
chr1,10,20
chr4,34,56

All you need is to parse the text contents of the UI input and assigned it to DF variable. Your run event handler could look like:

  ## overlapping between query file and table
  overlap <- eventReactive(input$run, {
    req(input$run)
    
    #Requiring text input and parsing it
    req(input$query.text)
    DF <- data.table(read.csv(text=input$query.text)) %>% setkey(chr, start, end)

    withProgress(message = 'Analysis in progress', value = 0, {
      query.overlap<- foverlaps(text ,DF,  nomatch = 0) %>%
        unique()
    })
  })

Then when clicking on the run button, the analysis is executed and the overlap reactive value is updated. If the comparison is always between file and text provided, you should also include a req(user_query.file()) to ensure the file has been provided and correctly parsed.

Upvotes: 1

Related Questions