
Run PCA for single cell
calculate_pca_sc.RdThis function will run PCA on the detected highly variable genes. You can use randomised SVD for speed and there is an option for sparse SVD for very large data sets to avoid memory pressure.
Usage
calculate_pca_sc(
object,
no_pcs,
pca_params = params_sc_pca(),
sparse_svd = FALSE,
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
Boolean. Shall sparse solvers be used that do not do scaling. If set to yes, in the case of
random_svd = FALSE, Lanczos iterations are used to solve the sparse SVD. Withrandom_svd = TRUE, the sparse initial matrix is multiplied with the random matrix, yielding a much smaller dense matrix that does not increase the memory pressure massively. 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 if
randomised_svd = TRUE.- 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. 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)