Skip to content

Commit

Permalink
first try at replacing calculatePCA with BiocSingular::runPCA
Browse files Browse the repository at this point in the history
  • Loading branch information
shazanfar committed Jul 16, 2024
1 parent dbd6ccd commit 423567a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 17 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ biocViews: SingleCell, DimensionReduction, Software
Depends: R (>= 4.4.0),
Imports:
igraph,
scater,
slam,
BiocNeighbors,
Matrix,
Expand All @@ -34,7 +33,9 @@ Imports:
SummarizedExperiment,
methods,
MatrixGenerics,
BiocGenerics
BiocGenerics,
BiocSingular,
BiocParallel
Suggests:
knitr,
UpSetR,
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export(mosaicDataTopology)
export(mosaicDataUpSet)
export(reWeightEmbedding)
export(stabMap)
importFrom(BiocParallel,SerialParam)
importFrom(BiocParallel,bpstart)
importFrom(BiocParallel,bpstop)
importFrom(MatrixGenerics,rowMaxs)
importFrom(MatrixGenerics,rowWeightedVars)
importFrom(SummarizedExperiment,assay)
60 changes: 46 additions & 14 deletions R/stabMap.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
#' the SummarizedExperiment objects in assay_list or a named list of assay
#' names, where the names corrispond to the names SE objects in assay_list
#' (default "logcounts")
#' @param BPPARAM a BiocParallelParam object specifying how parallelisation
#' should be performed
#' @param verbose Logical whether console output is provided (default TRUE)
#'
#' @return matrix containing common embedding with rows corresponding to cells,
Expand Down Expand Up @@ -78,6 +80,7 @@
#' head(out)
#'
#' @importFrom MatrixGenerics rowMaxs rowWeightedVars
#' @importFrom BiocParallel SerialParam bpstart bpstop
#' @export
stabMap <- function(assay_list,
labels_list = NULL,
Expand All @@ -94,7 +97,9 @@ stabMap <- function(assay_list,
scale.center = TRUE,
scale.scale = TRUE,
SE_assay_names = "logcounts",
verbose = TRUE) {
BPPARAM = SerialParam(),
verbose = TRUE
) {
# check various things and error if not:

if (is.null(reference_list)) {
Expand Down Expand Up @@ -248,16 +253,30 @@ stabMap <- function(assay_list,
reference_scores <- reference_scores_list[[reference_dataset]]
restrictFeatures <- FALSE
} else {
reference_scores_raw <- sm(
scater::calculatePCA(
assay_list[[reference_dataset]][
reference_features_list[[reference_dataset]],
],
ncomponents = nPC,
scale = FALSE
)
# reference_scores_raw <- sm(
# scater::calculatePCA(
# assay_list[[reference_dataset]][
# reference_features_list[[reference_dataset]],
# ],
# ncomponents = nPC,
# scale = FALSE
# )
# )
# start replacement
reference_scores_obj = BiocSingular::runPCA(
t(assay_list[[reference_dataset]][
reference_features_list[[reference_dataset]],
]),
rank = nPC,
scale = FALSE,
BPPARAM = BPPARAM
)

reference_scores_raw <- reference_scores_obj$x
attr(reference_scores_raw, "rotation") <- reference_scores_obj$rotation
# end replacement


attr(reference_scores_raw, "rotation") <- list(
attr(reference_scores_raw, "rotation"),
stats::setNames(
Expand Down Expand Up @@ -411,11 +430,24 @@ stabMap <- function(assay_list,
ncomponentsSubset[[reference_dataset]], length(features_current)
)

dimred_current <- sm(scater::calculatePCA(
assay_list[[path_current[1]]][features_current, ],
ncomponents = nPC_sub,
scale = FALSE
))
# dimred_current <- sm(scater::calculatePCA(
# assay_list[[path_current[1]]][features_current, ],
# ncomponents = nPC_sub,
# scale = FALSE
# ))
# start replacement
dimred_current_obj = BiocSingular::runPCA(
t(assay_list[[path_current[1]]][features_current, ]),
rank = nPC_sub,
scale = FALSE,
BPPARAM = BPPARAM
)

dimred_current <- dimred_current_obj$x
attr(dimred_current, "rotation") <- dimred_current_obj$rotation
# end replacement


attr(dimred_current, "rotation") <- list(
attr(dimred_current, "rotation"),
rowMeans(assay_list[[path_current[1]]][features_current, ])
Expand Down
4 changes: 4 additions & 0 deletions man/stabMap.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion vignettes/stabMap_PBMC_Multiome.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ knitr::opts_chunk$set(
```{r setup, message=FALSE, warning=FALSE}
library(StabMap)
library(scran)
library(scater)
library(magrittr)
library(SingleCellMultiModal)
Expand Down

0 comments on commit 423567a

Please sign in to comment.