singler_classic_markers
Classic marker detection for SingleR
Loading...
Searching...
No Matches
choose.hpp
Go to the documentation of this file.
1#ifndef SINGLER_CLASSIC_MARKERS_CHOOSE_HPP
2#define SINGLER_CLASSIC_MARKERS_CHOOSE_HPP
3
4#include <cstddef>
5#include <optional>
6#include <vector>
7
8#include "sanisizer/sanisizer.hpp"
9#include "tatami/tatami.hpp"
10#include "tatami_stats/tatami_stats.hpp"
11
12#include "queue.hpp"
13#include "scan.hpp"
14#include "number.hpp"
15
22
31 std::optional<std::size_t> number;
32
38 bool keep_ties = false;
39
44 int num_threads = 1;
45};
46
50template<bool include_stat_, typename Stat_, typename Value_, typename Index_, typename Label_>
51Markers<include_stat_, Index_, Stat_> choose_raw(
52 const tatami::Matrix<Value_, Index_>& matrix,
53 const Label_* label,
54 const ChooseOptions& options
55) {
56 const auto NC = matrix.ncol();
57 auto group_sizes = tatami_stats::tabulate_groups(label, NC);
58 const auto ngroups = group_sizes.size();
59
60 const auto num_keep = get_num_keep<Index_>(ngroups, options.number);
61 auto pqueues = sanisizer::create<std::vector<std::optional<PairwiseTopQueues<Stat_, Index_> > > >(options.num_threads);
62
63 const auto num_used = scan_matrix<Stat_>(
64 matrix,
65 sanisizer::cast<std::size_t>(ngroups),
66 label,
67 group_sizes,
68
69 /* setup = */ [&]() -> PairwiseTopQueues<Stat_, Index_> {
70 PairwiseTopQueues<Stat_, Index_> output;
71 allocate_pairwise_queues(output, num_keep, ngroups, options.keep_ties, /* check_nan = */ true);
72 return output;
73 },
74
75 /* fun = */ [&](const Index_ r, const std::vector<Stat_>& medians, PairwiseTopQueues<Stat_, Index_>& curqueues) -> void {
76 for (I<decltype(ngroups)> g1 = 1; g1 < ngroups; ++g1) {
77 for (I<decltype(ngroups)> g2 = 0; g2 < g1; ++g2) {
78 const auto delta = medians[g1] - medians[g2];
79 curqueues[g1][g2].emplace(delta, r);
80 curqueues[g2][g1].emplace(-delta, r);
81 }
82 }
83 },
84
85 /* finalize = */ [&](const int t, PairwiseTopQueues<Stat_, Index_>& curqueues) -> void {
86 pqueues[t] = std::move(curqueues);
87 },
88
89 options.num_threads
90 );
91
92 pqueues.resize(num_used);
93 Markers<include_stat_, Index_, Stat_> output;
94 report_best_top_queues<include_stat_>(pqueues, ngroups, output);
95 return output;
96}
125template<typename Stat_ = double, typename Value_, typename Index_, typename Label_>
126std::vector<std::vector<std::vector<std::pair<Index_, Stat_> > > > choose(
127 const tatami::Matrix<Value_, Index_>& matrix,
128 const Label_* label,
129 const ChooseOptions& options
130) {
131 return choose_raw<true, Stat_>(matrix, label, options);
132}
133
153template<typename Stat_ = double, typename Value_, typename Index_, typename Label_>
154std::vector<std::vector<std::vector<Index_> > > choose_index(
155 const tatami::Matrix<Value_, Index_>& matrix,
156 const Label_* label,
157 const ChooseOptions& options
158) {
159 return choose_raw<false, Stat_>(matrix, label, options);
160}
161
162}
163
164#endif
virtual Index_ ncol() const=0
Implementation of the classic SingleR marker detection.
Definition choose.hpp:21
std::vector< std::vector< std::vector< Index_ > > > choose_index(const tatami::Matrix< Value_, Index_ > &matrix, const Label_ *label, const ChooseOptions &options)
Definition choose.hpp:154
std::vector< std::vector< std::vector< std::pair< Index_, Stat_ > > > > choose(const tatami::Matrix< Value_, Index_ > &matrix, const Label_ *label, const ChooseOptions &options)
Definition choose.hpp:126
Options for choose().
Definition choose.hpp:26
int num_threads
Definition choose.hpp:44
std::optional< std::size_t > number
Definition choose.hpp:31
bool keep_ties
Definition choose.hpp:38