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>
35 Float_ quantile = 0.8;
36
44 Float_ fine_tune_threshold = 0.05;
45
50 bool fine_tune = true;
51
56 int num_threads = 1;
57};
58
64template<typename Label_ = DefaultLabel, typename Float_ = DefaultFloat>
70 Label_* best;
71
78 std::vector<Float_*> scores;
79
85 Float_* delta;
86};
87
131template<typename Value_, typename Index_, typename Float_, typename Label_>
133 const tatami::Matrix<Value_, Index_>& test,
134 const TrainedSingle<Index_, Float_>& trained,
136 const ClassifySingleOptions<Float_>& options)
137{
138 if (trained.test_nrow() != test.nrow()) {
139 throw std::runtime_error("number of rows in 'test' is not the same as that used to build 'trained'");
140 }
141 annotate_cells_single(
142 test,
143 trained,
144 options.quantile,
145 options.fine_tune,
146 options.fine_tune_threshold,
147 buffers.best,
148 buffers.scores,
149 buffers.delta,
150 options.num_threads
151 );
152}
153
159template<typename Label_ = DefaultLabel, typename Float_ = DefaultFloat>
164 ClassifySingleResults(std::size_t num_cells, std::size_t num_labels) : best(num_cells), delta(num_cells) {
165 scores.reserve(num_labels);
166 for (decltype(num_labels) l = 0; l < num_labels; ++l) {
167 scores.emplace_back(num_cells);
168 }
169 }
178 std::vector<Label_> best;
179
185 std::vector<std::vector<Float_> > scores;
186
191 std::vector<Float_> delta;
192};
193
209template<typename Label_ = DefaultLabel, typename Value_, typename Index_, typename Float_>
211 const tatami::Matrix<Value_, Index_>& test,
212 const TrainedSingle<Index_, Float_>& trained,
213 const ClassifySingleOptions<Float_>& options)
214{
215 ClassifySingleResults<Label_, Float_> output(test.ncol(), trained.num_labels());
216
218 buffers.best = output.best.data();
219 buffers.delta = output.delta.data();
220 buffers.scores.reserve(output.scores.size());
221 for (auto& s : output.scores) {
222 buffers.scores.emplace_back(s.data());
223 }
224
225 classify_single(test, trained, buffers, options);
226 return output;
227}
228
229}
230
231#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:132
Output buffers for classify_single().
Definition classify_single.hpp:65
Float_ * delta
Definition classify_single.hpp:85
Label_ * best
Definition classify_single.hpp:70
std::vector< Float_ * > scores
Definition classify_single.hpp:78
Options for classify_single() and friends.
Definition classify_single.hpp:27
bool fine_tune
Definition classify_single.hpp:50
Float_ fine_tune_threshold
Definition classify_single.hpp:44
int num_threads
Definition classify_single.hpp:56
Float_ quantile
Definition classify_single.hpp:35
Results of classify_single() and classify_single().
Definition classify_single.hpp:160
std::vector< Float_ > delta
Definition classify_single.hpp:191
std::vector< Label_ > best
Definition classify_single.hpp:178
std::vector< std::vector< Float_ > > scores
Definition classify_single.hpp:185
Train a classifier from a single reference.