Principal Component Analysis (PCA) with log normalization and variable gene selection
PCA.RdPerforms PCA on single-cell RNA-seq data with Seurat-style log normalization, variable gene selection, and z-score normalization. This function implements the complete workflow: normalization, feature selection, and dimensionality reduction.
Usage
# Default S3 method
PCA(
x,
feature_selection_method = select_features_variance,
scale_factor = 10000,
n_features = 1000L,
n_dimensions = 50L,
threads = get_threads(),
verbose = getOption("MarchR.verbose", FALSE)
)
# S3 method for class 'MarchRProject'
PCA(
x,
assay_name = character(0),
dataset_names = character(0),
output_assay_name = "PCA",
dim_reduction_name = "PCA",
feature_selection_method = select_features_variance,
scale_factor = 10000,
n_features = 1000L,
n_dimensions = 50L,
overwrite = FALSE,
threads = get_threads(),
verbose = getOption("MarchR.verbose", FALSE)
)
# S3 method for class 'PCA'
project(x, mat, threads = NULL, ...)Arguments
- x
(IterableMatrix, MarchRDataset, or MarchRProject) Input matrix or MarchR object.
- feature_selection_method
(function) Function for selecting variable features. Default is
select_features_variance.- scale_factor
(numeric) Scaling factor for log normalization (equivalent to target.sum in Seurat). Default is 10000.
- n_features
(integer) Number of variable features to select. Default is 2000. Will be automatically limited to the number of available features in the matrix.
- n_dimensions
(integer) Number of principal components to compute. Default is 50. Will be automatically limited based on matrix dimensions after feature selection.
- threads
(integer) Maximum threads for normalization, default feature selection, and the SVD. Default
get_threads(). PCA reduces the thread count for these steps when BPCells would split a small matrix into chunks with too few entries. Custom feature selectors receive the requested thread count.project()runs single-threaded by default.- verbose
(logical) Print progress messages. Default
getOption("MarchR.verbose", FALSE).- assay_name
(character) For MarchRProject/MarchRDataset: name of the matrix to use.
- dataset_names
(character) For MarchRProject: names of datasets to include.
- output_assay_name
(character) Name for the output PCA embeddings matrix.
- dim_reduction_name
(character) Name for the stored DimReduction object.
- overwrite
(logical) Whether to overwrite existing results. Default is FALSE.
- mat
(IterableMatrix) For
project.PCA: New matrix to project into PCA space.- ...
Additional arguments passed to methods.
Value
For IterableMatrix: A list with cell_embeddings (matrix of PC embeddings) and
dim_reduction (PCA DimReduction object).
For MarchRDataset/MarchRProject: The input object with PCA embeddings added
as a new assay and DimReduction object stored.
For project.PCA: Matrix of PC embeddings for the new data.
Details
The PCA workflow includes:
Log normalization: counts per cell normalization followed by log1p transformation
Variable gene selection using the specified method
Z-score normalization of selected features
SVD computation to obtain principal components
Return PC embeddings scaled by singular values
See also
LSI for Latent Semantic Indexing
IterativeLSI for iterative LSI
select_features_variance for variable feature selection
Examples
# Basic PCA on a matrix
project <- create_example_project()
mat <- project$datasets$dataset_1$matrices$RNA
pca_result <- PCA(mat, n_features = 500, n_dimensions = 20)
# PCA on MarchR project
project_pca <- PCA(project,
dataset_names = "dataset_1",
assay_name = "RNA",
output_assay_name = "PCA",
n_features = 1000,
n_dimensions = 50)
# Project new data using fitted PCA model
new_mat <- project$datasets$dataset_1$matrices$RNA[, 1:10] # subset of cells
projected_embeddings <- project(pca_result$dim_reduction, new_mat)