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
81template<typename OutputValue_ = double, typename InputValue_, typename Index_, class SizeFactors_>
82std::shared_ptr<tatami::Matrix<OutputValue_, Index_> > normalize_counts(
83 std::shared_ptr<const tatami::Matrix<InputValue_, Index_> > counts,
84 SizeFactors_ size_factors,
85 const NormalizeCountsOptions& options)
86{
87 auto current_pseudo = options.pseudo_count;
88 if (options.preserve_sparsity && current_pseudo != 1 && options.log) {
89 for (auto& x : size_factors) {
90 x *= current_pseudo;
91 }
92 current_pseudo = 1;
93 }
94
95 static_assert(std::is_floating_point<OutputValue_>::value);
96 if (static_cast<size_t>(size_factors.size()) != static_cast<size_t>(counts->ncol())) {
97 throw std::runtime_error("length of 'size_factors' should be equal to the number of columns of 'counts'");
98 }
99
100 auto div = std::make_shared<tatami::DelayedUnaryIsometricOperation<OutputValue_, InputValue_, Index_> >(
101 std::move(counts),
102 std::make_shared<tatami::DelayedUnaryIsometricDivideVectorHelper<true, OutputValue_, InputValue_, Index_, SizeFactors_> >(std::move(size_factors), false)
103 );
104
105 if (!options.log) {
106 return div;
107 }
108
109 if (current_pseudo == 1) {
110 return std::make_shared<tatami::DelayedUnaryIsometricOperation<OutputValue_, OutputValue_, Index_> >(
111 std::move(div),
112 std::make_shared<tatami::DelayedUnaryIsometricLog1pHelper<OutputValue_, OutputValue_, Index_, OutputValue_> >(options.log_base)
113 );
114 } else {
115 auto add = std::make_shared<tatami::DelayedUnaryIsometricOperation<OutputValue_, OutputValue_, Index_> >(
116 std::move(div),
117 std::make_shared<tatami::DelayedUnaryIsometricAddScalarHelper<OutputValue_, OutputValue_, Index_, OutputValue_> >(current_pseudo)
118 );
119 return std::make_shared<tatami::DelayedUnaryIsometricOperation<OutputValue_, OutputValue_, Index_> >(
120 std::move(add),
121 std::make_shared<tatami::DelayedUnaryIsometricLogHelper<OutputValue_, OutputValue_, Index_, OutputValue_> >(options.log_base)
122 );
123 }
124};
125
129// Overload for template deduction.
130template<typename OutputValue_ = double, typename InputValue_, typename Index_, class SizeFactors_>
131std::shared_ptr<tatami::Matrix<OutputValue_, Index_> > normalize_counts(
132 std::shared_ptr<tatami::Matrix<InputValue_, Index_> > counts,
133 SizeFactors_ size_factors,
134 const NormalizeCountsOptions& options)
135{
136 return normalize_counts(std::shared_ptr<const tatami::Matrix<InputValue_, Index_> >(std::move(counts)), std::move(size_factors), options);
137}
142}
143
144#endif
Scaling normalization of single-cell data.
Definition center_size_factors.hpp:20
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:82
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