A collection of utility functions used within the anglemania package for calculating statistics based on the results created during the anglemania function.

get_dstat(corr_matrix)

big_mat_list_mean(matrix_list, weights, verbose = TRUE)

get_list_stats(matrix_list, weights, verbose = TRUE)

Arguments

corr_matrix

An FBM object.

matrix_list

A list of bigstatsr::FBM objects.

weights

A numeric vector of weights for each dataset or batch.

Value

A list with statistical measures including mean, sd, var, min, and max.

A new bigstatsr::FBM object containing the mean values.

A list containing three matrices: mean_zscore, sds_zscore, and sn_zscore which are later used to filter gene pairs based on the absolute mean z-score and signal-to-noise ratio of the angles.

Functions

  • get_dstat(): Compute mean, standard deviation, variance, min, and max of a correlation matrix stored as an FBM.

  • big_mat_list_mean(): Calculates the element-wise mean from a list of bigstatsr::FBM objects.

  • get_list_stats(): Calculate mean, standard deviation, and SNR across a list of FBMs.

See also

Examples

s_mat <- Matrix::rsparsematrix(nrow = 10, ncol = 5, density = 0.3)
fbm_mat <- sparse_to_fbm(s_mat)
result <- get_dstat(fbm_mat)
str(result)
#> List of 5
#>  $ mean: num [1:5] 0.352 0.21 0.052 -0.043 0.233
#>  $ var : num [1:5] 0.439 0.156 0.094 0.128 0.303
#>  $ min : num [1:5] 0 0 -0.54 -0.95 -0.58
#>  $ max : num [1:5] 2 1.2 0.63 0.52 1.2
#>  $ sd  : num [1:5] 0.662 0.395 0.307 0.358 0.55
result
#> $mean
#> [1]  0.352  0.210  0.052 -0.043  0.233
#> 
#> $var
#> [1] 0.4388844 0.1560000 0.0940400 0.1282678 0.3029789
#> 
#> $min
#> [1]  0.00  0.00 -0.54 -0.95 -0.58
#> 
#> $max
#> [1] 2.00 1.20 0.63 0.52 1.20
#> 
#> $sd
#> [1] 0.6624835 0.3949684 0.3066594 0.3581449 0.5504352
#> 
# Create FBMs
mat1 <- matrix(1:9, nrow = 3)
mat2 <- matrix(1:9, nrow = 3)

fbm1 <- bigstatsr::FBM(nrow = nrow(mat1), ncol = ncol(mat1), init = mat1)
fbm2 <- bigstatsr::FBM(nrow = nrow(mat2), ncol = ncol(mat2), init = mat2)

# Create weights
weights <- c(batch1 = 0.5, batch2 = 0.5)

# Create the list of FBMs
fbm_list <- list(batch1 = fbm1, batch2 = fbm2)

big_mat_list_mean(fbm_list, weights)
#> A Filebacked Big Matrix of type 'double' with 3 rows and 3 columns.
library(SingleCellExperiment)
library(S4Vectors)
sce <- sce_example()
sce <- anglemania(sce, batch_key = "batch")
#> No dataset_key specified.
#> Assuming that all samples belong to the same dataset and are separated by batch_key: batch
#> Preparing input...
#> Filtering each batch to at least 1 cells per gene...
#> 
  |                                                  | 0 % elapsed=00s   
  |==================================================| 100% elapsed=00s, remaining~00s
#> Using the intersection of filtered genes from all batches...
#> Number of genes in intersected set: 300
#> Extracting count matrices...
#> 
  |                                                  | 0 % elapsed=00s   
  |==================================================| 100% elapsed=00s, remaining~00s
#> Computing angles and transforming to z-scores...
#> 
  |                                                  | 0 % elapsed=00s   
#> Creating directory "/tmp/RtmpwM6D04/file1f8e5e0ddf01" which didn't exist..
#> 
  |=========================                         | 50% elapsed=00s, remaining~00s
#> Creating directory "/tmp/RtmpwM6D04/file1f8e5cd858ec" which didn't exist..
#> 
  |==================================================| 100% elapsed=00s, remaining~00s
#> Computing statistics...
#> Weighting matrix_list...
#> Calculating mean...
#> Calculating sds...
#> Pre-filtering features...
#> Extracting filtered features...
#> Selected 25 genes for integration.
matrix_list <- metadata(sce)$anglemania$matrix_list
weights <- setNames(
  S4Vectors::metadata(sce)$anglemania$weights$weight,
  S4Vectors::metadata(sce)$anglemania$weights$anglemania_batch
)
list_stats <- get_list_stats(matrix_list, weights)
#> Weighting matrix_list...
#> Calculating mean...
#> Calculating sds...
names(list_stats)
#> [1] "mean_zscore" "sds_zscore"  "sn_zscore"  
list_stats$mean_zscore[1:5, 1:5]
#>            [,1]       [,2]       [,3]       [,4]       [,5]
#> [1,]  0.0000000 -1.4773877 -0.6690115  0.8095740  0.2119111
#> [2,] -1.4329751  0.0000000  0.8094372  1.7959187  0.8749175
#> [3,] -0.7097561  0.8090174  0.0000000 -0.9000276 -0.1421519
#> [4,]  0.8102581  1.7916458 -0.8103351  0.0000000 -0.2938629
#> [5,]  0.2622850  0.8796571 -0.2190412 -0.3142955  0.0000000