deseq-server.R 4.71 KB
Newer Older
Midoux Cedric's avatar
Midoux Cedric committed
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
output$deseqContrastVarUI <- renderUI({
  validate(need(physeq(), ""))
  selectInput(
    "deseqContrastVar",
    label = "Experimental design : ",
    choices = c(sample_variables(physeq()))
  )
})

output$deseqContrastModUI <- renderUI({
  validate(need(physeq(), ""), need(input$deseqContrastVar, ""))
  checkboxGroupInput(
    "deseqContrastMod",
    label = "Contrast (exactly two required) : ",
    choices = NULL,
    inline = TRUE
  )
})

observe({
  validate(need(physeq(), ""), need(input$deseqContrastVar, ""))
  var <- levels(as.factor(get_variable(physeq(), input$deseqContrastVar)))
  updateCheckboxGroupInput(session,
                           inputId = "deseqContrastMod",
                           choices = var,
                           selected = var[c(1, 2)],
                           inline = TRUE
  )
})

output$deseqTitleUI <- renderUI({
  validate(need(physeq(), ""))
  textInput("deseqTitle",
            label = "Title : ",
            value = "Volcano Plot")
})

output$deseqPadjUI <- renderUI({
  validate(need(physeq(), ""))
  sliderInput("deseqPadj",
              label = "Adjusted p-value threshold :",
              min = 0,
              max = 1,
              value = 0.05)
})

output$deseqUI <- renderUI({
  validate(need(physeq(), ""))
  box(
    title = "Setting : " ,
    width = NULL,
    status = "primary",
    uiOutput("deseqContrastVarUI"),
    uiOutput("deseqContrastModUI"),
    uiOutput("deseqTitleUI"),
    uiOutput("deseqPadjUI")
  )
})

output$deseq <- metaRender2(renderPlot, {
  validate(
    need(physeq(), "Requires an abundance dataset"),
    need(length(input$deseqContrastMod) == 2, "Requires two conditions"))
  data <- physeq()
  
  metaExpr({
    deseq_data <- phyloseq_to_deseq2(data, ..(as.formula(paste("~", input$deseqContrastVar))))
    dds <- DESeq2::DESeq(deseq_data, sfType = "poscounts")
    
    results <- DESeq2::results(dds, 
                               tidy = TRUE, 
                               contrast = ..(c(input$deseqContrastVar, input$deseqContrastMod[1], input$deseqContrastMod[2]))) %>% 
      rename(OTU = row)
    
    da_volcano <- data.frame(
      #otu      = row.names(results),
      otu      = results$OTU,
      evidence = -log10(results$padj), 
      lfc      = results$log2FoldChange) %>% 
      na.omit()
    # add a threshol line
    y_axix_volcano_line <- -log10(..(input$deseqPadj))
    # Modify dataset to add new coloumn of colors
    da_volcano <- da_volcano %>%
      mutate(
        color = case_when(
          lfc > 0 & evidence > y_axix_volcano_line ~ "More", 
          lfc < 0 & evidence > y_axix_volcano_line ~ "Less", 
          TRUE                                     ~ "Equal"
        )
      )
    
    # Color corresponds to fold change directionality
    volcano_plot <- ggplot(da_volcano, 
                           aes(x = lfc, y = evidence, label = otu)) + 
      geom_point(aes(color = factor(color)), size = 1.75, alpha = 0.8, na.rm = T) + # add gene points
      geom_text() +
      theme_bw(base_size = 16) +                                                    # clean up theme
      theme(legend.position = "none") +                                             # remove legend 
      ggtitle(label = ..(input$deseqTitle)) +                                       # add title
      xlab(expression(log[2]("FoldChange"))) +                                      # x-axis label
      ylab(expression(-log[10]("adjusted p-value"))) +                              # y-axis label
      geom_vline(xintercept = 0, colour = "grey80", linetype = 2) +                                # add line at 0
      geom_hline(aes(yintercept = y_axix_volcano_line), yintercept = y_axix_volcano_line, colour = "grey80", linetype = 2) +
      annotate(geom = "text", 
               label = paste("padj =", ..(input$deseqPadj)), 
               x = min(da_volcano$lfc), 
               y = y_axix_volcano_line + 0.25, 
               size = 4,
               colour = "black",
               vjust = 0,
               hjust = 0) + # add pvalue threshold           
      scale_color_manual(values = c("More" = "red", "Less" = "chartreuse", "Equal" = "darkgray")) # change colors
    # Plot figure
    volcano_plot + scale_y_continuous(trans = "log1p")
  })
})

observeEvent(input$deseq_output_code,
             {
               displayCodeModal(
                 expandChain(
                   quote(library(phyloseq)),
                   quote(library(phyloseq.extended)),
                   quote(library(DESeq2)),
                   quote(library(ggplot2)),
                   quote(library(magrittr)),
                   quote(library(dplyr)),
                   
                   "# Replace `data` with you own data.",
                   output$deseq()
                 )
               )
             }
)