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);
201 std::fill_n(output, ngenes, ngenes + 1);
208 auto& curstore = stores[t - 1];
209 if (curstore.empty()) {
210 curstore.resize(ngenes, ngenes + 1);
212 curoutput = curstore.data();
214 std::vector<std::pair<Stat_, Index_> > buffer(ngenes);
216 for (
size_t g = start, end = start + length; g < end; ++g) {
220 auto used = fill_and_sort_rank_buffer(effects + g, ngroups, buffer);
221 compute_min_rank_internal(used, buffer, curoutput);
223 }, ngroups, threads);
225 for (
const auto& curstore : stores) {
227 for (
auto x : curstore) {
236template<
typename Stat_,
typename Index_,
typename Rank_>
237void compute_min_rank_pairwise(Index_ ngenes,
size_t ngroups,
const Stat_* effects,
const std::vector<SummaryBuffers<Stat_, Rank_> >& output,
int threads) {
238 size_t shift = ngroups * ngroups;
241 std::vector<std::pair<Stat_, Index_> > buffer(ngenes);
242 for (
size_t g = start, end = start + length; g < end; ++g) {
243 auto target = output[g].min_rank;
244 if (target == NULL) {
248 std::fill_n(target, ngenes, ngenes + 1);
249 auto base = effects + g * ngroups;
251 for (
size_t g2 = 0; g2 < ngroups; ++g2) {
255 auto used = fill_and_sort_rank_buffer(base + g2, shift, buffer);
256 compute_min_rank_internal(used, buffer, target);
259 }, ngroups, threads);
262template<
typename Stat_,
typename Rank_>
263SummaryBuffers<Stat_, Rank_> fill_summary_results(
265 SummaryResults<Stat_, Rank_>& out,
270 bool compute_min_rank)
272 SummaryBuffers<Stat_, Rank_> ptr;
275 out.min.resize(ngenes
276#ifdef SCRAN_MARKERS_TEST_INIT
277 , SCRAN_MARKERS_TEST_INIT
280 ptr.min = out.min.data();
283 out.mean.resize(ngenes
284#ifdef SCRAN_MARKERS_TEST_INIT
285 , SCRAN_MARKERS_TEST_INIT
288 ptr.mean = out.mean.data();
290 if (compute_median) {
291 out.median.resize(ngenes
292#ifdef SCRAN_MARKERS_TEST_INIT
293 , SCRAN_MARKERS_TEST_INIT
296 ptr.median = out.median.data();
299 out.max.resize(ngenes
300#ifdef SCRAN_MARKERS_TEST_INIT
301 , SCRAN_MARKERS_TEST_INIT
304 ptr.max = out.max.data();
306 if (compute_min_rank) {
307 out.min_rank.resize(ngenes
308#ifdef SCRAN_MARKERS_TEST_INIT
309 , SCRAN_MARKERS_TEST_INIT
312 ptr.min_rank = out.min_rank.data();
318template<
typename Stat_,
typename Rank_>
319std::vector<SummaryBuffers<Stat_, Rank_> > fill_summary_results(
322 std::vector<SummaryResults<Stat_, Rank_> >& outputs,
327 bool compute_min_rank)
329 outputs.resize(ngroups);
330 std::vector<SummaryBuffers<Stat_, Rank_> > ptrs;
331 ptrs.reserve(ngroups);
332 for (
size_t g = 0; g < ngroups; ++g) {
333 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