forked from chhylp123/hifiasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Correct.h
1200 lines (974 loc) · 30.5 KB
/
Correct.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
#ifndef __CORRECT__
#define __CORRECT__
#include <stdint.h>
#include "Hash_Table.h"
#include "Levenshtein_distance.h"
#include "POA.h"
#include "Process_Read.h"
#include "Correct.h"
//#define CORRECT_THRESHOLD 0.70
#define CORRECT_THRESHOLD 0.60
///#define CORRECT_THRESHOLD_SECOND 0.55
#define CORRECT_THRESHOLD_HOMOPOLYMER 0.515
#define MIN_COVERAGE_THRESHOLD 3
#define CORRECT_INDEL_LENGTH 2
#define MISMATCH 1
#define INSERTION 2
#define DELETION 3
#define WINDOW_MAX_SIZE (WINDOW + (int)(1.0 / HA_MIN_OV_DIFF) + 3) // TODO: why 1/max_ov_diff?
///#define FLAG_THRE 0
#define MAX(x, y) (((x) >= (y))?(x):(y))
#define MIN(x, y) (((x) <= (y))?(x):(y))
#define DIFF(x, y) ((MAX((x), (y))) - (MIN((x), (y))))
#define OVERLAP(x_start, x_end, y_start, y_end) (MIN(x_end, y_end) - MAX(x_start, y_start) + 1)
///#define OVERLAP(x_start, x_end, y_start, y_end) MIN(x_end, y_end) - MAX(x_start, y_start) + 1
#define Get_MisMatch_Base(RECORD) (s_H[(RECORD>>3)])
#define Get_Match_Base(RECORD) (s_H[(RECORD&7)])
#define Coverage_Threshold(coverage, r_len) (coverage*r_len*1.1)
#define Get_Max_DP_Value(RECORD) (RECORD>>32)
#define Get_Max_DP_ID(RECORD) (RECORD&(uint64_t)0xffffffff)
#define Adjust_Threshold(threshold, x_len) ((threshold == 0 && x_len >= 4)? 1: threshold)
typedef struct
{
long long read_length;
long long window_length;
long long window_num;
long long window_start;
long long window_end;
long long tail_length;
int terminal;
}Window_Pool;
inline void init_Window_Pool(Window_Pool* dumy, long long read_length, long long window_length, long long tail_length)
{
dumy->terminal = 0;
dumy->read_length = read_length;
dumy->window_length = window_length;
dumy->tail_length = tail_length;
dumy->window_start = 0;
dumy->window_end = dumy->window_length - 1;
if (dumy->window_end >= dumy->read_length)
{
dumy->window_end = dumy->read_length - 1;
}
dumy->window_num = (dumy->read_length + dumy->window_length - 1) / dumy->window_length;
}
inline int get_Window(Window_Pool* dumy, long long* w_beg, long long* w_end)
{
(*w_beg) = dumy->window_start;
(*w_end) = dumy->window_end;
if(dumy->window_end == dumy->read_length - 1)
{
if(dumy->terminal == 1 || dumy->read_length == 0)
{
return 0;
}
else if(dumy->terminal == 0)
{
dumy->terminal = 1;
}
}
dumy->window_start = dumy->window_start + dumy->window_length;
dumy->window_end = dumy->window_end + dumy->window_length;
if (dumy->window_end >= dumy->read_length)
{
dumy->window_end = dumy->read_length - 1;
}
return 1;
}
typedef struct
{
/**[0-1] bits are type:**/
/**[2-31] bits are length**/
char current_operation;
int current_operation_length;
uint32_t* record;
uint64_t size;
uint64_t length;
uint32_t new_read_length;
char* lost_base;
uint64_t lost_base_size;
uint64_t lost_base_length;
}Cigar_record;
typedef struct
{
long long length;
long long size;
Cigar_record* buffer;
}Cigar_record_alloc;
typedef struct
{
////the position of snp in read itself
uint32_t site;
////the overlapID
uint32_t overlapID;
////the position of snp in that overlap
uint32_t overlapSite;
///there are several types: 0: equal to read 1: not equal to read, but it is a mismatch 2: is a gap
uint8_t type;
///misbase
char misBase;
}haplotype_evdience;
typedef struct
{
///the id of this snp
uint32_t id;
uint32_t overlap_num;
uint32_t occ_0;
uint32_t occ_1;
uint32_t occ_2;
uint32_t homopolymer_num;
uint32_t non_homopolymer_num;
int score;
////the position of snp in read itself
uint32_t site;
uint8_t is_homopolymer;
}
SnpStats;
typedef struct
{
uint32_t beg;
uint32_t end;
uint32_t occ_0;
uint32_t occ_1;
uint32_t homopolymer_num;
uint32_t non_homopolymer_num;
uint32_t is_remove;
}
Snp_ID_Vector;
typedef struct
{
long long IDs_size;
long long IDs_length;
long long max_snp_id;
Snp_ID_Vector* IDs;
long long buffer_size;
long long buffer_length;
uint32_t* buffer;
}
Snp_ID_Vector_Alloc;
#define Get_DP_Backtrack_Column(matrix, i) (matrix.backtrack + matrix.snp_num * i)
#define Get_DP_Backtrack_Column_Length(matrix, i) (matrix.snp_num)
typedef struct
{
// uint32_t snp_size;
// uint32_t snp_num;
// uint32_t* max;
// uint32_t* colum_len;
// uint32_t* colum;
// uint32_t matrix_size;
uint32_t snp_num;
uint8_t* visit;
uint32_t* max;
uint64_t* max_for_sort;
uint32_t snp_size;
uint32_t* backtrack_length;
uint32_t* backtrack;
uint32_t backtrack_size;
uint32_t* buffer;
uint32_t* max_buffer;
int max_snp_num;
///int max_snp_ID;
int max_score;
int current_snp_num;
Snp_ID_Vector_Alloc SNP_IDs;
}
DP_matrix;
#define Get_SNP_Martix_Size(matrix) (matrix.snp * matrix.overlap)
#define Get_SNP_Vector(matrix, i) (matrix.snp_matrix + matrix.overlap * i)
#define Get_SNP_Vector_Length(matrix) (matrix.overlap)
#define Get_Result_SNP_Vector(matrix) (matrix.snp_matrix + matrix.overlap*matrix.snp)
typedef struct
{
haplotype_evdience* list;
uint32_t sub_list_start;
uint32_t sub_list_length;
uint32_t length;
uint32_t size;
/****************************may have bugs********************************/
uint8_t flag[WINDOW_MAX_SIZE];
/****************************may have bugs********************************/
uint32_t available_snp;
uint32_t core_snp;
uint32_t snp;
uint32_t overlap;
int8_t* snp_matrix;
uint32_t snp_matrix_size;
SnpStats* snp_stat;
SnpStats result_stat;
uint32_t snp_stat_size;
DP_matrix dp;
}
haplotype_evdience_alloc;
inline int filter_snp(int x, int y, int total)
{
double available;
if(x <= y)
{
available = x;
}
else
{
available = y;
}
double threshold = 0.30;
available = available/((double)(total));
if(available <= threshold && available < 6)
{
return 0;
}
return 1;
}
inline int filter_one_snp(int occ_0, int occ_1, int total)
{
double available;
if(occ_0 <= occ_1)
{
available = occ_0;
}
else
{
available = occ_1;
}
double threshold = 0.35;
available = available/((double)(total));
if(available < threshold || occ_0 < MIN_COVERAGE_THRESHOLD + 1 || total < 10)
///if(available < threshold || total < 10)
{
return 0;
}
return 1;
}
inline void count_nearby_snps(haplotype_evdience_alloc* hap, uint32_t* SNPs, int SNPsLen, int* nearsnp, int* non_nearsnps)
{
long long i, current_id, large_id, small_id;
long long distance = 10;
if(SNPsLen == 1)
{
(*non_nearsnps) = 1;
(*nearsnp) = 0;
return;
}
if(SNPsLen == 0)
{
(*nearsnp) = 0;
(*non_nearsnps) = 0;
return;
}
(*nearsnp) = 0;
(*non_nearsnps) = 0;
for (i = 0; i < SNPsLen; i++)
{
if(i > 0 && i < SNPsLen - 1)
{
current_id= SNPs[i];
///since SNPs[i - 1].site is larger than SNPs[i]
large_id = SNPs[i - 1];
small_id = SNPs[i + 1];
if(hap->snp_stat[large_id].site - hap->snp_stat[current_id].site < distance
||
hap->snp_stat[current_id].site - hap->snp_stat[small_id].site < distance)
{
(*nearsnp)++;
}
else
{
(*non_nearsnps)++;
}
if(hap->snp_stat[current_id].site > hap->snp_stat[large_id].site ||
hap->snp_stat[current_id].site < hap->snp_stat[small_id].site)
{
fprintf(stderr, "error\n");
}
}
else if(i == 0)
{
current_id= SNPs[i];
small_id = SNPs[i + 1];
if(hap->snp_stat[current_id].site - hap->snp_stat[small_id].site < distance)
{
(*nearsnp)++;
}
else
{
(*non_nearsnps)++;
}
if(hap->snp_stat[current_id].site < hap->snp_stat[small_id].site)
{
fprintf(stderr, "error\n");
}
}
else
{
large_id = SNPs[i - 1];
current_id= SNPs[i];
if(hap->snp_stat[large_id].site - hap->snp_stat[current_id].site < distance)
{
(*nearsnp)++;
}
else
{
(*non_nearsnps)++;
}
if(hap->snp_stat[current_id].site > hap->snp_stat[large_id].site)
{
fprintf(stderr, "error\n");
}
}
}
if((*nearsnp) + (*non_nearsnps) != SNPsLen)
{
fprintf(stderr, "(*nearsnp): %d, (*non_nearsnps): %d, SNPsLen: %d\n", (*nearsnp), (*non_nearsnps), SNPsLen);
}
}
inline int filter_one_snp_advance_nearby(haplotype_evdience_alloc* hap, int occ_0, int occ_1, int total,
long long homopolymer_num, long long non_homopolymer_num,
uint32_t* SNPs, int SNPsLen)
{
double available;
if(occ_0 <= occ_1)
{
available = occ_0;
}
else
{
available = occ_1;
}
int min = available;
double threshold1 = 0.35;
double threshold2 = 0.24;
available = available/((double)(total));
///if((non_homopolymer_num > 0 && min >= 5) || (min >= 6))
if(min >= 5)
{
if(available < threshold2 || total < 10)
{
return 0;
}
}
else if(available < threshold1 || occ_0 < MIN_COVERAGE_THRESHOLD + 1 || total < 10)
{
return 0;
}
return 1;
}
inline int if_is_homopolymer_strict(long long site, char* read, long long read_length)
{
long long beg, end, i;
long long threshold = 3;
beg = site - threshold;
if(beg < 0)
{
beg = 0;
}
end = site + threshold;
if(end >= read_length)
{
end = read_length - 1;
}
char f_homopolymer_ch = 0;
long long f_homopolymer_len = 0;
for (i = site + 1; i <= end; i++)
{
if(f_homopolymer_ch == 0)
{
f_homopolymer_ch = read[i];
f_homopolymer_len = 1;
}
else
{
if(read[i] != f_homopolymer_ch)
{
break;
}
else
{
f_homopolymer_len++;
}
}
}
char b_homopolymer_ch = 0;
long long b_homopolymer_len = 0;
for (i = site - 1; i >= beg; i--)
{
if(b_homopolymer_ch == 0)
{
b_homopolymer_ch = read[i];
b_homopolymer_len = 1;
}
else
{
if(read[i] != b_homopolymer_ch)
{
break;
}
else
{
b_homopolymer_len++;
}
}
}
if(f_homopolymer_ch == read[site])
{
f_homopolymer_len++;
}
else if(b_homopolymer_ch == read[site])
{
b_homopolymer_len++;
}
if(f_homopolymer_len >= threshold || b_homopolymer_len >= threshold)
{
return 1;
}
if (read[site] == f_homopolymer_ch
&&
b_homopolymer_ch == f_homopolymer_ch
&&
(f_homopolymer_len + b_homopolymer_len >= threshold))
{
return 1;
}
return 0;
}
inline int if_is_homopolymer_repeat(long long site, char* read, long long read_length)
{
long long beg, end, i;
long long threshold = 3;
beg = site - threshold;
if(beg < 0)
{
beg = 0;
}
end = site + threshold;
if(end >= read_length)
{
end = read_length - 1;
}
char f_homopolymer_ch = 0;
long long f_homopolymer_len = 0;
for (i = site + 1; i <= end; i++)
{
if(f_homopolymer_ch == 0)
{
f_homopolymer_ch = read[i];
f_homopolymer_len = 1;
}
else
{
if(read[i] != f_homopolymer_ch)
{
break;
}
else
{
f_homopolymer_len++;
}
}
}
char b_homopolymer_ch = 0;
long long b_homopolymer_len = 0;
for (i = site - 1; i >= beg; i--)
{
if(b_homopolymer_ch == 0)
{
b_homopolymer_ch = read[i];
b_homopolymer_len = 1;
}
else
{
if(read[i] != b_homopolymer_ch)
{
break;
}
else
{
b_homopolymer_len++;
}
}
}
if(f_homopolymer_ch == read[site])
{
f_homopolymer_len++;
}
else if(b_homopolymer_ch == read[site])
{
b_homopolymer_len++;
}
if(f_homopolymer_len >= threshold || b_homopolymer_len >= threshold)
{
return 1;
}
if (read[site] == f_homopolymer_ch
&&
b_homopolymer_ch == f_homopolymer_ch
&&
(f_homopolymer_len + b_homopolymer_len >= threshold))
{
return 1;
}
return 0;
}
inline void InsertSNPVector(haplotype_evdience_alloc* h, haplotype_evdience* sub_list, long long sub_length, char misBase,
UC_Read* g_read)
{
if(sub_length <= 0)
return;
long long i = 0;
h->snp_stat[h->available_snp].id = h->available_snp;
h->snp_stat[h->available_snp].occ_0 = 0;
h->snp_stat[h->available_snp].occ_1 = 0;
h->snp_stat[h->available_snp].occ_2 = 0;
h->snp_stat[h->available_snp].overlap_num = 0;
h->snp_stat[h->available_snp].site = sub_list[0].site;
h->snp_stat[h->available_snp].is_homopolymer =
if_is_homopolymer_strict(h->snp_stat[h->available_snp].site, g_read->seq, g_read->length);
int8_t* vector = Get_SNP_Vector((*h), h->available_snp);
for (i = 0; i < sub_length; i++)
{
if(sub_list[i].type == 0)
{
vector[sub_list[i].overlapID] = 0;
h->snp_stat[h->available_snp].occ_0++;
}
else if(sub_list[i].type == 1 && sub_list[i].misBase == misBase)
{
vector[sub_list[i].overlapID] = 1;
h->snp_stat[h->available_snp].occ_1++;
}
else
{
vector[sub_list[i].overlapID] = 2;
h->snp_stat[h->available_snp].occ_2++;
}
h->snp_stat[h->available_snp].overlap_num++;
}
int new_occ_0 = h->snp_stat[h->available_snp].occ_0 + 1;
int new_occ_1 = h->snp_stat[h->available_snp].occ_1;
if(filter_snp(new_occ_0, new_occ_1, new_occ_0 + new_occ_1) == 0)
{
h->snp_stat[h->available_snp].score = -1;
}
else
{
h->core_snp++;
double consensus = new_occ_0 + new_occ_1 - abs(new_occ_0 - new_occ_1);
consensus = consensus /((double)(new_occ_0 + new_occ_1));
///50% vs 50%
if(new_occ_0 == new_occ_1)
{
consensus = consensus + 0.25;
}
else if(consensus >= 0.8)
{
consensus = consensus + 0.2;
}
else if(consensus >= 0.6)
{
consensus = consensus + 0.15;
}
else if(consensus >= 0.4)
{
consensus = consensus + 0.1;
}
else if(consensus >= 0.2)
{
consensus = consensus + 0.05;
}
consensus= consensus*((double)(new_occ_0 + new_occ_1));
h->snp_stat[h->available_snp].score = consensus;
}
h->available_snp++;
}
inline int calculate_score(int new_occ_0, int new_occ_1)
{
if(new_occ_0 + new_occ_1 == 0)
{
return -1;
}
if(filter_snp(new_occ_0, new_occ_1, new_occ_0 + new_occ_1) == 0)
{
return -1;
}
double consensus = new_occ_0 + new_occ_1 - abs(new_occ_0 - new_occ_1);
consensus = consensus /((double)(new_occ_0 + new_occ_1));
///50% vs 50%
if(new_occ_0 == new_occ_1)
{
consensus = consensus + 0.25;
}
else if(consensus >= 0.8)
{
consensus = consensus + 0.2;
}
else if(consensus >= 0.6)
{
consensus = consensus + 0.15;
}
else if(consensus >= 0.4)
{
consensus = consensus + 0.1;
}
else if(consensus >= 0.2)
{
consensus = consensus + 0.05;
}
consensus= consensus*((double)(new_occ_0 + new_occ_1));
return consensus;
}
inline void SetSnpMatrix(haplotype_evdience_alloc* h, long long snp_num, long long overlap_num)
{
long long new_size = (snp_num + 1)* overlap_num;
if(h->snp_matrix_size < new_size)
{
h->snp_matrix_size = new_size;
h->snp_matrix = (int8_t*)realloc(h->snp_matrix, h->snp_matrix_size);
}
if(h->snp_stat_size < snp_num)
{
h->snp_stat_size = snp_num;
h->snp_stat = (SnpStats*)realloc(h->snp_stat, h->snp_stat_size * sizeof(SnpStats));
}
///h->snp may be different with the number of snp vector
///since some snps have been filtered
h->snp = snp_num;
h->overlap = overlap_num;
h->available_snp = 0;
h->core_snp = 0;
memset(h->snp_matrix, -1, h->snp * h->overlap);
}
inline void init_SNP_IDs(Snp_ID_Vector_Alloc* SNP_IDs)
{
SNP_IDs->max_snp_id = -1;
SNP_IDs->buffer_length = 0;
SNP_IDs->buffer_size = 1000;
SNP_IDs->buffer = (uint32_t*)malloc(sizeof(uint32_t) * SNP_IDs->buffer_size);
SNP_IDs->IDs_length = 0;
SNP_IDs->IDs_size = 10;
SNP_IDs->IDs = (Snp_ID_Vector*)calloc(SNP_IDs->IDs_size, sizeof(Snp_ID_Vector));
}
inline void clear_SNP_IDs(Snp_ID_Vector_Alloc* SNP_IDs)
{
SNP_IDs->IDs_length = 0;
SNP_IDs->buffer_length = 0;
SNP_IDs->max_snp_id = -1;
}
inline void destory_SNP_IDs(Snp_ID_Vector_Alloc* SNP_IDs)
{
free(SNP_IDs->buffer);
free(SNP_IDs->IDs);
}
inline void insert_SNP_IDs_addition(Snp_ID_Vector_Alloc* SNP_IDs, uint32_t* IDs_vec, int IDs_vec_length,
uint32_t occ_0, uint32_t occ_1, uint32_t homopolymer_num, uint32_t non_homopolymer_num)
{
if(SNP_IDs->IDs_length + 1 > SNP_IDs->IDs_size)
{
SNP_IDs->IDs_size = SNP_IDs->IDs_size * 2;
SNP_IDs->IDs = (Snp_ID_Vector*)realloc(SNP_IDs->IDs, SNP_IDs->IDs_size * sizeof(Snp_ID_Vector));
}
SNP_IDs->IDs[SNP_IDs->IDs_length].beg = SNP_IDs->buffer_length;
SNP_IDs->IDs[SNP_IDs->IDs_length].end = SNP_IDs->buffer_length + IDs_vec_length - 1;
SNP_IDs->IDs[SNP_IDs->IDs_length].occ_0 = occ_0;
SNP_IDs->IDs[SNP_IDs->IDs_length].occ_1 = occ_1;
SNP_IDs->IDs[SNP_IDs->IDs_length].homopolymer_num = homopolymer_num;
SNP_IDs->IDs[SNP_IDs->IDs_length].non_homopolymer_num = non_homopolymer_num;
if(SNP_IDs->buffer_length + IDs_vec_length > SNP_IDs->buffer_size)
{
SNP_IDs->buffer_size = (SNP_IDs->buffer_length + IDs_vec_length) * 2;
SNP_IDs->buffer = (uint32_t*)realloc(SNP_IDs->buffer, SNP_IDs->buffer_size * sizeof(uint32_t));
}
memcpy(SNP_IDs->buffer + SNP_IDs->buffer_length, IDs_vec, IDs_vec_length*sizeof(uint32_t));
SNP_IDs->IDs_length++;
SNP_IDs->buffer_length += IDs_vec_length;
}
inline void insert_SNP_IDs_addition(Snp_ID_Vector_Alloc* SNP_IDs, uint32_t* IDs_vec, int IDs_vec_length)
{
if(SNP_IDs->IDs_length + 1 > SNP_IDs->IDs_size)
{
SNP_IDs->IDs_size = SNP_IDs->IDs_size * 2;
SNP_IDs->IDs = (Snp_ID_Vector*)realloc(SNP_IDs->IDs, SNP_IDs->IDs_size * sizeof(Snp_ID_Vector));
}
SNP_IDs->IDs[SNP_IDs->IDs_length].beg = SNP_IDs->buffer_length;
SNP_IDs->IDs[SNP_IDs->IDs_length].end = SNP_IDs->buffer_length + IDs_vec_length - 1;
if(SNP_IDs->buffer_length + IDs_vec_length > SNP_IDs->buffer_size)
{
SNP_IDs->buffer_size = (SNP_IDs->buffer_length + IDs_vec_length) * 2;
SNP_IDs->buffer = (uint32_t*)realloc(SNP_IDs->buffer, SNP_IDs->buffer_size * sizeof(uint32_t));
}
memcpy(SNP_IDs->buffer + SNP_IDs->buffer_length, IDs_vec, IDs_vec_length*sizeof(uint32_t));
SNP_IDs->IDs_length++;
SNP_IDs->buffer_length += IDs_vec_length;
}
inline void init_DP_matrix(DP_matrix* dp, uint32_t snp_num)
{
if(snp_num > dp->snp_size)
{
dp->snp_size = snp_num;
dp->max = (uint32_t*)realloc(dp->max, dp->snp_size * sizeof(uint32_t));
dp->max_for_sort = (uint64_t*)realloc(dp->max_for_sort, dp->snp_size * sizeof(uint64_t));
dp->visit = (uint8_t*)realloc(dp->visit, dp->snp_size * sizeof(uint8_t));
dp->buffer = (uint32_t*)realloc(dp->buffer, dp->snp_size * sizeof(uint32_t));
dp->max_buffer = (uint32_t*)realloc(dp->max_buffer, dp->snp_size * sizeof(uint32_t));
dp->backtrack_length = (uint32_t*)realloc(dp->backtrack_length, dp->snp_size * sizeof(uint32_t));
dp->backtrack_size = snp_num*snp_num;
dp->backtrack = (uint32_t*)realloc(dp->backtrack, dp->backtrack_size * sizeof(uint32_t));
}
dp->snp_num = snp_num;
clear_SNP_IDs(&(dp->SNP_IDs));
}
inline void InitHaplotypeEvdience(haplotype_evdience_alloc* h)
{
h->snp = 0;
h->available_snp = 0;
h->overlap = 0;
h->snp_matrix_size = 0;
h->snp_stat_size = 0;
h->snp_matrix = NULL;
h->snp_stat = NULL;
h->sub_list_start = 0;
h->sub_list_length = 0;
h->length = 0;
h->size = 100;
h->list = (haplotype_evdience*)calloc(h->size, sizeof(haplotype_evdience));
/****************************may have bugs********************************/
memset(h->flag, 0, WINDOW_MAX_SIZE * sizeof(uint8_t));
/****************************may have bugs********************************/
// h->dp.max = NULL;
// h->dp.colum = NULL;
// h->dp.colum_len = NULL;
// h->dp.matrix_size = 0;
// h->dp.snp_num = 0;
// h->dp.snp_size = 0;
h->dp.snp_num = 0;
h->dp.max = NULL;
h->dp.max_for_sort = NULL;
h->dp.visit = NULL;
h->dp.snp_size = 0;
h->dp.backtrack = NULL;
h->dp.backtrack_size = 0;
h->dp.backtrack_length = NULL;
h->dp.buffer = NULL;
h->dp.max_buffer = NULL;
init_SNP_IDs(&(h->dp.SNP_IDs));
}
inline void StarSubListHaplotypeEvdience(haplotype_evdience_alloc* h)
{
h->sub_list_start = h->length;
}
inline void EndSubListHaplotypeEvdience(haplotype_evdience_alloc* h)
{
h->sub_list_length = h->length - h->sub_list_start;
}
inline void destoryHaplotypeEvdience(haplotype_evdience_alloc* h)
{
free(h->list);
free(h->snp_stat);
free(h->snp_matrix);
free(h->dp.backtrack);
free(h->dp.max);
free(h->dp.max_for_sort);
free(h->dp.visit);
free(h->dp.backtrack_length);
free(h->dp.buffer);
free(h->dp.max_buffer);
destory_SNP_IDs(&(h->dp.SNP_IDs));
}
inline void ResizeInitHaplotypeEvdience(haplotype_evdience_alloc* h)
{
h->snp = 0;
h->length = 0;
h->sub_list_start = 0;
h->sub_list_length = 0;
/****************************may have bugs********************************/
memset(h->flag, 0, WINDOW_MAX_SIZE * sizeof(uint8_t));
/****************************may have bugs********************************/
}
inline void RsetInitHaplotypeEvdienceFlag(haplotype_evdience_alloc* h, long long beg, long long useful_length)
{
/****************************may have bugs********************************/
memset(h->flag + beg, 0, useful_length * sizeof(uint8_t));
/****************************may have bugs********************************/
}
inline void addHaplotypeEvdience(haplotype_evdience_alloc* h, haplotype_evdience* ev)
{
uint32_t new_length = h->length + 1;
if(new_length > h->size)
{
h->size = h->size * 2;
if(h->size < new_length)
{
h->size = new_length;
}
h->list = (haplotype_evdience*)realloc(h->list, sizeof(haplotype_evdience)*h->size);
}
h->list[h->length] = (*ev);