deseq-server.R 6.81 KB
Newer Older
Midoux Cedric's avatar
Midoux Cedric committed
1
2
3
4
5
6
7
8
9
10
output$deseqContrastVarUI <- renderUI({
  validate(need(physeq(), ""))
  selectInput(
    "deseqContrastVar",
    label = "Experimental design : ",
    choices = c(sample_variables(physeq()))
  )
})

output$deseqContrastModUI <- renderUI({
Midoux Cedric's avatar
Midoux Cedric committed
11
12
13
14
  validate(need(physeq(), ""), 
           need(input$deseqContrastVar, ""),
           need(class(get_variable(physeq(), input$deseqContrastVar)) != "numeric", "")
           )
Midoux Cedric's avatar
Midoux Cedric committed
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
  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",
Midoux Cedric's avatar
Midoux Cedric committed
44
              label = "Adjusted p-value threshold (recommended 0.05 ):",
Midoux Cedric's avatar
Midoux Cedric committed
45
46
              min = 0,
              max = 1,
Midoux Cedric's avatar
Midoux Cedric committed
47
48
              value = 0.05,
              step = 0.01)
Midoux Cedric's avatar
Midoux Cedric committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
})

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"),
Midoux Cedric's avatar
Midoux Cedric committed
67
68
69
    need(class(get_variable(physeq(), input$deseqContrastVar)) != "numeric" || 
           length(input$deseqContrastMod) == 2, "Requires a continuous design or a selection of two modalities for a discrete design.")
    )
Midoux Cedric's avatar
Midoux Cedric committed
70
  data <- physeq()
Midoux Cedric's avatar
Midoux Cedric committed
71
72
73
74

  design <- metaExpr({as.formula(..(paste("~", input$deseqContrastVar)))})
  cds <- metaExpr({phyloseq_to_deseq2(data, design = design)})
  dds <- metaExpr({DESeq2::DESeq(cds, sfType = "poscounts")})
Midoux Cedric's avatar
Midoux Cedric committed
75
  
Midoux Cedric's avatar
Midoux Cedric committed
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
  results <- if (class(get_variable(data, input$deseqContrastVar)) == "numeric") {
    # First case: regression against a continuous variable
    metaExpr({
      DESeq2::results(object = dds,
                      name = ..(input$deseqContrastVar),
                      tidy = TRUE) %>%
        as_tibble() %>%
        rename(OTU = row) %>%
        inner_join(tax_table(data) %>% as("matrix") %>% as_tibble(rownames = "OTU"), by = "OTU")
      })
    } else {
      if (length(levels(get_variable(data, input$deseqContrastVar))) == 2) {
        # Second case: regression against a binary variable
        metaExpr({
          DESeq2::results(object = dds,
                          name = ..(DESeq2::resultsNames(dds)[-1]),
                          tidy = TRUE) %>%
            as_tibble() %>% rename(OTU = row) %>%
            inner_join(tax_table(data) %>% as("matrix") %>% as_tibble(rownames = "OTU"), by = "OTU")
          })
      } else {
        # Third case: regression against a qualiative variable with three or more levels
        metaExpr({
          DESeq2::results(object = dds,
                          contrast = ..(c(input$deseqContrastVar, input$deseqContrastMod[1], input$deseqContrastMod[2])),
                          tidy = TRUE) %>%
            as_tibble() %>% rename(OTU = row) %>%
            inner_join(tax_table(data) %>% as("matrix") %>% as_tibble(rownames = "OTU"), by = "OTU")
        })
      }
    }
  
  detail <- if (class(get_variable(data, input$deseqContrastVar)) == "numeric") {
    # First case
    metaExpr({
      ..(paste0("You compare low and high values of the continuous variable ", input$deseqContrastVar, ".\nA positive log2FoldChange means more abundant for high values of ", input$deseqContrastVar, "."))
    })
  } else {
    if (length(levels(get_variable(data, input$deseqContrastVar))) == 2) {
      # Second case
      metaExpr({
        ..(paste0("You compare ", input$deseqContrastMod[1], " to ", input$deseqContrastMod[2], " for the variable ", input$deseqContrastVar, ".\nA positive log2FoldChange means more abundant in ", input$deseqContrastMod[2], " than in ", input$deseqContrastMod[1], "."))
      })
    } else {
      # Third case
      metaExpr({
        ..(paste0("You choose to compare ", input$deseqContrastMod[1], " to ", input$deseqContrastMod[2], " for the variable", input$deseqContrastVar, ".\nA positive log2FoldChange means more abundant in ", input$deseqContrastMod[1], " than in ", input$deseqContrastMod[2], "."))
      })
    }
  }
  
  deseqTable <- metaExpr({
Midoux Cedric's avatar
Midoux Cedric committed
128
    
Midoux Cedric's avatar
Midoux Cedric committed
129
130
131
132
133
134
135
136
137
138
139
  })
  
  deseqPlot <- metaExpr({
    ggplot(results %>% mutate(evidence = -log10(padj), 
                              evolution = case_when(
                                padj <= ..(input$deseqPadj) & log2FoldChange < 0 ~ "Down", 
                                padj <= ..(input$deseqPadj) & log2FoldChange > 0 ~ "Up", 
                                TRUE                              ~ "Not DA"
                              )),
           aes(x = log2FoldChange, y = evidence)) + 
      geom_point(aes(color = evolution), size = 1.75, alpha = 0.8, na.rm = T) +     # base layer 
Midoux Cedric's avatar
Midoux Cedric committed
140
      theme_bw(base_size = 16) +                                                    # clean up theme
Midoux Cedric's avatar
Midoux Cedric committed
141
142
143
      theme(legend.position = "none",                                               # remove legend 
            plot.subtitle = element_text(size = 12)) +                              # add subtitle
      ggtitle(label = ..(input$deseqTitle), subtitle = detail) +                    # add informative title
Midoux Cedric's avatar
Midoux Cedric committed
144
145
      xlab(expression(log[2]("FoldChange"))) +                                      # x-axis label
      ylab(expression(-log[10]("adjusted p-value"))) +                              # y-axis label
Midoux Cedric's avatar
Midoux Cedric committed
146
147
148
149
150
151
152
153
154
155
156
157
158
      geom_vline(xintercept = 0, colour = "grey80", linetype = 2) +                 # add line at 0
      geom_hline(yintercept = -log10(..(input$deseqPadj)), colour = "grey80", linetype = 2) +
      scale_color_manual(values = c("Down" = "red", "Not DA" = "grey20", "Up" = "green")) # change colors
  })
  
  metaExpr({
    design <- ..(design)
    cds <- ..(cds)
    dds <- ..(dds)
    results <- ..(results)
    detail <- ..(detail)
    p <- ..(deseqPlot)
    p
Midoux Cedric's avatar
Midoux Cedric committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
  })
})

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()
                 )
               )
             }
)