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"
146template<
typename Stat_,
typename Index_>
152 std::vector<std::vector<Stat_> >
mean;
170 std::vector<std::vector<std::vector<std::pair<Index_, Stat_> > > >
cohens_d;
182 std::vector<std::vector<std::vector<std::pair<Index_, Stat_> > > >
auc;
194 std::vector<std::vector<std::vector<std::pair<Index_, Stat_> > > >
delta_mean;
206 std::vector<std::vector<std::vector<std::pair<Index_, Stat_> > > >
delta_detected;
214template<
typename Stat_,
typename Index_>
215using PairwiseTopQueues = std::vector<std::vector<topicks::TopQueue<Stat_, Index_> > >;
217template<
typename Stat_,
typename Index_>
218void allocate_best_top_queues(
219 PairwiseTopQueues<Stat_, Index_>& pqueues,
220 const std::size_t ngroups,
223 const bool keep_ties,
224 const std::optional<Stat_>& bound
229 if (bound.has_value()) {
233 sanisizer::resize(pqueues, ngroups);
234 for (
auto& x : pqueues) {
236 for (
decltype(I(ngroups)) g = 0; g < ngroups; ++g) {
237 x.emplace_back(top, larger, opt);
242template<
typename Stat_,
typename Index_>
243void add_best_top_queues(
244 PairwiseTopQueues<Stat_, Index_>& pqueues,
247 const std::vector<Stat_>& effects
249 for (
decltype(I(ngroups)) g1 = 0; g1 < ngroups; ++g1) {
250 for (
decltype(I(ngroups)) g2 = 0; g2 < ngroups; ++g2) {
251 const auto val = effects[sanisizer::nd_offset<std::size_t>(g2, ngroups, g1)];
253 pqueues[g1][g2].emplace(val, gene);
259template<
typename Stat_,
typename Index_>
260void report_best_top_queues(
261 std::vector<PairwiseTopQueues<Stat_, Index_> >& pqueues,
263 std::vector<std::vector<std::vector<std::pair<Index_, Stat_> > > >& output
266 const int num_threads = pqueues.size();
269 auto& true_pqueue = pqueues.front();
270 for (
int t = 1; t < num_threads; ++t) {
271 for (
decltype(I(ngroups)) g1 = 0; g1 < ngroups; ++g1) {
272 for (
decltype(I(ngroups)) g2 = 0; g2 < ngroups; ++g2) {
273 auto& current_in = pqueues[t][g1][g2];
274 auto& current_out = true_pqueue[g1][g2];
275 while (!current_in.empty()) {
276 current_out.push(current_in.top());
284 sanisizer::resize(output, ngroups);
285 for (
decltype(I(ngroups)) g1 = 0; g1 < ngroups; ++g1) {
286 sanisizer::resize(output[g1], ngroups);
287 for (
decltype(I(ngroups)) g2 = 0; g2 < ngroups; ++g2) {
291 auto& current_in = true_pqueue[g1][g2];
292 auto& current_out = output[g1][g2];
293 while (!current_in.empty()) {
294 const auto& best = current_in.top();
295 current_out.emplace_back(best.second, best.first);
298 std::reverse(current_out.begin(), current_out.end());
303template<
typename Index_,
typename Stat_>
304void find_best_simple_best_effects(
306 const std::size_t ngroups,
307 const std::size_t nblocks,
308 const std::size_t ncombos,
309 const std::vector<Stat_>& combo_weights,
310 std::vector<Stat_>& combo_means,
311 std::vector<Stat_>& combo_vars,
312 std::vector<Stat_>& combo_detected,
313 ScoreMarkersBestResults<Stat_, Index_>& output,
315 const ScoreMarkersBestOptions& options
317 std::vector<Stat_> total_weights_per_group;
318 const Stat_* total_weights_ptr = combo_weights.data();
320 total_weights_per_group = compute_total_weight_per_group(ngroups, nblocks, combo_weights.data());
321 total_weights_ptr = total_weights_per_group.data();
323 PrecomputedPairwiseWeights<Stat_> preweights(ngroups, nblocks, combo_weights.data());
325 std::vector<Stat_*> mptrs;
326 mptrs.reserve(ngroups);
327 sanisizer::resize(output.mean, ngroups);
328 for (
auto& x : output.mean) {
329 sanisizer::resize(x, ngenes);
330 mptrs.push_back(x.data());
333 std::vector<Stat_*> dptrs;
334 dptrs.reserve(ngroups);
335 sanisizer::resize(output.detected, ngroups);
336 for (
auto& x : output.detected) {
337 sanisizer::resize(x, ngenes);
338 dptrs.push_back(x.data());
342 std::vector<PairwiseTopQueues<Stat_, Index_> > cohens_d_queues, delta_detected_queues, delta_mean_queues;
343 if (options.compute_cohens_d) {
344 sanisizer::resize(cohens_d_queues, options.num_threads);
346 if (options.compute_delta_mean) {
347 sanisizer::resize(delta_mean_queues, options.num_threads);
349 if (options.compute_delta_detected) {
350 sanisizer::resize(delta_detected_queues, options.num_threads);
353 const auto ngroups2 = sanisizer::product<typename std::vector<Stat_>::size_type>(ngroups, ngroups);
356 if (options.compute_cohens_d) {
357 allocate_best_top_queues(cohens_d_queues[t], ngroups, top, options.largest_cohens_d, options.keep_ties, options.threshold_cohens_d);
359 if (options.compute_delta_mean) {
360 allocate_best_top_queues(delta_mean_queues[t], ngroups, top, options.largest_delta_mean, options.keep_ties, options.threshold_delta_mean);
362 if (options.compute_delta_detected) {
363 allocate_best_top_queues(delta_detected_queues[t], ngroups, top, options.largest_delta_detected, options.keep_ties, options.threshold_delta_detected);
365 std::vector<Stat_> buffer(ngroups2);
367 for (Index_ gene = start, end = start + length; gene < end; ++gene) {
368 auto in_offset = sanisizer::product_unsafe<std::size_t>(gene, ncombos);
369 const auto tmp_means = combo_means.data() + in_offset;
370 const auto tmp_variances = combo_vars.data() + in_offset;
371 const auto tmp_detected = combo_detected.data() + in_offset;
372 average_group_stats(gene, ngroups, nblocks, tmp_means, tmp_detected, combo_weights.data(), total_weights_ptr, mptrs, dptrs);
375 if (options.compute_cohens_d) {
376 compute_pairwise_cohens_d(tmp_means, tmp_variances, ngroups, nblocks, preweights, options.threshold, buffer.data());
377 add_best_top_queues(cohens_d_queues[t], gene, ngroups, buffer);
380 if (options.compute_delta_mean) {
381 compute_pairwise_simple_diff(tmp_means, ngroups, nblocks, preweights, buffer.data());
382 add_best_top_queues(delta_mean_queues[t], gene, ngroups, buffer);
385 if (options.compute_delta_detected) {
386 compute_pairwise_simple_diff(tmp_detected, ngroups, nblocks, preweights, buffer.data());
387 add_best_top_queues(delta_detected_queues[t], gene, ngroups, buffer);
390 }, ngenes, options.num_threads);
393 if (options.compute_cohens_d) {
394 report_best_top_queues(cohens_d_queues, ngroups, output.cohens_d);
397 if (options.compute_delta_mean) {
398 report_best_top_queues(delta_mean_queues, ngroups, output.delta_mean);
401 if (options.compute_delta_detected) {
402 report_best_top_queues(delta_detected_queues, ngroups, output.delta_detected);
416 const std::size_t ngroups,
417 const Group_*
const group,
418 const std::size_t nblocks,
419 const Block_*
const block,
420 const std::size_t ncombos,
421 const std::size_t*
const combo,
422 const std::vector<Index_>& combo_sizes,
424 const ScoreMarkersBestOptions& options
426 const auto ngenes = matrix.
nrow();
427 const auto payload_size = sanisizer::product<typename std::vector<Stat_>::size_type>(ngenes, ncombos);
428 std::vector<Stat_> combo_means(payload_size), combo_vars(payload_size), combo_detected(payload_size);
434 options.block_weight_policy,
435 options.variable_block_weight_parameters
438 ScoreMarkersBestResults<Stat_, Index_> output;
440 if (options.compute_auc) {
441 auto auc_queues = sanisizer::create<std::vector<PairwiseTopQueues<Stat_, Index_> > >(options.num_threads);
443 struct AucResultWorkspace {
444 AucResultWorkspace() =
default;
445 AucResultWorkspace(
const std::size_t ngroups, PairwiseTopQueues<Stat_, Index_>& pqueue) :
446 pairwise_buffer(sanisizer::product<typename std::vector<Stat_>::size_type>(ngroups, ngroups)),
449 std::vector<Stat_> pairwise_buffer;
450 PairwiseTopQueues<Stat_, Index_>* queue_ptr;
453 scan_matrix_by_row_custom_auc<single_block_>(
465 [&](
int t) -> AucResultWorkspace {
466 allocate_best_top_queues(auc_queues[t], ngroups, top, options.largest_auc, options.keep_ties, options.threshold_auc);
467 return AucResultWorkspace(ngroups, auc_queues[t]);
469 [&](
const Index_ gene, AucScanWorkspace<Value_, Group_, Index_, Stat_>& auc_work, AucResultWorkspace& res_work) ->
void {
470 process_auc_for_rows(auc_work, ngroups, nblocks, options.threshold, res_work.pairwise_buffer.data());
471 add_best_top_queues(*(res_work.queue_ptr), gene, ngroups, res_work.pairwise_buffer);
478 report_best_top_queues(auc_queues, ngroups, output.auc);
481 scan_matrix_by_row_full_auc<single_block_>(
492 static_cast<Stat_*
>(NULL),
500 scan_matrix_by_column(
503 if constexpr(single_block_) {
510 if constexpr(single_block_) {
524 find_best_simple_best_effects(
568template<
typename Stat_,
typename Value_,
typename Index_,
typename Group_>
571 const Group_*
const group,
575 const Index_ NC = matrix.
ncol();
576 const auto group_sizes = tatami_stats::tabulate_groups(group, NC);
577 const auto ngroups = sanisizer::cast<std::size_t>(group_sizes.size());
579 return internal::score_markers_best<true, Stat_>(
584 static_cast<int*
>(NULL),
586 static_cast<std::size_t*
>(NULL),
619template<
typename Stat_,
typename Value_,
typename Index_,
typename Group_,
typename Block_>
622 const Group_*
const group,
623 const Block_*
const block,
627 const Index_ NC = matrix.
ncol();
628 const auto ngroups = tatami_stats::total_groups(group, NC);
629 const auto nblocks = tatami_stats::total_groups(block, NC);
631 const auto combinations = internal::create_combinations(ngroups, group, block, NC);
632 const auto combo_sizes = internal::tabulate_combinations<Index_>(ngroups, nblocks, combinations);
633 const auto ncombos = combo_sizes.size();
635 return internal::score_markers_best<false, Stat_>(
637 sanisizer::cast<std::size_t>(ngroups),
639 sanisizer::cast<std::size_t>(nblocks),
641 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:569
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:620
void parallelize(Function_ fun, const Index_ tasks, const int threads)
std::optional< Stat_ > bound