-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclassifier.h
180 lines (157 loc) · 7.85 KB
/
classifier.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
/*
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_CLASSIFY_H
#define PHENIQS_CLASSIFY_H
#include "include.h"
#include "selector.h"
#include "read.h"
enum class ClassifierType : int8_t {
UNKNOWN = -1,
SAMPLE = 0,
CELLULAR = 1,
MOLECULAR = 2,
};
string to_string(const ClassifierType& value);
bool from_string(const char* value, ClassifierType& result);
void to_kstring(const ClassifierType& value, kstring_t& result);
bool from_string(const string& value, ClassifierType& result);
ostream& operator<<(ostream& o, const ClassifierType& value);
template<> bool decode_value_by_key< ClassifierType >(const Value::Ch* key, ClassifierType& value, const Value& container);
template<> ClassifierType decode_value_by_key< ClassifierType >(const Value::Ch* key, const Value& container);
vector< string > decode_tag_id_by_index(const Value& ontology);
template < class T > class Classifier : public AccumulatingSelector {
protected:
T* decoded;
T unclassified;
vector< T > tag_array;
const bool multiplexing_classifier;
const uint8_t corrected_quality;
public:
Classifier(const Value& ontology) try :
AccumulatingSelector(decode_value_by_key< int32_t >("index", ontology)),
decoded(NULL),
unclassified(ontology["undetermined"]),
tag_array(decode_value_by_key< vector< T > >("codec", ontology)),
multiplexing_classifier(decode_value_by_key< bool >("multiplexing classifier", ontology)),
corrected_quality(decode_value_by_key< uint8_t >("corrected quality", ontology)) {
decoded = &unclassified;
} catch(Error& error) {
error.push("Classifier");
throw;
};
Classifier(const Classifier< T >& other) :
AccumulatingSelector(other),
decoded(NULL),
unclassified(other.unclassified),
tag_array(other.tag_array),
multiplexing_classifier(other.multiplexing_classifier),
corrected_quality(other.corrected_quality) {
decoded = &unclassified;
};
virtual inline void classify(const Read& input, Read& output) {
++(decoded->count);
if(!output.qcfail()) {
++(decoded->pf_count);
}
if(multiplexing_classifier) {
output.channel_index = decoded->index;
}
};
virtual inline void collect(const Classifier& other) {
AccumulatingSelector::collect(other);
unclassified.collect(other.unclassified);
for(size_t index(0); index < tag_array.size(); ++index) {
tag_array[index].collect(other.tag_array[index]);
}
};
inline void finalize() override {
/* collect the counts from the tags to get the total */
for(auto& element : tag_array) {
this->classified_count += element.count;
this->pf_classified_count += element.pf_count;
}
this->count = this->classified_count + unclassified.count;
this->pf_count = this->pf_classified_count + unclassified.pf_count;
/* compute noise prior estimate
first get the portion of reads that failed the noise filter from high confidence classified. */
double estimated_noise_count(this->low_conditional_confidence_count);
double confident_noise_ratio(estimated_noise_count / (estimated_noise_count + this->pf_classified_count));
/* than assume that the same ratio applies to low confidnce reads */
if(this->low_confidence_count > 0) {
estimated_noise_count += double(this->low_confidence_count) * confident_noise_ratio;
}
this->estimated_noise_prior = estimated_noise_count / double(this->count);
/* finalize the tags */
double estimated_not_noise_prior(1.0 - this->estimated_noise_prior);
for(auto& element : tag_array) {
element.finalize(*this);
element.estimated_concentration_prior = estimated_not_noise_prior * element.pf_pooled_classified_fraction;
}
unclassified.finalize(*this);
/* finalize the selector */
AccumulatingSelector::finalize();
};
void adjust_prior(Value& container, Document& document) {
/* adjust the noise prior */
encode_key_value("noise", estimated_noise_prior, container, document);
/* build a map of barcode sequence to prior */
unordered_map< string, double > concentration_prior_by_barcode(tag_array.size());
kstring_t buffer({ 0, 0, NULL });
for(auto& tag: tag_array) {
tag.encode_iupac_ambiguity(buffer);
concentration_prior_by_barcode.insert(make_pair<string, double>(string(buffer.s, buffer.l), double(tag.estimated_concentration_prior)));
ks_clear(buffer);
}
ks_free(buffer);
/* adjust the concentration prior on each barcode */
Value::MemberIterator reference = container.FindMember("codec");
if(reference != container.MemberEnd()) {
list< string > barcode_segment;
for(auto& record : reference->value.GetObject()) {
if(decode_value_by_key< list< string > >("barcode", barcode_segment, record.value)) {
string barcode_string;
for(auto& segment : barcode_segment) {
if(!barcode_string.empty()) {
barcode_string.append("-");
}
barcode_string.append(segment);
auto concentration_record = concentration_prior_by_barcode.find(barcode_string);
if(concentration_record != concentration_prior_by_barcode.end()) {
encode_key_value("concentration", concentration_record->second, record.value, document);
}
}
}
}
}
};
void encode(Value& container, Document& document) const override {
AccumulatingSelector::encode(container, document);
Value unclassified_report(kObjectType);
unclassified.encode(unclassified_report, document);
container.AddMember("unclassified", unclassified_report.Move(), document.GetAllocator());
if(!tag_array.empty()) {
Value element_report_array(kArrayType);
for(auto& element : tag_array) {
Value element_report(kObjectType);
element.encode(element_report, document);
element_report_array.PushBack(element_report.Move(), document.GetAllocator());
}
container.AddMember("classified", element_report_array.Move(), document.GetAllocator());
}
};
};
#endif /* PHENIQS_CLASSIFY_H */