kaori
A C++ library for barcode extraction and matching
Loading...
Searching...
No Matches
SingleBarcodePairedEnd.hpp
Go to the documentation of this file.
1#ifndef KAORI_SINGLE_BARCODE_PAIRED_END_HPP
2#define KAORI_SINGLE_BARCODE_PAIRED_END_HPP
3
4#include "../SimpleSingleMatch.hpp"
5#include <vector>
6
13namespace kaori {
14
24template<size_t max_size>
26public:
30 struct Options {
35 bool use_first = true;
36
41
45 SearchStrand strand = SearchStrand::FORWARD;
46
50 DuplicateAction duplicates = DuplicateAction::ERROR;
51 };
52
53public:
62 SingleBarcodePairedEnd(const char* template_seq, size_t template_length, const BarcodePool& barcode_pool, const Options& options) :
63 matcher(
64 template_seq,
65 template_length,
66 barcode_pool,
67 [&]{
69 ssopt.strand = options.strand;
70 ssopt.max_mismatches = options.max_mismatches;
71 ssopt.duplicates = options.duplicates;
72 return ssopt;
73 }()
74 ),
75 counts(barcode_pool.size()),
76 use_first(options.use_first)
77 {}
78
79public:
83 struct State {
84 State() {}
85
86 State(typename SimpleSingleMatch<max_size>::State s, size_t nvar) : search(std::move(s)), counts(nvar) {}
87
88 typename SimpleSingleMatch<max_size>::State search;
89 std::vector<int> counts;
90 int total = 0;
91 };
92
93 void process(State& state, const std::pair<const char*, const char*>& r1, const std::pair<const char*, const char*>& r2) const {
94 if (use_first) {
95 if (matcher.search_first(r1.first, r1.second - r1.first, state.search)) {
96 ++state.counts[state.search.index];
97 } else if (matcher.search_first(r2.first, r2.second - r2.first, state.search)) {
98 ++state.counts[state.search.index];
99 }
100 } else {
101 bool found1 = matcher.search_best(r1.first, r1.second - r1.first, state.search);
102 auto id1 = state.search.index;
103 auto mm1 = state.search.mismatches;
104
105 bool found2 = matcher.search_best(r2.first, r2.second - r2.first, state.search);
106 auto id2 = state.search.index;
107 auto mm2 = state.search.mismatches;
108
109 if (found1 && !found2) {
110 ++state.counts[id1];
111 } else if (!found1 && found2) {
112 ++state.counts[id2];
113 } else if (found1 && found2) {
114 if (mm1 < mm2) {
115 ++state.counts[id1];
116 } else if (mm1 > mm2) {
117 ++state.counts[id2];
118 } else if (id1 == id2) {
119 ++state.counts[id1];
120 }
121 }
122 }
123 ++state.total;
124 }
125
126 static constexpr bool use_names = false;
131public:
135 State initialize() const {
136 return State(matcher.initialize(), counts.size());
137 }
138
139 void reduce(State& s) {
140 matcher.reduce(s.search);
141 for (size_t i = 0; i < counts.size(); ++i) {
142 counts[i] += s.counts[i];
143 }
144 total += s.total;
145 }
150private:
151 SimpleSingleMatch<max_size> matcher;
152 std::vector<int> counts;
153 int total = 0;
154 bool use_first = true;
155
156public:
161 const std::vector<int>& get_counts() const {
162 return counts;
163 }
164
168 int get_total() const {
169 return total;
170 }
171};
172
173}
174
175#endif
Handler for paired-end single barcodes.
Definition SingleBarcodePairedEnd.hpp:25
SingleBarcodePairedEnd(const char *template_seq, size_t template_length, const BarcodePool &barcode_pool, const Options &options)
Definition SingleBarcodePairedEnd.hpp:62
int get_total() const
Definition SingleBarcodePairedEnd.hpp:168
const std::vector< int > & get_counts() const
Definition SingleBarcodePairedEnd.hpp:161
Namespace for the kaori barcode-matching library.
Definition BarcodePool.hpp:13
Pool of barcode sequences for a variable region.
Definition BarcodePool.hpp:21
Optional parameters for SimpleSingleMatch.
Definition SimpleSingleMatch.hpp:36
SearchStrand strand
Definition SimpleSingleMatch.hpp:50
int max_mismatches
Definition SimpleSingleMatch.hpp:40
DuplicateAction duplicates
Definition SimpleSingleMatch.hpp:45
Optional parameters for SingleBarcodePairedEnd.
Definition SingleBarcodePairedEnd.hpp:30
bool use_first
Definition SingleBarcodePairedEnd.hpp:35
DuplicateAction duplicates
Definition SingleBarcodePairedEnd.hpp:50
SearchStrand strand
Definition SingleBarcodePairedEnd.hpp:45
int max_mismatches
Definition SingleBarcodePairedEnd.hpp:40