forked from chhylp123/hifiasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hic.cpp
17132 lines (15085 loc) · 546 KB
/
hic.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
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
#define __STDC_LIMIT_MACROS
#include "float.h"
#include <math.h>
#include "hic.h"
#include "htab.h"
#include "assert.h"
#include "Overlaps.h"
#include "Hash_Table.h"
#include "Correct.h"
#include "Purge_Dups.h"
#include "rcut.h"
#include "khashl.h"
#include "kthread.h"
#include "ksort.h"
#include "kseq.h" // FASTA/Q parser
#include "kdq.h"
#include "horder.h"
KSEQ_INIT(gzFile, gzread)
KDQ_INIT(uint64_t)
#define OFFSET_RATE 0.000000001
#define OFFSET_SECOND_RATE 0.0000000001
#define SCALL 10000
#define OFFSET_RATE_MAX_W 6.90675477865*SCALL
#define OFFSET_RATE_MIN_W 4.0000003e-10*SCALL
#define HIC_COUNTER_BITS 12
#define HIC_MAX_COUNT ((1<<HIC_COUNTER_BITS)-1)
#define HIC_KEY_MODE ((uint64_t)(((uint64_t)-1)-HIC_MAX_COUNT))
#define HIC_R_E_RATE 0.01
const unsigned char b2rc[5] = {'T', 'G', 'C', 'A', 'N'};
#define hic_ct_eq(a, b) ((a)>>HIC_COUNTER_BITS == (b)>>HIC_COUNTER_BITS)
#define hic_ct_hash(a) ((a)>>HIC_COUNTER_BITS)
KHASHL_MAP_INIT(static klib_unused, hc_pt_t, hc_pt, uint64_t, uint64_t, hic_ct_hash, hic_ct_eq)
#define u_trans_m_key(a) (((uint64_t)((a).qn)<<32) | ((uint64_t)((a).tn)))
KRADIX_SORT_INIT(u_trans_m, u_trans_t, u_trans_m_key, 8)
#define u_trans_occ_key(a) ((a).occ)
KRADIX_SORT_INIT(u_trans_occ, u_trans_t, u_trans_occ_key, member_size(u_trans_t, occ))
#define is_hom_hit(a) ((a).id == (uint64_t)-1)
#define HC_PT_MA 65
typedef struct {
kv_gg_status sg;
uint64_t xs;
} psg_t;
typedef struct{
kvec_t(char) name;
kvec_t(uint64_t) name_Len;
kvec_t(char) r;
kvec_t(uint64_t) r_Len;
uint64_t idx;
} reads_t;
typedef struct{
kvec_t(hc_edge_warp) rGraph;
kvec_t(uint64_t) order;
pdq pq;
kvec_t(uint8_t) rGraphSet;
kvec_t(uint8_t) rGraphVis;
kvec_t(uint8_t) utgVis;
kvec_t(uint8_t) bmerVis;
kdq_t(uint64_t) *q;
kvec_t(uint32_t) parent;
kvec_t(double) p_weight;
const uint64_t* enzymes;
uint64_t uID_mode, uID_shift, n, src, dest, n_e, c_e;
int p_mer, a_mer, b_mer;
} min_cut_t;
typedef struct{
kvec_t(uint32_t) a;
uint32_t h[2];
uint8_t full_bub;
int status[2];
double weight[2], weight_convex;
}partition_warp;
typedef struct{
size_t n, m;
partition_warp* a;
uint32_t* index;
}G_partition;
typedef struct{
kvec_t(uint8_t) vis;
double weight;
long long bid, uid, chainID;
}block_phase_type;
typedef struct{
uint64_t n;
uint8_t* lock;
uint32_t* hap;
uint32_t m[3];
uint32_t label, label_add, label_shift;
hc_links* link;
G_partition g_p;
G_partition group_g_p;
kvec_t(double) label_buffer;
block_phase_type b;
}H_partition;
typedef struct {
uint32_t p; // the optimal parent vertex
uint32_t d; // the shortest distance from the initial vertex
uint32_t nc; // max count of reads, no matter positive or negative
double nh, w[2];
uint32_t uc, ac; // used vertex/allowed vertex
uint32_t r:31, s:1; // r: the number of remaining incoming arc; s: state
//s: state, s=0, this edge has not been visited, otherwise, s=1
} bub_p_t;
typedef struct {
///all information for each node
bub_p_t *a;
kvec_t(uint32_t) S; // set of vertices without parents, nodes with all incoming edges visited
kvec_t(uint32_t) T; // set of tips
kvec_t(uint32_t) b; // visited vertices
kvec_t(uint32_t) e; // visited edges/arcs
uint32_t exist_hap_label;
} bub_p_t_warp;
typedef struct {
hc_pt_t *h;
uint64_t n;
uint64_t *a;
khint_t end;///end of total idx
} hc_pt1_t;
typedef struct {
ma_ug_t* ug;
asg_t* read_g;
///hc_links* link;
trans_chain* t_ch;
uint64_t uID_bits;
uint64_t uID_mode;
uint64_t pos_bits;
uint64_t pos_mode;
uint64_t rev_mode;
uint64_t k;
uint64_t hap_cnt;
uint64_t pre;
uint64_t tot;
uint64_t tot_pos;
uint64_t up_bound, low_bound;
hc_pt1_t* idx_buf;
long double a, b, frac, max_d;
} ha_ug_index;
typedef struct { // data structure for each step in kt_pipeline()
uint64_t key, pos;
} ch_buf_t;
typedef struct {
kvec_t(uint64_t) a;
} kvec_cnt;
typedef struct {
kvec_t(ch_buf_t) a;
} kvec_pos;
typedef struct { // global data structure for kt_pipeline()
int is_cnt;
uint64_t buf_bytes;
ha_ug_index *h;
kvec_cnt* cnt;
kvec_pos* buf;
uint64_t n_thread;
} pldat_t;
typedef struct {
uint64_t *a, id;
uint16_t occ1, occ2;
} pe_hit_hap;
typedef struct {
pe_hit_hap* a;
size_t n, m;
uint64_t n_u;
} kvec_pe_hit_hap;
typedef struct {
kvec_t(hc_edge) a;
}kvec_hc_edge;
#define pe_hit_an1_key(x) ((x).s)
KRADIX_SORT_INIT(pe_hit_an1, pe_hit, pe_hit_an1_key, member_size(pe_hit, s))
#define pe_hit_an2_key(x) ((x).e)
KRADIX_SORT_INIT(pe_hit_an2, pe_hit, pe_hit_an2_key, member_size(pe_hit, e))
#define pe_hit_an1_idx_key(x) ((x).s<<1)
KRADIX_SORT_INIT(pe_hit_idx_an1, pe_hit, pe_hit_an1_idx_key, member_size(pe_hit, s))
#define pe_hit_an2_idx_key(x) ((x).e<<1)
KRADIX_SORT_INIT(pe_hit_idx_an2, pe_hit, pe_hit_an2_idx_key, member_size(pe_hit, e))
#define generic_key(x) (x)
KRADIX_SORT_INIT(hc64, uint64_t, generic_key, 8)
KRADIX_SORT_INIT(u32, uint32_t, generic_key, 4)
#define g_partition_key(x) (((x)>>1)+((x)<<63))
KRADIX_SORT_INIT(g_partition, uint64_t, g_partition_key, 8)
#define get_pe_s(x) ((x).a[0])
#define get_pe_e(x) ((x).a[(x).occ1])
KRADIX_SORT_INIT(pe_an1, pe_hit_hap, get_pe_s, 8)
KRADIX_SORT_INIT(pe_an2, pe_hit_hap, get_pe_e, 8)
#define pe_occ_key_1(x) ((x).occ1)
KRADIX_SORT_INIT(pe_occ1, pe_hit_hap, pe_occ_key_1, member_size(pe_hit_hap, occ1))
#define pe_occ_key_2(x) ((x).occ2)
KRADIX_SORT_INIT(pe_occ2, pe_hit_hap, pe_occ_key_2, member_size(pe_hit_hap, occ2))
#define pe_occ_key_t(x) (((uint64_t)((x).occ1))+((uint64_t)((x).occ2)))
KRADIX_SORT_INIT(pe_occ_t, pe_hit_hap, pe_occ_key_t, 8)
#define asg_arc_key(a) ((a).ul)
KRADIX_SORT_INIT(asg_e, asg_arc_t, asg_arc_key, 8)
typedef struct { // global data structure for kt_pipeline()
const ha_ug_index* idx;
kseq_t *ks1, *ks2;
int64_t chunk_size;
uint64_t n_thread;
uint64_t total_base;
uint64_t total_pair;
kvec_pe_hit hits;
///kvec_pe_hit_hap hits;
trans_chain* t_ch;
} sldat_t;
typedef struct {
uint64_t ref;
uint64_t off_cnt;
} s_hit;
typedef struct {
kvec_t(s_hit) a;
} kvec_vote;
typedef struct { // data structure for each step in kt_pipeline()
const ha_ug_index* idx;
int n, m, sum_len;
uint64_t *len, id;
char **seq;
ch_buf_t *buf;
kvec_vote* pos_buf;
pe_hit* pos;
///pe_hit_hap* pos;
trans_chain* t_ch;
} stepdat_t;
#define generic_key(x) (x)
KRADIX_SORT_INIT(b64, uint64_t, generic_key, 8)
#define ch_buf_t_key(a) ((a).key)
KRADIX_SORT_INIT(ch_buf, ch_buf_t, ch_buf_t_key, member_size(ch_buf_t, key))
#define hc_pos_key(x) ((x)<<1)
KRADIX_SORT_INIT(hc_pos, uint64_t, hc_pos_key, 8)
#define hc_s_hit_an1_key(a) ((a).ref)
KRADIX_SORT_INIT(hc_s_hit_an1, s_hit, hc_s_hit_an1_key, 8)
#define hc_s_hit_an2_key(a) ((uint32_t)(a).off_cnt)
KRADIX_SORT_INIT(hc_s_hit_an2, s_hit, hc_s_hit_an2_key, 8)
#define hc_s_hit_off_cnt_key(a) ((a).off_cnt)
KRADIX_SORT_INIT(hc_s_hit_off_cnt, s_hit, hc_s_hit_off_cnt_key, 8)
#define hc_edge_key_u(a) ((a).uID)
KRADIX_SORT_INIT(hc_edge_u, hc_edge, hc_edge_key_u, 4)
#define hc_edge_key_d(a) ((a).dis)
KRADIX_SORT_INIT(hc_edge_d, hc_edge, hc_edge_key_d, member_size(hc_edge, dis))
#define k_trans_qs_key(a) ((a).qs)
KRADIX_SORT_INIT(k_trans_qs, u_trans_t, k_trans_qs_key, member_size(u_trans_t, qs))
#define get_hit_suid(x, k) (((x).a.a[(k)].s<<1)>>(64 - (x).uID_bits))
#define get_hit_spos(x, k) ((x).a.a[(k)].s & (x).pos_mode)
#define get_hit_euid(x, k) (((x).a.a[(k)].e<<1)>>(64 - (x).uID_bits))
#define get_hit_epos(x, k) ((x).a.a[(k)].e & (x).pos_mode)
typedef struct {
kvec_t(kvec_t_u64_warp) matrix;
uint64_t uID_shift, dis_mode;
} MT;
typedef struct{
uint64_t beg, end, dis, cnt_0, cnt_1;
} trans_p_t;
typedef struct{
trans_p_t* a;
size_t n, m;
uint64_t max, med;
} trans_idx;
reads_t R1, R2;
ha_ug_index* ug_index;
void print_debug_bubble_graph(bubble_type* bub, ma_ug_t* ug, const char *fn);
void build_bub_graph(ma_ug_t* ug, bubble_type* bub);
void init_ha_ug_index_opt(ha_ug_index* idx, ma_ug_t *ug, int k, pldat_t* p, uint64_t up_occ,
uint64_t low_occ, uint64_t thread_num)
{
uint64_t i, n;
for (idx->uID_bits=1; (uint64_t)(1<<idx->uID_bits)<(uint64_t)ug->u.n; idx->uID_bits++);
idx->pos_bits = 64 - idx->uID_bits - 1;
idx->uID_mode = (((uint64_t)-1) << (64-idx->uID_bits))>>1;
idx->pos_mode = ((uint64_t)-1) >> (64-idx->pos_bits);
idx->rev_mode = ((uint64_t)1) << 63;
idx->ug = ug;
idx->k = k;
idx->pre = HIC_COUNTER_BITS;
idx->tot = 1 << idx->pre;
idx->tot_pos = 0;
///idx->up_bound = 1;
idx->up_bound = up_occ;
idx->low_bound = low_occ;
CALLOC(idx->idx_buf, idx->tot);
for (i = 0; i < idx->tot; i++)
{
idx->idx_buf[i].h = hc_pt_init();
}
for (i = n = 0; i < ug->u.n; i++)
{
n += ug->u.a[i].len;
}
n = n << 3;
p->h = idx;
p->buf_bytes = n>>7;
CALLOC(p->cnt, idx->tot);
CALLOC(p->buf, idx->tot);
for (i = 0; i < idx->tot; i++)
{
kv_init(p->cnt[i].a);
kv_init(p->buf[i].a);
}
p->n_thread = thread_num;
}
inline uint64_t get_k_direction(uint64_t x[4])
{
if(x[1] != x[3])
{
return x[1] < x[3]? 0 : 1;
}
else if(x[0] != x[2])
{
return x[0] < x[2]? 0 : 1;
}
else
{
return (uint64_t)-1;
}
}
inline uint64_t hc_hash_long(uint64_t x[4], uint64_t* skip, uint64_t k)
{
///compare forward k-mer and reverse complementary strand
(*skip) = get_k_direction(x);
if((*skip) == (uint64_t)-1) return (*skip);
if (k <= 32) return ((x[(*skip)<<1|0]<<32)|(x[(*skip)<<1|1]));
return yak_hash64_64(x[(*skip)<<1|0]) + yak_hash64_64(x[(*skip)<<1|1]);
}
inline uint64_t get_hc_pt1_count(ha_ug_index* index, uint64_t key, uint64_t** pos_list)
{
uint64_t bucket_mask = (1ULL<<index->pre) - 1;
hc_pt1_t* h = &(index->idx_buf[key & bucket_mask]);
uint64_t beg;
khint_t k;
k = hc_pt_get(h->h, key);
if (k == kh_end(h->h))
{
return 0;
}
beg = kh_val(h->h, k);
if(pos_list) *pos_list = h->a + beg;
if((kh_key(h->h, k)&HIC_MAX_COUNT)<HIC_MAX_COUNT) return kh_key(h->h, k)&HIC_MAX_COUNT;
if(k == h->end) return h->n - beg;
for (k++; k != kh_end(h->h); ++k)
{
if (kh_exist(h->h, k))
{
return kh_val(h->h, k) - beg;
}
}
return h->n - beg;
}
void test_hc_pt1(char* seq, uint64_t len, uint64_t uID, ha_ug_index* idx)
{
uint64_t i, l, k, pos, *pos_list = NULL, cnt;
uint64_t x[4], mask = (1ULL<<idx->k) - 1, shift = idx->k - 1, hash, skip;
for (i = l = 0, x[0] = x[1] = x[2] = x[3] = 0; i < len; ++i) {
int c = seq_nt4_table[(uint8_t)seq[i]];
///c = 00, 01, 10, 11
if (c < 4) { // not an "N" base
///x[0] & x[1] are the forward k-mer
///x[2] & x[3] are the reverse complementary k-mer
x[0] = (x[0] << 1 | (c&1)) & mask;
x[1] = (x[1] << 1 | (c>>1)) & mask;
x[2] = x[2] >> 1 | (uint64_t)(1 - (c&1)) << shift;
x[3] = x[3] >> 1 | (uint64_t)(1 - (c>>1)) << shift;
if (++l >= idx->k)
{
hash = hc_hash_long(x, &skip, idx->k);
if(skip == (uint64_t)-1) continue;
pos = (skip << 63) | ((uID << (64-idx->uID_bits))>>1) | (i & idx->pos_mode);
cnt = get_hc_pt1_count(idx, hash, &pos_list);
if(cnt == 0) fprintf(stderr, "ERROR cnt, uID: %lu\n", uID);
for (k = 0; k < cnt; k++)
{
if(pos_list[k]==pos)
{
pos_list[k] = (uint64_t)-1;
break;
}
}
if(k == cnt) fprintf(stderr, "ERROR k\n");
}
} else l = 0, x[0] = x[1] = x[2] = x[3] = 0; // if there is an "N", restart
}
}
void test_unitig_index(ha_ug_index* idx, ma_ug_t *ug)
{
double index_time = yak_realtime();
uint32_t i, j;
ma_utg_t *u = NULL;
hc_pt1_t *h = NULL;
idx->ug = ug;
for (i = 0; i < idx->ug->u.n; i++)
{
u = &(idx->ug->u.a[i]);
if(u->m == 0) continue;
test_hc_pt1(u->s, u->len, i, idx);
}
for (i = 0; i < idx->tot; i++)
{
h = &(idx->idx_buf[i]);
for (j = 0; j < h->n; j++)
{
if(h->a[j] != (uint64_t)-1)
{
fprintf(stderr, "ERROR j\n");
}
}
}
fprintf(stderr, "[M::%s::%.3f] ==> Test has been passed\n", __func__, yak_realtime()-index_time);
}
void hc_pt_t_gen_single(hc_pt1_t* pt, uint64_t* up_bound, uint64_t* low_bound)
{
khint_t k;
uint64_t c;
if(up_bound || low_bound)
{
for (k = 0; k != kh_end(pt->h); ++k) {
if (kh_exist(pt->h, k)) {
if((up_bound && kh_val(pt->h, k) > (*up_bound)) ||
(low_bound && kh_val(pt->h, k) < (*low_bound)))
{
kh_val(pt->h, k) = 0;
kh_key(pt->h, k) = (kh_key(pt->h, k)&HIC_KEY_MODE)|
(kh_val(pt->h, k)<HIC_MAX_COUNT?kh_val(pt->h, k):HIC_MAX_COUNT);
}
}
}
}
for (k = 0, pt->n = 0; k != kh_end(pt->h); ++k) {
if (kh_exist(pt->h, k)) {
c = kh_val(pt->h, k);
kh_val(pt->h, k) = pt->n;
pt->n += c;
pt->end = k;
}
}
CALLOC(pt->a, pt->n);
}
void write_dbug(ma_ug_t* ug, FILE* fp)
{
ma_utg_t *u = NULL;
uint32_t t, i;
fwrite(&(ug->u.n), sizeof(ug->u.n), 1, fp);
for (i = 0; i < ug->u.n; i++)
{
u = &(ug->u.a[i]);
t = u->len;
fwrite(&t, sizeof(t), 1, fp);
t = u->circ;
fwrite(&t, sizeof(t), 1, fp);
fwrite(&(u->start), sizeof(u->start), 1, fp);
fwrite(&(u->end), sizeof(u->end), 1, fp);
fwrite(&(u->n), sizeof(u->n), 1, fp);
fwrite(u->a, sizeof(uint64_t), u->n, fp);
}
}
uint32_t test_dbug(ma_ug_t* ug, FILE* fp)
{
uint32_t f_flag = 0, t, i, r_flag = 0;
size_t tt;
ma_utg_t ua, *ub = NULL; memset(&ua, 0, sizeof(ua));
f_flag = fread(&tt, sizeof(tt), 1, fp);
if(f_flag == 0 || tt != ug->u.n) goto DES;
for (i = 0; i < tt; i++)
{
ub = &(ug->u.a[i]);
f_flag = fread(&t, sizeof(t), 1, fp);
if(f_flag == 0 || t != ub->len) goto DES;
f_flag = fread(&t, sizeof(t), 1, fp);
if(f_flag == 0 || t != ub->circ) goto DES;
f_flag = fread(&(ua.start), sizeof(ua.start), 1, fp);
if(f_flag == 0 || ua.start != ub->start) goto DES;
f_flag = fread(&(ua.end), sizeof(ua.end), 1, fp);
if(f_flag == 0 || ua.end != ub->end) goto DES;
f_flag = fread(&(ua.n), sizeof(ua.n), 1, fp);
if(f_flag == 0 || ua.n != ub->n) goto DES;
t = ua.n;
ua.n = 0;
kv_resize(uint64_t, ua, t);
ua.n = t;
f_flag = fread(ua.a, sizeof(uint64_t), ua.n, fp);
if(f_flag == 0 || memcmp(ua.a, ub->a, ua.n)) goto DES;
}
r_flag = 1;
DES:
free(ua.a);
return r_flag;
}
int write_hc_pt_index(ha_ug_index* idx, char* file_name)
{
char* gfa_name = (char*)malloc(strlen(file_name)+25);
sprintf(gfa_name, "%s.hic.tlb.bin", file_name);
FILE* fp = fopen(gfa_name, "w");
if (!fp) {
free(gfa_name);
return 0;
}
uint64_t i = HC_PT_MA;
fwrite(&i, sizeof(i), 1, fp);
fwrite(&idx->uID_bits, sizeof(idx->uID_bits), 1, fp);
fwrite(&idx->uID_mode, sizeof(idx->uID_mode), 1, fp);
fwrite(&idx->pos_bits, sizeof(idx->pos_bits), 1, fp);
fwrite(&idx->pos_mode, sizeof(idx->pos_mode), 1, fp);
fwrite(&idx->rev_mode, sizeof(idx->rev_mode), 1, fp);
fwrite(&idx->k, sizeof(idx->k), 1, fp);
fwrite(&idx->pre, sizeof(idx->pre), 1, fp);
fwrite(&idx->tot, sizeof(idx->tot), 1, fp);
fwrite(&idx->tot_pos, sizeof(idx->tot_pos), 1, fp);
for (i = 0; i < idx->tot; i++)
{
fwrite(&idx->idx_buf[i].n, sizeof(idx->idx_buf[i].n), 1, fp);
fwrite(&idx->idx_buf[i].end, sizeof(idx->idx_buf[i].end), 1, fp);
fwrite(idx->idx_buf[i].a, sizeof(uint64_t), idx->idx_buf[i].n, fp);
hc_pt_save(idx->idx_buf[i].h, fp);
}
write_dbug(idx->ug, fp);
fprintf(stderr, "[M::%s] Index has been written.\n", __func__);
free(gfa_name);
fclose(fp);
return 1;
}
void destory_hc_pt_index(ha_ug_index* idx);
int load_hc_pt_index(ha_ug_index** r_idx, ma_ug_t *ug, char* file_name)
{
uint64_t flag = 0;
// double index_time = yak_realtime();
char* gfa_name = (char*)malloc(strlen(file_name)+25);
sprintf(gfa_name, "%s.hic.tlb.bin", file_name);
FILE* fp = fopen(gfa_name, "r");
if (!fp) {
free(gfa_name);
return 0;
}
ha_ug_index* idx = NULL; CALLOC(idx, 1);
uint64_t i;
flag += fread(&i, sizeof(i), 1, fp);
if(i != HC_PT_MA)
{
free(gfa_name);
destory_hc_pt_index(idx);
free(idx);
(*r_idx) = NULL;
fclose(fp);
fprintf(stderr, "[M::%s::] ==> Renew Hi-C index\n", __func__);
return 0;
}
flag += fread(&idx->uID_bits, sizeof(idx->uID_bits), 1, fp);
flag += fread(&idx->uID_mode, sizeof(idx->uID_mode), 1, fp);
flag += fread(&idx->pos_bits, sizeof(idx->pos_bits), 1, fp);
flag += fread(&idx->pos_mode, sizeof(idx->pos_mode), 1, fp);
flag += fread(&idx->rev_mode, sizeof(idx->rev_mode), 1, fp);
flag += fread(&idx->k, sizeof(idx->k), 1, fp);
flag += fread(&idx->pre, sizeof(idx->pre), 1, fp);
flag += fread(&idx->tot, sizeof(idx->tot), 1, fp);
flag += fread(&idx->tot_pos, sizeof(idx->tot_pos), 1, fp);
MALLOC(idx->idx_buf, idx->tot);
for (i = 0; i < idx->tot; i++)
{
flag += fread(&idx->idx_buf[i].n, sizeof(idx->idx_buf[i].n), 1, fp);
flag += fread(&idx->idx_buf[i].end, sizeof(idx->idx_buf[i].end), 1, fp);
MALLOC(idx->idx_buf[i].a, idx->idx_buf[i].n);
flag += fread(idx->idx_buf[i].a, sizeof(uint64_t), idx->idx_buf[i].n, fp);
hc_pt_load(&(idx->idx_buf[i].h), fp);
}
(*r_idx) = idx;
free(gfa_name);
if(!test_dbug(ug, fp))
{
destory_hc_pt_index(idx);
free(idx);
(*r_idx) = NULL;
fclose(fp);
fprintf(stderr, "[M::%s::] ==> Renew Hi-C index\n", __func__);
return 0;
}
fclose(fp);
// fprintf(stderr, "[M::%s::%.3f] ==> HiC index has been loaded\n", __func__, yak_realtime()-index_time);
return 1;
}
static void worker_for_sort(void *data, long i, int tid) // callback for kt_for()
{
pldat_t *pl = (pldat_t*)data;
hc_pt1_t *h = &(pl->h->idx_buf[i]);
khint_t k;
uint64_t beg, cnt = 0;
uint64_t* pos_list;
for (k = 0; k != kh_end(h->h); ++k) {
if (kh_exist(h->h, k)) {
beg = kh_val(h->h, k);
pos_list = h->a + beg;
if((kh_key(h->h, k)&HIC_MAX_COUNT)<HIC_MAX_COUNT)
{
cnt = kh_key(h->h, k)&HIC_MAX_COUNT;
}
else if(k == h->end)
{
cnt = h->n - beg;
}
else
{
for (k++; k != kh_end(h->h); ++k)
{
if (kh_exist(h->h, k))
{
cnt = kh_val(h->h, k) - beg;
break;
}
}
}
if(cnt > 0) radix_sort_hc_pos(pos_list, pos_list+cnt);
}
}
}
void hc_pt_t_gen(ha_ug_index* idx, pldat_t* pl)
{
if(pl == NULL)
{
uint64_t i;
for (i = 0; i < idx->tot; i++)
{
hc_pt_t_gen_single(&(idx->idx_buf[i]), &(idx->up_bound), &(idx->low_bound));
}
}
else
{
kt_for(pl->n_thread, worker_for_sort, pl, pl->h->tot);
}
}
static void worker_for(void *data, long i, int tid) // callback for kt_for()
{
pldat_t *pl = (pldat_t*)data;
hc_pt1_t *h = &(pl->h->idx_buf[i]);
uint64_t m = 0, beg, end, occ;
khint_t key;
int absent;
if(pl->is_cnt)
{
uint64_t* cnt = NULL;
if(pl->cnt[i].a.n > 2) radix_sort_b64(pl->cnt[i].a.a, pl->cnt[i].a.a + pl->cnt[i].a.n);
cnt = pl->cnt[i].a.a;
occ = pl->cnt[i].a.n;
for (m = beg = end = 0; m < occ; m++)
{
if(cnt[beg] == cnt[m])
{
end = m;
}
else
{
key = hc_pt_put(h->h, cnt[beg], &absent);
if(absent) kh_val(h->h, key) = 0;
kh_val(h->h, key) += (end - beg + 1);
kh_key(h->h, key) = (kh_key(h->h, key)&HIC_KEY_MODE)|
(kh_val(h->h, key)<HIC_MAX_COUNT?kh_val(h->h, key):HIC_MAX_COUNT);
beg = end = m;
}
}
if(occ > 0)
{
key = hc_pt_put(h->h, cnt[beg], &absent);
if(absent) kh_val(h->h, key) = 0;
kh_val(h->h, key) += (end - beg + 1);
kh_key(h->h, key) = (kh_key(h->h, key)&HIC_KEY_MODE)|
(kh_val(h->h, key)<HIC_MAX_COUNT?kh_val(h->h, key):HIC_MAX_COUNT);
}
pl->cnt[i].a.n = 0;
}
if(!pl->is_cnt)
{
ch_buf_t* pos = NULL;
uint64_t num, *pos_list = NULL, k, k_n, pos_k;
if(pl->buf[i].a.n > 2) radix_sort_ch_buf(pl->buf[i].a.a, pl->buf[i].a.a + pl->buf[i].a.n);
pos = pl->buf[i].a.a;
occ = pl->buf[i].a.n;
for (m = beg = end = 0; m < occ; m++)
{
if(pos[beg].key == pos[m].key)
{
end = m;
}
else
{
num = get_hc_pt1_count(pl->h, pos[beg].key, &pos_list);
if(num > 0)
{
k_n=(end-beg+1);pos_k=pos_list[num-1];pos_list[num-1]+=k_n;
for (k = 0; k < k_n; k++)
{
pos_list[pos_k+k] = pos[beg+k].pos;
}
}
beg = end = m;
}
}
if(occ > 0)
{
num = get_hc_pt1_count(pl->h, pos[beg].key, &pos_list);
if(num > 0)
{
k_n=(end-beg+1);pos_k=pos_list[num-1];pos_list[num-1]+=k_n;
for (k = 0; k < k_n; k++)
{
pos_list[pos_k+k] = pos[beg+k].pos;
}
}
}
pl->buf[i].a.n = 0;
}
}
void parallel_count_hc_pt1(pldat_t* pl)
{
uint64_t i, l = 0, uID, num_pos = 0, pos_thre;
uint64_t x[4], mask = (1ULL<<pl->h->k) - 1, shift = pl->h->k - 1, hash, pos, skip, bucket_mask = (1ULL<<pl->h->pre) - 1;
ma_utg_t *u = NULL;
ch_buf_t k_pos;
if(pl->is_cnt) l = ((pl->buf_bytes>>3)/pl->h->tot) + 1, pos_thre = pl->buf_bytes>>3;
if(!pl->is_cnt) l = ((pl->buf_bytes>>4)/pl->h->tot) + 1, pos_thre = pl->buf_bytes>>4;
for (i = 0; i < pl->h->tot; i++)
{
if(pl->is_cnt)
{
kv_resize(uint64_t, pl->cnt[i].a, l);
pl->cnt[i].a.n = 0;
}
if(!pl->is_cnt)
{
kv_resize(ch_buf_t, pl->buf[i].a, l);
pl->buf[i].a.n = 0;
}
}
for (uID = 0; uID < pl->h->ug->u.n; uID++)
{
u = &(pl->h->ug->u.a[uID]);
if(u->m == 0) continue;
for (i = l = 0, x[0] = x[1] = x[2] = x[3] = 0; i < u->len; ++i) {
int c = seq_nt4_table[(uint8_t)u->s[i]];
///c = 00, 01, 10, 11
if (c < 4) { // not an "N" base
///x[0] & x[1] are the forward k-mer
///x[2] & x[3] are the reverse complementary k-mer
x[0] = (x[0] << 1 | (c&1)) & mask;
x[1] = (x[1] << 1 | (c>>1)) & mask;
x[2] = x[2] >> 1 | (uint64_t)(1 - (c&1)) << shift;
x[3] = x[3] >> 1 | (uint64_t)(1 - (c>>1)) << shift;
if (++l >= pl->h->k)
{
hash = hc_hash_long(x, &skip, pl->h->k);
if(skip == (uint64_t)-1) continue;
if(pl->is_cnt)
{
kv_push(uint64_t, pl->cnt[hash & bucket_mask].a, hash);
}
else
{
pos = (skip << 63) | ((uID << (64-pl->h->uID_bits))>>1) | (i & pl->h->pos_mode);
k_pos.key = hash; k_pos.pos = pos;
kv_push(ch_buf_t, pl->buf[hash & bucket_mask].a, k_pos);
}
num_pos++;
if(num_pos >= pos_thre)
{
num_pos = 0;
kt_for(pl->n_thread, worker_for, pl, pl->h->tot);
}
}
} else l = 0, x[0] = x[1] = x[2] = x[3] = 0; // if there is an "N", restart
}
}
if(num_pos > 0) kt_for(pl->n_thread, worker_for, pl, pl->h->tot);
for (i = 0; i < pl->h->tot; i++)
{
if(pl->cnt[i].a.m > 0) kv_destroy(pl->cnt[i].a), kv_init(pl->cnt[i].a);
if(pl->buf[i].a.m > 0) kv_destroy(pl->buf[i].a), kv_init(pl->buf[i].a);
}
}
ha_ug_index* build_unitig_index(ma_ug_t *ug, int k, uint64_t up_occ, uint64_t low_occ, uint64_t thread_num)
{
ha_ug_index* idx = NULL; CALLOC(idx, 1);
pldat_t pl; pl.h = idx; pl.is_cnt = 1;
double index_time = yak_realtime(), beg_time;
init_ha_ug_index_opt(idx, ug, k, &pl, up_occ, low_occ, thread_num);
beg_time = yak_realtime();
pl.is_cnt = 1;
parallel_count_hc_pt1(&pl);
fprintf(stderr, "[M::%s::%.3f] ==> Counting\n", __func__, yak_realtime()-beg_time);
beg_time = yak_realtime();
hc_pt_t_gen(pl.h, NULL);
fprintf(stderr, "[M::%s::%.3f] ==> Memory allocating\n", __func__, yak_realtime()-beg_time);
beg_time = yak_realtime();
pl.is_cnt = 0;
parallel_count_hc_pt1(&pl);
fprintf(stderr, "[M::%s::%.3f] ==> Filling pos\n", __func__, yak_realtime()-beg_time);
beg_time = yak_realtime();
hc_pt_t_gen(pl.h, &pl);
fprintf(stderr, "[M::%s::%.3f] ==> Sorting pos\n", __func__, yak_realtime()-beg_time);
fprintf(stderr, "[M::%s::%.3f] ==> HiC index has been built\n", __func__, yak_realtime()-index_time);
uint64_t i;
for (i = 0; i < idx->tot; i++)
{
kv_destroy(pl.cnt[i].a);
kv_destroy(pl.buf[i].a);
}
free(pl.cnt); free(pl.buf);
return idx;
}
void destory_hc_pt_index(ha_ug_index* idx)
{
if(idx->idx_buf)
{
uint64_t i = 0;
for (i = 0; i < idx->tot; i++)
{
if(idx->idx_buf[i].a) free(idx->idx_buf[i].a);
if(idx->idx_buf[i].h) hc_pt_destroy(idx->idx_buf[i].h);
}
free(idx->idx_buf);
}
}
inline void interpret_pos(const ha_ug_index* idx, s_hit *p, uint64_t* rev, uint64_t* uID,
uint64_t* ref_p, uint64_t* self_p, uint64_t* exact_len, uint64_t* total_len)
{
(*rev) = p->ref>>63;
(*uID) = (p->ref << 1) >> (64 - idx->uID_bits);
(*self_p) = (uint32_t)p->off_cnt;
///(*exact_len) = p->off_cnt >> 32;
(*exact_len) = (p->off_cnt>>32) & ((uint64_t)65535);
if(total_len != NULL)
{
///(*exact_len) = (p->off_cnt>>32) & ((uint64_t)65535);
(*total_len) = (p->off_cnt>>48) + (*exact_len);
}
if((p->ref & idx->pos_mode)>>(idx->pos_bits - 1))
{
(*ref_p) = (*self_p) - (p->ref&(idx->pos_mode>>1));
}
else
{
(*ref_p) = (*self_p) + (p->ref&(idx->pos_mode));
}
}
inline uint64_t check_exact_match(char* a, long long a_beg, long long a_total, char* b, long long b_beg,
long long b_total, long long Len, uint64_t rev, uint64_t dir)
{
long long i = 0;
if(rev == 0)
{
if(dir == 0)
{
for (i = 0; i < Len && a_beg < a_total && b_beg < b_total; i++)
{
if(a[a_beg++] != b[b_beg++]) return i;
}
}
else
{
for (i = 0; i < Len && a_beg >= 0 && b_beg >= 0; i++)
{
if(a[a_beg--] != b[b_beg--]) return i;
}
}
}
else
{
if(dir == 0)
{
for (i = 0; i < Len && a_beg < a_total && b_beg < b_total; i++)
{
if(a[a_beg] != b2rc[seq_nt4_table[(uint8_t)b[b_total - b_beg - 1]]]) return i;
a_beg++; b_beg++;
}
}
else
{
for (i = 0; i < Len && a_beg >= 0 && b_beg >= 0; i++)
{
if(a[a_beg] != b2rc[seq_nt4_table[(uint8_t)b[b_total - b_beg - 1]]]) return i;
a_beg--; b_beg--;
}
}
}
return i;
}
uint64_t debug_hash_value(char *r, uint64_t end, uint64_t k_mer)
{
uint64_t i;
uint64_t x[4], mask = (1ULL<<k_mer) - 1, shift = k_mer - 1, skip;
for (i = end + 1 - k_mer, x[0] = x[1] = x[2] = x[3] = 0; i <= end; i++)
{
int c = seq_nt4_table[(uint8_t)r[i]];
///c = 00, 01, 10, 11
if (c < 4) { // not an "N" base
///x[0] & x[1] are the forward k-mer
///x[2] & x[3] are the reverse complementary k-mer
x[0] = (x[0] << 1 | (c&1)) & mask;
x[1] = (x[1] << 1 | (c>>1)) & mask;