Debian Codegraph @

meryl/mervin.C

raw
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <errno.h>
4 #include <string.h>
5
6 #include "bio++.H"
7 #include "sweatShop.H"
8
9 #include "libmeryl.H"
10
11 #include <algorithm>
12
13 using namespace std;
14
15 // var, old, new -- returns true if "(var == old) and var <- new"
16 //
17 // CAS - #elif (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) > 40100
18
19 const uint32 pileMax = 32768;
20
21 const uint32 kmerSize = 22;
22 const uint32 kmerBits = 2 * kmerSize;
23
24 const uint32 pilePreSize = 6;
25 const uint32 pilePreBits = 2 * pilePreSize;
26
27 const uint32 sortPreSize = 10;
28 const uint32 sortPreBits = 2 * sortPreSize;
29
30
31 class kmerPile {
32 public:
33 kmerPile(uint32 prefix) {
34 pileLen = 0;
35 pilePrefix = prefix;
36 };
37 ~kmerPile() {
38 };
39
40 void initialize(uint32 prefix) {
41 pileLen = 0;
42 pilePrefix = prefix;
43 };
44
45 void addMer(uint64 mer) {
46 pileDat[pileLen++] = mer;
47 };
48
49 void sort(void) {
50 ::sort(pileDat, pileDat + pileLen);
51 };
52
53 uint32 pileLen;
54 uint32 pilePrefix;
55
56 uint64 pileDat[pileMax];
57 };
58
59
60
61
62 class kmerSorter {
63 public:
64 kmerSorter() {
65 sorterLocked = 0;
66 sorterLen = 0;
67 sorterMax = 4;
68 sorterMer = new uint64 [sorterMax];
69 sorterCnt = new uint32 [sorterMax];
70 };
71 ~kmerSorter() {
72 delete [] sorterMer;
73 delete [] sorterCnt;
74 };
75
76 void merge(uint64 *pileDat, uint32 pileLen) {
77 uint32 nmax = MAX(16, sorterLen + pileLen / 4);
78 uint64 *nmer = new uint64 [nmax];
79 uint32 *ncnt = new uint32 [nmax];
80 uint32 npos = 0;
81
82 assert(nmax > 0);
83
84 uint32 spos = 0;
85 uint32 ppos = 0;
86
87 bool useSorterFirst = false;
88
89 if ((sorterLen > 0) && (pileLen > 0)) {
90 useSorterFirst = (sorterMer[0] < pileDat[0]);
91
92 } else if (spos < sorterLen) {
93 useSorterFirst = true;
94
95 } else if (ppos < pileLen) {
96 useSorterFirst = false;
97
98 } else {
99 assert(0);
100 }
101
102 if (useSorterFirst) {
103 nmer[0] = sorterMer[spos];
104 ncnt[0] = sorterCnt[spos];
105 spos++;
106 } else {
107 nmer[0] = pileDat[ppos];
108 ncnt[0] = 1;
109 ppos++;
110 }
111
112 while ((spos < sorterLen) && (ppos < pileLen)) {
113
114 if (nmax <= npos + 1) {
115 nmax += (pileLen - ppos) + (sorterLen - spos) + 1;
116
117 uint64 *nmermore = new uint64 [nmax];
118 uint32 *ncntmore = new uint32 [nmax];
119
120 memcpy(nmermore, nmer, sizeof(uint64) * (npos + 1));
121 memcpy(ncntmore, ncnt, sizeof(uint32) * (npos + 1));
122
123 delete [] nmer; nmer = nmermore;
124 delete [] ncnt; ncnt = ncntmore;
125 }
126
127 if (nmer[npos] == sorterMer[spos]) {
128 ncnt[npos] += sorterCnt[spos];
129 spos++;
130
131 } else if (nmer[npos] == pileDat[ppos]) {
132 ncnt[npos] += 1;
133 ppos++;
134
135 } else if (sorterMer[spos] < pileDat[ppos]) {
136 npos++;
137 nmer[npos] = sorterMer[spos];
138 ncnt[npos] = sorterCnt[spos];
139 spos++;
140
141 } else {
142 npos++;
143 nmer[npos] = pileDat[ppos];
144 ncnt[npos] = 1;
145 ppos++;
146 }
147 }
148
149 uint32 remain = (sorterLen - spos) + (pileLen - ppos);
150
151 if (nmax < npos + 1 + remain) {
152 nmax = npos + 1 + remain;
153
154 uint64 *nmermore = new uint64 [nmax];
155 uint32 *ncntmore = new uint32 [nmax];
156
157 memcpy(nmermore, nmer, sizeof(uint64) * (npos + 1));
158 memcpy(ncntmore, ncnt, sizeof(uint32) * (npos + 1));
159
160 delete [] nmer; nmer = nmermore;
161 delete [] ncnt; ncnt = ncntmore;
162 }
163
164
165
166 while (spos < sorterLen) {
167 if (nmer[npos] == sorterMer[spos]) {
168 ncnt[npos] += sorterCnt[spos];
169 } else {
170 npos++;
171 nmer[npos] = sorterMer[spos];
172 ncnt[npos] = sorterCnt[spos];
173 }
174
175 spos++;
176 }
177
178
179 while (ppos < pileLen) {
180 if (nmer[npos] == pileDat[ppos]) {
181 ncnt[npos] += 1;
182 } else {
183 npos++;
184 nmer[npos] = pileDat[ppos];
185 ncnt[npos] = 1;
186 }
187
188 ppos++;
189 }
190
191 delete [] sorterMer;
192 delete [] sorterCnt;
193
194 sorterMer = nmer;
195 sorterCnt = ncnt;
196 sorterLen = npos + 1;
197 sorterMax = nmax;
198
199 #if 1
200 bool broken = false;
201
202 for (uint32 i=1; i<sorterLen; i++) {
203 assert(sorterMer[i-1] < sorterMer[i]);
204 if (sorterMer[i-1] >= sorterMer[i])
205 broken = true;
206 }
207 #endif
208
209 };
210
211 void write(uint32 prefix, FILE *F, merylStreamWriter *W) {
212 char km[64] = {0};
213 uint32 kp = pilePreSize;
214 uint32 np = 0;
215
216 {
217 uint32 pre = prefix;
218
219 for (uint32 pp=0; pp<pilePreSize; pp++) {
220 km[--kp] = bitsToLetter[pre & 0x03];
221 pre >>= 2;
222 }
223 }
224
225 np = kmerSize - pilePreSize;
226
227 for (uint32 ii=0; ii<sorterLen; ii++) {
228 uint64 mer = sorterMer[ii];
229
230 kp = kmerSize;
231
232 for (uint32 pp=0; pp<np; pp++) {
233 km[--kp] = bitsToLetter[mer & 0x03];
234 mer >>= 2;
235 }
236
237 fprintf(F, ">"uint32FMT"\n%s\n", sorterCnt[ii], km);
238
239 if (W)
240 W->addMer(prefix, pilePreBits,
241 sorterMer[ii], kmerBits - pilePreBits,
242 sorterCnt[ii],
243 0L);
244 }
245 };
246
247 volatile uint32 sorterLocked;
248 uint32 sorterLen;
249 uint32 sorterMax;
250
251 uint64 *sorterMer;
252 uint32 *sorterCnt;
253 };
254
255
256
257
258 class kmerGlobal {
259 public:
260 kmerGlobal() {
261 inName = NULL;
262 inFile = NULL;
263
264 #if 0
265 inputBufferMax = 131072;
266 inputBufferLen = 0;
267 inputBufferPos = 0;
268 inputBuffer = new char [inputBufferMax];
269 #endif
270
271 inputBufferMax = 0;
272 inputBufferLen = 0;
273 inputBufferPos = 0;
274 inputBuffer = NULL;
275
276 outPrefix = NULL;
277 outFile = NULL;
278
279 fkPre = 0;
280 fkMer = 0;
281
282 rkPre = 0;
283 rkMer = 0;
284
285 kLen = 0;
286
287 pilesFreeLock = 0;
288 pilesFreeLen = 2048;
289 pilesFreeMax = 2 << pilePreBits;
290 pilesFree = new kmerPile * [pilesFreeMax];
291
292 memset(pilesFree, 0, sizeof(kmerPile *) * pilesFreeMax);
293
294 piles = new kmerPile * [1 << pilePreBits];
295 sorters = new kmerSorter [1 << sortPreBits];
296
297 memset(piles, 0, sizeof(kmerPile *) * (1 << pilePreBits));
298
299 for (uint32 i=0; i<pilesFreeLen; i++)
300 pilesFree[i] = new kmerPile(0);
301
302 for (uint32 i=0; i< (1 << pilePreBits); i++)
303 piles[i] = new kmerPile(i);
304
305 pilesToSortLen = 0;
306 pilesToSortMax = 2 * (1 << pilePreBits);
307 pilesToSort = new kmerPile * [pilesToSortMax];
308 };
309 ~kmerGlobal() {
310 delete [] piles;
311 delete [] sorters;
312 delete [] pilesToSort;
313 //delete [] inputBuffer;
314 };
315
316
317 void initialize(void) {
318 //inBuffer = new readBuffer(inName, 0);
319
320 #if 1
321 inputBufferMax = 0;
322 inputBufferLen = 0;
323 inputBufferPos = 0;
324 inputBuffer = (char *)mapFile(inName, &inputBufferLen, 'r');
325 #endif
326
327 naptime.tv_sec = 0;
328 naptime.tv_nsec = 166666666ULL; // 1/6 second
329 naptime.tv_nsec = 250000ULL;
330 };
331
332 kmerPile *getFreePile(uint32 prefix) {
333 kmerPile *pp;
334
335 while (__sync_bool_compare_and_swap(&pilesFreeLock, 0, 1) == false)
336 nanosleep(&naptime, 0L);
337
338 assert(pilesFreeLock == 1);
339
340 if (pilesFreeLen == 0) {
341 pilesFreeLock = 0;
342 //fprintf(stderr, "ALLOCATE PILE!\n");
343 pp = new kmerPile(prefix);
344
345 } else {
346 pp = pilesFree[--pilesFreeLen];
347 pilesFreeLock = 0;
348 }
349
350 pp->initialize(prefix);
351
352 return(pp);
353 };
354
355 void releasePile(kmerPile *pile) {
356
357 if (pilesFreeLen >= pilesFreeMax) {
358 //fprintf(stderr, "DELETE PILE!\n");
359 delete pile;
360
361 } else {
362 while (__sync_bool_compare_and_swap(&pilesFreeLock, 0, 1) == false)
363 nanosleep(&naptime, 0L);
364
365 assert(pilesFreeLock == 1);
366
367 pilesFree[pilesFreeLen++] = pile;
368
369 pilesFreeLock = 0;
370 }
371
372 };
373
374
375 void addToPile(uint64 pre, uint64 mer) {
376
377 assert(piles[pre] != NULL);
378 //if (piles[pre] == NULL)
379 // piles[pre] = getFreePile(pre);
380
381 if (piles[pre]->pileLen < pileMax) {
382 piles[pre]->addMer(mer);
383 return;
384 }
385
386 if (pilesToSortMax <= pilesToSortLen) {
387 fprintf(stderr, "realloc\n");
388 exit(1);
389 }
390
391 pilesToSort[pilesToSortLen++] = piles[pre];
392
393 piles[pre] = getFreePile(pre);
394 piles[pre]->addMer(mer);
395 };
396
397
398 kmerPile *getFullPile(void) {
399 if (pilesToSortLen == 0)
400 return(NULL);
401
402 //fprintf(stderr, "return pile "uint32FMT"\n", pilesToSort[pilesToSortLen-1]->pilePrefix);
403 return(pilesToSort[--pilesToSortLen]);
404 };
405
406
407 kmerPile *allDataLoaded(void) {
408
409 for (uint32 pp=0; pp < (1 << pilePreBits); pp++) {
410 if ((piles[pp] != NULL) &&
411 (piles[pp]->pileLen > 0)) {
412 //fprintf(stderr, "Add pile "uint32FMT" to list.\n", pp);
413 pilesToSort[pilesToSortLen++] = piles[pp];
414 } else {
415 delete piles[pp];
416 }
417
418 piles[pp] = NULL;
419 }
420
421 fprintf(stderr, "allDataLoaded()-- pilesToSortLen = "uint32FMT"\n", pilesToSortLen);
422
423 return(getFullPile());
424 };
425
426
427 void addBases(uint32 bgn, uint32 len) {
428 uint32 kp2 = kmerBits - pilePreBits - 2;
429 uint32 pp2 = pilePreBits - 2;
430
431 uint64 mpp = uint64MASK(pilePreBits);
432 uint64 mkp = uint64MASK(kmerBits - pilePreBits);
433
434 for (uint32 pos=0; pos<len; pos++) {
435 uint64 bt = letterToBits[ inputBuffer[bgn+pos] ];
436
437 if (bt > 4) {
438 kLen = 0;
439 continue;
440 }
441
442 uint64 tm = 0;
443
444 tm = fkMer >> kp2;
445 tm &= 0x00000003;
446
447 fkPre <<= 2;
448 fkPre |= tm;
449
450 fkMer <<= 2;
451 fkMer |= bt;
452
453 tm = rkMer & 0x00000003;
454
455 rkPre >>= 2;
456 rkPre |= tm << pp2;
457
458 rkMer >>= 2;
459 rkMer |= bt << kp2;
460
461 kLen++;
462
463 if (kLen < kmerSize)
464 continue;
465
466 kLen = kmerSize;
467
468 fkPre &= mpp;
469 fkMer &= mkp;
470
471 rkPre &= mpp;
472 rkMer &= mkp;
473
474 addToPile(fkPre, fkMer);
475 addToPile(rkPre, rkMer);
476 }
477 }
478
479 bool addBaseToKmer(char base) {
480 uint64 bt = letterToBits[base];
481
482 if (bt > 4) {
483 kLen = 0;
484 return(false);
485 }
486
487 uint64 tm = 0;
488
489 tm = fkMer >> (kmerBits - pilePreBits - 2);
490 tm &= 0x00000003;
491
492 fkPre <<= 2;
493 fkPre |= tm;
494
495 fkMer <<= 2;
496 fkMer |= bt;
497
498 tm = rkMer & 0x00000003;
499
500 rkPre >>= 2;
501 rkPre |= tm << (pilePreBits - 2);
502
503 rkMer >>= 2;
504 rkMer |= bt << (kmerBits - pilePreBits - 2);
505
506 kLen++;
507
508 if (kLen < kmerSize) {
509 return(false);
510 }
511
512 kLen = kmerSize;
513
514 fkPre &= uint64MASK(pilePreBits);
515 fkMer &= uint64MASK(kmerBits - pilePreBits);
516
517 rkPre &= uint64MASK(pilePreBits);
518 rkMer &= uint64MASK(kmerBits - pilePreBits);
519
520 addToPile(fkPre, fkMer);
521 addToPile(rkPre, rkMer);
522
523 return(true);
524 };
525
526
527 void write(void) {
528 char outName[FILENAME_MAX];
529
530 sprintf(outName, "%s.fasta", outPrefix);
531
532 errno = 0;
533 FILE *F = fopen(outName, "w");
534 if (errno)
535 fprintf(stderr, "Failed to open '%s' for writing: %s\n", outName, strerror(errno)), exit(1);
536
537 //merylStreamWriter *W = new merylStreamWriter(outPrefix, kmerSize, 0, sortPreBits, false);
538
539 for (uint32 ss=0; ss < (1 << sortPreBits); ss++)
540 sorters[ss].write(ss, F, NULL);
541
542 fclose(F);
543 //delete W;
544 }
545
546 char *inName;
547 FILE *inFile;
548
549 readBuffer *inBuffer;
550
551 uint64 inputBufferMax;
552 uint64 inputBufferLen;
553 uint64 inputBufferPos;
554 char *inputBuffer;
555
556 char *outPrefix;
557 FILE *outFile;
558
559 uint64 fkPre; // Forward loaded kmer
560 uint64 fkMer;
561
562 uint64 rkPre; // Reverse loaded kmer
563 uint64 rkMer;
564
565 uint32 kLen;
566
567 uint32 pilesFreeLock;
568 uint32 pilesFreeLen;
569 uint32 pilesFreeMax;
570 kmerPile **pilesFree;
571
572 kmerPile **piles;
573 kmerSorter *sorters;
574
575 struct timespec naptime;
576
577 uint32 pilesToSortLen;
578 uint32 pilesToSortMax;
579 kmerPile **pilesToSort;
580 };
581
582
583
584
585
586 uint64 bytesLoaded = 0;
587 uint64 basesLoaded = 0;
588 speedCounter bytes(" bytes %8.0f Mbytes (%8.5f Mbytes/sec\r", 1048576, 1048576, true);
589
590 // Reads input, constructs kmers, adds kmers to piles of kmers.
591 void*
592 sifterThread(void *global) {
593 kmerGlobal *glob = (kmerGlobal *)global;
594 kmerPile *pile = glob->getFullPile();
595
596 if (pile)
597 return(pile);
598
599 //if ((glob->inFile == NULL) && (glob->inBuffer == NULL))
600 // return(NULL);
601
602 anotherBase:
603 //bytesLoaded++;
604 //if ((bytesLoaded % (16 * 1048576)) == 0)
605 // fprintf(stderr, "sifterThread()-- loaded "uint64FMT" MB.\n", bytesLoaded >> 20);
606
607 #if 0
608 // Uses the readBuffer in char-by-char mode
609 //
610 char ch = glob->inBuffer->read();
611 bytes.tick();
612
613 if (glob->inBuffer->eof()) {
614 delete glob->inBuffer;
615 glob->inBuffer = NULL;
616 return(glob->allDataLoaded());
617 }
618
619 if (glob->addBaseToKmer(ch) == false)
620 goto anotherBase;
621
622 #endif
623
624
625 #if 0
626 // Uses the readBuffer in block-copy mode
627 //
628 uint32 len = glob->inBuffer->read(glob->inputBuffer, glob->inputBufferMax);
629
630 if (len == 0) {
631 delete glob->inBuffer;
632 glob->inBuffer = NULL;
633 return(glob->allDataLoaded());
634 }
635
636 glob->addBases(0, len);
637 bytes.tick(len);
638
639 #endif
640
641 #if 1
642 // Uses a direct mmap'd file
643 //
644 uint64 len = glob->inputBufferLen - glob->inputBufferPos;
645
646 if (len == 0)
647 return(NULL);
648
649 if (len > 16 * 1048576)
650 len = 16 * 1048576;
651
652 //fprintf(stderr, "Add "uint64FMT" bases.\n", len);
653
654 glob->addBases(glob->inputBufferPos, len);
655 bytes.tick(len);
656
657 glob->inputBufferPos += len;
658 #endif
659
660 pile = glob->getFullPile();
661
662 if (pile == NULL)
663 goto anotherBase;
664
665 return(pile);
666 }
667
668
669
670 // Takes a pile of kmers, sorts it, and them merges into the appropriate kmerSorter objects.
671 void
672 sorterThread(void *global, void *thread, void *thing) {
673 kmerGlobal *glob = (kmerGlobal *)global;
674 kmerPile *pile = (kmerPile *)thing;
675
676 struct timespec naptime;
677 naptime.tv_sec = 0;
678 naptime.tv_nsec = 166666666ULL; // 1/6 second
679 naptime.tv_nsec = 250000ULL;
680
681 if (pile->pileLen == 0)
682 // Nothing to add.
683 return;
684
685 pile->sort();
686
687 uint32 pileBgn = 0;
688 uint32 pileEnd = 1;
689
690 uint32 pileMaskShift = sortPreBits - pilePreBits;
691 uint32 pileDataShift = kmerBits - sortPreBits;
692
693 uint64 pileToSortPreMask = uint64MASK(sortPreBits - pilePreBits) << (kmerBits - sortPreBits);
694 uint64 pileToSortMask = uint64MASK(kmerBits - sortPreBits);
695
696 uint32 sortPre = 0;
697 uint64 pileToSort = 0;
698
699 while (pileBgn < pile->pileLen) {
700 sortPre = (pile->pilePrefix << pileMaskShift) | (pile->pileDat[pileBgn] >> pileDataShift);
701 pileToSort = pile->pileDat[pileBgn] & pileToSortPreMask;
702
703 //fprintf(stderr, "0x"uint64HEX"\n", pileToSortPreMask);
704 //fprintf(stderr, "0x"uint64HEX"\n", pileToSortMask);
705
706 while ((pileEnd < pile->pileLen) &&
707 ((pile->pileDat[pileEnd] & pileToSortPreMask) == pileToSort)) {
708 //fprintf(stderr, "0x"uint64HEX" -> 0x"uint64HEX" "uint64FMT"\n",
709 // pile->pileDat[pileEnd],
710 // pile->pileDat[pileEnd] & pileToSortMask,
711 // pile->pileDat[pileEnd] & pileToSortMask);
712 pile->pileDat[pileEnd] &= pileToSortMask;
713 pileEnd++;
714 }
715
716 while (__sync_bool_compare_and_swap(&glob->sorters[sortPre].sorterLocked, 0, 1) == false)
717 nanosleep(&naptime, 0L);
718
719 assert(glob->sorters[sortPre].sorterLocked == 1);
720
721 glob->sorters[sortPre].merge(pile->pileDat + pileBgn, pileEnd - pileBgn);
722
723 glob->sorters[sortPre].sorterLocked = 0;
724
725 pileBgn = pileEnd;
726 }
727 }
728
729
730
731 // Does nothing but delete the pile object. We don't output till the end.
732 void
733 nullThread(void *global, void *thing) {
734 kmerGlobal *glob = (kmerGlobal *)global;
735 kmerPile *pile = (kmerPile *)thing;
736
737 glob->releasePile(pile);
738 }
739
740
741
742 int
743 main(int argc, char **argv) {
744 kmerGlobal *kg = new kmerGlobal;
745
746 int arg=1;
747 int err=0;
748
749 while (arg < argc) {
750 if (strcmp(argv[arg], "-i") == 0)
751 kg->inName = argv[++arg];
752
753 else if (strcmp(argv[arg], "-o") == 0)
754 kg->outPrefix = argv[++arg];
755
756 else
757 err++;
758
759 arg++;
760 }
761 if (kg->inName == NULL)
762 err++;
763 if (kg->outPrefix == NULL)
764 err++;
765 if (err) {
766 fprintf(stderr, "usage: %s -i in.sequence -i prefix\n", argv[0]);
767 exit(1);
768 }
769
770 kg->initialize();
771
772 sweatShop *ss = new sweatShop(sifterThread,
773 sorterThread,
774 nullThread);
775
776 ss->setLoaderBatchSize(512);
777
778 ss->setNumberOfWorkers(1);
779 ss->setWriterQueueSize(16384);
780
781 //for (uint32 i=0; i<config._numSearchThreads; i++)
782 // ss->setThreadData(i, new searcherState(i));
783
784 ss->run(kg, true);
785
786 delete ss;
787
788 kg->write();
789
790 delete kg;
791
792 exit(0);
793 }