singlepp_loaders
Load pre-processed reference datasets for SingleR
Loading...
Searching...
No Matches
labels.hpp
Go to the documentation of this file.
1#ifndef SINGLEPP_LOADERS_LABELS_HPP
2#define SINGLEPP_LOADERS_LABELS_HPP
3
4#include "byteme/PerByte.hpp"
5#include "byteme/RawFileReader.hpp"
6#include "byteme/GzipFileReader.hpp"
7#include "byteme/ZlibBufferReader.hpp"
8
9#include "tatami/tatami.hpp"
10#include "singlepp/singlepp.hpp"
11
12#include <string>
13#include <vector>
14#include <cctype>
15#include <type_traits>
16#include <stdexcept>
17
24
32 bool parallel = false;
33
37 size_t buffer_size = 65536;
38};
39
43namespace internal {
44
45template<typename Label_>
46std::vector<Label_> load_labels(byteme::Reader& reader, bool parallel) {
47 bool non_empty = false;
48 int current = 0;
49 std::vector<Label_> labels;
50
51 std::unique_ptr<byteme::PerByteInterface<char> > pbptr;
52 if (parallel) {
53 pbptr.reset(new byteme::PerByteParallel<char, byteme::Reader*>(&reader));
54 } else {
55 pbptr.reset(new byteme::PerByteSerial<char, byteme::Reader*>(&reader));
56 }
57 auto& pb = *pbptr;
58
59 bool okay = pb.valid();
60 while (okay) {
61 char x = pb.get();
62 okay = pb.advance();
63
64 if (x == '\n') {
65 if (!non_empty) {
66 throw std::runtime_error("label index must be an integer");
67 }
68 labels.push_back(current);
69 current = 0;
70 non_empty = false;
71 } else if (std::isdigit(x)) {
72 non_empty = true;
73 current *= 10;
74 current += (x - '0');
75 } else {
76 throw std::runtime_error("label index must be an integer");
77 }
78 }
79
80 if (non_empty) {
81 labels.push_back(current);
82 }
83
84 return labels;
85}
86
87}
105template<typename Label_ = singlepp::DefaultLabel>
106std::vector<Label_> load_labels_from_text_file(const char* path, const LoadLabelsOptions& options) {
107 byteme::RawFileReaderOptions read_opt;
108 read_opt.buffer_size = options.buffer_size;
109 byteme::RawFileReader reader(path, read_opt);
110 return internal::load_labels<Label_>(reader, options.parallel);
111}
112
123template<typename Label_ = singlepp::DefaultLabel>
124std::vector<Label_> load_labels_from_gzip_file(const char* path, const LoadLabelsOptions& options) {
125 byteme::GzipFileReaderOptions read_opt;
126 read_opt.buffer_size = options.buffer_size;
127 byteme::GzipFileReader reader(path, read_opt);
128 return internal::load_labels<Label_>(reader, options.parallel);
129}
130
142template<typename Label_ = singlepp::DefaultLabel>
143std::vector<Label_> load_labels_from_zlib_buffer(const unsigned char* buffer, size_t len, const LoadLabelsOptions& options) {
144 byteme::ZlibBufferReaderOptions read_opt;
145 read_opt.mode = 3;
146 read_opt.buffer_size = options.buffer_size;
147 byteme::ZlibBufferReader reader(buffer, len, read_opt);
148 return internal::load_labels<Label_>(reader, options.parallel);
149}
150
151}
152
153#endif
Load pre-processed single reference datasets.
Definition labels.hpp:23
std::vector< Label_ > load_labels_from_gzip_file(const char *path, const LoadLabelsOptions &options)
Definition labels.hpp:124
std::vector< Label_ > load_labels_from_text_file(const char *path, const LoadLabelsOptions &options)
Definition labels.hpp:106
std::vector< Label_ > load_labels_from_zlib_buffer(const unsigned char *buffer, size_t len, const LoadLabelsOptions &options)
Definition labels.hpp:143
Options for loading labels.
Definition labels.hpp:28
size_t buffer_size
Definition labels.hpp:37
bool parallel
Definition labels.hpp:32