-
Notifications
You must be signed in to change notification settings - Fork 4
/
pamld.cpp
180 lines (159 loc) · 7.66 KB
/
pamld.cpp
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
/*
Pheniqs : PHilology ENcoder wIth Quality Statistics
Copyright (C) 2018 Lior Galanti
NYU Center for Genetics and System Biology
Author: Lior Galanti <[email protected]>
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/>.
*/
#include "pamld.h"
template < class T > PamlDecoder< T >::PamlDecoder(const Value& ontology) try :
Decoder< T >(ontology),
noise(decode_value_by_key< double >("noise", ontology)),
confidence_threshold(decode_value_by_key< double >("confidence threshold", ontology)),
random_barcode_probability(decode_value_by_key< double >("random barcode probability", ontology)),
adjusted_noise_probability(noise * random_barcode_probability),
conditional_decoding_probability(0),
decoding_confidence(0) {
} catch(Error& error) {
error.push("PamlDecoder");
throw;
};
template < class T > void PamlDecoder< T >::classify(const Read& input, Read& output) {
this->observation.clear();
this->rule.apply(input, this->observation);
/* Compute the posterior probability P( observed | expected ) for each barcode.
Keep track of the channel that yield the maximal prior adjusted probability.
If r is the observed sequence and b is the barcode sequence
P(r|b) is the probability that r was observed given that b was sequenced.
Accumulate probabilities P(b) * P(r|b), in sigma_p using the Kahan summation algorithm
to minimize floating point drift. see https://en.wikipedia.org/wiki/Kahan_summation_algorithm.
p is the prior adjusted conditional probability
d is the decoding Hamming distance
sigma_p accumulates the prior adjusted conditional probabilities to compute the decoding confidence
*/
double p(0);
double y(0);
double t(0);
int32_t d(0);
int32_t hqd(0);
double sigma_p(0);
double compensation(0);
double conditional_probability(0);
double adjusted_conditional_decoding_probability(0);
for(auto& barcode : this->tag_array) {
/* The conditional probability, P(r|b), is the probability of the observation r
given b was expected.
P(b), barcode.concentration, is the prior probability of observing b
p is the prior adjusted conditional probability, P(b) * P(r|b),
sigma_p is the sum of p over b */
barcode.compensated_decoding_probability(this->observation, this->high_quality_threshold, conditional_probability, d, hqd);
p = conditional_probability * barcode.concentration;
y = p - compensation;
t = sigma_p + y;
compensation = (t - sigma_p) - y;
sigma_p = t;
if(p > adjusted_conditional_decoding_probability) {
this->decoded = &barcode;
this->edit_distance = d;
this->high_quality_edit_distance = hqd;
adjusted_conditional_decoding_probability = p;
conditional_decoding_probability = conditional_probability;
}
}
/* add the prior adjusted noise probability to sigma_p */
y = adjusted_noise_probability - compensation;
t = sigma_p + y;
compensation = (t - sigma_p) - y;
sigma_p = t;
/* P(b|r), decoding confidence, is the posterior probability.
The probability that barcode b was sequenced given the observation r.
adjusted_conditional_decoding_probability is the highest prior adjusted conditional probability,
P(r|b) of all possible b */
decoding_confidence = adjusted_conditional_decoding_probability / sigma_p;
/* This is a noise filter, when the conditional probability is lower than the probability of
a random abservation, the entropy is too high for the information to be meaningful */
if(conditional_decoding_probability > random_barcode_probability) {
/* if the posterior probability is higher than the confidence_threshold */
if(decoding_confidence > confidence_threshold) {
this->decoded->accumulated_confidence += decoding_confidence;
/* if high_quality_distance_threshold is 0 the entire filter is disabled */
if(this->high_quality_distance_threshold > 0 && this->high_quality_edit_distance >= this->high_quality_distance_threshold) {
output.set_qcfail(true);
}
if(!output.qcfail()) {
this->decoded->accumulated_pf_confidence += decoding_confidence;
}
} else {
++this->decoded->low_confidence_count;
output.set_qcfail(true);
}
} else {
++this->decoded->low_conditional_confidence_count;
output.set_qcfail(true);
this->decoded = &this->unclassified;
this->edit_distance = 0;
this->high_quality_edit_distance = 0;
decoding_confidence = 0;
}
Decoder< T >::classify(input, output);
};
PamlSampleDecoder::PamlSampleDecoder(const Value& ontology) try :
PamlDecoder< Barcode >(ontology),
rg_by_barcode_index(decode_tag_id_by_index(ontology)) {
} catch(Error& error) {
error.push("PamlSampleDecoder");
throw;
};
void PamlSampleDecoder::classify(const Read& input, Read& output) {
PamlDecoder< Barcode >::classify(input, output);
output.append_to_raw_sample_barcode(this->observation);
output.append_to_corrected_sample_barcode_sequence(*this->decoded, this->observation, corrected_quality);
output.update_sample_distance(this->edit_distance);
output.update_sample_decoding_confidence(this->decoding_confidence);
output.set_RG(this->rg_by_barcode_index[this->decoded->index]);
};
PamlCellularDecoder::PamlCellularDecoder(const Value& ontology) try :
PamlDecoder< Barcode >(ontology) {
} catch(Error& error) {
error.push("PamlCellularDecoder");
throw;
};
void PamlCellularDecoder::classify(const Read& input, Read& output) {
PamlDecoder< Barcode >::classify(input, output);
output.append_to_raw_cellular_barcode(this->observation);
output.append_to_corrected_cellular_barcode_sequence(*this->decoded, this->observation, corrected_quality);
if(this->decoded->is_classified()) {
output.update_cellular_decoding_confidence(this->decoding_confidence);
output.update_cellular_distance(this->edit_distance);
} else {
output.set_cellular_decoding_confidence(0);
output.set_cellular_distance(0);
}
};
PamlMolecularDecoder::PamlMolecularDecoder(const Value& ontology) try :
PamlDecoder< Barcode >(ontology) {
} catch(Error& error) {
error.push("PamlMolecularDecoder");
throw;
};
void PamlMolecularDecoder::classify(const Read& input, Read& output) {
PamlDecoder< Barcode >::classify(input, output);
output.append_to_raw_molecular_barcode(this->observation);
output.append_to_corrected_molecular_barcode_sequence(*this->decoded, this->observation, corrected_quality);
if(this->decoded->is_classified()) {
output.update_molecular_decoding_confidence(this->decoding_confidence);
output.update_molecular_distance(this->edit_distance);
} else {
output.set_molecular_decoding_confidence(0);
output.set_molecular_distance(0);
}
};