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 <stdexcept>
13
19namespace singlepp {
20
25template<typename Float_ = DefaultFloat>
35
44
49 bool fine_tune = true;
50
55 int num_threads = 1;
56};
57
63template<typename Label_ = DefaultLabel, typename Float_ = DefaultFloat>
70
77 std::vector<Float_*> scores;
78
85};
86
130template<typename Value_, typename Index_, typename Float_, typename Label_>
132 const tatami::Matrix<Value_, Index_>& test,
136{
137 if (trained.get_test_nrow() != test.nrow()) {
138 throw std::runtime_error("number of rows in 'test' is not the same as that used to build 'trained'");
139 }
140 internal::annotate_cells_single(
141 test,
142 trained.get_subset(),
143 trained.get_references(),
144 trained.get_markers(),
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
171template<typename Value_, typename Index_, typename Float_, typename Label_>
173 const tatami::Matrix<Value_, Index_>& test,
177{
178 if (trained.get_test_nrow() != static_cast<Index_>(-1) && trained.get_test_nrow() != test.nrow()) {
179 throw std::runtime_error("number of rows in 'test' is not the same as that used to build 'trained'");
180 }
181 internal::annotate_cells_single(
182 test,
183 trained.get_test_subset(),
184 trained.get_references(),
185 trained.get_markers(),
186 options.quantile,
187 options.fine_tune,
188 options.fine_tune_threshold,
189 buffers.best,
190 buffers.scores,
191 buffers.delta,
192 options.num_threads
193 );
194}
195
201template<typename Label_ = DefaultLabel, typename Float_ = DefaultFloat>
206 ClassifySingleResults(size_t num_cells, size_t num_labels) : best(num_cells), delta(num_cells) {
207 scores.reserve(num_labels);
208 for (size_t l = 0; l < num_labels; ++l) {
209 scores.emplace_back(num_cells);
210 }
211 }
220 std::vector<Label_> best;
221
227 std::vector<std::vector<Float_> > scores;
228
233 std::vector<Float_> delta;
234};
235
239namespace internal {
240
241template<typename Label_, typename Float_>
244 output.best = results.best.data();
245 output.delta = results.delta.data();
246 output.scores.reserve(results.scores.size());
247 for (auto& s : results.scores) {
248 output.scores.emplace_back(s.data());
249 }
250 return output;
251}
252
253}
273template<typename Label_ = DefaultLabel, typename Value_, typename Index_, typename Float_>
275 const tatami::Matrix<Value_, Index_>& test,
278{
279 ClassifySingleResults<Label_, Float_> output(test.ncol(), trained.get_references().size());
280 auto buffers = internal::results_to_buffers(output);
282 return output;
283}
284
300template<typename Label_ = DefaultLabel, typename Value_, typename Index_, typename Float_>
302 const tatami::Matrix<Value_, Index_>& test,
305{
306 ClassifySingleResults<Label_, Float_> output(test.ncol(), trained.get_references().size());
307 auto buffers = internal::results_to_buffers(output);
309 return output;
310}
311
312}
313
314#endif
Common definitions for singlepp.
Cell type classification using the SingleR algorithm in C++.
Definition classify_single.hpp:19
std::vector< std::vector< std::vector< Index_ > > > Markers
Definition Markers.hpp:40
void classify_single_intersect(const tatami::Matrix< Value_, Index_ > &test, const TrainedSingleIntersect< Index_, Float_ > &trained, const ClassifySingleBuffers< Label_, Float_ > &buffers, const ClassifySingleOptions< Float_ > &options)
Definition classify_single.hpp:172
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:131
Output buffers for classify_single().
Definition classify_single.hpp:64
Float_ * delta
Definition classify_single.hpp:84
Label_ * best
Definition classify_single.hpp:69
std::vector< Float_ * > scores
Definition classify_single.hpp:77
Options for classify_single() and friends.
Definition classify_single.hpp:26
bool fine_tune
Definition classify_single.hpp:49
Float_ fine_tune_threshold
Definition classify_single.hpp:43
int num_threads
Definition classify_single.hpp:55
Float_ quantile
Definition classify_single.hpp:34
Results of classify_single() and classify_single_intersect().
Definition classify_single.hpp:202
std::vector< Float_ > delta
Definition classify_single.hpp:233
std::vector< Label_ > best
Definition classify_single.hpp:220
std::vector< std::vector< Float_ > > scores
Definition classify_single.hpp:227
Train a classifier from a single reference.