1#ifndef SCRAN_AGGREGATE_AGGREGATE_ACROSS_GENES_HPP
2#define SCRAN_AGGREGATE_AGGREGATE_ACROSS_GENES_HPP
6#include <unordered_set>
11#include "tatami_stats/tatami_stats.hpp"
12#include "sanisizer/sanisizer.hpp"
44template <
typename Sum_>
51 std::vector<Sum_*>
sum;
58template <
typename Sum_>
65 std::vector<std::vector<Sum_> >
sum;
71template<
typename Index_,
typename Gene_,
typename Weight_>
72std::vector<Gene_> create_subset(
const std::vector<std::tuple<std::size_t, const Gene_*, const Weight_*> >& gene_sets,
const Index_ nrow) {
73 std::unordered_set<Gene_> of_interest;
74 for (
const auto& set : gene_sets) {
75 const auto set_size = std::get<0>(set);
76 const auto set_genes = std::get<1>(set);
77 of_interest.insert(set_genes, set_genes + set_size);
80 std::vector<Index_> subset(of_interest.begin(), of_interest.end());
81 if (!subset.empty()) {
82 std::sort(subset.begin(), subset.end());
83 if (subset.front() < 0 || subset.back() >= nrow) {
84 throw std::runtime_error(
"set indices are out of range");
91template<
typename Index_>
92std::pair<std::vector<Index_>, Index_> create_subset_mapping(
const std::vector<Index_>& subset) {
93 const Index_ offset = subset.front();
94 const Index_ span = subset.back() - offset + 1;
96 const auto nsubs = subset.size();
97 for (I<
decltype(nsubs)> i = 0; i < nsubs; ++i) {
98 mapping[subset[i] - offset] = i;
100 return std::make_pair(std::move(mapping), offset);
103template<
typename Data_,
typename Index_,
typename Gene_,
typename Weight_,
typename Sum_>
104void aggregate_across_genes_by_column(
106 const std::vector<std::tuple<std::size_t, const Gene_*, const Weight_*> >& gene_sets,
107 const AggregateAcrossGenesBuffers<Sum_>& buffers,
108 const AggregateAcrossGenesOptions& options)
112 const auto& subset = *subset_of_interest;
113 const Index_ nsubs = subset.size();
116 const auto num_sets = gene_sets.size();
117 auto remapping = sanisizer::create<std::vector<std::pair<std::vector<Index_>,
const Weight_*> > >(num_sets);
119 const auto sub_mapping = create_subset_mapping(subset);
120 const auto& mapping = sub_mapping.first;
121 const Gene_ offset = sub_mapping.second;
123 for (I<
decltype(num_sets)> s = 0; s < num_sets; ++s) {
124 const auto& set = gene_sets[s];
125 const auto set_size = std::get<0>(set);
126 const auto set_genes = std::get<1>(set);
128 auto& remapped = remapping[s].first;
129 remapped.reserve(set_size);
130 for (I<
decltype(set_size)> g = 0; g < set_size; ++g) {
131 remapped.push_back(mapping[set_genes[g] - offset]);
133 remapping[s].second = std::get<2>(set);
143 for (Index_ x = start, end = start + length; x < end; ++x) {
144 const auto ptr = ext->fetch(vbuffer.data());
145 for (I<
decltype(num_sets)> s = 0; s < num_sets; ++s) {
146 const auto& set = remapping[s];
150 for (I<
decltype(set.first.size())> i = 0, send = set.first.size(); i < send; ++i) {
151 value += ptr[set.first[i]] * set.second[i];
154 for (
const auto ix : set.first) {
159 buffers.sum[s][x] = value;
163 }, p.
ncol(), options.num_threads);
166template<
typename Data_,
typename Index_,
typename Gene_,
typename Weight_,
typename Sum_>
167void aggregate_across_genes_by_row(
169 const std::vector<std::tuple<std::size_t, const Gene_*, const Weight_*> >& gene_sets,
170 const AggregateAcrossGenesBuffers<Sum_>& buffers,
171 const AggregateAcrossGenesOptions& options)
174 const auto subset = create_subset<Index_>(gene_sets, p.
nrow());
175 const Index_ nsubs = subset.size();
176 const auto sub_oracle = std::make_shared<tatami::FixedViewOracle<Index_> >(subset.data(), nsubs);
178 const auto num_sets = gene_sets.size();
181 const auto sub_mapping = create_subset_mapping(subset);
182 const auto& mapping = sub_mapping.first;
183 const Gene_ offset = sub_mapping.second;
185 for (I<
decltype(num_sets)> s = 0; s < num_sets; ++s) {
186 const auto& set = gene_sets[s];
187 const auto set_size = std::get<0>(set);
188 const auto set_genes = std::get<1>(set);
189 const auto set_weights = std::get<2>(set);
192 for (I<
decltype(set_size)> g = 0; g < set_size; ++g) {
193 remapping[mapping[set_genes[g] - offset]].emplace_back(s, set_weights[g]);
196 for (I<
decltype(set_size)> g = 0; g < set_size; ++g) {
197 remapping[mapping[set_genes[g] - offset]].emplace_back(s, 1);
204 auto get_sum = [&](Index_ i) -> Sum_* {
return buffers.sum[i]; };
205 tatami_stats::LocalOutputBuffers<Sum_, I<
decltype(get_sum)>> local_sums(t, num_sets, start, length, std::move(get_sum));
212 for (Index_ sub = 0; sub < nsubs; ++sub) {
213 const auto range = ext->fetch(vbuffer.data(), ibuffer.data());
215 for (
const auto& sw : remapping[sub]) {
216 const auto outptr = local_sums.data(sw.first);
217 const auto wt = sw.second;
218 for (Index_ c = 0; c < range.number; ++c) {
219 outptr[range.index[c] - start] += range.value[c] * wt;
228 for (Index_ sub = 0; sub < nsubs; ++sub) {
229 const auto ptr = ext->fetch(vbuffer.data());
230 for (
const auto& sw : remapping[sub]) {
231 const auto outptr = local_sums.data(sw.first);
232 const auto wt = sw.second;
233 for (Index_ cell = 0; cell < length; ++cell) {
234 outptr[cell] += ptr[cell] * wt;
240 local_sums.transfer();
241 }, p.
ncol(), options.num_threads);
269template<
typename Data_,
typename Index_,
typename Gene_,
typename Weight_,
typename Sum_>
272 const std::vector<std::tuple<std::size_t, const Gene_*, const Weight_*> >& gene_sets,
277 aggregate_across_genes_by_row(input, gene_sets, buffers, options);
279 aggregate_across_genes_by_column(input, gene_sets, buffers, options);
283 const auto nsets = gene_sets.size();
285 const Index_ NC = input.
ncol();
286 for (Index_ s = start, end = start + length; s < end; ++s) {
287 const auto& set = gene_sets[s];
288 const auto set_size = std::get<0>(set);
291 const auto set_weights = std::get<2>(set);
293 denom = std::accumulate(set_weights, set_weights + set_size,
static_cast<Sum_
>(0));
298 const auto current = buffers.
sum[s];
299 for (Index_ c = 0; c < NC; ++c) {
326template<
typename Sum_ =
double,
typename Data_,
typename Index_,
typename Gene_,
typename Weight_>
329 const std::vector<std::tuple<std::size_t, const Gene_*, const Weight_*> >& gene_sets,
335 const Index_ NC = input.
ncol();
336 const auto nsets = gene_sets.size();
337 sanisizer::resize(output.
sum, nsets);
338 sanisizer::resize(buffers.
sum, nsets);
340 for (I<
decltype(nsets)> s = 0; s < nsets; ++s) {
344#ifdef SCRAN_AGGREGATE_TEST_INIT
345 , SCRAN_AGGREGATE_TEST_INIT
348 buffers.
sum[s] = output.
sum[s].data();
virtual Index_ ncol() const=0
virtual Index_ nrow() const=0
virtual bool prefer_rows() const=0
virtual std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, const Options &opt) const=0
Aggregate single-cell expression values.
Definition aggregate_across_cells.hpp:20
void aggregate_across_genes(const tatami::Matrix< Data_, Index_ > &input, const std::vector< std::tuple< std::size_t, const Gene_ *, const Weight_ * > > &gene_sets, const AggregateAcrossGenesBuffers< Sum_ > &buffers, const AggregateAcrossGenesOptions &options)
Definition aggregate_across_genes.hpp:270
auto new_extractor(const Matrix< Value_, Index_ > &matrix, const bool row, MaybeOracle< oracle_, Index_ > oracle, Args_ &&... args)
std::shared_ptr< const std::vector< Index_ > > VectorPtr
void parallelize(Function_ fun, const Index_ tasks, const int threads)
void resize_container_to_Index_size(Container_ &container, const Index_ x, Args_ &&... args)
Container_ create_container_of_Index_size(const Index_ x, Args_ &&... args)
auto consecutive_extractor(const Matrix< Value_, Index_ > &matrix, const bool row, const Index_ iter_start, const Index_ iter_length, Args_ &&... args)
Buffers for aggregate_across_genes().
Definition aggregate_across_genes.hpp:45
std::vector< Sum_ * > sum
Definition aggregate_across_genes.hpp:51
Options for aggregate_across_genes().
Definition aggregate_across_genes.hpp:26
bool average
Definition aggregate_across_genes.hpp:37
int num_threads
Definition aggregate_across_genes.hpp:31
Results of aggregate_across_genes().
Definition aggregate_across_genes.hpp:59
std::vector< std::vector< Sum_ > > sum
Definition aggregate_across_genes.hpp:65