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
50template<typename Value_, typename Index_, typename Block_, typename Float_>
51irlba::Metrics compute_blocked(const tatami::Matrix<Value_, Index_>& matrix, const Block_* const block, const Options& options, const Buffers<Float_>& output) {
52 if (check_edge_cases(matrix, options.rank, output)) {
53 irlba::Metrics metrics;
54 metrics.converged = true;
55 return metrics;
56 }
57
58 scran_pca::BlockedPcaOptions bopt;
59 bopt.number = options.rank;
60 bopt.scale = options.scale;
61 bopt.block_weight_policy = options.block_weight_policy;
62 bopt.variable_block_weight_parameters = options.variable_block_weight_parameters;
63 bopt.realize_matrix = options.realize_matrix;
64 bopt.num_threads = options.num_threads;
65 bopt.irlba_options = options.irlba_options;
66 bopt.center_scores_by_block = true; // we'll add them back in afterwards.
67 const auto res = scran_pca::blocked_pca(matrix, block, bopt);
68
69 // Here, we restore the block-specific centers.
70 static_assert(!Eigen::MatrixXd::IsRowMajor); // just double-checking...
71 const auto nfeat = res.center.cols();
72 const auto nblocks = res.center.rows();
73 auto block_means = sanisizer::create<std::vector<Float_> >(nblocks);
74
75 for (I<decltype(nfeat)> f = 0; f < nfeat; ++f) {
76 for (I<decltype(nblocks)> b = 0; b < nblocks; ++b) {
77 block_means[b] += res.center.coeff(b, f);
78 }
79 }
80 for (auto& b : block_means) {
81 b /= nfeat;
82 }
83
84 const auto ncells = res.components.cols();
85 for (I<decltype(ncells)> c = 0; c < ncells; ++c) {
86 output.scores[c] = block_means[block[c]];
87 }
88 process_output(res.rotation, res.components, options.scale, res.scale, output);
89
90 return res.metrics;
91}
92
110template<typename Float_ = double, typename Value_, typename Index_, typename Block_>
111Results<Float_> compute_blocked(const tatami::Matrix<Value_, Index_>& matrix, const Block_* const block, const Options& options) {
112 Results<Float_> output;
113 sanisizer::resize(output.weights, matrix.nrow()
114#ifdef SCRAN_QC_TEST_INIT
115 , SCRAN_QC_TEST_INIT
116#endif
117 );
118 sanisizer::resize(output.scores, matrix.ncol()
119#ifdef SCRAN_QC_TEST_INIT
120 , SCRAN_QC_TEST_INIT
121#endif
122 );
123
124 Buffers<Float_> buffers;
125 buffers.weights = output.weights.data();
126 buffers.scores = output.scores.data();
127
128 output.metrics = compute_blocked(matrix, block, options, buffers);
129 return output;
130}
131
132}
133
134#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
irlba::Metrics compute_blocked(const tatami::Matrix< Value_, Index_ > &matrix, const Block_ *const block, const Options &options, const Buffers< Float_ > &output)
Definition blocked.hpp:51
Buffers for the results of compute() and compute_blocked().
Definition Results.hpp:20
Float_ * scores
Definition Results.hpp:25
Float_ * weights
Definition Results.hpp:31
Options for compute() and compute_blocked().
Definition Options.hpp:18
bool realize_matrix
Definition Options.hpp:64
int rank
Definition Options.hpp:34
scran_blocks::WeightPolicy block_weight_policy
Definition Options.hpp:46
scran_blocks::VariableWeightParameters variable_block_weight_parameters
Definition Options.hpp:52
irlba::Options< Eigen::VectorXd > irlba_options
Definition Options.hpp:69
int num_threads
Definition Options.hpp:58
bool scale
Definition Options.hpp:41
Results of compute() and compute_blocked().
Definition Results.hpp:39
std::vector< Float_ > weights
Definition Results.hpp:51
std::vector< Float_ > scores
Definition Results.hpp:44
irlba::Metrics metrics
Definition Results.hpp:56