-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmultiplex.h
251 lines (233 loc) · 9.24 KB
/
multiplex.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
Pheniqs : PHilology ENcoder wIth Quality Statistics
Copyright (C) 2018 Lior Galanti
NYU Center for Genetics and System Biology
Author: Lior Galanti <lior.galanti@nyu.edu>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PHENIQS_CHANNEL_H
#define PHENIQS_CHANNEL_H
#include "include.h"
#include "feed.h"
class AveragePhreadAccumulator {
public:
uint64_t count;
double min_value;
double max_value;
double sum_value;
double mean_value;
vector< uint64_t > distribution;
AveragePhreadAccumulator();
AveragePhreadAccumulator(const AveragePhreadAccumulator& other) :
count(other.count),
min_value(other.min_value),
max_value(other.max_value),
sum_value(other.sum_value),
mean_value(other.mean_value),
distribution(other.distribution) {
};
inline void increment(const Segment& segment) {
++count;
double value(0);
for(int32_t i(0); i < segment.length; ++i) {
value += segment.quality[i];
}
value /= double(segment.length);
sum_value += value;
min_value = min(min_value, value);
max_value = max(max_value, value);
++(distribution[static_cast< size_t >(value)]);
};
void finalize();
AveragePhreadAccumulator& operator=(const AveragePhreadAccumulator& rhs);
AveragePhreadAccumulator& operator+=(const AveragePhreadAccumulator& rhs);
};
class NucleotideAccumulator {
public:
uint64_t count;
uint8_t min_quality;
uint8_t max_quality;
uint64_t sum_quality;
double mean_quality;
uint8_t Q1;
uint8_t Q3;
uint8_t IQR;
uint8_t LW;
uint8_t RW;
uint8_t median_quality;
vector< uint64_t > distribution;
NucleotideAccumulator();
NucleotideAccumulator(const NucleotideAccumulator& other) :
count(other.count),
min_quality(other.min_quality),
max_quality(other.max_quality),
sum_quality(other.sum_quality),
mean_quality(other.mean_quality),
Q1(other.Q1),
Q3(other.Q3),
IQR(other.IQR),
LW(other.LW),
RW(other.RW),
median_quality(other.median_quality),
distribution(other.distribution) {
};
inline void increment(const uint8_t phred) {
++(distribution[phred]);
};
inline uint64_t quantile(const double portion) {
uint64_t position(portion * count);
uint8_t phred(0);
while (position > 0) {
if(distribution[phred] >= position) {
break;
}
position -= distribution[phred];
++phred;
while (distribution[phred] == 0) {
++phred;
}
}
return phred;
};
void finalize();
NucleotideAccumulator& operator=(const NucleotideAccumulator& rhs);
NucleotideAccumulator& operator+=(const NucleotideAccumulator& rhs);
};
class CycleAccumulator {
public:
vector< NucleotideAccumulator > nucleotide_by_code;
CycleAccumulator();
CycleAccumulator(const CycleAccumulator& other) :
nucleotide_by_code(other.nucleotide_by_code) {
};
inline void increment(const uint8_t nucleotide, const uint8_t phred) {
nucleotide_by_code[nucleotide].increment(phred);
};
void finalize();
CycleAccumulator& operator=(const CycleAccumulator& rhs);
CycleAccumulator& operator+=(const CycleAccumulator& rhs);
};
class SegmentAccumulator {
public:
void operator=(SegmentAccumulator const &) = delete;
int32_t capacity;
int32_t shortest;
vector < uint64_t > nucleic_acid_count_by_code;
AveragePhreadAccumulator average_phred;
vector< CycleAccumulator > cycle_by_index;
SegmentAccumulator();
SegmentAccumulator(const SegmentAccumulator& other) :
// index(other.index),
capacity(other.capacity),
shortest(other.shortest),
nucleic_acid_count_by_code(other.nucleic_acid_count_by_code),
average_phred(other.average_phred),
cycle_by_index(other.cycle_by_index) {
};
inline void increment(const Segment& segment) {
if(segment.length > capacity) {
cycle_by_index.resize(segment.length);
capacity = segment.length;
}
if(segment.length < shortest) {
shortest = segment.length;
}
for(int32_t i(0); i < segment.length; ++i) {
++(nucleic_acid_count_by_code[NO_NUCLEOTIDE]);
++(nucleic_acid_count_by_code[segment.code[i]]);
cycle_by_index[i].increment(segment.code[i], segment.quality[i]);
}
average_phred.increment(segment);
};
void finalize();
SegmentAccumulator& operator+=(const SegmentAccumulator& rhs);
};
bool encode_value(const SegmentAccumulator& value, Value& container, Document& document);
class ReadAccumulator {
public:
void operator=(ReadAccumulator const &) = delete;
vector< SegmentAccumulator > segment_accumulator_by_index;
ReadAccumulator(const int32_t& cardinality);
ReadAccumulator(const ReadAccumulator& other) :
segment_accumulator_by_index(other.segment_accumulator_by_index) {
};
inline void increment(const Read& read) {
for(size_t i(0); i < segment_accumulator_by_index.size(); ++i) {
segment_accumulator_by_index[i].increment(read[i]);
}
};
void finalize();
ReadAccumulator& operator+=(const ReadAccumulator& rhs);
};
bool encode_value(const ReadAccumulator& value, Value& container, Document& document);
class Channel {
public:
void operator=(Channel const &) = delete;
const int32_t index;
// const HeadRGAtom rg;
const bool filter_outgoing_qc_fail;
const bool enable_quality_control;
const list< URL > output_feed_url_by_segment;
vector< Feed* > output_feed_lock_order;
vector< Feed* > output_feed_by_segment;
ReadAccumulator read_accumulator;
Channel(const Value& ontology);
Channel(const Channel& other);
inline void push(const Read& read) {
if(output_feed_lock_order.size() > 0) {
if(!filter_outgoing_qc_fail || !read.qcfail()) {
/* acquire a push lock for all feeds in a fixed order */
vector< unique_lock< mutex > > feed_locks;
feed_locks.reserve(output_feed_lock_order.size());
for(const auto feed : output_feed_lock_order) {
feed_locks.push_back(feed->acquire_push_lock());
}
/* push the segments to the output feeds */
for(size_t i(0); i < output_feed_by_segment.size(); ++i) {
output_feed_by_segment[i]->push(read[i]);
}
/* release the locks on the feeds in reverse order */
for(auto feed_lock(feed_locks.rbegin()); feed_lock != feed_locks.rend(); ++feed_lock) {
feed_lock->unlock();
}
}
}
if(enable_quality_control) {
read_accumulator.increment(read);
}
};
void populate(unordered_map< URL, Feed* >& output_feed_by_url);
void finalize();
void encode(Value& container, Document& document) const;
Channel& operator+=(const Channel& rhs);
};
template<> vector< Channel > decode_value< vector< Channel > >(const Value& container);
class Multiplexer {
public:
const bool filter_outgoing_qc_fail;
const bool enable_quality_control;
vector< Channel > channel_by_index;
Multiplexer(const Value& ontology);
Multiplexer(const Multiplexer& other);
inline void push(const Read& read) {
channel_by_index[read.channel_index].push(read);
};
void populate(unordered_map< URL, Feed* >& output_feed_by_url) {
for(auto& channel : channel_by_index) {
channel.populate(output_feed_by_url);
}
};
void collect(const Multiplexer& other);
void finalize();
void encode(Value& container, Document& document) const;
};
#endif /* PHENIQS_CHANNEL_H */