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
100 std::move(counts),
102 std::move(size_factors),
103 false
104 )
105 );
106
107 if (!options.log) {
108 return div;
109 }
110
111 if (current_pseudo == 1) {
113 std::move(div),
115 );
116 } else {
118 std::move(div),
120 );
122 std::move(add),
124 );
125 }
126};
127
131// Overload for template deduction.
132template<typename OutputValue_ = double, typename InputValue_, typename Index_, class SizeFactors_>
133std::shared_ptr<tatami::Matrix<OutputValue_, Index_> > normalize_counts(
134 std::shared_ptr<tatami::Matrix<InputValue_, Index_> > counts,
135 SizeFactors_ size_factors,
136 const NormalizeCountsOptions& options)
137{
138 return normalize_counts(std::shared_ptr<const tatami::Matrix<InputValue_, Index_> >(std::move(counts)), std::move(size_factors), options);
139}
144}
145
146#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