forked from ruanjue/wtdbg2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kbm.h
2321 lines (2246 loc) · 66 KB
/
kbm.h
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
/*
*
* Copyright (c) 2011, Jue Ruan <[email protected]>
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __KMER_BINMAP_RJ_H
#define __KMER_BINMAP_RJ_H
#include "list.h"
#include "hashset.h"
#include "dna.h"
#include "filereader.h"
#include "bitvec.h"
#include "bitsvec.h"
#include "bit2vec.h"
#include "thread.h"
#include "txtplot.h"
//#define __DEBUG__ 1
#define TEST_MODE
#define KBM_BSIZE 256
#define KBM_BIN_SIZE KBM_BSIZE
#define KBM_MAX_BINCNT 0xFFFFFFFFFFLLU // 40 bits, 1024 G
#define KBM_MAX_RDCNT 0x3FFFFFFF // 30 bits, 1 G
#define KBM_MAX_RDBINCNT 0xFFFFFF // 24 bits
// able to index reference sequences
#define KBM_MAX_RDLEN 0xFFFFFFFFU // 32 bits, 4 G bp
#define KBM_MAX_KSIZE 23
#define KBM_MAX_KCNT 0xFFFF // 16 bits, 65535
#define KBM_N_HASH 4096
#define KBM_KF_BITS 32
#define KBM_KF_SIZE (1LLU << KBM_KF_BITS)
#define KBM_KF_MASK (KBM_KF_SIZE - 1LLU)
#define KBM_LOGF stderr
#define KBM_LOGFNO STDERR_FILENO
static int KBM_LOG = 0;
#define KBM_LOG_LOW 1
#define KBM_LOG_MID 2
#define KBM_LOG_HIG 3
#define KBM_LOG_ALL 4
#define KBM_MAX_RDGRP1 0x7FFFFF
#define KBM_MAX_RDGRP2 0xFF
typedef struct {
u8i rdoff:40, bincnt:24;
u4i rdlen, binoff;
char *tag;
} kbm_read_t;
define_list(kbmreadv, kbm_read_t);
const obj_desc_t kbm_read_t_obj_desc = {"kbm_read_t_obj_desc", sizeof(kbm_read_t), 1, {1}, {offsetof(kbm_read_t, tag)}, {&OBJ_DESC_CHAR_ARRAY}, NULL, NULL};
static inline size_t kbmreadv_deep_obj_desc_cnt(void *list, int idx){ if(idx == 0) return ((kbmreadv*)list)->size; else return 0; }
static const obj_desc_t kbmreadv_deep_obj_desc = {.tag = "kbmreadv_deep_obj_desc", .size = sizeof(kbmreadv), .n_child = 1, .mem_type = {1}, .addr = {offsetof(kbmreadv, buffer)}, .desc = {&kbm_read_t_obj_desc}, .cnt = kbmreadv_deep_obj_desc_cnt, .post = NULL};
#if 0
#define KBM_MAX_BIN_DEGREE 0x7FFU
#endif
#define KBM_MAX_BIN_DEGREE 0x1FFU
// each BIN takes KBM_BIN_SIZE bp in uncompressed reads
typedef struct {
#if 0
u4i ridx:28, off:24, closed:1, degree:11; // off * KBM_BIN_SIZE is the real position
#endif
u4i ridx:30, off:24, closed:1, degree:9; // off * KBM_BIN_SIZE is the real position
} kbm_bin_t;
define_list(kbmbinv, kbm_bin_t);
typedef struct {
u4i bidx;
} kbm_bmer_t;
define_list(kbmbmerv, kbm_bmer_t);
typedef struct {
u1i bidx; // bidx = (kbm_baux_t->bidx << 32 | kbm_bmer_t->bidx)
u1i dir:1, koff:7; // koff is the real (offset? >> 1), here offset is +0 or +1
} kbm_baux_t;
define_list(kbmbauxv, kbm_baux_t);
#define getval_bidx(kbm, offset) ((((u8i)((kbm)->sauxs->buffer[offset].bidx)) << 32) | (kbm)->seeds->buffer[offset].bidx)
//#define kbm_kmer_smear(K) ((K) ^ ((K) >> 4) ^ ((K) >> 7) ^ ((K) >> 12))
#define kbm_kmer_smear(K) ((K) ^ ((K) >> 4) ^ ((K) >> 7))
typedef struct {
u8i mer:46, tot:17, flt:1;
} kbm_kmer_t;
define_list(kbmkmerv, kbm_kmer_t);
#define KBM_KMERCODE(E) ((E).mer)
#define KBM_KMEREQUALS(E1, E2) ((E1).mer == (E2).mer)
#define KBM_KEYEQUALS(K, E) ((K) == (E).mer)
define_hashtable(kbmhash, kbm_kmer_t, KBM_KMERCODE, KBM_KMEREQUALS, u8i, ITSELF, KBM_KEYEQUALS, kbm_kmer_t*, ITSELF);
typedef struct {
u8i off:40, cnt:24;
} kbm_kaux_t;
define_list(kbmkauxv, kbm_kaux_t);
typedef struct {
kbm_kmer_t *mer;
kbm_kaux_t *aux;
u4i kidx;
u4i off:24, dir:1, pdir:1, fine:1, closed:1, extra_bits1:4;
u4i qbidx;
u4i poffs[2];
u8i bidx, boff, bend;
//kbm_bmer_t *b, *end;
} kbm_ref_t;
define_list(kbmrefv, kbm_ref_t);
#if 0
#define heap_cmp_kbm_bmer(refs, a, b) num_cmpx(refs[a].b->bidx, refs[b].b->bidx, refs[a].poffs[refs[a].pdir], refs[b].poffs[refs[b].pdir])
#endif
#define heap_cmp_kbm_bmer(refs, a, b) num_cmpx(refs[a]->bidx, refs[b]->bidx, refs[a].poffs[refs[a].pdir], refs[b].poffs[refs[b].pdir])
typedef struct {
u4i koff;
u4i kcnt:8, kmat:9, boff:15; // offset from the start bin_idx
} kbm_cmer_t;
define_list(kbmcmerv, kbm_cmer_t);
static const kbm_cmer_t KBM_CMER_NULL = {0, 0, 0, 0};
typedef struct {
u8i beg:46, mat:16, bt:2;
b2i var;
u2i gap;
b4i score;
} kbm_cell_t;
static const kbm_cell_t KBM_CELL_NULL = {0, 0, 0, 0, 0, 0};
define_list(kbmcellv, kbm_cell_t);
typedef struct {
u8i beg, end;
u4i mat;
int score;
} kbm_path_t;
define_list(kbmpathv, kbm_path_t);
#define kbmpath_hashcode(E) E.beg
#define kbmpath_hashequals(E1, E2) (E1).beg == (E2).beg
define_hashset(kbmphash, kbm_path_t, kbmpath_hashcode, kbmpath_hashequals);
typedef struct {
u4i qidx:31, qdir:1;
u4i tidx:31, tdir:1;
u8i cgoff:40, cglen:24;
int qb, qe, tb, te;
int mat, cnt, aln, gap; // gap is counted in BINs
} kbm_map_t;
define_list(kbmmapv, kbm_map_t);
typedef struct {
int rd_len_order; // 0
//int hk; // 0
int use_kf; // 0
int min_bin_degree; // 2
u4i ksize, psize; // 0, 21
u4i kmax, kmin, kmer_mod, ksampling; // 1000, 1, 4.0 * KBM_N_HASH, KBM_BSIZE
float ktop; // 0.05
// runtime
u4i strand_mask; // 3. 1: forward; 2: reverse; 3: both
int self_aln; // 0. 0: map to all; 1: only map to greater read_id; 2: itself but reverse complementary
int skip_contained; // 1
u2i max_bgap; // 4
u2i max_bvar; // 4
float max_gap; // 0.6
u8i max_bcnt; // 0xFFFF
int pgap, pvar; // -7, -21
u4i max_hit; // 1000
int min_aln, min_mat; // 2048/256, 200
float aln_var; // 0.25
float min_sim; // kmer similarity: 0.05
int test_mode; // see codes
} KBMPar;
static const obj_desc_t kbmpar_obj_desc = {"kbmpar_obj_desc", sizeof(KBMPar), 0, {}, {}, {}, NULL, NULL};
typedef struct {
u8i flags; // 64 bits, 0: mem_load all, 1: mem_load rdseqs+reads; 2: Single Hash Mode;3-63: unused
KBMPar *par;
BaseBank *rdseqs;
kbmreadv *reads;
cuhash *tag2idx;
kbmbinv *bins;
BitVec *binmarks;
//u8i *kfs;
kbmbmerv *seeds;
kbmbauxv *sauxs;
kbmhash *hashs[KBM_N_HASH];
kbmkauxv *kauxs[KBM_N_HASH];
} KBM;
static inline size_t kbm_obj_desc_cnt(void *kbm, int idx){
UNUSED(kbm);
if(idx == 8 || idx == 9) return KBM_N_HASH;
else return 1;
}
static inline void rebuild_tag2idx_kbm(void *kbm, size_t aux);
static const obj_desc_t kbm_obj_desc = {.tag = "kbm_obj_desc", .size = sizeof(KBM), .n_child = 10,
.mem_type = {1, 1, 1, 1, 1, 1, 1, 1, 2, 2},
.addr = {offsetof(KBM, par), offsetof(KBM, rdseqs), offsetof(KBM, reads), offsetof(KBM, tag2idx), offsetof(KBM, bins), offsetof(KBM, binmarks), offsetof(KBM, seeds), offsetof(KBM, sauxs), offsetof(KBM, hashs), offsetof(KBM, kauxs)},
.desc = {&kbmpar_obj_desc, &basebank_obj_desc, &kbmreadv_deep_obj_desc, &cuhash_obj_desc, &kbmbinv_obj_desc, &bitvec_obj_desc, &kbmbmerv_obj_desc, &kbmbauxv_obj_desc, &kbmhash_obj_desc, &kbmkauxv_obj_desc},
kbm_obj_desc_cnt, rebuild_tag2idx_kbm
};
// Please note that, kbm->tag2idx is not functional after mem_load, because we use cuhash_obj_desc instread of cuhash_deep_obj_desc
typedef struct {
#if 0
u4i poff, bidx;
u4i refidx:26, koff:6;
#endif
u4i poff;
u4i refidx;
u8i bidx, koff; // ICC can't vectorize if there are bit fields
} kbm_dpe_t;
define_list(kbmdpev, kbm_dpe_t);
typedef struct {
kbmdpev *kms; // kmer offset in query and bidx
u4i km_len;
BitVec *cmask; // bit for kbm_cmer_t
kbmcmerv *cms;
u4v *coffs; // kbm_cmer_t offset for each bin
BitVec *rmask[2];
kbmcellv *cells[2];
Bit2Vec *bts; // back trace flag: 0: diagonal, 1: horizontal, 2: vertical
kbmphash *paths; // storing best unique paths by now
u8i boff;
u8i last_bidx;
} KBMDP;
typedef struct {
u8i kmer;
u4i off;
u4i kidx:30, dir:1, closed:1;
} kmer_off_t;
define_list(kmeroffv, kmer_off_t);
typedef struct {
KBM *kbm;
KBMPar *par; // can diff from kbm->par
char *qtag;
BaseBank *qseqs;
u8i qsoff;
u4i qlen, slen, qnbin, qnbit;
u4i qidx;
u8i bmin, bmax;
kmeroffv *koffs[2];
kbmrefv *refs;
u4v *rank, **heaps;
u4i nheap;
u4i hptr;
u2i *binmap;
u4i bmlen, bmoff, bmcnt;
kbmdpev *caches[2];
KBMDP *dps[2];
kbmmapv *hits;
BitsVec *cigars;
BitVec *solids;
String *str;
} KBMAux;
static inline KBMPar* init_kbmpar(){
KBMPar *par;
par = malloc(sizeof(KBMPar));
par->rd_len_order = 0;
par->use_kf = 0;
par->min_bin_degree = 2;
par->ksize = 0;
par->psize = 21;
par->kmax = 1000;
par->kmin = 1;
par->kmer_mod = 4 * KBM_N_HASH;
par->ksampling = KBM_BSIZE;
par->ktop = 0.05;
par->strand_mask = 3;
par->self_aln = 0;
par->skip_contained = 1;
par->max_bgap = 4; // 4 * KBM_BIN_SIZE
par->max_bvar = 4;
par->max_bcnt = 0xFFFF;
par->max_gap = 0.6;
par->pgap = -7;
par->pvar = -21;
par->max_hit = 1000;
par->min_aln = 2048 / KBM_BIN_SIZE;
par->min_mat = 200;
par->min_sim = 0.05;
par->aln_var = 0.25;
par->test_mode = 0;
return par;
}
static inline void free_kbmpar(KBMPar *par){ free(par); }
static inline KBM* init_kbm(KBMPar *par){
KBM *kbm;
u4i i;
kbm = malloc(sizeof(KBM));
kbm->flags = 0;
kbm->par = par;
kbm->rdseqs = init_basebank();
kbm->reads = init_kbmreadv(64);
kbm->tag2idx = init_cuhash(1023);
kbm->bins = init_kbmbinv(64);
kbm->binmarks = init_bitvec(1024);
//kbm->kfs = NULL;
kbm->seeds = init_kbmbmerv(64);
kbm->sauxs = init_kbmbauxv(64);
for(i=0;i<KBM_N_HASH;i++) kbm->hashs[i] = init_kbmhash(1023);
for(i=0;i<KBM_N_HASH;i++) kbm->kauxs[i] = init_kbmkauxv(64);
return kbm;
}
static inline void free_kbm(KBM *kbm){
u4i i;
if(kbm->flags & 0x1) return;
if(kbm->flags & 0x2){
} else {
free_basebank(kbm->rdseqs);
for(i=0;i<kbm->reads->size;i++){
if(kbm->reads->buffer[i].tag) free(kbm->reads->buffer[i].tag);
}
free_kbmreadv(kbm->reads);
}
free_cuhash(kbm->tag2idx);
free_kbmbinv(kbm->bins);
free_bitvec(kbm->binmarks);
//if(kbm->kfs) free(kbm->kfs);
free_kbmbmerv(kbm->seeds);
free_kbmbauxv(kbm->sauxs);
for(i=0;i<KBM_N_HASH;i++) free_kbmhash(kbm->hashs[i]);
for(i=0;i<KBM_N_HASH;i++) free_kbmkauxv(kbm->kauxs[i]);
free(kbm);
}
static inline void reset_index_kbm(KBM *kbm){
u4i i;
if(kbm->flags & 0x04){
free_kbmhash(kbm->hashs[0]);
kbm->hashs[0] = init_kbmhash(1023);
free_kbmkauxv(kbm->kauxs[0]);
kbm->kauxs[0] = init_kbmkauxv(64);
} else {
for(i=0;i<KBM_N_HASH;i++){
free_kbmhash(kbm->hashs[i]);
kbm->hashs[i] = init_kbmhash(1023);
free_kbmkauxv(kbm->kauxs[i]);
kbm->kauxs[i] = init_kbmkauxv(64);
}
}
free_kbmbmerv(kbm->seeds);
kbm->seeds = init_kbmbmerv(64);
free_kbmbauxv(kbm->sauxs);
kbm->sauxs = init_kbmbauxv(64);
//zeros_bitvec(kbm->binmarks);
}
static inline void clear_kbm(KBM *kbm){
u4i i;
if(kbm->flags & 0x1) return;
if(kbm->flags & 0x2){
} else {
clear_basebank(kbm->rdseqs);
for(i=0;i<kbm->reads->size;i++){
if(kbm->reads->buffer[i].tag) free(kbm->reads->buffer[i].tag);
}
clear_kbmreadv(kbm->reads);
clear_cuhash(kbm->tag2idx);
}
clear_kbmbinv(kbm->bins);
clear_bitvec(kbm->binmarks);
for(i=0;i<KBM_N_HASH;i++){
free_kbmhash(kbm->hashs[i]);
kbm->hashs[i] = init_kbmhash(1023);
free_kbmkauxv(kbm->kauxs[i]);
kbm->kauxs[i] = init_kbmkauxv(64);
if(kbm->flags & 0x04) break;
}
clear_kbmbmerv(kbm->seeds);
clear_kbmbauxv(kbm->sauxs);
}
static inline int cvt_kbm_read_length(u4i seqlen){
if(seqlen > KBM_MAX_RDLEN) seqlen = KBM_MAX_RDLEN;
seqlen = (seqlen / KBM_BIN_SIZE) * KBM_BIN_SIZE;
return seqlen;
}
static inline void push_kbm(KBM *kbm, char *tag, int taglen, char *seq, u4i seqlen){
kbm_read_t *rd;
char *ptr;
if(taglen){
ptr = malloc(taglen + 1);
memcpy(ptr, tag, taglen);
ptr[taglen] = 0;
} else {
ptr = NULL;
}
seqlen = cvt_kbm_read_length(seqlen);
rd = next_ref_kbmreadv(kbm->reads);
rd->rdoff = kbm->rdseqs->size;
rd->rdlen = seqlen;
rd->binoff = 0;
rd->bincnt = 0;
rd->tag = ptr;
seq2basebank(kbm->rdseqs, seq, seqlen);
// make sure rdoff is even
if(kbm->rdseqs->size & 0x1){
bit2basebank(kbm->rdseqs, 0);
}
}
static inline void bitpush_kbm(KBM *kbm, char *tag, int taglen, u8i *seqs, u8i seqoff, u4i seqlen){
kbm_read_t *rd;
char *ptr;
if(taglen){
ptr = malloc(taglen + 1);
memcpy(ptr, tag, taglen);
ptr[taglen] = 0;
} else {
ptr = NULL;
}
seqlen = cvt_kbm_read_length(seqlen);
rd = next_ref_kbmreadv(kbm->reads);
rd->rdoff = kbm->rdseqs->size;
rd->rdlen = seqlen;
rd->binoff = 0;
rd->bincnt = 0;
rd->tag = ptr;
fast_fwdbits2basebank(kbm->rdseqs, seqs, seqoff, seqlen);
// make sure rdoff is even
if(kbm->rdseqs->size & 0x1){
bit2basebank(kbm->rdseqs, 0);
}
}
// Please call no more than once
static inline u8i filter_reads_kbm(KBM *kbm, u8i retain_size, int strategy){
u8i m, b, e, len;
if(kbm->reads->size == 0) return 0;
if(retain_size == 0 || retain_size >= kbm->rdseqs->size) return kbm->rdseqs->size;
if((kbm->flags & 0x2) == 0){
if(kbm->par->rd_len_order){
sort_array(kbm->reads->buffer, kbm->reads->size, kbm_read_t, num_cmpgt(b.rdlen, a.rdlen));
if(strategy == 0){ // longest
len = 0;
for(e=0;e<kbm->reads->size;e++){
len += kbm->reads->buffer[e].rdlen;
if(len >= retain_size) break;
}
kbm->reads->size = e;
} else if(strategy == 1){ // median
m = kbm->reads->size / 2;
len = kbm->reads->buffer[m].rdlen;
e = m;
for(b=0;b<=m&&len<retain_size;b++){
len += kbm->reads->buffer[m - b].rdlen;
len += kbm->reads->buffer[m + b].rdlen;
}
e = b * 2;
b = m - b;
if(b){
remove_array_kbmreadv(kbm->reads, 0, b);
}
kbm->reads->size = e;
} else {
return kbm->rdseqs->size;
}
return len;
} else {
return kbm->rdseqs->size;
}
} else {
return kbm->rdseqs->size;
}
}
static inline void ready_kbm(KBM *kbm){
kbm_read_t *rd;
u4i i, j;
if((kbm->flags & 0x2) == 0){
if(kbm->par->rd_len_order){
sort_array(kbm->reads->buffer, kbm->reads->size, kbm_read_t, num_cmpgt(b.rdlen, a.rdlen));
}
encap_basebank(kbm->rdseqs, KBM_BSIZE);
}
clear_kbmbinv(kbm->bins);
for(i=0;i<kbm->reads->size;i++){
rd = ref_kbmreadv(kbm->reads, i);
if(rd->tag) put_cuhash(kbm->tag2idx, (cuhash_t){rd->tag, i});
if((kbm->flags & 0x2) == 0) rd->binoff = kbm->bins->size;
for(j=0;j+KBM_BIN_SIZE<=rd->rdlen;j+=KBM_BIN_SIZE){
push_kbmbinv(kbm->bins, (kbm_bin_t){i, j / KBM_BIN_SIZE, 0, 0});
}
if((kbm->flags & 0x2) == 0) rd->bincnt = j / KBM_BIN_SIZE;
}
clear_bitvec(kbm->binmarks);
encap_bitvec(kbm->binmarks, kbm->bins->size);
kbm->binmarks->n_bit = kbm->bins->size;
zeros_bitvec(kbm->binmarks);
}
// Share seqs, reads
static inline KBM* clone_seqs_kbm(KBM *src, KBMPar *par){
KBM *dst;
dst = init_kbm(par);
free_basebank(dst->rdseqs);
free_kbmreadv(dst->reads);
dst->rdseqs = src->rdseqs;
dst->reads = src->reads;
dst->flags = 1LLU << 1;
ready_kbm(dst); // Notice encap_basebank in ready_kbm
return dst;
}
// rs[0]->n_head MUST >= 1
static inline void split_FIXP_kmers_kbm(BaseBank *rdseqs, u8i offset, u4i length, u1i ksize, u1i psize, u4i kmod, kmeroffv *rs[2]){
kmer_off_t *kp;
u8i kmer, krev, hv, npz, kmask, p, pmask;
u4i ki, npl;
int i, j;
u1i c, b, kshift, pshift, kpshf;
clear_kmeroffv(rs[0]);
clear_kmeroffv(rs[1]);
kshift = (ksize - 1) << 1;
kmask = (1LLU << (ksize << 1)) - 1LLU;
pshift = (psize - 1) << 1;
pmask = (1LLU << (psize << 1)) - 1LLU;
kpshf = psize << 1;
if(ksize){
// scan F-part of kmers
kmer = krev = 0;
for(i=0;i+1<ksize;i++){
c = bits2bit(rdseqs->bits, offset + i);
kmer = (kmer << 2) | c;
krev = (krev >> 2) | (((u8i)((~c) & 0x3)) << kshift);
}
for(;i<(int)length;i++){
c = bits2bit(rdseqs->bits, offset + i);
kmer = ((kmer << 2) | c) & kmask;
krev = (krev >> 2) | (((u8i)((~c) & 0x3)) << kshift);
if(kmer < krev){
push_kmeroffv(rs[0], (kmer_off_t){kmer, i - (ksize - 1), 0, 0, 1});
} else if(krev < kmer){
push_kmeroffv(rs[1], (kmer_off_t){krev, i - (ksize - 1), 0, 1, 1});
}
}
if(psize){
// scan P-part of forward kmers
assert(rs[0]->n_head > 0);
memset(rs[0]->buffer - 1, 0, sizeof(kmer_off_t)); // rs[0]->n_head > 0
rs[0]->buffer[-1].off = length;
npz = 0;
npl = 0;
kp = rs[0]->buffer + rs[0]->size - 1;
b = 4;
for(i=length-1;i>=0;i--){
if(kp->off + (ksize - 1) == (u4i)i){
if(npl >= psize){
p = npz & pmask;
kp->closed = 0;
kp->kmer = (kp->kmer << kpshf) | p;
}
kp --;
}
c = bits2revbit(rdseqs->bits, offset + i);
if(c == b){
} else {
npz = (npz << 2) | c;
npl ++;
b = c;
}
}
// scan P-part of reverse kmers
encap_kmeroffv(rs[1], 1); memset(rs[1]->buffer + rs[1]->size, 0xFF, sizeof(kmer_off_t));
npz = 0;
npl = 0;
kp = rs[1]->buffer;
b = 4;
for(i=0;i<(int)length;i++){
if(kp->off == (u4i)(i)){
if(npl >= psize){
p = npz & pmask;
kp->closed = 0;
kp->kmer = (kp->kmer << kpshf) | p;
kp->off = kp->off - psize;
}
kp ++;
}
c = bits2bit(rdseqs->bits, offset + i);
if(c == b){
} else {
npz = (npz << 2) | c;
npl ++;
b = c;
}
}
} else {
for(i=0;(u4i)i<rs[0]->size;i++) rs[0]->buffer[i].closed = 0;
for(i=0;(u4i)i<rs[1]->size;i++) rs[1]->buffer[i].closed = 0;
}
} else if(psize){
kmer = krev = 0;
b = 4;
for(i=j=0;i<(int)length;i++){
c = bits2bit(rdseqs->bits, offset + i);
if(b == c) continue;
b = c;
kmer = ((kmer << 2) | c) & pmask;
krev = (krev >> 2) | (((u8i)((~c) & 0x3)) << pshift);
j ++;
if(j < psize) continue;
if(kmer < krev){
push_kmeroffv(rs[0], (kmer_off_t){kmer, i - (psize - 1), 0, 0, 0});
} else if(krev < kmer){
push_kmeroffv(rs[1], (kmer_off_t){krev, i - (psize - 1), 0, 1, 0});
}
}
}
for(b=0;b<2;b++){
for(i=0;(u4i)i<rs[b]->size;i++){
if(rs[b]->buffer[i].closed) continue;
hv = kbm_kmer_smear(rs[b]->buffer[i].kmer);
ki = hv % kmod;
if(ki >= KBM_N_HASH) rs[b]->buffer[i].closed = 1;
rs[b]->buffer[i].kidx = ki;
}
}
}
static inline u8i seed2solid_idx_kbm(KBM *kbm, kbm_dpe_t *p){
kbm_bin_t *b;
kbm_read_t *rd;
u8i seqoff;
b = kbm->bins->buffer + p->bidx;
rd = kbm->reads->buffer + b->ridx;
seqoff = ((rd->rdoff + b->off * KBM_BSIZE) >> 1) + p->koff;
return seqoff;
}
static inline u8i rdoff2solid_idx_kbm(KBM *kbm, u4i ridx, u4i roff){
kbm_read_t *rd;
u8i seqoff;
rd = kbm->reads->buffer + ridx;
seqoff = (rd->rdoff + roff) >> 1;
return seqoff;
}
#define binoff2solid_koff_kbm(kbm, bidx, boff) ((boff) >> 1)
typedef struct {
u8i mer:50, kidx:14;
u8i bidx;
u4i cnt:22, koff:8, dir:1, used:1;
} kbm_midx_t;
define_list(kbmmidxv, kbm_midx_t);
typedef struct {
u8i bidx;
kbm_baux_t aux;
} kbm_tmp_bmer_t;
define_list(tmpbmerv, kbm_tmp_bmer_t);
thread_beg_def(midx);
KBM *kbm;
u4i beg, end; // (end - beg) * KBM_BSIZE MUST <= KBM_KMEROFF_MAX
u8i ktot, nrem, Nrem, none, nflt, offset;
u8i srem, Srem;
u8i *cnts, n_cnt;
int task;
int cal_degree;
pthread_mutex_t *locks;
thread_end_def(midx);
thread_beg_func(midx);
KBM *kbm;
kbm_bin_t *bin;
kbmmidxv **kidxs;
kbm_midx_t *mx;
kmer_off_t *f;
kbm_kmer_t *u;
kbm_kaux_t *x;
kmeroffv *kmers[2];
tmpbmerv *bms;
u8i off;
u4i bidx, i, j, k, len, ncpu, tidx, kidx;
int exists;
kbm = midx->kbm;
ncpu = midx->n_cpu;
tidx = midx->t_idx;
kmers[0] = adv_init_kmeroffv(64, 0, 1);
kmers[1] = adv_init_kmeroffv(64, 0, 1);
kidxs = malloc(KBM_N_HASH * sizeof(kbmmidxv*));
for(i=0;i<KBM_N_HASH;i++) kidxs[i] = init_kbmmidxv(64);
bms = init_tmpbmerv(KBM_MAX_KCNT);
thread_beg_loop(midx);
if(midx->task == 1){
// counting kmers
for(i=0;i<KBM_N_HASH;i++) clear_kbmmidxv(kidxs[i]);
for(bidx=midx->beg+tidx;bidx<midx->end;bidx+=ncpu){
if(KBM_LOG == 0 && tidx == 0 && ((bidx - midx->beg) % 100000) == 0){ fprintf(KBM_LOGF, "\r%u", bidx - midx->beg); fflush(KBM_LOGF); }
bin = ref_kbmbinv(kbm->bins, bidx);
if(bin->closed) continue;
if(((u4i)bin->off + 1) * KBM_BIN_SIZE > kbm->reads->buffer[bin->ridx].rdlen) continue;
off = kbm->reads->buffer[bin->ridx].rdoff + bin->off * KBM_BIN_SIZE;
len = KBM_BIN_SIZE;
split_FIXP_kmers_kbm(kbm->rdseqs, off, len, kbm->par->ksize, kbm->par->psize, kbm->par->kmer_mod, kmers);
for(i=0;i<2;i++){
for(j=0;j<kmers[i]->size;j++){
f = ref_kmeroffv(kmers[i], j);
if(f->closed) continue;
mx = next_ref_kbmmidxv(kidxs[f->kidx]);
mx->mer = f->kmer;
mx->kidx = f->kidx;
mx->bidx = bidx;
mx->dir = i;
mx->koff = f->off;
if(kidxs[f->kidx]->size >= 64){
kidx = f->kidx;
// lock hashs[kidx]
pthread_mutex_lock(midx->locks + kidx);
// hash adding
for(k=0;k<kidxs[kidx]->size;k++){
mx = ref_kbmmidxv(kidxs[kidx], k);
u = prepare_kbmhash(kbm->hashs[kidx], mx->mer, &exists);
if(exists){
if(u->tot < KBM_MAX_KCNT) u->tot ++;
} else {
u->mer = mx->mer;
u->tot = 1;
u->flt = 0;
}
}
// free hashs[f->kidx]
pthread_mutex_unlock(midx->locks + kidx);
clear_kbmmidxv(kidxs[kidx]);
}
}
}
}
for(kidx=0;kidx<KBM_N_HASH;kidx++){
if(kidxs[kidx]->size){
// lock hashs[kidx]
pthread_mutex_lock(midx->locks + kidx);
// hash adding
for(k=0;k<kidxs[kidx]->size;k++){
mx = ref_kbmmidxv(kidxs[kidx], k);
u = prepare_kbmhash(kbm->hashs[kidx], mx->mer, &exists);
if(exists){
if(u->tot < KBM_MAX_KCNT) u->tot ++;
} else {
u->mer = mx->mer;
u->tot = 1;
u->flt = 0;
}
}
// free hashs[f->kidx]
pthread_mutex_unlock(midx->locks + kidx);
clear_kbmmidxv(kidxs[kidx]);
}
}
} else if(midx->task == 2){
// delete low freq kmers
for(i=tidx;i<KBM_N_HASH;i+=ncpu){
reset_iter_kbmhash(kbm->hashs[i]);
while((u = ref_iter_kbmhash(kbm->hashs[i]))){
if(u->tot < kbm->par->kmin){
delete_kbmhash(kbm->hashs[i], u);
}
}
}
} else if(midx->task == 3){
// stat kmer counts
memset(midx->cnts, 0, midx->n_cnt * sizeof(u8i));
for(i=tidx;i<KBM_N_HASH;i+=ncpu){
reset_iter_kbmhash(kbm->hashs[i]);
while((u = ref_iter_kbmhash(kbm->hashs[i]))){
midx->cnts[num_min(midx->n_cnt, u->tot) - 1] ++;
}
}
} else if(midx->task == 4){
// stat counts
midx->offset = 0;
for(i=tidx;i<KBM_N_HASH;i+=ncpu){
reset_iter_kbmhash(kbm->hashs[i]);
while((u = ref_iter_kbmhash(kbm->hashs[i]))){
x = ref_kbmkauxv(kbm->kauxs[i], offset_kbmhash(kbm->hashs[i], u));
x->off = midx->offset;
x->cnt = 0;
midx->ktot += u->tot;
if(u->tot < kbm->par->kmin){
u->flt = 1;
} else if(u->tot > kbm->par->kmax){
u->flt = 1;
} else {
midx->offset += u->tot;
}
}
}
} else if(midx->task == 5){
// revise offset
for(i=tidx;i<KBM_N_HASH;i+=ncpu){
for(off=0;off<kbm->kauxs[i]->size;off++) kbm->kauxs[i]->buffer[off].off += midx->offset;
}
} else if(midx->task == 6){
// fill seeds
for(i=0;i<KBM_N_HASH;i++) clear_kbmmidxv(kidxs[i]);
u4v *chgs;
chgs = init_u4v(KBM_BSIZE);
for(bidx=midx->beg+tidx;bidx<midx->end;bidx+=ncpu){
if(KBM_LOG == 0 && tidx == 0 && ((bidx - midx->beg) % 100000) == 0){ fprintf(KBM_LOGF, "\r%u", bidx - midx->beg); fflush(KBM_LOGF); }
bin = ref_kbmbinv(kbm->bins, bidx);
if(bin->closed) continue;
bin->degree = 0;
if(((u4i)bin->off + 1) * KBM_BIN_SIZE > kbm->reads->buffer[bin->ridx].rdlen) continue;
off = kbm->reads->buffer[bin->ridx].rdoff + bin->off * KBM_BIN_SIZE;
len = KBM_BIN_SIZE;
split_FIXP_kmers_kbm(kbm->rdseqs, off, len, kbm->par->ksize, kbm->par->psize, kbm->par->kmer_mod, kmers);
clear_u4v(chgs);
for(i=0;i<2;i++){
for(j=0;j<kmers[i]->size;j++){
f = ref_kmeroffv(kmers[i], j);
if(f->closed) continue;
mx = next_ref_kbmmidxv(kidxs[f->kidx]);
mx->mer = f->kmer;
mx->kidx = f->kidx;
mx->bidx = bidx;
mx->dir = i;
mx->koff = f->off;
if(kidxs[f->kidx]->size == 64){
push_u4v(chgs, f->kidx);
}
}
}
for(i=0;i<chgs->size;i++){
kidx = chgs->buffer[i];
if(kidxs[kidx]->size == 0) continue;
pthread_mutex_lock(midx->locks + kidx);
for(k=0;k<kidxs[kidx]->size;k++){
mx = ref_kbmmidxv(kidxs[kidx], k);
u = get_kbmhash(kbm->hashs[kidx], mx->mer);
if(u && u->flt == 0){
x = ref_kbmkauxv(kbm->kauxs[kidx], offset_kbmhash(kbm->hashs[kidx], u));
kbm->bins->buffer[mx->bidx].degree ++;
if(x->cnt < u->tot){
if(x->cnt && getval_bidx(kbm, x->off + x->cnt - 1) == mx->bidx && kbm->sauxs->buffer[x->off + x->cnt - 1].dir == mx->dir){
// repeated kmer within one bin
} else {
kbm->seeds->buffer[x->off + x->cnt].bidx = mx->bidx & MAX_U4;
kbm->sauxs->buffer[x->off + x->cnt].bidx = mx->bidx >> 32;
kbm->sauxs->buffer[x->off + x->cnt].dir = mx->dir;
kbm->sauxs->buffer[x->off + x->cnt].koff = mx->koff >> 1;
x->cnt ++;
}
}
}
}
pthread_mutex_unlock(midx->locks + kidx);
clear_kbmmidxv(kidxs[kidx]);
}
}
for(kidx=0;kidx<KBM_N_HASH;kidx++){
if(kidxs[kidx]->size){
// lock hashs[kidx]
pthread_mutex_lock(midx->locks + kidx);
// hash adding
for(k=0;k<kidxs[kidx]->size;k++){
mx = ref_kbmmidxv(kidxs[kidx], k);
u = get_kbmhash(kbm->hashs[kidx], mx->mer);
if(u && u->flt == 0){
x = ref_kbmkauxv(kbm->kauxs[kidx], offset_kbmhash(kbm->hashs[kidx], u));
kbm->bins->buffer[mx->bidx].degree ++;
if(x->cnt < u->tot){
if(x->cnt && getval_bidx(kbm, x->off + x->cnt - 1) == mx->bidx && kbm->sauxs->buffer[x->off + x->cnt - 1].dir == mx->dir){
// repeated kmer within one bin
} else {
kbm->seeds->buffer[x->off + x->cnt].bidx = mx->bidx & MAX_U4;
kbm->sauxs->buffer[x->off + x->cnt].bidx = mx->bidx >> 32;
kbm->sauxs->buffer[x->off + x->cnt].dir = mx->dir;
kbm->sauxs->buffer[x->off + x->cnt].koff = mx->koff >> 1;
x->cnt ++;
}
}
}
}
// free hashs[f->kidx]
pthread_mutex_unlock(midx->locks + kidx);
clear_kbmmidxv(kidxs[kidx]);
}
}
free_u4v(chgs);
} else if(midx->task == 7){
// count added kmers
midx->srem = midx->Srem = 0;
for(i=tidx;i<KBM_N_HASH;i+=ncpu){
reset_iter_kbmhash(kbm->hashs[i]);
while((u = ref_iter_kbmhash(kbm->hashs[i]))){
x = ref_kbmkauxv(kbm->kauxs[i], offset_kbmhash(kbm->hashs[i], u));
if(x->cnt){
midx->srem ++;
midx->Srem += x->cnt;
}
}
}
} else if(midx->task == 8){
// sort seeds within a kmer
for(i=tidx;i<KBM_N_HASH;i+=ncpu){
reset_iter_kbmhash(kbm->hashs[i]);
while((u = ref_iter_kbmhash(kbm->hashs[i]))){
x = ref_kbmkauxv(kbm->kauxs[i], offset_kbmhash(kbm->hashs[i], u));
if(x->cnt < 2) continue;
clear_tmpbmerv(bms);
for(j=0;j<x->cnt;j++){
push_tmpbmerv(bms, (kbm_tmp_bmer_t){getval_bidx(kbm, x->off + j), kbm->sauxs->buffer[x->off + j]});
}
sort_array(bms->buffer, bms->size, kbm_tmp_bmer_t, num_cmpgt(a.bidx, b.bidx));
kbm->seeds->buffer[x->off + 0].bidx = bms->buffer[0].bidx & MAX_U4;
kbm->sauxs->buffer[x->off + 0].bidx = bms->buffer[0].bidx >> 32;
kbm->sauxs->buffer[x->off + 0] = bms->buffer[0].aux;
len = 1;
for(j=1;j<x->cnt;j++){
if(bms->buffer[j].bidx < bms->buffer[j - 1].bidx){
continue;
}
kbm->seeds->buffer[x->off + len].bidx = bms->buffer[j].bidx & MAX_U4;
kbm->sauxs->buffer[x->off + len].bidx = bms->buffer[0].bidx >> 32;
kbm->sauxs->buffer[x->off + len] = bms->buffer[j].aux;
len ++;
}
x->cnt = len;
}
}
}
thread_end_loop(midx);
free_kmeroffv(kmers[0]);
free_kmeroffv(kmers[1]);
for(i=0;i<KBM_N_HASH;i++) free_kbmmidxv(kidxs[i]);
free(kidxs);
free_tmpbmerv(bms);
thread_end_func(midx);
static inline void index_kbm(KBM *kbm, u8i beg, u8i end, u4i ncpu, FILE *kmstat){
u8i ktyp, nflt, nrem, Nrem, none, ktot, srem, Srem, off, cnt, *kcnts, MAX;
u8i i, b, e, n;
u4i j, kavg, batch_size;
pthread_mutex_t *hash_locks;
thread_preprocess(midx);
batch_size = 10000;
clear_kbmbmerv(kbm->seeds);
for(i=0;i<KBM_N_HASH;i++) clear_kbmhash(kbm->hashs[i]);
kcnts = NULL;
MAX = KBM_MAX_KCNT;
//if(kbm->kfs){
//free(kbm->kfs);
//kbm->kfs = NULL;
//}
//if(kbm->par->kmin <= 1) kbm->par->use_kf = 0;
//kbm->kfs = kbm->par->use_kf? calloc(KBM_KF_SIZE / 4 / 8, 8) : NULL;
hash_locks = calloc(KBM_N_HASH, sizeof(pthread_mutex_t));
thread_beg_init(midx, ncpu);
midx->kbm = kbm;
midx->beg = beg;
midx->end = end;
midx->cnts = NULL;
midx->n_cnt = MAX;
midx->task = 0;
midx->cal_degree = 0;
midx->locks = hash_locks;
thread_end_init(midx);
fprintf(KBM_LOGF, "[%s] - scanning kmers (K%dP%dS%0.2f) from %llu bins\n", date(), kbm->par->ksize, kbm->par->psize, 1.0 * kbm->par->kmer_mod / KBM_N_HASH, end - beg);
b = e = beg;
thread_apply_all(midx, midx->task = 1);
if(KBM_LOG == 0){ fprintf(KBM_LOGF, "\r%llu bins\n", end - beg); fflush(KBM_LOGF); }
kcnts = calloc(MAX, sizeof(u8i));
thread_beg_iter(midx);
midx->cnts = calloc(MAX, sizeof(u8i));
midx->task = 3; // counting raw kmers
thread_wake(midx);
thread_end_iter(midx);
thread_beg_iter(midx);
thread_wait(midx);