... | ... |
@@ -55,7 +55,7 @@ |
55 | 55 |
#' @importFrom doParallel registerDoParallel |
56 | 56 |
#' @importFrom methods is |
57 | 57 |
#' @examples |
58 |
-#' \donttest{ |
|
58 |
+#' \dontrun{ |
|
59 | 59 |
#' data(celdaCGSim) |
60 | 60 |
#' ## Run various combinations of parameters with 'celdaGridSearch' |
61 | 61 |
#' celdaCGGridSearchRes <- celdaGridSearch(celdaCGSim$counts, |
... | ... |
@@ -55,7 +55,7 @@ |
55 | 55 |
#' @importFrom doParallel registerDoParallel |
56 | 56 |
#' @importFrom methods is |
57 | 57 |
#' @examples |
58 |
-#' \dontrun{ |
|
58 |
+#' \donttest{ |
|
59 | 59 |
#' data(celdaCGSim) |
60 | 60 |
#' ## Run various combinations of parameters with 'celdaGridSearch' |
61 | 61 |
#' celdaCGGridSearchRes <- celdaGridSearch(celdaCGSim$counts, |
... | ... |
@@ -46,7 +46,6 @@ |
46 | 46 |
#' @return A \linkS4class{SingleCellExperiment} object. Function |
47 | 47 |
#' parameter settings and celda model results are stored in the |
48 | 48 |
#' \link{metadata} \code{"celda_grid_search"} slot. |
49 |
-#' @param ... Ignored. Placeholder to prevent check warning. |
|
50 | 49 |
#' @seealso \link{celda_G} for feature clustering, \link{celda_C} for |
51 | 50 |
#' clustering of cells, and \link{celda_CG} for simultaneous clustering of |
52 | 51 |
#' features and cells. \link{subsetCeldaList} can subset the \code{celdaList} |
... | ... |
@@ -68,7 +67,22 @@ |
68 | 67 |
#' cores = 1) |
69 | 68 |
#' } |
70 | 69 |
#' @export |
71 |
-setGeneric("celdaGridSearch", function(x, ...) { |
|
70 |
+setGeneric("celdaGridSearch", |
|
71 |
+ function( |
|
72 |
+ x, |
|
73 |
+ useAssay = "counts", |
|
74 |
+ altExpName = "featureSubset", |
|
75 |
+ model, |
|
76 |
+ paramsTest, |
|
77 |
+ paramsFixed = NULL, |
|
78 |
+ maxIter = 200, |
|
79 |
+ nchains = 3, |
|
80 |
+ cores = 1, |
|
81 |
+ bestOnly = TRUE, |
|
82 |
+ seed = 12345, |
|
83 |
+ perplexity = TRUE, |
|
84 |
+ verbose = TRUE, |
|
85 |
+ logfilePrefix = "Celda") { |
|
72 | 86 |
standardGeneric("celdaGridSearch")}) |
73 | 87 |
|
74 | 88 |
|
... | ... |
@@ -442,7 +456,6 @@ setMethod("celdaGridSearch", |
442 | 456 |
#' models in list \code{"celda_grid_search"} in \code{metadata(x)}. |
443 | 457 |
#' @param altExpName The name for the \link{altExp} slot |
444 | 458 |
#' to use. Default "featureSubset". |
445 |
-#' @param ... Ignored. Placeholder to prevent check warning. |
|
446 | 459 |
#' @return One of |
447 | 460 |
#' \itemize{ |
448 | 461 |
#' \item A new \linkS4class{SingleCellExperiment} object containing |
... | ... |
@@ -464,7 +477,9 @@ setMethod("celdaGridSearch", |
464 | 477 |
#' chains in parallel. \link{selectBestModel} can get the best model for each |
465 | 478 |
#' combination of parameters. |
466 | 479 |
#' @export |
467 |
-setGeneric("subsetCeldaList", function(x, ...) { |
|
480 |
+setGeneric("subsetCeldaList", |
|
481 |
+ function(x, params, altExpName = "featureSubset") { |
|
482 |
+ |
|
468 | 483 |
standardGeneric("subsetCeldaList")}) |
469 | 484 |
|
470 | 485 |
|
... | ... |
@@ -583,7 +598,6 @@ setMethod("subsetCeldaList", |
583 | 598 |
#' corresponding celda model object. |
584 | 599 |
#' @param altExpName The name for the \link{altExp} slot |
585 | 600 |
#' to use. Default "featureSubset". |
586 |
-#' @param ... Ignored. Placeholder to prevent check warning. |
|
587 | 601 |
#' @return One of |
588 | 602 |
#' \itemize{ |
589 | 603 |
#' \item A new \linkS4class{SingleCellExperiment} object containing |
... | ... |
@@ -602,7 +616,9 @@ setMethod("subsetCeldaList", |
602 | 616 |
#' instead of a \code{celdaList} object.} |
603 | 617 |
#' @seealso \link{celdaGridSearch} \link{subsetCeldaList} |
604 | 618 |
#' @export |
605 |
-setGeneric("selectBestModel", function(x, ...) { |
|
619 |
+setGeneric("selectBestModel", |
|
620 |
+ function(x, asList = FALSE, altExpName = "featureSubset") { |
|
621 |
+ |
|
606 | 622 |
standardGeneric("selectBestModel")}) |
607 | 623 |
|
608 | 624 |
|
... | ... |
@@ -622,6 +622,7 @@ setMethod("selectBestModel", signature(x = "SingleCellExperiment"), |
622 | 622 |
c("index", "chain", "logLikelihood", "mean_perplexity", "seed")) |
623 | 623 |
runParams <- S4Vectors::metadata(altExp)$celda_grid_search@runParams |
624 | 624 |
dt <- data.table::as.data.table(runParams) |
625 |
+ .SD <- NULL # fix check note |
|
625 | 626 |
newRunParams <- as.data.frame(dt[, .SD[which.max(logLikelihood)], |
626 | 627 |
by = group]) |
627 | 628 |
newRunParams <- newRunParams[, colnames(runParams)] |
... | ... |
@@ -654,6 +655,7 @@ setMethod("selectBestModel", signature(x = "celdaList"), |
654 | 655 |
group <- setdiff(colnames(runParams(x)), |
655 | 656 |
c("index", "chain", "logLikelihood", "mean_perplexity", "seed")) |
656 | 657 |
dt <- data.table::as.data.table(runParams(x)) |
658 |
+ .SD <- NULL # fix check note |
|
657 | 659 |
newRunParams <- as.data.frame(dt[, .SD[which.max(logLikelihood)], |
658 | 660 |
by = group]) |
659 | 661 |
newRunParams <- newRunParams[, colnames(runParams(x))] |
... | ... |
@@ -12,8 +12,8 @@ |
12 | 12 |
#' with the matrix located in the assay slot under \code{useAssay}. |
13 | 13 |
#' Rows represent features and columns represent cells. |
14 | 14 |
#' @param useAssay A string specifying the name of the |
15 |
-#' \link[SummarizedExperiment]{assay} slot to use. Default "counts". |
|
16 |
-#' @param altExpName The name for the \link[SingleCellExperiment]{altExp} slot |
|
15 |
+#' \link{assay} slot to use. Default "counts". |
|
16 |
+#' @param altExpName The name for the \link{altExp} slot |
|
17 | 17 |
#' to use. Default "featureSubset". |
18 | 18 |
#' @param model Celda model. Options available in \link{availableModels}. |
19 | 19 |
#' @param paramsTest List. A list denoting the combinations of parameters to |
... | ... |
@@ -45,7 +45,7 @@ |
45 | 45 |
#' and main process. Default "Celda". |
46 | 46 |
#' @return A \linkS4class{SingleCellExperiment} object. Function |
47 | 47 |
#' parameter settings and celda model results are stored in the |
48 |
-#' \link[S4Vectors]{metadata} \code{"celda_grid_search"} slot. |
|
48 |
+#' \link{metadata} \code{"celda_grid_search"} slot. |
|
49 | 49 |
#' @param ... Ignored. Placeholder to prevent check warning. |
50 | 50 |
#' @seealso \link{celda_G} for feature clustering, \link{celda_C} for |
51 | 51 |
#' clustering of cells, and \link{celda_CG} for simultaneous clustering of |
... | ... |
@@ -440,7 +440,7 @@ setMethod("celdaGridSearch", |
440 | 440 |
#' \item celdaList object.} |
441 | 441 |
#' @param params List. List of parameters used to subset the matching celda |
442 | 442 |
#' models in list \code{"celda_grid_search"} in \code{metadata(x)}. |
443 |
-#' @param altExpName The name for the \link[SingleCellExperiment]{altExp} slot |
|
443 |
+#' @param altExpName The name for the \link{altExp} slot |
|
444 | 444 |
#' to use. Default "featureSubset". |
445 | 445 |
#' @param ... Ignored. Placeholder to prevent check warning. |
446 | 446 |
#' @return One of |
... | ... |
@@ -451,10 +451,10 @@ setMethod("celdaGridSearch", |
451 | 451 |
#' \code{"celda_grid_search"} slot in \code{metadata(x)} matches |
452 | 452 |
#' the given criteria, a new \linkS4class{SingleCellExperiment} object |
453 | 453 |
#' with the matching model stored in the |
454 |
-#' \link[S4Vectors]{metadata} |
|
454 |
+#' \link{metadata} |
|
455 | 455 |
#' \code{"celda_parameters"} slot will be returned. Otherwise, a new |
456 | 456 |
#' \linkS4class{SingleCellExperiment} object with the subset models stored |
457 |
-#' in the \link[S4Vectors]{metadata} |
|
457 |
+#' in the \link{metadata} |
|
458 | 458 |
#' \code{"celda_grid_search"} slot will be returned. |
459 | 459 |
#' \item A new \code{celdaList} object containing all models matching the |
460 | 460 |
#' provided criteria in \code{params}. If only one item in the |
... | ... |
@@ -581,7 +581,7 @@ setMethod("subsetCeldaList", |
581 | 581 |
#' best model as a |
582 | 582 |
#' \code{celdaList} object or not. If \code{FALSE}, return the best model as a |
583 | 583 |
#' corresponding celda model object. |
584 |
-#' @param altExpName The name for the \link[SingleCellExperiment]{altExp} slot |
|
584 |
+#' @param altExpName The name for the \link{altExp} slot |
|
585 | 585 |
#' to use. Default "featureSubset". |
586 | 586 |
#' @param ... Ignored. Placeholder to prevent check warning. |
587 | 587 |
#' @return One of |
... | ... |
@@ -591,10 +591,10 @@ setMethod("subsetCeldaList", |
591 | 591 |
#' \code{metadata(x)}. If there is only one set of parameters, |
592 | 592 |
#' a new \linkS4class{SingleCellExperiment} object |
593 | 593 |
#' with the matching model stored in the |
594 |
-#' \link[S4Vectors]{metadata} |
|
594 |
+#' \link{metadata} |
|
595 | 595 |
#' \code{"celda_parameters"} slot will be returned. Otherwise, a new |
596 | 596 |
#' \linkS4class{SingleCellExperiment} object with the subset models stored |
597 |
-#' in the \link[S4Vectors]{metadata} |
|
597 |
+#' in the \link{metadata} |
|
598 | 598 |
#' \code{"celda_grid_search"} slot will be returned. |
599 | 599 |
#' \item A new \code{celdaList} object containing one model with the best |
600 | 600 |
#' log-likelihood for each set of parameters. If only one set of parameters |
... | ... |
@@ -440,9 +440,6 @@ setMethod("celdaGridSearch", |
440 | 440 |
#' \item celdaList object.} |
441 | 441 |
#' @param params List. List of parameters used to subset the matching celda |
442 | 442 |
#' models in list \code{"celda_grid_search"} in \code{metadata(x)}. |
443 |
-#' @param useAssay A string specifying which \code{assay} |
|
444 |
-#' slot to use if \code{x} is a |
|
445 |
-#' \linkS4class{SingleCellExperiment} object. Default "counts". |
|
446 | 443 |
#' @param altExpName The name for the \link[SingleCellExperiment]{altExp} slot |
447 | 444 |
#' to use. Default "featureSubset". |
448 | 445 |
#' @param ... Ignored. Placeholder to prevent check warning. |
... | ... |
@@ -479,7 +476,7 @@ setGeneric("subsetCeldaList", function(x, ...) { |
479 | 476 |
#' @export |
480 | 477 |
setMethod("subsetCeldaList", |
481 | 478 |
signature(x = "SingleCellExperiment"), |
482 |
- function(x, params, useAssay = "counts", altExpName = "featureSubset") { |
|
479 |
+ function(x, params, altExpName = "featureSubset") { |
|
483 | 480 |
|
484 | 481 |
## Check for bad parameter names |
485 | 482 |
if (!all(names(params) %in% colnames(runParams(x, |
... | ... |
@@ -584,9 +581,6 @@ setMethod("subsetCeldaList", |
584 | 581 |
#' best model as a |
585 | 582 |
#' \code{celdaList} object or not. If \code{FALSE}, return the best model as a |
586 | 583 |
#' corresponding celda model object. |
587 |
-#' @param useAssay A string specifying which \code{assay} |
|
588 |
-#' slot to use if \code{x} is a |
|
589 |
-#' \linkS4class{SingleCellExperiment} object. Default "counts". |
|
590 | 584 |
#' @param altExpName The name for the \link[SingleCellExperiment]{altExp} slot |
591 | 585 |
#' to use. Default "featureSubset". |
592 | 586 |
#' @param ... Ignored. Placeholder to prevent check warning. |
... | ... |
@@ -620,8 +614,7 @@ setGeneric("selectBestModel", function(x, ...) { |
620 | 614 |
#' @importFrom data.table as.data.table |
621 | 615 |
#' @export |
622 | 616 |
setMethod("selectBestModel", signature(x = "SingleCellExperiment"), |
623 |
- function(x, asList = FALSE, useAssay = "counts", |
|
624 |
- altExpName = "featureSubset") { |
|
617 |
+ function(x, asList = FALSE, altExpName = "featureSubset") { |
|
625 | 618 |
|
626 | 619 |
altExp <- SingleCellExperiment::altExp(x, altExpName) |
627 | 620 |
logLikelihood <- NULL |
... | ... |
@@ -46,6 +46,7 @@ |
46 | 46 |
#' @return A \linkS4class{SingleCellExperiment} object. Function |
47 | 47 |
#' parameter settings and celda model results are stored in the |
48 | 48 |
#' \link[S4Vectors]{metadata} \code{"celda_grid_search"} slot. |
49 |
+#' @param ... Ignored. Placeholder to prevent check warning. |
|
49 | 50 |
#' @seealso \link{celda_G} for feature clustering, \link{celda_C} for |
50 | 51 |
#' clustering of cells, and \link{celda_CG} for simultaneous clustering of |
51 | 52 |
#' features and cells. \link{subsetCeldaList} can subset the \code{celdaList} |
... | ... |
@@ -444,6 +445,7 @@ setMethod("celdaGridSearch", |
444 | 445 |
#' \linkS4class{SingleCellExperiment} object. Default "counts". |
445 | 446 |
#' @param altExpName The name for the \link[SingleCellExperiment]{altExp} slot |
446 | 447 |
#' to use. Default "featureSubset". |
448 |
+#' @param ... Ignored. Placeholder to prevent check warning. |
|
447 | 449 |
#' @return One of |
448 | 450 |
#' \itemize{ |
449 | 451 |
#' \item A new \linkS4class{SingleCellExperiment} object containing |
... | ... |
@@ -587,6 +589,7 @@ setMethod("subsetCeldaList", |
587 | 589 |
#' \linkS4class{SingleCellExperiment} object. Default "counts". |
588 | 590 |
#' @param altExpName The name for the \link[SingleCellExperiment]{altExp} slot |
589 | 591 |
#' to use. Default "featureSubset". |
592 |
+#' @param ... Ignored. Placeholder to prevent check warning. |
|
590 | 593 |
#' @return One of |
591 | 594 |
#' \itemize{ |
592 | 595 |
#' \item A new \linkS4class{SingleCellExperiment} object containing |
... | ... |
@@ -505,7 +505,8 @@ setMethod("subsetCeldaList", |
505 | 505 |
} |
506 | 506 |
|
507 | 507 |
## Get index of selected models, subset celdaList, and return |
508 |
- ix <- match(newRunParams$index, runParams(x, altExpName = altExpName)$index) |
|
508 |
+ ix <- match(newRunParams$index, runParams(x, |
|
509 |
+ altExpName = altExpName)$index) |
|
509 | 510 |
altExp <- SingleCellExperiment::altExp(x, altExpName) |
510 | 511 |
|
511 | 512 |
if (length(ix) == 1) { |
... | ... |
@@ -480,29 +480,32 @@ setMethod("subsetCeldaList", |
480 | 480 |
function(x, params, useAssay = "counts", altExpName = "featureSubset") { |
481 | 481 |
|
482 | 482 |
## Check for bad parameter names |
483 |
- if (!all(names(params) %in% colnames(runParams(x)))) { |
|
484 |
- badParams <- setdiff(names(params), colnames(runParams(x))) |
|
483 |
+ if (!all(names(params) %in% colnames(runParams(x, |
|
484 |
+ altExpName = altExpName)))) { |
|
485 |
+ badParams <- setdiff(names(params), |
|
486 |
+ colnames(runParams(x, altExpName = altExpName))) |
|
485 | 487 |
stop("The following elements in 'params' are not columns in", |
486 |
- " runParams(x) ", |
|
488 |
+ " runParams(x, altExpName = altExpName) ", |
|
487 | 489 |
paste(badParams, collapse = ",") |
488 | 490 |
) |
489 | 491 |
} |
490 | 492 |
|
491 | 493 |
## Subset 'runParams' based on items in 'params' |
492 |
- newRunParams <- runParams(x) |
|
494 |
+ newRunParams <- runParams(x, altExpName = altExpName) |
|
493 | 495 |
for (i in names(params)) { |
494 | 496 |
newRunParams <- |
495 | 497 |
subset(newRunParams, newRunParams[, i] %in% params[[i]]) |
496 | 498 |
|
497 | 499 |
if (nrow(newRunParams) == 0) { |
498 | 500 |
stop("No runs matched the criteria given in 'params'. Check", |
499 |
- " 'runParams(x)' for complete list of parameters used", |
|
501 |
+ " 'runParams(x, altExpName = altExpName)' for complete", |
|
502 |
+ " list of parameters used", |
|
500 | 503 |
" to generate 'x'.") |
501 | 504 |
} |
502 | 505 |
} |
503 | 506 |
|
504 | 507 |
## Get index of selected models, subset celdaList, and return |
505 |
- ix <- match(newRunParams$index, runParams(x)$index) |
|
508 |
+ ix <- match(newRunParams$index, runParams(x, altExpName = altExpName)$index) |
|
506 | 509 |
altExp <- SingleCellExperiment::altExp(x, altExpName) |
507 | 510 |
|
508 | 511 |
if (length(ix) == 1) { |
... | ... |
@@ -618,7 +621,7 @@ setMethod("selectBestModel", signature(x = "SingleCellExperiment"), |
618 | 621 |
|
619 | 622 |
altExp <- SingleCellExperiment::altExp(x, altExpName) |
620 | 623 |
logLikelihood <- NULL |
621 |
- group <- setdiff(colnames(runParams(x)), |
|
624 |
+ group <- setdiff(colnames(runParams(x, altExpName = altExpName)), |
|
622 | 625 |
c("index", "chain", "logLikelihood", "mean_perplexity", "seed")) |
623 | 626 |
runParams <- S4Vectors::metadata(altExp)$celda_grid_search@runParams |
624 | 627 |
dt <- data.table::as.data.table(runParams) |
... | ... |
@@ -6,14 +6,15 @@ |
6 | 6 |
#' Fixed parameters to be used in all models, such as \code{sampleLabel}, can |
7 | 7 |
#' be passed as a list to the argument \code{paramsFixed}. When |
8 | 8 |
#' \code{verbose = TRUE}, output from each chain will be sent to a log file |
9 |
-#' but not be displayed in stdout. |
|
9 |
+#' but not be displayed in \code{stdout}. |
|
10 | 10 |
#' @param x A numeric \link{matrix} of counts or a |
11 | 11 |
#' \linkS4class{SingleCellExperiment} |
12 | 12 |
#' with the matrix located in the assay slot under \code{useAssay}. |
13 | 13 |
#' Rows represent features and columns represent cells. |
14 |
-#' @param useAssay A string specifying which \link[SummarizedExperiment]{assay} |
|
15 |
-#' slot to use if \code{x} is a |
|
16 |
-#' \link[SingleCellExperiment]{SingleCellExperiment} object. Default "counts". |
|
14 |
+#' @param useAssay A string specifying the name of the |
|
15 |
+#' \link[SummarizedExperiment]{assay} slot to use. Default "counts". |
|
16 |
+#' @param altExpName The name for the \link[SingleCellExperiment]{altExp} slot |
|
17 |
+#' to use. Default "featureSubset". |
|
17 | 18 |
#' @param model Celda model. Options available in \link{availableModels}. |
18 | 19 |
#' @param paramsTest List. A list denoting the combinations of parameters to |
19 | 20 |
#' run in a celda model. For example, |
... | ... |
@@ -76,6 +77,7 @@ setMethod("celdaGridSearch", |
76 | 77 |
signature(x = "SingleCellExperiment"), |
77 | 78 |
function(x, |
78 | 79 |
useAssay = "counts", |
80 |
+ altExpName = "featureSubset", |
|
79 | 81 |
model, |
80 | 82 |
paramsTest, |
81 | 83 |
paramsFixed = NULL, |
... | ... |
@@ -89,7 +91,19 @@ setMethod("celdaGridSearch", |
89 | 91 |
logfilePrefix = "Celda") { |
90 | 92 |
|
91 | 93 |
xClass <- "SingleCellExperiment" |
92 |
- counts <- SummarizedExperiment::assay(x, i = useAssay) |
|
94 |
+ |
|
95 |
+ if (!altExpName %in% SingleCellExperiment::altExpNames(x)) { |
|
96 |
+ stop(altExpName, " not in 'altExpNames(x)'. Run ", |
|
97 |
+ "selectFeatures(x) first!") |
|
98 |
+ } |
|
99 |
+ |
|
100 |
+ altExp <- SingleCellExperiment::altExp(x, altExpName) |
|
101 |
+ |
|
102 |
+ if (!useAssay %in% SummarizedExperiment::assayNames(altExp)) { |
|
103 |
+ stop(useAssay, " not in assayNames(altExp(x, altExpName))") |
|
104 |
+ } |
|
105 |
+ |
|
106 |
+ counts <- SummarizedExperiment::assay(altExp, i = useAssay) |
|
93 | 107 |
|
94 | 108 |
celdaList <- .celdaGridSearch(counts = counts, |
95 | 109 |
model = paste0(".", model), |
... | ... |
@@ -104,8 +118,8 @@ setMethod("celdaGridSearch", |
104 | 118 |
verbose = verbose, |
105 | 119 |
logfilePrefix = logfilePrefix) |
106 | 120 |
|
107 |
- sce <- .createSCEceldaGridSearch(celdaList = celdaList, |
|
108 |
- sce = x, |
|
121 |
+ altExp <- .createSCEceldaGridSearch(celdaList = celdaList, |
|
122 |
+ sce = altExp, |
|
109 | 123 |
xClass = xClass, |
110 | 124 |
useAssay = useAssay, |
111 | 125 |
model = model, |
... | ... |
@@ -119,6 +133,7 @@ setMethod("celdaGridSearch", |
119 | 133 |
perplexity = perplexity, |
120 | 134 |
verbose = verbose, |
121 | 135 |
logfilePrefix = logfilePrefix) |
136 |
+ SingleCellExperiment::altExp(x, altExpName) <- altExp |
|
122 | 137 |
return(sce) |
123 | 138 |
}) |
124 | 139 |
|
... | ... |
@@ -128,6 +143,8 @@ setMethod("celdaGridSearch", |
128 | 143 |
setMethod("celdaGridSearch", |
129 | 144 |
signature(x = "matrix"), |
130 | 145 |
function(x, |
146 |
+ useAssay = "counts", |
|
147 |
+ altExpName = "featureSubset", |
|
131 | 148 |
model, |
132 | 149 |
paramsTest, |
133 | 150 |
paramsFixed = NULL, |
... | ... |
@@ -140,10 +157,12 @@ setMethod("celdaGridSearch", |
140 | 157 |
verbose = TRUE, |
141 | 158 |
logfilePrefix = "Celda") { |
142 | 159 |
|
160 |
+ ls <- list() |
|
161 |
+ ls[[useAssay]] <- x |
|
162 |
+ sce <- SingleCellExperiment::SingleCellExperiment(assays = ls) |
|
163 |
+ SingleCellExperiment::altExp(sce, altExpName) <- sce |
|
143 | 164 |
xClass <- "matrix" |
144 |
- useAssay <- NULL |
|
145 |
- sce <- SingleCellExperiment::SingleCellExperiment( |
|
146 |
- assays = list(counts = x)) |
|
165 |
+ |
|
147 | 166 |
celdaList <- .celdaGridSearch(counts = x, |
148 | 167 |
model = paste0(".", model), |
149 | 168 |
paramsTest = paramsTest, |
... | ... |
@@ -157,8 +176,8 @@ setMethod("celdaGridSearch", |
157 | 176 |
verbose = verbose, |
158 | 177 |
logfilePrefix = logfilePrefix) |
159 | 178 |
|
160 |
- sce <- .createSCEceldaGridSearch(celdaList = celdaList, |
|
161 |
- sce = sce, |
|
179 |
+ altExp <- .createSCEceldaGridSearch(celdaList = celdaList, |
|
180 |
+ sce = SingleCellExperiment::altExp(sce, altExpName), |
|
162 | 181 |
xClass = xClass, |
163 | 182 |
useAssay = useAssay, |
164 | 183 |
model = model, |
... | ... |
@@ -172,6 +191,7 @@ setMethod("celdaGridSearch", |
172 | 191 |
perplexity = perplexity, |
173 | 192 |
verbose = verbose, |
174 | 193 |
logfilePrefix = logfilePrefix) |
194 |
+ SingleCellExperiment::altExp(sce, altExpName) <- altExp |
|
175 | 195 |
return(sce) |
176 | 196 |
}) |
177 | 197 |
|
... | ... |
@@ -422,6 +442,8 @@ setMethod("celdaGridSearch", |
422 | 442 |
#' @param useAssay A string specifying which \code{assay} |
423 | 443 |
#' slot to use if \code{x} is a |
424 | 444 |
#' \linkS4class{SingleCellExperiment} object. Default "counts". |
445 |
+#' @param altExpName The name for the \link[SingleCellExperiment]{altExp} slot |
|
446 |
+#' to use. Default "featureSubset". |
|
425 | 447 |
#' @return One of |
426 | 448 |
#' \itemize{ |
427 | 449 |
#' \item A new \linkS4class{SingleCellExperiment} object containing |
... | ... |
@@ -455,13 +477,13 @@ setGeneric("subsetCeldaList", function(x, ...) { |
455 | 477 |
#' @export |
456 | 478 |
setMethod("subsetCeldaList", |
457 | 479 |
signature(x = "SingleCellExperiment"), |
458 |
- function(x, params, useAssay = "counts") { |
|
480 |
+ function(x, params, useAssay = "counts", altExpName = "featureSubset") { |
|
459 | 481 |
|
460 | 482 |
## Check for bad parameter names |
461 | 483 |
if (!all(names(params) %in% colnames(runParams(x)))) { |
462 | 484 |
badParams <- setdiff(names(params), colnames(runParams(x))) |
463 | 485 |
stop("The following elements in 'params' are not columns in", |
464 |
- " runParams (x) ", |
|
486 |
+ " runParams(x) ", |
|
465 | 487 |
paste(badParams, collapse = ",") |
466 | 488 |
) |
467 | 489 |
} |
... | ... |
@@ -481,13 +503,17 @@ setMethod("subsetCeldaList", |
481 | 503 |
|
482 | 504 |
## Get index of selected models, subset celdaList, and return |
483 | 505 |
ix <- match(newRunParams$index, runParams(x)$index) |
506 |
+ altExp <- SingleCellExperiment::altExp(x, altExpName) |
|
507 |
+ |
|
484 | 508 |
if (length(ix) == 1) { |
485 |
- x <- .subsetCeldaListSCE(x, ix) |
|
509 |
+ altExp <- .subsetCeldaListSCE(altExp, ix) |
|
486 | 510 |
} else { |
487 |
- x@metadata$celda_grid_search@runParams <- |
|
511 |
+ altExp@metadata$celda_grid_search@runParams <- |
|
488 | 512 |
as.data.frame(newRunParams) |
489 |
- x@metadata$celda_grid_search@resList <- resList(x)[ix] |
|
513 |
+ altExp@metadata$celda_grid_search@resList <- |
|
514 |
+ altExp@metadata$celda_grid_search@resList[ix] |
|
490 | 515 |
} |
516 |
+ SingleCellExperiment::altExp(x, altExpName) <- altExp |
|
491 | 517 |
return(x) |
492 | 518 |
} |
493 | 519 |
) |
... | ... |
@@ -555,6 +581,8 @@ setMethod("subsetCeldaList", |
555 | 581 |
#' @param useAssay A string specifying which \code{assay} |
556 | 582 |
#' slot to use if \code{x} is a |
557 | 583 |
#' \linkS4class{SingleCellExperiment} object. Default "counts". |
584 |
+#' @param altExpName The name for the \link[SingleCellExperiment]{altExp} slot |
|
585 |
+#' to use. Default "featureSubset". |
|
558 | 586 |
#' @return One of |
559 | 587 |
#' \itemize{ |
560 | 588 |
#' \item A new \linkS4class{SingleCellExperiment} object containing |
... | ... |
@@ -585,11 +613,14 @@ setGeneric("selectBestModel", function(x, ...) { |
585 | 613 |
#' @importFrom data.table as.data.table |
586 | 614 |
#' @export |
587 | 615 |
setMethod("selectBestModel", signature(x = "SingleCellExperiment"), |
588 |
- function(x, asList = FALSE, useAssay = "counts") { |
|
616 |
+ function(x, asList = FALSE, useAssay = "counts", |
|
617 |
+ altExpName = "featureSubset") { |
|
618 |
+ |
|
619 |
+ altExp <- SingleCellExperiment::altExp(x, altExpName) |
|
589 | 620 |
logLikelihood <- NULL |
590 | 621 |
group <- setdiff(colnames(runParams(x)), |
591 | 622 |
c("index", "chain", "logLikelihood", "mean_perplexity", "seed")) |
592 |
- runParams <- S4Vectors::metadata(x)$celda_grid_search@runParams |
|
623 |
+ runParams <- S4Vectors::metadata(altExp)$celda_grid_search@runParams |
|
593 | 624 |
dt <- data.table::as.data.table(runParams) |
594 | 625 |
newRunParams <- as.data.frame(dt[, .SD[which.max(logLikelihood)], |
595 | 626 |
by = group]) |
... | ... |
@@ -597,12 +628,14 @@ setMethod("selectBestModel", signature(x = "SingleCellExperiment"), |
597 | 628 |
|
598 | 629 |
ix <- match(newRunParams$index, runParams$index) |
599 | 630 |
if (nrow(newRunParams) == 1 & !asList) { |
600 |
- x <- .subsetCeldaListSCE(x, ix) |
|
631 |
+ altExp <- .subsetCeldaListSCE(altExp, ix) |
|
601 | 632 |
} else { |
602 |
- x@metadata$celda_grid_search@runParams <- |
|
633 |
+ altExp@metadata$celda_grid_search@runParams <- |
|
603 | 634 |
as.data.frame(newRunParams) |
604 |
- x@metadata$celda_grid_search@resList <- resList(x)[ix] |
|
635 |
+ altExp@metadata$celda_grid_search@resList <- |
|
636 |
+ altExp@metadata$celda_grid_search@resList[ix] |
|
605 | 637 |
} |
638 |
+ SingleCellExperiment::altExp(x, altExpName) <- altExp |
|
606 | 639 |
return(x) |
607 | 640 |
} |
608 | 641 |
) |
... | ... |
@@ -676,7 +709,8 @@ setMethod("selectBestModel", signature(x = "celdaList"), |
676 | 709 |
.subsetCeldaListSCE <- function(x, ix) { |
677 | 710 |
cgsparam <- x@metadata$celda_grid_search@celdaGridSearchParameters |
678 | 711 |
if (cgsparam$model == "celda_C") { |
679 |
- x <- .createSCEceldaC(celdaCMod = resList(x)[[ix]], |
|
712 |
+ x <- .createSCEceldaC(celdaCMod = |
|
713 |
+ x@metadata$celda_grid_search@resList[[ix]], |
|
680 | 714 |
sce = x, |
681 | 715 |
xClass = cgsparam$xClass, |
682 | 716 |
useAssay = cgsparam$useAssay, |
... | ... |
@@ -691,7 +725,8 @@ setMethod("selectBestModel", signature(x = "celdaList"), |
691 | 725 |
logfile = cgsparam$logfile, |
692 | 726 |
verbose = cgsparam$verbose) |
693 | 727 |
} else if (cgsparam$model == "celda_G") { |
694 |
- x <- .createSCEceldaG(celdaGMod = resList(x)[[ix]], |
|
728 |
+ x <- .createSCEceldaG(celdaGMod = |
|
729 |
+ x@metadata$celda_grid_search@resList[[ix]], |
|
695 | 730 |
sce = x, |
696 | 731 |
xClass = cgsparam$xClass, |
697 | 732 |
useAssay = cgsparam$useAssay, |
... | ... |
@@ -705,7 +740,8 @@ setMethod("selectBestModel", signature(x = "celdaList"), |
705 | 740 |
logfile = cgsparam$logfile, |
706 | 741 |
verbose = cgsparam$verbose) |
707 | 742 |
} else if (cgsparam$model == "celda_CG") { |
708 |
- x <- .createSCEceldaCG(celdaCGMod = resList(x)[[ix]], |
|
743 |
+ x <- .createSCEceldaCG(celdaCGMod = |
|
744 |
+ x@metadata$celda_grid_search@resList[[ix]], |
|
709 | 745 |
sce = x, |
710 | 746 |
xClass = cgsparam$xClass, |
711 | 747 |
useAssay = cgsparam$useAssay, |
... | ... |
@@ -722,7 +758,7 @@ setMethod("selectBestModel", signature(x = "celdaList"), |
722 | 758 |
logfile = cgsparam$logfile, |
723 | 759 |
verbose = cgsparam$verbose) |
724 | 760 |
} else { |
725 |
- stop("S4Vectors::metadata(X)$celda_grid_search@", |
|
761 |
+ stop("S4Vectors::metadata(altExp(x, altExpName))$celda_grid_search@", |
|
726 | 762 |
"celdaGridSearchParameters$model must be", |
727 | 763 |
" one of 'celda_C', 'celda_G', or 'celda_CG'") |
728 | 764 |
} |
... | ... |
@@ -673,7 +673,7 @@ setMethod("selectBestModel", signature(x = "celdaList"), |
673 | 673 |
} |
674 | 674 |
|
675 | 675 |
|
676 |
-.subsetCeldaListSCE <- function (x, ix) { |
|
676 |
+.subsetCeldaListSCE <- function(x, ix) { |
|
677 | 677 |
cgsparam <- x@metadata$celda_grid_search@celdaGridSearchParameters |
678 | 678 |
if (cgsparam$model == "celda_C") { |
679 | 679 |
x <- .createSCEceldaC(celdaCMod = resList(x)[[ix]], |
... | ... |
@@ -686,10 +686,10 @@ setMethod("selectBestModel", signature(x = "celdaList"), |
686 | 686 |
splitOnIter = cgsparam$splitOnIter, |
687 | 687 |
splitOnLast = cgsparam$splitOnLast, |
688 | 688 |
nchains = cgsparam$nchains, |
689 |
- zInitialize = cgsparamp[["zInitialize"]], |
|
690 |
- zInit = cgsparamp[["zInit"]], |
|
691 |
- logfile = cgsparamp$logfile, |
|
692 |
- verbose = cgsparamp$verbose) |
|
689 |
+ zInitialize = cgsparam[["zInitialize"]], |
|
690 |
+ zInit = cgsparam[["zInit"]], |
|
691 |
+ logfile = cgsparam$logfile, |
|
692 |
+ verbose = cgsparam$verbose) |
|
693 | 693 |
} else if (cgsparam$model == "celda_G") { |
694 | 694 |
x <- .createSCEceldaG(celdaGMod = resList(x)[[ix]], |
695 | 695 |
sce = x, |
... | ... |
@@ -675,7 +675,7 @@ setMethod("selectBestModel", signature(x = "celdaList"), |
675 | 675 |
|
676 | 676 |
.subsetCeldaListSCE <- function (x, ix) { |
677 | 677 |
cgsparam <- x@metadata$celda_grid_search@celdaGridSearchParameters |
678 |
- if (cgsparam$model == "celda_c") { |
|
678 |
+ if (cgsparam$model == "celda_C") { |
|
679 | 679 |
x <- .createSCEceldaC(celdaCMod = resList(x)[[ix]], |
680 | 680 |
sce = x, |
681 | 681 |
xClass = cgsparam$xClass, |
... | ... |
@@ -413,14 +413,15 @@ setMethod("celdaGridSearch", |
413 | 413 |
#' @param x Can be one of |
414 | 414 |
#' \itemize{ |
415 | 415 |
#' \item A \linkS4class{SingleCellExperiment} object returned from |
416 |
-#' \code{celdaGridSearch}. Must contain a list named |
|
416 |
+#' \code{celdaGridSearch}, \code{recursiveSplitModule}, |
|
417 |
+#' or \code{recursiveSplitCell}. Must contain a list named |
|
417 | 418 |
#' \code{"celda_grid_search"} in \code{metadata(x)}. |
418 | 419 |
#' \item celdaList object.} |
419 | 420 |
#' @param params List. List of parameters used to subset the matching celda |
420 | 421 |
#' models in list \code{"celda_grid_search"} in \code{metadata(x)}. |
421 | 422 |
#' @param useAssay A string specifying which \code{assay} |
422 | 423 |
#' slot to use if \code{x} is a |
423 |
-#' \link[SingleCellExperiment]{SingleCellExperiment} object. Default "counts". |
|
424 |
+#' \linkS4class{SingleCellExperiment} object. Default "counts". |
|
424 | 425 |
#' @return One of |
425 | 426 |
#' \itemize{ |
426 | 427 |
#' \item A new \linkS4class{SingleCellExperiment} object containing |
... | ... |
@@ -472,19 +473,16 @@ setMethod("subsetCeldaList", |
472 | 473 |
subset(newRunParams, newRunParams[, i] %in% params[[i]]) |
473 | 474 |
|
474 | 475 |
if (nrow(newRunParams) == 0) { |
475 |
- stop( |
|
476 |
- "No runs matched the criteria given in 'params'. Check", |
|
476 |
+ stop("No runs matched the criteria given in 'params'. Check", |
|
477 | 477 |
" 'runParams(x)' for complete list of parameters used", |
478 |
- " to generate 'x'." |
|
479 |
- ) |
|
478 |
+ " to generate 'x'.") |
|
480 | 479 |
} |
481 | 480 |
} |
482 | 481 |
|
483 | 482 |
## Get index of selected models, subset celdaList, and return |
484 | 483 |
ix <- match(newRunParams$index, runParams(x)$index) |
485 | 484 |
if (length(ix) == 1) { |
486 |
- x <- celdatosce(resList(x)[[ix]], |
|
487 |
- SummarizedExperiment::assay(x, i = useAssay)) |
|
485 |
+ x <- .subsetCeldaListSCE(x, ix) |
|
488 | 486 |
} else { |
489 | 487 |
x@metadata$celda_grid_search@runParams <- |
490 | 488 |
as.data.frame(newRunParams) |
... | ... |
@@ -543,9 +541,13 @@ setMethod("subsetCeldaList", |
543 | 541 |
#' @description Select the chain with the best log likelihood for each |
544 | 542 |
#' combination of tested parameters from a \code{SCE} object gererated by |
545 | 543 |
#' \link{celdaGridSearch} or from a \code{celdaList} object. |
546 |
-#' @param x Object of class \linkS4class{SingleCellExperiment} or |
|
547 |
-#' \code{celdaList}. An object containing celda |
|
548 |
-#' models returned from \link{celdaGridSearch}. |
|
544 |
+#' @param x Can be one of |
|
545 |
+#' \itemize{ |
|
546 |
+#' \item A \linkS4class{SingleCellExperiment} object returned from |
|
547 |
+#' \code{celdaGridSearch}, \code{recursiveSplitModule}, |
|
548 |
+#' or \code{recursiveSplitCell}. Must contain a list named |
|
549 |
+#' \code{"celda_grid_search"} in \code{metadata(x)}. |
|
550 |
+#' \item celdaList object.} |
|
549 | 551 |
#' @param asList \code{TRUE} or \code{FALSE}. Whether to return the |
550 | 552 |
#' best model as a |
551 | 553 |
#' \code{celdaList} object or not. If \code{FALSE}, return the best model as a |
... | ... |
@@ -595,8 +597,7 @@ setMethod("selectBestModel", signature(x = "SingleCellExperiment"), |
595 | 597 |
|
596 | 598 |
ix <- match(newRunParams$index, runParams$index) |
597 | 599 |
if (nrow(newRunParams) == 1 & !asList) { |
598 |
- x <- celdatosce(resList(x)[[ix]], |
|
599 |
- SummarizedExperiment::assay(x, i = useAssay)) |
|
600 |
+ x <- .subsetCeldaListSCE(x, ix) |
|
600 | 601 |
} else { |
601 | 602 |
x@metadata$celda_grid_search@runParams <- |
602 | 603 |
as.data.frame(newRunParams) |
... | ... |
@@ -670,3 +671,60 @@ setMethod("selectBestModel", signature(x = "celdaList"), |
670 | 671 |
logfilePrefix = logfilePrefix) |
671 | 672 |
return(sce) |
672 | 673 |
} |
674 |
+ |
|
675 |
+ |
|
676 |
+.subsetCeldaListSCE <- function (x, ix) { |
|
677 |
+ cgsparam <- x@metadata$celda_grid_search@celdaGridSearchParameters |
|
678 |
+ if (cgsparam$model == "celda_c") { |
|
679 |
+ x <- .createSCEceldaC(celdaCMod = resList(x)[[ix]], |
|
680 |
+ sce = x, |
|
681 |
+ xClass = cgsparam$xClass, |
|
682 |
+ useAssay = cgsparam$useAssay, |
|
683 |
+ algorithm = cgsparam$algorithm, |
|
684 |
+ stopIter = cgsparam$stopIter, |
|
685 |
+ maxIter = cgsparam$maxIter, |
|
686 |
+ splitOnIter = cgsparam$splitOnIter, |
|
687 |
+ splitOnLast = cgsparam$splitOnLast, |
|
688 |
+ nchains = cgsparam$nchains, |
|
689 |
+ zInitialize = cgsparamp[["zInitialize"]], |
|
690 |
+ zInit = cgsparamp[["zInit"]], |
|
691 |
+ logfile = cgsparamp$logfile, |
|
692 |
+ verbose = cgsparamp$verbose) |
|
693 |
+ } else if (cgsparam$model == "celda_G") { |
|
694 |
+ x <- .createSCEceldaG(celdaGMod = resList(x)[[ix]], |
|
695 |
+ sce = x, |
|
696 |
+ xClass = cgsparam$xClass, |
|
697 |
+ useAssay = cgsparam$useAssay, |
|
698 |
+ stopIter = cgsparam$stopIter, |
|
699 |
+ maxIter = cgsparam$maxIter, |
|
700 |
+ splitOnIter = cgsparam$splitOnIter, |
|
701 |
+ splitOnLast = cgsparam$splitOnLast, |
|
702 |
+ nchains = cgsparam$nchains, |
|
703 |
+ yInitialize = cgsparam[["yInitialize"]], |
|
704 |
+ yInit = cgsparam[["yInit"]], |
|
705 |
+ logfile = cgsparam$logfile, |
|
706 |
+ verbose = cgsparam$verbose) |
|
707 |
+ } else if (cgsparam$model == "celda_CG") { |
|
708 |
+ x <- .createSCEceldaCG(celdaCGMod = resList(x)[[ix]], |
|
709 |
+ sce = x, |
|
710 |
+ xClass = cgsparam$xClass, |
|
711 |
+ useAssay = cgsparam$useAssay, |
|
712 |
+ algorithm = cgsparam$algorithm, |
|
713 |
+ stopIter = cgsparam$stopIter, |
|
714 |
+ maxIter = cgsparam$maxIter, |
|
715 |
+ splitOnIter = cgsparam$splitOnIter, |
|
716 |
+ splitOnLast = cgsparam$splitOnLast, |
|
717 |
+ nchains = cgsparam$nchains, |
|
718 |
+ zInitialize = cgsparam[["zInitialize"]], |
|
719 |
+ yInitialize = cgsparam[["yInitialize"]], |
|
720 |
+ zInit = cgsparam[["zInit"]], |
|
721 |
+ yInit = cgsparam[["yInit"]], |
|
722 |
+ logfile = cgsparam$logfile, |
|
723 |
+ verbose = cgsparam$verbose) |
|
724 |
+ } else { |
|
725 |
+ stop("S4Vectors::metadata(X)$celda_grid_search@", |
|
726 |
+ "celdaGridSearchParameters$model must be", |
|
727 |
+ " one of 'celda_C', 'celda_G', or 'celda_CG'") |
|
728 |
+ } |
|
729 |
+ return(x) |
|
730 |
+} |
... | ... |
@@ -587,12 +587,13 @@ setMethod("selectBestModel", signature(x = "SingleCellExperiment"), |
587 | 587 |
logLikelihood <- NULL |
588 | 588 |
group <- setdiff(colnames(runParams(x)), |
589 | 589 |
c("index", "chain", "logLikelihood", "mean_perplexity", "seed")) |
590 |
- dt <- data.table::as.data.table(runParams(celdaList)) |
|
590 |
+ runParams <- S4Vectors::metadata(x)$celda_grid_search@runParams |
|
591 |
+ dt <- data.table::as.data.table(runParams) |
|
591 | 592 |
newRunParams <- as.data.frame(dt[, .SD[which.max(logLikelihood)], |
592 | 593 |
by = group]) |
593 |
- newRunParams <- newRunParams[, colnames(runParams(celdaList))] |
|
594 |
+ newRunParams <- newRunParams[, colnames(runParams)] |
|
594 | 595 |
|
595 |
- ix <- match(newRunParams$index, runParams(celdaList)$index) |
|
596 |
+ ix <- match(newRunParams$index, runParams$index) |
|
596 | 597 |
if (nrow(newRunParams) == 1 & !asList) { |
597 | 598 |
x <- celdatosce(resList(x)[[ix]], |
598 | 599 |
SummarizedExperiment::assay(x, i = useAssay)) |
... | ... |
@@ -47,7 +47,7 @@ |
47 | 47 |
#' \link[S4Vectors]{metadata} \code{"celda_grid_search"} slot. |
48 | 48 |
#' @seealso \link{celda_G} for feature clustering, \link{celda_C} for |
49 | 49 |
#' clustering of cells, and \link{celda_CG} for simultaneous clustering of |
50 |
-#' features and cells. \link{subsetCeldaList} can subset the \link{celdaList} |
|
50 |
+#' features and cells. \link{subsetCeldaList} can subset the \code{celdaList} |
|
51 | 51 |
#' object. \link{selectBestModel} can get the best model for each combination |
52 | 52 |
#' of parameters. |
53 | 53 |
#' @import foreach |
... | ... |
@@ -606,6 +606,7 @@ setMethod("selectBestModel", signature(x = "SingleCellExperiment"), |
606 | 606 |
) |
607 | 607 |
|
608 | 608 |
|
609 |
+#' @rdname selectBestModel |
|
609 | 610 |
#' @examples |
610 | 611 |
#' data(celdaCGGridSearchRes) |
611 | 612 |
#' ## Returns same result as running celdaGridSearch with "bestOnly = TRUE" |
... | ... |
@@ -33,7 +33,7 @@ |
33 | 33 |
#' @param seed Integer. Passed to \link[withr]{with_seed}. For reproducibility, |
34 | 34 |
#' a default value of 12345 is used. Seed values |
35 | 35 |
#' \code{seq(seed, (seed + nchains - 1))} will be supplied to each chain in |
36 |
-#' \code{nchains} If NULL, no calls to |
|
36 |
+#' \code{nchains}. If NULL, no calls to |
|
37 | 37 |
#' \link[withr]{with_seed} are made. |
38 | 38 |
#' @param perplexity Logical. Whether to calculate perplexity for each model. |
39 | 39 |
#' If FALSE, then perplexity can be calculated later with |
... | ... |
@@ -37,7 +37,7 @@ |
37 | 37 |
#' \link[withr]{with_seed} are made. |
38 | 38 |
#' @param perplexity Logical. Whether to calculate perplexity for each model. |
39 | 39 |
#' If FALSE, then perplexity can be calculated later with |
40 |
-#' `resamplePerplexity()`. Default TRUE. |
|
40 |
+#' \link{resamplePerplexity}. Default TRUE. |
|
41 | 41 |
#' @param verbose Logical. Whether to print log messages during celda chain |
42 | 42 |
#' execution. Default TRUE. |
43 | 43 |
#' @param logfilePrefix Character. Prefix for log files from worker threads |
... | ... |
@@ -541,50 +541,97 @@ setMethod("subsetCeldaList", |
541 | 541 |
|
542 | 542 |
#' @title Select best chain within each combination of parameters |
543 | 543 |
#' @description Select the chain with the best log likelihood for each |
544 |
-#' combination of tested parameters from a `celdaList` object gererated by |
|
545 |
-#' `celdaGridSearch()`. |
|
546 |
-#' @param celdaList Object of class `celdaList`. An object containing celda |
|
547 |
-#' models returned from `celdaGridSearch()`. |
|
548 |
-#' @param asList `TRUE` or `FALSE`. Whether to return the best model as a |
|
549 |
-#' `celdaList` object or not. If `FALSE`, return the best model as a |
|
550 |
-#' corresponding `celda_C`, `celda_G` or `celda_CG` object. |
|
551 |
-#' @return A new `celdaList` object containing one model with the best log |
|
552 |
-#' likelihood for each set of parameters. If only one set of parameters is in |
|
553 |
-#' the `celdaList`, the best model will be returned directly instead of a |
|
554 |
-#' `celdaList` object. |
|
555 |
-#' @seealso `celdaGridSearch()` can run Celda with multiple parameters and |
|
556 |
-#' chains in parallel. `subsetCeldaList()` can subset the `celdaList` object. |
|
544 |
+#' combination of tested parameters from a \code{SCE} object gererated by |
|
545 |
+#' \link{celdaGridSearch} or from a \code{celdaList} object. |
|
546 |
+#' @param x Object of class \linkS4class{SingleCellExperiment} or |
|
547 |
+#' \code{celdaList}. An object containing celda |
|
548 |
+#' models returned from \link{celdaGridSearch}. |
|
549 |
+#' @param asList \code{TRUE} or \code{FALSE}. Whether to return the |
|
550 |
+#' best model as a |
|
551 |
+#' \code{celdaList} object or not. If \code{FALSE}, return the best model as a |
|
552 |
+#' corresponding celda model object. |
|
553 |
+#' @param useAssay A string specifying which \code{assay} |
|
554 |
+#' slot to use if \code{x} is a |
|
555 |
+#' \linkS4class{SingleCellExperiment} object. Default "counts". |
|
556 |
+#' @return One of |
|
557 |
+#' \itemize{ |
|
558 |
+#' \item A new \linkS4class{SingleCellExperiment} object containing |
|
559 |
+#' one model with the best log-likelihood for each set of parameters in |
|
560 |
+#' \code{metadata(x)}. If there is only one set of parameters, |
|
561 |
+#' a new \linkS4class{SingleCellExperiment} object |
|
562 |
+#' with the matching model stored in the |
|
563 |
+#' \link[S4Vectors]{metadata} |
|
564 |
+#' \code{"celda_parameters"} slot will be returned. Otherwise, a new |
|
565 |
+#' \linkS4class{SingleCellExperiment} object with the subset models stored |
|
566 |
+#' in the \link[S4Vectors]{metadata} |
|
567 |
+#' \code{"celda_grid_search"} slot will be returned. |
|
568 |
+#' \item A new \code{celdaList} object containing one model with the best |
|
569 |
+#' log-likelihood for each set of parameters. If only one set of parameters |
|
570 |
+#' is in the \code{celdaList}, the best model will be returned directly |
|
571 |
+#' instead of a \code{celdaList} object.} |
|
572 |
+#' @seealso \link{celdaGridSearch} \link{subsetCeldaList} |
|
573 |
+#' @export |
|
574 |
+setGeneric("selectBestModel", function(x, ...) { |
|
575 |
+ standardGeneric("selectBestModel")}) |
|
576 |
+ |
|
577 |
+ |
|
578 |
+#' @rdname selectBestModel |
|
579 |
+#' @examples |
|
580 |
+#' data(sceCeldaCGGridSearch) |
|
581 |
+#' ## Returns same result as running celdaGridSearch with "bestOnly = TRUE" |
|
582 |
+#' sce <- selectBestModel(sceCeldaCGGridSearch) |
|
583 |
+#' @importFrom data.table as.data.table |
|
584 |
+#' @export |
|
585 |
+setMethod("selectBestModel", signature(x = "SingleCellExperiment"), |
|
586 |
+ function(x, asList = FALSE, useAssay = "counts") { |
|
587 |
+ logLikelihood <- NULL |
|
588 |
+ group <- setdiff(colnames(runParams(x)), |
|
589 |
+ c("index", "chain", "logLikelihood", "mean_perplexity", "seed")) |
|
590 |
+ dt <- data.table::as.data.table(runParams(celdaList)) |
|
591 |
+ newRunParams <- as.data.frame(dt[, .SD[which.max(logLikelihood)], |
|
592 |
+ by = group]) |
|
593 |
+ newRunParams <- newRunParams[, colnames(runParams(celdaList))] |
|
594 |
+ |
|
595 |
+ ix <- match(newRunParams$index, runParams(celdaList)$index) |
|
596 |
+ if (nrow(newRunParams) == 1 & !asList) { |
|
597 |
+ x <- celdatosce(resList(x)[[ix]], |
|
598 |
+ SummarizedExperiment::assay(x, i = useAssay)) |
|
599 |
+ } else { |
|
600 |
+ x@metadata$celda_grid_search@runParams <- |
|
601 |
+ as.data.frame(newRunParams) |
|
602 |
+ x@metadata$celda_grid_search@resList <- resList(x)[ix] |
|
603 |
+ } |
|
604 |
+ return(x) |
|
605 |
+ } |
|
606 |
+) |
|
607 |
+ |
|
608 |
+ |
|
557 | 609 |
#' @examples |
558 | 610 |
#' data(celdaCGGridSearchRes) |
559 | 611 |
#' ## Returns same result as running celdaGridSearch with "bestOnly = TRUE" |
560 | 612 |
#' cgsBest <- selectBestModel(celdaCGGridSearchRes) |
561 | 613 |
#' @importFrom data.table as.data.table |
562 | 614 |
#' @export |
563 |
-selectBestModel <- function(celdaList, asList = FALSE) { |
|
564 |
- if (!methods::is(celdaList, "celdaList")) { |
|
565 |
- stop("celdaList parameter was not of class celdaList.") |
|
566 |
- } |
|
615 |
+setMethod("selectBestModel", signature(x = "celdaList"), |
|
616 |
+ function(x, asList = FALSE) { |
|
617 |
+ logLikelihood <- NULL |
|
618 |
+ group <- setdiff(colnames(runParams(x)), |
|
619 |
+ c("index", "chain", "logLikelihood", "mean_perplexity", "seed")) |
|
620 |
+ dt <- data.table::as.data.table(runParams(x)) |
|
621 |
+ newRunParams <- as.data.frame(dt[, .SD[which.max(logLikelihood)], |
|
622 |
+ by = group]) |
|
623 |
+ newRunParams <- newRunParams[, colnames(runParams(x))] |
|
567 | 624 |
|
568 |
- logLikelihood <- NULL |
|
569 |
- group <- setdiff( |
|
570 |
- colnames(runParams(celdaList)), |
|
571 |
- c("index", "chain", "logLikelihood", "mean_perplexity", "seed") |
|
572 |
- ) |
|
573 |
- dt <- data.table::as.data.table(runParams(celdaList)) |
|
574 |
- newRunParams <- as.data.frame(dt[, .SD[which.max(logLikelihood)], |
|
575 |
- by = group |
|
576 |
- ]) |
|
577 |
- newRunParams <- newRunParams[, colnames(runParams(celdaList))] |
|
578 |
- |
|
579 |
- ix <- match(newRunParams$index, runParams(celdaList)$index) |
|
580 |
- if (nrow(newRunParams) == 1 & !asList) { |
|
581 |
- return(resList(celdaList)[[ix]]) |
|
582 |
- } else { |
|
583 |
- celdaList@runParams <- as.data.frame(newRunParams) |
|
584 |
- celdaList@resList <- resList(celdaList)[ix] |
|
585 |
- return(celdaList) |
|
586 |
- } |
|
587 |
-} |
|
625 |
+ ix <- match(newRunParams$index, runParams(x)$index) |
|
626 |
+ if (nrow(newRunParams) == 1 & !asList) { |
|
627 |
+ return(resList(x)[[ix]]) |
|
628 |
+ } else { |
|
629 |
+ x@runParams <- as.data.frame(newRunParams) |
|
630 |
+ x@resList <- resList(x)[ix] |
|
631 |
+ return(x) |
|
632 |
+ } |
|
633 |
+ } |
|
634 |
+) |
|
588 | 635 |
|
589 | 636 |
|
590 |