gsdecon
C++ port of the GSDecon algorithm
Loading...
Searching...
No Matches
blocked.hpp
Go to the documentation of this file.
1#ifndef GSDECON_BLOCKED_HPP
2#define GSDECON_BLOCKED_HPP
3
4#include <vector>
5
6#include "Eigen/Dense"
7#include "tatami/tatami.hpp"
8#include "irlba/irlba.hpp"
9#include "scran_pca/scran_pca.hpp"
10#include "sanisizer/sanisizer.hpp"
11
12#include "Options.hpp"
13#include "Results.hpp"
14#include "utils.hpp"
15
21namespace gsdecon {
22
48template<typename Value_, typename Index_, typename Block_, typename Float_>
49void compute_blocked(const tatami::Matrix<Value_, Index_>& matrix, const Block_* const block, const Options& options, const Buffers<Float_>& output) {
50 if (internal::check_edge_cases(matrix, options.rank, output)) {
51 return;
52 }
53
54 scran_pca::BlockedPcaOptions bopt;
55 bopt.number = options.rank;
56 bopt.scale = options.scale;
57 bopt.block_weight_policy = options.block_weight_policy;
58 bopt.variable_block_weight_parameters = options.variable_block_weight_parameters;
59 bopt.realize_matrix = options.realize_matrix;
60 bopt.num_threads = options.num_threads;
61 bopt.irlba_options = options.irlba_options;
62 const auto res = scran_pca::blocked_pca(matrix, block, bopt);
63
64 // Here, we restore the block-specific centers.
65 static_assert(!Eigen::MatrixXd::IsRowMajor); // just double-checking...
66 const auto nfeat = res.center.cols();
67 const auto nblocks = res.center.rows();
68 auto block_means = sanisizer::create<std::vector<Float_> >(nblocks);
69
70 for (decltype(I(nfeat)) f = 0; f < nfeat; ++f) {
71 for (decltype(I(nblocks)) b = 0; b < nblocks; ++b) {
72 block_means[b] += res.center.coeff(b, f);
73 }
74 }
75 for (auto& b : block_means) {
76 b /= nfeat;
77 }
78
79 const auto ncells = res.components.cols();
80 for (decltype(I(ncells)) c = 0; c < ncells; ++c) {
81 output.scores[c] = block_means[block[c]];
82 }
83 internal::process_output(res.rotation, res.components, options.scale, res.scale, output);
84}
85
103template<typename Float_ = double, typename Value_, typename Index_, typename Block_>
104Results<Float_> compute_blocked(const tatami::Matrix<Value_, Index_>& matrix, const Block_* const block, const Options& options) {
105 Results<Float_> output;
106 sanisizer::resize(output.weights, matrix.nrow()
107#ifdef SCRAN_QC_TEST_INIT
108 , SCRAN_QC_TEST_INIT
109#endif
110 );
111 sanisizer::resize(output.scores, matrix.ncol()
112#ifdef SCRAN_QC_TEST_INIT
113 , SCRAN_QC_TEST_INIT
114#endif
115 );
116
117 Buffers<Float_> buffers;
118 buffers.weights = output.weights.data();
119 buffers.scores = output.scores.data();
120
121 compute_blocked(matrix, block, options, buffers);
122 return output;
123}
124
125}
126
127#endif
Options for the gsdecon algorithm.
Classes for storing the results.
virtual Index_ ncol() const=0
virtual Index_ nrow() const=0
Gene set scoring with gsdecon.
Definition blocked.hpp:21
void compute_blocked(const tatami::Matrix< Value_, Index_ > &matrix, const Block_ *const block, const Options &options, const Buffers< Float_ > &output)
Definition blocked.hpp:49
Buffers for the results of compute() and compute_blocked().
Definition Results.hpp:18
Float_ * scores
Definition Results.hpp:23
Float_ * weights
Definition Results.hpp:29
Options for compute() and compute_blocked().
Definition Options.hpp:17
bool realize_matrix
Definition Options.hpp:63
int rank
Definition Options.hpp:33
scran_blocks::WeightPolicy block_weight_policy
Definition Options.hpp:45
scran_blocks::VariableWeightParameters variable_block_weight_parameters
Definition Options.hpp:51
int num_threads
Definition Options.hpp:57
irlba::Options irlba_options
Definition Options.hpp:68
bool scale
Definition Options.hpp:40
Results of compute() and compute_blocked().
Definition Results.hpp:37
std::vector< Float_ > weights
Definition Results.hpp:49
std::vector< Float_ > scores
Definition Results.hpp:42