singlepp
A C++ library for cell type classification
Loading...
Searching...
No Matches
classify_single.hpp
Go to the documentation of this file.
1#ifndef SINGLEPP_CLASSIFY_SINGLE_HPP
2#define SINGLEPP_CLASSIFY_SINGLE_HPP
3
4#include "defs.hpp"
5
6#include "tatami/tatami.hpp"
7
8#include "annotate_cells_single.hpp"
9#include "train_single.hpp"
10
11#include <vector>
12#include <cstddef>
13#include <stdexcept>
14
20namespace singlepp {
21
26template<typename Float_ = DefaultFloat>
36 Float_ quantile = 0.8;
37
45 Float_ fine_tune_threshold = 0.05;
46
51 bool fine_tune = true;
52
57 int num_threads = 1;
58};
59
65template<typename Label_ = DefaultLabel, typename Float_ = DefaultFloat>
71 Label_* best;
72
79 std::vector<Float_*> scores;
80
86 Float_* delta;
87};
88
132template<typename Value_, typename Index_, typename Float_, typename Label_>
134 const tatami::Matrix<Value_, Index_>& test,
135 const TrainedSingle<Index_, Float_>& trained,
137 const ClassifySingleOptions<Float_>& options)
138{
139 if (trained.test_nrow() != test.nrow()) {
140 throw std::runtime_error("number of rows in 'test' is not the same as that used to build 'trained'");
141 }
142 annotate_cells_single(
143 test,
144 trained,
145 options.quantile,
146 options.fine_tune,
147 options.fine_tune_threshold,
148 buffers.best,
149 buffers.scores,
150 buffers.delta,
151 options.num_threads
152 );
153}
154
160template<typename Label_ = DefaultLabel, typename Float_ = DefaultFloat>
165 ClassifySingleResults(const std::size_t num_cells, const std::size_t num_labels) :
166 best(sanisizer::cast<I<decltype(best.size())> >(num_cells)),
167 delta(sanisizer::cast<I<decltype(delta.size())> >(num_cells))
168 {
169 scores.reserve(num_labels);
170 for (I<decltype(num_labels)> l = 0; l < num_labels; ++l) {
171 scores.emplace_back(num_cells);
172 }
173 }
182 std::vector<Label_> best;
183
189 std::vector<std::vector<Float_> > scores;
190
195 std::vector<Float_> delta;
196};
197
213template<typename Label_ = DefaultLabel, typename Value_, typename Index_, typename Float_>
215 const tatami::Matrix<Value_, Index_>& test,
216 const TrainedSingle<Index_, Float_>& trained,
217 const ClassifySingleOptions<Float_>& options)
218{
219 ClassifySingleResults<Label_, Float_> output(test.ncol(), trained.num_labels());
220
222 buffers.best = output.best.data();
223 buffers.delta = output.delta.data();
224 buffers.scores.reserve(output.scores.size());
225 for (auto& s : output.scores) {
226 buffers.scores.emplace_back(s.data());
227 }
228
229 classify_single(test, trained, buffers, options);
230 return output;
231}
232
233}
234
235#endif
Classifier trained from a single reference.
Definition train_single.hpp:85
Index_ test_nrow() const
Definition train_single.hpp:115
std::size_t num_labels() const
Definition train_single.hpp:141
Common definitions for singlepp.
Cell type classification using the SingleR algorithm in C++.
Definition classify_single.hpp:20
void classify_single(const tatami::Matrix< Value_, Index_ > &test, const TrainedSingle< Index_, Float_ > &trained, const ClassifySingleBuffers< Label_, Float_ > &buffers, const ClassifySingleOptions< Float_ > &options)
Implements the SingleR algorithm for automated annotation of single-cell RNA-seq data.
Definition classify_single.hpp:133
Output buffers for classify_single().
Definition classify_single.hpp:66
Float_ * delta
Definition classify_single.hpp:86
Label_ * best
Definition classify_single.hpp:71
std::vector< Float_ * > scores
Definition classify_single.hpp:79
Options for classify_single() and friends.
Definition classify_single.hpp:27
bool fine_tune
Definition classify_single.hpp:51
Float_ fine_tune_threshold
Definition classify_single.hpp:45
int num_threads
Definition classify_single.hpp:57
Float_ quantile
Definition classify_single.hpp:36
Results of classify_single() and classify_single().
Definition classify_single.hpp:161
std::vector< Float_ > delta
Definition classify_single.hpp:195
std::vector< Label_ > best
Definition classify_single.hpp:182
std::vector< std::vector< Float_ > > scores
Definition classify_single.hpp:189
Train a classifier from a single reference.