-
Notifications
You must be signed in to change notification settings - Fork 5
/
Genome.cpp
260 lines (220 loc) · 5.88 KB
/
Genome.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
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
252
253
254
255
256
257
258
259
260
//
// Genome.cpp
// NB_C++
//
// Created by Alexandru Cristian on 01/05/2017.
//
//
#include "Genome.hpp"
bool Genome::STORE_ALL_NUMERATORS = false;
Genome::Genome(path _kmr_path, path _sequence_path){
kmr_path = _kmr_path;
sequence_path = _sequence_path;
}
Genome::~Genome(){
unload();
}
path Genome::getKmrPath(){
return kmr_path;
}
void Genome::loadSequence(){
// Remove this code, it never gets used & was never meant for production anyway
std::ifstream in(sequence_path.native());
sequence = new string;
// Read the first line first (genome ID, other info)
in>>getSequence();
// Discard and overwrite with actual sequence.
in>>getSequence();
in.close();
sequenceLoaded = true;
}
void Genome::loadKmerCounts(){
if(!loadKmersLock.try_lock()){
/*
* Another thread is trying to load this at the same time,
* wait for it to finish and then return.
*/
loadKmersLock.lock();
loadKmersLock.unlock();
// Since we're here, another thread has already loaded this data. Return.
return;
}
std::ifstream in(kmr_path.native());
kmer_counts = new unordered_map<int, int>;
int kmer, kmer_count; string kmer_s;
while(in>>kmer_s>>kmer_count){
kmer = 0;
for(string::iterator it = kmer_s.begin(); it != kmer_s.end(); it++){
kmer<<=2;
switch(*it){
case 'A':
case 'a':
kmer+=0;
break;
case 'C':
case 'c':
kmer+=1;
break;
case 'G':
case 'g':
kmer+=2;
break;
case 'T':
case 't':
kmer+=3;
break;
}
}
(*kmer_counts)[kmer]=kmer_count;
}
in.close();
kmersLoaded = true;
loadKmersLock.unlock();
}
void Genome::unload(){
if(kmersLoaded){
delete kmer_counts;
kmersLoaded = false;
}
if(sequenceLoaded){
delete sequence;
sequenceLoaded = false;
}
}
unordered_map<int, int>& Genome::getKmerCounts(){
if(!kmersLoaded){
loadKmerCounts();
}
return *kmer_counts;
}
string& Genome::getSequence(){
return *sequence;
}
void Genome::computeClassificationNumerator(Class<int>* cl){
accumulator_set<double, features<tag::sum_kahan> > current, sumfrq;
current(cl->getNGenomes_lg());
ostringstream strs;
if(NB::debug_flag == NB::Debug::LOG_ALL){
strs<<"("<<cl->getId()<<"): "<<sum_kahan(current);
}
for(unordered_map<int, int>::iterator freq = getKmerCounts().begin();
freq != getKmerCounts().end(); freq++){
sumfrq(freq->second);
current(freq->second * cl->getFreqCount_lg(freq->first));
if(NB::debug_flag == NB::Debug::LOG_ALL){
strs<<" + "<<freq->second<<" * "<<cl->getFreqCount_lg(freq->first);
}
}
current(-sum_kahan(sumfrq) * cl->getSumFreq_lg());
if(NB::debug_flag == NB::Debug::LOG_ALL){
strs<<" - "<<sum_kahan(sumfrq)<<" * "<<cl->getSumFreq_lg()<<" = ";
strs<<sum_kahan(current)<<"\n";
}
numeratorAccess.lock();
if(!Genome::STORE_ALL_NUMERATORS){
double candidateNumerator = sum_kahan(current);
if(maximumNumeratorClass == NULL
|| maximumNumerator < candidateNumerator){
maximumNumerator = candidateNumerator;
maximumNumeratorClass = cl;
}
}else{
numerator.push(make_pair(sum_kahan(current), cl));
if(NB::debug_flag == NB::Debug::LOG_ALL){
cout<<strs.str();
}
}
numeratorAccess.unlock();
}
Genome::score Genome::getMaximum(){
numeratorAccess.lock();
Genome::score sc = make_pair(maximumNumerator, maximumNumeratorClass);
numeratorAccess.unlock();
return sc;
}
Genome::pqueue Genome::getConfidences(){
accumulator_set<double, features<tag::sum_kahan> > p_denominator(1);
double max, denominator;
pqueue num_cpy = numerator;
max = num_cpy.top().first;
vector<score> cache; cache.push_back(num_cpy.top());
num_cpy.pop();
while(!num_cpy.empty()){
p_denominator(exp(num_cpy.top().first - max));
cache.push_back(num_cpy.top());
num_cpy.pop();
}
denominator = max + log(sum_kahan(p_denominator));
pqueue confidence;
for(vector<score>::iterator num=cache.begin();
num != cache.end(); num++){
double confidence_lg = num->first - denominator;
confidence.push(make_pair(confidence_lg, num->second));
//processFreqs(getKmerCounts(), num->second, confidence_lg);
}
return confidence;
}
int Genome::size(){
return getSequence().size();
}
long long int Genome::computeAlignmentScore(Genome* seq){
Genome *a, *b;
if(seq->size() < getSequence().size()){
a=seq;
b=this;
}else{
a=this;
b=seq;
}
vector<vector<long long int> > cost(2);
int v_len = a->size();
for(int i=0; i < v_len; i++){
cost[0].push_back(0);
cost[1].push_back(0);
}
vector<int> cmax(v_len), c_ind(v_len);
int rep_n = b->size(), lmax, l_ind, highscore, hs_ind=-1;
for(int i=0; i < rep_n; i++){
for(int j=1; i < v_len; j++){
throw exception();
cost[1][j] = max(cost[0][j-1] /*+ distance(a->charAt(i), b->charAt(j))*/,
cmax[j] /*- penalty(j - c_ind[j])*/,
(l_ind != -1) ? lmax /*- penalty(j - l_ind)*/ : 0,
0);
if(cost[1][j] > lmax || l_ind == -1){
lmax = cost[1][j];
l_ind = j;
}
if(cost[1][j] > cmax[j] /*- penalty(j - c_ind[j])*/){
cmax[j] = cost[1][j];
c_ind[j] = j;
}
if(cost[1][j] > highscore || hs_ind == -1){
hs_ind = 1;
highscore = cost[1][j];
}
}
cost[0].swap(cost[1]);
}
return highscore;
}
string Genome::charAt(int pos){
if(!sequenceLoaded){
loadSequence();
}
return string(1, getSequence()[pos]);
}
long long int Genome::max(long long int a,
long long int b,
long long int c,
long long int d){
if(a>=b && a>=c && a>=d){
return a;
}else if(b>=a && b>=c && b>=d){
return b;
}else if(c>=b && c>=a && c>=d){
return c;
}else{
return d;
}
}