scran_blocks
Blocking utilities for libscran
Loading...
Searching...
No Matches
Blocking utilities for libscran

Unit tests Documentation Codecov

Overview

This repository contains utilities for blocked analyses for the other libscran repositories. Any uninteresting factor of variation (usually across the cells) can be used as a blocking factor, e.g., experimental batches, sample/patient of origin. In the presence of blocks, our general strategy is to perform the analysis within each block before combining our conclusions across blocks. This ensures that our results are not affected by the uninteresting differences between blocks.

Averaging vectors

The scran_blocks::average_vectors() function will compute the element-wise averages of any number of equi-length arrays. This is typically used to average statistics across blocks, where each array contains the statistics for a single block over all genes.

std::vector<double> stat1 { 1, 2, 3, 4 };
std::vector<double> stat2 { 5, 6, 7, 8 };
// Contains { 3, 4, 5, 6 }.
stat1.size(),
{ stat1.data(), stat2.data() }
/* skip_nan = */ false
);
void average_vectors(size_t n, std::vector< Stat_ * > in, Output_ *out, bool skip_nan)
Definition average_vectors.hpp:121
Blocking utilities for libscran.

If NaNs are present, they can be ignored:

#include <limits>
auto nan = std::numeric_limits<double>::quiet_NaN();
std::vector<double> stat1n { 1, nan, 3, nan };
std::vector<double> stat2n { 5, 6, nan, nan };
// Contains { 3, 6, 3, nan }.
stat1n.size(),
{ stat1n.data(), stat2n.data() }
/* skip_nan = */ true
);

We also support per-vector weights:

std::vector<double> stat1w { 1, 2, 3, 4 };
std::vector<double> stat2w { 5, 6, 7, 8 };
std::vector<double> weights { 1, 9 };
// Contains { 4.6, 5.6, 6.6, 7.6 }.
stat1w.size(),
{ stat1w.data(), stat2w.data() }
weights.data(),
/* skip_nan = */ false
);
void average_vectors_weighted(size_t n, std::vector< Stat_ * > in, const Weight_ *w, Output_ *out, bool skip_nan)
Definition average_vectors.hpp:163

See the reference documentation for more details.

Weighting blocks

When combining statistics across blocks, it may be desirable to weight each block by its size, favoring larger blocks that can emit more stable statistics. This is done using the scran_blocks::compute_weights() function that calculates a weight for each block based on its size.

std::vector<size_t> block_sizes { 10, 100, 1000 };
block_sizes,
/* policy = */ scran_blocks::WeightPolicy::VARIABLE,
/* variable = */ { 0, 200 }
);
void compute_weights(size_t num_blocks, const Size_ *sizes, WeightPolicy policy, const VariableWeightParameters &variable, Weight_ *weights)
Definition block_weights.hpp:84

The above code chunk uses a variable block weight that increases linearly with block size from 0 to 200, after which it is capped at 1. This VARIABLE policy penalizes very small blocks to ensure that their unstable statistics do not overly influence the average. Blocks are equally weighted once they are "large enough", ensuring that the average is not dominated by a single very large block.

Users can also change the policy to NONE, where weights are equal to the block size; or EQUAL, where all blocks are equally weighted regardless of size (assuming they are non-empty). In such cases, the variable argument is ignored. Check out the reference documentation for more details.

Building projects

CMake with <tt>FetchContent</tt>

If you're using CMake, you just need to add something like this to your CMakeLists.txt:

include(FetchContent)
FetchContent_Declare(
scran_blocks
GIT_REPOSITORY https://github.com/libscran/scran_blocks
GIT_TAG master # or any version of interest
)
FetchContent_MakeAvailable(scran_blocks)

Then you can link to scran_blocks to make the headers available during compilation:

# For executables:
target_link_libraries(myexe libscran::scran_blocks)
# For libaries
target_link_libraries(mylib INTERFACE libscran::scran_blocks)

CMake with <tt>find_package()</tt>

find_package(libscran_scran_blocks CONFIG REQUIRED)
target_link_libraries(mylib INTERFACE libscran::scran_blocks)

To install the library, use:

mkdir build && cd build
cmake .. -DSCRAN_BLOCKS_TESTS=OFF
cmake --build . --target install

Manual

If you're not using CMake, the simple approach is to just copy the files in include/ - either directly or with Git submodules - and include their path during compilation with, e.g., GCC's -I.