Browse code

Deprecate `getTSNE`, add `runTSNE` and `runQuickTSNE`

Yichen Wang authored on 28/09/2022 06:39:03
Showing1 changed files
... ...
@@ -1,7 +1,7 @@
1 1
 #' Generic Wrapper function for running dimensionality reduction
2 2
 #' @details Wrapper function to run one of the available dimensionality
3 3
 #' reduction algorithms integrated within SCTK from \code{\link{scaterPCA}},
4
-#' \code{\link{runSeuratPCA}}, \code{\link{runSeuratICA}}, \code{\link{getTSNE}},
4
+#' \code{\link{runSeuratPCA}}, \code{\link{runSeuratICA}}, \code{\link{runTSNE}},
5 5
 #' \code{\link{runSeuratTSNE}}, \code{\link{runUMAP}} and
6 6
 #' \code{\link{runSeuratUMAP}}. Users can use an assay by specifying
7 7
 #' \code{useAssay}, use the assay in an altExp by specifying both
... ...
@@ -77,7 +77,7 @@ runDimReduce <- function(inSCE,
77 77
                      useFeatureSubset = useFeatureSubset, scale = scale,
78 78
                      reducedDimName = reducedDimName, seed = seed, ...)
79 79
   } else if (method == "rTSNE") {
80
-    inSCE <- getTSNE(inSCE = inSCE, useAssay = useAssay, useAltExp = useAltExp,
80
+    inSCE <- runTSNE(inSCE = inSCE, useAssay = useAssay, useAltExp = useAltExp,
81 81
                      useReducedDim = useReducedDim,
82 82
                      useFeatureSubset = useFeatureSubset, scale = scale,
83 83
                      reducedDimName = reducedDimName, seed = seed, ...)
Browse code

Deprecate "getUMAP", add "runUMAP" and "runQuickUMAP"

Yichen Wang authored on 28/09/2022 03:49:28
Showing1 changed files
... ...
@@ -2,7 +2,7 @@
2 2
 #' @details Wrapper function to run one of the available dimensionality
3 3
 #' reduction algorithms integrated within SCTK from \code{\link{scaterPCA}},
4 4
 #' \code{\link{runSeuratPCA}}, \code{\link{runSeuratICA}}, \code{\link{getTSNE}},
5
-#' \code{\link{runSeuratTSNE}}, \code{\link{getUMAP}} and
5
+#' \code{\link{runSeuratTSNE}}, \code{\link{runUMAP}} and
6 6
 #' \code{\link{runSeuratUMAP}}. Users can use an assay by specifying
7 7
 #' \code{useAssay}, use the assay in an altExp by specifying both
8 8
 #' \code{useAltExp} and \code{useAssay}, or use a low-dimensionality
... ...
@@ -72,7 +72,7 @@ runDimReduce <- function(inSCE,
72 72
                        useFeatureSubset = useFeatureSubset, scale = scale,
73 73
                        seed = seed, ...)
74 74
   } else if (method == "scaterUMAP") {
75
-    inSCE <- getUMAP(inSCE = inSCE, useAssay = useAssay, useAltExp = useAltExp,
75
+    inSCE <- runUMAP(inSCE = inSCE, useAssay = useAssay, useAltExp = useAltExp,
76 76
                      useReducedDim = useReducedDim,
77 77
                      useFeatureSubset = useFeatureSubset, scale = scale,
78 78
                      reducedDimName = reducedDimName, seed = seed, ...)
Browse code

Add .selectSCEMatrix() and .manageCellVar; together with refining batch correction functions

Yichen Wang authored on 11/08/2022 16:49:05
Showing1 changed files
... ...
@@ -1,65 +1,3 @@
1
-.matrixTypeCheck <- function(inSCE, redDimType = NULL,
2
-                             useAssay = NULL, useReducedDim = NULL,
3
-                             useAltExp = NULL) {
4
-  # Helper function for checking if the specified matrix type is valid
5
-  if (!inherits(inSCE, "SingleCellExperiment")){
6
-    stop("Please use a SingleCellExperiment object")
7
-  }
8
-  if (redDimType == "embedding") {
9
-    if (is.null(useAssay) && is.null(useReducedDim)) {
10
-      stop("`useAssay` and `useReducedDim` cannot be NULL at the same time.")
11
-    } else if (!is.null(useAssay) && !is.null(useReducedDim)) {
12
-      stop("`useAssay` and `useReducedDim` cannot be specified at the same time.")
13
-    } else {
14
-      if (!is.null(useReducedDim)) {
15
-        if (!useReducedDim %in% SingleCellExperiment::reducedDimNames(inSCE)) {
16
-          stop("Specified `useReducedDim` not found.")
17
-        }
18
-        if (!is.null(useAltExp)) {
19
-          warning("`useAltExp` will be ignored when using `useReducedDim`.")
20
-        }
21
-        sce <- inSCE
22
-      } else {
23
-        if (!is.null(useAltExp)) {
24
-          if (!useAltExp %in% SingleCellExperiment::altExpNames(inSCE)) {
25
-            stop("Specified `useAltExp` not found.")
26
-          }
27
-          sce <- SingleCellExperiment::altExp(inSCE, useAltExp)
28
-          if (!useAssay %in% SummarizedExperiment::assayNames(sce)) {
29
-            stop("Specified `useAssay` not found in `useAltExp`.")
30
-          }
31
-        } else {
32
-          if (!useAssay %in% SummarizedExperiment::assayNames(inSCE)) {
33
-            stop("Specified `useAssay` not found.")
34
-          }
35
-          sce <- inSCE
36
-        }
37
-      }
38
-    }
39
-  } else if (redDimType == "linear") {
40
-    if (!is.null(useReducedDim)) {
41
-      stop("Currently `useReducedDim` is not allowed for linear dimension reduction.")
42
-    }
43
-    if (is.null(useAssay)) {
44
-      stop("`useAssay` cannot be NULL for linear dimension reduction")
45
-    } else {
46
-      if (is.null(useAltExp)) {
47
-        if (!useAssay %in% SummarizedExperiment::assayNames(inSCE)) {
48
-          stop("Specified `useAssay` not found.")
49
-        }
50
-      } else {
51
-        if (!useAltExp %in% SingleCellExperiment::altExpNames(inSCE)) {
52
-          stop("Specified `useAltExp` not found.")
53
-        }
54
-        sce <- SingleCellExperiment::altExp(inSCE, useAltExp)
55
-        if (!useAssay %in% SummarizedExperiment::assayNames(sce)) {
56
-          stop("Specified `useAssay` not found in `useAltExp`.")
57
-        }
58
-      }
59
-    }
60
-  }
61
-}
62
-
63 1
 #' Generic Wrapper function for running dimensionality reduction
64 2
 #' @details Wrapper function to run one of the available dimensionality
65 3
 #' reduction algorithms integrated within SCTK from \code{\link{scaterPCA}},
... ...
@@ -81,9 +19,9 @@
81 19
 #' @param useReducedDim The low dimension representation to use for embedding
82 20
 #' computation. Default \code{NULL}.
83 21
 #' @param reducedDimName The name of the result matrix. Required.
84
-#' @param useFeatureSubset Subset of feature to use for dimension reduction. A 
22
+#' @param useFeatureSubset Subset of feature to use for dimension reduction. A
85 23
 #' character string indicating a \code{rowData} variable that stores the logical
86
-#' vector of HVG selection, or a vector that can subset the rows of 
24
+#' vector of HVG selection, or a vector that can subset the rows of
87 25
 #' \code{inSCE}. Default \code{NULL}.
88 26
 #' @param scale Logical scalar, whether to standardize the expression values.
89 27
 #' Default \code{TRUE}.
... ...
@@ -115,39 +53,43 @@ runDimReduce <- function(inSCE,
115 53
                                     "scaterUMAP",
116 54
                                     "seuratUMAP"),
117 55
                          useAssay = NULL, useReducedDim = NULL,
118
-                         useAltExp = NULL, reducedDimName = method, 
119
-                         nComponents = 20, useFeatureSubset = NULL, 
120
-                         scale = FALSE, seed = NULL, ...) 
56
+                         useAltExp = NULL, reducedDimName = method,
57
+                         nComponents = 20, useFeatureSubset = NULL,
58
+                         scale = FALSE, seed = NULL, ...)
121 59
 {
122 60
 
123 61
   method <- match.arg(method)
124 62
   args <- list(...)
125
-  
126
-  if (method %in% c("scaterPCA", "seuratPCA", "seuratICA")) {
127
-    .matrixTypeCheck(inSCE, "linear", useAssay, useReducedDim, useAltExp)
128
-  } else {
129
-    .matrixTypeCheck(inSCE, "embedding", useAssay, useReducedDim, useAltExp)
63
+  if (method %in% c("scaterPCA", "seuratPCA", "seuratICA") &
64
+      !is.null(useReducedDim)) {
65
+    stop("`useReducedDim` is not allowed for linear dimension reduction.")
130 66
   }
131 67
 
132 68
   if (method == "scaterPCA") {
133
-    inSCE <- scaterPCA(inSCE = inSCE, useAssay = useAssay, 
134
-                       useAltExp = useAltExp, reducedDimName = reducedDimName, 
135
-                       nComponents = nComponents, 
136
-                       useFeatureSubset = useFeatureSubset, scale = scale, 
69
+    inSCE <- scaterPCA(inSCE = inSCE, useAssay = useAssay,
70
+                       useAltExp = useAltExp, reducedDimName = reducedDimName,
71
+                       nComponents = nComponents,
72
+                       useFeatureSubset = useFeatureSubset, scale = scale,
137 73
                        seed = seed, ...)
138 74
   } else if (method == "scaterUMAP") {
139 75
     inSCE <- getUMAP(inSCE = inSCE, useAssay = useAssay, useAltExp = useAltExp,
140
-                     useReducedDim = useReducedDim, 
141
-                     useFeatureSubset = useFeatureSubset, scale = scale, 
76
+                     useReducedDim = useReducedDim,
77
+                     useFeatureSubset = useFeatureSubset, scale = scale,
142 78
                      reducedDimName = reducedDimName, seed = seed, ...)
143 79
   } else if (method == "rTSNE") {
144 80
     inSCE <- getTSNE(inSCE = inSCE, useAssay = useAssay, useAltExp = useAltExp,
145
-                     useReducedDim = useReducedDim, 
146
-                     useFeatureSubset = useFeatureSubset, scale = scale, 
81
+                     useReducedDim = useReducedDim,
82
+                     useFeatureSubset = useFeatureSubset, scale = scale,
147 83
                      reducedDimName = reducedDimName, seed = seed, ...)
148 84
   } else {
149 85
     # Seurat part
150
-    
86
+    # TODO: Honestly, the input checks should have been implemented for
87
+    # functions being wrapped because they are being exposed to users as well.
88
+    # We should not being performing redundant checks when wrapping them again.
89
+    useMat <- .selectSCEMatrix(inSCE, useAssay = useAssay,
90
+                               useReducedDim = useReducedDim,
91
+                               useAltExp = useAltExp, returnMatrix = FALSE)
92
+    useAssay <- useMat$names$useAssay
151 93
     if (!is.null(useAltExp)) {
152 94
       tempSCE <- SingleCellExperiment::altExp(inSCE, useAltExp)
153 95
     } else if (!is.null(useAssay)) {
... ...
@@ -157,17 +99,17 @@ runDimReduce <- function(inSCE,
157 99
       ## SeuratPCA/ICA
158 100
       if (method == "seuratPCA") {
159 101
         message(paste0(date(), " ... Computing Seurat PCA."))
160
-        tempSCE <- runSeuratPCA(tempSCE, useAssay = useAssay, 
102
+        tempSCE <- runSeuratPCA(tempSCE, useAssay = useAssay,
161 103
                                 reducedDimName = reducedDimName,
162
-                                nPCs = nComponents, 
104
+                                nPCs = nComponents,
163 105
                                 useFeatureSubset = useFeatureSubset,
164 106
                                 scale = scale, seed = seed, ...)
165 107
       } else if (method == "seuratICA") {
166 108
         message(paste0(date(), " ... Computing Seurat ICA."))
167 109
         tempSCE <- runSeuratICA(tempSCE, useAssay = useAssay,
168 110
                                 reducedDimName = reducedDimName,
169
-                                nics = nComponents, 
170
-                                useFeatureSubset = useFeatureSubset, 
111
+                                nics = nComponents,
112
+                                useFeatureSubset = useFeatureSubset,
171 113
                                 scale = scale, seed = seed, ...)
172 114
       }
173 115
       seuratObj <- tempSCE@metadata$seurat
... ...
@@ -187,24 +129,24 @@ runDimReduce <- function(inSCE,
187 129
           message(paste0(date(), " ... Computing Seurat PCA."))
188 130
           tempSCE <- runSeuratPCA(inSCE = tempSCE,
189 131
                                useAssay = useAssay,
190
-                               reducedDimName = paste0(useAssay, "_seuratPCA"), 
132
+                               reducedDimName = paste0(useAssay, "_seuratPCA"),
191 133
                                useFeatureSubset = useFeatureSubset, seed = seed)
192 134
         } else if (args$useReduction == "ica") {
193 135
           message(paste0(date(), " ... Computing Seurat ICA."))
194 136
           tempSCE <- runSeuratICA(inSCE = tempSCE,
195 137
                                useAssay = useAssay,
196
-                               reducedDimName = paste0(useAssay, "_seuratICA"), 
138
+                               reducedDimName = paste0(useAssay, "_seuratICA"),
197 139
                                useFeatureSubset = useFeatureSubset, seed = seed)
198 140
         }
199 141
         if (method == "seuratUMAP") {
200 142
           message(paste0(date(), " ... Computing Seurat UMAP."))
201
-          tempSCE <- runSeuratUMAP(inSCE = tempSCE, 
202
-                                   reducedDimName = reducedDimName, 
143
+          tempSCE <- runSeuratUMAP(inSCE = tempSCE,
144
+                                   reducedDimName = reducedDimName,
203 145
                                    seed = seed, ...)
204 146
         } else {
205 147
           message(paste0(date(), " ... Computing Seurat tSNE."))
206 148
           tempSCE <- runSeuratTSNE(inSCE = tempSCE,
207
-                                   reducedDimName = reducedDimName, 
149
+                                   reducedDimName = reducedDimName,
208 150
                                    seed = seed, ...)
209 151
         }
210 152
       } else {
... ...
@@ -227,13 +169,13 @@ runDimReduce <- function(inSCE,
227 169
           # hard-code useReduction="pca"
228 170
           message(paste0(date(), " ... Computing Seurat UMAP."))
229 171
           tempSCE <- runSeuratUMAP(inSCE = tempSCE, useReduction = "pca",
230
-                                   reducedDimName = reducedDimName, 
172
+                                   reducedDimName = reducedDimName,
231 173
                                    seed = seed, ...)
232 174
         } else {
233 175
           # hard-code useReduction="pca"
234 176
           message(paste0(date(), " ... Computing Seurat tSNE."))
235 177
           tempSCE <- runSeuratTSNE(inSCE = tempSCE, useReduction = "pca",
236
-                                   reducedDimName = reducedDimName, 
178
+                                   reducedDimName = reducedDimName,
237 179
                                    seed = seed, ...)
238 180
         }
239 181
       }
Browse code

Update feature selection related functions

Yichen Wang authored on 30/05/2022 22:53:42
Showing1 changed files
... ...
@@ -81,8 +81,10 @@
81 81
 #' @param useReducedDim The low dimension representation to use for embedding
82 82
 #' computation. Default \code{NULL}.
83 83
 #' @param reducedDimName The name of the result matrix. Required.
84
-#' @param useHVGList A character string indicating a \code{rowData} variable 
85
-#' that stores the logical vector of HVG selection. Default \code{NULL}.
84
+#' @param useFeatureSubset Subset of feature to use for dimension reduction. A 
85
+#' character string indicating a \code{rowData} variable that stores the logical
86
+#' vector of HVG selection, or a vector that can subset the rows of 
87
+#' \code{inSCE}. Default \code{NULL}.
86 88
 #' @param scale Logical scalar, whether to standardize the expression values.
87 89
 #' Default \code{TRUE}.
88 90
 #' @param nComponents Specify the number of dimensions to compute with the
... ...
@@ -114,8 +116,8 @@ runDimReduce <- function(inSCE,
114 116
                                     "seuratUMAP"),
115 117
                          useAssay = NULL, useReducedDim = NULL,
116 118
                          useAltExp = NULL, reducedDimName = method, 
117
-                         nComponents = 20, useHVGList = NULL, scale = FALSE,
118
-                         seed = NULL, ...) 
119
+                         nComponents = 20, useFeatureSubset = NULL, 
120
+                         scale = FALSE, seed = NULL, ...) 
119 121
 {
120 122
 
121 123
   method <- match.arg(method)
... ...
@@ -130,18 +132,19 @@ runDimReduce <- function(inSCE,
130 132
   if (method == "scaterPCA") {
131 133
     inSCE <- scaterPCA(inSCE = inSCE, useAssay = useAssay, 
132 134
                        useAltExp = useAltExp, reducedDimName = reducedDimName, 
133
-                       nComponents = nComponents, useHVGList = useHVGList,
134
-                       scale = scale, seed = seed, ...)
135
+                       nComponents = nComponents, 
136
+                       useFeatureSubset = useFeatureSubset, scale = scale, 
137
+                       seed = seed, ...)
135 138
   } else if (method == "scaterUMAP") {
136 139
     inSCE <- getUMAP(inSCE = inSCE, useAssay = useAssay, useAltExp = useAltExp,
137
-                     useReducedDim = useReducedDim, useHVGList = useHVGList,
138
-                     scale = scale, reducedDimName = reducedDimName, 
139
-                     seed = seed, ...)
140
+                     useReducedDim = useReducedDim, 
141
+                     useFeatureSubset = useFeatureSubset, scale = scale, 
142
+                     reducedDimName = reducedDimName, seed = seed, ...)
140 143
   } else if (method == "rTSNE") {
141 144
     inSCE <- getTSNE(inSCE = inSCE, useAssay = useAssay, useAltExp = useAltExp,
142
-                     useReducedDim = useReducedDim, useHVGList = useHVGList,
143
-                     scale = scale, reducedDimName = reducedDimName, 
144
-                     seed = seed, ...)
145
+                     useReducedDim = useReducedDim, 
146
+                     useFeatureSubset = useFeatureSubset, scale = scale, 
147
+                     reducedDimName = reducedDimName, seed = seed, ...)
145 148
   } else {
146 149
     # Seurat part
147 150
     
... ...
@@ -156,13 +159,15 @@ runDimReduce <- function(inSCE,
156 159
         message(paste0(date(), " ... Computing Seurat PCA."))
157 160
         tempSCE <- runSeuratPCA(tempSCE, useAssay = useAssay, 
158 161
                                 reducedDimName = reducedDimName,
159
-                                nPCs = nComponents, useHVGList = useHVGList,
162
+                                nPCs = nComponents, 
163
+                                useFeatureSubset = useFeatureSubset,
160 164
                                 scale = scale, seed = seed, ...)
161 165
       } else if (method == "seuratICA") {
162 166
         message(paste0(date(), " ... Computing Seurat ICA."))
163 167
         tempSCE <- runSeuratICA(tempSCE, useAssay = useAssay,
164 168
                                 reducedDimName = reducedDimName,
165
-                                nics = nComponents, useHVGList = useHVGList, 
169
+                                nics = nComponents, 
170
+                                useFeatureSubset = useFeatureSubset, 
166 171
                                 scale = scale, seed = seed, ...)
167 172
       }
168 173
       seuratObj <- tempSCE@metadata$seurat
... ...
@@ -182,21 +187,25 @@ runDimReduce <- function(inSCE,
182 187
           message(paste0(date(), " ... Computing Seurat PCA."))
183 188
           tempSCE <- runSeuratPCA(inSCE = tempSCE,
184 189
                                useAssay = useAssay,
185
-                               reducedDimName = paste0(useAssay, "_seuratPCA"), seed = seed)
190
+                               reducedDimName = paste0(useAssay, "_seuratPCA"), 
191
+                               useFeatureSubset = useFeatureSubset, seed = seed)
186 192
         } else if (args$useReduction == "ica") {
187 193
           message(paste0(date(), " ... Computing Seurat ICA."))
188 194
           tempSCE <- runSeuratICA(inSCE = tempSCE,
189 195
                                useAssay = useAssay,
190
-                               reducedDimName = paste0(useAssay, "_seuratICA"), seed = seed)
196
+                               reducedDimName = paste0(useAssay, "_seuratICA"), 
197
+                               useFeatureSubset = useFeatureSubset, seed = seed)
191 198
         }
192 199
         if (method == "seuratUMAP") {
193 200
           message(paste0(date(), " ... Computing Seurat UMAP."))
194
-          tempSCE <- runSeuratUMAP(inSCE = tempSCE,
195
-                                   reducedDimName = reducedDimName, seed = seed, ...)
201
+          tempSCE <- runSeuratUMAP(inSCE = tempSCE, 
202
+                                   reducedDimName = reducedDimName, 
203
+                                   seed = seed, ...)
196 204
         } else {
197 205
           message(paste0(date(), " ... Computing Seurat tSNE."))
198 206
           tempSCE <- runSeuratTSNE(inSCE = tempSCE,
199
-                                   reducedDimName = reducedDimName, seed = seed, ...)
207
+                                   reducedDimName = reducedDimName, 
208
+                                   seed = seed, ...)
200 209
         }
201 210
       } else {
202 211
         ### using external reducedDim
... ...
@@ -218,12 +227,14 @@ runDimReduce <- function(inSCE,
218 227
           # hard-code useReduction="pca"
219 228
           message(paste0(date(), " ... Computing Seurat UMAP."))
220 229
           tempSCE <- runSeuratUMAP(inSCE = tempSCE, useReduction = "pca",
221
-                                   reducedDimName = reducedDimName, seed = seed, ...)
230
+                                   reducedDimName = reducedDimName, 
231
+                                   seed = seed, ...)
222 232
         } else {
223 233
           # hard-code useReduction="pca"
224 234
           message(paste0(date(), " ... Computing Seurat tSNE."))
225 235
           tempSCE <- runSeuratTSNE(inSCE = tempSCE, useReduction = "pca",
226
-                                   reducedDimName = reducedDimName, seed = seed, ...)
236
+                                   reducedDimName = reducedDimName, 
237
+                                   seed = seed, ...)
227 238
         }
228 239
       }
229 240
     }
Browse code

Update HVG PCA UMAP cluster UI

Yichen Wang authored on 23/05/2022 18:48:24
Showing1 changed files
... ...
@@ -81,6 +81,10 @@
81 81
 #' @param useReducedDim The low dimension representation to use for embedding
82 82
 #' computation. Default \code{NULL}.
83 83
 #' @param reducedDimName The name of the result matrix. Required.
84
+#' @param useHVGList A character string indicating a \code{rowData} variable 
85
+#' that stores the logical vector of HVG selection. Default \code{NULL}.
86
+#' @param scale Logical scalar, whether to standardize the expression values.
87
+#' Default \code{TRUE}.
84 88
 #' @param nComponents Specify the number of dimensions to compute with the
85 89
 #'  selected method in case of PCA/ICA and the number of components to
86 90
 #'  use in the case of TSNE/UMAP methods.
... ...
@@ -109,14 +113,14 @@ runDimReduce <- function(inSCE,
109 113
                                     "scaterUMAP",
110 114
                                     "seuratUMAP"),
111 115
                          useAssay = NULL, useReducedDim = NULL,
112
-                         useAltExp = NULL, reducedDimName, nComponents = 20, seed = NULL, ...
113
-) {
116
+                         useAltExp = NULL, reducedDimName = method, 
117
+                         nComponents = 20, useHVGList = NULL, scale = FALSE,
118
+                         seed = NULL, ...) 
119
+{
114 120
 
115 121
   method <- match.arg(method)
116 122
   args <- list(...)
117
-  if (is.null(reducedDimName)) {
118
-    stop("Must specify `reducedDimName` to store the result.")
119
-  }
123
+  
120 124
   if (method %in% c("scaterPCA", "seuratPCA", "seuratICA")) {
121 125
     .matrixTypeCheck(inSCE, "linear", useAssay, useReducedDim, useAltExp)
122 126
   } else {
... ...
@@ -124,41 +128,42 @@ runDimReduce <- function(inSCE,
124 128
   }
125 129
 
126 130
   if (method == "scaterPCA") {
127
-    message(paste0(date(), " ... Computing Scater PCA."))
128
-    inSCE <- scaterPCA(inSCE = inSCE, useAssay = useAssay, useAltExp = useAltExp,
129
-                       reducedDimName = reducedDimName, nComponents = nComponents, seed = seed, ...)
131
+    inSCE <- scaterPCA(inSCE = inSCE, useAssay = useAssay, 
132
+                       useAltExp = useAltExp, reducedDimName = reducedDimName, 
133
+                       nComponents = nComponents, useHVGList = useHVGList,
134
+                       scale = scale, seed = seed, ...)
130 135
   } else if (method == "scaterUMAP") {
131
-    message(paste0(date(), " ... Computing Scater UMAP."))
132 136
     inSCE <- getUMAP(inSCE = inSCE, useAssay = useAssay, useAltExp = useAltExp,
133
-                     useReducedDim = useReducedDim,
134
-                     reducedDimName = reducedDimName, seed = seed, ...)
137
+                     useReducedDim = useReducedDim, useHVGList = useHVGList,
138
+                     scale = scale, reducedDimName = reducedDimName, 
139
+                     seed = seed, ...)
135 140
   } else if (method == "rTSNE") {
136
-    message(paste0(date(), " ... Computing RtSNE."))
137 141
     inSCE <- getTSNE(inSCE = inSCE, useAssay = useAssay, useAltExp = useAltExp,
138
-                     useReducedDim = useReducedDim,
139
-                     reducedDimName = reducedDimName, seed = seed, ...)
142
+                     useReducedDim = useReducedDim, useHVGList = useHVGList,
143
+                     scale = scale, reducedDimName = reducedDimName, 
144
+                     seed = seed, ...)
140 145
   } else {
141 146
     # Seurat part
147
+    
142 148
     if (!is.null(useAltExp)) {
143 149
       tempSCE <- SingleCellExperiment::altExp(inSCE, useAltExp)
144
-      # tempSCE <- runSeuratFindHVG(inSCE = tempSCE, useAssay = useAssay,
145
-      #                          altExp = TRUE)
146 150
     } else if (!is.null(useAssay)) {
147 151
       tempSCE <- inSCE
148
-      #tempSCE <- runSeuratFindHVG(inSCE = tempSCE, useAssay = useAssay)
149 152
     }
150 153
     if (method %in% c("seuratPCA", "seuratICA")) {
151 154
       ## SeuratPCA/ICA
152 155
       if (method == "seuratPCA") {
153 156
         message(paste0(date(), " ... Computing Seurat PCA."))
154
-        tempSCE <- runSeuratPCA(tempSCE, useAssay = useAssay,
155
-                             reducedDimName = reducedDimName,
156
-                             nPCs = nComponents, features = rownames(inSCE), seed = seed, ...)
157
+        tempSCE <- runSeuratPCA(tempSCE, useAssay = useAssay, 
158
+                                reducedDimName = reducedDimName,
159
+                                nPCs = nComponents, useHVGList = useHVGList,
160
+                                scale = scale, seed = seed, ...)
157 161
       } else if (method == "seuratICA") {
158 162
         message(paste0(date(), " ... Computing Seurat ICA."))
159 163
         tempSCE <- runSeuratICA(tempSCE, useAssay = useAssay,
160
-                             reducedDimName = reducedDimName,
161
-                             nics = nComponents, seed = seed, ...)
164
+                                reducedDimName = reducedDimName,
165
+                                nics = nComponents, useHVGList = useHVGList, 
166
+                                scale = scale, seed = seed, ...)
162 167
       }
163 168
       seuratObj <- tempSCE@metadata$seurat
164 169
       if (!is.null(useAltExp)) {
Browse code

Function updates for HVG, PCA, UMAP and TSNE

Yichen Wang authored on 20/05/2022 14:07:18
Showing1 changed files
... ...
@@ -95,11 +95,10 @@
95 95
 #' data(scExample, package = "singleCellTK")
96 96
 #' sce <- subsetSCECols(sce, colData = "type != 'EmptyDroplet'")
97 97
 #' sce <- runNormalization(sce, useAssay = "counts",
98
-#'                         outAssayName = "logcounts_scaled",
99
-#'                         normalizationMethod = "logNormCounts",
100
-#'                         scale = TRUE)
98
+#'                         outAssayName = "logcounts",
99
+#'                         normalizationMethod = "logNormCounts")
101 100
 #' sce <- runDimReduce(inSCE = sce, method = "scaterPCA",
102
-#'                     useAssay = "logcounts_scaled",
101
+#'                     useAssay = "logcounts", scale = TRUE,
103 102
 #'                     reducedDimName = "PCA")
104 103
 runDimReduce <- function(inSCE,
105 104
                          method = c("scaterPCA",
Browse code

R CMD check fixes

Irzam Sarfraz authored on 19/04/2022 11:51:35
Showing1 changed files
... ...
@@ -63,9 +63,9 @@
63 63
 #' Generic Wrapper function for running dimensionality reduction
64 64
 #' @details Wrapper function to run one of the available dimensionality
65 65
 #' reduction algorithms integrated within SCTK from \code{\link{scaterPCA}},
66
-#' \code{\link{seuratPCA}}, \code{\link{seuratICA}}, \code{\link{getTSNE}},
67
-#' \code{\link{seuratTSNE}}, \code{\link{getUMAP}} and
68
-#' \code{\link{seuratUMAP}}. Users can use an assay by specifying
66
+#' \code{\link{runSeuratPCA}}, \code{\link{runSeuratICA}}, \code{\link{getTSNE}},
67
+#' \code{\link{runSeuratTSNE}}, \code{\link{getUMAP}} and
68
+#' \code{\link{runSeuratUMAP}}. Users can use an assay by specifying
69 69
 #' \code{useAssay}, use the assay in an altExp by specifying both
70 70
 #' \code{useAltExp} and \code{useAssay}, or use a low-dimensionality
71 71
 #' representation by specifying \code{useReducedDim}.
Browse code

Renamed seurat methods

Irzam Sarfraz authored on 05/04/2022 09:46:47
Showing1 changed files
... ...
@@ -64,8 +64,8 @@
64 64
 #' @details Wrapper function to run one of the available dimensionality
65 65
 #' reduction algorithms integrated within SCTK from \code{\link{scaterPCA}},
66 66
 #' \code{\link{seuratPCA}}, \code{\link{seuratICA}}, \code{\link{getTSNE}},
67
-#' \code{\link{seuratRunTSNE}}, \code{\link{getUMAP}} and
68
-#' \code{\link{seuratRunUMAP}}. Users can use an assay by specifying
67
+#' \code{\link{seuratTSNE}}, \code{\link{getUMAP}} and
68
+#' \code{\link{seuratUMAP}}. Users can use an assay by specifying
69 69
 #' \code{useAssay}, use the assay in an altExp by specifying both
70 70
 #' \code{useAltExp} and \code{useAssay}, or use a low-dimensionality
71 71
 #' representation by specifying \code{useReducedDim}.
... ...
@@ -142,22 +142,22 @@ runDimReduce <- function(inSCE,
142 142
     # Seurat part
143 143
     if (!is.null(useAltExp)) {
144 144
       tempSCE <- SingleCellExperiment::altExp(inSCE, useAltExp)
145
-      # tempSCE <- seuratFindHVG(inSCE = tempSCE, useAssay = useAssay,
145
+      # tempSCE <- runSeuratFindHVG(inSCE = tempSCE, useAssay = useAssay,
146 146
       #                          altExp = TRUE)
147 147
     } else if (!is.null(useAssay)) {
148 148
       tempSCE <- inSCE
149
-      #tempSCE <- seuratFindHVG(inSCE = tempSCE, useAssay = useAssay)
149
+      #tempSCE <- runSeuratFindHVG(inSCE = tempSCE, useAssay = useAssay)
150 150
     }
151 151
     if (method %in% c("seuratPCA", "seuratICA")) {
152 152
       ## SeuratPCA/ICA
153 153
       if (method == "seuratPCA") {
154 154
         message(paste0(date(), " ... Computing Seurat PCA."))
155
-        tempSCE <- seuratPCA(tempSCE, useAssay = useAssay,
155
+        tempSCE <- runSeuratPCA(tempSCE, useAssay = useAssay,
156 156
                              reducedDimName = reducedDimName,
157 157
                              nPCs = nComponents, features = rownames(inSCE), seed = seed, ...)
158 158
       } else if (method == "seuratICA") {
159 159
         message(paste0(date(), " ... Computing Seurat ICA."))
160
-        tempSCE <- seuratICA(tempSCE, useAssay = useAssay,
160
+        tempSCE <- runSeuratICA(tempSCE, useAssay = useAssay,
161 161
                              reducedDimName = reducedDimName,
162 162
                              nics = nComponents, seed = seed, ...)
163 163
       }
... ...
@@ -176,22 +176,22 @@ runDimReduce <- function(inSCE,
176 176
         }
177 177
         if (args$useReduction == "pca") {
178 178
           message(paste0(date(), " ... Computing Seurat PCA."))
179
-          tempSCE <- seuratPCA(inSCE = tempSCE,
179
+          tempSCE <- runSeuratPCA(inSCE = tempSCE,
180 180
                                useAssay = useAssay,
181 181
                                reducedDimName = paste0(useAssay, "_seuratPCA"), seed = seed)
182 182
         } else if (args$useReduction == "ica") {
183 183
           message(paste0(date(), " ... Computing Seurat ICA."))
184
-          tempSCE <- seuratICA(inSCE = tempSCE,
184
+          tempSCE <- runSeuratICA(inSCE = tempSCE,
185 185
                                useAssay = useAssay,
186 186
                                reducedDimName = paste0(useAssay, "_seuratICA"), seed = seed)
187 187
         }
188 188
         if (method == "seuratUMAP") {
189 189
           message(paste0(date(), " ... Computing Seurat UMAP."))
190
-          tempSCE <- seuratRunUMAP(inSCE = tempSCE,
190
+          tempSCE <- runSeuratUMAP(inSCE = tempSCE,
191 191
                                    reducedDimName = reducedDimName, seed = seed, ...)
192 192
         } else {
193 193
           message(paste0(date(), " ... Computing Seurat tSNE."))
194
-          tempSCE <- seuratRunTSNE(inSCE = tempSCE,
194
+          tempSCE <- runSeuratTSNE(inSCE = tempSCE,
195 195
                                    reducedDimName = reducedDimName, seed = seed, ...)
196 196
         }
197 197
       } else {
... ...
@@ -213,12 +213,12 @@ runDimReduce <- function(inSCE,
213 213
         if (method == "seuratUMAP") {
214 214
           # hard-code useReduction="pca"
215 215
           message(paste0(date(), " ... Computing Seurat UMAP."))
216
-          tempSCE <- seuratRunUMAP(inSCE = tempSCE, useReduction = "pca",
216
+          tempSCE <- runSeuratUMAP(inSCE = tempSCE, useReduction = "pca",
217 217
                                    reducedDimName = reducedDimName, seed = seed, ...)
218 218
         } else {
219 219
           # hard-code useReduction="pca"
220 220
           message(paste0(date(), " ... Computing Seurat tSNE."))
221
-          tempSCE <- seuratRunTSNE(inSCE = tempSCE, useReduction = "pca",
221
+          tempSCE <- runSeuratTSNE(inSCE = tempSCE, useReduction = "pca",
222 222
                                    reducedDimName = reducedDimName, seed = seed, ...)
223 223
         }
224 224
       }
Browse code

Added seed to seuratPCA and seuratICA

Irzam Sarfraz authored on 19/12/2021 20:47:24
Showing1 changed files
... ...
@@ -84,9 +84,8 @@
84 84
 #' @param nComponents Specify the number of dimensions to compute with the
85 85
 #'  selected method in case of PCA/ICA and the number of components to
86 86
 #'  use in the case of TSNE/UMAP methods.
87
-#' @param seed Random seed for reproducibility of results. Only used with UMAP
88
-#' and tSNE methods. Default \code{NULL} will use global seed in use by the 
89
-#' R environment.
87
+#' @param seed Random seed for reproducibility of results.
88
+#' Default \code{NULL} will use global seed in use by the R environment.
90 89
 #' @param ... The other arguments for running a specific algorithm. Please refer
91 90
 #' to the one you use.
92 91
 #' @return The input \linkS4class{SingleCellExperiment} object with
... ...
@@ -155,12 +154,12 @@ runDimReduce <- function(inSCE,
155 154
         message(paste0(date(), " ... Computing Seurat PCA."))
156 155
         tempSCE <- seuratPCA(tempSCE, useAssay = useAssay,
157 156
                              reducedDimName = reducedDimName,
158
-                             nPCs = nComponents, features = rownames(inSCE), ...)
157
+                             nPCs = nComponents, features = rownames(inSCE), seed = seed, ...)
159 158
       } else if (method == "seuratICA") {
160 159
         message(paste0(date(), " ... Computing Seurat ICA."))
161 160
         tempSCE <- seuratICA(tempSCE, useAssay = useAssay,
162 161
                              reducedDimName = reducedDimName,
163
-                             nics = nComponents, ...)
162
+                             nics = nComponents, seed = seed, ...)
164 163
       }
165 164
       seuratObj <- tempSCE@metadata$seurat
166 165
       if (!is.null(useAltExp)) {
... ...
@@ -179,21 +178,21 @@ runDimReduce <- function(inSCE,
179 178
           message(paste0(date(), " ... Computing Seurat PCA."))
180 179
           tempSCE <- seuratPCA(inSCE = tempSCE,
181 180
                                useAssay = useAssay,
182
-                               reducedDimName = paste0(useAssay, "_seuratPCA"))
181
+                               reducedDimName = paste0(useAssay, "_seuratPCA"), seed = seed)
183 182
         } else if (args$useReduction == "ica") {
184 183
           message(paste0(date(), " ... Computing Seurat ICA."))
185 184
           tempSCE <- seuratICA(inSCE = tempSCE,
186 185
                                useAssay = useAssay,
187
-                               reducedDimName = paste0(useAssay, "_seuratICA"))
186
+                               reducedDimName = paste0(useAssay, "_seuratICA"), seed = seed)
188 187
         }
189 188
         if (method == "seuratUMAP") {
190 189
           message(paste0(date(), " ... Computing Seurat UMAP."))
191 190
           tempSCE <- seuratRunUMAP(inSCE = tempSCE,
192
-                                   reducedDimName = reducedDimName, ...)
191
+                                   reducedDimName = reducedDimName, seed = seed, ...)
193 192
         } else {
194 193
           message(paste0(date(), " ... Computing Seurat tSNE."))
195 194
           tempSCE <- seuratRunTSNE(inSCE = tempSCE,
196
-                                   reducedDimName = reducedDimName, ...)
195
+                                   reducedDimName = reducedDimName, seed = seed, ...)
197 196
         }
198 197
       } else {
199 198
         ### using external reducedDim
... ...
@@ -215,12 +214,12 @@ runDimReduce <- function(inSCE,
215 214
           # hard-code useReduction="pca"
216 215
           message(paste0(date(), " ... Computing Seurat UMAP."))
217 216
           tempSCE <- seuratRunUMAP(inSCE = tempSCE, useReduction = "pca",
218
-                                   reducedDimName = reducedDimName, ...)
217
+                                   reducedDimName = reducedDimName, seed = seed, ...)
219 218
         } else {
220 219
           # hard-code useReduction="pca"
221 220
           message(paste0(date(), " ... Computing Seurat tSNE."))
222 221
           tempSCE <- seuratRunTSNE(inSCE = tempSCE, useReduction = "pca",
223
-                                   reducedDimName = reducedDimName, ...)
222
+                                   reducedDimName = reducedDimName, seed = seed, ...)
224 223
         }
225 224
       }
226 225
     }
Browse code

Added seed to scaterPCA

Irzam Sarfraz authored on 17/12/2021 12:59:59
Showing1 changed files
... ...
@@ -128,7 +128,7 @@ runDimReduce <- function(inSCE,
128 128
   if (method == "scaterPCA") {
129 129
     message(paste0(date(), " ... Computing Scater PCA."))
130 130
     inSCE <- scaterPCA(inSCE = inSCE, useAssay = useAssay, useAltExp = useAltExp,
131
-                       reducedDimName = reducedDimName, nComponents = nComponents, ...)
131
+                       reducedDimName = reducedDimName, nComponents = nComponents, seed = seed, ...)
132 132
   } else if (method == "scaterUMAP") {
133 133
     message(paste0(date(), " ... Computing Scater UMAP."))
134 134
     inSCE <- getUMAP(inSCE = inSCE, useAssay = useAssay, useAltExp = useAltExp,
Browse code

Added console messages to some observeEvents

Irzam Sarfraz authored on 10/12/2021 12:28:34
Showing1 changed files
... ...
@@ -126,13 +126,16 @@ runDimReduce <- function(inSCE,
126 126
   }
127 127
 
128 128
   if (method == "scaterPCA") {
129
+    message(paste0(date(), " ... Computing Scater PCA."))
129 130
     inSCE <- scaterPCA(inSCE = inSCE, useAssay = useAssay, useAltExp = useAltExp,
130 131
                        reducedDimName = reducedDimName, nComponents = nComponents, ...)
131 132
   } else if (method == "scaterUMAP") {
133
+    message(paste0(date(), " ... Computing Scater UMAP."))
132 134
     inSCE <- getUMAP(inSCE = inSCE, useAssay = useAssay, useAltExp = useAltExp,
133 135
                      useReducedDim = useReducedDim,
134 136
                      reducedDimName = reducedDimName, seed = seed, ...)
135 137
   } else if (method == "rTSNE") {
138
+    message(paste0(date(), " ... Computing RtSNE."))
136 139
     inSCE <- getTSNE(inSCE = inSCE, useAssay = useAssay, useAltExp = useAltExp,
137 140
                      useReducedDim = useReducedDim,
138 141
                      reducedDimName = reducedDimName, seed = seed, ...)
... ...
@@ -149,10 +152,12 @@ runDimReduce <- function(inSCE,
149 152
     if (method %in% c("seuratPCA", "seuratICA")) {
150 153
       ## SeuratPCA/ICA
151 154
       if (method == "seuratPCA") {
155
+        message(paste0(date(), " ... Computing Seurat PCA."))
152 156
         tempSCE <- seuratPCA(tempSCE, useAssay = useAssay,
153 157
                              reducedDimName = reducedDimName,
154 158
                              nPCs = nComponents, features = rownames(inSCE), ...)
155 159
       } else if (method == "seuratICA") {
160
+        message(paste0(date(), " ... Computing Seurat ICA."))
156 161
         tempSCE <- seuratICA(tempSCE, useAssay = useAssay,
157 162
                              reducedDimName = reducedDimName,
158 163
                              nics = nComponents, ...)
... ...
@@ -171,18 +176,22 @@ runDimReduce <- function(inSCE,
171 176
           stop("Must specify `useReduction` when using `useAssay` in seuratUMAP/TSNE")
172 177
         }
173 178
         if (args$useReduction == "pca") {
179
+          message(paste0(date(), " ... Computing Seurat PCA."))
174 180
           tempSCE <- seuratPCA(inSCE = tempSCE,
175 181
                                useAssay = useAssay,
176 182
                                reducedDimName = paste0(useAssay, "_seuratPCA"))
177 183
         } else if (args$useReduction == "ica") {
184
+          message(paste0(date(), " ... Computing Seurat ICA."))
178 185
           tempSCE <- seuratICA(inSCE = tempSCE,
179 186
                                useAssay = useAssay,
180 187
                                reducedDimName = paste0(useAssay, "_seuratICA"))
181 188
         }
182 189
         if (method == "seuratUMAP") {
190
+          message(paste0(date(), " ... Computing Seurat UMAP."))
183 191
           tempSCE <- seuratRunUMAP(inSCE = tempSCE,
184 192
                                    reducedDimName = reducedDimName, ...)
185 193
         } else {
194
+          message(paste0(date(), " ... Computing Seurat tSNE."))
186 195
           tempSCE <- seuratRunTSNE(inSCE = tempSCE,
187 196
                                    reducedDimName = reducedDimName, ...)
188 197
         }
... ...
@@ -204,10 +213,12 @@ runDimReduce <- function(inSCE,
204 213
                                        key = paste0(key, "_"), assay = "RNA")
205 214
         if (method == "seuratUMAP") {
206 215
           # hard-code useReduction="pca"
216
+          message(paste0(date(), " ... Computing Seurat UMAP."))
207 217
           tempSCE <- seuratRunUMAP(inSCE = tempSCE, useReduction = "pca",
208 218
                                    reducedDimName = reducedDimName, ...)
209 219
         } else {
210 220
           # hard-code useReduction="pca"
221
+          message(paste0(date(), " ... Computing Seurat tSNE."))
211 222
           tempSCE <- seuratRunTSNE(inSCE = tempSCE, useReduction = "pca",
212 223
                                    reducedDimName = reducedDimName, ...)
213 224
         }
Browse code

Added seed to tsne/umap functions

Irzam Sarfraz authored on 30/11/2021 21:06:14
Showing1 changed files
... ...
@@ -84,6 +84,9 @@
84 84
 #' @param nComponents Specify the number of dimensions to compute with the
85 85
 #'  selected method in case of PCA/ICA and the number of components to
86 86
 #'  use in the case of TSNE/UMAP methods.
87
+#' @param seed Random seed for reproducibility of results. Only used with UMAP
88
+#' and tSNE methods. Default \code{NULL} will use global seed in use by the 
89
+#' R environment.
87 90
 #' @param ... The other arguments for running a specific algorithm. Please refer
88 91
 #' to the one you use.
89 92
 #' @return The input \linkS4class{SingleCellExperiment} object with
... ...
@@ -108,7 +111,7 @@ runDimReduce <- function(inSCE,
108 111
                                     "scaterUMAP",
109 112
                                     "seuratUMAP"),
110 113
                          useAssay = NULL, useReducedDim = NULL,
111
-                         useAltExp = NULL, reducedDimName, nComponents = 20, ...
114
+                         useAltExp = NULL, reducedDimName, nComponents = 20, seed = NULL, ...
112 115
 ) {
113 116
 
114 117
   method <- match.arg(method)
... ...
@@ -128,11 +131,11 @@ runDimReduce <- function(inSCE,
128 131
   } else if (method == "scaterUMAP") {
129 132
     inSCE <- getUMAP(inSCE = inSCE, useAssay = useAssay, useAltExp = useAltExp,
130 133
                      useReducedDim = useReducedDim,
131
-                     reducedDimName = reducedDimName, ...)
134
+                     reducedDimName = reducedDimName, seed = seed, ...)
132 135
   } else if (method == "rTSNE") {
133 136
     inSCE <- getTSNE(inSCE = inSCE, useAssay = useAssay, useAltExp = useAltExp,
134 137
                      useReducedDim = useReducedDim,
135
-                     reducedDimName = reducedDimName, ...)
138
+                     reducedDimName = reducedDimName, seed = seed, ...)
136 139
   } else {
137 140
     # Seurat part
138 141
     if (!is.null(useAltExp)) {
Browse code

Updated uwot::umap to scater::calculateUMAP in docs and vignettes

Irzam Sarfraz authored on 26/10/2021 15:53:29
Showing1 changed files
... ...
@@ -71,7 +71,7 @@
71 71
 #' representation by specifying \code{useReducedDim}.
72 72
 #' @param inSCE Input \linkS4class{SingleCellExperiment} object.
73 73
 #' @param method One from \code{"scaterPCA"}, \code{"seuratPCA"},
74
-#' \code{"seuratICA"}, \code{"rTSNE"}, \code{"seuratTSNE"}, \code{"uwotUMAP"}
74
+#' \code{"seuratICA"}, \code{"rTSNE"}, \code{"seuratTSNE"}, \code{"scaterUMAP"}
75 75
 #' and \code{"seuratUMAP"}.
76 76
 #' @param useAssay Assay to use for computation. If \code{useAltExp} is
77 77
 #' specified, \code{useAssay} has to exist in
... ...
@@ -105,7 +105,7 @@ runDimReduce <- function(inSCE,
105 105
                                     "seuratICA",
106 106
                                     "rTSNE",
107 107
                                     "seuratTSNE",
108
-                                    "uwotUMAP",
108
+                                    "scaterUMAP",
109 109
                                     "seuratUMAP"),
110 110
                          useAssay = NULL, useReducedDim = NULL,
111 111
                          useAltExp = NULL, reducedDimName, nComponents = 20, ...
... ...
@@ -125,7 +125,7 @@ runDimReduce <- function(inSCE,
125 125
   if (method == "scaterPCA") {
126 126
     inSCE <- scaterPCA(inSCE = inSCE, useAssay = useAssay, useAltExp = useAltExp,
127 127
                        reducedDimName = reducedDimName, nComponents = nComponents, ...)
128
-  } else if (method == "uwotUMAP") {
128
+  } else if (method == "scaterUMAP") {
129 129
     inSCE <- getUMAP(inSCE = inSCE, useAssay = useAssay, useAltExp = useAltExp,
130 130
                      useReducedDim = useReducedDim,
131 131
                      reducedDimName = reducedDimName, ...)
Browse code

Fixed some bugs in seurat functions breaking the a la carte workflow

Irzam Sarfraz authored on 22/09/2021 16:42:02
Showing1 changed files
... ...
@@ -137,18 +137,18 @@ runDimReduce <- function(inSCE,
137 137
     # Seurat part
138 138
     if (!is.null(useAltExp)) {
139 139
       tempSCE <- SingleCellExperiment::altExp(inSCE, useAltExp)
140
-      tempSCE <- seuratFindHVG(inSCE = tempSCE, useAssay = useAssay,
141
-                               altExp = TRUE)
140
+      # tempSCE <- seuratFindHVG(inSCE = tempSCE, useAssay = useAssay,
141
+      #                          altExp = TRUE)
142 142
     } else if (!is.null(useAssay)) {
143 143
       tempSCE <- inSCE
144
-      tempSCE <- seuratFindHVG(inSCE = tempSCE, useAssay = useAssay)
144
+      #tempSCE <- seuratFindHVG(inSCE = tempSCE, useAssay = useAssay)
145 145
     }
146 146
     if (method %in% c("seuratPCA", "seuratICA")) {
147 147
       ## SeuratPCA/ICA
148 148
       if (method == "seuratPCA") {
149 149
         tempSCE <- seuratPCA(tempSCE, useAssay = useAssay,
150 150
                              reducedDimName = reducedDimName,
151
-                             nPCs = nComponents, ...)
151
+                             nPCs = nComponents, features = rownames(inSCE), ...)
152 152
       } else if (method == "seuratICA") {
153 153
         tempSCE <- seuratICA(tempSCE, useAssay = useAssay,
154 154
                              reducedDimName = reducedDimName,
Browse code

RMD Check fixes

Irzam Sarfraz authored on 06/09/2021 12:14:58
Showing1 changed files
... ...
@@ -81,6 +81,9 @@
81 81
 #' @param useReducedDim The low dimension representation to use for embedding
82 82
 #' computation. Default \code{NULL}.
83 83
 #' @param reducedDimName The name of the result matrix. Required.
84
+#' @param nComponents Specify the number of dimensions to compute with the
85
+#'  selected method in case of PCA/ICA and the number of components to
86
+#'  use in the case of TSNE/UMAP methods.
84 87
 #' @param ... The other arguments for running a specific algorithm. Please refer
85 88
 #' to the one you use.
86 89
 #' @return The input \linkS4class{SingleCellExperiment} object with
Browse code

Minor bug fix

Yichen Wang authored on 05/09/2021 17:10:44
Showing1 changed files
... ...
@@ -110,7 +110,9 @@ runDimReduce <- function(inSCE,
110 110
 
111 111
   method <- match.arg(method)
112 112
   args <- list(...)
113
-
113
+  if (is.null(reducedDimName)) {
114
+    stop("Must specify `reducedDimName` to store the result.")
115
+  }
114 116
   if (method %in% c("scaterPCA", "seuratPCA", "seuratICA")) {
115 117
     .matrixTypeCheck(inSCE, "linear", useAssay, useReducedDim, useAltExp)
116 118
   } else {
... ...
@@ -173,19 +175,19 @@ runDimReduce <- function(inSCE,
173 175
         }
174 176
         if (method == "seuratUMAP") {
175 177
           tempSCE <- seuratRunUMAP(inSCE = tempSCE,
176
-                                   reducedDimName = reducedDimName,
177
-                                   dims = nComponents, ...)
178
+                                   reducedDimName = reducedDimName, ...)
178 179
         } else {
179 180
           tempSCE <- seuratRunTSNE(inSCE = tempSCE,
180
-                                   reducedDimName = reducedDimName,
181
-                                   dims = nComponents, ...)
181
+                                   reducedDimName = reducedDimName, ...)
182 182
         }
183 183
       } else {
184 184
         ### using external reducedDim
185
-        if ("useReduction" %in% names(args)) {
185
+        if (!is.null(args$useReduction)) {
186 186
           stop("Cannot specify `useReduction` when using `useReducedDim` in seuratUMAP/TSNE")
187 187
         }
188 188
         tempSCE <- inSCE
189
+        seuratObj <- convertSCEToSeurat(inSCE)
190
+        tempSCE@metadata$seurat$obj <- seuratObj
189 191
         reDim <- SingleCellExperiment::reducedDim(inSCE, useReducedDim)
190 192
         colnames(reDim) <- paste0(useReducedDim, "_", seq_len(length(colnames(reDim))))
191 193
         rownames(reDim) <- gsub('_', '-', rownames(reDim))
... ...
@@ -197,13 +199,11 @@ runDimReduce <- function(inSCE,
197 199
         if (method == "seuratUMAP") {
198 200
           # hard-code useReduction="pca"
199 201
           tempSCE <- seuratRunUMAP(inSCE = tempSCE, useReduction = "pca",
200
-                                   reducedDimName = reducedDimName,
201
-                                   dims = nComponents, ...)
202
+                                   reducedDimName = reducedDimName, ...)
202 203
         } else {
203 204
           # hard-code useReduction="pca"
204 205
           tempSCE <- seuratRunTSNE(inSCE = tempSCE, useReduction = "pca",
205
-                                   reducedDimName = reducedDimName,
206
-                                   dims = nComponents, ...)
206
+                                   reducedDimName = reducedDimName, ...)
207 207
         }
208 208
       }
209 209
     }
Browse code

Update UI server for new runDimReduce func; some default param values changed

Yichen Wang authored on 02/09/2021 06:09:23
Showing1 changed files
... ...
@@ -105,29 +105,21 @@ runDimReduce <- function(inSCE,
105 105
                                     "uwotUMAP",
106 106
                                     "seuratUMAP"),
107 107
                          useAssay = NULL, useReducedDim = NULL,
108
-                         useAltExp = NULL, reducedDimName, ...
108
+                         useAltExp = NULL, reducedDimName, nComponents = 20, ...
109 109
 ) {
110 110
 
111 111
   method <- match.arg(method)
112 112
   args <- list(...)
113 113
 
114
-  params <- list(
115
-    inSCE = inSCE,
116
-    useAssay = useAssay,
117
-    useAltExp = useAltExp,
118
-    reducedDimName = reducedDimName
119
-  )
120
-
121 114
   if (method %in% c("scaterPCA", "seuratPCA", "seuratICA")) {
122 115
     .matrixTypeCheck(inSCE, "linear", useAssay, useReducedDim, useAltExp)
123 116
   } else {
124 117
     .matrixTypeCheck(inSCE, "embedding", useAssay, useReducedDim, useAltExp)
125
-    params$useReducedDim <- useReducedDim
126 118
   }
127 119
 
128 120
   if (method == "scaterPCA") {
129 121
     inSCE <- scaterPCA(inSCE = inSCE, useAssay = useAssay, useAltExp = useAltExp,
130
-                       reducedDimName = reducedDimName, ...)
122
+                       reducedDimName = reducedDimName, nComponents = nComponents, ...)
131 123
   } else if (method == "uwotUMAP") {
132 124
     inSCE <- getUMAP(inSCE = inSCE, useAssay = useAssay, useAltExp = useAltExp,
133 125
                      useReducedDim = useReducedDim,
... ...
@@ -150,10 +142,18 @@ runDimReduce <- function(inSCE,
150 142
       ## SeuratPCA/ICA
151 143
       if (method == "seuratPCA") {
152 144
         tempSCE <- seuratPCA(tempSCE, useAssay = useAssay,
153
-                             reducedDimName = reducedDimName, ...)
145
+                             reducedDimName = reducedDimName,
146
+                             nPCs = nComponents, ...)
154 147
       } else if (method == "seuratICA") {
155 148
         tempSCE <- seuratICA(tempSCE, useAssay = useAssay,
156
-                             reducedDimName = reducedDimName, ...)
149
+                             reducedDimName = reducedDimName,
150
+                             nics = nComponents, ...)
151
+      }
152
+      seuratObj <- tempSCE@metadata$seurat
153
+      if (!is.null(useAltExp)) {
154
+        altExp(inSCE, useAltExp)@metadata$seurat <- seuratObj
155
+      } else if (!is.null(useAssay)) {
156
+        inSCE@metadata$seurat <- seuratObj
157 157
       }
158 158
     } else {
159 159
       ## SeuratUMAP/TSNE
... ...
@@ -173,10 +173,12 @@ runDimReduce <- function(inSCE,
173 173
         }
174 174
         if (method == "seuratUMAP") {
175 175
           tempSCE <- seuratRunUMAP(inSCE = tempSCE,
176
-                                   reducedDimName = reducedDimName, ...)
176
+                                   reducedDimName = reducedDimName,
177
+                                   dims = nComponents, ...)
177 178
         } else {
178 179
           tempSCE <- seuratRunTSNE(inSCE = tempSCE,
179
-                                   reducedDimName = reducedDimName, ...)
180
+                                   reducedDimName = reducedDimName,
181
+                                   dims = nComponents, ...)
180 182
         }
181 183
       } else {
182 184
         ### using external reducedDim
... ...
@@ -195,11 +197,13 @@ runDimReduce <- function(inSCE,
195 197
         if (method == "seuratUMAP") {
196 198
           # hard-code useReduction="pca"
197 199
           tempSCE <- seuratRunUMAP(inSCE = tempSCE, useReduction = "pca",
198
-                                   reducedDimName = reducedDimName, ...)
200
+                                   reducedDimName = reducedDimName,
201
+                                   dims = nComponents, ...)
199 202
         } else {
200 203
           # hard-code useReduction="pca"
201 204
           tempSCE <- seuratRunTSNE(inSCE = tempSCE, useReduction = "pca",
202
-                                   reducedDimName = reducedDimName, ...)
205
+                                   reducedDimName = reducedDimName,
206
+                                   dims = nComponents, ...)
203 207
         }
204 208
       }
205 209
     }
Browse code

fix R CMD check error

Yichen Wang authored on 30/08/2021 07:17:38
Showing1 changed files
... ...
@@ -62,10 +62,10 @@
62 62
 
63 63
 #' Generic Wrapper function for running dimensionality reduction
64 64
 #' @details Wrapper function to run one of the available dimensionality
65
-#' reduction algorithms integrated within SCTK from \code{\link{scaterPCA()}},
66
-#' \code{\link{seuratPCA()}}, \code{\link{seuratICA()}}, \code{\link{rTSNE()}},
67
-#' \code{\link{seuratTSNE()}}, \code{\link{uwotUMAP()}} and
68
-#' \code{\link{seuratUMAP()}}. Users can use an assay by specifying
65
+#' reduction algorithms integrated within SCTK from \code{\link{scaterPCA}},
66
+#' \code{\link{seuratPCA}}, \code{\link{seuratICA}}, \code{\link{getTSNE}},
67
+#' \code{\link{seuratRunTSNE}}, \code{\link{getUMAP}} and
68
+#' \code{\link{seuratRunUMAP}}. Users can use an assay by specifying
69 69
 #' \code{useAssay}, use the assay in an altExp by specifying both
70 70
 #' \code{useAltExp} and \code{useAssay}, or use a low-dimensionality
71 71
 #' representation by specifying \code{useReducedDim}.
Browse code

HUGE UPDATE for Generic DimReduce Wrapper

Yichen Wang authored on 30/08/2021 06:17:02
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,210 @@
1
+.matrixTypeCheck <- function(inSCE, redDimType = NULL,
2
+                             useAssay = NULL, useReducedDim = NULL,
3
+                             useAltExp = NULL) {
4
+  # Helper function for checking if the specified matrix type is valid
5
+  if (!inherits(inSCE, "SingleCellExperiment")){
6
+    stop("Please use a SingleCellExperiment object")
7
+  }
8
+  if (redDimType == "embedding") {
9
+    if (is.null(useAssay) && is.null(useReducedDim)) {
10
+      stop("`useAssay` and `useReducedDim` cannot be NULL at the same time.")
11
+    } else if (!is.null(useAssay) && !is.null(useReducedDim)) {
12
+      stop("`useAssay` and `useReducedDim` cannot be specified at the same time.")
13
+    } else {
14
+      if (!is.null(useReducedDim)) {
15
+        if (!useReducedDim %in% SingleCellExperiment::reducedDimNames(inSCE)) {
16
+          stop("Specified `useReducedDim` not found.")
17
+        }
18
+        if (!is.null(useAltExp)) {
19
+          warning("`useAltExp` will be ignored when using `useReducedDim`.")
20
+        }
21
+        sce <- inSCE
22
+      } else {
23
+        if (!is.null(useAltExp)) {
24
+          if (!useAltExp %in% SingleCellExperiment::altExpNames(inSCE)) {
25
+            stop("Specified `useAltExp` not found.")
26
+          }
27
+          sce <- SingleCellExperiment::altExp(inSCE, useAltExp)
28
+          if (!useAssay %in% SummarizedExperiment::assayNames(sce)) {
29
+            stop("Specified `useAssay` not found in `useAltExp`.")
30
+          }
31
+        } else {
32
+          if (!useAssay %in% SummarizedExperiment::assayNames(inSCE)) {
33
+            stop("Specified `useAssay` not found.")
34
+          }
35
+          sce <- inSCE
36
+        }
37
+      }
38
+    }
39
+  } else if (redDimType == "linear") {
40
+    if (!is.null(useReducedDim)) {
41
+      stop("Currently `useReducedDim` is not allowed for linear dimension reduction.")
42
+    }
43
+    if (is.null(useAssay)) {
44
+      stop("`useAssay` cannot be NULL for linear dimension reduction")
45
+    } else {
46
+      if (is.null(useAltExp)) {
47
+        if (!useAssay %in% SummarizedExperiment::assayNames(inSCE)) {
48
+          stop("Specified `useAssay` not found.")
49
+        }
50
+      } else {
51
+        if (!useAltExp %in% SingleCellExperiment::altExpNames(inSCE)) {
52
+          stop("Specified `useAltExp` not found.")
53
+        }
54
+        sce <- SingleCellExperiment::altExp(inSCE, useAltExp)
55
+        if (!useAssay %in% SummarizedExperiment::assayNames(sce)) {
56
+          stop("Specified `useAssay` not found in `useAltExp`.")
57
+        }
58
+      }
59
+    }
60
+  }
61
+}
62
+
63
+#' Generic Wrapper function for running dimensionality reduction
64
+#' @details Wrapper function to run one of the available dimensionality
65
+#' reduction algorithms integrated within SCTK from \code{\link{scaterPCA()}},
66
+#' \code{\link{seuratPCA()}}, \code{\link{seuratICA()}}, \code{\link{rTSNE()}},
67
+#' \code{\link{seuratTSNE()}}, \code{\link{uwotUMAP()}} and
68
+#' \code{\link{seuratUMAP()}}. Users can use an assay by specifying
69
+#' \code{useAssay}, use the assay in an altExp by specifying both
70
+#' \code{useAltExp} and \code{useAssay}, or use a low-dimensionality
71
+#' representation by specifying \code{useReducedDim}.
72
+#' @param inSCE Input \linkS4class{SingleCellExperiment} object.
73
+#' @param method One from \code{"scaterPCA"}, \code{"seuratPCA"},
74
+#' \code{"seuratICA"}, \code{"rTSNE"}, \code{"seuratTSNE"}, \code{"uwotUMAP"}
75
+#' and \code{"seuratUMAP"}.
76
+#' @param useAssay Assay to use for computation. If \code{useAltExp} is
77
+#' specified, \code{useAssay} has to exist in
78
+#' \code{assays(altExp(inSCE, useAltExp))}. Default \code{"counts"}.
79
+#' @param useAltExp The subset to use for computation, usually for the
80
+#' selected variable features. Default \code{NULL}.
81
+#' @param useReducedDim The low dimension representation to use for embedding
82
+#' computation. Default \code{NULL}.
83
+#' @param reducedDimName The name of the result matrix. Required.
84
+#' @param ... The other arguments for running a specific algorithm. Please refer
85
+#' to the one you use.
86
+#' @return The input \linkS4class{SingleCellExperiment} object with
87
+#' \code{reducedDim} updated with the result.
88
+#' @export
89
+#' @examples
90
+#' data(scExample, package = "singleCellTK")
91
+#' sce <- subsetSCECols(sce, colData = "type != 'EmptyDroplet'")
92
+#' sce <- runNormalization(sce, useAssay = "counts",
93
+#'                         outAssayName = "logcounts_scaled",
94
+#'                         normalizationMethod = "logNormCounts",
95
+#'                         scale = TRUE)
96
+#' sce <- runDimReduce(inSCE = sce, method = "scaterPCA",
97
+#'                     useAssay = "logcounts_scaled",
98
+#'                     reducedDimName = "PCA")
99
+runDimReduce <- function(inSCE,
100
+                         method = c("scaterPCA",
101
+                                    "seuratPCA",
102
+                                    "seuratICA",
103
+                                    "rTSNE",
104
+                                    "seuratTSNE",
105
+                                    "uwotUMAP",
106
+                                    "seuratUMAP"),
107
+                         useAssay = NULL, useReducedDim = NULL,
108
+                         useAltExp = NULL, reducedDimName, ...
109
+) {
110
+
111
+  method <- match.arg(method)
112
+  args <- list(...)
113
+
114
+  params <- list(
115
+    inSCE = inSCE,
116
+    useAssay = useAssay,
117
+    useAltExp = useAltExp,
118
+    reducedDimName = reducedDimName
119
+  )
120
+
121
+  if (method %in% c("scaterPCA", "seuratPCA", "seuratICA")) {
122
+    .matrixTypeCheck(inSCE, "linear", useAssay, useReducedDim, useAltExp)
123
+  } else {
124
+    .matrixTypeCheck(inSCE, "embedding", useAssay, useReducedDim, useAltExp)
125
+    params$useReducedDim <- useReducedDim
126
+  }
127
+
128
+  if (method == "scaterPCA") {
129
+    inSCE <- scaterPCA(inSCE = inSCE, useAssay = useAssay, useAltExp = useAltExp,
130
+                       reducedDimName = reducedDimName, ...)
131
+  } else if (method == "uwotUMAP") {
132
+    inSCE <- getUMAP(inSCE = inSCE, useAssay = useAssay, useAltExp = useAltExp,
133
+                     useReducedDim = useReducedDim,
134
+                     reducedDimName = reducedDimName, ...)
135
+  } else if (method == "rTSNE") {
136
+    inSCE <- getTSNE(inSCE = inSCE, useAssay = useAssay, useAltExp = useAltExp,
137
+                     useReducedDim = useReducedDim,
138
+                     reducedDimName = reducedDimName, ...)
139
+  } else {
140
+    # Seurat part
141
+    if (!is.null(useAltExp)) {
142
+      tempSCE <- SingleCellExperiment::altExp(inSCE, useAltExp)
143
+      tempSCE <- seuratFindHVG(inSCE = tempSCE, useAssay = useAssay,
144
+                               altExp = TRUE)
145
+    } else if (!is.null(useAssay)) {
146
+      tempSCE <- inSCE
147
+      tempSCE <- seuratFindHVG(inSCE = tempSCE, useAssay = useAssay)
148
+    }
149
+    if (method %in% c("seuratPCA", "seuratICA")) {
150
+      ## SeuratPCA/ICA
151
+      if (method == "seuratPCA") {
152
+        tempSCE <- seuratPCA(tempSCE, useAssay = useAssay,
153
+                             reducedDimName = reducedDimName, ...)
154
+      } else if (method == "seuratICA") {
155
+        tempSCE <- seuratICA(tempSCE, useAssay = useAssay,
156
+                             reducedDimName = reducedDimName, ...)
157
+      }
158
+    } else {
159
+      ## SeuratUMAP/TSNE
160
+      if (is.null(useReducedDim)) {
161
+        ### using assay
162
+        if (!"useReduction" %in% names(args)) {
163
+          stop("Must specify `useReduction` when using `useAssay` in seuratUMAP/TSNE")
164
+        }
165
+        if (args$useReduction == "pca") {
166
+          tempSCE <- seuratPCA(inSCE = tempSCE,
167
+                               useAssay = useAssay,
168
+                               reducedDimName = paste0(useAssay, "_seuratPCA"))
169
+        } else if (args$useReduction == "ica") {
170
+          tempSCE <- seuratICA(inSCE = tempSCE,
171
+                               useAssay = useAssay,
172
+                               reducedDimName = paste0(useAssay, "_seuratICA"))
173
+        }
174
+        if (method == "seuratUMAP") {
175
+          tempSCE <- seuratRunUMAP(inSCE = tempSCE,
176
+                                   reducedDimName = reducedDimName, ...)
177
+        } else {
178
+          tempSCE <- seuratRunTSNE(inSCE = tempSCE,
179
+                                   reducedDimName = reducedDimName, ...)
180
+        }
181
+      } else {
182
+        ### using external reducedDim
183
+        if ("useReduction" %in% names(args)) {
184
+          stop("Cannot specify `useReduction` when using `useReducedDim` in seuratUMAP/TSNE")
185
+        }
186
+        tempSCE <- inSCE
187
+        reDim <- SingleCellExperiment::reducedDim(inSCE, useReducedDim)
188
+        colnames(reDim) <- paste0(useReducedDim, "_", seq_len(length(colnames(reDim))))
189
+        rownames(reDim) <- gsub('_', '-', rownames(reDim))
190
+        key <-  gsub('_', '', useReducedDim)
191
+        # hard-code "pca"
192
+        tempSCE@metadata$seurat$obj@reductions$pca <-
193
+          Seurat::CreateDimReducObject(embeddings = reDim,
194
+                                       key = paste0(key, "_"), assay = "RNA")
195
+        if (method == "seuratUMAP") {
196
+          # hard-code useReduction="pca"
197
+          tempSCE <- seuratRunUMAP(inSCE = tempSCE, useReduction = "pca",
198
+                                   reducedDimName = reducedDimName, ...)
199
+        } else {
200
+          # hard-code useReduction="pca"
201
+          tempSCE <- seuratRunTSNE(inSCE = tempSCE, useReduction = "pca",
202
+                                   reducedDimName = reducedDimName, ...)
203
+        }
204
+      }
205
+    }
206
+    SingleCellExperiment::reducedDim(inSCE, reducedDimName) <-
207
+      SingleCellExperiment::reducedDim(tempSCE, reducedDimName)
208
+  }
209
+  return(inSCE)
210
+}