Feature selection functions
feature_selection.RdApply a feature selection method to a non-normalized (features x cells) matrix. We recommend using counts matrices as input and to
apply normalizations prior to feature selection via the normalize argument (if available).
Instead of directly subsetting the input matrix,
an output dataframe is returned, indicating which features are highly variable, and the scoring of each feature.
Usage
# Default S3 method
select_features_variance(
mat,
num_feats = 25000L,
normalize_method = NULL,
threads = 1L
)
# Default S3 method
select_features_dispersion(
mat,
num_feats = 0.05,
normalize_method = NULL,
threads = 1L
)
# Default S3 method
select_features_mean(
mat,
num_feats = 0.05,
normalize_method = NULL,
threads = 1L
)
# Default S3 method
select_features_binned_dispersion(
mat,
num_feats = 0.05,
n_bins = 20,
threads = 1L
)
select_features_top(
mat,
num_feats = 25000,
filter_quantile = 0.995,
n_total = NULL,
filter_top_features = NULL,
feature_means = NULL,
threads = 1L,
...
)Arguments
- mat
(IterableMatrix) Counts matrix with dimensions
(features x cells).- num_feats
(numeric) Number of features to mark as highly_variable. Integer values are used directly as the feature count. Values in
(0, 1)are interpreted as a fraction of features (e.g.0.05selects the top 5%). Default25000L(matchesselect_features_top()and ArchR'svarFeaturesfor iterative-LSI workflows on tile/peak matrices).- normalize_method
(function) Used to normalize the matrix prior to feature selection by calling
normalize_method(mat)if it is notNULL. For example, passnormalize_log()ornormalize_tfidf().- threads
(integer) Threads for the
matrix_stats()pass. Also overrides the threads argument innormalize_method. Default1L; not read fromset_threads().- filter_quantile
(numeric) Quantile threshold for removing universal-open outlier features. Features above this quantile by mean accessibility are excluded before selecting the top
num_feats. Default0.995removes the top 0.5%%, matching ArchR'sfilterQuantile.- n_total
(numeric or NULL) Feature-set size for the removal count. NULL uses the matrix row count. Default NULL.
- filter_top_features
(integer or NULL) Exact number of the most accessible features to remove before selecting
num_feats. When non-NULL, this takes precedence over thefilter_quantileandn_totalcalculation. Default NULL.- feature_means
(numeric or NULL) Optional precomputed row means for
select_features_top(). Named values are aligned torownames(mat); unnamed values must already be in matrix-row order. WhenNULL(default), the means are computed frommat.
Value
Return a dataframe with the following columns:
feature: Feature name.score: Scoring of the feature, depending on the method used.highly_variable: Logical vector of whether the feature is highly variable.
Each different feature selection method will have a different scoring method. Consider a matrix \(X\), where the row index \(i\) refers to each feature and the column index \(j\) refers to each cell. For each feature \(x_{i} \in X\), we define the following feature-selection scores:
select_features_variance: \(\mathrm{Score}(x_i) = \frac{1}{n - 1} \sum_{j=1}^{n} \bigl(x_{ij} - \bar{x}_i\bigr)^2\)
Details
Top-accessibility filtering is skipped when the number of nonzero features is at most 2.25 times the requested feature count. Zero-count features are never selected, and removal cannot exhaust the attainable requested feature set.
Functions
select_features_top(): Select features by total accessibility (rowMeans), optionally removing the most accessible universal-open outlier features first. Best for ATAC data (TileMatrix/PeakMatrix) where raw accessibility is a robust proxy for informativeness. Matches ArchR'sfirstSelection = "top".
Examples
## Prep data
set.seed(12345)
mat <- matrix(rpois(5*8, lambda=1), nrow=5, ncol=8)
rownames(mat) <- paste0("gene", seq_len(nrow(mat)))
mat
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
#> gene1 1 0 0 1 1 1 2 0
#> gene2 2 0 0 1 0 1 0 2
#> gene3 2 1 1 1 3 1 0 2
#> gene4 2 1 0 0 1 0 1 1
#> gene5 1 4 1 3 1 1 1 0
mat <- as(mat, "IterableMatrix")
#######################################################################
## select_features_variance() examples
#######################################################################
select_features_variance(
mat,
num_feats=2,
normalize_method=normalize_log
)
#> # A tibble: 5 × 3
#> feature score highly_variable
#> <chr> <dbl> <lgl>
#> 1 gene1 15.9 TRUE
#> 2 gene2 17.6 TRUE
#> 3 gene3 8.14 FALSE
#> 4 gene4 15.6 FALSE
#> 5 gene5 8.35 FALSE
## Because of how the BPCells `normalize` functions behave when the matrix
## argument is missing, we can also customize the normalization parameters
## using partial arguments:
variable_features <- select_features_variance(
mat,
num_feats=2,
normalize_method=normalize_log(scale_factor=20)
)
## One can then filter to only variable features using the subset operator:
mat[variable_features$feature[variable_features$highly_variable],]
#> 2 x 8 IterableMatrix object with class MatrixSubset
#>
#> Row names: gene1, gene2
#> Col names: unknown names
#>
#> Data type: double
#> Storage order: column major
#>
#> Queued Operations:
#> 1. Load dgCMatrix from memory
#> 2. Select rows: 1, 2 and cols: all