Browse code

new density graph of genes

pablo-rodr-bio2 authored on 11/06/2020 11:21:26
Showing2 changed files

... ...
@@ -197,7 +197,7 @@ gsva_information <- function(input, output, session) {
197 197
   else
198 198
     stop("Unknown output generated by the call to the 'gsva()' function.")
199 199
 
200
-  #Rendering text
200
+  #Rendering text1
201 201
   output$text1 <- renderUI({
202 202
     HTML(paste("<br/>", "\t To see the Empirical Cumulative Distribution Function 
203 203
     of a Sample, click on its line in this plot and go
... ...
@@ -205,12 +205,14 @@ gsva_information <- function(input, output, session) {
205 205
   })
206 206
   
207 207
   # Rendering graph1
208
-  dat.t <- melt(as.data.table(generated_gsva), variable.name = "Sample")
208
+  dat.t <- melt(as.data.table(generated_gsva, keep.rownames = "gene.sets"), 
209
+                variable.name = "Sample", id.vars="gene.sets")
210
+  
209 211
   output$plot <- renderPlotly({
210 212
     p <- ggplot(data = dat.t, aes(x=value, color=Sample)) +
211 213
       stat_density(geom="line", position = "identity") +
212 214
       theme(legend.position = "none") + labs(x="GSVA Scores", y="Density")
213
-    ggplotly(p, tooltip = "Sample", source = "click")
215
+    ggplotly(p, tooltip = "Sample", source = "click1")
214 216
   })
215 217
   
216 218
   # Rendering table
... ...
@@ -220,20 +222,63 @@ gsva_information <- function(input, output, session) {
220 222
   colnames(resultInformation) <- c("Nr. of gene sets", "Nr. of samples")
221 223
   output$result <- renderTable(resultInformation)
222 224
   
225
+  #Rendering text2
226
+  output$text2 <- renderUI({
227
+    HTML(paste("<br/>", "\t To see the Kernel Density Estimation of genes of 
228
+    any given Gene Set in this Sample,  click on any point in this plot and a
229
+    second plot will appear bellow it", "<br/>", sep="<br/>"))
230
+  })
231
+  
223 232
   #Rendering graph2
224
-  eventData <- reactive({
225
-    event_data("plotly_click", source = "click")
233
+  eventData1 <- reactive({
234
+    event_data("plotly_click", source = "click1")
235
+  })
236
+  
237
+  sample.c <- reactive({
238
+    req(eventData1())
239
+    ind <- eventData1()$curveNumber+1
240
+    colnames(generated_gsva)[ind]
241
+  })
242
+  
243
+  plot2 <- reactive({
244
+    req(sample.c())
245
+    data <- dat.t[Sample==sample.c()]
246
+    p <- ggplot(data = data, aes(x=value, color=Sample)) +
247
+      stat_ecdf(geom="point") + theme(legend.position = "none") + 
248
+      labs(x=paste0("GSVA Scores of ", sample.c()), y="Empirical Cumulative Density") +
249
+      scale_color_discrete(drop=TRUE, limits=levels(dat.t$Sample))
250
+    p <- ggplotly(p, source="click2")
251
+    style(p, text=data$gene.sets)
226 252
   })
253
+  
227 254
   output$plot2 <- renderPlotly({
228
-    req(eventData())
229
-    ind <- eventData()$curveNumber+1
230
-    subind <- colnames(generated_gsva)[ind]
231
-    p <- ggplot(data = dat.t[Sample==subind], aes(x=value, color=Sample)) +
232
-      stat_ecdf(geom="point") + 
255
+    req(plot2())
256
+    plot2()
257
+  })
258
+  
259
+  # Rendering graph 3
260
+  eventData2 <- reactive({
261
+    event_data("plotly_click", source = "click2")
262
+  })
263
+  
264
+  gene.set <- reactive({
265
+    plot2()$x$data[[1]]$text[eventData2()$pointNumber+1]
266
+  })
267
+  
268
+  output$plot3 <- renderPlotly({
269
+    req(eventData2())
270
+    genes.toplot <- geneIds(genes)[[gene.set()]]
271
+    mt <- match(genes.toplot, rownames(newY))
272
+    x <-  newY[na.omit(mt), sample.c()]
273
+    df <- as.data.frame(x)
274
+    df$x <- as.numeric(df$x)
275
+    df$Gene <- rownames(df)
276
+    p1 <- ggplot(data = df, aes(x=x)) +
277
+      stat_density(geom="line", position = "identity") +
278
+      geom_rug(aes(color=Gene)) +
233 279
       theme(legend.position = "none") + 
234
-      labs(x=paste0("GSVA Scores of ", subind), y="Empirical Cumulative Density")
235
-    p <- ggplotly(p)
236
-    style(p, text=row.names(generated_gsva))
280
+      labs(x=paste0("Gene Expressions from GeneSet ", gene.set()), y="Density") 
281
+    ggplotly(p1) %>% style(hoverinfo="none", traces = 1)
237 282
   })
238 283
   
239 284
   # Rendering Session Info
... ...
@@ -77,7 +77,9 @@ mainDataInput <- function(id) {
77 77
                                  tableOutput("result"),
78 78
                                  uiOutput("download")),
79 79
                         tabPanel("Gene Sets",
80
-                                 plotlyOutput("plot2")),
80
+                                 htmlOutput("text2"),
81
+                                 plotlyOutput("plot2"),
82
+                                 plotlyOutput("plot3")),
81 83
                         tabPanel("Session Info",
82 84
                                  verbatimTextOutput("sessionInfo"))
83 85
                                  )