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);
96 std::move(counts),
98 std::move(size_factors),
99 false
100 )
101 );
102
103 if (!options.log) {
104 return div;
105 }
106
107 if (current_pseudo == 1) {
109 std::move(div),
111 );
112 } else {
114 std::move(div),
116 );
118 std::move(add),
120 );
121 }
122};
123
127// Overload for template deduction.
128template<typename OutputValue_ = double, typename InputValue_, typename Index_, class SizeFactors_>
129std::shared_ptr<tatami::Matrix<OutputValue_, Index_> > normalize_counts(
130 std::shared_ptr<tatami::Matrix<InputValue_, Index_> > counts,
131 SizeFactors_ size_factors,
132 const NormalizeCountsOptions& options)
133{
134 return normalize_counts(std::shared_ptr<const tatami::Matrix<InputValue_, Index_> >(std::move(counts)), std::move(size_factors), options);
135}
140}
141
142#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
std::shared_ptr< Matrix< OutputValue_, Index_ > > make_DelayedUnaryIsometricOperation(std::shared_ptr< const Matrix< InputValue_, Index_ > > matrix, Operation_ operation)
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