1#ifndef SCRAN_LOG_NORM_COUNTS_H
2#define SCRAN_LOG_NORM_COUNTS_H
4#include "../utils/macros.hpp"
10#include "tatami/tatami.hpp"
13#include "SanitizeSizeFactors.hpp"
175 handle_non_finite = n;
199 choose_pseudo_count = c;
246 template<
class MAT,
class V>
247 std::shared_ptr<MAT>
run(std::shared_ptr<MAT> mat, V size_factors)
const {
248 return run_blocked(std::move(mat), std::move(size_factors),
static_cast<int*
>(NULL));
269 template<
class MAT,
class V,
typename B>
270 std::shared_ptr<MAT>
run_blocked(std::shared_ptr<MAT> mat, V size_factors,
const B* block)
const {
274 if (size_factors.size() != mat->ncol()) {
275 throw std::runtime_error(
"number of size factors and columns are not equal");
280 cresults = centerer.
run_blocked(size_factors.size(), size_factors.data(), block);
286 sanitizer.
set_handle_zero(handle_zeros ? SanitizeSizeFactors::HandlerAction::SANITIZE : SanitizeSizeFactors::HandlerAction::ERROR);
287 sanitizer.
set_handle_non_finite(handle_non_finite ? SanitizeSizeFactors::HandlerAction::SANITIZE : SanitizeSizeFactors::HandlerAction::ERROR);
288 sanitizer.
run(size_factors.size(), size_factors.data(), cresults);
290 double current_pseudo = pseudo_count;
291 if (choose_pseudo_count) {
292 current_pseudo = pseudo_chooser.
run(size_factors.size(), size_factors.data());
295 if (sparse_addition && current_pseudo != 1) {
296 for (
auto& d : size_factors) {
302 typedef typename MAT::value_type Value_;
303 auto div = tatami::make_DelayedUnaryIsometricOp(std::move(mat), tatami::make_DelayedDivideVectorHelper<true, 1, Value_>(std::move(size_factors)));
305 if (current_pseudo == 1) {
306 return tatami::make_DelayedUnaryIsometricOp(std::move(div), tatami::DelayedLog1pHelper<Value_>(2.0));
308 auto add = tatami::make_DelayedUnaryIsometricOp(std::move(div), tatami::make_DelayedAddScalarHelper<Value_>(current_pseudo));
309 return tatami::make_DelayedUnaryIsometricOp(std::move(add), tatami::DelayedLogHelper<Value_>(2.0));
325 std::shared_ptr<MAT>
run(std::shared_ptr<MAT> mat)
const {
326 auto size_factors = tatami::column_sums(mat.get(), nthreads);
327 return run_blocked(std::move(mat), std::move(size_factors),
static_cast<int*
>(NULL));
342 template<
class MAT,
typename B>
343 std::shared_ptr<MAT>
run_blocked(std::shared_ptr<MAT> mat,
const B* block)
const {
344 auto size_factors = tatami::column_sums(mat.get(), nthreads);
345 return run_blocked(mat, std::move(size_factors), block);
Center size factors prior to normalization.
Choose a suitable pseudo-count.
Center size factors prior to scaling normalization.
Definition CenterSizeFactors.hpp:27
BlockMode
Definition CenterSizeFactors.hpp:32
SizeFactorValidity run_blocked(size_t n, T *size_factors, const B *block) const
Definition CenterSizeFactors.hpp:157
CenterSizeFactors & set_block_mode(BlockMode b=Defaults::block_mode)
Definition CenterSizeFactors.hpp:69
Choose a pseudo-count for log-transformation.
Definition ChoosePseudoCount.hpp:35
ChoosePseudoCount & set_max_bias(double b=Defaults::max_bias)
Definition ChoosePseudoCount.hpp:80
ChoosePseudoCount & set_min_value(double v=Defaults::min_value)
Definition ChoosePseudoCount.hpp:91
ChoosePseudoCount & set_quantile(double q=Defaults::quantile)
Definition ChoosePseudoCount.hpp:70
double run(size_t n, const double *size_factors, double *buffer) const
Definition ChoosePseudoCount.hpp:122
Compute log-normalized expression values.
Definition LogNormCounts.hpp:33
LogNormCounts & set_sparse_addition(bool a=Defaults::sparse_addition)
Definition LogNormCounts.hpp:113
LogNormCounts & set_center(bool c=Defaults::center)
Definition LogNormCounts.hpp:131
std::shared_ptr< MAT > run_blocked(std::shared_ptr< MAT > mat, const B *block) const
Definition LogNormCounts.hpp:343
LogNormCounts & set_quantile(double q=ChoosePseudoCount::Defaults::quantile)
Definition LogNormCounts.hpp:218
LogNormCounts & set_block_mode(CenterSizeFactors::BlockMode b=CenterSizeFactors::Defaults::block_mode)
Definition LogNormCounts.hpp:141
std::shared_ptr< MAT > run(std::shared_ptr< MAT > mat) const
Definition LogNormCounts.hpp:325
LogNormCounts & set_handle_non_finite(bool n=Defaults::handle_non_finite)
Definition LogNormCounts.hpp:174
std::shared_ptr< MAT > run_blocked(std::shared_ptr< MAT > mat, V size_factors, const B *block) const
Definition LogNormCounts.hpp:270
LogNormCounts & set_handle_zeros(bool z=Defaults::handle_zeros)
Definition LogNormCounts.hpp:158
LogNormCounts & set_num_threads(int n=Defaults::num_threads)
Definition LogNormCounts.hpp:186
LogNormCounts & set_min_value(double m=ChoosePseudoCount::Defaults::min_value)
Definition LogNormCounts.hpp:228
LogNormCounts & set_choose_pseudo_count(bool c=Defaults::choose_pseudo_count)
Definition LogNormCounts.hpp:198
LogNormCounts & set_max_bias(double m=ChoosePseudoCount::Defaults::max_bias)
Definition LogNormCounts.hpp:208
std::shared_ptr< MAT > run(std::shared_ptr< MAT > mat, V size_factors) const
Definition LogNormCounts.hpp:247
LogNormCounts & set_pseudo_count(double p=Defaults::pseudo_count)
Definition LogNormCounts.hpp:99
Sanitize invalid size factors.
Definition SanitizeSizeFactors.hpp:75
void run(size_t n, T *size_factors, const SizeFactorValidity &status) const
Definition SanitizeSizeFactors.hpp:251
SanitizeSizeFactors & set_handle_non_finite(HandlerAction h)
Definition SanitizeSizeFactors.hpp:200
SanitizeSizeFactors & set_handle_zero(HandlerAction h=Defaults::handle_zero)
Definition SanitizeSizeFactors.hpp:133
Functions for single-cell RNA-seq analyses.
Definition AggregateAcrossCells.hpp:18
SizeFactorValidity validate_size_factors(size_t n, const T *size_factors)
Definition SanitizeSizeFactors.hpp:46
static constexpr BlockMode block_mode
Definition CenterSizeFactors.hpp:41
static constexpr double quantile
Definition ChoosePseudoCount.hpp:44
static constexpr double max_bias
Definition ChoosePseudoCount.hpp:49
static constexpr double min_value
Definition ChoosePseudoCount.hpp:54
Default parameter settings.
Definition LogNormCounts.hpp:38
static constexpr bool handle_zeros
Definition LogNormCounts.hpp:62
static constexpr int num_threads
Definition LogNormCounts.hpp:72
static constexpr double pseudo_count
Definition LogNormCounts.hpp:42
static constexpr bool choose_pseudo_count
Definition LogNormCounts.hpp:52
static constexpr bool handle_non_finite
Definition LogNormCounts.hpp:67
static constexpr bool sparse_addition
Definition LogNormCounts.hpp:47
static constexpr bool center
Definition LogNormCounts.hpp:57
Validity of size factors.
Definition SanitizeSizeFactors.hpp:12