Browse code

report template also using gene_plot

Federico Marini authored on 11/09/2024 23:55:21
Showing 1 changed files

... ...
@@ -212,65 +212,61 @@ This gene was selected in the interactive session.
212 212
 
213 213
 ```{r}
214 214
 anno_id <- rownames(values$mydst)
215
-      anno_gene <- values$myannotation$gene_name
216
-
217
-      # if(is.null(input$color_by) & input$genefinder!="")
218
-      #   return(ggplot() + annotate("text",label="Select a factor to plot your gene",0,0) + theme_bw())
219
-      # if(is.null(input$color_by) & input$genefinder=="")
220
-      #   return(ggplot() + annotate("text",label="Select a gene and a factor to plot gene",0,0) + theme_bw())
221
-      # if(input$genefinder=="")
222
-      #   return(ggplot() + annotate("text",label="Type in a gene name/id",0,0) + theme_bw())
223
-      # if(!input$genefinder %in% anno_gene & !input$genefinder %in% anno_id)
224
-      #   return(ggplot() + annotate("text",label="Gene not found...",0,0) + theme_bw())
215
+anno_gene <- values$myannotation$gene_name
216
+
217
+# if(is.null(input$color_by) & input$genefinder!="")
218
+#   return(ggplot() + annotate("text",label="Select a factor to plot your gene",0,0) + theme_bw())
219
+# if(is.null(input$color_by) & input$genefinder=="")
220
+#   return(ggplot() + annotate("text",label="Select a gene and a factor to plot gene",0,0) + theme_bw())
221
+# if(input$genefinder=="")
222
+#   return(ggplot() + annotate("text",label="Type in a gene name/id",0,0) + theme_bw())
223
+# if(!input$genefinder %in% anno_gene & !input$genefinder %in% anno_id)
224
+#   return(ggplot() + annotate("text",label="Gene not found...",0,0) + theme_bw())
225 225
 if(input$genefinder!="") {
226
-
226
+  
227 227
   if (input$genefinder %in% anno_id) {
228 228
     selectedGene <- rownames(values$mydst)[match(input$genefinder,rownames(values$mydst))]
229 229
     selectedGeneSymbol <- values$myannotation$gene_name[match(selectedGene,rownames(values$myannotation))]
230
-    }
231
-    if (input$genefinder %in% anno_gene) {
230
+  }
231
+  if (input$genefinder %in% anno_gene) {
232 232
     selectedGeneSymbol <- values$myannotation$gene_name[which(values$myannotation$gene_name==input$genefinder)]
233 233
     if (length(selectedGeneSymbol) > 1) return(ggplot() + annotate("text",label=paste0("Type in a gene name/id of the following:\n",paste(selectedGene,collapse=", ")),0,0) + theme_bw())
234 234
     selectedGene <- rownames(values$myannotation)[which(values$myannotation$gene_name==input$genefinder)]
235
-    }
236
-    genedata <- plotCounts(values$mydds,gene=selectedGene,intgroup = input$color_by,returnData = TRUE)
237
-    onlyfactors <- genedata[,match(input$color_by,colnames(genedata))]
238
-    genedata$plotby <- interaction(onlyfactors)
239
-    
240
-    if(input$plot_style=="boxplot"){
241
-    res <- ggplot(genedata,aes_string(x="plotby",y="count",fill="plotby")) +
242
-      geom_boxplot(outlier.shape = NA,alpha=0.7) + theme_bw()
243
-    if(input$ylimZero){
244
-      res <- res + scale_y_log10(name="Normalized counts - log10 scale",limits=c(0.4,NA))
245
-    } else {
246
-      res <- res + scale_y_log10(name="Normalized counts - log10 scale")
247
-    }
248
-    
249
-    res <- res +
250
-      labs(title=paste0("Normalized counts for ",selectedGeneSymbol," - ",selectedGene)) +
251
-      scale_x_discrete(name="") +
252
-      geom_jitter(aes_string(x="plotby",y="count"),position = position_jitter(width = 0.1)) +
253
-      scale_fill_discrete(name="Experimental\nconditions")
254
-    # exportPlots$genesBoxplot <- res
255
-    res
256
-    
257
-    } else if(input$plot_style=="violin plot"){
258
-    res <- ggplot(genedata,aes_string(x="plotby",y="count",fill="plotby")) +
259
-      geom_violin(aes_string(col="plotby"),alpha = 0.6) + theme_bw()
260
-    if(input$ylimZero){
261
-      res <- res + scale_y_log10(name="Normalized counts - log10 scale",limits=c(0.4,NA))
262
-    } else {
263
-      res <- res + scale_y_log10(name="Normalized counts - log10 scale")
264
-    }
265
-    
266
-    res <- res +
267
-      labs(title=paste0("Normalized counts for ",selectedGeneSymbol," - ",selectedGene)) +
268
-      scale_x_discrete(name="") +
269
-      geom_jitter(aes_string(x="plotby",y="count"),alpha = 0.8,position = position_jitter(width = 0.1)) +
270
-      scale_fill_discrete(name="Experimental\nconditions") + scale_color_discrete(guide="none")
271
-    # exportPlots$genefinder <- res
272
-    res
273 235
   }
236
+  genedata <- plotCounts(values$mydds,gene=selectedGene,intgroup = input$color_by,returnData = TRUE)
237
+  onlyfactors <- genedata[,match(input$color_by,colnames(genedata))]
238
+  genedata$plotby <- interaction(onlyfactors)
239
+  
240
+  if (input$plot_style == "boxplot") {
241
+    plot_style <- "boxplot"
242
+  } else if (input$plot_style == "violin plot") {
243
+    plot_style <- "violin"
244
+  } else {
245
+    plot_style <- "auto"
246
+  }
247
+  
248
+  res <- mosdef::gene_plot(de_container = values$mydds,
249
+                           gene = selectedGene,
250
+                           intgroup = input$color_by,
251
+                           annotation_obj = values$myannotation,
252
+                           normalized = TRUE,
253
+                           labels_display = input$addsamplelabels,
254
+                           plot_type = plot_style)
255
+  
256
+  if (input$ylimZero) {
257
+    res <- res + scale_y_log10(name = "Normalized counts - log10 scale", limits = c(0.4, NA))
258
+  } else {
259
+    res <- res + scale_y_log10(name = "Normalized counts - log10 scale")
260
+  }
261
+  
262
+  res <- res +
263
+    labs(title = paste0("Normalized counts for ", selectedGeneSymbol, " - ", selectedGene)) +
264
+    scale_x_discrete(name = "") +
265
+    scale_fill_discrete(name = "Experimental\nconditions")
266
+  
267
+  exportPlots$genefinder_countsplot <- res
268
+  
269
+  res
274 270
 }
275 271
 ```
276 272