1#ifndef SCRAN_MARKERS_SCORE_MARKERS_BEST_HPP
2#define SCRAN_MARKERS_SCORE_MARKERS_BEST_HPP
9#include "tatami_stats/tatami_stats.hpp"
10#include "sanisizer/sanisizer.hpp"
13#include "scan_matrix.hpp"
14#include "average_group_stats.hpp"
16#include "create_combinations.hpp"
17#include "cohens_d.hpp"
18#include "simple_diff.hpp"
172template<
typename Stat_,
typename Index_>
178 std::vector<std::vector<Stat_> >
mean;
196 std::vector<std::vector<std::vector<std::pair<Index_, Stat_> > > >
cohens_d;
208 std::vector<std::vector<std::vector<std::pair<Index_, Stat_> > > >
auc;
220 std::vector<std::vector<std::vector<std::pair<Index_, Stat_> > > >
delta_mean;
232 std::vector<std::vector<std::vector<std::pair<Index_, Stat_> > > >
delta_detected;
240template<
typename Stat_,
typename Index_>
241using PairwiseTopQueues = std::vector<std::vector<topicks::TopQueue<Stat_, Index_> > >;
243template<
typename Stat_,
typename Index_>
244void allocate_best_top_queues(
245 PairwiseTopQueues<Stat_, Index_>& pqueues,
246 const std::size_t ngroups,
249 const bool keep_ties,
250 const std::optional<Stat_>& bound
255 if (bound.has_value()) {
259 sanisizer::resize(pqueues, ngroups);
260 for (
auto& x : pqueues) {
262 for (I<
decltype(ngroups)> g = 0; g < ngroups; ++g) {
263 x.emplace_back(top, larger, opt);
268template<
typename Stat_,
typename Index_>
269void add_best_top_queues(
270 PairwiseTopQueues<Stat_, Index_>& pqueues,
273 const std::vector<Stat_>& effects
275 for (I<
decltype(ngroups)> g1 = 0; g1 < ngroups; ++g1) {
276 for (I<
decltype(ngroups)> g2 = 0; g2 < ngroups; ++g2) {
277 const auto val = effects[sanisizer::nd_offset<std::size_t>(g2, ngroups, g1)];
279 pqueues[g1][g2].emplace(val, gene);
285template<
typename Stat_,
typename Index_>
286void report_best_top_queues(
287 std::vector<PairwiseTopQueues<Stat_, Index_> >& pqueues,
289 std::vector<std::vector<std::vector<std::pair<Index_, Stat_> > > >& output
292 const int num_threads = pqueues.size();
295 auto& true_pqueue = pqueues.front();
296 for (
int t = 1; t < num_threads; ++t) {
297 for (I<
decltype(ngroups)> g1 = 0; g1 < ngroups; ++g1) {
298 for (I<
decltype(ngroups)> g2 = 0; g2 < ngroups; ++g2) {
299 auto& current_in = pqueues[t][g1][g2];
300 auto& current_out = true_pqueue[g1][g2];
301 while (!current_in.empty()) {
302 current_out.push(current_in.top());
310 sanisizer::resize(output, ngroups);
311 for (I<
decltype(ngroups)> g1 = 0; g1 < ngroups; ++g1) {
312 sanisizer::resize(output[g1], ngroups);
313 for (I<
decltype(ngroups)> g2 = 0; g2 < ngroups; ++g2) {
317 auto& current_in = true_pqueue[g1][g2];
318 auto& current_out = output[g1][g2];
319 while (!current_in.empty()) {
320 const auto& best = current_in.top();
321 current_out.emplace_back(best.second, best.first);
324 std::reverse(current_out.begin(), current_out.end());
329template<
typename Index_,
typename Stat_>
330void find_best_simple_best_effects(
332 const std::size_t ngroups,
333 const std::size_t nblocks,
334 const std::size_t ncombos,
335 const std::vector<Stat_>& combo_means,
336 const std::vector<Stat_>& combo_vars,
337 const std::vector<Stat_>& combo_detected,
338 const BlockAverageInfo<Stat_>& average_info,
340 const ScoreMarkersBestOptions& options,
341 ScoreMarkersBestResults<Stat_, Index_>& output
343 std::optional<std::vector<Stat_> > total_weights_per_group;
344 const Stat_* total_weights_ptr = NULL;
345 if (average_info.use_mean()) {
346 if (options.compute_group_mean || options.compute_group_detected) {
348 total_weights_per_group = compute_total_weight_per_group(ngroups, nblocks, average_info.combo_weights().data());
349 total_weights_ptr = total_weights_per_group->data();
351 total_weights_ptr = average_info.combo_weights().data();
356 std::vector<Stat_*> mptrs;
357 if (options.compute_group_mean) {
358 mptrs.reserve(ngroups);
359 sanisizer::resize(output.mean, ngroups);
360 for (
auto& x : output.mean) {
361 sanisizer::resize(x, ngenes);
362 mptrs.push_back(x.data());
366 std::vector<Stat_*> dptrs;
367 if (options.compute_group_detected) {
368 dptrs.reserve(ngroups);
369 sanisizer::resize(output.detected, ngroups);
370 for (
auto& x : output.detected) {
371 sanisizer::resize(x, ngenes);
372 dptrs.push_back(x.data());
376 std::optional<PrecomputedPairwiseWeights<Stat_> > preweights;
377 if (average_info.use_mean()) {
378 if (options.compute_cohens_d || options.compute_delta_mean || options.compute_delta_detected) {
379 preweights.emplace(ngroups, nblocks, average_info.combo_weights().data());
384 std::vector<PairwiseTopQueues<Stat_, Index_> > cohens_d_queues, delta_detected_queues, delta_mean_queues;
385 if (options.compute_cohens_d) {
386 sanisizer::resize(cohens_d_queues, options.num_threads);
388 if (options.compute_delta_mean) {
389 sanisizer::resize(delta_mean_queues, options.num_threads);
391 if (options.compute_delta_detected) {
392 sanisizer::resize(delta_detected_queues, options.num_threads);
395 const auto ngroups2 = sanisizer::product<typename std::vector<Stat_>::size_type>(ngroups, ngroups);
398 if (options.compute_cohens_d) {
399 allocate_best_top_queues(cohens_d_queues[t], ngroups, top, options.largest_cohens_d, options.keep_ties, options.threshold_cohens_d);
401 if (options.compute_delta_mean) {
402 allocate_best_top_queues(delta_mean_queues[t], ngroups, top, options.largest_delta_mean, options.keep_ties, options.threshold_delta_mean);
404 if (options.compute_delta_detected) {
405 allocate_best_top_queues(delta_detected_queues[t], ngroups, top, options.largest_delta_detected, options.keep_ties, options.threshold_delta_detected);
407 std::vector<Stat_> buffer;
408 if (options.compute_cohens_d || options.compute_delta_mean || options.compute_delta_detected) {
409 buffer.resize(ngroups2);
412 std::optional<std::vector<Stat_> > qbuffer, qrevbuffer;
413 std::optional<scran_blocks::SingleQuantileVariable<Stat_, typename std::vector<Stat_>::iterator> > qcalc;
414 if (!average_info.use_mean()) {
416 qrevbuffer.emplace();
417 qcalc.emplace(nblocks, average_info.quantile());
420 for (Index_ gene = start, end = start + length; gene < end; ++gene) {
421 auto in_offset = sanisizer::product_unsafe<std::size_t>(gene, ncombos);
423 if (options.compute_group_mean) {
424 const auto tmp_means = combo_means.data() + in_offset;
425 if (average_info.use_mean()) {
426 average_group_stats_blockmean(gene, ngroups, nblocks, tmp_means, average_info.combo_weights().data(), total_weights_ptr, mptrs);
428 average_group_stats_blockquantile(gene, ngroups, nblocks, tmp_means, *qbuffer, *qcalc, mptrs);
432 if (options.compute_group_detected) {
433 const auto tmp_detected = combo_detected.data() + in_offset;
434 if (average_info.use_mean()) {
435 average_group_stats_blockmean(gene, ngroups, nblocks, tmp_detected, average_info.combo_weights().data(), total_weights_ptr, dptrs);
437 average_group_stats_blockquantile(gene, ngroups, nblocks, tmp_detected, *qbuffer, *qcalc, dptrs);
442 if (options.compute_cohens_d) {
443 const auto tmp_means = combo_means.data() + in_offset;
444 const auto tmp_variances = combo_vars.data() + in_offset;
445 if (average_info.use_mean()) {
446 compute_pairwise_cohens_d_blockmean(tmp_means, tmp_variances, ngroups, nblocks, options.threshold, *preweights, buffer.data());
448 compute_pairwise_cohens_d_blockquantile(tmp_means, tmp_variances, ngroups, nblocks, options.threshold, *qbuffer, *qrevbuffer, *qcalc, buffer.data());
450 add_best_top_queues(cohens_d_queues[t], gene, ngroups, buffer);
453 if (options.compute_delta_mean) {
454 const auto tmp_means = combo_means.data() + in_offset;
455 if (average_info.use_mean()) {
456 compute_pairwise_simple_diff_blockmean(tmp_means, ngroups, nblocks, *preweights, buffer.data());
458 compute_pairwise_simple_diff_blockquantile(tmp_means, ngroups, nblocks, *qbuffer, *qcalc, buffer.data());
460 add_best_top_queues(delta_mean_queues[t], gene, ngroups, buffer);
463 if (options.compute_delta_detected) {
464 const auto tmp_detected = combo_detected.data() + in_offset;
465 if (average_info.use_mean()) {
466 compute_pairwise_simple_diff_blockmean(tmp_detected, ngroups, nblocks, *preweights, buffer.data());
468 compute_pairwise_simple_diff_blockquantile(tmp_detected, ngroups, nblocks, *qbuffer, *qcalc, buffer.data());
470 add_best_top_queues(delta_detected_queues[t], gene, ngroups, buffer);
473 }, ngenes, options.num_threads);
476 if (options.compute_cohens_d) {
477 report_best_top_queues(cohens_d_queues, ngroups, output.cohens_d);
480 if (options.compute_delta_mean) {
481 report_best_top_queues(delta_mean_queues, ngroups, output.delta_mean);
484 if (options.compute_delta_detected) {
485 report_best_top_queues(delta_detected_queues, ngroups, output.delta_detected);
499 const std::size_t ngroups,
500 const Group_*
const group,
501 const std::size_t nblocks,
502 const Block_*
const block,
503 const std::size_t ncombos,
504 const std::size_t*
const combo,
505 const std::vector<Index_>& combo_sizes,
507 const ScoreMarkersBestOptions& options
509 const auto ngenes = matrix.
nrow();
510 const auto payload_size = sanisizer::product<typename std::vector<Stat_>::size_type>(ngenes, ncombos);
511 std::vector<Stat_> combo_means, combo_vars, combo_detected;
512 if (options.compute_group_mean || options.compute_cohens_d || options.compute_delta_mean) {
513 combo_means.resize(payload_size);
515 if (options.compute_cohens_d) {
516 combo_vars.resize(payload_size);
518 if (options.compute_group_detected || options.compute_delta_detected) {
519 combo_detected.resize(payload_size);
524 BlockAverageInfo<Stat_> average_info;
525 if (options.block_average_policy == BlockAveragePolicy::MEAN) {
526 average_info = BlockAverageInfo<Stat_>(
529 options.block_weight_policy,
530 options.variable_block_weight_parameters
534 average_info = BlockAverageInfo<Stat_>(options.block_quantile);
537 ScoreMarkersBestResults<Stat_, Index_> output;
539 if (options.compute_auc) {
540 auto auc_queues = sanisizer::create<std::vector<PairwiseTopQueues<Stat_, Index_> > >(options.num_threads);
542 struct AucResultWorkspace {
543 AucResultWorkspace(
const std::size_t ngroups, PairwiseTopQueues<Stat_, Index_>& pqueue) :
544 pairwise_buffer(sanisizer::product<typename std::vector<Stat_>::size_type>(ngroups, ngroups)),
547 std::vector<Stat_> pairwise_buffer;
548 PairwiseTopQueues<Stat_, Index_>* queue_ptr;
551 scan_matrix_by_row_custom_auc<single_block_>(
565 [&](
int t) -> AucResultWorkspace {
566 allocate_best_top_queues(auc_queues[t], ngroups, top, options.largest_auc, options.keep_ties, options.threshold_auc);
567 return AucResultWorkspace(ngroups, auc_queues[t]);
569 [&](
const Index_ gene, AucScanWorkspace<Value_, Group_, Stat_, Index_>& auc_work, AucResultWorkspace& res_work) ->
void {
570 process_auc_for_rows(auc_work, ngroups, nblocks, options.threshold, res_work.pairwise_buffer.data());
571 add_best_top_queues(*(res_work.queue_ptr), gene, ngroups, res_work.pairwise_buffer);
576 report_best_top_queues(auc_queues, ngroups, output.auc);
579 scan_matrix_by_row_full_auc<single_block_>(
592 static_cast<Stat_*
>(NULL),
598 scan_matrix_by_column(
601 if constexpr(single_block_) {
608 if constexpr(single_block_) {
622 find_best_simple_best_effects(
666template<
typename Stat_,
typename Value_,
typename Index_,
typename Group_>
669 const Group_*
const group,
673 const Index_ NC = matrix.
ncol();
674 const auto group_sizes = tatami_stats::tabulate_groups(group, NC);
675 const auto ngroups = sanisizer::cast<std::size_t>(group_sizes.size());
677 return internal::score_markers_best<true, Stat_>(
682 static_cast<int*
>(NULL),
684 static_cast<std::size_t*
>(NULL),
717template<
typename Stat_,
typename Value_,
typename Index_,
typename Group_,
typename Block_>
720 const Group_*
const group,
721 const Block_*
const block,
725 const Index_ NC = matrix.
ncol();
726 const auto ngroups = tatami_stats::total_groups(group, NC);
727 const auto nblocks = tatami_stats::total_groups(block, NC);
729 const auto combinations = internal::create_combinations(ngroups, group, block, NC);
730 const auto combo_sizes = internal::tabulate_combinations<Index_>(ngroups, nblocks, combinations);
731 const auto ncombos = combo_sizes.size();
733 return internal::score_markers_best<false, Stat_>(
735 sanisizer::cast<std::size_t>(ngroups),
737 sanisizer::cast<std::size_t>(nblocks),
739 sanisizer::cast<std::size_t>(ncombos),
Averaging statistics over blocks.
virtual Index_ ncol() const=0
virtual Index_ nrow() const=0
virtual bool prefer_rows() const=0
void compute_weights(const std::size_t num_blocks, const Size_ *const sizes, const WeightPolicy policy, const VariableWeightParameters &variable, Weight_ *const weights)
Marker detection for single-cell data.
Definition score_markers_pairwise.hpp:26
BlockAveragePolicy
Definition block_averages.hpp:27
ScoreMarkersBestResults< Stat_, Index_ > score_markers_best_blocked(const tatami::Matrix< Value_, Index_ > &matrix, const Group_ *const group, const Block_ *const block, const Index_ top, const ScoreMarkersBestOptions &options)
Definition score_markers_best.hpp:718
ScoreMarkersBestResults< Stat_, Index_ > score_markers_best(const tatami::Matrix< Value_, Index_ > &matrix, const Group_ *const group, const Index_ top, const ScoreMarkersBestOptions &options)
Definition score_markers_best.hpp:667
void parallelize(Function_ fun, const Index_ tasks, const int threads)
std::optional< Stat_ > bound