Browse code

Added jackstraw plot to dimRed tab

Irzam Sarfraz authored on 07/11/2020 15:57:02
Showing 4 changed files

... ...
@@ -187,6 +187,7 @@ seuratICA <- function(inSCE, useAssay, reducedDimName = "seuratICA", nics = 20)
187 187
 #' @param inSCE (sce) object on which to compute and store jackstraw plot
188 188
 #' @param useAssay Assay containing scaled counts to use in JackStraw calculation.
189 189
 #' @param dims Number of components to test in Jackstraw. If \code{NULL}, then all components are used. Default \code{NULL}.
190
+#' @param externalReduction Pass DimReduc object if PCA/ICA computed through other libraries. Default \code{NULL}.
190 191
 #' @examples
191 192
 #' data(scExample, package = "singleCellTK")
192 193
 #' \dontrun{
... ...
@@ -198,8 +199,17 @@ seuratICA <- function(inSCE, useAssay, reducedDimName = "seuratICA", nics = 20)
198 199
 #' }
199 200
 #' @return Updated \code{SingleCellExperiment} object with jackstraw computations stored in it
200 201
 #' @export
201
-seuratComputeJackStraw <- function(inSCE, useAssay, dims = NULL) {
202
+seuratComputeJackStraw <- function(inSCE, useAssay, dims = NULL, externalReduction = NULL) {
202 203
   seuratObject <- convertSCEToSeurat(inSCE, scaledAssay = useAssay)
204
+  if(!is.null(externalReduction)){
205
+    seuratObject <- FindVariableFeatures(seuratObject)
206
+    seuratObject <- ScaleData(seuratObject)
207
+    seuratObject@reductions <- list(pca = externalReduction)
208
+    seuratObject@[email protected] <- seuratObject@[email protected][match(rownames(GetAssayData(seuratObject, assay = "RNA", slot = "scale.data")), rownames(seuratObject@[email protected])),]
209
+    seuratObject@commands$RunPCA.RNA <- seuratObject@commands$NormalizeData.RNA
210
+    seuratObject@commands$RunPCA.RNA@params$rev.pca <- FALSE
211
+    seuratObject@commands$RunPCA.RNA@params$weight.by.var <- TRUE
212
+    }
203 213
   if(is.null(seuratObject@reductions[["pca"]])) {
204 214
     stop("'seuratPCA' must be run before JackStraw can be computed.")
205 215
   }
... ...
@@ -216,6 +226,7 @@ seuratComputeJackStraw <- function(inSCE, useAssay, dims = NULL) {
216 226
 #' Computes the plot object for jackstraw plot from the pca slot in the input sce object
217 227
 #' @param inSCE (sce) object from which to compute the jackstraw plot (pca should be computed)
218 228
 #' @param dims Number of components to plot in Jackstraw. If \code{NULL}, then all components are plotted Default \code{NULL}.
229
+#' @param externalReduction Pass DimReduc object if PCA/ICA computed through other libraries. Default \code{NULL}.
219 230
 #' @examples
220 231
 #' data(scExample, package = "singleCellTK")
221 232
 #' \dontrun{
... ...
@@ -228,8 +239,11 @@ seuratComputeJackStraw <- function(inSCE, useAssay, dims = NULL) {
228 239
 #' }
229 240
 #' @return plot object
230 241
 #' @export
231
-seuratJackStrawPlot <- function(inSCE, dims = NULL) {
242
+seuratJackStrawPlot <- function(inSCE, dims = NULL, externalReduction = NULL) {
232 243
   seuratObject <- convertSCEToSeurat(inSCE)
244
+  if(!is.null(externalReduction)){
245
+    seuratObject@reductions <- list(pca = externalReduction)
246
+  }
233 247
   if(is.null(seuratObject@reductions[["pca"]])) {
234 248
     stop("'seuratPCA' must be run before JackStraw can be computed.")
235 249
   }
... ...
@@ -2105,8 +2105,18 @@ shinyServer(function(input, output, session) {
2105 2105
     
2106 2106
     
2107 2107
     #extra code added by irzam starts here:
2108
+    redDim <- reducedDim(vals$counts, gsub(" ", "_", input$dimRedNameInput))
2109
+    new_pca <- CreateDimReducObject(
2110
+      embeddings = redDim, 
2111
+      assay = "RNA",
2112
+      loadings = attr(redDim, "rotation"),
2113
+      stdev = as.numeric(attr(redDim, "percentVar")), 
2114
+      key = "PC_")
2115
+    
2108 2116
     removeTab(inputId = "dimRedPCAICA_plotTabset", target = "PCA Plot")
2109 2117
     removeTab(inputId = "dimRedPCAICA_plotTabset", target = "Elbow Plot")
2118
+    removeTab(inputId = "dimRedPCAICA_plotTabset", target = "Heatmap Plot")
2119
+    removeTab(inputId = "dimRedPCAICA_plotTabset", target = "JackStraw Plot")
2110 2120
     
2111 2121
     appendTab(inputId = "dimRedPCAICA_plotTabset", tabPanel(title = "PCA Plot",
2112 2122
                                                             panel(heading = "PCA Plot",
... ...
@@ -2130,13 +2140,7 @@ shinyServer(function(input, output, session) {
2130 2140
                                                           )
2131 2141
       ))
2132 2142
       
2133
-      redDim <- reducedDim(vals$counts, gsub(" ", "_", input$dimRedNameInput))
2134 2143
       withProgress(message = "Generating Elbow Plot", max = 1, value = 1, {
2135
-        new_pca <- CreateDimReducObject(
2136
-          embeddings = redDim, 
2137
-          assay = "RNA",
2138
-          stdev = as.numeric(attr(redDim, "percentVar")), 
2139
-          key = "PC_")
2140 2144
         
2141 2145
         output$plotDimRed_elbow <- renderPlotly({
2142 2146
           seuratElbowPlot(inSCE = vals$counts,
... ...
@@ -2177,12 +2181,6 @@ shinyServer(function(input, output, session) {
2177 2181
         ))
2178 2182
         
2179 2183
         withProgress(message = "Generating Heatmaps", max = 1, value = 1, {
2180
-          new_pca <- CreateDimReducObject(
2181
-            embeddings = redDim, 
2182
-            assay = "RNA",
2183
-            loadings = attr(redDim, "rotation"),
2184
-            stdev = as.numeric(attr(redDim, "percentVar")), 
2185
-            key = "PC_")
2186 2184
           vals$counts@metadata$seurat$heatmap_dimRed <- seuratComputeHeatmap(inSCE = vals$counts,
2187 2185
                                                                           useAssay = input$dimRedAssaySelect,
2188 2186
                                                                           useReduction = "pca",
... ...
@@ -2198,6 +2196,23 @@ shinyServer(function(input, output, session) {
2198 2196
                               labels = c("PC1", "PC2", "PC3", "PC4"))
2199 2197
           })
2200 2198
         })
2199
+        
2200
+          appendTab(inputId = "dimRedPCAICA_plotTabset", tabPanel(title = "JackStraw Plot",
2201
+                                                              panel(heading = "JackStraw Plot",
2202
+                                                                    plotlyOutput(outputId = "plot_jackstraw_dimRed")
2203
+                                                              )
2204
+          ))
2205
+          
2206
+          withProgress(message = "Generating JackStraw Plot", max = 1, value = 1, {
2207
+            vals$counts <- seuratComputeJackStraw(inSCE = vals$counts,
2208
+                                                  useAssay = input$dimRedAssaySelect,
2209
+                                                  dims = 10,
2210
+                                                  externalReduction = new_pca)
2211
+            output$plot_jackstraw_dimRed <- renderPlotly({
2212
+              plotly::ggplotly(seuratJackStrawPlot(inSCE = vals$counts,
2213
+                                                   dims = 10))
2214
+            })
2215
+          })
2201 2216
   })
2202 2217
   
2203 2218
   observeEvent(input$runDimred_tsneUmap, {
... ...
@@ -5,7 +5,7 @@
5 5
 \title{seuratComputeJackStraw
6 6
 Compute jackstraw plot and store the computations in the input sce object}
7 7
 \usage{
8
-seuratComputeJackStraw(inSCE, useAssay, dims = NULL)
8
+seuratComputeJackStraw(inSCE, useAssay, dims = NULL, externalReduction = NULL)
9 9
 }
10 10
 \arguments{
11 11
 \item{inSCE}{(sce) object on which to compute and store jackstraw plot}
... ...
@@ -13,6 +13,8 @@ seuratComputeJackStraw(inSCE, useAssay, dims = NULL)
13 13
 \item{useAssay}{Assay containing scaled counts to use in JackStraw calculation.}
14 14
 
15 15
 \item{dims}{Number of components to test in Jackstraw. If \code{NULL}, then all components are used. Default \code{NULL}.}
16
+
17
+\item{externalReduction}{Pass DimReduc object if PCA/ICA computed through other libraries. Default \code{NULL}.}
16 18
 }
17 19
 \value{
18 20
 Updated \code{SingleCellExperiment} object with jackstraw computations stored in it
... ...
@@ -5,12 +5,14 @@
5 5
 \title{seuratJackStrawPlot
6 6
 Computes the plot object for jackstraw plot from the pca slot in the input sce object}
7 7
 \usage{
8
-seuratJackStrawPlot(inSCE, dims = NULL)
8
+seuratJackStrawPlot(inSCE, dims = NULL, externalReduction = NULL)
9 9
 }
10 10
 \arguments{
11 11
 \item{inSCE}{(sce) object from which to compute the jackstraw plot (pca should be computed)}
12 12
 
13 13
 \item{dims}{Number of components to plot in Jackstraw. If \code{NULL}, then all components are plotted Default \code{NULL}.}
14
+
15
+\item{externalReduction}{Pass DimReduc object if PCA/ICA computed through other libraries. Default \code{NULL}.}
14 16
 }
15 17
 \value{
16 18
 plot object