Browse code

gene plot widgets now use the gene_plot function from mosdef

Federico Marini authored on 11/09/2024 23:54:57
Showing 1 changed files

... ...
@@ -40,7 +40,9 @@
40 40
 #'
41 41
 #' pcaExplorer() # and then upload count matrix, covariate matrix (and eventual annotation)
42 42
 #' }
43
-#'
43
+#' 
44
+#' @importFrom mosdef gene_plot
45
+#' 
44 46
 #' @export
45 47
 pcaExplorer <- function(dds = NULL,
46 48
                         dst = NULL,
... ...
@@ -1723,48 +1725,35 @@ pcaExplorer <- function(dds = NULL,
1723 1725
 
1724 1726
       genedata$sampleID <- rownames(genedata)
1725 1727
 
1726
-      # input$plot_style chooses the style of plotting
1727 1728
       if (input$plot_style == "boxplot") {
1728
-        res <- ggplot(genedata, aes_string(x = "plotby", y = "count", fill = "plotby")) +
1729
-          geom_boxplot(outlier.shape = NA, alpha=0.7) + theme_bw()
1730
-        if (input$ylimZero_genes) {
1731
-          res <- res + scale_y_log10(name = "Normalized counts - log10 scale", limits = c(0.4, NA))
1732
-        } else {
1733
-          res <- res + scale_y_log10(name = "Normalized counts - log10 scale")
1734
-        }
1735
-
1736
-        res <- res +
1737
-          labs(title = paste0("Normalized counts for ", selectedGeneSymbol, " - ", selectedGene)) +
1738
-          scale_x_discrete(name = "") +
1739
-          geom_jitter(aes_string(x = "plotby", y = "count"), position = position_jitter(width = 0.1)) +
1740
-          scale_fill_discrete(name = "Experimental\nconditions")
1741
-        # if(input$addsamplelabels){
1742
-        #   res <- res + geom_text(aes(label=sampleID),hjust=-.1,vjust=0)
1743
-        # }
1744
-
1745
-        exportPlots$genesBoxplot <- res
1746
-        res
1747
-      } else if (input$plot_style == "violin plot"){
1748
-        res <- ggplot(genedata, aes_string(x = "plotby", y = "count", fill = "plotby")) +
1749
-          geom_violin(aes_string(col = "plotby"), alpha = 0.6) + theme_bw()
1750
-        if (input$ylimZero_genes){
1751
-          res <- res + scale_y_log10(name = "Normalized counts - log10 scale", limits = c(0.4, NA))
1752
-        } else {
1753
-          res <- res + scale_y_log10(name = "Normalized counts - log10 scale")
1754
-        }
1755
-
1756
-        res <- res +
1757
-          labs(title = paste0("Normalized counts for ", selectedGeneSymbol, " - ", selectedGene)) +
1758
-          scale_x_discrete(name = "") +
1759
-          geom_jitter(aes_string(x = "plotby", y = "count"), alpha = 0.8, position = position_jitter(width = 0.1)) +
1760
-          scale_fill_discrete(name = "Experimental\nconditions") + scale_color_discrete(guide = "none")
1761
-        # if(input$addsamplelabels){
1762
-        #   res <- res + geom_text(aes(label=sampleID),hjust=-.1,vjust=0)
1763
-        # }
1764
-
1765
-        exportPlots$genesBoxplot <- res
1766
-        res
1729
+        plot_style <- "boxplot"
1730
+      } else if (input$plot_style == "violin plot") {
1731
+        plot_style <- "violin"
1732
+      } else {
1733
+        plot_style <- "auto"
1734
+      }
1735
+      
1736
+      res <- mosdef::gene_plot(de_container = values$mydds,
1737
+                               gene = selectedGene,
1738
+                               intgroup = input$color_by,
1739
+                               annotation_obj = values$myannotation,
1740
+                               normalized = TRUE,
1741
+                               plot_type = plot_style)
1742
+      
1743
+      if (input$ylimZero_genes) {
1744
+        res <- res + scale_y_log10(name = "Normalized counts - log10 scale", limits = c(0.4, NA))
1745
+      } else {
1746
+        res <- res + scale_y_log10(name = "Normalized counts - log10 scale")
1767 1747
       }
1748
+      
1749
+      res <- res +
1750
+        labs(title = paste0("Normalized counts for ", selectedGeneSymbol, " - ", selectedGene)) +
1751
+        scale_x_discrete(name = "") +
1752
+        scale_fill_discrete(name = "Experimental\nconditions")
1753
+      
1754
+      exportPlots$genesBoxplot <- res
1755
+      
1756
+      res
1768 1757
     })
1769 1758
 
1770 1759
     # for reading in the brushed/clicked points
... ...
@@ -1933,47 +1922,38 @@ pcaExplorer <- function(dds = NULL,
1933 1922
       onlyfactors <- genedata[, match(input$color_by, colnames(genedata))]
1934 1923
       genedata$plotby <- interaction(onlyfactors)
1935 1924
       genedata$sampleID <- rownames(genedata)
1936
-
1937
-      # input$plot_style chooses the style of plotting
1925
+      
1938 1926
       if (input$plot_style == "boxplot") {
1939
-        res <- ggplot(genedata, aes_string(x = "plotby", y = "count", fill = "plotby")) +
1940
-          geom_boxplot(outlier.shape = NA, alpha = 0.7) + theme_bw()
1941
-        if (input$ylimZero) {
1942
-          res <- res + scale_y_log10(name = "Normalized counts - log10 scale", limits = c(0.4, NA))
1943
-        } else {
1944
-          res <- res + scale_y_log10(name = "Normalized counts - log10 scale")
1945
-        }
1946
-
1947
-        res <- res +
1948
-          labs(title = paste0("Normalized counts for ", selectedGeneSymbol, " - ", selectedGene)) +
1949
-          scale_x_discrete(name = "") +
1950
-          geom_jitter(aes_string(x = "plotby", y = "count"), position = position_jitter(width = 0.1)) +
1951
-          scale_fill_discrete(name = "Experimental\nconditions")
1952
-        if (input$addsamplelabels) {
1953
-          res <- res + geom_text(aes_string(label = "sampleID"), hjust = -.1, vjust = 0)
1954
-        }
1955
-        exportPlots$genefinder_countsplot <- res
1956
-        res
1927
+        plot_style <- "boxplot"
1957 1928
       } else if (input$plot_style == "violin plot") {
1958
-        res <- ggplot(genedata, aes_string(x = "plotby", y = "count", fill = "plotby")) +
1959
-          geom_violin(aes_string(col = "plotby"), alpha = 0.6) + theme_bw()
1960
-        if (input$ylimZero) {
1961
-          res <- res + scale_y_log10(name = "Normalized counts - log10 scale", limits = c(0.4, NA))
1962
-        } else {
1963
-          res <- res + scale_y_log10(name = "Normalized counts - log10 scale")
1964
-        }
1965
-
1966
-        res <- res +
1967
-          labs(title = paste0("Normalized counts for ", selectedGeneSymbol, " - ", selectedGene)) +
1968
-          scale_x_discrete(name = "") +
1969
-          geom_jitter(aes_string(x = "plotby", y = "count"), alpha = 0.8, position = position_jitter(width = 0.1)) +
1970
-          scale_fill_discrete(name = "Experimental\nconditions") + scale_color_discrete(guide = "none")
1971
-        if (input$addsamplelabels) {
1972
-          res <- res + geom_text(aes_string(label = "sampleID"), hjust = -.1, vjust = 0)
1973
-        }
1974
-        exportPlots$genefinder_countsplot <- res
1975
-        res
1929
+        plot_style <- "violin"
1930
+      } else {
1931
+        plot_style <- "auto"
1932
+      }
1933
+      
1934
+      res <- mosdef::gene_plot(de_container = values$mydds,
1935
+                               gene = selectedGene,
1936
+                               intgroup = input$color_by,
1937
+                               annotation_obj = values$myannotation,
1938
+                               normalized = TRUE,
1939
+                               labels_display = input$addsamplelabels,
1940
+                               plot_type = plot_style)
1941
+      
1942
+      if (input$ylimZero) {
1943
+        res <- res + scale_y_log10(name = "Normalized counts - log10 scale", limits = c(0.4, NA))
1944
+      } else {
1945
+        res <- res + scale_y_log10(name = "Normalized counts - log10 scale")
1976 1946
       }
1947
+      
1948
+      res <- res +
1949
+        labs(title = paste0("Normalized counts for ", selectedGeneSymbol, " - ", selectedGene)) +
1950
+        scale_x_discrete(name = "") +
1951
+        scale_fill_discrete(name = "Experimental\nconditions")
1952
+      
1953
+      exportPlots$genefinder_countsplot <- res
1954
+      
1955
+      res
1956
+      
1977 1957
     })
1978 1958
 
1979 1959
     output$genefinder_table <- DT::renderDataTable({