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"
15#include "PrecomputedPairwiseWeights.hpp"
16#include "create_combinations.hpp"
17#include "cohens_d.hpp"
18#include "simple_diff.hpp"
156template<
typename Stat_,
typename Index_>
162 std::vector<std::vector<Stat_> >
mean;
180 std::vector<std::vector<std::vector<std::pair<Index_, Stat_> > > >
cohens_d;
192 std::vector<std::vector<std::vector<std::pair<Index_, Stat_> > > >
auc;
204 std::vector<std::vector<std::vector<std::pair<Index_, Stat_> > > >
delta_mean;
216 std::vector<std::vector<std::vector<std::pair<Index_, Stat_> > > >
delta_detected;
224template<
typename Stat_,
typename Index_>
225using PairwiseTopQueues = std::vector<std::vector<topicks::TopQueue<Stat_, Index_> > >;
227template<
typename Stat_,
typename Index_>
228void allocate_best_top_queues(
229 PairwiseTopQueues<Stat_, Index_>& pqueues,
230 const std::size_t ngroups,
233 const bool keep_ties,
234 const std::optional<Stat_>& bound
239 if (bound.has_value()) {
243 sanisizer::resize(pqueues, ngroups);
244 for (
auto& x : pqueues) {
246 for (
decltype(I(ngroups)) g = 0; g < ngroups; ++g) {
247 x.emplace_back(top, larger, opt);
252template<
typename Stat_,
typename Index_>
253void add_best_top_queues(
254 PairwiseTopQueues<Stat_, Index_>& pqueues,
257 const std::vector<Stat_>& effects
259 for (
decltype(I(ngroups)) g1 = 0; g1 < ngroups; ++g1) {
260 for (
decltype(I(ngroups)) g2 = 0; g2 < ngroups; ++g2) {
261 const auto val = effects[sanisizer::nd_offset<std::size_t>(g2, ngroups, g1)];
263 pqueues[g1][g2].emplace(val, gene);
269template<
typename Stat_,
typename Index_>
270void report_best_top_queues(
271 std::vector<PairwiseTopQueues<Stat_, Index_> >& pqueues,
273 std::vector<std::vector<std::vector<std::pair<Index_, Stat_> > > >& output
276 const int num_threads = pqueues.size();
279 auto& true_pqueue = pqueues.front();
280 for (
int t = 1; t < num_threads; ++t) {
281 for (
decltype(I(ngroups)) g1 = 0; g1 < ngroups; ++g1) {
282 for (
decltype(I(ngroups)) g2 = 0; g2 < ngroups; ++g2) {
283 auto& current_in = pqueues[t][g1][g2];
284 auto& current_out = true_pqueue[g1][g2];
285 while (!current_in.empty()) {
286 current_out.push(current_in.top());
294 sanisizer::resize(output, ngroups);
295 for (
decltype(I(ngroups)) g1 = 0; g1 < ngroups; ++g1) {
296 sanisizer::resize(output[g1], ngroups);
297 for (
decltype(I(ngroups)) g2 = 0; g2 < ngroups; ++g2) {
301 auto& current_in = true_pqueue[g1][g2];
302 auto& current_out = output[g1][g2];
303 while (!current_in.empty()) {
304 const auto& best = current_in.top();
305 current_out.emplace_back(best.second, best.first);
308 std::reverse(current_out.begin(), current_out.end());
313template<
typename Index_,
typename Stat_>
314void find_best_simple_best_effects(
316 const std::size_t ngroups,
317 const std::size_t nblocks,
318 const std::size_t ncombos,
319 const std::vector<Stat_>& combo_weights,
320 std::vector<Stat_>& combo_means,
321 std::vector<Stat_>& combo_vars,
322 std::vector<Stat_>& combo_detected,
323 ScoreMarkersBestResults<Stat_, Index_>& output,
325 const ScoreMarkersBestOptions& options
327 std::vector<Stat_> total_weights_per_group;
328 const Stat_* total_weights_ptr = combo_weights.data();
330 total_weights_per_group = compute_total_weight_per_group(ngroups, nblocks, combo_weights.data());
331 total_weights_ptr = total_weights_per_group.data();
333 PrecomputedPairwiseWeights<Stat_> preweights(ngroups, nblocks, combo_weights.data());
335 std::vector<Stat_*> mptrs;
336 if (options.compute_group_mean) {
337 mptrs.reserve(ngroups);
338 sanisizer::resize(output.mean, ngroups);
339 for (
auto& x : output.mean) {
340 sanisizer::resize(x, ngenes);
341 mptrs.push_back(x.data());
345 std::vector<Stat_*> dptrs;
346 if (options.compute_group_detected) {
347 dptrs.reserve(ngroups);
348 sanisizer::resize(output.detected, ngroups);
349 for (
auto& x : output.detected) {
350 sanisizer::resize(x, ngenes);
351 dptrs.push_back(x.data());
356 std::vector<PairwiseTopQueues<Stat_, Index_> > cohens_d_queues, delta_detected_queues, delta_mean_queues;
357 if (options.compute_cohens_d) {
358 sanisizer::resize(cohens_d_queues, options.num_threads);
360 if (options.compute_delta_mean) {
361 sanisizer::resize(delta_mean_queues, options.num_threads);
363 if (options.compute_delta_detected) {
364 sanisizer::resize(delta_detected_queues, options.num_threads);
367 const auto ngroups2 = sanisizer::product<typename std::vector<Stat_>::size_type>(ngroups, ngroups);
370 if (options.compute_cohens_d) {
371 allocate_best_top_queues(cohens_d_queues[t], ngroups, top, options.largest_cohens_d, options.keep_ties, options.threshold_cohens_d);
373 if (options.compute_delta_mean) {
374 allocate_best_top_queues(delta_mean_queues[t], ngroups, top, options.largest_delta_mean, options.keep_ties, options.threshold_delta_mean);
376 if (options.compute_delta_detected) {
377 allocate_best_top_queues(delta_detected_queues[t], ngroups, top, options.largest_delta_detected, options.keep_ties, options.threshold_delta_detected);
379 std::vector<Stat_> buffer;
380 if (options.compute_cohens_d || options.compute_delta_mean || options.compute_delta_detected) {
381 buffer.resize(ngroups2);
384 for (Index_ gene = start, end = start + length; gene < end; ++gene) {
385 auto in_offset = sanisizer::product_unsafe<std::size_t>(gene, ncombos);
387 if (options.compute_group_mean) {
388 const auto tmp_means = combo_means.data() + in_offset;
389 average_group_stats(gene, ngroups, nblocks, tmp_means, combo_weights.data(), total_weights_ptr, mptrs);
391 if (options.compute_group_detected) {
392 const auto tmp_detected = combo_detected.data() + in_offset;
393 average_group_stats(gene, ngroups, nblocks, tmp_detected, combo_weights.data(), total_weights_ptr, dptrs);
397 if (options.compute_cohens_d) {
398 const auto tmp_means = combo_means.data() + in_offset;
399 const auto tmp_variances = combo_vars.data() + in_offset;
400 compute_pairwise_cohens_d(tmp_means, tmp_variances, ngroups, nblocks, preweights, options.threshold, buffer.data());
401 add_best_top_queues(cohens_d_queues[t], gene, ngroups, buffer);
404 if (options.compute_delta_mean) {
405 const auto tmp_means = combo_means.data() + in_offset;
406 compute_pairwise_simple_diff(tmp_means, ngroups, nblocks, preweights, buffer.data());
407 add_best_top_queues(delta_mean_queues[t], gene, ngroups, buffer);
410 if (options.compute_delta_detected) {
411 const auto tmp_detected = combo_detected.data() + in_offset;
412 compute_pairwise_simple_diff(tmp_detected, ngroups, nblocks, preweights, buffer.data());
413 add_best_top_queues(delta_detected_queues[t], gene, ngroups, buffer);
416 }, ngenes, options.num_threads);
419 if (options.compute_cohens_d) {
420 report_best_top_queues(cohens_d_queues, ngroups, output.cohens_d);
423 if (options.compute_delta_mean) {
424 report_best_top_queues(delta_mean_queues, ngroups, output.delta_mean);
427 if (options.compute_delta_detected) {
428 report_best_top_queues(delta_detected_queues, ngroups, output.delta_detected);
442 const std::size_t ngroups,
443 const Group_*
const group,
444 const std::size_t nblocks,
445 const Block_*
const block,
446 const std::size_t ncombos,
447 const std::size_t*
const combo,
448 const std::vector<Index_>& combo_sizes,
450 const ScoreMarkersBestOptions& options
452 const auto ngenes = matrix.
nrow();
453 const auto payload_size = sanisizer::product<typename std::vector<Stat_>::size_type>(ngenes, ncombos);
454 std::vector<Stat_> combo_means, combo_vars, combo_detected;
455 if (options.compute_group_mean || options.compute_cohens_d || options.compute_delta_mean) {
456 combo_means.resize(payload_size);
458 if (options.compute_cohens_d) {
459 combo_vars.resize(payload_size);
461 if (options.compute_group_detected || options.compute_delta_detected) {
462 combo_detected.resize(payload_size);
469 options.block_weight_policy,
470 options.variable_block_weight_parameters
473 ScoreMarkersBestResults<Stat_, Index_> output;
475 if (options.compute_auc) {
476 auto auc_queues = sanisizer::create<std::vector<PairwiseTopQueues<Stat_, Index_> > >(options.num_threads);
478 struct AucResultWorkspace {
479 AucResultWorkspace() =
default;
480 AucResultWorkspace(
const std::size_t ngroups, PairwiseTopQueues<Stat_, Index_>& pqueue) :
481 pairwise_buffer(sanisizer::product<typename std::vector<Stat_>::size_type>(ngroups, ngroups)),
484 std::vector<Stat_> pairwise_buffer;
485 PairwiseTopQueues<Stat_, Index_>* queue_ptr;
488 scan_matrix_by_row_custom_auc<single_block_>(
500 [&](
int t) -> AucResultWorkspace {
501 allocate_best_top_queues(auc_queues[t], ngroups, top, options.largest_auc, options.keep_ties, options.threshold_auc);
502 return AucResultWorkspace(ngroups, auc_queues[t]);
504 [&](
const Index_ gene, AucScanWorkspace<Value_, Group_, Index_, Stat_>& auc_work, AucResultWorkspace& res_work) ->
void {
505 process_auc_for_rows(auc_work, ngroups, nblocks, options.threshold, res_work.pairwise_buffer.data());
506 add_best_top_queues(*(res_work.queue_ptr), gene, ngroups, res_work.pairwise_buffer);
513 report_best_top_queues(auc_queues, ngroups, output.auc);
516 scan_matrix_by_row_full_auc<single_block_>(
527 static_cast<Stat_*
>(NULL),
535 scan_matrix_by_column(
538 if constexpr(single_block_) {
545 if constexpr(single_block_) {
559 find_best_simple_best_effects(
603template<
typename Stat_,
typename Value_,
typename Index_,
typename Group_>
606 const Group_*
const group,
610 const Index_ NC = matrix.
ncol();
611 const auto group_sizes = tatami_stats::tabulate_groups(group, NC);
612 const auto ngroups = sanisizer::cast<std::size_t>(group_sizes.size());
614 return internal::score_markers_best<true, Stat_>(
619 static_cast<int*
>(NULL),
621 static_cast<std::size_t*
>(NULL),
654template<
typename Stat_,
typename Value_,
typename Index_,
typename Group_,
typename Block_>
657 const Group_*
const group,
658 const Block_*
const block,
662 const Index_ NC = matrix.
ncol();
663 const auto ngroups = tatami_stats::total_groups(group, NC);
664 const auto nblocks = tatami_stats::total_groups(block, NC);
666 const auto combinations = internal::create_combinations(ngroups, group, block, NC);
667 const auto combo_sizes = internal::tabulate_combinations<Index_>(ngroups, nblocks, combinations);
668 const auto ncombos = combo_sizes.size();
670 return internal::score_markers_best<false, Stat_>(
672 sanisizer::cast<std::size_t>(ngroups),
674 sanisizer::cast<std::size_t>(nblocks),
676 sanisizer::cast<std::size_t>(ncombos),
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:25
ScoreMarkersBestResults< Stat_, Index_ > score_markers_best(const tatami::Matrix< Value_, Index_ > &matrix, const Group_ *const group, int top, const ScoreMarkersBestOptions &options)
Definition score_markers_best.hpp:604
ScoreMarkersBestResults< Stat_, Index_ > score_markers_best_blocked(const tatami::Matrix< Value_, Index_ > &matrix, const Group_ *const group, const Block_ *const block, int top, const ScoreMarkersBestOptions &options)
Definition score_markers_best.hpp:655
void parallelize(Function_ fun, const Index_ tasks, const int threads)
std::optional< Stat_ > bound