-
Notifications
You must be signed in to change notification settings - Fork 0
/
center_attempt.cpp
496 lines (439 loc) · 12.2 KB
/
center_attempt.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#include<cassert>
#include<cmath>
#include<string>
#include<iostream>
#include<sstream>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<list>
#include<queue>
#include<cstdarg>
#include<algorithm>
using namespace std;
ostringstream oMsg;
string sbuf;
typedef uint64_t pType;
#include "defs.h"
double errorRate = 0.01;
double ambigConst = 1;
int nthreads;
string ufFilename;
double threshold;
class Read {
public:
string id;
string seq;
int count;
float freq;
bool operator<(Read x) const {
return x.seq < seq;
}
bool operator==(string x) const {
return x == this->seq;
}
};
class Params {
public:
int thread;
pType seekstart;
pType seekend;
};
/*bool findPseudoKmers (string x, string y, string & new1, string & new2) {
assert(x.length() == y.length());
int diff = 0;
char x1, x2, y1, y2;
int pos1, pos2;
for (int i = 0; i < x.length(); i++) {
if (x[i] == y[i]) continue;
diff++;
if (diff == 1) {
pos1 = i;
x1 = x[i];
y1 = y[i];
} else if (diff == 2) {
pos2 = i;
x2 = x[i];
y2 = y[i];
} else {
return false;
}
}
if (diff == 2) {
new1 = x;
new1[pos1] = y1;
new2 = y;
new2[pos2] = x2;
return true;
}
return false;
}
*/
double calcMultCoef(vector<int> & distances, vector<Read> & kmers) {
int kmersize = kmers[0].seq.size();
double prob = 0;
double theta;
//cout << "calcMultCoef. distances = " << distances << "\t\t\t"; for (int i = 0; i < kmers.size(); i++) cout << kmers[i].mult << "\t";
//cout << "\nadding:\t";
for (size_t i = 0; i < kmers.size(); i++) {
theta = kmers[i].count * -((kmersize - distances[i]) * log(1 - errorRate) + distances[i] * log(errorRate));
//cout << theta << "\t";
prob = prob + theta;
}
//cout << endl;
return prob;
}
string find_consensus(vector<Read> & block) {
string c;
for (int i = 0; i < block[0].seq.length(); i++) {
int scores[4] = {0,0,0,0};
for (int j = 0; j < block.size(); j++) {
scores[nt2num(block[j].seq.at(i))]++;
}
c.push_back(num2nt(argmax(scores, 4)));
}
return c;
}
/*
for (int i = 0; i < origBlockSize; i++) {
for (int j = 0; j < origBlockSize; j++) {
string new1, new2;
if (findPseudoKmers(block[i].seq, block[j].seq, new1, new2)) {
Read cur;
new1 = rcnorm(new1);
new2 = rcnorm(new2);
vector<Read>::iterator it1 = find(block.begin(), block.end(), new1);
vector<Read>::iterator it2 = find(block.begin(), block.end(), new2);
if (it1 == block.end()) {
cur.seq = new1;
cur.count = 0;
block.push_back(cur);
}
if (it2 == block.end()) {
cur.seq = new2;
cur.count = 0;
block.push_back(cur);
}
}
}
}
*/
void process_block(vector<Read> & block, string blockNum, double threshold, ofstream & outf) {
int origBlockSize = block.size();
if (block.size() == 0) return;
vector<double> multiCoef(block.size() + 1,1000000);//add one for the consensus
vector<int> distance(block.size() + 1 , 0); //add one for the consensus
vector< vector<int> > distances(block.size() + 1 , distance); //add one for the consensus
//sort (block.begin(), block.end());
string newkmer;
bool change = false;
string reason = "noreason";
bool bigboy = false;
if (block.size() > 1) {
//This is a shortcut that deviates from the definition of the algorithm but speeds everything up
//if one node has a count more than double of another one
int max1 = 0;
int max2 = 1;
if (block[max1].count < block[max2].count) swap(max1, max2);
for (int i = 2; i < block.size(); i++) {
if (block[i].count > block[max1].count) {
max2 = max1;
max1 = i;
} else if (block[i].count > block[max2].count) {
max2 = i;
}
}
if (block[max1].count >= 2* block[max2].count && block[max1].count >= 20) {
change = true;
newkmer = block[max1].seq;
bigboy = true;
} else {
//Add a consensus sequence
Read consensus;
consensus.seq = find_consensus(block);
consensus.count = 0;
bool found = false;
for (int i = 0; i < block.size(); i++) {
if (block[i].seq == consensus.seq) {
found = true;
break;
}
}
if (!found) block.push_back(consensus);
//Calculate distance matrix
for (int i = 0; i < block.size(); i++) {
distances[i][i] = 0;
for (int j = i + 1; j < block.size(); j++) {
distances[i][j] = hamdist(block[i].seq, block[j].seq, ANY_STRAND);
distances[j][i] = distances[i][j];
}
}
//Calculate multinmial coefficient
for (int i = 0; i < block.size(); i++) {
multiCoef[i] = calcMultCoef(distances[i], block);
}
//Pick a center node
double low1 = 1000000000;
int ind1 = -1;
int ind2 = -1;
double low2 = 1000000000;
for (int i = 0; i < block.size(); i++) {
if (multiCoef[i] < low1) {
low2 = low1;
ind2 = ind1;
low1 = multiCoef[i];
ind1 = i;
} else if (multiCoef[i] < low2) {
low2 = multiCoef[i];
ind2 = i;
}
}
if (low1 < low2 - ambigConst) {
change = true;
if (ind1 == origBlockSize) reason = "pseudo"; //consensus was picked
}
newkmer = block[ind1].seq;
}
}
for (int i = 0; i < origBlockSize; i++) {
if (block.size() == 1) { //singleton
if (block[i].freq >= threshold) { //keep reads
newkmer = block[i].seq;
reason = "goodSingleton";
change = false;
} else {
newkmer = "removed";
reason = "badSingleton";
change = true;
}
} else { //multiton
if (change) {
if (reason != "pseudo") {
if (newkmer == block[i].seq) {
reason = "center";
} else if (block[i].freq > 9) { //save from destruction cause its too frequent
newkmer = block[i].seq;
reason = "center";
} else {
reason = "change";
}
}
} else { //ambig, treat like singleton
if (block[i].freq >= threshold) { //keep reads
newkmer = block[i].seq;
reason = "goodAmbig";
} else {
newkmer = "removed";
reason = "badAmbig";
}
}
}
outf << blockNum << "\t" << block[i].seq << "\t" << newkmer << "\t";
outf << block[i].count << "\t" << block[i].freq << "\t" << block.size();
outf << "\t" << multiCoef[i] << "\t" << reason << "\t";
outf << distances[i][0];
if( !bigboy) {
for (int j = 1; j < distances[i].size(); j++) outf << "_" << distances[i][j];
}
outf << endl;
}
outf << endl;
return;
}
void * onethread(void * params) {
int thread = ((Params *) params)->thread;
pType seekstart = ((Params *) params)->seekstart;
pType seekend = ((Params *) params)->seekend;
ifstream inf;
open_file(inf, ufFilename);
inf.seekg(seekstart);
ofstream outf;
open_file(outf, "reads.uf.corr." + make_string(thread));
vector<Read> block;
int counter=0;
vector<string> row;
string curBlockNum;
string lastBlockNum = "GO LAKERS!";
while (get_row_whitespace(inf, row)) {
//cerr << "Thread_" << thread << "\t" << row << endl;
if (row.size() < 1 || row[0] != "ITEM") {
//outf << row << endl;
continue;
}
//if (atoi(row[1].c_str()) % nthreads != thread) continue;
if (++counter % 1000000 == 0) cerr << "Processed " << add_commas(counter) << ", ";
Read cur;
curBlockNum = row[1];
cur.id = row[2];
cur.seq = row[3];
cur.count = atoi(row[4].c_str());
cur.freq = atof(row[5].c_str());
if (lastBlockNum == curBlockNum) { //add to current reads
block.push_back(cur);
} else {
process_block(block,lastBlockNum, threshold, outf);
block.clear();
block.push_back(cur);
}
lastBlockNum = curBlockNum;
pType curp = inf.tellg();
if (curp == seekend) break;
}
process_block(block,curBlockNum, threshold, outf);
cerr << "Finished\n";
inf.close();
outf.close();
pthread_exit(NULL);
}
int main(int argc, char * argv[]) {
ufFilename = argv[1];
threshold = atof(argv[2]);
nthreads = atoi(argv[3]);
/*
if (threshold == -1) {
cerr << "First phase...\n";
threshold = get_threshold(ufFilename);
}
*/
cerr << "Finding positions to parallelize...\n";
//find start and end locs for threads
Params params[nthreads];
long ufFilesize = get_filesize(ufFilename);
for (int i = 0; i < nthreads; i++) {
params[i].thread = i;
long prelimend = (i + 1 )* (1 + (ufFilesize / nthreads));
params[i].seekend = min (ufFilesize, prelimend);
}
//for (int i = 0; i < nthreads; i++) cout << "prelim: params = " << params[i].thread << "\tstart = " << add_commas(params[i].seekstart) << "\tend = " << add_commas(params[i].seekend) << endl;
int counter = 0;
ifstream inf;
open_file(inf, ufFilename);
vector<string> row;
int curthread = 0;
string lastBlockNum = "GO LAKERS!";
pType lastp = inf.tellg();
while (get_row_whitespace(inf, row)) {
if (++counter % 1000000 == 0) cerr << "counter = " << add_commas(counter) << " lastp = " << add_commas(lastp) << " curthread = " << curthread << endl;
if (lastp > params[curthread].seekend) {
if (row.size() != 0 && row[1] != lastBlockNum) { //set breakpoint
params[curthread++].seekend = lastp;
}
}
if (row.size() > 0) lastBlockNum = row[1];
lastp = inf.tellg();
}
params[curthread].seekend = lastp;
//set seek starts
params[0].seekstart = 0;
for (int i = 1; i < nthreads; i++) params[i].seekstart = params[i - 1].seekend;
inf.close();
for (int i = 0; i < nthreads; i++) cout << "params = " << params[i].thread << "\tstart = " << params[i].seekstart << "\tend = " << params[i].seekend << endl;
//start threads
cerr << "Second phase (threshold = " << threshold << ")...\n";
pthread_t thread[nthreads];
for (int i = 0; i < nthreads; i++) {
pthread_create(&thread[i], NULL, onethread, (void *) ¶ms[i]);
}
for (int i = 0; i < nthreads; i++) {
pthread_join(thread[i], NULL);
}
return 0;
}
/*
double get_min(vector<double> & counts) {
//cout << endl << counts << "\tCOUNTS" << endl;
//processes the counts
int res = 10;
vector<double> bigbins(100,0);
vector<double> smallbins(100*res,0);
//put them into big and small bins first.
for (int i = 0; i < counts.size(); i++) {
int bin = int(counts[i]);
if (bin < 100) bigbins.at(bin) += 1;
bin = int(counts[i] * res);
if (bin < 100 * res ) smallbins.at(bin) += 1;
}
ofstream dump;
open_file(dump, "singletonCounts.txt");
dump << bigbins << "\tBIG_BINS" << endl;
dump << smallbins << "\tSMALL_BINS" << endl;
dump.close();
//find first min big bin
int minBigBin = -1;
for (int i = 1; i < bigbins.size() - 1; i++) {
if (bigbins[i-1] > bigbins[i] && bigbins[i] < bigbins[i+1]) {
minBigBin = i;
break;
}
}
//cout << bigbins << "\tBIG_BINS" << endl; cout << minBigBin << "\tminBigBin" << endl;
if (minBigBin == -1) {
cerr << "Cannot find minimum in frequency distribution! Will use 0.\n";
return 0;
}
//find first min bin
int minSmallBin = -1;
for (int i = max(0, (minBigBin -1 ) * res) + 1; i < min(int(smallbins.size()), res * (minBigBin + 1) ) - 1; i++) {
if (smallbins[i-1] > smallbins[i] && smallbins[i] < smallbins[i+1]) {
minSmallBin = i;
break;
}
}
return minBigBin;
double retval = minBigBin - 1 + (minSmallBin / double(res));
assert (minSmallBin >= 0);
return retval;
}
double get_total_counts(vector<Read> & block) {
// returns the sum of the weights in this block, -1 if its not a singleton
//Create list of kmers and their multiplicities
if (block.size() == 0) return -1;
//vector<Read> orBlock(block);
double sum = block[0].count;
string seq = rcnorm(block[0].seq);
for (int i = 1; i < block.size(); i++) {
if (rcnorm(block[i].seq) != seq) return -1;
sum += block[i].count;
}
return sum;
}
double get_threshold(string ufFilename) {
double retval;
ifstream inf;
open_file(inf, ufFilename);
vector<double> counts;
vector<Read> block;
int counter=0;
double totCount;
vector<string> row;
string curBlockNum;
string lastBlockNum = "GO LAKERS!";
while (get_row_whitespace(inf, row)) {
if (++counter % 1000000 == 0) cerr << "Processed " << add_commas(counter) << ", ";
if (row.size() < 1 || row[0] != "ITEM") {
continue;
}
Read cur;
curBlockNum = row[1];
cur.id = row[2];
cur.seq = row[3];
cur.count = atof(row[4].c_str());
if (lastBlockNum == curBlockNum) { //add to current reads
block.push_back(cur);
} else {
totCount = get_total_counts(block);
if (totCount >= 0) counts.push_back(totCount);
block.clear();
block.push_back(cur);
}
lastBlockNum = curBlockNum;
}
cerr << endl;
totCount = get_total_counts(block);
if (totCount >= 0) counts.push_back(totCount);
retval = get_min(counts);
inf.close();
return retval;
}
*/