scran_norm
Scaling normalization of single-cell data
Loading...
Searching...
No Matches
normalize_counts.hpp
Go to the documentation of this file.
1#ifndef SCRAN_NORM_NORMALIZE_COUNTS_HPP
2#define SCRAN_NORM_NORMALIZE_COUNTS_HPP
3
4#include <type_traits>
5#include <vector>
6#include <memory>
7
8#include "tatami/tatami.hpp"
9
15namespace scran_norm {
16
28 double pseudo_count = 1;
29
37 bool preserve_sparsity = false;
38
42 bool log = true;
43
48 double log_base = 2;
49};
50
80template<typename OutputValue_ = double, typename InputValue_, typename Index_, class SizeFactors_>
81std::shared_ptr<tatami::Matrix<OutputValue_, Index_> > normalize_counts(
82 std::shared_ptr<const tatami::Matrix<InputValue_, Index_> > counts,
83 SizeFactors_ size_factors,
84 const NormalizeCountsOptions& options)
85{
86 auto current_pseudo = options.pseudo_count;
87 if (options.preserve_sparsity && current_pseudo != 1 && options.log) {
88 for (auto& x : size_factors) {
89 x *= current_pseudo;
90 }
91 current_pseudo = 1;
92 }
93
94 static_assert(std::is_floating_point<OutputValue_>::value);
95 if (static_cast<size_t>(size_factors.size()) != static_cast<size_t>(counts->ncol())) {
96 throw std::runtime_error("length of 'size_factors' should be equal to the number of columns of 'counts'");
97 }
98
99 auto div = std::make_shared<tatami::DelayedUnaryIsometricOperation<OutputValue_, InputValue_, Index_> >(
100 std::move(counts),
101 std::make_shared<tatami::DelayedUnaryIsometricDivideVectorHelper<true, OutputValue_, InputValue_, Index_, SizeFactors_> >(std::move(size_factors), false)
102 );
103
104 if (!options.log) {
105 return div;
106 }
107
108 if (current_pseudo == 1) {
109 return std::make_shared<tatami::DelayedUnaryIsometricOperation<OutputValue_, OutputValue_, Index_> >(
110 std::move(div),
111 std::make_shared<tatami::DelayedUnaryIsometricLog1pHelper<OutputValue_, OutputValue_, Index_, OutputValue_> >(options.log_base)
112 );
113 } else {
114 auto add = std::make_shared<tatami::DelayedUnaryIsometricOperation<OutputValue_, OutputValue_, Index_> >(
115 std::move(div),
116 std::make_shared<tatami::DelayedUnaryIsometricAddScalarHelper<OutputValue_, OutputValue_, Index_, OutputValue_> >(current_pseudo)
117 );
118 return std::make_shared<tatami::DelayedUnaryIsometricOperation<OutputValue_, OutputValue_, Index_> >(
119 std::move(add),
120 std::make_shared<tatami::DelayedUnaryIsometricLogHelper<OutputValue_, OutputValue_, Index_, OutputValue_> >(options.log_base)
121 );
122 }
123};
124
128// Overload for template deduction.
129template<typename OutputValue_ = double, typename InputValue_, typename Index_, class SizeFactors_>
130std::shared_ptr<tatami::Matrix<OutputValue_, Index_> > normalize_counts(
131 std::shared_ptr<tatami::Matrix<InputValue_, Index_> > counts,
132 SizeFactors_ size_factors,
133 const NormalizeCountsOptions& options)
134{
135 return normalize_counts(std::shared_ptr<const tatami::Matrix<InputValue_, Index_> >(std::move(counts)), std::move(size_factors), options);
136}
141}
142
143#endif
Scaling normalization of single-cell data.
Definition center_size_factors.hpp:18
std::shared_ptr< tatami::Matrix< OutputValue_, Index_ > > normalize_counts(std::shared_ptr< const tatami::Matrix< InputValue_, Index_ > > counts, SizeFactors_ size_factors, const NormalizeCountsOptions &options)
Definition normalize_counts.hpp:81
Options for normalize_counts().
Definition normalize_counts.hpp:20
double log_base
Definition normalize_counts.hpp:48
double pseudo_count
Definition normalize_counts.hpp:28
bool log
Definition normalize_counts.hpp:42
bool preserve_sparsity
Definition normalize_counts.hpp:37