scran
C++ library for basic single-cell RNA-seq analyses
Loading...
Searching...
No Matches
CenterSizeFactors.hpp
Go to the documentation of this file.
1#ifndef SCRAN_CENTER_SIZE_FACTORS_HPP
2#define SCRAN_CENTER_SIZE_FACTORS_HPP
3
4#include "../utils/macros.hpp"
5
6#include "../utils/blocking.hpp"
7
8#include "SanitizeSizeFactors.hpp"
9#include <stdexcept>
10#include <vector>
11#include <algorithm>
12
18namespace scran {
19
28public:
32 enum BlockMode { PER_BLOCK, LOWEST };
33
37 struct Defaults {
41 static constexpr BlockMode block_mode = LOWEST;
42
46 static constexpr bool ignore_zeros = true;
47 };
48
49private:
51
52 bool ignore_zeros = Defaults::ignore_zeros;
53
54public:
70 block_mode = b;
71 return *this;
72 }
73
86 ignore_zeros = i;
87 return *this;
88 }
89
90public:
102 template<typename T>
103 SizeFactorValidity run(size_t n, T* size_factors) const {
104 size_t num_used = 0;
105 SizeFactorValidity output;
106
107 double mean = 0;
108 for (size_t i = 0; i < n; ++i) {
109 const auto& current = size_factors[i];
110 if (current < 0) {
111 output.has_negative = true;
112 continue;
113 } else if (current == 0) {
114 output.has_zero = true;
115 if (ignore_zeros) {
116 continue;
117 }
118 } else if (std::isnan(current)) {
119 output.has_nan = true;
120 continue;
121 } else if (std::isinf(current)) {
122 output.has_infinite = true;
123 continue;
124 }
125
126 ++num_used;
127 mean += current;
128 }
129
130 if (mean) {
131 mean /= num_used;
132 for (size_t i = 0; i < n; ++i){
133 size_factors[i] /= mean;
134 }
135 }
136
137 return output;
138 }
139
156 template<typename T, typename B>
157 SizeFactorValidity run_blocked(size_t n, T* size_factors, const B* block) const {
158 if (block == NULL) {
159 return run(n, size_factors);
160 }
161
162 size_t ngroups = count_ids(n, block);
163 std::vector<double> group_mean(ngroups);
164 std::vector<double> group_num(ngroups);
165
166 SizeFactorValidity output;
167 for (size_t i = 0; i < n; ++i) {
168 const auto& current = size_factors[i];
169 if (current < 0) {
170 output.has_negative = true;
171 continue;
172 } else if (current == 0) {
173 output.has_zero = true;
174 if (ignore_zeros) {
175 continue;
176 }
177 } else if (std::isinf(current)) {
178 output.has_infinite = true;
179 continue;
180 } else if (std::isnan(current)) {
181 output.has_nan = true;
182 continue;
183 }
184
185 const auto& b = block[i];
186 group_mean[b] += size_factors[i];
187 ++group_num[b];
188 }
189
190 for (size_t g = 0; g < ngroups; ++g) {
191 if (group_num[g]) {
192 group_mean[g] /= group_num[g];
193 }
194 }
195
196 if (block_mode == PER_BLOCK) {
197 for (size_t i = 0; i < n; ++i) {
198 const auto& div = group_mean[block[i]];
199 if (div) {
200 size_factors[i] /= div;
201 }
202 }
203
204 } else if (block_mode == LOWEST) {
205 // Ignore groups with means of zeros, either because they're full of zeros themselves
206 // or they have no cells associated with them.
207 double min = std::numeric_limits<double>::infinity();
208 for (size_t b = 0; b < ngroups; ++b) {
209 const auto& div = group_mean[b];
210 if (div && div < min) {
211 min = div;
212 }
213 }
214
215 if (std::isfinite(min)) {
216 for (size_t i = 0; i < n; ++i) {
217 size_factors[i] /= min;
218 }
219 }
220 }
221
222 return output;
223 }
224};
225
226}
227
228#endif
Center size factors prior to scaling normalization.
Definition CenterSizeFactors.hpp:27
SizeFactorValidity run(size_t n, T *size_factors) const
Definition CenterSizeFactors.hpp:103
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
CenterSizeFactors & set_ignore_zeros(bool i=Defaults::ignore_zeros)
Definition CenterSizeFactors.hpp:85
Functions for single-cell RNA-seq analyses.
Definition AggregateAcrossCells.hpp:18
size_t count_ids(size_t length, const Id_ *ids)
Definition blocking.hpp:29
Default parameter settings.
Definition CenterSizeFactors.hpp:37
static constexpr BlockMode block_mode
Definition CenterSizeFactors.hpp:41
static constexpr bool ignore_zeros
Definition CenterSizeFactors.hpp:46
Validity of size factors.
Definition SanitizeSizeFactors.hpp:12
bool has_nan
Definition SanitizeSizeFactors.hpp:26
bool has_zero
Definition SanitizeSizeFactors.hpp:21
bool has_infinite
Definition SanitizeSizeFactors.hpp:31
bool has_negative
Definition SanitizeSizeFactors.hpp:16