-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsplit.c
2129 lines (1843 loc) · 57.8 KB
/
split.c
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
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/********************************************************************************************
*
* Phase 1 of FastK: First the minimal core prefix trie is found over the first 1 Gbp of
* the data set and then the entire data set is scanned and partitioned into super-mers
* that are sent to file buckets according to the trie.
*
* Author : Gene Myers
* Date : October 2020
*
********************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <math.h>
#include <pthread.h>
#include "libfastk.h"
#include "FastK.h"
#define USE_MAPPING
#undef HISTOGRAM_TEST
#undef MAXIMUM_TEST
#undef FIND_MTHRESH
#undef DEBUG_PADDED
#define PAD_LEVEL 0
#undef DEBUG_SCHEME
#undef DEBUG_DISTRIBUTE
#undef SHOW_PACKETS
#define PACKET -1
#undef DEBUG_COMPRESS
#undef DEBUG_KMER_SPLIT
#undef CHECK_KMER_SPLIT
#define THREAD pthread_t
/*******************************************************************************************
*
* DETERMINE PARTITION FOR SUPER K-MER DISTRIBUTION USING FIRST BLOCK
* int Determine_Scheme(DATA_BLOCK *block)
* Determine base mapping and then the core prefix trie in a series of
* scans that measure the tentative core prefix frequencies on the large (1Gbp)
* supplied training set in block. Once the core trie is computed, then
* assign prefixes to buckets as evenly as possible with a heuristic.
* Return the # of k-mers in the longest super-mer.
*
********************************************************************************************/
#define MIN_LEN 5 // Seed minimizer length
#define MIN_TOT 0x400 // = 4^MIN_LEN
// Translation vectors
static IO_UTYPE Dran[256]; // Dran[x] = 0,1,2,3 for a,c,g,t
static IO_UTYPE Fran[256]; // fran[x] = 3,2,1,0 for a,c,g,t
static uint64 Tran[256]; // Tran[x] in 0-3 for a,c,g,t according to frequency
static uint64 Cran[256]; // Cran[x] complement of Tran[x] shifted by MIN_L1 or later PAD_L1
static uint64 Sran[4]; // Tran[x] for acgt = 0123, 4 for all other chars
static uint64 Bran[4]; // Cran[x] for acgt = 0123, 4 for all other chars
// Padded Minimizer scheme
static int PAD; // # of base pairs beyond MIN_LEN
static int PAD2; // 2*PAD
static int PAD_LEN; // MIN_LEN + PAD
static int PAD_L1; // = PAD_LEN-1
static int64 PAD_TOT; // = 4^PAD_LEN
static uint64 PAD_MSK; // = PAD_TOT-1
static int MAX_NRUN; // 1 + 63*2*MAX_SUPER 0's = length of longest possible invalid block
static int Min_States;
static int *Min_Part; // The core prefix trie: MP[x] < 0 => goto a-MP[x] on base a
// MP[x] >= 0 => bucket for prefix
typedef struct
{ DATA_BLOCK *block;
int beg;
int end;
int64 freq[256];
int64 nmin;
int64 *count;
} Partition_Arg;
// Thread to sample the frequency of bases (needed for base mapping Tran/Cran
static void *frequency_thread(void *arg)
{ Partition_Arg *data = (Partition_Arg *) arg;
DATA_BLOCK *block = data->block;
int64 *freq = data->freq;
int p, q;
char *s;
for (p = 0; p < 256; p++)
freq[p] = 0;
s = block->bases + block->boff[data->beg];
q = block->boff[data->end] - block->boff[data->beg];
for (p = 0; p < q; p++)
freq[(int) s[p]] += 1; // Don't care if a few \0 counts are recorded
return (NULL);
}
// for reads [beg,end) compute # of kmers for each core minimizer prefix in proposed scheme
static void *padded_minimizer_thread(void *arg)
{ Partition_Arg *data = (Partition_Arg *) arg;
DATA_BLOCK *block = data->block;
int beg = data->beg;
int end = data->end;
int64 *count = data->count;
char *bases = block->bases;
int *boff = block->boff+1;
int P2M2 = PAD2-2;
#ifdef DEBUG_PADDED
int P = (PAD_LEN-1)/2 + 1;
int M = (MIN_LEN-1)/2 + 1;
#endif
int force;
int i, p, q, x;
uint64 c, u;
char *s, *t;
uint64 min[MOD_LEN];
uint64 mp, mc;
int m, n;
int last;
int nmin;
int b, v;
int o;
nmin = 0;
t = bases + boff[beg-1];
for (i = beg; i < end; i++)
{ s = t;
t = bases + boff[i];
s += BC_PREFIX;
q = (t-s) - 1;
if (q < KMER)
continue;
m = 0;
mc = PAD_TOT;
c = u = 0;
for (p = 0; p < KMER; p++)
{ x = s[p];
c = ((c << 2) | Tran[x]) & PAD_MSK;
u = (u >> 2) | Cran[x];
#ifdef DEBUG_PADDED
if (PAD == PAD_LEVEL)
printf(" %5d: %c %0*llx %0*llx",p,x,P,c,P,u);
fflush(stdout);
#endif
if (p >= PAD_L1)
{ if (u < c)
mp = u;
else
mp = c;
min[p] = mp;
if (mp < mc)
{ m = p;
mc = mp;
}
#ifdef DEBUG_PADDED
if (PAD == PAD_LEVEL)
printf(" %0*llx <%5d:%0*llx>",P,mp,m,P,mc);
fflush(stdout);
#endif
}
#ifdef DEBUG_PADDED
if (PAD == PAD_LEVEL)
printf("\n");
fflush(stdout);
#endif
}
#ifdef DEBUG_PADDED
if (PAD == PAD_LEVEL)
printf(" -----\n");
fflush(stdout);
#endif
last = KMER-1;
for (p = KMER; p < q; p++)
{ x = s[p];
c = ((c << 2) | Tran[x]) & PAD_MSK;
u = (u >> 2) | Cran[x];
if (u < c)
mp = u;
else
mp = c;
min[p & MOD_MSK] = mp;
force = (p-m >= MAX_SUPER);
if (force || mp < mc)
{ o = (mc >> PAD2);
v = count[o];
b = P2M2;
while (v < 0)
{ o = ((mc >> b) & 0x3) - v;
v = count[o];
b -= 2;
}
count[o] += (p-last);
nmin += 1;
#ifdef DEBUG_PADDED
if (PAD == PAD_LEVEL)
{ if (force)
printf(" >");
else
printf(" +");
printf(" (%d) -> %0*llx / %0*llx -> %d\n",
p-last,M,mc>>PAD2,P,mc>>(b+2),o);
}
fflush(stdout);
#endif
if (force)
{ mc = min[(++m) & MOD_MSK];
for (n = m+1; n <= p; n++)
{ mp = min[n & MOD_MSK];
if (mp < mc)
{ m = n;
mc = mp;
}
}
}
else
{ m = p;
mc = mp;
}
last = p;
}
#ifdef DEBUG_PADDED
if (PAD == PAD_LEVEL)
printf(" %5d: %c %0*llx %0*llx %0*llx <%5d:%0*llx>\n",p,x,P,c,P,u,P,mp,m,P,mc);
fflush(stdout);
#endif
}
#ifdef DEBUG_PADDED
if (PAD == PAD_LEVEL)
printf("\n");
fflush(stdout);
#endif
}
data->nmin = nmin;
return (NULL);
}
// assign each core minimzer to a distribution bucket so that the k-mers
// will be distributed as evenly as possible.
static int64 *_CNT;
static int PSORT(const void *l, const void *r)
{ int x = *((int *) l);
int y = *((int *) r);
if (_CNT[x] > _CNT[y])
return (-1);
else if (_CNT[x] == _CNT[y])
return (0);
else
return (1);
}
static void assign_pieces(int nstates, int64 *count, int nparts, int64 pmer)
{ int64 *buck;
int *perm;
int i, j, n, x;
int64 p, q;
int64 v, t;
perm = Malloc(sizeof(int)*nstates,"Allocating permutation array");
buck = Malloc(sizeof(int64)*nparts,"Allocating permutation array");
for (i = 0; i < nstates; i++)
perm[i] = i;
_CNT = count;
qsort(perm,nstates,sizeof(int),PSORT);
#ifdef DEBUG_SCHEME
printf("\nPieces:\n");
for (i = 0; i < nstates; i++)
if (count[perm[i]] > 0)
printf(" %5d: %7lld -> %5.2f%%\n",perm[i],count[perm[i]],(100.*count[perm[i]])/pmer);
#endif
for (i = 0; i < nparts; i++)
buck[i] = 0;
for (i = 0; i < nstates; i++)
{ x = perm[i];
p = count[x];
if (count[x] < 0)
continue;
if (p == 0)
{ count[x] = nparts-1;
continue;
}
v = 0;
for (j = 0; j < nparts; j++) // if fits in >1 bucket, pick at random weighted
if (buck[j] + p <= pmer) // by how much room each available bucket has
v += pmer-buck[j];
if (v == 0)
{ n = 0;
for (j = 1; j < nparts; j++)
if (buck[j] < buck[n])
n = j;
buck[n] += p;
count[x] = n;
}
else
{ t = v*drand48();
v = 0;
for (j = 0; j < nparts; j++)
if (buck[j] + p <= pmer)
{ v += pmer-buck[j];
if (v >= t)
{ buck[j] += p;
count[x] = j;
break;
}
}
}
}
#ifdef DEBUG_SCHEME
{ int64 bmax, bmin;
printf("\nPacking:\n");
bmax = bmin = buck[0];
for (i = 0; i < nparts; i++)
{ printf(" %7llu -> %5.2f%%\n",buck[i],(100.*buck[i])/pmer);
if (bmax < buck[i])
bmax = buck[i];
else if (bmin > buck[i])
bmin = buck[i];
}
printf(" Range %lld - %lld => %g%% diff\n",bmin,bmax,(100.*(bmax-bmin))/pmer);
}
#endif
for (i = 0; i < nstates; i++)
if (perm[i] >= 0)
{ j = i;
p = count[i];
do
{ j = perm[j];
q = count[j];
count[j] = p;
p = q;
}
while (j != i);
}
free(buck);
free(perm);
}
#ifdef DEBUG_SCHEME
static char Invert[4];
static void _print_tree(int lev, int i, int64 ktot, int64 *count)
{ int j, a;
if (count[i] >= 0)
printf(" %10lld %.3f%% (%d)\n",count[i],(100.*count[i])/ktot,lev);
else
{ printf("\n");
j = -count[i];
for (a = 0; a < 4; a++)
{ printf("%*s -> %c:",2*lev,"",Invert[a]);
_print_tree(lev+1,j+a,ktot,count);
}
}
}
static void print_tree(int64 ktot, int64 *count)
{ int i;
for (i = 0; i < MIN_TOT; i++)
{ printf(" %5d:",i);
_print_tree(0,i,ktot,count);
}
}
static void _print_ass(int lev, int i, int *part)
{ int j, a;
if (part[i] >= 0)
printf(" %d\n",part[i]);
else
{ printf("\n");
j = -part[i];
for (a = 0; a < 4; a++)
{ printf("%*s -> %c:",2*lev,"",Invert[a]);
_print_ass(lev+1,j+a,part);
}
}
}
static void print_ass(int *part)
{ int i;
for (i = 0; i < MIN_TOT; i++)
{ printf(" %5d:",i);
_print_ass(0,i,part);
}
}
#endif
static void _refine_tree(int lev, int i, int64 kthresh, int64 *count)
{ int j, a;
lev += 1;
if (count[i] >= 0)
{ if (count[i] > kthresh)
{
#ifdef DEBUG_SCHEME
printf(" U %d->%d: %lld > %lld (%d)\n",i,Min_States,count[i],kthresh,lev);
#endif
count[i] = -Min_States;
for (a = 0; a < 4; a++)
count[Min_States++] = 0;
if (lev > PAD)
#ifdef FIND_MTHRESH
PAD += 1;
#else
PAD += 2;
#endif
}
else
count[i] = 0;
}
else
{ j = -count[i];
for (a = 0; a < 4; a++)
_refine_tree(lev,j+a,kthresh,count);
}
}
static void refine_tree(int64 kthresh, int64 *count)
{ int i;
for (i = 0; i < MIN_TOT; i++)
_refine_tree(0,i,kthresh,count);
}
#ifdef HISTOGRAM_TEST
int64 *COUNT;
int HSORT(const void *l, const void *r)
{ int x = *((int *) l);
int y = *((int *) r);
return (COUNT[y] - COUNT[x]);
}
#endif
static char DNA[4] = { 'a', 'c', 'g', 't' };
// Determine the base mapping, the core prefix trie, and the assignment of core prefixes
// to buckets (set up in globals PAD and Min_Map, et.c above)
int Determine_Scheme(DATA_BLOCK *block)
{ int64 *count;
int nreads, npieces;
int64 ktot, mtot, kthresh;
int64 max_count, last_max;
int i, j;
#if !(defined(DEBUG_MINIMIZER0) && defined(DEBUG_MINIMIZER1) && defined(DEBUG_PADDED))
THREAD threads[NTHREADS];
#endif
Partition_Arg parmt[NTHREADS];
(void) DNA;
nreads = block->nreads;
npieces = 2*NPARTS;
parmt[0].beg = 0;
for (i = 1; i < NTHREADS; i++)
parmt[i].beg = parmt[i-1].end = (((int64) nreads) * i) / NTHREADS;
parmt[NTHREADS-1].end = nreads;
for (i = 0; i < NTHREADS; i++)
parmt[i].block = block;
#ifdef DEBUG_SCHEME
printf(" K = %d, Need %d pieces, Training on %lld bases\n",
KMER,npieces,block->boff[nreads]-nreads);
#endif
// Determine best mapping of bases based on their frequency
for (i = 1; i < NTHREADS; i++)
pthread_create(threads+i,NULL,frequency_thread,parmt+i);
frequency_thread(parmt);
for (i = 1; i < NTHREADS; i++)
pthread_join(threads[i],NULL);
{ int64 *freq, f;
int assn[4];
int c;
#if defined(DEBUG_SCHEME) || defined(HISTOGRAM_TEST)
int64 m, ftot;
#endif
freq = parmt[0].freq;
for (i = 0; i < 256; i++)
for (j = 0; j < NTHREADS; j++)
freq[i] += parmt[j].freq[i];
freq[0] = freq['a'] + freq['A'];
freq[1] = freq['c'] + freq['C'];
freq[2] = freq['g'] + freq['G'];
freq[3] = freq['t'] + freq['T'];
#if defined(DEBUG_SCHEME) || defined(HISTOGRAM_TEST)
ftot = m = 0;
#endif
for (i = 0; i < 4; i++)
{ c = 0;
f = freq[i];
for (j = 0; j < 4; j++)
if (freq[j] < f)
c += 1;
else if (freq[j] == f && j < i)
c += 1;
assn[i] = c;
#if defined(DEBUG_SCHEME) || defined(HISTOGRAM_TEST)
if (f > m)
m = f;
ftot += f;
#endif
}
#ifndef USE_MAPPING
for (i = 0; i < 4; i++)
assn[i] = i;
#endif
for (i = 0; i < 256; i++)
Tran[i] = assn[0];
Tran['a'] = Tran['A'] = assn[0];
Tran['c'] = Tran['C'] = assn[1];
Tran['g'] = Tran['G'] = assn[2];
Tran['t'] = Tran['T'] = assn[3];
#if defined(DEBUG_SCHEME) || defined(HISTOGRAM_TEST)
printf(" Most freq = %lld gc = %d%%\n",
m,(int) ((100.*(freq[1]+freq[2]))/ftot));
for (i = 0; i < 4; i++)
printf(" Tran[%c] -> %d (%lld / %7.4f)\n",DNA[i],assn[i],freq[i],(100.*freq[i])/ftot);
Invert[Tran['a']] = 'a';
Invert[Tran['c']] = 'c';
Invert[Tran['g']] = 'g';
Invert[Tran['t']] = 't';
#endif
}
for (i = 0; i < 256; i++)
Dran[i] = 4;
Dran['a'] = Dran['A'] = 0;
Dran['c'] = Dran['C'] = 1;
Dran['g'] = Dran['G'] = 2;
Dran['t'] = Dran['T'] = 3;
for (i = 0; i < 256; i++)
Fran[i] = 4;
Fran['a'] = Fran['A'] = 3;
Fran['c'] = Fran['C'] = 2;
Fran['g'] = Fran['G'] = 1;
Fran['t'] = Fran['T'] = 0;
// Iteratively determine the padding needed for each MIN_LEN-mer given the target # of pieces
// Initial prefix trie is the complete quartenary tree of height MIN_LEN
count = Malloc(sizeof(int64)*MIN_TOT*NTHREADS,"Allocating count array");
if (count == NULL)
Clean_Exit(1);
PAD = 0;
for (i = 0; i < MIN_TOT; i++)
count[i] = 0;
Min_States = MIN_TOT;
last_max = 0;
while (1)
{ int o;
// Compute stats with current prefix trie
PAD2 = 2*PAD;
PAD_LEN = MIN_LEN + PAD;
PAD_TOT = (((int64) MIN_TOT) << PAD2);
PAD_L1 = PAD_LEN - 1;
PAD_MSK = PAD_TOT - 1;
MAX_SUPER = KMER - PAD_L1;
{ uint64 ct = (Tran['t'] << (2*PAD_L1));
for (i = 0; i < 256; i++)
Cran[i] = ct;
Cran['a'] = Cran['A'] = ct;
Cran['c'] = Cran['C'] = (Tran['g'] << (2*PAD_L1));
Cran['g'] = Cran['G'] = (Tran['c'] << (2*PAD_L1));
Cran['t'] = Cran['T'] = (Tran['a'] << (2*PAD_L1));
}
parmt[0].count = count;
for (i = 1; i < NTHREADS; i++)
{ parmt[i].count = count + i*Min_States;
memcpy(parmt[i].count,count,Min_States*sizeof(int64));
}
#ifdef DEBUG_SCHEME
printf("\n Scheme: padding = %d(%d), # states = %d, target = %.3f%%\n",
PAD,MAX_SUPER,Min_States,100./npieces);
#endif
#ifdef DEBUG_PADDED
for (i = 0; i < NTHREADS; i++)
padded_minimizer_thread(parmt+i);
#else
for (i = 1; i < NTHREADS; i++)
pthread_create(threads+i,NULL,padded_minimizer_thread,parmt+i);
padded_minimizer_thread(parmt);
for (i = 1; i < NTHREADS; i++)
pthread_join(threads[i],NULL);
#endif
// For each leaf/core prefix, increase padding if frequency is still too large
// First just count how much bigger trie will be
ktot = 0;
for (i = 0; i < Min_States; i++)
if (count[i] >= 0)
{ for (j = 1; j < NTHREADS; j++)
count[i] += parmt[j].count[i];
ktot += count[i];
}
mtot = 0;
for (j = 0; j < NTHREADS; j++)
mtot += parmt[j].nmin;
if (ktot < (block->totlen - (KMER-1)*block->nreads)/2)
{ fprintf(stderr,
"\n%s: Warning: Too much of the data is in reads on the order of the k-mer size\n",
Prog_Name);
}
kthresh = ktot/npieces;
max_count = 0;
o = Min_States;
for (i = 0; i < Min_States; i++)
if (count[i] >= 0)
{ if (count[i] > kthresh)
o += 4;
if (count[i] > max_count)
max_count = count[i];
}
#ifdef MAXIMUM_TEST
{ int64 biggest;
biggest = 0;
for (i = 0; i < MIN_TOT; i++)
if (count[i] > biggest)
biggest = count[i];
printf("max = %6.3f => %.2f pieces\n",(100.*biggest)/ktot,ktot/(1.*biggest));
exit (0);
}
#endif
#ifdef HISTOGRAM_TEST
{ int perm[MIN_TOT];
int numc;
for (i = 0; i < MIN_TOT; i++)
perm[i] = i;
COUNT = count;
qsort(perm,MIN_TOT,sizeof(int),HSORT);
if (MIN_LEN & 0x1)
numc = MIN_TOT/2;
else
numc = (MIN_TOT + (MIN_TOT>>MIN_LEN))/2;
for (i = 0; i < numc; i++)
printf("%5d %5d %6.3f\n",i,perm[i],(100.*count[perm[i]])/ktot);
printf("max = %6.3f => %.2f pieces\n",(100.*count[perm[0]])/ktot,ktot/(1.*count[perm[0]]));
exit (0);
}
#endif
#ifdef DEBUG_SCHEME
printf(" S_k = %lld, S_m = %lld, super ave = %.1f, kthr = %lld\n",
ktot,mtot,(1.*ktot)/mtot,kthresh);
printf(" States needing spliting = %d",(o-Min_States)/4);
if (PAD > 0)
printf(", gain = %.3f",(1.*last_max)/max_count);
printf("\n");
// print_tree(ktot,count);
#endif
// If trie is core or improvment is less than 2%, stop
if (o == Min_States)
break;
if (PAD > 0 && last_max < 1.02*max_count)
{ npieces = ktot/max_count+1;
NPARTS = npieces/2;
fprintf(stderr," Even split not possible, dividing into %d parts (%.1f x request)\n",
NPARTS,(1.*max_count)/kthresh);
break;
}
if (PAD_LEN >= KMER-1)
break;
// Else adjust trie size and actually do the refinement
count = Realloc(count,sizeof(int64)*o*NTHREADS,"Expanding count array");
if (count == NULL)
Clean_Exit(1);
refine_tree(kthresh,count);
Min_States = o;
#ifdef DEBUG_SCHEME
printf("Max Bucket = %.3f%% Padding = %d",(100.*max_count)/ktot,PAD);
#endif
last_max = max_count;
}
if (KMER < PAD_LEN)
{ fprintf(stderr,"\n%s: K-mer must be at least %d\n",Prog_Name,PAD_LEN);
Clean_Exit(1);
}
if (VERBOSE)
fprintf(stderr," Using %d-minimizers with %d core prefixes\n",PAD_LEN,Min_States);
#ifdef FIND_MTHRESH
Clean_Exit(1);
#endif
#ifdef DEBUG_SCHEME
print_tree(ktot,count);
#endif
// Assign buckets to core prefixes and finalize data structure
assign_pieces(Min_States,count,NPARTS,ktot/NPARTS);
Min_Part = (int *) count;
for (i = 0; i < Min_States; i++)
Min_Part[i] = count[i];
Min_Part = Realloc(Min_Part,sizeof(int)*Min_States,"Finalizing count array");
if (Min_Part == NULL)
Clean_Exit(1);
#ifdef DEBUG_SCHEME
printf("\nPadded Assignments: # states = %d\n",Min_States);
print_ass(Min_Part);
fflush(stdout);
#endif
return (MAX_SUPER);
}
/*******************************************************************************************
*
* SUPER K-MER DISTRIBUTOR
*
* void Split_Kmers(Input_Partition *io, char *root)
*
* The input is scanned in parallel by threads working on each partition of it
* specified by io, and for each the data is partitioned into super-mers that
* are distibuted to blocks according to the core trie encoded in Min_Part.
* Split_Kmers calls Scan_All_Input in the io.c module, which spawns the threads,
* each feeding:
*
* void Distribute_Block(DATA_BLOCK *block, int tid)
*
* with the thread's input block at a time. Each thread uses system IO doing its
* own buffering and statistics gathering. The super-mer packets sent to each
* stream are bit compacted.
*
********************************************************************************************/
// Stuff ints and DNA super-mers into bit packed buffer
static inline IO_UTYPE *Stuff_Int(int64 n, int nbits, IO_UTYPE *buf, int *bitp)
{ int rem;
IO_UTYPE val;
rem = *bitp;
val = (IO_UTYPE) n;
#ifdef DEBUG_COMPRESS
printf("v = %d/%llx b=%d/%016llx ->",nbits,val,rem,*buf);
#endif
if (rem > nbits)
{ rem -= nbits;
*buf |= (val << rem);
#ifdef DEBUG_COMPRESS
printf(" b = %d/%016llx\n",rem,*buf);
#endif
}
else if (rem == nbits)
{ *buf++ |= val;
rem = IO_UBITS;
*buf = 0;
#ifdef DEBUG_COMPRESS
printf(" b = %d/%016llx\n",rem,buf[-1]);
#endif
}
else
{ *buf++ |= (val >> (nbits-rem));
rem += IO_UBITS - nbits;
*buf = (val << rem);
#ifdef DEBUG_COMPRESS
printf(" b = %d/%016llx|%016llx\n",rem,buf[-1],*buf);
#endif
}
*bitp = rem;
return (buf);
}
static inline IO_UTYPE *Stuff_Seq(char *s, int len, IO_UTYPE *buf, int *bitp, int flip, int *pref)
{ int rem, i;
IO_UTYPE val, pre4;
rem = *bitp;
#ifdef DEBUG_COMPRESS
printf("seq %d/%d b=%d/%016llx\n",len,flip,rem,*buf);
#endif
pre4 = 0;
if (flip)
{ for (i = 1; i <= 4; i++)
{ val = Fran[(int) s[len-i]];
if (rem > 2)
{ rem -= 2;
*buf |= (val << rem);
#ifdef DEBUG_COMPRESS
printf(" %llx: b = %2d/%016llx\n",val,rem,*buf);
#endif
}
else if (rem == 2)
{ *buf++ |= val;
rem = IO_UBITS;
*buf = 0;
#ifdef DEBUG_COMPRESS
printf(" %llx: b = %2d/%016llx\n",val,rem,buf[-1]);
#endif
}
else
{ *buf++ |= (val >> 1);
rem = IO_UBITS - 1;
*buf = (val << rem);
#ifdef DEBUG_COMPRESS
printf(" %llx: b = %2d/%016llx|%016llx\n",val,rem,buf[-1],*buf);
#endif
}
pre4 = (pre4 << 2 | val);
}
for (i = len-5; i >= 0; i--)
{ val = Fran[(int) s[i]];
if (rem > 2)
{ rem -= 2;
*buf |= (val << rem);
#ifdef DEBUG_COMPRESS
printf(" %llx: b = %2d/%016llx\n",val,rem,*buf);
#endif
}
else if (rem == 2)
{ *buf++ |= val;
rem = IO_UBITS;
*buf = 0;
#ifdef DEBUG_COMPRESS
printf(" %llx: b = %2d/%016llx\n",val,rem,buf[-1]);
#endif
}
else
{ *buf++ |= (val >> 1);
rem = IO_UBITS - 1;
*buf = (val << rem);
#ifdef DEBUG_COMPRESS
printf(" %llx: b = %2d/%016llx|%016llx\n",val,rem,buf[-1],*buf);
#endif
}
}
}
else
{ for (i = 0; i < 4; i++)
{ val = Dran[(int) s[i]];
if (rem > 2)
{ rem -= 2;
*buf |= (val << rem);
#ifdef DEBUG_COMPRESS
printf(" %llx: b = %2d/%016llx\n",val,rem,*buf);
#endif
}
else if (rem == 2)
{ *buf++ |= val;
rem = IO_UBITS;
*buf = 0;
#ifdef DEBUG_COMPRESS
printf(" %llx: b = %2d/%016llx\n",val,rem,buf[-1]);
#endif
}
else
{ *buf++ |= (val >> 1);
rem = IO_UBITS - 1;
*buf = (val << rem);
#ifdef DEBUG_COMPRESS
printf(" %llx: b = %2d/%016llx|%016llx\n",val,rem,buf[-1],*buf);
#endif
}
pre4 = (pre4 << 2 | val);
}
for (i = 4; i < len; i++)
{ val = Dran[(int) s[i]];
if (rem > 2)
{ rem -= 2;
*buf |= (val << rem);
#ifdef DEBUG_COMPRESS
printf(" %llx: b = %2d/%016llx\n",val,rem,*buf);
#endif
}
else if (rem == 2)
{ *buf++ |= val;
rem = IO_UBITS;
*buf = 0;
#ifdef DEBUG_COMPRESS
printf(" %llx: b = %2d/%016llx\n",val,rem,buf[-1]);
#endif
}
else
{ *buf++ |= (val >> 1);
rem = IO_UBITS - 1;
*buf = (val << rem);
#ifdef DEBUG_COMPRESS
printf(" %llx: b = %2d/%016llx|%016llx\n",val,rem,buf[-1],*buf);
#endif
}
}
}
#ifdef DEBUG_COMPRESS
printf(" pref = %llx\n",pre4);
#endif
*bitp = rem;
*pref = (int) pre4;
return (buf);
}
// Super-mer output files & buffering
typedef struct
{ int stream; // Open stream
char *sname; // File name for stream
int64 kmers; // Number of k-mers
int64 nmers; // Numer of super-mers
int64 fours[256]; // fours[i] = Count of canonical super-mers with first byte i
int bbits; // Current bit offset in current word
IO_UTYPE *bptrs; // Current word being stuffered in buffer
IO_UTYPE *data; // End of buffer (start is at data-IO_BUF_LEN)
int nbits; // # of bits for last profile write (if DO_PROFILE)