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.get_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 internal::annotate_cells_single(
142 test,
143 trained.get_subset(),
144 trained.get_references(),
145 trained.get_markers(),
146 options.quantile,
147 options.fine_tune,
148 options.fine_tune_threshold,
149 buffers.best,
150 buffers.scores,
151 buffers.delta,
152 options.num_threads
153 );
154}
155
172template<typename Value_, typename Index_, typename Float_, typename Label_>
174 const tatami::Matrix<Value_, Index_>& test,
177 const ClassifySingleOptions<Float_>& options)
178{
179 if (trained.get_test_nrow() != static_cast<Index_>(-1) && trained.get_test_nrow() != test.nrow()) {
180 throw std::runtime_error("number of rows in 'test' is not the same as that used to build 'trained'");
181 }
182 internal::annotate_cells_single(
183 test,
184 trained.get_test_subset(),
185 trained.get_references(),
186 trained.get_markers(),
187 options.quantile,
188 options.fine_tune,
189 options.fine_tune_threshold,
190 buffers.best,
191 buffers.scores,
192 buffers.delta,
193 options.num_threads
194 );
195}
196
202template<typename Label_ = DefaultLabel, typename Float_ = DefaultFloat>
207 ClassifySingleResults(std::size_t num_cells, std::size_t num_labels) : best(num_cells), delta(num_cells) {
208 scores.reserve(num_labels);
209 for (decltype(num_labels) l = 0; l < num_labels; ++l) {
210 scores.emplace_back(num_cells);
211 }
212 }
221 std::vector<Label_> best;
222
228 std::vector<std::vector<Float_> > scores;
229
234 std::vector<Float_> delta;
235};
236
240namespace internal {
241
242template<typename Label_, typename Float_>
245 output.best = results.best.data();
246 output.delta = results.delta.data();
247 output.scores.reserve(results.scores.size());
248 for (auto& s : results.scores) {
249 output.scores.emplace_back(s.data());
250 }
251 return output;
252}
253
254}
274template<typename Label_ = DefaultLabel, typename Value_, typename Index_, typename Float_>
276 const tatami::Matrix<Value_, Index_>& test,
277 const TrainedSingle<Index_, Float_>& trained,
278 const ClassifySingleOptions<Float_>& options)
279{
280 ClassifySingleResults<Label_, Float_> output(test.ncol(), trained.get_references().size());
281 auto buffers = internal::results_to_buffers(output);
282 classify_single(test, trained, buffers, options);
283 return output;
284}
285
301template<typename Label_ = DefaultLabel, typename Value_, typename Index_, typename Float_>
303 const tatami::Matrix<Value_, Index_>& test,
305 const ClassifySingleOptions<Float_>& options)
306{
307 ClassifySingleResults<Label_, Float_> output(test.ncol(), trained.get_references().size());
308 auto buffers = internal::results_to_buffers(output);
309 classify_single_intersect(test, trained, buffers, options);
310 return output;
311}
312
313}
314
315#endif
Classifier built from an intersection of genes.
Definition train_single.hpp:218
Index_ get_test_nrow() const
Definition train_single.hpp:250
const Markers< Index_ > & get_markers() const
Definition train_single.hpp:260
const std::vector< Index_ > & get_test_subset() const
Definition train_single.hpp:269
Classifier trained from a single reference.
Definition train_single.hpp:89
const std::vector< Index_ > & get_subset() const
Definition train_single.hpp:136
Index_ get_test_nrow() const
Definition train_single.hpp:118
const Markers< Index_ > & get_markers() const
Definition train_single.hpp:128
Common definitions for singlepp.
Cell type classification using the SingleR algorithm in C++.
Definition classify_single.hpp:20
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:173
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_intersect().
Definition classify_single.hpp:203
std::vector< Float_ > delta
Definition classify_single.hpp:234
std::vector< Label_ > best
Definition classify_single.hpp:221
std::vector< std::vector< Float_ > > scores
Definition classify_single.hpp:228
Train a classifier from a single reference.