1#ifndef SCRAN_MARKERS_SUMMARIZE_COMPARISONS_HPP
2#define SCRAN_MARKERS_SUMMARIZE_COMPARISONS_HPP
4#include "tatami_stats/tatami_stats.hpp"
24template<
typename Stat_ =
double,
typename Rank_ =
int>
68template<
typename Stat_ =
double,
typename Rank_ =
int>
74 std::vector<Stat_>
min;
92 std::vector<Stat_>
max;
106template<
typename Stat_,
typename Rank_>
155template<
typename Stat_,
typename Rank_>
156void summarize_comparisons(
size_t ngenes,
size_t ngroups,
const Stat_* effects,
const std::vector<SummaryBuffers<Stat_, Rank_> >& output,
int threads) {
157 size_t shift = ngroups * ngroups;
160 std::vector<Stat_> buffer(ngroups);
161 auto effect_ptr = effects + start * shift;
163 for (
size_t gene = start, end = start + length; gene < end; ++gene, effect_ptr += shift) {
164 auto current_effects = effect_ptr;
165 for (
size_t l = 0; l < ngroups; ++l, current_effects += ngroups) {
166 summarize_comparisons(ngroups, current_effects, l, gene, output[l], buffer);
172template<
typename Stat_,
typename Index_>
173Index_ fill_and_sort_rank_buffer(
const Stat_* effects,
size_t stride, std::vector<std::pair<Stat_, Index_> >& buffer) {
174 auto bIt = buffer.begin();
175 for (Index_ i = 0, end = buffer.size(); i < end; ++i, effects += stride) {
176 if (!std::isnan(*effects)) {
177 bIt->first = -*effects;
182 std::sort(buffer.begin(), bIt);
183 return bIt - buffer.begin();
186template<
typename Stat_,
typename Index_,
typename Rank_>
187void compute_min_rank_internal(Index_ use,
const std::vector<std::pair<Stat_, Index_> >& buffer, Rank_* output) {
189 for (Index_ i = 0; i < use; ++i) {
190 auto& current = output[buffer[i].second];
191 if (counter < current) {
198template<
typename Stat_,
typename Index_,
typename Rank_>
199void compute_min_rank_for_group(Index_ ngenes,
size_t ngroups,
size_t group,
const Stat_* effects, Rank_* output,
int threads) {
200 std::vector<std::vector<Rank_> > stores(threads - 1);
206 std::fill_n(curoutput, ngenes, ngenes + 1);
208 stores[t - 1].resize(ngenes, ngenes + 1);
209 curoutput = stores[t - 1].data();
211 std::vector<std::pair<Stat_, Index_> > buffer(ngenes);
213 for (
size_t g = start, end = start + length; g < end; ++g) {
217 auto used = fill_and_sort_rank_buffer(effects + g, ngroups, buffer);
218 compute_min_rank_internal(used, buffer, curoutput);
220 }, ngroups, threads);
222 for (
const auto& curstore : stores) {
224 for (
auto x : curstore) {
233template<
typename Stat_,
typename Index_,
typename Rank_>
234void compute_min_rank_pairwise(Index_ ngenes,
size_t ngroups,
const Stat_* effects,
const std::vector<SummaryBuffers<Stat_, Rank_> >& output,
int threads) {
235 size_t shift = ngroups * ngroups;
238 std::vector<std::pair<Stat_, Index_> > buffer(ngenes);
239 for (
size_t g = start, end = start + length; g < end; ++g) {
240 auto target = output[g].min_rank;
241 if (target == NULL) {
245 std::fill_n(target, ngenes, ngenes + 1);
246 auto base = effects + g * ngroups;
248 for (
size_t g2 = 0; g2 < ngroups; ++g2) {
252 auto used = fill_and_sort_rank_buffer(base + g2, shift, buffer);
253 compute_min_rank_internal(used, buffer, target);
256 }, ngroups, threads);
259template<
typename Stat_,
typename Rank_>
260SummaryBuffers<Stat_, Rank_> fill_summary_results(
262 SummaryResults<Stat_, Rank_>& out,
267 bool compute_min_rank)
269 SummaryBuffers<Stat_, Rank_> ptr;
272 out.min.resize(ngenes);
273 ptr.min = out.min.data();
276 out.mean.resize(ngenes);
277 ptr.mean = out.mean.data();
279 if (compute_median) {
280 out.median.resize(ngenes);
281 ptr.median = out.median.data();
284 out.max.resize(ngenes);
285 ptr.max = out.max.data();
287 if (compute_min_rank) {
288 out.min_rank.resize(ngenes);
289 ptr.min_rank = out.min_rank.data();
295template<
typename Stat_,
typename Rank_>
296std::vector<SummaryBuffers<Stat_, Rank_> > fill_summary_results(
299 std::vector<SummaryResults<Stat_, Rank_> >& outputs,
304 bool compute_min_rank)
306 outputs.resize(ngroups);
307 std::vector<SummaryBuffers<Stat_, Rank_> > ptrs;
308 ptrs.reserve(ngroups);
309 for (
size_t g = 0; g < ngroups; ++g) {
310 ptrs.emplace_back(fill_summary_results(ngenes, outputs[g], compute_min, compute_mean, compute_median, compute_max, compute_min_rank));
Marker detection for single-cell data.
Definition score_markers_pairwise.hpp:23
void parallelize(Function_ fun, Index_ tasks, int threads)
Pointers to arrays to hold the summary statistics.
Definition summarize_comparisons.hpp:25
Stat_ * mean
Definition summarize_comparisons.hpp:38
Stat_ * median
Definition summarize_comparisons.hpp:45
Stat_ * min
Definition summarize_comparisons.hpp:31
Stat_ * max
Definition summarize_comparisons.hpp:52
Rank_ * min_rank
Definition summarize_comparisons.hpp:59
Container for the summary statistics.
Definition summarize_comparisons.hpp:69
std::vector< Stat_ > mean
Definition summarize_comparisons.hpp:80
std::vector< Stat_ > median
Definition summarize_comparisons.hpp:86
std::vector< Stat_ > min
Definition summarize_comparisons.hpp:74
std::vector< Rank_ > min_rank
Definition summarize_comparisons.hpp:98
std::vector< Stat_ > max
Definition summarize_comparisons.hpp:92