-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.cpp
799 lines (686 loc) · 23.5 KB
/
Parser.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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
/***
**
* File: Parser.cpp
* Created: Dec 12, 2009 4:05 PM
*
* Author: Xiao Yang <isuxyang@gmail.com>
*
* Copyright 2009 Xiao Yang, Karin Dorman, Srinivas Aluru
* Copyright 2011-12 Ankit Shah, Sriram P C, Srinivas Aluru
*
* This file is part of Reptile (version 1.1)
*
* Reptile is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Reptile 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Libpnorm. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <algorithm>
#include "Parser.h"
#include "util.h"
#include "find_neighbors.h"
void print(int i) {
std::cout << " " << i;
}
void printHex(int i) {
std::cout << " " << std::hex << i;
}
void Parser::ec(const Para& myPara) {
readID_ = myPara.startFromLineNo;
if(myPara.storeReads) {
// reads are already obtained from input file and stored in ECData
processBatch(ecdata->m_ReadsString,ecdata->m_QualsString,
ecdata->m_ReadsOffset,ecdata->m_QualsOffset,myPara);
} else {
processReadsFromFile(myPara);
}
}
void Parser::processBatch(cvec_t &ReadsString,cvec_t &QualsString,
ivec_t &ReadsOffset,ivec_t &QualsOffset,
const Para& myPara) {
#ifdef DEBUG
std::stringstream out3 ;
#endif
for(unsigned long i = 0; i < ReadsOffset.size();i++) {
int position = ReadsOffset[i];
int qposition = QualsOffset[i];
char* addr = const_cast<char*> (&ReadsString[position]);
char* qAddr = const_cast<char*> (&QualsString[qposition]);
#ifdef DEBUG
out3 << addr << std::endl;
#endif
readEC(addr, qAddr, myPara);
readID_++;
}
#ifdef DEBUG
for(int i = 0; i< size;i++){
if(rank == i){
std::cout << out3.str();
}
MPI_Barrier(MPI_COMM_WORLD);
}
#endif
}
void Parser::processReadsFromFile(const Para& myPara) {
std::ifstream read_stream(myPara.iFaName.c_str());
assert(read_stream.good() == true);
std::ifstream qual_stream(myPara.iQName.c_str());
assert(qual_stream.good() == true);
read_stream.seekg(myPara.offsetStart,std::ios::beg);
qual_stream.seekg(myPara.qOffsetStart,std::ios::beg);
bIO::FASTA_input fasta(read_stream);
bIO::FASTA_input qual(qual_stream);
++fasta;++qual;
cvec_t ReadsString; // full string
cvec_t QualsString; // full quality score
ivec_t ReadsOffset;
ivec_t QualsOffset;
while(1){
bool lastRead = readBatch( fasta,qual,ReadsString,ReadsOffset,
QualsString,QualsOffset,myPara);
#ifdef DEBUG
{
std::stringstream out ;
out << "PROC : " << myPara.mpi_env->rank() << " "
<< ReadsOffset.size() << std::endl;
std::cout << out.str();
}
#endif
assert(ReadsOffset.size() == QualsOffset.size());
processBatch(ReadsString,QualsString,ReadsOffset,QualsOffset,myPara);
// std::cout << out3.str();
ReadsString.resize(0);
QualsString.resize(0);
ReadsOffset.resize(0);
QualsOffset.resize(0);
if(lastRead) break;
}
return;
}
void Parser::readEC(char* addr, char* qAddr, const Para& myPara) {
ipair_t hdUb(myPara.hdMax, myPara.hdMax);
ipair_t dPoints(0, myPara.K - myPara.step); //dividing points //a
std::string myRead = addr;
int readLen = myRead.length();
int nextPos = 0, prePos = -1;
bool crawlingFlag = false;
int crawlTo = -1;
while (1) {
upair_t mosaic;
uint64_t ID1, ID2;
if (!(toID(ID1, &myRead[nextPos], myPara.K) &&
toID(ID2, &myRead[nextPos + myPara.step], myPara.K))) {
break; /* containing non-acgt*/
}
mosaic = upair_t(ID1, ID2);
if (errorCall(mosaic, dPoints, hdUb, qAddr + nextPos, myPara)) {//a
updateRead(myRead, nextPos);
if (nextPos + myPara.step + myPara.K == readLen) {
break;
}
prePos = nextPos;
if (crawlingFlag == false) {
if (nextPos + myPara.step <= readLen - myPara.K - myPara.step) {
dPoints = ipair_t(myPara.K, 0);
nextPos += myPara.step;
hdUb = ipair_t(0, myPara.hdMax);
}
else {
dPoints = ipair_t(myPara.K, nextPos + myPara.K + 2 * myPara.step - readLen);
nextPos = readLen - myPara.K - myPara.step;
hdUb = ipair_t(0, myPara.hdMax);
}
} else {
hdUb = ipair_t(0, 1);
nextPos = prePos + 1;
dPoints = ipair_t(myPara.K, myPara.K - 3);
}
} else { // else errorCall
if (crawlingFlag == false){
crawlTo = nextPos;
}
if(crawlTo == prePos + 1) {
crawlingFlag = false;
} else {
crawlingFlag = true;
}
// crawling --- to do reverse crawling
if (prePos == -1 || nextPos == prePos + 1){
break;
}
nextPos = prePos + 1;
hdUb = ipair_t(0, 1);
dPoints = ipair_t(myPara.K, myPara.K - 3);
} // end if errorCall
} // end while(1)
}
void Parser::updateRead(std::string& myRead, int shift) {
if (readErr_.size()) {
// 1. adding shift to err positions w.r.t read & update read
for (unsigned int i = 0; i < readErr_.size(); ++i) {
readErr_[i].pos += shift;
myRead[readErr_[i].pos] = bits_to_char(readErr_[i].from);
}
// 2. update records_
if (records_.size() && records_.back().readID == readID_) {
(records_.back()).evec.insert((records_.back()).evec.end(),
readErr_.begin(), readErr_.end());
} else {
records_.push_back(record_t(readID_, readErr_));
}
// 3. update snKArray_ and lnKArray_ --- todo
readErr_.clear();
}
}
void candidates(ivec_t& candies, const kcvec_t& tiles, int threshold) {
for (unsigned i = 1; i < tiles.size(); ++i) {
if (tiles[i].goodCnt >= threshold)
candies.push_back(i);
}
}
void diff(evec_t& errs, uint64_t from, uint64_t to, char* qAddr, int len) {
uint64_t tmp = from ^ to;
int idx = 0;
while (tmp) {
if (tmp & 0x3) {
e_t err;
err.pos = len - 1 - idx;
err.from = from & 0x3;
err.to = to & 0x3;
err.qual = (int) qAddr[(len - 1) - idx];
errs.push_back(err);
}
tmp >>= 2;
from >>= 2;
to >>= 2;
idx++;
}
}
bool goodQuals(char* qAddr, int len, int threshold) {
for (int i = 0; i < len; ++i) {
if (qAddr[i] < threshold)
return false;
}
return true;
}
bool Parser::correctTile(kcvec_t& tiles, char* qAddr, const Para& myPara) {
if (tiles.size() == 0){
return false;
}
if (tiles[0].goodCnt >= myPara.tGoodTile) {
return true;
}
if (tiles.size() == 1) { // try to correct
if (tiles[0].goodCnt >= myPara.tCard &&
goodQuals(qAddr, myPara.K + myPara.step, myPara.Qlb)) {
return true;
}
} else {
ivec_t candies; // indices of tiles
candidates(candies, tiles, myPara.tCard);
if (tiles[0].goodCnt >= myPara.tCard) {
/*
* ec only if non-ambig correction of low qual bases
* could be identified and correcting to one of the
* high cardinality neighbors
*/
int tGoodCnt = tiles[0].goodCnt / myPara.tRatio;
ivec_t highCardNbs;
for (unsigned i = 0; i < candies.size(); ++i) {
if (tiles[candies[i]].goodCnt >= tGoodCnt)
highCardNbs.push_back(candies[i]);
}
int alterNum = 0;
for (unsigned i = 0; i < highCardNbs.size(); ++i) {
/*
* check all candidates, if ambig, then do not ec
*/
evec_t errs;
diff(errs, tiles[highCardNbs[i]].ID, tiles[0].ID,
qAddr, myPara.step + myPara.K);
for (unsigned j = 0; j < errs.size(); ++j) {
if (errs[j].qual < myPara.Qlb) {
readErr_ = errs;
alterNum++;
break;
}
}
}
if (alterNum > 1) { // ambig
readErr_.clear();
} else
return true;
} else {
/*
* ec only if non-ambig correction to one of [candies]
* could be identified
*/
if (candies.size() == 1) {
diff(readErr_, tiles[candies[0]].ID, tiles[0].ID,
qAddr, myPara.step + myPara.K);
return true;
}
}
}
return false;
}
bool Parser::errorCall(const upair_t& mosaic, const ipair_t& dPoints,
const ipair_t& hdUb, char* qAddr, const Para& myPara) {
upair_t rvMosaic(
reverse_complementary<uint32_t, uint32_t > (mosaic.first, myPara.K),
reverse_complementary<uint32_t, uint32_t > (mosaic.second, myPara.K));
uvec_t heads, tails, rvHeads, rvTails;
if (ecdata->findKmer((kmer_id_t)mosaic.first)){
heads.push_back(mosaic.first);
}
if (ecdata->findKmer((kmer_id_t)mosaic.second)){
tails.push_back(mosaic.second);
}
if (ecdata->findKmer((kmer_id_t)rvMosaic.second)){
rvHeads.push_back(rvMosaic.second);
}
if (ecdata->findKmer((kmer_id_t)rvMosaic.first)){
rvTails.push_back(rvMosaic.first);
}
ipair_t hd(0, 0);
while (hd.first <= hdUb.first) {
kcvec_t tiles, rvTiles;
tiling(tiles, heads, tails, myPara);
// print_kcvec("tiles", tiles, myPara.K + myPara.step);
tiling(rvTiles, rvHeads, rvTails, myPara);
// print_kcvec("rvtiles", rvTiles, myPara.K + myPara.step);
mergeTiles(tiles, rvTiles, myPara);
// print_kcvec("After Merging", tiles, myPara.K + myPara.step);
/*
* Error Calling Current Tile
* tiles.size() may equal to 0 due to error correction
*/
if(correctTile(tiles, qAddr, myPara))
return true;
/*
* increase Hamming Distance to search for more neighbors
*/
hd.second++;
if (hd.second <= hdUb.second) {
unitNeighbor(tails, dPoints.second, myPara);
unitNeighbor(rvHeads, dPoints.second, myPara);
} else {
hd.first++;
if (hd.first <= hdUb.first) {
unitNeighbor(heads, dPoints.first, myPara);
unitNeighbor(rvTails, dPoints.first, myPara);
}
else {
// all possible searches have been done, till this stage
// if tile[0] is the maximum and > tCard then, it is considered
// as correct due to low coverage region
int maxCnt = 0;
bool tflag = true;
if (tiles.size() > 0) maxCnt = tiles[0].goodCnt;
if (maxCnt >= myPara.tCard) {
for (unsigned i = 1; i < tiles.size(); i ++){
if (maxCnt < tiles[i].goodCnt) {
tflag = false;
break;
}
}
if (tflag) return true;
}
}
}
}
return false;
}
void Parser::unitNeighbor(uvec_t& myNB, int dPoint, const Para& myPara) {
// Commented as it is no longer used
// if(myPara.useMaskedLists){
// tableUnitNeighbor(myNB, dPoint,myPara);
// return;
// }
genericUnitNeighbor(myNB,dPoint,myPara);
}
//
// Finds the unit neighbor by generating each possible neighbor
void Parser::genericUnitNeighbor(uvec_t& myNB, int dPoint,const Para& myPara){
// Parallel Reptile ::
// Replaced this function with the query mechanism
#ifdef DEBUG
std::cout << "DPOINT : " << dPoint
<< " INPUT : " << myNB.size() << " : " ;
for(int i = 0; i < myNB.size(); i++ )
std::cout << myNB[i] << " " ;
std::cout << std::endl;
#endif
if (myNB.size() == 0 || dPoint >= myPara.K) {
#ifdef DEBUG
std::cout << "OUTPUT : NONE" << std::endl ;
#endif
return;
}
std::vector<kmer_id_t> nbIDs;
// TODO: update as required
stopPoint = dPoint;
for (unsigned i = 0; i < myNB.size(); ++i) {
get_dneighbors(myPara.K,1, (kmer_id_t)myNB[i],stopPoint,nbIDs);
}
std::sort(nbIDs.begin(), nbIDs.end());
uvec_t::iterator it
= std::unique_copy(nbIDs.begin(), nbIDs.end(), nbIDs.begin());
nbIDs.resize(it - nbIDs.begin());
#ifdef DEBUG
std::cout << "OUTPUT : " ;
#endif
for (unsigned i = 0; i < nbIDs.size(); ++i) {
if ( nbIDs[i] != 0 && ecdata->findKmerCacheAware(nbIDs[i])){
#ifdef DEBUG
std::cout << nbIDs[i] << " " ;
#endif
myNB.push_back(nbIDs[i]);
}
}
#ifdef DEBUG
std::cout << std::endl;
#endif
}
void Parser::tableMaker(const Para& myPara) {
// std::cout << "Constructing Facility Tables ... \n";
/*
* 1. split K positions into random chunks based
* on myPara.k and myPara.eSearch
*/
ivec_t indices(myPara.K, 0);
for (int i = 0; i < myPara.K; ++i) indices[i] = i;
std::random_shuffle(indices.begin(), indices.end());
//std::for_each(indices.begin(), indices.end(), print); std::cout <<"\n";
int unitSize = (log2(myPara.eSearch) / 2);
unsigned num = myPara.K / unitSize;
for (unsigned i = 0; i < num; ++i) {
ivec_t tmp(unitSize, 0);
std::copy(indices.begin() + i*unitSize, indices.begin() + (i + 1)*unitSize, tmp.begin());
//tmp.insert(tmp.end(), indices.begin() + i*unitSize,
// indices.begin() + (i + 1) * unitSize);
maskIdx_.push_back(tmp);
}
int lastUnitSize = myPara.K % unitSize;
if (lastUnitSize != 0) {
ivec_t tmp;
tmp.insert(tmp.end(), indices.begin() + num*unitSize, indices.end());
maskIdx_.push_back(tmp);
num++;
}
//std::cout << "\t" << num << " Tables to be created\n";
/*
* 2. Create Masks for tables
*/
masks_.resize(num);
for (unsigned i = 0; i < num; masks_[i] = 0xFFFFFFFF, i++);
for (unsigned i = 0; i < num; ++i) {
for (unsigned j = 0; j < maskIdx_[i].size(); ++j) {
masks_[i] &= ~(1 << (2 * maskIdx_[i][j]));
masks_[i] &= ~(1 << (2 * maskIdx_[i][j] + 1));
}
}
//std::for_each(masks_.begin(), masks_.end(), printHex);
//std::cout << "\n";
/*
* 3. Create Tables Then sort according to masks for each table
*/
for (unsigned i = 0; i < num; ++ i){
for(int j = 0; j < ecdata->m_kcount;j++)
table_.insert(table_.end(), ecdata->m_karray[j].ID);
}
/*
* sort tables
*/
int tableSize = ecdata->m_kcount;
for (unsigned i = 0; i < num; ++i) {
std::sort(table_.begin() + i*tableSize,
table_.begin() + (i + 1) * tableSize, TComp(masks_[i]));
}
// std::cout << "\tdone !\n\n";
}
bool checkPoint(const ivec_t& indices, int dPoint) {
for (unsigned i = 0; i < indices.size(); ++i) {
if (indices[i] >= dPoint)
return true;
}
return false;
}
//
// This unit neighbor function uses the table of masked lists
//
void Parser::tableUnitNeighbor(uvec_t& myNB, int dPoint, const Para& myPara) {
#ifdef DEBUG
std::cout << "DPOINT : " << dPoint
<< " INPUT : " << myNB.size() << " : " ;
for(int i = 0; i < myNB.size(); i++ )
std::cout << myNB[i] << " " ;
std::cout << std::endl;
#endif
//
if (myNB.size() == 0){
#ifdef DEBUG
std::cout << "OUTPUT : NONE" << std::endl ;
#endif
return;
}
uvec_t inputIDs(myNB.size(), 0);
for (unsigned i = 0; i < myNB.size(); inputIDs.push_back(myNB[i]), ++ i);
std::sort(inputIDs.begin(), inputIDs.end());
uvec_t nbIDs; // neigbhoring IDs
for (unsigned i = 0; i < myNB.size(); ++i) {
uint32_t myID = myNB[i];
uvec_t myNeighbor; //store ID's neighbors by searching in all tables
/*
* search in every table
*/
int tableSize = table_.size() / masks_.size();
for (unsigned j = 0; j < masks_.size(); ++j) {
if (!checkPoint(maskIdx_[j], dPoint)) continue; //a
std::pair<uvec_t::iterator, uvec_t::iterator> bounds;
bounds = std::equal_range(table_.begin() + j*tableSize,
table_.begin() + (j + 1) * tableSize, myID, TComp(masks_[j]));
// identify if HD = 1
for (uvec_t::iterator it = bounds.first; it != bounds.second; ++it) {
// check if differed position satisfies dPoint
int idx = myPara.K - 1;
bool dPointFlag = true;
//if (*it == myID) continue;
if (std::binary_search(inputIDs.begin(), inputIDs.end(), *it)) continue;
uint32_t tmp = (*it) ^ myID;
int cnt = 0;
while (tmp) {
if (idx < dPoint) {
dPointFlag = false;
break;
}
if (tmp & 0x3) cnt++;
tmp >>= 2;
}
if (cnt == 1 && dPointFlag) {
myNeighbor.push_back(*it);
}
}
}
nbIDs.insert(nbIDs.begin(), myNeighbor.begin(), myNeighbor.end());
}
/*
* remove duplicates
*/
std::sort(nbIDs.begin(), nbIDs.end());
uvec_t::iterator it
= std::unique_copy(nbIDs.begin(), nbIDs.end(), nbIDs.begin());
nbIDs.resize(it - nbIDs.begin());
/*
* now have all nbIDs from table, then search in [snKArray_]
* to get complete multiplicity and quality score information
*/
#ifdef DEBUG
uvec_t outnbs;
std::cout << "OUTPUT : " ;
#endif
for (unsigned i = 0; i < nbIDs.size(); ++i) {
if (ecdata->findKmerCacheAware(nbIDs[i])){
#ifdef DEBUG
outnbs.push_back(nbIDs[i]);
#endif
myNB.push_back(nbIDs[i]);
}
}
#ifdef DEBUG
for (int i = 0; i < outnbs.size(); ++i)
std::cout << outnbs[i] << " " ;
std::cout << std::endl;
#endif
}
/*
* Keep elements of N only if the ID equals to the last 2*len bits in tiles
*/
void updateNodes(kcvec_t& N, kcvec_t& N_rv, const kcvec_t& tiles, int len) {
std::set<uint64_t> IDs, IDs_rv;
kcvec_t tmpVec;
for (unsigned i = 0; i < tiles.size(); ++i) {
uint64_t last2k = tiles[i].ID & ((0x1 << 2 * len) - 1);
IDs.insert(last2k);
IDs_rv.insert(reverse_complementary <uint32_t, uint32_t > (last2k, len));
}
for (unsigned i = 0; i < N.size(); ++i) {
if (IDs.count(N[i].ID)) tmpVec.push_back(N[i]);
}
N = tmpVec;
tmpVec.clear();
for (unsigned i = 0; i < N_rv.size(); ++i) {
if (IDs_rv.count(N_rv[i].ID)) tmpVec.push_back(N_rv[i]);
}
N_rv = tmpVec;
}
bool unicpy(const kc_t& e1, const kc_t& e2) {
return (e1.ID == e2.ID);
}
void Parser::mergeTiles(kcvec_t& tileTo, kcvec_t& tileFrom, const Para& myPara) {
/*
* convert IDs in [tileFrom] to revcompl IDs and insert to [tileTo]
* then sort and merge (remove duplicates); keep the repID in place
*/
if (tileFrom.size() == 0) return;
/* convert to reverse compl IDs
*/
for (unsigned i = 0; i < tileFrom.size(); ++i) {
tileFrom[i].ID = reverse_complementary <uint64_t, uint64_t >
(tileFrom[i].ID, myPara.K + myPara.step);
}
if (tileTo.size() == 0){
tileTo = tileFrom;
tileFrom.clear();
return;
}
uint64_t repID = tileTo[0].ID;
tileTo.insert(tileTo.end(), tileFrom.begin(), tileFrom.end());
tileFrom.clear();
/* sort
*/
std::sort(tileTo.begin(), tileTo.end(), Knumcomp());
/* merge
*/
int idx1 = 0;
for (int idx2 = idx1 + 1; idx2 < (int)tileTo.size(); ++idx2) {
if (tileTo[idx2].ID == tileTo[idx1].ID) {
tileTo[idx1].goodCnt += tileTo[idx2].goodCnt;
tileTo[idx1].cnt += tileTo[idx2].cnt;
} else {
idx1 = idx2;
}
}
kcvec_t::iterator it =
std::unique_copy(tileTo.begin(), tileTo.end(), tileTo.begin(), unicpy);
tileTo.resize(it - tileTo.begin());
/* keep repID in the first pos
*/
if (tileTo[0].ID != repID) {
for (unsigned i = 1; i < tileTo.size(); ++i) {
if (tileTo[i].ID == repID) {
kc_t tmp = tileTo[0];
tileTo[0] = tileTo[i];
tileTo[i] = tmp;
break;
}
}
}
}
/*
* flag: true -- forward flase--reverse
*/
void Parser::tiling(kcvec_t& tiles, const uvec_t& N1,
const uvec_t& N2, const Para& myPara) {
if (N1.size() == 0 || N2.size() == 0) return;
uint64_t reptile;
if (!overlay(reptile, N1[0], N2[0], myPara)) {
std::cout << "Err: errCall, reptile construction fail\n";
exit(1);
}
kc_t output;
int idx = ecdata->findTile(reptile,output);
if (idx != -1) {
tiles.push_back(output);
}
else {
tiles.push_back(kc_t(reptile, 0, 0));
}
for (unsigned i = 0; i < N1.size(); ++i) {
for (unsigned j = 0; j < N2.size(); ++j) {
if (i == 0 && j == 0) continue;
uint64_t tmptile;
if (overlay(tmptile, N1[i], N2[j], myPara)) {
//binary search
kc_t tmpoutput;
idx = ecdata->findTile(tmptile,tmpoutput);
if (idx != -1) {
tiles.push_back(tmpoutput);
}
}
}
}
}
bool Parser::overlay(uint64_t& rslt, uint32_t n1, uint32_t n2, const Para& myPara) {
int shift = 2 * myPara.step;
uint64_t a = n1, b = n2;
// comparison suffix - prefix if necessary
if (myPara.step >= myPara.K ||
(b >> shift) == (a & ((1 << 2*(myPara.K - myPara.step)) - 1))) {
rslt = (a << shift) | b;
return true;
}
return false;
}
void Parser::output(const std::string& filename) {
/*
* Resulting file format:
* ReadID (sorted) ErrNum [pos from to qual] [pos from to qual]...
* from: reference; to: read (numercial value Aa:0 Cc:1 Gg:2 Tt:3 others 4)
* qual: quality (numerical value)
*/
std::ofstream oHandle(filename.c_str());
if (!oHandle.good()) {
std::cout << "open " << filename << " failed, correct path?\n";
exit(1);
}
for (unsigned i = 0; i < records_.size(); ++i) {
oHandle << records_[i].readID << "\t" << records_[i].evec.size();
for (unsigned j = 0; j < records_[i].evec.size(); ++j) {
oHandle << "\t" << records_[i].evec[j].pos << "\t"
<< records_[i].evec[j].from << "\t" << records_[i].evec[j].to
<< "\t" << records_[i].evec[j].qual;
}
oHandle << "\n";
}
oHandle.close();
}