1#ifndef SCRAN_MARKERS_SCORE_MARKERS_BEST_HPP
2#define SCRAN_MARKERS_SCORE_MARKERS_BEST_HPP
10#include "tatami_stats/tatami_stats.hpp"
11#include "sanisizer/sanisizer.hpp"
13#include "quickstats/quickstats.hpp"
15#include "scan_matrix.hpp"
16#include "average_group_stats.hpp"
18#include "create_combinations.hpp"
19#include "cohens_d.hpp"
20#include "simple_diff.hpp"
174template<
typename Stat_,
typename Index_>
180 std::vector<std::vector<Stat_> >
mean;
198 std::vector<std::vector<std::vector<std::pair<Index_, Stat_> > > >
cohens_d;
210 std::vector<std::vector<std::vector<std::pair<Index_, Stat_> > > >
auc;
222 std::vector<std::vector<std::vector<std::pair<Index_, Stat_> > > >
delta_mean;
234 std::vector<std::vector<std::vector<std::pair<Index_, Stat_> > > >
delta_detected;
242template<
typename Stat_,
typename Index_>
243using PairwiseTopQueues = std::vector<std::vector<topicks::TopQueue<Stat_, Index_> > >;
245template<
typename Stat_,
typename Index_>
246void allocate_best_top_queues(
247 PairwiseTopQueues<Stat_, Index_>& pqueues,
248 const std::size_t ngroups,
251 const bool keep_ties,
252 const std::optional<Stat_>& bound
257 if (bound.has_value()) {
261 sanisizer::resize(pqueues, ngroups);
262 for (
auto& x : pqueues) {
264 for (I<
decltype(ngroups)> g = 0; g < ngroups; ++g) {
265 x.emplace_back(top, larger, opt);
270template<
typename Stat_,
typename Index_>
271void add_best_top_queues(
272 PairwiseTopQueues<Stat_, Index_>& pqueues,
275 const std::vector<Stat_>& effects
277 for (I<
decltype(ngroups)> g1 = 0; g1 < ngroups; ++g1) {
278 for (I<
decltype(ngroups)> g2 = 0; g2 < ngroups; ++g2) {
279 const auto val = effects[sanisizer::nd_offset<std::size_t>(g2, ngroups, g1)];
281 pqueues[g1][g2].emplace(val, gene);
287template<
typename Stat_,
typename Index_>
288void report_best_top_queues(
289 std::vector<std::optional<PairwiseTopQueues<Stat_, Index_> > >& pqueues,
291 std::vector<std::vector<std::vector<std::pair<Index_, Stat_> > > >& output
294 const int num_available = pqueues.size();
297 if (num_available == 0) {
298 sanisizer::resize(output, ngroups);
299 for (I<
decltype(ngroups)> g1 = 0; g1 < ngroups; ++g1) {
300 sanisizer::resize(output[g1], ngroups);
306 auto& true_pqueue = *(pqueues.front());
307 for (
int t = 1; t < num_available; ++t) {
308 auto& current_pqueue = *(pqueues[t]);
309 for (I<
decltype(ngroups)> g1 = 0; g1 < ngroups; ++g1) {
310 for (I<
decltype(ngroups)> g2 = 0; g2 < ngroups; ++g2) {
311 auto& current_in = current_pqueue[g1][g2];
312 auto& current_out = true_pqueue[g1][g2];
313 while (!current_in.empty()) {
314 current_out.push(current_in.top());
322 sanisizer::resize(output, ngroups);
323 for (I<
decltype(ngroups)> g1 = 0; g1 < ngroups; ++g1) {
324 sanisizer::resize(output[g1], ngroups);
325 for (I<
decltype(ngroups)> g2 = 0; g2 < ngroups; ++g2) {
329 auto& current_in = true_pqueue[g1][g2];
330 auto& current_out = output[g1][g2];
331 while (!current_in.empty()) {
332 const auto& best = current_in.top();
333 current_out.emplace_back(best.second, best.first);
336 std::reverse(current_out.begin(), current_out.end());
341template<
typename Index_,
typename Stat_>
342void find_best_simple_best_effects(
344 const std::size_t ngroups,
345 const std::size_t nblocks,
346 const std::size_t ncombos,
347 const std::vector<Stat_>& combo_means,
348 const std::vector<Stat_>& combo_vars,
349 const std::vector<Stat_>& combo_detected,
350 const BlockAverageInfo<Stat_>& average_info,
352 const ScoreMarkersBestOptions& options,
353 ScoreMarkersBestResults<Stat_, Index_>& output
355 std::optional<std::vector<Stat_> > total_weights_per_group;
356 const Stat_* total_weights_ptr = NULL;
357 if (average_info.use_mean()) {
358 if (options.compute_group_mean || options.compute_group_detected) {
360 total_weights_per_group = compute_total_weight_per_group(ngroups, nblocks, average_info.combo_weights().data());
361 total_weights_ptr = total_weights_per_group->data();
363 total_weights_ptr = average_info.combo_weights().data();
368 std::vector<Stat_*> mptrs;
369 if (options.compute_group_mean) {
370 mptrs.reserve(ngroups);
371 sanisizer::resize(output.mean, ngroups);
372 for (
auto& x : output.mean) {
373 sanisizer::resize(x, ngenes);
374 mptrs.push_back(x.data());
378 std::vector<Stat_*> dptrs;
379 if (options.compute_group_detected) {
380 dptrs.reserve(ngroups);
381 sanisizer::resize(output.detected, ngroups);
382 for (
auto& x : output.detected) {
383 sanisizer::resize(x, ngenes);
384 dptrs.push_back(x.data());
388 std::optional<PrecomputedPairwiseWeights<Stat_> > preweights;
389 if (average_info.use_mean()) {
390 if (options.compute_cohens_d || options.compute_delta_mean || options.compute_delta_detected) {
391 preweights.emplace(ngroups, nblocks, average_info.combo_weights().data());
396 std::optional<std::vector<std::optional<PairwiseTopQueues<Stat_, Index_> > > > threaded_cohens_d_queues, threaded_delta_detected_queues, threaded_delta_mean_queues;
397 if (options.compute_cohens_d) {
398 threaded_cohens_d_queues.emplace(sanisizer::cast<I<
decltype(threaded_cohens_d_queues->size())> >(options.num_threads));
400 if (options.compute_delta_mean) {
401 threaded_delta_mean_queues.emplace(sanisizer::cast<I<
decltype(threaded_delta_mean_queues->size())> >(options.num_threads));
403 if (options.compute_delta_detected) {
404 threaded_delta_detected_queues.emplace(sanisizer::cast<I<
decltype(threaded_delta_detected_queues->size())> >(options.num_threads));
407 const auto ngroups2 = sanisizer::product<typename std::vector<Stat_>::size_type>(ngroups, ngroups);
409 int num_used =
tatami::parallelize([&](
const int t,
const Index_ start,
const Index_ length) ->
void {
410 std::optional<PairwiseTopQueues<Stat_, Index_> > local_cohens_d_queue, local_delta_mean_queue, local_delta_detected_queue;
411 if (options.compute_cohens_d) {
412 local_cohens_d_queue.emplace();
413 allocate_best_top_queues(*local_cohens_d_queue, ngroups, top, options.largest_cohens_d, options.keep_ties, options.threshold_cohens_d);
415 if (options.compute_delta_mean) {
416 local_delta_mean_queue.emplace();
417 allocate_best_top_queues(*local_delta_mean_queue, ngroups, top, options.largest_delta_mean, options.keep_ties, options.threshold_delta_mean);
419 if (options.compute_delta_detected) {
420 local_delta_detected_queue.emplace();
421 allocate_best_top_queues(*local_delta_detected_queue, ngroups, top, options.largest_delta_detected, options.keep_ties, options.threshold_delta_detected);
424 std::vector<Stat_> buffer;
425 if (options.compute_cohens_d || options.compute_delta_mean || options.compute_delta_detected) {
426 buffer.resize(ngroups2);
429 std::optional<std::vector<Stat_> > qbuffer, qrevbuffer;
430 std::optional<quickstats::SingleQuantileVariableNumber<Stat_, std::size_t> > qcalc;
431 if (!average_info.use_mean()) {
433 qrevbuffer.emplace();
434 qcalc.emplace(nblocks, average_info.quantile());
437 for (Index_ gene = start, end = start + length; gene < end; ++gene) {
438 auto in_offset = sanisizer::product_unsafe<std::size_t>(gene, ncombos);
440 if (options.compute_group_mean) {
441 const auto tmp_means = combo_means.data() + in_offset;
442 if (average_info.use_mean()) {
443 average_group_stats_blockmean(gene, ngroups, nblocks, tmp_means, average_info.combo_weights().data(), total_weights_ptr, mptrs);
445 average_group_stats_blockquantile(gene, ngroups, nblocks, tmp_means, *qbuffer, *qcalc, mptrs);
449 if (options.compute_group_detected) {
450 const auto tmp_detected = combo_detected.data() + in_offset;
451 if (average_info.use_mean()) {
452 average_group_stats_blockmean(gene, ngroups, nblocks, tmp_detected, average_info.combo_weights().data(), total_weights_ptr, dptrs);
454 average_group_stats_blockquantile(gene, ngroups, nblocks, tmp_detected, *qbuffer, *qcalc, dptrs);
459 if (options.compute_cohens_d) {
460 const auto tmp_means = combo_means.data() + in_offset;
461 const auto tmp_variances = combo_vars.data() + in_offset;
462 if (average_info.use_mean()) {
463 compute_pairwise_cohens_d_blockmean(tmp_means, tmp_variances, ngroups, nblocks, options.threshold, *preweights, buffer.data());
465 compute_pairwise_cohens_d_blockquantile(tmp_means, tmp_variances, ngroups, nblocks, options.threshold, *qbuffer, *qrevbuffer, *qcalc, buffer.data());
467 add_best_top_queues(*local_cohens_d_queue, gene, ngroups, buffer);
470 if (options.compute_delta_mean) {
471 const auto tmp_means = combo_means.data() + in_offset;
472 if (average_info.use_mean()) {
473 compute_pairwise_simple_diff_blockmean(tmp_means, ngroups, nblocks, *preweights, buffer.data());
475 compute_pairwise_simple_diff_blockquantile(tmp_means, ngroups, nblocks, *qbuffer, *qcalc, buffer.data());
477 add_best_top_queues(*local_delta_mean_queue, gene, ngroups, buffer);
480 if (options.compute_delta_detected) {
481 const auto tmp_detected = combo_detected.data() + in_offset;
482 if (average_info.use_mean()) {
483 compute_pairwise_simple_diff_blockmean(tmp_detected, ngroups, nblocks, *preweights, buffer.data());
485 compute_pairwise_simple_diff_blockquantile(tmp_detected, ngroups, nblocks, *qbuffer, *qcalc, buffer.data());
487 add_best_top_queues(*local_delta_detected_queue, gene, ngroups, buffer);
492 if (options.compute_cohens_d) {
493 (*threaded_cohens_d_queues)[t] = std::move(local_cohens_d_queue);
495 if (options.compute_delta_mean) {
496 (*threaded_delta_mean_queues)[t] = std::move(local_delta_mean_queue);
498 if (options.compute_delta_detected) {
499 (*threaded_delta_detected_queues)[t] = std::move(local_delta_detected_queue);
501 }, ngenes, options.num_threads);
504 if (options.compute_cohens_d) {
505 threaded_cohens_d_queues->resize(num_used);
506 report_best_top_queues(*threaded_cohens_d_queues, ngroups, output.cohens_d);
508 if (options.compute_delta_mean) {
509 threaded_delta_mean_queues->resize(num_used);
510 report_best_top_queues(*threaded_delta_mean_queues, ngroups, output.delta_mean);
512 if (options.compute_delta_detected) {
513 threaded_delta_detected_queues->resize(num_used);
514 report_best_top_queues(*threaded_delta_detected_queues, ngroups, output.delta_detected);
528 const std::size_t ngroups,
529 const Group_*
const group,
530 const std::size_t nblocks,
531 const Block_*
const block,
532 const std::size_t ncombos,
533 const std::size_t*
const combo,
534 const std::vector<Index_>& combo_sizes,
536 const ScoreMarkersBestOptions& options
538 const auto ngenes = matrix.
nrow();
539 const auto payload_size = sanisizer::product<typename std::vector<Stat_>::size_type>(ngenes, ncombos);
540 std::vector<Stat_> combo_means, combo_vars, combo_detected;
541 if (options.compute_group_mean || options.compute_cohens_d || options.compute_delta_mean) {
542 combo_means.resize(payload_size);
544 if (options.compute_cohens_d) {
545 combo_vars.resize(payload_size);
547 if (options.compute_group_detected || options.compute_delta_detected) {
548 combo_detected.resize(payload_size);
553 BlockAverageInfo<Stat_> average_info;
554 if (options.block_average_policy == BlockAveragePolicy::MEAN) {
555 average_info = BlockAverageInfo<Stat_>(
558 options.block_weight_policy,
559 options.variable_block_weight_parameters
563 average_info = BlockAverageInfo<Stat_>(options.block_quantile);
566 ScoreMarkersBestResults<Stat_, Index_> output;
568 if (options.compute_auc) {
569 auto auc_queues = sanisizer::create<std::vector<std::optional<PairwiseTopQueues<Stat_, Index_> > > >(options.num_threads);
571 struct AucResultWorkspace {
572 AucResultWorkspace(
const std::size_t ngroups) : pairwise_buffer(sanisizer::product<typename std::vector<Stat_>::size_type>(ngroups, ngroups)) {};
573 std::vector<Stat_> pairwise_buffer;
574 PairwiseTopQueues<Stat_, Index_> queue;
577 const auto num_used = scan_matrix_by_row_custom_auc<single_block_>(
591 [&](
const int) -> AucResultWorkspace {
592 AucResultWorkspace res_work(ngroups);
593 allocate_best_top_queues(res_work.queue, ngroups, top, options.largest_auc, options.keep_ties, options.threshold_auc);
596 [&](
const Index_ gene, AucScanWorkspace<Value_, Group_, Stat_, Index_>& auc_work, AucResultWorkspace& res_work) ->
void {
597 process_auc_for_rows(auc_work, ngroups, nblocks, options.threshold, res_work.pairwise_buffer.data());
598 add_best_top_queues(res_work.queue, gene, ngroups, res_work.pairwise_buffer);
600 [&](
const int t, AucResultWorkspace& res_work) ->
void {
601 auc_queues[t] = std::move(res_work.queue);
606 auc_queues.resize(num_used);
607 report_best_top_queues(auc_queues, ngroups, output.auc);
610 scan_matrix_by_row_full_auc<single_block_>(
623 static_cast<Stat_*
>(NULL),
629 scan_matrix_by_column(
632 if constexpr(single_block_) {
639 if constexpr(single_block_) {
653 find_best_simple_best_effects(
697template<
typename Stat_,
typename Value_,
typename Index_,
typename Group_>
700 const Group_*
const group,
704 const Index_ NC = matrix.
ncol();
705 const auto group_sizes = tatami_stats::tabulate_groups(group, NC);
706 const auto ngroups = sanisizer::cast<std::size_t>(group_sizes.size());
708 return internal::score_markers_best<true, Stat_>(
713 static_cast<int*
>(NULL),
715 static_cast<std::size_t*
>(NULL),
748template<
typename Stat_,
typename Value_,
typename Index_,
typename Group_,
typename Block_>
751 const Group_*
const group,
752 const Block_*
const block,
756 const Index_ NC = matrix.
ncol();
757 const auto ngroups = tatami_stats::total_groups(group, NC);
758 const auto nblocks = tatami_stats::total_groups(block, NC);
760 const auto combinations = internal::create_combinations(ngroups, group, nblocks, block, NC);
761 const auto combo_sizes = internal::tabulate_combinations<Index_>(ngroups, nblocks, combinations);
762 const auto ncombos = combo_sizes.size();
764 return internal::score_markers_best<false, Stat_>(
766 sanisizer::cast<std::size_t>(ngroups),
768 sanisizer::cast<std::size_t>(nblocks),
770 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:27
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:749
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:698
int parallelize(Function_ fun, const Index_ tasks, const int workers)
std::optional< Stat_ > bound