forked from thegenemyers/FASTGA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGIXmake.c
executable file
·1598 lines (1365 loc) · 44.4 KB
/
GIXmake.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
/*******************************************************************************************
*
* Produce a k-mer index of genome's contigs suitable for finding adaptamer seed matches.
* As such only k-mers whose count is less than the adaptamer frequency cutoff are in
* the index which consists of a FASTK k-mer table and an associated position list that
* contains the positions at which each k-mer occurs in order of the k-mers of the table.
* What would normally be the count field for a k-mer contains the lcp of the k-mer with
* its predecessor, and the # of positions at which the k-mer occurs, each in a byte.
*
* Author: Gene Myers
* Date : February 2023
*
*******************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <math.h>
#include <pthread.h>
#include <sys/resource.h>
#include "libfastk.h"
#include "GDB.h"
static char *Usage[] =
{ "[-v] [-T<int(8)>] [-P<dir(/tmp)>] [-k<int(40)] [-f<int(10)>]",
"( <source:path>[.1gdb] | <source:path>[<fa_extn>|<1_extn>] [<target:path>[.gix]] )"
};
static int FREQ; // -f
static int VERBOSE; // -v
static char *SORT_PATH; // -P
static char *TPATH;
static char *TROOT;
static char *POST_NAME;
static int NTHREADS; // by default 8
static int KMER; // by default 40, must be >= 12 and divisible by 4
static int KBYTES; // Bytes for 2-bit compress k-mer (KMER/4)
#undef DEBUG_MAP
#undef DEBUG_THREADS
#undef DEBUG_SORT
#define BUFFER_LEN 1000000
static int *Perm; // Size sorted permutation of contigs
static int *InvP; // Inverse of Perm
static int PostBytes; // # of bytes needed for a position
static int ContBytes; // # of bytes for a contig + sign bit
static int Comp[256]; // DNA complement of packed byte
static int Select[256]; // 1st k-mer byte -> block (of NTHREAD)
// For p in [0,NTHREADS):
static int *DBsplit; // DB split: contig [DBsplit[p],DBsplit[p+1])
static int64 *DBpost; // DB post: post of start of contig DBsplit[p] is DBpost[p]
static int *Ksplit; // Kmer split: 1st bytes [Ksplit[p],Ksplit[p+1])
static int64 **Buckets; // NTHREADS x 256 array:
// B[i][j] = # of posts whose canonical k-mer's 1st byte
// is j from block DBsplit[i],DBsplit[i+1]
// Also used as "fingers" for loading initial sort array.
static int *Units; // NTHREADS^2 IO units for distribution and import & k-mer table parts
static int *Pnits; // NTHREADS^2 extra IO units for post parts
typedef struct
{ int beg;
int end;
int64 off;
} Range;
extern void msd_sort(uint8 *array, int64 nelem, int rsize, int ksize,
int64 *part, int nthreads, Range *range);
/***********************************************************************************************
*
* DISTRIBUTION PHASE: Output signed posts for each 1/NTHREAD of the data and the k-mers
* At completion file ._post.<jobid>.<i*NTHREADS+j>.idx contains the signed posts from
* contigs (DBsplit[j],DBplist[j+1]) whose k-mers 1st byte is in Ksplit[i],Ksplit[i+1].
*
**********************************************************************************************/
typedef struct
{ int beg;
int end;
uint8 *seq;
uint8 *neq;
uint8 *ceq;
int64 buck[256];
} BP;
// Thread computes neq[beg..end-1] and ceq[beg..end-1] given seq
static void *pack_thread(void *args)
{ BP *parm = (BP *) args;
int beg = parm->beg;
int end = parm->end;
uint8 *seq = parm->seq;
uint8 *neq = parm->neq;
uint8 *ceq = parm->ceq;
int i, k;
uint8 *s1, *s2, *s3, *n1;
s1 = seq+1;
s2 = seq+2;
s3 = seq+3;
for (i = beg; i < end; i += 4)
{ neq[i] = (seq[i] << 6) | (s1[i] << 4) | (s2[i] << 2) | s3[i];
ceq[i] = Comp[neq[i]];
}
n1 = neq-1;
for (k = 1; k < 4; k++)
for (i = beg+k; i < end; i += 4)
{ neq[i] = (n1[i] << 2) | s3[i];
ceq[i] = Comp[neq[i]];
}
return (NULL);
}
// Given neq & ceq, upon completion, for i in [beg,end), seq[i] = file k-mer starting at i should
// go to and whether it should be complemented or not (0x80 flag) in order to be canonical.
// Also accumulates # of these k-mers with a given 1st byte in buck.
static void *map_thread(void *args)
{ BP *parm = (BP *) args;
int beg = parm->beg;
int end = parm->end;
uint8 *seq = parm->seq;
uint8 *neq = parm->neq;
uint8 *ceq = parm->ceq;
int64 *buck = parm->buck;
int kspn = KMER-4;
int i, u, v;
for (i = beg; i < end; i++)
{ for (u = i, v = i+kspn; neq[u] == ceq[v]; u += 4, v -= 4)
if (u >= v)
break;
if (ceq[v] < neq[u])
{ u = ceq[i+kspn];
seq[i] = Select[u] | 0x80;
}
else
{ u = neq[i];
seq[i] = Select[u];
}
buck[u] += 1;
}
return (NULL);
}
typedef struct
{ int tid;
int inum;
uint8 *seq;
int len;
int out;
uint8 *buffer;
uint8 *bend;
int64 post;
int64 last;
} DP;
// The thread scans seq and sends those posts assigned to file tid*Nthreads + ? to their
// designated file relative to the last emission in compressed form:
// x0 -> byte, 6-bit post delta with sign x
// x10 -> short, 13-bit ...
// x110 -> short, 28-bit ...
// x111 -> 0x10000000 spacer
static void *distribute_thread(void *args)
{ DP *parm = (DP *) args;
int tid = parm->tid;
uint8 *seq = parm->seq;
int len = parm->len;
int out = parm->out;
uint8 *buffer = parm->buffer;
uint8 *bend = parm->bend;
int64 post, last;
uint8 *lust = (uint8 *) (&last);
uint8 *b;
int i, u;
b = buffer;
last = parm->last;
post = parm->post;
for (i = 0; i < len; i++, post++)
{ u = seq[i];
if ((u & 0x7f) == tid)
{ last = post-last;
if (last < 0x3f)
{ if (u & 0x80)
*b++ = 0x80 | last;
else
*b++ = last;
}
else if (last < 0x1fff)
{ if (u & 0x80)
last |= 0xc000;
else
last |= 0x4000;
*b++ = lust[1];
*b++ = lust[0];
}
else
{ while (last >= 0x10000000)
{ if (u & 0x80)
*b++ = 0xf0;
else
*b++ = 0x70;
if (b >= bend)
{ if (write(out,buffer,b-buffer) < 0)
{ fprintf(stderr,"%s: IO write to file %s%d.idx failed\n",
Prog_Name,POST_NAME,parm->inum);
exit (1);
}
b = buffer;
}
last -= 0x10000000;
}
if (u & 0x80)
last |= 0xe0000000;
else
last |= 0x60000000;
*b++ = lust[3];
*b++ = lust[2];
*b++ = lust[1];
*b++ = lust[0];
}
if (b >= bend)
{ if (write(out,buffer,b-buffer) < 0)
{ fprintf(stderr,"%s: IO write to file %s%d.idx failed\n",
Prog_Name,POST_NAME,parm->inum);
exit (1);
}
b = buffer;
}
last = post;
}
}
if (b > buffer)
if (write(out,buffer,b-buffer) < 0)
{ fprintf(stderr,"%s: IO write to file %s%d.idx failed\n",
Prog_Name,POST_NAME,parm->inum);
exit (1);
}
parm->last = last;
parm->post = post;
return (NULL);
}
// Distribute posts to the appropriate NTHREADS^2 files based on section of the DB and 1st byte
// of its canonical k-mer
void distribute(GDB *gdb)
{ uint8 *seq;
int len, ren;
uint8 *neq, *ceq;
int p, r, i, j;
DP parm[NTHREADS];
BP barm[NTHREADS];
#ifndef DEBUG_THREADS
pthread_t threads[NTHREADS];
#endif
seq = (uint8 *) New_Contig_Buffer(gdb); // Allocate work vectorss and set up fixed parts of
neq = (uint8 *) New_Contig_Buffer(gdb); // of the thread records
ceq = (uint8 *) New_Contig_Buffer(gdb);
if (seq == NULL || neq == NULL || ceq == NULL)
exit (1);
parm[0].buffer = Malloc(BUFFER_LEN*NTHREADS,"Allocating IO buffer");
for (i = 0; i < NTHREADS; i++)
{ parm[i].tid = i;
parm[i].seq = seq;
parm[i].buffer = parm[0].buffer + BUFFER_LEN*i;
parm[i].bend = parm[i].buffer + (BUFFER_LEN-4);
parm[i].post = 0;
parm[i].last = 0;
}
for (i = 0; i < NTHREADS; i++)
{ barm[i].seq = seq;
barm[i].neq = neq;
barm[i].ceq = ceq;
bzero(barm[i].buck,256*sizeof(int64));
}
// For each segment of the input ...
for (p = 0; p < NTHREADS; p++)
// Open the NTHREAD files to recieve posts from this segment
{ for (i = 0; i < NTHREADS; i++)
{ parm[i].out = Units[i*NTHREADS+p];
parm[i].inum = i*NTHREADS+p;
}
// For each contig in the segment ...
ren = DBsplit[p+1];
for (r = DBsplit[p]; r < ren; r++)
{ if (gdb->contigs[r].boff < 0)
continue;
len = gdb->contigs[r].clen;
Get_Contig(gdb,r,NUMERIC,(char *) seq); // Load the contig
#ifdef DEBUG_MAP
printf("Src:");
for (i = 0; i < len; i++)
printf(" %d",seq[i]);
printf("\n");
#endif
// In segments each thread computes neq and ceq from seq
len -= 3;
barm[0].beg = 0;
for (i = 1; i < NTHREADS; i++)
barm[i-1].end = barm[i].beg = (((int64) i)*len)/NTHREADS;
barm[NTHREADS-1].end = len;
#ifdef DEBUG_THREADS
for (i = 0; i < NTHREADS; i++)
pack_thread(barm+i);
#else
for (i = 1; i < NTHREADS; i++)
pthread_create(threads+i,NULL,pack_thread,barm+i);
pack_thread(barm);
for (i = 1; i < NTHREADS; i++)
pthread_join(threads[i],NULL);
#endif
#ifdef DEBUG_MAP
printf("Neq:");
for (i = 0; i < len; i++)
printf(" %02x",neq[i]);
printf("\n");
printf("Ceq:");
for (i = 0; i < len; i++)
printf(" %02x",ceq[i]);
printf("\n");
#endif
// In segments each thread computes processed seq array from neq,ceq
len -= (KMER-4);
if (len < 0)
{ for (i = 0; i < NTHREADS; i++)
parm[i].post += len + (KMER-1);
continue;
}
barm[0].beg = 0;
for (i = 1; i < NTHREADS; i++)
barm[i-1].end = barm[i].beg = (((int64) i)*len)/NTHREADS;
barm[NTHREADS-1].end = len;
#ifdef DEBUG_THREADS
for (i = 0; i < NTHREADS; i++)
map_thread(barm+i);
#else
for (i = 1; i < NTHREADS; i++)
pthread_create(threads+i,NULL,map_thread,barm+i);
map_thread(barm);
for (i = 1; i < NTHREADS; i++)
pthread_join(threads[i],NULL);
#endif
#ifdef DEBUG_MAP
printf("Out:");
for (i = 0; i < len; i++)
printf(" (%d)%d %02x %02x\n",(seq[i]&0x80)!=0,seq[i]&0x7f,neq[i],ceq[i+KMER-4]);
printf("\n");
#endif
// Each thread scans *all* of seq, writing the k-mers assigned to "its" file
for (i = 0; i < NTHREADS; i++)
parm[i].len = len;
#ifdef DEBUG_THREADS
for (i = 0; i < NTHREADS; i++)
distribute_thread(parm+i);
#else
for (i = 1; i < NTHREADS; i++)
pthread_create(threads+i,NULL,distribute_thread,parm+i);
distribute_thread(parm);
for (i = 1; i < NTHREADS; i++)
pthread_join(threads[i],NULL);
#endif
for (i = 0; i < NTHREADS; i++)
parm[i].post += (KMER-1);
}
// Accumulate bucket counts into Bucket vector for data panel
{ int64 *buck;
buck = Buckets[p];
for (i = 0; i < NTHREADS; i++)
{ for (j = 0; j < 256; j++)
buck[j] += barm[i].buck[j];
parm[i].last = parm[i].post;
}
}
}
free(parm[0].buffer);
free(seq-1);
free(neq-1);
free(ceq-1);
// Complete the bucket array by accumulating counts across k-mer 1st bytes
{ int i, j;
int64 cum;
cum = 0;
for (i = 0; i < 256; i++)
{ for (j = 0; j < NTHREADS; j++)
Buckets[j][i] += cum;
if (i < 255 && Select[i] == Select[i+1])
cum = Buckets[NTHREADS-1][i];
else
cum = 0;
}
}
}
/***********************************************************************************************
*
* SORT PHASE: Read in posts for a given k-mer 1st byte range, recompute their 2-bit compressed
* canonical k-mer, and place them and their k-mer in a sort array in order of 1st byte
* using the Bucket array as a set of "fingers". Then complete the sort with a min-first
* radix sort. Finally output the k-mer table and index for each k-mer range.
*
**********************************************************************************************/
#ifdef DEBUG_SORT
static void print_table(uint8 *array, int swide, int64 nelem)
{ static char DNA[4] = { 'a', 'c', 'g', 't' };
static char *fmer[256], _fmer[1280];
static int first = 1;
int64 x, post, cont, flag, end;
int k;
uint8 *pust = (uint8 *) &post;
uint8 *cust = (uint8 *) &cont;
if (first)
{ char *t;
int i, l3, l2, l1, l0;
first = 0;
i = 0;
t = _fmer;
for (l3 = 0; l3 < 4; l3++)
for (l2 = 0; l2 < 4; l2++)
for (l1 = 0; l1 < 4; l1++)
for (l0 = 0; l0 < 4; l0++)
{ fmer[i] = t;
*t++ = DNA[l3];
*t++ = DNA[l2];
*t++ = DNA[l1];
*t++ = DNA[l0];
*t++ = 0;
i += 1;
}
}
flag = (0x1ll << (8*ContBytes-1));
post = 0;
cont = 0;
end = nelem * swide;
for (x = 0; x < end; x += swide)
{ printf(" %10lld %2d: ",x,array[x]);
for (k = 1; k < KBYTES; k++)
printf("%s",fmer[array[x+k]]);
for (k = 0; k < PostBytes; k++)
pust[k] = array[x+KBYTES+k];
for (k = 0; k < ContBytes; k++)
cust[k] = array[x+KBYTES+PostBytes+k];
if (cont & flag)
printf(" - %11lld (%d)\n",post,cont-flag);
else
printf(" + %11lld (%d)\n",post,cont);
fflush(stdout);
}
}
#endif
typedef struct
{ GDB gdb;
int tid;
int inum;
int in;
int swide;
uint8 *sarr;
uint8 *buff;
} SP;
// Read post file, uncompressing it, recomputing the canonical k-mer at its absolute
// position and loading the k-mer and the signed post into the initial soring array
// for the current 1st byte panel.
static void *setup_thread(void *args)
{ SP *parm = (SP *) args;
int in = parm->in;
int tid = parm->tid;
int swide = parm->swide;
uint8 *sarr = parm->sarr;
uint8 *buffer = parm->buff;
GDB *gdb = &(parm->gdb);
int64 *buck = Buckets[tid];
int iamt;
uint8 *seq;
int len;
uint8 *neq, *ceq, *keq;
int ncntg, inv;
int64 post, bost, last;
int64 cont, nont, flag;
uint8 *lust = (uint8 *) (&last);
uint8 *bust = (uint8 *) (&bost);
uint8 *cust = (uint8 *) (&cont);
uint8 *nust = (uint8 *) (&nont);
int64 nextpost, basepost;
uint8 *bend, *btop, *b;
seq = (uint8 *) New_Contig_Buffer(gdb);
neq = (uint8 *) New_Contig_Buffer(gdb);
ceq = (uint8 *) New_Contig_Buffer(gdb);
if (seq == NULL || neq == NULL || ceq == NULL)
exit (1);
keq = ceq+(KMER-4);
flag = (0x1ll << (8*ContBytes-1));
if (lseek(in,0,SEEK_SET) < 0)
{ fprintf(stderr,"%s: Rewind of file %s%d.idx failed\n",
Prog_Name,POST_NAME,parm->inum);
exit (1);
}
iamt = read(in,buffer,BUFFER_LEN);
if (iamt < 0)
{ fprintf(stderr,"%s: IO read to file %s%d.idx failed\n",
Prog_Name,POST_NAME,parm->inum);
exit (1);
}
bend = buffer + iamt;
if (bend-buffer < BUFFER_LEN)
btop = bend;
else
btop = bend-4;
b = buffer;
if (btop <= buffer) // Nothing to do
{ close(in);
return (NULL);
}
ncntg = DBsplit[tid];
post = DBpost[tid];
nextpost = post;
basepost = post;
while (1)
{ { int v;
int64 extra;
v = *b++; // Get next encoded post, uncompress it and "un-delta"
inv = ((v & 0x80) != 0);
if (v & 0x40)
if (v & 0x20)
{ extra = 0;
while (v & 0x1)
{ extra -= 0x10000000;
v = *b++;
}
last = 0;
lust[3] = (v & 0x0f);
lust[2] = *b++;
lust[1] = *b++;
lust[0] = *b++;
last += extra;
}
else
{ last = 0;
lust[1] = (v & 0x1f);
lust[0] = *b++;
}
else
last = (v & 0x3f);
post += last;
}
if (post >= nextpost) // if position is not in current contig, then fetch the next one
{ int i, k, l;
uint8 *s1, *s2, *s3, *n1;
do
{ len = gdb->contigs[ncntg].clen;
basepost = nextpost;
if (gdb->contigs[ncntg].boff >= 0)
nextpost += len;
ncntg += 1;
}
while (post >= nextpost);
Get_Contig(gdb,ncntg-1,NUMERIC,(char *) seq);
cont = InvP[ncntg-1];
nont = cont | flag;
s1 = seq+1;
s2 = seq+2;
s3 = seq+3;
n1 = neq-1;
l = len-3;
for (i = 0; i < l; i += 4)
{ neq[i] = (seq[i] << 6) | (s1[i] << 4) | (s2[i] << 2) | s3[i];
ceq[i] = Comp[neq[i]];
}
for (k = 1; k < 4; k++)
for (i = k; i < l; i += 4)
{ neq[i] = (n1[i] << 2) | s3[i];
ceq[i] = Comp[neq[i]];
}
}
{ int i; // load the k-mer / post pair into the next available slot in the
uint8 *n, *x; // initial sort array using the bucket index
bost = post-basepost;
if (inv)
{ n = keq+bost;
x = sarr + swide * buck[*n]++;
*x++ = 0;
for (i = 4; i < KMER; i += 4)
*x++ = n[-i];
for (i = 0; i < PostBytes; i++)
*x++ = bust[i];
for (i = 0; i < ContBytes; i++)
*x++ = nust[i];
}
else
{ n = neq+bost;
x = sarr + swide * buck[*n]++;
*x++ = 0;
for (i = 4; i < KMER; i += 4)
*x++ = n[i];
for (i = 0; i < PostBytes; i++)
*x++ = bust[i];
for (i = 0; i < ContBytes; i++)
*x++ = cust[i];
}
}
if (b >= btop) // refill input buffer if needed
{ int ex = bend-b;
memcpy(buffer,b,ex);
bend = buffer+ex;
iamt = read(in,bend,BUFFER_LEN-ex);
if (iamt < 0)
{ fprintf(stderr,"%s: IO read to file %s%d.idx failed\n",
Prog_Name,POST_NAME,parm->inum);
exit (1);
}
bend += iamt;
if (bend == buffer)
break;
if (bend-buffer < BUFFER_LEN)
btop = bend;
else
btop = bend-4;
b = buffer;
}
}
free(seq-1);
free(neq-1);
free(ceq-1);
close(in);
return (NULL);
}
typedef struct
{ int inum;
int swide;
int64 *panel;
uint8 *sarr;
uint8 *buff;
Range *range;
int tout;
int pout;
int64 *prefix;
int64 *posfix;
int64 nelim;
int64 nbase;
int64 nkmer;
int64 npost;
} RP;
// for 1st k-mer byte range [beg,end), find each group of equal k-mers and if < FREQ, then
// write to FastK table under construtions and output (signed) posts of each equal
// k-mer to the associated post list. K-mer 2byte payloads contain the count in the
// first byte and the lcp with its predecessor in the 2nd byte.
static void *output_thread(void *args)
{ RP *parm = (RP *) args;
int swide = parm->swide;
int64 *panel = parm->panel;
uint8 *sarray = parm->sarr;
Range *range = parm->range;
int beg = range->beg;
int end = range->end;
int tout = parm->tout;
int pout = parm->pout;
int64 *prefix = parm->prefix;
int64 *posfix = parm->posfix;
uint8 *buf1 = parm->buff;
uint8 *buf2 = buf1 + 500000;
uint8 *bed1 = (buf1 + 500000) - (KBYTES-1);
uint8 *bed2 = (buf2 + 500000) - (PostBytes+ContBytes);
int64 nelim, nkmer, nbase;
int64 x, e, y;
int o, w, k, z, lcp, idx;
uint8 *_w = (uint8 *) &w;
uint8 *b, *c;
nelim = nkmer = nbase = 0;
o = KMER;
b = buf1;
c = buf2;
x = range->off;
lcp = sarray[x];
prefix += (beg << 16);
posfix += (beg << 8);
for (o = beg; o < end; o++, prefix += 0x10000, posfix += 0x100)
{ e = x + panel[o];
z = (sarray[e] == 0); // Caution: end of panel can have 0 lcp
if (z)
sarray[e] = 1;
for (e = x + panel[o]; x < e; )
{ y = x+swide;
w = 1;
while (sarray[y] == 0)
{ y += swide;
w += 1;
}
// sorted w entries in [x,y) are all equal
if (w >= FREQ)
{ x = y;
if (sarray[x] < lcp)
lcp = sarray[x];
nelim += 1;
nbase += w;
continue;
}
// if < FREQ then output to k-mer table and post list
idx = sarray[x+1];
posfix[idx] += (y-x)/swide;
idx = ((idx << 8) | sarray[x+2]);
prefix[idx] += 1;
for (k = 3; k < KBYTES; k++)
*b++ = sarray[x+k];
*b++ = _w[0];
*b++ = lcp;
if (b >= bed1)
{ if (write(tout,buf1,b-buf1) < 0)
{ fprintf(stderr,"%s: IO write to file %s%d.ktb failed\n",
Prog_Name,POST_NAME,parm->inum);
exit (1);
}
b = buf1;
}
nkmer += 1;
while (x < y)
{ for (k = KBYTES; k < swide; k++)
*c++ = sarray[x+k];
if (c >= bed2)
{ if (write(pout,buf2,c-buf2) < 0)
{ fprintf(stderr,"%s: IO write to file %s%d.pst failed\n",
Prog_Name,POST_NAME,parm->inum);
exit (1);
}
c = buf2;
}
x += swide;
}
lcp = sarray[x];
}
if (z)
lcp = sarray[e] = 0;
}
if (b > buf1)
if (write(tout,buf1,b-buf1) < 0)
{ fprintf(stderr,"%s: IO write to file %s%d.ktb failed\n",
Prog_Name,POST_NAME,parm->inum);
exit (1);
}
if (c > buf2)
if (write(pout,buf2,c-buf2) < 0)
{ fprintf(stderr,"%s: IO write to file %s%d.pst failed\n",
Prog_Name,POST_NAME,parm->inum);
exit (1);
}
parm->npost = (x-range->off)/swide - nbase;
parm->nelim = nelim;
parm->nbase = nbase;
parm->nkmer = nkmer;
return (NULL);
}
typedef struct
{ int tid;
uint8 *bufr;
int64 nkmer;
int64 npost;
int fail;
int tout, pout;
} CP;
static void *catenate_thread(void *args)
{ CP *parm = (CP *) args;
uint8 *bufr = parm->bufr;
int tid = parm->tid;
int tout = parm->tout;
int pout = parm->pout;
int tin, pin;
int p, x;
parm->fail = 1;
if (write(tout,&KMER,sizeof(int)) < 0) goto tout_write_fail;
if (write(tout,&parm->nkmer,sizeof(int64)) < 0) goto tout_write_fail;
for (p = tid*NTHREADS; p < (tid+1)*NTHREADS; p++)
{ tin = Units[p];
if (lseek(tin,0,SEEK_SET) < 0)
{ fprintf(stderr,"%s: Rewind of file %s%d.ktb failed\n",Prog_Name,POST_NAME,p);
return (NULL);
}
while (1)
{ x = read(tin,bufr,BUFFER_LEN);
if (x < 0)
{ fprintf(stderr,"%s: IO read from file %s%d.ktb failed\n",Prog_Name,POST_NAME,p);
return (NULL);
}
if (x == 0)
break;
if (write(tout,bufr,x) < 0) goto tout_write_fail;
}
close(tin);
}
close(tout);
if (write(pout,&PostBytes,sizeof(int)) < 0) goto pout_write_fail;
if (write(pout,&ContBytes,sizeof(int)) < 0) goto pout_write_fail;
if (write(pout,&parm->npost,sizeof(int64)) < 0) goto pout_write_fail;
for (p = tid*NTHREADS; p < (tid+1)*NTHREADS; p++)
{ pin = Pnits[p];
if (lseek(pin,0,SEEK_SET) < 0)
{ fprintf(stderr,"%s: Rewind of file %s%d.pst failed\n",Prog_Name,POST_NAME,p);
return (NULL);
}
while (1)
{ x = read(pin,bufr,BUFFER_LEN);
if (x < 0)
{ fprintf(stderr,"%s: IO read from file %s%d.pst failed\n",Prog_Name,POST_NAME,p);
return (NULL);
}
if (x == 0)
break;
if (write(pout,bufr,x) < 0) goto pout_write_fail;
}
close(pin);
}
close(pout);
parm->fail = 0;
return (NULL);
tout_write_fail:
fprintf(stderr,"%s: IO error writing to part file %s/.%s.ktab.%d\n",Prog_Name,TPATH,TROOT,tid+1);
return (NULL);
pout_write_fail:
fprintf(stderr,"%s: IO error writing to part file %s/.%s.post.%d\n",Prog_Name,TPATH,TROOT,tid+1);
return (NULL);
}
void k_sort(GDB *gdb)
{ uint8 *sarray;
uint8 *buffer;
int p, part, swide;
int64 panel[256];
Range range[NTHREADS];
SP sarm[NTHREADS];
RP rarm[NTHREADS];
CP carm[NTHREADS];
int64 nelim, nbase, nkmer;
int64 *prefix;
int64 *posfix;
#ifndef DEBUG_THREADS
pthread_t threads[NTHREADS];
#endif
{ int64 x, nelmax;
nelmax = 0;
for (p = 0; p < NTHREADS; p++)
{ x = Buckets[NTHREADS-1][Ksplit[p+1]-1];
if (nelmax < x)
nelmax = x;
}
swide = KBYTES + PostBytes + ContBytes;
prefix = Malloc(sizeof(int64)*0x1000000,"Allocating prefix array");
posfix = Malloc(sizeof(int64)*0x10000,"Allocating postfix array");
sarray = Malloc(nelmax*swide+1,"Allocating sort array");
buffer = Malloc(NTHREADS*BUFFER_LEN,"Allocating input buffers");
bzero(prefix,sizeof(int64)*0x1000000);
bzero(posfix,sizeof(int64)*0x10000);
}
{ int i, j;
for (i = 255; i >= 0; i--)
{ for (j = NTHREADS-1; j >= 1; j--)
Buckets[j][i] = Buckets[j-1][i];
if (i == 0 || Select[i] != Select[i-1])
Buckets[0][i] = 0;
else
Buckets[0][i] = Buckets[NTHREADS-1][i-1];
}
}
// Must open a separate file descriptor to DB bases for each setup thread !!!
for (p = 0; p < NTHREADS; p++)
{ sarm[p].tid = p;
sarm[p].swide = swide;
sarm[p].sarr = sarray;
sarm[p].buff = buffer + p*BUFFER_LEN;
sarm[p].gdb = *gdb;
if (p > 0)
{ sarm[p].gdb.seqs = fopen(gdb->seqpath,"r");
if (sarm[p].gdb.seqs == NULL)
{ fprintf(stderr,"%s: Cannot open another copy of GDB\n",Prog_Name);
exit (1);
}
}
}
for (p = 0; p < NTHREADS; p++)
{ rarm[p].swide = swide;
rarm[p].sarr = sarray;
rarm[p].buff = buffer + p*BUFFER_LEN;
rarm[p].range = range + p;
rarm[p].panel = panel;
rarm[p].prefix = prefix;
rarm[p].posfix = posfix;
}
for (p = 0; p < NTHREADS; p++)