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
5#include <vector>
6
13namespace kaori {
14
24template<SeqLength 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, SeqLength template_length, const BarcodePool& barcode_pool, const Options& options) :
63 my_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 my_counts(barcode_pool.size()),
76 my_use_first(options.use_first)
77 {}
78
79public:
83 struct State {
84 State() = default;
85 State(typename SimpleSingleMatch<max_size_>::State s, typename std::vector<Count>::size_type nvar) : search(std::move(s)), counts(nvar) {}
86
87 typename SimpleSingleMatch<max_size_>::State search;
88 std::vector<Count> counts;
89 Count total = 0;
90 };
91
92 void process(State& state, const std::pair<const char*, const char*>& r1, const std::pair<const char*, const char*>& r2) const {
93 if (my_use_first) {
94 if (my_matcher.search_first(r1.first, r1.second - r1.first, state.search)) {
95 ++state.counts[state.search.index];
96 } else if (my_matcher.search_first(r2.first, r2.second - r2.first, state.search)) {
97 ++state.counts[state.search.index];
98 }
99 } else {
100 bool found1 = my_matcher.search_best(r1.first, r1.second - r1.first, state.search);
101 auto id1 = state.search.index;
102 auto mm1 = state.search.mismatches;
103
104 bool found2 = my_matcher.search_best(r2.first, r2.second - r2.first, state.search);
105 auto id2 = state.search.index;
106 auto mm2 = state.search.mismatches;
107
108 if (found1 && !found2) {
109 ++state.counts[id1];
110 } else if (!found1 && found2) {
111 ++state.counts[id2];
112 } else if (found1 && found2) {
113 if (mm1 < mm2) {
114 ++state.counts[id1];
115 } else if (mm1 > mm2) {
116 ++state.counts[id2];
117 } else if (id1 == id2) {
118 ++state.counts[id1];
119 }
120 }
121 }
122 ++state.total;
123 }
124
125 static constexpr bool use_names = false;
130public:
134 State initialize() const {
135 return State(my_matcher.initialize(), my_counts.size());
136 }
137
138 void reduce(State& s) {
139 my_matcher.reduce(s.search);
140 for (decltype(my_counts.size()) i = 0, end = my_counts.size(); i < end; ++i) {
141 my_counts[i] += s.counts[i];
142 }
143 my_total += s.total;
144 }
149private:
150 SimpleSingleMatch<max_size_> my_matcher;
151 std::vector<Count> my_counts;
152 Count my_total = 0;
153 bool my_use_first = true;
154
155public:
160 const std::vector<Count>& get_counts() const {
161 return my_counts;
162 }
163
167 Count get_total() const {
168 return my_total;
169 }
170};
171
172}
173
174#endif
Defines the SimpleSingleMatch class.
Pool of barcode sequences.
Definition BarcodePool.hpp:24
Handler for paired-end single barcodes.
Definition SingleBarcodePairedEnd.hpp:25
Count get_total() const
Definition SingleBarcodePairedEnd.hpp:167
const std::vector< Count > & get_counts() const
Definition SingleBarcodePairedEnd.hpp:160
SingleBarcodePairedEnd(const char *template_seq, SeqLength template_length, const BarcodePool &barcode_pool, const Options &options)
Definition SingleBarcodePairedEnd.hpp:62
Namespace for the kaori barcode-matching library.
Definition BarcodePool.hpp:16
std::size_t SeqLength
Definition utils.hpp:37
SearchStrand
Definition utils.hpp:31
DuplicateAction
Definition utils.hpp:26
unsigned long long Count
Definition utils.hpp:67
Optional parameters for SimpleSingleMatch.
Definition SimpleSingleMatch.hpp:36
DuplicateAction duplicates
Definition SimpleSingleMatch.hpp:45
int max_mismatches
Definition SimpleSingleMatch.hpp:40
SearchStrand strand
Definition SimpleSingleMatch.hpp:50
Optional parameters for SingleBarcodePairedEnd.
Definition SingleBarcodePairedEnd.hpp:30
DuplicateAction duplicates
Definition SingleBarcodePairedEnd.hpp:50
SearchStrand strand
Definition SingleBarcodePairedEnd.hpp:45
bool use_first
Definition SingleBarcodePairedEnd.hpp:35
int max_mismatches
Definition SingleBarcodePairedEnd.hpp:40