
Run PCA for single cell
calculate_pca_sc.RdThis function will run PCA on the detected highly variable genes. The
solver is set via svd_solver in params_sc_pca(); the default "auto"
runs Lanczos on the sparse path and the gene x gene cross-product on the
dense one, the fastest exact option on each.
Usage
calculate_pca_sc(
object,
no_pcs,
pca_params = params_sc_pca(),
sparse_svd = NULL,
hvg = NULL,
seed = 42L,
residuals = FALSE,
.verbose = TRUE
)Arguments
- object
SingleCells,MetaCells(or potentially other) class.- no_pcs
Integer. Number of PCs to calculate.
- pca_params
Named list. Controls the parameters to be used for the PCA calculation which is single cell-specific, see
params_sc_pca()- sparse_svd
Optional boolean. Solve on the sparse data with implicit centring and scaling instead of materialising the dense scaled matrix.
NULLpicks the sparse path, which is faster and lighter on memory for every solver, or the dense one forresiduals = TRUE.FALSEuses the dense path, but only below 500,000 cells. Not used forMetaCells.- hvg
Optional integer. If you want to provide your own HVG genes. Otherwise, the function will default to what is found in
get_hvg(). Please provide 1-indexed genes here! If you provide these, the internal HVG will be overwritten.- seed
Integer. Controls reproducibility. Only relevant for
svd_solver = "randomised"and the Lanczos start vector.- residuals
Boolean. Run the PCA on the Pearson residuals of a model fitted with
fit_residuals_sc()instead of the stored normalised layer. Needsparams_sc_pca(normalise_variance = FALSE, clr = FALSE), since the residuals already carry the signal as variance, and does not supportsparse_svd: a residual column is dense even where the counts are not. On this dense path the defaultsvd_solver = "auto"pays for a full gene x gene cross-product and is slower thansvd_solver = "randomised", so pick the latter inparams_sc_pca()when speed matters. Not supported forMetaCells.- .verbose
Boolean or integer. Controls verbosity and returns run times.
FALSE-> quiet,TRUEor1L-> normal verbosity,2L-> detailed verbosity.
Value
The function will add the PCA factors, loadings and singular values to the object cache in memory.
Examples
# PCA on the highly variable genes
sc <- demo_single_cells(prepped = FALSE)
sc <- find_hvg_sc(sc, hvg_no = 30L, .verbose = FALSE)
sc <- calculate_pca_sc(sc, no_pcs = 10L, .verbose = FALSE)
dim(get_pca_factors(sc))
#> [1] 500 10
unlink(sc@dir_data, recursive = TRUE, force = TRUE)