library(shiny) library(QTLseqr) library(data.table) library(dplyr) library(tidyr) library(vcfR) library(ggplot2) options(shiny.maxRequestSize = 50*1024^3) options(timeout = 3600) # QTL-seq Analysis Function qtlseq <- function(input_csv = NULL, output_table = "/tmp/output_file.table", delta_snp_plot = NULL, qtlseq_csv = "/tmp/BSA_QTLseq.csv", refAlleleFreq = 0.20, minTotalDepth = 100, maxTotalDepth = 400, minSampleDepth = 40, minGQ = 99, windowSize = NULL, popStruc = NULL, bulkSize = NULL, replications = 10000, intervals = c(95, 99), alpha = 0.01) { data <- read.csv(input_csv) write.table(data, file = output_table, sep = "\t", row.names = FALSE, quote = FALSE) col_data <- colnames(data[5:ncol(data)]) unique_samples <- unique(sub("\\..*", "", col_data)) HighBulk <- unique_samples[1] LowBulk <- unique_samples[2] chroms <- as.vector(unique(data[1])) df <- importFromGATK(file = output_table, highBulk = HighBulk, lowBulk = LowBulk) df_filt <- filterSNPs( SNPset = df, refAlleleFreq = refAlleleFreq, minTotalDepth = minTotalDepth, maxTotalDepth = maxTotalDepth, minSampleDepth = minSampleDepth, minGQ = minGQ ) df_filt <- runGprimeAnalysis( SNPset = df_filt, windowSize = windowSize, outlierFilter = "deltaSNP" ) df_filt1 <- runQTLseqAnalysis( SNPset = df_filt, windowSize = windowSize, popStruc = popStruc, bulkSize = bulkSize, replications = replications, intervals = intervals ) p <- ggplot(df_filt1, aes(x = POS)) + geom_point(aes(y = deltaSNP), color = "#3b82f6", size = 0.5) + geom_line(aes(y = tricubeDeltaSNP), color = "#ef4444", size = 0.8) + geom_line(aes(y = CI_95), color = "#f97316", linetype = "solid", size = 0.6) + geom_line(aes(y = -CI_95), color = "#f97316", linetype = "solid", size = 0.6) + geom_line(aes(y = CI_99), color = "#10b981", linetype = "solid", size = 0.6) + geom_line(aes(y = -CI_99), color = "#10b981", linetype = "solid", size = 0.6) + geom_hline(yintercept = c(-1, 0, 1), linetype = "dashed", color = "#0ea5e9") + facet_wrap(~CHROM, ncol = 4, scales = "free_x") + labs(x = "Position (Mb)", y = expression(Delta *"SNP-index")) + theme_minimal(base_size = 12) + theme( strip.text = element_text(face = "bold"), axis.title = element_text(face = "bold") ) ggsave("/tmp/deltaSNPIndex.png", plot = p, dpi = 600, width = 12, height = 10, units = "in") getQTLTable(SNPset = df_filt1, alpha = alpha, export = TRUE, fileName = qtlseq_csv) return(list(QTLseqResults = df_filt1, GprimeResults = df_filt, plot = p)) } # UI ui <- fluidPage( tags$head( tags$style(HTML(" @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600;700&display=swap'); body { font-family: 'Poppins', sans-serif; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); min-height: 100vh; padding: 20px; } .main-container { background: white; border-radius: 20px; box-shadow: 0 20px 60px rgba(0,0,0,0.3); padding: 0; overflow: hidden; max-width: 1400px; margin: 0 auto; } .header { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 30px 40px; text-align: center; } .header h1 { margin: 0; font-size: 2.5em; font-weight: 700; text-shadow: 2px 2px 4px rgba(0,0,0,0.2); } .header p { margin: 10px 0 0 0; font-size: 1.1em; opacity: 0.95; } .content-wrapper { padding: 40px; } .section-title { color: #667eea; font-size: 1.5em; font-weight: 600; margin-bottom: 20px; padding-bottom: 10px; border-bottom: 3px solid #667eea; } .card { background: #f8f9fa; border-radius: 15px; padding: 25px; margin-bottom: 30px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); transition: transform 0.3s ease; } .card:hover { transform: translateY(-5px); box-shadow: 0 8px 12px rgba(0,0,0,0.15); } .upload-area { border: 3px dashed #667eea; border-radius: 15px; padding: 30px; text-align: center; background: linear-gradient(135deg, #f0f4ff 0%, #e9d5ff 100%); transition: all 0.3s ease; } .upload-area:hover { border-color: #764ba2; background: linear-gradient(135deg, #e9d5ff 0%, #f0f4ff 100%); } .btn-primary { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border: none; color: white; padding: 12px 30px; font-size: 1.1em; font-weight: 600; border-radius: 25px; cursor: pointer; transition: all 0.3s ease; box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4); } .btn-primary:hover { transform: translateY(-2px); box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6); } .shiny-input-container { margin-bottom: 20px; } .shiny-input-container label { color: #4a5568; font-weight: 600; margin-bottom: 8px; display: block; } input[type='number'], input[type='text'], select { width: 100%; padding: 12px; border: 2px solid #e2e8f0; border-radius: 10px; font-size: 1em; transition: all 0.3s ease; font-family: 'Poppins', sans-serif; } input[type='number']:focus, input[type='text']:focus, select:focus { outline: none; border-color: #667eea; box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1); } .param-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 20px; } .progress-section { background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%); border-radius: 15px; padding: 20px; margin: 20px 0; text-align: center; } .results-section { background: linear-gradient(135deg, #d1fae5 0%, #a7f3d0 100%); border-radius: 15px; padding: 25px; margin-top: 20px; } .plot-container { background: white; border-radius: 10px; padding: 20px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); margin-top: 20px; } .download-btn { background: linear-gradient(135deg, #10b981 0%, #059669 100%); border: none; color: white; padding: 10px 25px; font-weight: 600; border-radius: 20px; cursor: pointer; margin: 5px; transition: all 0.3s ease; } .download-btn:hover { transform: translateY(-2px); box-shadow: 0 4px 12px rgba(16, 185, 129, 0.4); } .info-box { background: linear-gradient(135deg, #dbeafe 0%, #bfdbfe 100%); border-left: 4px solid #3b82f6; padding: 15px; border-radius: 10px; margin: 15px 0; } .shiny-notification { position: fixed; top: 20px; right: 20px; border-radius: 10px; font-family: 'Poppins', sans-serif; } ")) ), div(class = "main-container", div(class = "header", h1("🧬 QTL-seq Analysis Platform"), p("Quantitative Trait Loci Sequencing Analysis Tool") ), div(class = "content-wrapper", # File Upload Section div(class = "card", h3(class = "section-title", "📁 Data Input"), div(class = "upload-area", fileInput("csvFile", label = NULL, accept = c(".csv"), buttonLabel = "Browse...", placeholder = "Select CSV file"), p(style = "color: #667eea; font-weight: 600; margin-top: 10px;", "Upload your QTL-seq data in CSV format") ) ), # Parameters Section div(class = "card", h3(class = "section-title", "⚙️ Analysis Parameters"), div(class = "info-box", p(style = "margin: 0; color: #1e40af;", "📌 Configure analysis parameters below. Default values are pre-filled.") ), div(class = "param-grid", numericInput("refAlleleFreq", "Reference Allele Frequency", 0.20, min = 0, max = 1, step = 0.01), numericInput("minTotalDepth", "Min Total Depth", 100, min = 0), numericInput("maxTotalDepth", "Max Total Depth", 400, min = 0), numericInput("minSampleDepth", "Min Sample Depth", 40, min = 0), numericInput("minGQ", "Min Genotype Quality", 99, min = 0, max = 100), numericInput("windowSize", "Window Size", 500000, min = 0) ), div(class = "param-grid", style = "margin-top: 20px;", selectInput("popStruc", "Population Structure", choices = c("F2" = "F2", "RIL" = "RIL", "BC" = "BC")), textInput("bulkSize", "Bulk Sizes (comma-separated)", "20,20"), numericInput("replications", "Replications", 10000, min = 1000), numericInput("alpha", "Alpha Level", 0.01, min = 0, max = 1, step = 0.01) ) ), # Run Analysis Button div(style = "text-align: center; margin: 30px 0;", actionButton("runAnalysis", "🚀 Run QTL-seq Analysis", class = "btn-primary", style = "font-size: 1.2em; padding: 15px 40px;") ), # Progress Section conditionalPanel( condition = "input.runAnalysis > 0", div(class = "progress-section", h4(style = "color: #92400e; margin-top: 0;", "⏳ Analysis in Progress..."), uiOutput("progressText") ) ), # Results Section conditionalPanel( condition = "output.plotReady", div(class = "results-section", h3(class = "section-title", style = "color: #059669;", "✅ Analysis Complete!"), div(class = "plot-container", h4(style = "color: #4a5568; margin-top: 0;", "Delta SNP-index Plot"), plotOutput("deltaPlot", height = "800px") ), div(style = "text-align: center; margin-top: 20px;", downloadButton("downloadPlot", "📊 Download Plot", class = "download-btn"), downloadButton("downloadQTL", "📄 Download QTL Table", class = "download-btn"), downloadButton("downloadResults", "💾 Download All Results", class = "download-btn") ), div(style = "margin-top: 30px;", h4(style = "color: #4a5568;", "📊 QTL Summary Statistics"), tableOutput("qtlSummary") ) ) ) ) ) ) # Server server <- function(input, output, session) { results <- reactiveValues(data = NULL, plot = NULL) observeEvent(input$runAnalysis, { req(input$csvFile) withProgress(message = 'Running QTL-seq Analysis', value = 0, { tryCatch({ incProgress(0.2, detail = "Loading data...") bulkSizes <- as.numeric(strsplit(input$bulkSize, ",")[[1]]) incProgress(0.3, detail = "Filtering SNPs...") result <- qtlseq( input_csv = input$csvFile$datapath, refAlleleFreq = input$refAlleleFreq, minTotalDepth = input$minTotalDepth, maxTotalDepth = input$maxTotalDepth, minSampleDepth = input$minSampleDepth, minGQ = input$minGQ, windowSize = input$windowSize, popStruc = input$popStruc, bulkSize = bulkSizes, replications = input$replications, alpha = input$alpha ) incProgress(0.8, detail = "Generating plots...") results$data <- result results$plot <- result$plot incProgress(1, detail = "Complete!") showNotification("Analysis completed successfully!", type = "message", duration = 5) }, error = function(e) { showNotification(paste("Error:", e$message), type = "error", duration = 10) }) }) }) output$plotReady <- reactive({ return(!is.null(results$plot)) }) outputOptions(output, "plotReady", suspendWhenHidden = FALSE) output$deltaPlot <- renderPlot({ req(results$plot) results$plot }) output$qtlSummary <- renderTable({ req(results$data) qtl_table <- read.csv("/tmp/BSA_QTLseq.csv") head(qtl_table, 10) }) output$progressText <- renderUI({ p(style = "color: #92400e; font-weight: 600;", "Please wait while the analysis is running. This may take several minutes...") }) output$downloadPlot <- downloadHandler( filename = function() { paste("deltaSNPIndex_", Sys.Date(), ".png", sep = "") }, content = function(file) { file.copy("/tmp/deltaSNPIndex.png", file) } ) output$downloadQTL <- downloadHandler( filename = function() { paste("BSA_QTLseq_", Sys.Date(), ".csv", sep = "") }, content = function(file) { file.copy("/tmp/BSA_QTLseq.csv", file) } ) output$downloadResults <- downloadHandler( filename = function() { paste("QTLseq_results_", Sys.Date(), ".zip", sep = "") }, content = function(file) { files <- c("/tmp/deltaSNPIndex.png", "/tmp/BSA_QTLseq.csv", "/tmp/output_file.table") zip(file, files[file.exists(files)]) } ) } # Run the application shinyApp(ui = ui, server = server)