1#ifndef SCRAN_PAIRWISE_EFFECTS_HPP
2#define SCRAN_PAIRWISE_EFFECTS_HPP
4#include "../utils/macros.hpp"
6#include "MatrixCalculator.hpp"
8#include "simple_diff.hpp"
10#include "../utils/vector_to_pointers.hpp"
11#include "../utils/blocking.hpp"
12#include "../utils/average_vectors.hpp"
14#include "tatami/tatami.hpp"
216 do_delta_detected = c;
226 block_weight_policy = w;
237 variable_block_weight_parameters = v;
270 template<
typename Data_,
typename Index_,
typename Group_,
typename Stat_>
272 const tatami::Matrix<Data_, Index_>* p,
274 std::vector<Stat_*> means,
275 std::vector<Stat_*> detected,
279 Stat_* delta_detected)
281 int ngroups = means.size();
282 differential_analysis::MatrixCalculator runner(nthreads, threshold, block_weight_policy, variable_block_weight_parameters);
283 Overlord overlord(auc);
284 auto state = runner.run(p, group, ngroups, overlord);
285 process_simple_effects(p->nrow(), ngroups, 1, state, means, detected, cohen, lfc, delta_detected);
320 template<
typename Data_,
typename Index_,
typename Group_,
typename Block_,
typename Stat_>
322 const tatami::Matrix<Data_, Index_>* p,
325 std::vector<Stat_*> means,
326 std::vector<Stat_*> detected,
330 Stat_* delta_detected)
333 run(p, group, std::move(means), std::move(detected), cohen, auc, lfc, delta_detected);
336 auto ngenes = p->nrow();
337 int ngroups = means.size();
338 int nblocks =
count_ids(p->ncol(), block);
340 int ncombos = ngroups * nblocks;
341 std::vector<std::vector<Stat_> > means_store(ncombos), detected_store(ncombos);
342 std::vector<Stat_*> means2(ncombos), detected2(ncombos);
343 for (
int c = 0; c < ncombos; ++c) {
344 means_store[c].resize(ngenes);
345 detected_store[c].resize(ngenes);
346 means2[c] = means_store[c].data();
347 detected2[c] = detected_store[c].data();
350 differential_analysis::MatrixCalculator runner(nthreads, threshold, block_weight_policy, variable_block_weight_parameters);
351 Overlord overlord(auc);
352 auto state = runner.run_blocked(p, group, ngroups, block, nblocks, overlord);
353 process_simple_effects(p->nrow(), ngroups, nblocks, state, means2, detected2, cohen, lfc, delta_detected);
356 std::vector<double> weights(nblocks);
357 std::vector<Stat_*> mstats(nblocks), dstats(nblocks);
359 for (
int gr = 0; gr < ngroups; ++gr) {
360 for (
int b = 0; b < nblocks; ++b) {
361 size_t offset = gr *
static_cast<size_t>(nblocks) + b;
362 weights[b] = state.level_weight[offset];
363 mstats[b] = means2[offset];
364 dstats[b] = detected2[offset];
374 template<
typename Stat_>
376 Overlord(Stat_* auc_) : auc(auc_) {}
378 bool needs_auc()
const {
384 Stat_* prepare_auc_buffer(
size_t gene,
size_t ngroups) {
385 return auc + gene * ngroups * ngroups;
389 template<
typename Stat_>
390 void process_simple_effects(
394 const differential_analysis::MatrixCalculator::State& state,
395 std::vector<Stat_*>& means,
396 std::vector<Stat_*>& detected,
399 Stat_* delta_detected)
401 const auto& level_size = state.level_size;
402 size_t nlevels = level_size.size();
404 tatami::parallelize([&](
size_t,
size_t start,
size_t length) ->
void {
405 auto in_offset = nlevels * start;
406 const auto* tmp_means = state.means.data() + in_offset;
407 const auto* tmp_variances = state.variances.data() + in_offset;
408 const auto* tmp_detected = state.detected.data() + in_offset;
410 size_t squared = ngroups * ngroups;
411 size_t out_offset = start * squared;
412 for (
size_t gene = start, end = start + length; gene < end; ++gene, out_offset += squared) {
413 for (
size_t l = 0; l < nlevels; ++l) {
414 means[l][gene] = tmp_means[l];
415 detected[l][gene] = tmp_detected[l];
419 differential_analysis::compute_pairwise_cohens_d(tmp_means, tmp_variances, state.level_weight, ngroups, nblocks, threshold, cohen + out_offset);
422 if (delta_detected != NULL) {
423 differential_analysis::compute_pairwise_simple_diff(tmp_detected, state.level_weight, ngroups, nblocks, delta_detected + out_offset);
427 differential_analysis::compute_pairwise_simple_diff(tmp_means, state.level_weight, ngroups, nblocks, lfc + out_offset);
430 tmp_means += nlevels;
431 tmp_variances += nlevels;
432 tmp_detected += nlevels;
434 }, ngenes, nthreads);
449 template<
typename Stat_>
454 Results(
size_t ngenes,
size_t ngroups,
bool do_cohen,
bool do_auc,
bool do_lfc,
bool do_delta) :
455 means(ngroups, std::vector<Stat_>(ngenes)),
456 detected(ngroups, std::vector<Stat_>(ngenes))
458 size_t nelements = ngenes * ngroups * ngroups;
460 cohen.resize(nelements);
463 auc.resize(nelements);
466 lfc.resize(nelements);
505 std::vector<std::vector<Stat_> >
means;
529 template<
typename Stat_ =
double,
typename Data_ =
double,
typename Index_ =
int,
typename Group_ =
int>
531 size_t ngroups =
count_ids(p->ncol(), group);
532 Results<Stat_> res(p->nrow(), ngroups, do_cohen, do_auc, do_lfc, do_delta_detected);
538 harvest_pointer(res.
cohen, do_cohen),
539 harvest_pointer(res.
auc, do_auc),
540 harvest_pointer(res.
lfc, do_lfc),
563 template<
typename Stat_ =
double,
typename Data_ =
double,
typename Index_ =
int,
typename Group_ =
int,
typename Block_ =
int>
565 size_t ngroups =
count_ids(p->ncol(), group);
566 Results<Stat_> res(p->nrow(), ngroups, do_cohen, do_auc, do_lfc, do_delta_detected);
573 harvest_pointer(res.
cohen, do_cohen),
574 harvest_pointer(res.
auc, do_auc),
575 harvest_pointer(res.
lfc, do_lfc),
582 template<
typename Ptr>
583 static std::vector<Ptr> fetch_first(
const std::vector<std::vector<Ptr> >& input) {
584 std::vector<Ptr> output;
585 output.reserve(input.size());
586 for (
const auto& i : input) {
587 output.push_back(i.front());
592 template<
typename Stat>
593 static Stat* harvest_pointer(std::vector<Stat>& source,
bool use) {
595 return source.data();
597 return static_cast<Stat*
>(NULL);
Compute pairwise effect size between groups of cells.
Definition PairwiseEffects.hpp:89
PairwiseEffects & set_compute_auc(bool c=Defaults::compute_auc)
Definition PairwiseEffects.hpp:189
PairwiseEffects & set_variable_block_weight_parameters(VariableBlockWeightParameters v=Defaults::variable_block_weight_parameters)
Definition PairwiseEffects.hpp:236
Results< Stat_ > run_blocked(const tatami::Matrix< Data_, Index_ > *p, const Group_ *group, const Block_ *block)
Definition PairwiseEffects.hpp:564
PairwiseEffects & set_threshold(double t=Defaults::threshold)
Definition PairwiseEffects.hpp:154
Results< Stat_ > run(const tatami::Matrix< Data_, Index_ > *p, const Group_ *group)
Definition PairwiseEffects.hpp:530
void run(const tatami::Matrix< Data_, Index_ > *p, const Group_ *group, std::vector< Stat_ * > means, std::vector< Stat_ * > detected, Stat_ *cohen, Stat_ *auc, Stat_ *lfc, Stat_ *delta_detected) const
Definition PairwiseEffects.hpp:271
PairwiseEffects & set_compute_lfc(bool c=Defaults::compute_lfc)
Definition PairwiseEffects.hpp:202
PairwiseEffects & set_compute_cohen(bool c=Defaults::compute_cohen)
Definition PairwiseEffects.hpp:176
PairwiseEffects & set_num_threads(int n=Defaults::num_threads)
Definition PairwiseEffects.hpp:163
PairwiseEffects & set_compute_delta_detected(bool c=Defaults::compute_delta_detected)
Definition PairwiseEffects.hpp:215
PairwiseEffects & set_block_weight_policy(WeightPolicy w=Defaults::block_weight_policy)
Definition PairwiseEffects.hpp:225
void run_blocked(const tatami::Matrix< Data_, Index_ > *p, const Group_ *group, const Block_ *block, std::vector< Stat_ * > means, std::vector< Stat_ * > detected, Stat_ *cohen, Stat_ *auc, Stat_ *lfc, Stat_ *delta_detected) const
Definition PairwiseEffects.hpp:321
Functions for single-cell RNA-seq analyses.
Definition AggregateAcrossCells.hpp:18
size_t count_ids(size_t length, const Id_ *ids)
Definition blocking.hpp:29
void average_vectors_weighted(size_t n, std::vector< Stat_ * > in, const Weight_ *w, Output_ *out)
Definition average_vectors.hpp:158
std::vector< T * > vector_to_pointers(std::vector< std::vector< T > > &input)
Definition vector_to_pointers.hpp:26
WeightPolicy
Definition blocking.hpp:82
Default parameter settings.
Definition PairwiseEffects.hpp:94
static constexpr int num_threads
Definition PairwiseEffects.hpp:103
static constexpr double threshold
Definition PairwiseEffects.hpp:98
static constexpr bool compute_cohen
Definition PairwiseEffects.hpp:108
static constexpr bool compute_auc
Definition PairwiseEffects.hpp:113
static constexpr WeightPolicy block_weight_policy
Definition PairwiseEffects.hpp:128
static constexpr VariableBlockWeightParameters variable_block_weight_parameters
Definition PairwiseEffects.hpp:133
static constexpr bool compute_lfc
Definition PairwiseEffects.hpp:118
static constexpr bool compute_delta_detected
Definition PairwiseEffects.hpp:123
Pairwise effect size results.
Definition PairwiseEffects.hpp:450
std::vector< Stat_ > auc
Definition PairwiseEffects.hpp:486
std::vector< Stat_ > delta_detected
Definition PairwiseEffects.hpp:498
std::vector< std::vector< Stat_ > > detected
Definition PairwiseEffects.hpp:512
std::vector< std::vector< Stat_ > > means
Definition PairwiseEffects.hpp:505
std::vector< Stat_ > cohen
Definition PairwiseEffects.hpp:480
std::vector< Stat_ > lfc
Definition PairwiseEffects.hpp:492
Parameters for variable_block_weight().
Definition blocking.hpp:87