Browse code

sce celdaGridSearch

zhewa authored on 12/05/2020 06:07:15
Showing 43 changed files

... ...
@@ -1,5 +1,104 @@
1 1
 # Generated by roxygen2: do not edit by hand
2 2
 
3
+export("clusters<-")
4
+export("decontXcounts<-")
5
+export("modules<-")
6
+export("sampleLabel<-")
7
+export(appendCeldaList)
8
+export(availableModels)
9
+export(bestLogLikelihood)
10
+export(celda)
11
+export(celdaGridSearch)
12
+export(celdaHeatmap)
13
+export(celdaModel)
14
+export(celdaPerplexity)
15
+export(celdaProbabilityMap)
16
+export(celdaTsne)
17
+export(celdaUmap)
18
+export(celda_C)
19
+export(celda_CG)
20
+export(celda_G)
21
+export(clusterProbability)
22
+export(clusters)
23
+export(compareCountMatrix)
24
+export(countChecksum)
25
+export(decontX)
26
+export(decontXcounts)
27
+export(differentialExpression)
28
+export(distinctColors)
29
+export(factorizeMatrix)
30
+export(featureModuleLookup)
31
+export(featureModuleTable)
32
+export(findMarkersTree)
33
+export(geneSetEnrich)
34
+export(getDecisions)
35
+export(logLikelihood)
36
+export(logLikelihoodHistory)
37
+export(matrixNames)
38
+export(moduleHeatmap)
39
+export(modules)
40
+export(normalizeCounts)
41
+export(params)
42
+export(perplexity)
43
+export(plotCeldaViolin)
44
+export(plotDecontXContamination)
45
+export(plotDecontXMarkerExpression)
46
+export(plotDecontXMarkerPercentage)
47
+export(plotDimReduceCluster)
48
+export(plotDimReduceFeature)
49
+export(plotDimReduceGrid)
50
+export(plotDimReduceModule)
51
+export(plotGridSearchPerplexity)
52
+export(plotGridSearchPerplexitycelda_C)
53
+export(plotGridSearchPerplexitycelda_CG)
54
+export(plotGridSearchPerplexitycelda_G)
55
+export(plotHeatmap)
56
+export(plotMarkerDendro)
57
+export(plotMarkerHeatmap)
58
+export(recodeClusterY)
59
+export(recodeClusterZ)
60
+export(recursiveSplitCell)
61
+export(recursiveSplitModule)
62
+export(resList)
63
+export(resamplePerplexity)
64
+export(retrieveFeatureIndex)
65
+export(runParams)
66
+export(sampleLabel)
67
+export(selectBestModel)
68
+export(simulateCells)
69
+export(simulateContamination)
70
+export(topRank)
71
+exportMethods("clusters<-")
72
+exportMethods("decontXcounts<-")
73
+exportMethods("modules<-")
74
+exportMethods("sampleLabel<-")
75
+exportMethods(bestLogLikelihood)
76
+exportMethods(celdaGridSearch)
77
+exportMethods(celdaHeatmap)
78
+exportMethods(celdaModel)
79
+exportMethods(celdaPerplexity)
80
+exportMethods(celdaProbabilityMap)
81
+exportMethods(celdaTsne)
82
+exportMethods(celdaUmap)
83
+exportMethods(celda_C)
84
+exportMethods(celda_CG)
85
+exportMethods(celda_G)
86
+exportMethods(clusterProbability)
87
+exportMethods(clusters)
88
+exportMethods(countChecksum)
89
+exportMethods(decontX)
90
+exportMethods(decontXcounts)
91
+exportMethods(factorizeMatrix)
92
+exportMethods(featureModuleLookup)
93
+exportMethods(logLikelihood)
94
+exportMethods(logLikelihoodHistory)
95
+exportMethods(matrixNames)
96
+exportMethods(modules)
97
+exportMethods(params)
98
+exportMethods(perplexity)
99
+exportMethods(resList)
100
+exportMethods(runParams)
101
+exportMethods(sampleLabel)
3 102
 import(Rcpp)
4 103
 import(RcppEigen)
5 104
 import(SummarizedExperiment)
6 105
new file mode 100644
... ...
@@ -0,0 +1,246 @@
1
+setClass(
2
+    "celdaModel",
3
+    slots = c(
4
+        params = "list",
5
+        # K, L, model priors, checksum
6
+        names = "list",
7
+        completeLogLik = "numeric",
8
+        finalLogLik = "numeric",
9
+        clusters = "list"
10
+    )
11
+) # z and or y
12
+
13
+
14
+#' @title Get or set the cell cluster labels from a celda
15
+#'  \link[SingleCellExperiment]{SingleCellExperiment} object or celda model
16
+#'  object.
17
+#' @description Return or set the cell cluster labels determined
18
+#'  by \link{celda_C} or \link{celda_CG} models.
19
+#' @param x Can be one of
20
+#'  \itemize{
21
+#'  \item A \linkS4class{SingleCellExperiment} object returned by
22
+#'  \link{celda_C}, or \link{celda_CG}, with the matrix
23
+#'  located in the \code{useAssay} assay slot.
24
+#'  Rows represent features and columns represent cells.
25
+#'  \item Celda model object.}
26
+#' @return One of
27
+#' \itemize{
28
+#'  \item Character vector if \code{x} is a
29
+#'  \linkS4class{SingleCellExperiment} object.
30
+#'  Contains cell cluster labels for each cell in x.
31
+#'  \item List if \code{x} is a celda model object. Contains cell cluster
32
+#'  labels (for celda_C and celdaCG
33
+#'  Models) and/or feature module labels (for celda_G and celdaCG Models).}
34
+#' @examples
35
+#' data(sceCeldaCG)
36
+#' clusters(sceCeldaCG)
37
+#' @export
38
+setGeneric("clusters",
39
+    function(x) {
40
+        standardGeneric("clusters")
41
+    })
42
+
43
+
44
+#' @rdname clusters
45
+#' @export
46
+setMethod("clusters",
47
+    signature(x = "SingleCellExperiment"),
48
+    function(x) {
49
+        return(SummarizedExperiment::colData(x)$celda_cell_cluster)
50
+    })
51
+
52
+
53
+#' @examples
54
+#' data(celdaCGMod)
55
+#' clusters(celdaCGMod)
56
+#' @rdname clusters
57
+#' @export
58
+setMethod("clusters",
59
+    signature(x = "celdaModel"),
60
+    function(x) {
61
+        return(x@clusters)
62
+    }
63
+)
64
+
65
+
66
+#' @rdname clusters
67
+#' @export
68
+setGeneric("clusters<-",
69
+    function(x, value) standardGeneric("clusters<-")
70
+)
71
+
72
+
73
+#' @rdname clusters
74
+#' @export
75
+setReplaceMethod("clusters", signature(x = "SingleCellExperiment"),
76
+    function(x, value) {
77
+        SummarizedExperiment::colData(x)$celda_cell_cluster <- value
78
+        return(x)
79
+    })
80
+
81
+
82
+#' @title Get or set the feature module labels from a celda
83
+#'  \link[SingleCellExperiment]{SingleCellExperiment} object.
84
+#' @description Return or set the feature module cluster labels determined
85
+#'  by \link{celda_G} or \link{celda_CG} models.
86
+#' @param sce A \linkS4class{SingleCellExperiment} object returned by
87
+#'  \link{celda_G}, or \link{celda_CG}, with the matrix
88
+#'  located in the \code{useAssay} assay slot.
89
+#'  Rows represent features and columns represent cells.
90
+#' @return Character vector. Contains feature module labels for each
91
+#'  feature in x.
92
+#' @examples
93
+#' data(celdaCGMod)
94
+#' modules(celdaCGMod)
95
+#' @export
96
+setGeneric("modules",
97
+    function(sce) {
98
+        standardGeneric("modules")
99
+    })
100
+#' @rdname modules
101
+#' @export
102
+setMethod("modules",
103
+    signature(sce = "SingleCellExperiment"),
104
+    function(sce) {
105
+        return(SummarizedExperiment::rowData(sce)$celda_feature_module)
106
+    })
107
+
108
+
109
+#' @rdname modules
110
+#' @export
111
+setGeneric("modules<-",
112
+    function(sce, value) standardGeneric("modules<-")
113
+)
114
+#' @rdname modules
115
+#' @export
116
+setReplaceMethod("modules", signature(sce = "SingleCellExperiment"),
117
+    function(sce, value) {
118
+        SummarizedExperiment::rowData(sce)$celda_feature_module <- value
119
+        return(sce)
120
+    })
121
+
122
+
123
+#' @title Get or set sample labels from a celda
124
+#'  \link[SingleCellExperiment]{SingleCellExperiment} object
125
+#' @description Return or set the sample labels for the cells in \code{sce}.
126
+#' @param x Can be one of
127
+#'  \itemize{
128
+#'  \item A \linkS4class{SingleCellExperiment} object returned by
129
+#'  \link{celda_C}, \link{celda_G}, or \link{celda_CG}, with the matrix
130
+#'  located in the \code{useAssay} assay slot.
131
+#'  Rows represent features and columns represent cells.
132
+#'  \item A celda model object.}
133
+#' @return Character. Contains the sample labels provided at model creation,
134
+#'  or those automatically generated by celda.
135
+#' @examples
136
+#' data(sceCeldaCG)
137
+#' sampleLabel(sceCeldaCG)
138
+#' @export
139
+setGeneric("sampleLabel",
140
+    function(x) {
141
+        standardGeneric("sampleLabel")
142
+    })
143
+#' @rdname sampleLabel
144
+#' @export
145
+setMethod("sampleLabel",
146
+    signature(x = "SingleCellExperiment"),
147
+    function(x) {
148
+        return(SummarizedExperiment::colData(x)$celda_sample_label)
149
+    })
150
+
151
+
152
+#' @rdname sampleLabel
153
+#' @export
154
+setGeneric("sampleLabel<-",
155
+    function(x, value) standardGeneric("sampleLabel<-")
156
+)
157
+#' @rdname sampleLabel
158
+#' @export
159
+setReplaceMethod("sampleLabel", signature(x = "SingleCellExperiment"),
160
+    function(x, value) {
161
+        SummarizedExperiment::colData(x)$celda_sample_label <- value
162
+        return(x)
163
+    })
164
+
165
+
166
+#' @examples
167
+#' data(celdaCGMod)
168
+#' sampleLabel(celdaCGMod)
169
+#' @rdname sampleLabel
170
+#' @export
171
+setMethod("sampleLabel",
172
+    signature(x = "celdaModel"),
173
+    function(x) {
174
+        x@sampleLabel
175
+    }
176
+)
177
+
178
+
179
+#' @title Get parameter values provided for celdaModel creation
180
+#' @description Retrieves the K/L, model priors (e.g. alpha, beta),
181
+#'  and count matrix checksum parameters provided during the creation of the
182
+#'  provided celdaModel.
183
+#' @param celdaMod celdaModel. Options available in `celda::availableModels`.
184
+#' @return List. Contains the model-specific parameters for the provided celda
185
+#'  model object depending on its class.
186
+#' @examples
187
+#' data(celdaCGMod)
188
+#' params(celdaCGMod)
189
+#' @export
190
+setGeneric(
191
+    "params",
192
+    function(celdaMod) {
193
+        standardGeneric("params")
194
+    }
195
+)
196
+#' @title Get parameter values provided for celdaModel creation
197
+#' @description Retrieves the K/L, model priors (e.g. alpha, beta),
198
+#'  and count matrix checksum parameters provided during the creation of the
199
+#'  provided celdaModel.
200
+#' @param celdaMod celdaModel. Options available in `celda::availableModels`.
201
+#' @return List. Contains the model-specific parameters for the provided celda
202
+#'  model object depending on its class.
203
+#' @examples
204
+#' data(celdaCGMod)
205
+#' params(celdaCGMod)
206
+#' @export
207
+setMethod("params",
208
+    signature = c(celdaMod = "celdaModel"),
209
+    function(celdaMod) {
210
+        celdaMod@params
211
+    }
212
+)
213
+
214
+
215
+#' @title Get feature, cell and sample names from a celdaModel
216
+#' @description Retrieves the row, column, and sample names used to generate
217
+#'  a celdaModel.
218
+#' @param celdaMod celdaModel. Options available in `celda::availableModels`.
219
+#' @return List. Contains row, column, and sample character vectors
220
+#'  corresponding to the values provided when the celdaModel was generated.
221
+#' @examples
222
+#' data(celdaCGMod)
223
+#' matrixNames(celdaCGMod)
224
+#' @export
225
+setGeneric(
226
+    "matrixNames",
227
+    function(celdaMod) {
228
+        standardGeneric("matrixNames")
229
+    }
230
+)
231
+#' @title Get feature, cell and sample names from a celdaModel
232
+#' @description Retrieves the row, column, and sample names used to generate a
233
+#'  celdaModel.
234
+#' @param celdaMod celdaModel. Options available in `celda::availableModels`.
235
+#' @return List. Contains row, column, and sample character vectors
236
+#'  corresponding to the values provided when the celdaModel was generated.
237
+#' @examples
238
+#' data(celdaCGMod)
239
+#' matrixNames(celdaCGMod)
240
+#' @export
241
+setMethod("matrixNames",
242
+    signature = c(celdaMod = "celdaModel"),
243
+    function(celdaMod) {
244
+        celdaMod@names
245
+    }
246
+)
... ...
@@ -1,85 +1,3 @@
1
-setClass(
2
-  "celdaModel",
3
-  representation(
4
-    params = "list",
5
-    # K, L, model priors, checksum
6
-    names = "list",
7
-    completeLogLik = "numeric",
8
-    finalLogLik = "numeric",
9
-    clusters = "list"
10
-  )
11
-) # z and or y
12
-
13
-
14
-#' @title Get parameter values provided for celdaModel creation
15
-#' @description Retrieves the K/L, model priors (e.g. alpha, beta),
16
-#'  and count matrix checksum parameters provided during the creation of the
17
-#'  provided celdaModel.
18
-#' @param celdaMod celdaModel. Options available in `celda::availableModels`.
19
-#' @return List. Contains the model-specific parameters for the provided celda
20
-#'  model object depending on its class.
21
-#' @examples
22
-#' data(celdaCGMod)
23
-#' params(celdaCGMod)
24
-#' @export
25
-setGeneric(
26
-  "params",
27
-  function(celdaMod) {
28
-    standardGeneric("params")
29
-  }
30
-)
31
-#' @title Get parameter values provided for celdaModel creation
32
-#' @description Retrieves the K/L, model priors (e.g. alpha, beta),
33
-#'  and count matrix checksum parameters provided during the creation of the
34
-#'  provided celdaModel.
35
-#' @param celdaMod celdaModel. Options available in `celda::availableModels`.
36
-#' @return List. Contains the model-specific parameters for the provided celda
37
-#'  model object depending on its class.
38
-#' @examples
39
-#' data(celdaCGMod)
40
-#' params(celdaCGMod)
41
-#' @export
42
-setMethod("params",
43
-  signature = c(celdaMod = "celdaModel"),
44
-  function(celdaMod) {
45
-    celdaMod@params
46
-  }
47
-)
48
-
49
-
50
-#' @title Get feature, cell and sample names from a celdaModel
51
-#' @description Retrieves the row, column, and sample names used to generate
52
-#'  a celdaModel.
53
-#' @param celdaMod celdaModel. Options available in `celda::availableModels`.
54
-#' @return List. Contains row, column, and sample character vectors
55
-#'  corresponding to the values provided when the celdaModel was generated.
56
-#' @examples
57
-#' data(celdaCGMod)
58
-#' matrixNames(celdaCGMod)
59
-#' @export
60
-setGeneric(
61
-  "matrixNames",
62
-  function(celdaMod) {
63
-    standardGeneric("matrixNames")
64
-  }
65
-)
66
-#' @title Get feature, cell and sample names from a celdaModel
67
-#' @description Retrieves the row, column, and sample names used to generate a
68
-#'  celdaModel.
69
-#' @param celdaMod celdaModel. Options available in `celda::availableModels`.
70
-#' @return List. Contains row, column, and sample character vectors
71
-#'  corresponding to the values provided when the celdaModel was generated.
72
-#' @examples
73
-#' data(celdaCGMod)
74
-#' matrixNames(celdaCGMod)
75
-#' @export
76
-setMethod("matrixNames",
77
-  signature = c(celdaMod = "celdaModel"),
78
-  function(celdaMod) {
79
-    celdaMod@names
80
-  }
81
-)
82
-
83 1
 
84 2
 #' @title Get log-likelihood history
85 3
 #' @description Retrieves the complete log-likelihood from all iterations of
... ...
@@ -149,127 +67,12 @@ setMethod("bestLogLikelihood",
149 67
 )
150 68
 
151 69
 
152
-#' @title Get or set the cell cluster labels from a celda
153
-#'  \link[SingleCellExperiment]{SingleCellExperiment} object
154
-#' @description Return or set the cell cluster labels determined
155
-#'  by \link{celda_C} or \link{celda_CG} models.
156
-#' @param sce A \link[SingleCellExperiment]{SingleCellExperiment} object
157
-#'  returned by \link{celda_C}, or \link{celda_CG}.
158
-#' @return List. Contains cell cluster z (for celda_C and celda_CG Models)
159
-#'  and/or gene module y (for celda_G and celda_CG Models).
160
-#' @examples
161
-#' data(sceCelda_CG)
162
-#' clusters(sceCelda_CG)
163
-#' @export
164
-setGeneric("clusters",
165
-    function(sce) {
166
-        standardGeneric("clusters")
167
-    })
168
-#' @rdname clusters
169
-#' @export
170
-setMethod("clusters",
171
-    signature(sce = "SingleCellExperiment"),
172
-    function(sce) {
173
-        return(SummarizedExperiment::colData(sce)$celda_cell_cluster)
174
-    })
175
-
176
-
177
-#' @rdname clusters
178
-#' @export
179
-setGeneric("clusters<-",
180
-    function(sce, value) standardGeneric("clusters<-")
181
-)
182
-#' @rdname clusters
183
-#' @export
184
-setReplaceMethod("clusters", signature(sce = "SingleCellExperiment"),
185
-    function(sce, value) {
186
-        SummarizedExperiment::colData(sce)$celda_cell_cluster <- value
187
-        return(sce)
188
-    })
189
-
190
-
191
-#' @title Get or set the feature module labels from a celda
192
-#'  \link[SingleCellExperiment]{SingleCellExperiment} object
193
-#' @description Return or set the feature module cluster labels determined
194
-#'  by \link{celda_G} or \link{celda_CG} models.
195
-#' @param sce A \link[SingleCellExperiment]{SingleCellExperiment} object
196
-#'  returned by \link{celda_G} or \link{celda_CG}.
197
-#' @return List. Contains feature module y (for celda_G and celda_CG Models).
198
-#' @examples
199
-#' data(sceCelda_CG)
200
-#' modules(sceCelda_CG)
201
-#' @export
202
-setGeneric("modules",
203
-    function(sce) {
204
-        standardGeneric("modules")
205
-    })
206
-#' @rdname modules
207
-#' @export
208
-setMethod("modules",
209
-    signature(sce = "SingleCellExperiment"),
210
-    function(sce) {
211
-        return(SummarizedExperiment::rowData(sce)$celda_feature_module)
212
-    })
213
-
214
-
215
-#' @rdname modules
216
-#' @export
217
-setGeneric("modules<-",
218
-    function(sce, value) standardGeneric("modules<-")
219
-)
220
-#' @rdname modules
221
-#' @export
222
-setReplaceMethod("modules", signature(sce = "SingleCellExperiment"),
223
-    function(sce, value) {
224
-        SummarizedExperiment::rowData(sce)$celda_feature_module <- value
225
-        return(sce)
226
-    })
227
-
228
-
229 70
 setClass("celda_C",
230 71
   representation(sampleLabel = "factor"),
231 72
   contains = "celdaModel"
232 73
 )
233 74
 
234 75
 
235
-#' @title Get or set sample labels from a celda
236
-#'  \link[SingleCellExperiment]{SingleCellExperiment} object
237
-#' @description Return or set the sample labels for the cells in \code{sce}.
238
-#' @param sce A \link[SingleCellExperiment]{SingleCellExperiment} object
239
-#'  returned by \link{celda_C}, \link{celda_G}, or \link{celda_CG}.
240
-#' @return Character. Contains the sample labels provided at model creation,
241
-#'  or those automatically generated by celda.
242
-#' @examples
243
-#' data(sceCelda_CG)
244
-#' sampleLabel(sceCelda_CG)
245
-#' @export
246
-setGeneric("sampleLabel",
247
-    function(sce) {
248
-        standardGeneric("sampleLabel")
249
-    })
250
-#' @rdname sampleLabel
251
-#' @export
252
-setMethod("sampleLabel",
253
-    signature(sce = "SingleCellExperiment"),
254
-    function(sce) {
255
-        return(SummarizedExperiment::colData(sce)$celda_sample_label)
256
-    })
257
-
258
-
259
-#' @rdname sampleLabel
260
-#' @export
261
-setGeneric("sampleLabel<-",
262
-    function(sce, value) standardGeneric("sampleLabel<-")
263
-)
264
-#' @rdname sampleLabel
265
-#' @export
266
-setReplaceMethod("sampleLabel", signature(sce = "SingleCellExperiment"),
267
-    function(sce, value) {
268
-        SummarizedExperiment::colData(sce)$celda_sample_label <- value
269
-        return(sce)
270
-    })
271
-
272
-
273 76
 #' @title Get celda model from a celda
274 77
 #'  \link[SingleCellExperiment]{SingleCellExperiment} object
275 78
 #' @description Return the celda model for \code{sce} returned by
... ...
@@ -279,8 +82,8 @@ setReplaceMethod("sampleLabel", signature(sce = "SingleCellExperiment"),
279 82
 #' @return Character. The celda model. Can be one of "celda_C", "celda_G", or
280 83
 #'  "celda_CG".
281 84
 #' @examples
282
-#' data(sceCelda_CG)
283
-#' celdaModel(sceCelda_CG)
85
+#' data(sceCeldaCG)
86
+#' celdaModel(sceCeldaCG)
284 87
 #' @export
285 88
 setGeneric("celdaModel",
286 89
     function(sce) {
... ...
@@ -516,8 +319,8 @@ setMethod("countChecksum",
516 319
 #' @param ... Additional parameters passed to \link{plotHeatmap}.
517 320
 #' @seealso `celdaTsne()` for generating 2-dimensional tSNE coordinates
518 321
 #' @examples
519
-#' data(sceCelda_CG)
520
-#' celdaHeatmap(sceCelda_CG)
322
+#' data(sceCeldaCG)
323
+#' celdaHeatmap(sceCeldaCG)
521 324
 #' @return list A list containing dendrogram information and the heatmap grob
522 325
 #' @export
523 326
 setGeneric("celdaHeatmap",
... ...
@@ -569,9 +372,9 @@ setMethod("celdaHeatmap", signature(sce = "SingleCellExperiment"),
569 372
 #'  provided \linkS4class{SingleCellExperiment}.
570 373
 #' @seealso `celda_C()` for clustering cells
571 374
 #' @examples
572
-#' data(sceCelda_C, sceCelda_CG)
573
-#' loglikC <- logLikelihood(sceCelda_C)
574
-#' loglikCG <- logLikelihood(sceCelda_CG)
375
+#' data(sceCeldaC, sceCeldaCG)
376
+#' loglikC <- logLikelihood(sceCeldaC)
377
+#' loglikCG <- logLikelihood(sceCeldaCG)
575 378
 #' @export
576 379
 setGeneric("logLikelihood",
577 380
     function(sce, ...) {
... ...
@@ -615,8 +418,8 @@ setMethod("logLikelihood", signature(sce = "SingleCellExperiment"),
615 418
 #'  probabilities will be returned. If \code{TRUE}, then the unnormalized log
616 419
 #'  probabilities will be returned. Default \code{FALSE}.
617 420
 #' @examples
618
-#' data(sceCelda_CG)
619
-#' clusterProb <- clusterProbability(sceCelda_CG, log = TRUE)
421
+#' data(sceCeldaCG)
422
+#' clusterProb <- clusterProbability(sceCeldaCG, log = TRUE)
620 423
 #' @return A list containging a matrix for the conditional cell subpopulation
621 424
 #'  cluster and/or feature module probabilities.
622 425
 #' @export
... ...
@@ -628,8 +431,8 @@ setGeneric("clusterProbability",
628 431
 
629 432
 #' @seealso `celda_C()` for clustering cells
630 433
 #' @examples
631
-#' data(sceCelda_C)
632
-#' clusterProb <- clusterProbability(sceCelda_C)
434
+#' data(sceCeldaC)
435
+#' clusterProb <- clusterProbability(sceCeldaC)
633 436
 #' @rdname clusterProbability
634 437
 #' @export
635 438
 setMethod("clusterProbability", signature(sce = "SingleCellExperiment"),
... ...
@@ -656,49 +459,6 @@ setMethod("clusterProbability", signature(sce = "SingleCellExperiment"),
656 459
     })
657 460
 
658 461
 
659
-#' @title Calculate the perplexity of a celda model
660
-#' @description Perplexity is a statistical measure of how well a probability
661
-#'  model can predict new data. Lower perplexity indicates a better model.
662
-#' @param sce A \linkS4class{SingleCellExperiment} object returned by
663
-#'  \link{celda_C}, \link{celda_G}, or \link{celda_CG}, with the matrix
664
-#'  located in the \code{useAssay} assay slot.
665
-#'  Rows represent features and columns represent cells.
666
-#' @param useAssay A string specifying which \link[SummarizedExperiment]{assay}
667
-#'  slot to use. Default "counts".
668
-#' @return Numeric. The perplexity for the provided
669
-#'  \linkS4class{SingleCellExperiment}.
670
-#' @examples
671
-#' data(sceCelda_CG)
672
-#' clusterProb <- clusterProbability(sceCelda_CG)
673
-#' @export
674
-setGeneric("perplexity",
675
-    function(sce, ...) {
676
-        standardGeneric("perplexity")
677
-    })
678
-
679
-
680
-#' @importFrom matrixStats logSumExp
681
-#' @rdname perplexity
682
-#' @export
683
-setMethod("perplexity", signature(sce = "SingleCellExperiment"),
684
-    function(sce, useAssay = "counts") {
685
-
686
-        if (celdaModel(sce) == "celda_C") {
687
-            p <- .perplexityCelda_C(sce = sce, useAssay = useAssay)
688
-            return(p)
689
-        } else if (celdaModel(sce) == "celda_CG") {
690
-            p <- .perplexityCelda_CG(sce = sce, useAssay = useAssay)
691
-            return(p)
692
-        } else if (celdaModel(sce) == "celda_G") {
693
-            p <- .perplexityCelda_G(sce = sce, useAssay = useAssay)
694
-            return(p)
695
-        } else {
696
-            stop("S4Vectors::metadata(sce)$celda_parameters$model must be",
697
-                " one of 'celda_C', 'celda_G', or 'celda_CG'")
698
-        }
699
-    })
700
-
701
-
702 462
 #' @title Simulate count data from the celda generative models.
703 463
 #' @description This function generates a \linkS4class{SingleCellExperiment}
704 464
 #'  containing a simulated counts matrix in the \code{"counts"} assay slot, as
... ...
@@ -812,63 +572,6 @@ simulateCells <- function(
812 572
 }
813 573
 
814 574
 
815
-#' @title Generate factorized matrices showing each feature's influence on cell
816
-#'  / gene clustering
817
-#' @description Generates factorized matrices showing the contribution of each
818
-#'  feature in each cell population or each cell population in each sample.
819
-#' @param sce A \linkS4class{SingleCellExperiment} object returned by
820
-#'  \link{celda_C}, \link{celda_G}, or \link{celda_CG}, with the matrix
821
-#'  located in the \code{useAssay} assay slot.
822
-#'  Rows represent features and columns represent cells.
823
-#' @param useAssay A string specifying which \link[SummarizedExperiment]{assay}
824
-#'  slot to use. Default "counts".
825
-#' @param type Character vector. A vector containing one or more of "counts",
826
-#'  "proportion", or "posterior". "counts" returns the raw number of counts for
827
-#'  each factorized matrix. "proportions" returns the normalized probabilities
828
-#'  for each factorized matrix, which are calculated by dividing the raw counts
829
-#'  in each factorized matrix by the total counts in each column. "posterior"
830
-#'  returns the posterior estimates which include the addition of the Dirichlet
831
-#'  concentration parameter (essentially as a pseudocount). Default
832
-#'  \code{"counts"}.
833
-#' @examples
834
-#' data(sceCelda_CG)
835
-#' factorizedMatrices <- factorizeMatrix(sceCelda_CG, type = "posterior")
836
-#' @return A list with elements for `counts`, `proportions`, or `posterior`
837
-#'  probabilities. Each element will be a list containing factorized matrices
838
-#'  for `module` and `sample`.
839
-#' @export
840
-setGeneric("factorizeMatrix",
841
-    function(sce, ...) {standardGeneric("factorizeMatrix")})
842
-
843
-
844
-#' @examples
845
-#' data(sceCelda_C)
846
-#' factorizedMatrices <- factorizeMatrix(sceCelda_C, type = "posterior")
847
-#' @seealso `celda_C()` for clustering cells
848
-#' @rdname factorizeMatrix
849
-#' @export
850
-setMethod("factorizeMatrix", signature(sce = "SingleCellExperiment"),
851
-    function(sce,
852
-        useAssay = "counts",
853
-        type = c("counts", "proportion", "posterior")) {
854
-
855
-        if (celdaModel(sce) == "celda_C") {
856
-            res <- .factorizeMatrixCelda_C(sce = sce, useAssay = useAssay,
857
-                type = type)
858
-        } else if (celdaModel(sce) == "celda_CG") {
859
-            res <- .factorizeMatrixCelda_CG(sce = sce, useAssay = useAssay,
860
-                type = type)
861
-        } else if (celdaModel(sce) == "celda_G") {
862
-            res <- .factorizeMatrixCelda_G(sce = sce, useAssay = useAssay,
863
-                type = type)
864
-        } else {
865
-            stop("S4Vectors::metadata(sce)$celda_parameters$model must be",
866
-                " one of 'celda_C', 'celda_G', or 'celda_CG'")
867
-        }
868
-        return(res)
869
-    })
870
-
871
-
872 575
 #' @title Renders probability and relative expression heatmaps to visualize the
873 576
 #'  relationship between feature modules and cell populations.
874 577
 #' @description It is often useful to visualize to what degree each feature
... ...
@@ -989,8 +692,8 @@ setGeneric("featureModuleLookup",
989 692
 
990 693
 
991 694
 #' @examples
992
-#' data(sceCelda_CG)
993
-#' module <- featureModuleLookup(sce = sceCelda_CG,
695
+#' data(sceCeldaCG)
696
+#' module <- featureModuleLookup(sce = sceCeldaCG,
994 697
 #'     feature = c("Gene_1", "Gene_XXX"))
995 698
 #' @export
996 699
 #' @rdname featureModuleLookup
... ...
@@ -1119,8 +822,8 @@ setMethod("featureModuleLookup", signature(sce = "SingleCellExperiment"),
1119 822
 #' @param cores Number of threads to use. Default 1.
1120 823
 #' @param ... Additional parameters to pass to \link[uwot]{umap}.
1121 824
 #' @examples
1122
-#' data(sceCelda_CG)
1123
-#' umapRes <- celdaUmap(sceCelda_CG)
825
+#' data(sceCeldaCG)
826
+#' umapRes <- celdaUmap(sceCeldaCG)
1124 827
 #' @return \code{sce} with UMAP coordinates
1125 828
 #'  (columns "celda_UMAP1" & "celda_UMAP2") added to
1126 829
 #'  \code{\link[SummarizedExperiment]{colData}(sce)}.
... ...
@@ -1363,8 +1066,8 @@ setMethod("celdaUmap", signature(sce = "SingleCellExperiment"),
1363 1066
 #' @seealso \link{celda_C} for clustering cells. \link{celda_CG} for
1364 1067
 #'  clustering features and cells
1365 1068
 #' @examples
1366
-#' data(sceCelda_CG)
1367
-#' celdaProbabilityMap(sceCelda_CG)
1069
+#' data(sceCeldaCG)
1070
+#' celdaProbabilityMap(sceCeldaCG)
1368 1071
 #' @return A grob containing the specified plots
1369 1072
 #' @export
1370 1073
 setGeneric("celdaProbabilityMap",
... ...
@@ -53,7 +53,8 @@
53 53
 #' @import foreach
54 54
 #' @importFrom doParallel registerDoParallel
55 55
 #' @importFrom methods is
56
-#' @examples \dontrun{
56
+#' @examples
57
+#' \dontrun{
57 58
 #' data(celdaCGSim)
58 59
 #' ## Run various combinations of parameters with 'celdaGridSearch'
59 60
 #' celdaCGGridSearchRes <- celdaGridSearch(celdaCGSim$counts,
... ...
@@ -62,7 +63,8 @@
62 63
 #'   paramsFixed = list(sampleLabel = celdaCGSim$sampleLabel),
63 64
 #'   bestOnly = TRUE,
64 65
 #'   nchains = 1,
65
-#'   cores = 1)}
66
+#'   cores = 1)
67
+#' }
66 68
 #' @export
67 69
 setGeneric("celdaGridSearch", function(x, ...) {
68 70
     standardGeneric("celdaGridSearch")})
... ...
@@ -90,7 +92,7 @@ setMethod("celdaGridSearch",
90 92
         counts <- SummarizedExperiment::assay(x, i = useAssay)
91 93
 
92 94
         celdaList <- .celdaGridSearch(counts = counts,
93
-            model = model,
95
+            model = paste0(".", model),
94 96
             paramsTest = paramsTest,
95 97
             paramsFixed = paramsFixed,
96 98
             maxIter = maxIter,
... ...
@@ -133,6 +135,7 @@ setMethod("celdaGridSearch",
133 135
         nchains = 3,
134 136
         cores = 1,
135 137
         bestOnly = TRUE,
138
+        seed = 12345,
136 139
         perplexity = TRUE,
137 140
         verbose = TRUE,
138 141
         logfilePrefix = "Celda") {
... ...
@@ -142,7 +145,7 @@ setMethod("celdaGridSearch",
142 145
         sce <- SingleCellExperiment::SingleCellExperiment(
143 146
             assays = list(counts = x))
144 147
         celdaList <- .celdaGridSearch(counts = x,
145
-            model = model,
148
+            model = paste0(".", model),
146 149
             paramsTest = paramsTest,
147 150
             paramsFixed = paramsFixed,
148 151
             maxIter = maxIter,
... ...
@@ -176,15 +179,15 @@ setMethod("celdaGridSearch",
176 179
 .celdaGridSearch <- function(counts,
177 180
                             model,
178 181
                             paramsTest,
179
-                            paramsFixed = NULL,
180
-                            maxIter = 200,
181
-                            nchains = 3,
182
-                            cores = 1,
183
-                            bestOnly = TRUE,
184
-                            seed = 12345,
185
-                            perplexity = TRUE,
186
-                            verbose = TRUE,
187
-                            logfilePrefix = "Celda") {
182
+                            paramsFixed,
183
+                            maxIter,
184
+                            nchains,
185
+                            cores,
186
+                            bestOnly,
187
+                            seed,
188
+                            perplexity,
189
+                            verbose,
190
+                            logfilePrefix) {
188 191
 
189 192
   ## Check parameters
190 193
   .validateCounts(counts)
... ...
@@ -194,7 +197,7 @@ setMethod("celdaGridSearch",
194 197
     badParams <- setdiff(names(paramsTest), names(modelParams))
195 198
     stop(
196 199
       "The following elements in 'paramsTest' are not arguments of '",
197
-      model,
200
+      substring(model, 2),
198 201
       "': ",
199 202
       paste(badParams, collapse = ",")
200 203
     )
... ...
@@ -206,7 +209,7 @@ setMethod("celdaGridSearch",
206 209
     stop(
207 210
       "The following elements in 'paramsFixed' are not arguments",
208 211
       " of '",
209
-      model,
212
+      substring(model, 2),
210 213
       "': ",
211 214
       paste(badParams, collapse = ",")
212 215
     )
... ...
@@ -228,7 +231,7 @@ setMethod("celdaGridSearch",
228 231
     stop(
229 232
       "The following arguments are not in 'paramsTest' or 'paramsFixed'",
230 233
       " but are required for '",
231
-      model,
234
+      substring(model, 2),
232 235
       "': ",
233 236
       paste(missing.params, collapse = ",")
234 237
     )
... ...
@@ -278,7 +281,7 @@ setMethod("celdaGridSearch",
278 281
   )
279 282
 
280 283
   .logMessages("Starting celdaGridSearch with",
281
-    model,
284
+    substring(model, 2),
282 285
     logfile = NULL,
283 286
     append = TRUE,
284 287
     verbose = verbose
... ...
@@ -323,9 +326,6 @@ setMethod("celdaGridSearch",
323 326
       chainParams[[j]] <- current.run[[j]]
324 327
     }
325 328
     chainParams$counts <- counts
326
-    # silently ignored if allSeeds is NULL!
327
-    chainParams$seed <- allSeeds[ifelse(i %% nchains == 0,
328
-      nchains, i %% nchains)]
329 329
     chainParams$maxIter <- maxIter
330 330
     chainParams$nchain <- 1
331 331
     chainParams$countChecksum <- countChecksum
... ...
@@ -344,9 +344,12 @@ setMethod("celdaGridSearch",
344 344
 
345 345
     ## Run model
346 346
     if (is.null(seed)) {
347
-      res <- do.call(model, c(chainParams, paramsFixed, list(seed = NULL)))
348
-    } else {
349 347
       res <- do.call(model, c(chainParams, paramsFixed))
348
+    } else {
349
+      chainSeed <- allSeeds[ifelse(i %% nchains == 0,
350
+          nchains, i %% nchains)]
351
+      res <- with_seed(chainSeed,
352
+          do.call(model, c(chainParams, paramsFixed)))
350 353
     }
351 354
     return(list(res))
352 355
   }
... ...
@@ -400,7 +403,7 @@ setMethod("celdaGridSearch",
400 403
   return(celdaRes)
401 404
 }
402 405
 
403
-#'
406
+
404 407
 #' ################################################################################
405 408
 #' # Methods for manipulating celdaList objects                                  #
406 409
 #' ################################################################################
... ...
@@ -626,6 +629,7 @@ celda <- function() {
626 629
     paramsTest,
627 630
     paramsFixed,
628 631
     maxIter,
632
+    seed,
629 633
     nchains,
630 634
     cores,
631 635
     bestOnly,
... ...
@@ -1019,10 +1019,21 @@ setMethod("celda_C",
1019 1019
 }
1020 1020
 
1021 1021
 
1022
-.perplexityCelda_C <- function(sce, useAssay) {
1022
+.perplexityCelda_C <- function(sce, useAssay, newCounts) {
1023
+
1023 1024
     counts <- SummarizedExperiment::assay(sce, i = useAssay)
1024 1025
     counts <- .processCounts(counts)
1025 1026
 
1027
+    if (is.null(newCounts)) {
1028
+        newCounts <- counts
1029
+    } else {
1030
+        newCounts <- .processCounts(newCounts)
1031
+    }
1032
+
1033
+    if (nrow(newCounts) != nrow(counts)) {
1034
+        stop("'newCounts' should have the same number of rows as 'sce'.")
1035
+    }
1036
+
1026 1037
     factorized <- factorizeMatrix(sce = sce,
1027 1038
         useAssay = useAssay,
1028 1039
         type = "posterior")
... ...
@@ -1030,10 +1041,11 @@ setMethod("celda_C",
1030 1041
     phi <- log(factorized$posterior$module)
1031 1042
     s <- as.integer(sampleLabel(sce))
1032 1043
 
1033
-    inner.log.prob <- eigenMatMultInt(phi, Counts) + theta[, s]
1044
+    # inner.log.prob = (t(phi) %*% newCounts) + theta[, s]
1045
+    inner.log.prob <- eigenMatMultInt(phi, newCounts) + theta[, s]
1034 1046
     logPx <- sum(apply(inner.log.prob, 2, matrixStats::logSumExp))
1035 1047
 
1036
-    perplexity <- exp(- (logPx / sum(Counts)))
1048
+    perplexity <- exp(- (logPx / sum(newCounts)))
1037 1049
     return(perplexity)
1038 1050
 }
1039 1051
 
... ...
@@ -1282,29 +1282,38 @@ setMethod("celda_CG",
1282 1282
 }
1283 1283
 
1284 1284
 
1285
-.perplexityCelda_CG <- function(sce, useAssay) {
1285
+.perplexityCelda_CG <- function(sce, useAssay, newCounts) {
1286 1286
     counts <- SummarizedExperiment::assay(sce, i = useAssay)
1287 1287
     counts <- .processCounts(counts)
1288 1288
 
1289
+    if (is.null(newCounts)) {
1290
+        newCounts <- counts
1291
+    } else {
1292
+        newCounts <- .processCounts(newCounts)
1293
+    }
1294
+    if (nrow(newCounts) != nrow(counts)) {
1295
+        stop("newCounts should have the same number of rows as counts.")
1296
+    }
1297
+
1289 1298
     factorized <- factorizeMatrix(sce = sce,
1290 1299
         useAssay = useAssay,
1291
-        type = c("posterior"))
1300
+        type = c("posterior", "counts"))
1292 1301
 
1293 1302
     theta <- log(factorized$posterior$sample)
1294 1303
     phi <- factorized$posterior$cellPopulation
1295 1304
     psi <- factorized$posterior$module
1296
-    s <- as.integer(sampleLabel(celdaMod))
1305
+    s <- as.integer(sampleLabel(sce))
1297 1306
     eta <- factorized$posterior$geneDistribution
1298 1307
     nGByTS <- factorized$counts$geneDistribution
1299 1308
 
1300 1309
     etaProb <- log(eta) * nGByTS
1301 1310
     geneByPopProb <- log(psi %*% phi)
1302
-    innerLogProb <- eigenMatMultInt(geneByPopProb, counts) + theta[, s]
1303
-    # innerLogProb = (t(geneByPopProb) %*% counts) + theta[, s]
1311
+    innerLogProb <- eigenMatMultInt(geneByPopProb, newCounts) + theta[, s]
1312
+    # innerLogProb = (t(geneByPopProb) %*% newCounts) + theta[, s]
1304 1313
 
1305 1314
     log.px <- sum(apply(innerLogProb, 2, matrixStats::logSumExp))
1306 1315
     # + sum(etaProb)
1307
-    perplexity <- exp(- (log.px / sum(counts)))
1316
+    perplexity <- exp(- (log.px / sum(newCounts)))
1308 1317
     return(perplexity)
1309 1318
 }
1310 1319
 
... ...
@@ -994,15 +994,23 @@ setMethod("celda_G",
994 994
 }
995 995
 
996 996
 
997
-.perplexityCelda_G <- function(sce, useAssay) {
997
+.perplexityCelda_G <- function(sce, useAssay, newCounts) {
998 998
     counts <- SummarizedExperiment::assay(sce, i = useAssay)
999 999
     counts <- .processCounts(counts)
1000
-    # compareCountMatrix(counts, celdaMod)
1000
+
1001
+    if (is.null(newCounts)) {
1002
+        newCounts <- counts
1003
+    } else {
1004
+        newCounts <- .processCounts(newCounts)
1005
+    }
1006
+    if (nrow(newCounts) != nrow(counts)) {
1007
+        stop("newCounts should have the same number of rows as counts.")
1008
+    }
1001 1009
 
1002 1010
     factorized <- factorizeMatrix(
1003 1011
         sce = sce,
1004 1012
         useAssay = useAssay,
1005
-        type = "posterior")
1013
+        type = c("posterior", "counts"))
1006 1014
     psi <- factorized$posterior$module
1007 1015
     phi <- factorized$posterior$cell
1008 1016
     eta <- factorized$posterior$geneDistribution
... ...
@@ -1010,15 +1018,15 @@ setMethod("celda_G",
1010 1018
 
1011 1019
     etaProb <- log(eta) * nGByTS
1012 1020
     # gene.by.cell.prob = log(psi %*% phi)
1013
-    # logPx = sum(gene.by.cell.prob * counts) # + sum(etaProb)
1021
+    # logPx = sum(gene.by.cell.prob * newCounts) # + sum(etaProb)
1014 1022
     logPx <- .perplexityGLogPx(
1015
-        counts,
1023
+        newCounts,
1016 1024
         phi,
1017 1025
         psi,
1018 1026
         modules(sce),
1019 1027
         S4Vectors::metadata(sce)$celda_parameters$L
1020 1028
     ) # + sum(etaProb)
1021
-    perplexity <- exp(- (logPx / sum(counts)))
1029
+    perplexity <- exp(- (logPx / sum(newCounts)))
1022 1030
     return(perplexity)
1023 1031
 }
1024 1032
 
... ...
@@ -1073,11 +1081,13 @@ setMethod("celda_G",
1073 1081
 }
1074 1082
 
1075 1083
 
1076
-.prepareCountsForDimReductionCeldaG <- function(counts,
1077
-    celdaMod,
1078
-    maxCells = NULL,
1079
-    minClusterSize = 100,
1080
-    modules = NULL) {
1084
+.prepareCountsForDimReductionCeldaG <- function(sce,
1085
+    useAssay,
1086
+    maxCells,
1087
+    minClusterSize,
1088
+    modules) {
1089
+
1090
+    counts <- SummarizedExperiment::assay(sce, i = useAssay)
1081 1091
 
1082 1092
     if (is.null(maxCells) || maxCells > ncol(counts)) {
1083 1093
         maxCells <- ncol(counts)
... ...
@@ -1086,11 +1096,7 @@ setMethod("celda_G",
1086 1096
         cellIx <- sample(seq(ncol(counts)), maxCells)
1087 1097
     }
1088 1098
 
1089
-    fm <- factorizeMatrix(
1090
-        counts = counts,
1091
-        celdaMod = celdaMod,
1092
-        type = "counts"
1093
-    )
1099
+    fm <- factorizeMatrix(sce = sce, useAssay = useAssay, type = "counts")
1094 1100
 
1095 1101
     modulesToUse <- seq(nrow(fm$counts$cell))
1096 1102
     if (!is.null(modules)) {
... ...
@@ -134,8 +134,8 @@ normalizeCounts <- function(counts,
134 134
 #' @return \linkS4class{SingleCellExperiment} object with recoded cell cluster
135 135
 #'  labels.
136 136
 #' @examples
137
-#' data(sceCelda_CG)
138
-#' sceReorderedZ <- recodeClusterZ(sceCelda_CG, c(1, 3), c(3, 1))
137
+#' data(sceCeldaCG)
138
+#' sceReorderedZ <- recodeClusterZ(sceCeldaCG, c(1, 3), c(3, 1))
139 139
 #' @importFrom plyr mapvalues
140 140
 #' @export
141 141
 recodeClusterZ <- function(sce, from, to) {
... ...
@@ -178,8 +178,8 @@ recodeClusterZ <- function(sce, from, to) {
178 178
 #' @return @return \linkS4class{SingleCellExperiment} object with recoded
179 179
 #'  feature module labels.
180 180
 #' @examples
181
-#' data(sceCelda_CG)
182
-#' sceReorderedY <- recodeClusterY(sceCelda_CG, c(1, 3), c(3, 1))
181
+#' data(sceCeldaCG)
182
+#' sceReorderedY <- recodeClusterY(sceCeldaCG, c(1, 3), c(3, 1))
183 183
 #' @export
184 184
 recodeClusterY <- function(sce, from, to) {
185 185
     if (length(setdiff(from, to)) != 0) {
... ...
@@ -441,8 +441,8 @@ distinctColors <- function(n,
441 441
 #'  not be created. Default NULL.
442 442
 #' @return Matrix. Contains a list of features per each column (feature module)
443 443
 #' @examples
444
-#' data(sceCelda_CG)
445
-#' featureModuleTable(csceCelda_CG)
444
+#' data(sceCeldaCG)
445
+#' featureModuleTable(csceCeldaCG)
446 446
 #' @importFrom stringi stri_list2matrix
447 447
 #' @export
448 448
 featureModuleTable <- function(sce, useAssay = "counts", outputFile = NULL) {
... ...
@@ -28,8 +28,8 @@
28 28
 #'  a default value of 12345 is used. If NULL, no calls to
29 29
 #'  \link[withr]{with_seed} are made.
30 30
 #' @examples
31
-#' data(sceCelda_CG)
32
-#' tsneRes <- celdaTsne(sceCelda_CG)
31
+#' data(sceCeldaCG)
32
+#' tsneRes <- celdaTsne(sceCeldaCG)
33 33
 #' @return \code{sce} with t-SNE coordinates
34 34
 #'  (columns "celda_tSNE1" & "celda_tSNE2") added to
35 35
 #'  \code{\link[SummarizedExperiment]{colData}(sce)}.
... ...
@@ -121,3 +121,32 @@ availableModels <- c("celda_C", "celda_G", "celda_CG")
121 121
 #' data(celdaGSim)
122 122
 #' sceCeldaG = celda_G(celdaGSim$counts, L = celdaGSim$L)
123 123
 "sceCeldaG"
124
+
125
+
126
+#' @title sceCeldaCG
127
+#' @description A \linkS4class{SingleCellExperiment} object containing the
128
+#'  results of running \link{celda_CG} on \link{celdaCGSim}.
129
+#' @format A \linkS4class{SingleCellExperiment} object
130
+#' @examples
131
+#' data(celdaCGSim)
132
+#' sceCeldaCG = celda_CG(celdaCGSim$counts,
133
+#'     K = celdaCGSim$K,
134
+#'     L = celdaCGSim$L,
135
+#'     sampleLabel = celdaCGSim$sampleLabel)
136
+"sceCeldaCG"
137
+
138
+
139
+#' @title sceCeldaCGGridSearch
140
+#' @description A \linkS4class{SingleCellExperiment} object containing the
141
+#'  results of running \link{celdaGridSearch} on \link{celdaCGSim}.
142
+#' @format A \linkS4class{SingleCellExperiment} object
143
+#' @examples
144
+#' data(celdaCGSim)
145
+#' sceCeldaCGGridSearch <- celdaGridSearch(celdaCGSim$counts,
146
+#'     model = "celda_CG",
147
+#'     paramsTest = list(K = seq(4, 6), L = seq(9, 11)),
148
+#'     paramsFixed = list(sampleLabel = celdaCGSim$sampleLabel),
149
+#'     bestOnly = TRUE,
150
+#'     nchains = 1,
151
+#'     cores = 1)
152
+"sceCeldaCGGridSearch"
124 153
new file mode 100644
... ...
@@ -0,0 +1,362 @@
1
+#' @title Generate factorized matrices showing each feature's influence on cell
2
+#'  / gene clustering
3
+#' @description Generates factorized matrices showing the contribution of each
4
+#'  feature in each cell population or each cell population in each sample.
5
+#' @param x Can be one of
6
+#'  \itemize{
7
+#'  \item A \linkS4class{SingleCellExperiment} object returned by
8
+#'  \link{celda_C}, \link{celda_G} or \link{celda_CG}, with the matrix
9
+#'  located in the \code{useAssay} assay slot.
10
+#'  Rows represent features and columns represent cells.
11
+#'  \item Integer counts matrix. Rows represent features and columns represent
12
+#'  cells. This matrix should be the same as the one used to generate
13
+#'  \code{celdaMod}.}
14
+#' @param useAssay A string specifying which \link[SummarizedExperiment]{assay}
15
+#'  slot to use if \code{x} is a \linkS4class{SingleCellExperiment} object.
16
+#'  Default "counts".
17
+#' @param celdaMod Celda model object. Only works if \code{x} is an integer
18
+#'  counts matrix.
19
+#' @param type Character vector. A vector containing one or more of "counts",
20
+#'  "proportion", or "posterior". "counts" returns the raw number of counts for
21
+#'  each factorized matrix. "proportions" returns the normalized probabilities
22
+#'  for each factorized matrix, which are calculated by dividing the raw counts
23
+#'  in each factorized matrix by the total counts in each column. "posterior"
24
+#'  returns the posterior estimates which include the addition of the Dirichlet
25
+#'  concentration parameter (essentially as a pseudocount). Default
26
+#'  \code{"counts"}.
27
+#' @export
28
+setGeneric("factorizeMatrix",
29
+    function(x, celdaMod, ...) {standardGeneric("factorizeMatrix")})
30
+
31
+
32
+#' @examples
33
+#' data(sceCeldaCG)
34
+#' factorizedMatrices <- factorizeMatrix(sceCeldaCG, type = "posterior")
35
+#' @rdname factorizeMatrix
36
+#' @export
37
+setMethod("factorizeMatrix", signature(x = "SingleCellExperiment"),
38
+    function(x,
39
+        useAssay = "counts",
40
+        type = c("counts", "proportion", "posterior")) {
41
+
42
+        if (celdaModel(x) == "celda_C") {
43
+            res <- .factorizeMatrixCelda_C(sce = x, useAssay = useAssay,
44
+                type = type)
45
+        } else if (celdaModel(x) == "celda_CG") {
46
+            res <- .factorizeMatrixCelda_CG(sce = x, useAssay = useAssay,
47
+                type = type)
48
+        } else if (celdaModel(x) == "celda_G") {
49
+            res <- .factorizeMatrixCelda_G(sce = x, useAssay = useAssay,
50
+                type = type)
51
+        } else {
52
+            stop("S4Vectors::metadata(x)$celda_parameters$model must be",
53
+                " one of 'celda_C', 'celda_G', or 'celda_CG'")
54
+        }
55
+        return(res)
56
+    })
57
+
58
+
59
+#' @return A list with elements for `counts`, `proportions`, or `posterior`
60
+#'  probabilities. Each element will be a list containing factorized matrices
61
+#'  for `module`, `cellPopulation`, and `sample`. Additionally, the
62
+#'  contribution of each module in each individual cell will be included in the
63
+#'  `cell` element of `counts` and `proportions` elements.
64
+#' @examples
65
+#' data(celdaCGSim, celdaCGMod)
66
+#' factorizedMatrices <- factorizeMatrix(
67
+#'   celdaCGSim$counts,
68
+#'   celdaCGMod,
69
+#'   "posterior")
70
+#' @rdname factorizeMatrix
71
+#' @export
72
+setMethod("factorizeMatrix", signature(x = "matrix", celdaMod = "celda_CG"),
73
+    function(x,
74
+        celdaMod,
75
+        type = c("counts", "proportion", "posterior")) {
76
+
77
+        counts <- .processCounts(x)
78
+        compareCountMatrix(counts, celdaMod)
79
+
80
+        K <- params(celdaMod)$K
81
+        L <- params(celdaMod)$L
82
+        z <- clusters(celdaMod)$z
83
+        y <- clusters(celdaMod)$y
84
+        alpha <- params(celdaMod)$alpha
85
+        beta <- params(celdaMod)$beta
86
+        delta <- params(celdaMod)$delta
87
+        gamma <- params(celdaMod)$gamma
88
+        sampleLabel <- sampleLabel(celdaMod)
89
+        s <- as.integer(sampleLabel)
90
+
91
+        ## Calculate counts one time up front
92
+        p <- .cCGDecomposeCounts(counts, s, z, y, K, L)
93
+        nS <- p$nS
94
+        nG <- p$nG
95
+        nM <- p$nM
96
+        mCPByS <- p$mCPByS
97
+        nTSByC <- p$nTSByC
98
+        nTSByCP <- p$nTSByCP
99
+        nByG <- p$nByG
100
+        nByTS <- p$nByTS
101
+        nGByTS <- p$nGByTS
102
+        nGByTS[nGByTS == 0] <- 1
103
+
104
+        nGByTS <- matrix(0, nrow = length(y), ncol = L)
105
+        nGByTS[cbind(seq(nG), y)] <- p$nByG
106
+
107
+        LNames <- paste0("L", seq(L))
108
+        KNames <- paste0("K", seq(K))
109
+        colnames(nTSByC) <- matrixNames(celdaMod)$column
110
+        rownames(nTSByC) <- LNames
111
+        colnames(nGByTS) <- LNames
112
+        rownames(nGByTS) <- matrixNames(celdaMod)$row
113
+        rownames(mCPByS) <- KNames
114
+        colnames(mCPByS) <- matrixNames(celdaMod)$sample
115
+        colnames(nTSByCP) <- KNames
116
+        rownames(nTSByCP) <- LNames
117
+
118
+        countsList <- c()
119
+        propList <- c()
120
+        postList <- c()
121
+        res <- list()
122
+
123
+        if (any("counts" %in% type)) {
124
+            countsList <- list(
125
+                sample = mCPByS,
126
+                cellPopulation = nTSByCP,
127
+                cell = nTSByC,
128
+                module = nGByTS,
129
+                geneDistribution = nGByTS
130
+            )
131
+            res <- c(res, list(counts = countsList))
132
+        }
133
+
134
+        if (any("proportion" %in% type)) {
135
+            ## Need to avoid normalizing cell/gene states with zero cells/genes
136
+            uniqueZ <- sort(unique(z))
137
+            tempNTSByCP <- nTSByCP
138
+            tempNTSByCP[, uniqueZ] <- normalizeCounts(tempNTSByCP[, uniqueZ],
139
+                normalize = "proportion"
140
+            )
141
+
142
+            uniqueY <- sort(unique(y))
143
+            tempNGByTS <- nGByTS
144
+            tempNGByTS[, uniqueY] <- normalizeCounts(tempNGByTS[, uniqueY],
145
+                normalize = "proportion"
146
+            )
147
+            tempNGByTS <- nGByTS / sum(nGByTS)
148
+
149
+            propList <- list(
150
+                sample = normalizeCounts(mCPByS,
151
+                    normalize = "proportion"
152
+                ),
153
+                cellPopulation = tempNTSByCP,
154
+                cell = normalizeCounts(nTSByC, normalize = "proportion"),
155
+                module = tempNGByTS,
156
+                geneDistribution = tempNGByTS
157
+            )
158
+            res <- c(res, list(proportions = propList))
159
+        }
160
+
161
+        if (any("posterior" %in% type)) {
162
+            gs <- nGByTS
163
+            gs[cbind(seq(nG), y)] <- gs[cbind(seq(nG), y)] + delta
164
+            gs <- normalizeCounts(gs, normalize = "proportion")
165
+            tempNGByTS <- (nGByTS + gamma) / sum(nGByTS + gamma)
166
+
167
+            postList <- list(
168
+                sample = normalizeCounts(mCPByS + alpha,
169
+                    normalize = "proportion"
170
+                ),
171
+                cellPopulation = normalizeCounts(nTSByCP + beta,
172
+                    normalize = "proportion"
173
+                ),
174
+                module = gs,
175
+                geneDistribution = tempNGByTS
176
+            )
177
+            res <- c(res, posterior = list(postList))
178
+        }
179
+        return(res)
180
+    }
181
+)
182
+
183
+
184
+#' @examples
185
+#' data(celdaCSim, celdaCMod)
186
+#' factorizedMatrices <- factorizeMatrix(
187
+#'   celdaCSim$counts,
188
+#'   celdaCMod, "posterior"
189
+#' )
190
+#' @return A list with elements for `counts`, `proportions`, or `posterior`
191
+#'  probabilities. Each element will be a list containing factorized matrices
192
+#'  for `module` and `sample`.
193
+#' @rdname factorizeMatrix
194
+#' @export
195
+setMethod(
196
+    "factorizeMatrix", signature(x = "matrix", celdaMod = "celda_C"),
197
+    function(x,
198
+        celdaMod,
199
+        type = c("counts", "proportion", "posterior")) {
200
+
201
+        counts <- .processCounts(x)
202
+        compareCountMatrix(counts, celdaMod)
203
+
204
+        K <- params(celdaMod)$K
205
+        z <- clusters(celdaMod)$z
206
+        alpha <- params(celdaMod)$alpha
207
+        beta <- params(celdaMod)$beta
208
+        sampleLabel <- sampleLabel(celdaMod)
209
+        s <- as.integer(sampleLabel)
210
+
211
+        p <- .cCDecomposeCounts(counts, s, z, K)
212
+        mCPByS <- p$mCPByS
213
+        nGByCP <- p$nGByCP
214
+
215
+        KNames <- paste0("K", seq(K))
216
+        rownames(nGByCP) <- matrixNames(celdaMod)$row
217
+        colnames(nGByCP) <- KNames
218
+        rownames(mCPByS) <- KNames
219
+        colnames(mCPByS) <- matrixNames(celdaMod)$sample
220
+
221
+        countsList <- c()
222
+        propList <- c()
223
+        postList <- c()
224
+        res <- list()
225
+
226
+        if (any("counts" %in% type)) {
227
+            countsList <- list(sample = mCPByS, module = nGByCP)
228
+            res <- c(res, list(counts = countsList))
229
+        }
230
+
231
+        if (any("proportion" %in% type)) {
232
+            ## Need to avoid normalizing cell/gene states with zero cells/genes
233
+            uniqueZ <- sort(unique(z))
234
+            tempNGByCP <- nGByCP
235
+            tempNGByCP[, uniqueZ] <- normalizeCounts(tempNGByCP[, uniqueZ],
236
+                normalize = "proportion"
237
+            )
238
+
239
+            propList <- list(
240
+                sample = normalizeCounts(mCPByS,
241
+                    normalize = "proportion"