deseq-server.R 7.09 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
44
45
46
47
48
  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$deseqUI <- renderUI({
  validate(need(physeq(), ""))
  box(
    title = "Setting : " ,
    width = NULL,
    status = "primary",
    uiOutput("deseqContrastVarUI"),
    uiOutput("deseqContrastModUI"),
Midoux Cedric's avatar
Midoux Cedric committed
49
50
    # actionButton("deseqButton", label = "Execute", icon = icon("check"), class = "btn-primary"),
    uiOutput("deseqTitleUI")
Midoux Cedric's avatar
Midoux Cedric committed
51
52
53
  )
})

Midoux Cedric's avatar
Midoux Cedric committed
54
55
56
57
58
59
60
61
62
design <- metaReactive2({
  req(input$deseqContrastVar)
  metaExpr({as.formula(..(paste("~", input$deseqContrastVar)))})
})

cds <- metaReactive2({
  req(design())
  req(physeq())
  
Midoux Cedric's avatar
Midoux Cedric committed
63
  data <- physeq()
Midoux Cedric's avatar
Midoux Cedric committed
64
65
66
67
68
69
70
71
  metaExpr({phyloseq_to_deseq2(data, design = ..(design()))})
  })

dds <- metaReactive2({
  req(cds())
  
  metaExpr({DESeq2::DESeq(..(cds()), sfType = "poscounts")})
})
Midoux Cedric's avatar
Midoux Cedric committed
72

Midoux Cedric's avatar
Midoux Cedric committed
73
74
75
76
77
results <- metaReactive2({
  req(input$deseqContrastVar)
  req(dds())
  req(physeq())
  req({class(get_variable(physeq(), input$deseqContrastVar)) == "numeric" || length(input$deseqContrastMod) == 2})
Midoux Cedric's avatar
Midoux Cedric committed
78
  
Midoux Cedric's avatar
Midoux Cedric committed
79
80
81
  data <- physeq()

  if (class(get_variable(data, input$deseqContrastVar)) == "numeric") {
Midoux Cedric's avatar
Midoux Cedric committed
82
83
    # First case: regression against a continuous variable
    metaExpr({
Midoux Cedric's avatar
Midoux Cedric committed
84
      DESeq2::results(object = ..(dds()),
Midoux Cedric's avatar
Midoux Cedric committed
85
86
87
88
89
90
91
                      name = ..(input$deseqContrastVar),
                      tidy = TRUE) %>%
        as_tibble() %>%
        rename(OTU = row) %>%
        inner_join(tax_table(data) %>% as("matrix") %>% as_tibble(rownames = "OTU"), by = "OTU")
      })
    } else {
Midoux Cedric's avatar
Midoux Cedric committed
92
93
      validate(need(length(input$deseqContrastMod) == 2, "Invalid input."))
      
Midoux Cedric's avatar
Midoux Cedric committed
94
95
96
      if (length(levels(get_variable(data, input$deseqContrastVar))) == 2) {
        # Second case: regression against a binary variable
        metaExpr({
Midoux Cedric's avatar
Midoux Cedric committed
97
98
          DESeq2::results(object = ..(dds()),
                          name = ..(DESeq2::resultsNames(..(dds()))[-1]),
Midoux Cedric's avatar
Midoux Cedric committed
99
100
101
102
                          tidy = TRUE) %>%
            as_tibble() %>% rename(OTU = row) %>%
            inner_join(tax_table(data) %>% as("matrix") %>% as_tibble(rownames = "OTU"), by = "OTU")
          })
Midoux Cedric's avatar
Midoux Cedric committed
103
104
105
106
107
108
109
110
111
112
        } 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")
          })
        }
Midoux Cedric's avatar
Midoux Cedric committed
113
    }
Midoux Cedric's avatar
Midoux Cedric committed
114
115
116
117
})

detail <-metaReactive2({
  data <- physeq()
Midoux Cedric's avatar
Midoux Cedric committed
118
  
Midoux Cedric's avatar
Midoux Cedric committed
119
  if (class(get_variable(data, input$deseqContrastVar)) == "numeric") {
Midoux Cedric's avatar
Midoux Cedric committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
    # 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], "."))
      })
    }
  }
Midoux Cedric's avatar
Midoux Cedric committed
137
138
139
140
141
142
143
144
145
146
})

output$deseq <- metaRender2(renderPlot, {
  validate(
    need(physeq(), "Requires an abundance dataset"),
    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.")#,
    #need(class(results()) == "DESeqResults", "Invalid input.")
    )
  data <- physeq()
Midoux Cedric's avatar
Midoux Cedric committed
147
148
  
  deseqPlot <- metaExpr({
Midoux Cedric's avatar
Midoux Cedric committed
149
    ggplot(..(results()) %>% mutate(evidence = -log10(padj), 
Midoux Cedric's avatar
Midoux Cedric committed
150
                              evolution = case_when(
Midoux Cedric's avatar
Midoux Cedric committed
151
152
                                padj <= 0.05 & log2FoldChange < 0 ~ "Down", 
                                padj <= 0.05 & log2FoldChange > 0 ~ "Up", 
Midoux Cedric's avatar
Midoux Cedric committed
153
154
155
156
                                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
157
      theme_bw(base_size = 16) +                                                    # clean up theme
Midoux Cedric's avatar
Midoux Cedric committed
158
159
      theme(legend.position = "none",                                               # remove legend 
            plot.subtitle = element_text(size = 12)) +                              # add subtitle
Midoux Cedric's avatar
Midoux Cedric committed
160
      ggtitle(label = ..(input$deseqTitle), subtitle = ..(detail())) +              # add informative title
Midoux Cedric's avatar
Midoux Cedric committed
161
162
      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
163
      geom_vline(xintercept = 0, colour = "grey80", linetype = 2) +                 # add line at 0
Midoux Cedric's avatar
Midoux Cedric committed
164
      geom_hline(yintercept = -log10(0.05), colour = "grey80", linetype = 2) +
Midoux Cedric's avatar
Midoux Cedric committed
165
166
167
168
169
170
      scale_color_manual(values = c("Down" = "red", "Not DA" = "grey20", "Up" = "green")) # change colors
  })
  
  metaExpr({
    p <- ..(deseqPlot)
    p
Midoux Cedric's avatar
Midoux Cedric committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
  })
})

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()
                 )
               )
             }
)
Midoux Cedric's avatar
Midoux Cedric committed
191
192
193
194

# output$deseqTable <- metaRender2(renderTable, {
# }
# )