Normalization helper functions
normalize.RdApply standard normalizations to a (features x cells) counts matrix.
Usage
# S3 method for class 'Matrix'
normalize_log(x, scale_factor = 10000, threads = 1L)
# S3 method for class 'matrix'
normalize_log(x, scale_factor = 10000, threads = 1L)
# S3 method for class 'IterableMatrix'
normalize_log(x, scale_factor = 10000, threads = 1L)
# S3 method for class 'MarchRDataset'
normalize_log(
x,
assay_name = character(0),
output_assay_name = character(0),
scale_factor = 10000,
return_matrix = FALSE,
threads = 1L
)
# S3 method for class 'MarchRProject'
normalize_log(
x,
assay_name = character(0),
dataset_names = character(0),
output_assay_name = character(0),
scale_factor = 10000,
return_matrix = FALSE,
threads = 1L
)
# S3 method for class 'Matrix'
normalize_tfidf(
x,
feature_means = NULL,
read_depth = NULL,
scale_factor = 10000,
lsi_method = 2,
threads = 1L
)
# S3 method for class 'matrix'
normalize_tfidf(
x,
feature_means = NULL,
read_depth = NULL,
scale_factor = 10000,
lsi_method = 2,
threads = 1L
)
# S3 method for class 'IterableMatrix'
normalize_tfidf(
x,
feature_means = NULL,
read_depth = NULL,
scale_factor = 10000,
lsi_method = 2,
threads = 1L
)
# S3 method for class 'MarchRDataset'
normalize_tfidf(
x,
assay_name = character(0),
output_assay_name = character(0),
feature_means = NULL,
scale_factor = 10000,
lsi_method = 2,
return_matrix = FALSE,
threads = 1L
)
# S3 method for class 'MarchRProject'
normalize_tfidf(
x,
assay_name = character(0),
dataset_names = character(0),
output_assay_name = character(0),
feature_means = NULL,
scale_factor = 10000,
lsi_method = 2,
return_matrix = FALSE,
threads = 1L
)Arguments
- x
(Matrix-like;
MarchR::MarchRDataset, orMarchR::MarchRProject) Input to normalize.If Matrix-like, Counts matrix with dimensions
(features x cells).If
MarchRDatasetorMarchRProject, objects with a matrix assay.- scale_factor
(numeric) Scaling factor to multiply matrix by during normalization (see formulas below).
- threads
(integer) Threads for the
matrix_stats()pass. Default1L; not read fromset_threads().- assay_name
(character) Name of the assay to apply the function to. If not provided, defaults to the first assay in the dataset.
- output_assay_name
(character) Name of the assay to store the results in. If not provided, overwrites the original assay with the results.
- return_matrix
(logical) If
TRUE, returns the resulting matrices instead of modifying the dataset/project- dataset_names
(character) Names of the datasets to apply the function to. If not provided, defaults to all datasets in the project.
Value
Normalized matrix with the same dimensions and type as the input matrix x. If the input matrix x is missing, returns
a copy of the original function with the other arguments pre-defined (see Details).
Normalization equations
Let \(x_{ij}\) be the value of x in row (feature) \(i\) and column (cell) \(j\).
Then the corresponding normalized value of the output \(\tilde{x}_{ij}\) is calculated as:
normalize_log: \(\tilde{x}_{ij} = \log(\frac{x_{ij} \cdot \text{scaleFactor}}{\text{colSum}_j} + 1)\)
If return_matrix = TRUE, returns a named list of normalized matrices, with names corresponding to the datasets.
If return_matrix = FALSE, returns the modified MarchRProject object with normalized assays.
Details
Passing to normalize_method parameters with non-default arguments
Other BPCells functions like select_features_variance() take a normalize_method argument, which can be set to normalize_log to
perform log-normalization with default arguments prior to feature selection. If non-default arguments are required, it is possible to
omit the x argument but set custom parameters as desired. For instance normalize_log(scale_factor=1e5) is roughly equivalent to
function(x) normalize_log(x, scale_factor=1e5). This works because when the x argument is missing, the return value is a
copy of the original function with all other arguments pre-defined (an example of partial function application in computer science terms).
Related functions from other packages
normalize_log: Corresponds toSeurat::NormalizeData()with its default "LogNormalize" method.
Examples
set.seed(12345)
base_r_mat <- matrix(rpois(4 * 5, lambda = 1), nrow = 4, ncol = 5)
base_r_mat
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 1 1 1 1 1
#> [2,] 2 0 4 0 1
#> [3,] 2 0 0 1 0
#> [4,] 2 1 0 1 3
mat <- as(base_r_mat, "IterableMatrix")
#######################################################################
## normalize_log() examples
#######################################################################
normalize_log(mat)
#> 4 x 5 IterableMatrix object with class TransformLog1p
#>
#> Row names: unknown names
#> Col names: unknown names
#>
#> Data type: double
#> Storage order: column major
#>
#> Queued Operations:
#> 1. Load dgCMatrix from memory
#> 2. Scale by 1e+04
#> 3. Scale columns by 0.143, 0.5 ... 0.2
#> 4. Transform log1p
## normalizations work on dgCMatrix and base matrix objects also
normalize_log(as(mat, "dgCMatrix"))
#> 4 x 5 sparse Matrix of class "dgCMatrix"
#>
#> [1,] 7.265130 8.517393 7.601402 8.112028 7.601402
#> [2,] 7.957927 . 8.987322 . 7.601402
#> [3,] 7.957927 . . 8.112028 .
#> [4,] 7.957927 8.517393 . 8.112028 8.699681
normalize_log(base_r_mat)
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 7.265130 8.517393 7.601402 8.112028 7.601402
#> [2,] 7.957927 0.000000 8.987322 0.000000 7.601402
#> [3,] 7.957927 0.000000 0.000000 8.112028 0.000000
#> [4,] 7.957927 8.517393 0.000000 8.112028 8.699681
## normalization functions can also be called with partial arguments
partial_log <- normalize_log(scale_factor = 1e3)
partial_log
#> normalize_log(scale_factor = 1000, ...)
partial_log(mat)
#> 4 x 5 IterableMatrix object with class TransformLog1p
#>
#> Row names: unknown names
#> Col names: unknown names
#>
#> Data type: double
#> Storage order: column major
#>
#> Queued Operations:
#> 1. Load dgCMatrix from memory
#> 2. Scale by 1e+03
#> 3. Scale columns by 0.143, 0.5 ... 0.2
#> 4. Transform log1p