forked from HongzheGuo/deBGA-VARA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_build.c
3639 lines (2936 loc) · 82.3 KB
/
index_build.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
#include <stdlib.h>
#include <unistd.h>
#include <malloc.h>
#include <string.h>
#include <dirent.h>
#include <inttypes.h>
#include "index_build.h"
#include "bit_operation.h"
#include "load_input.h"
#define VCF_ADD
#define VCF_ADD_MAX 50
#define VCF_ADD_MIN 2
#define INS_SEQ
#define ALT_SEQ_MAX 20000
typedef struct vcf_record_sort
{
uint32_t vcf_buff_sort[4];
} vcf_record_s;
uint8_t bit_shift_value = 0;
uint8_t vcf_bit_shift = 0;//cannot be > 7
uint16_t chr_no_n = 0;
uint32_t vcf_seqs_offset = 0;
uint32_t chr_posend_n = 0;
uint32_t chr_file_n_vcf = 0;
char** chr_no_a = NULL;
uint32_t* chr_vcf_n = NULL;
uint32_t* chr_vcf_seq_n = NULL;
const char* vcf_seq = "vcf_seq";
const char* vcf_pos_s = "vcf_pos";
int index_build(int argc, char *argv[])
{
if(load_input_index(argc, argv) == 1) return 1;
printf("kmer size to build index: %u\n", k_t);
fflush(stdout);
//load_reffile_kmer();
#ifndef DEBUG_VCF
load_reffile_kmer_fa();
file_kmer_qsort();
#else
/*
chr_end_n[0] = 2049;
chr_end_n[1] = 48131944;
chr_end_n[2] = 99436510;
chr_end_n[3] = 254707070;
chr_posend_n = 4;
ref_seq_n = 254707069;
strcpy(chr_names[0], "chr21");
strcpy(chr_names[1], "chr22");
strcpy(chr_names[2], "chrX");
*/
//chr_name_n = 25;
chr_posend_n = 26;
chr_file_n_vcf = chr_posend_n;
ref_seq_n = 3095696031;
strcpy(chr_names[0],"chr1");
strcpy(chr_names[1],"chr2");
strcpy(chr_names[2],"chr3");
strcpy(chr_names[3],"chr4");
strcpy(chr_names[4],"chr5");
strcpy(chr_names[5],"chr6");
strcpy(chr_names[6],"chr7");
strcpy(chr_names[7],"chr8");
strcpy(chr_names[8],"chr9");
strcpy(chr_names[9],"chr10");
strcpy(chr_names[10],"chr11");
strcpy(chr_names[11],"chr12");
strcpy(chr_names[12],"chr13");
strcpy(chr_names[13],"chr14");
strcpy(chr_names[14],"chr15");
strcpy(chr_names[15],"chr16");
strcpy(chr_names[16],"chr17");
strcpy(chr_names[17],"chr18");
strcpy(chr_names[18],"chr19");
strcpy(chr_names[19],"chr20");
strcpy(chr_names[20],"chr21");
strcpy(chr_names[21],"chr22");
strcpy(chr_names[22],"chrX");
strcpy(chr_names[23],"chrY");
strcpy(chr_names[24],"chrM");
chr_end_n[0]=2049;
chr_end_n[1]=249252670;
chr_end_n[2]=492452043;
chr_end_n[3]=690474473;
chr_end_n[4]=881628749;
chr_end_n[5]=1062544009;
chr_end_n[6]=1233659076;
chr_end_n[7]=1392797739;
chr_end_n[8]=1539161761;
chr_end_n[9]=1680375192;
chr_end_n[10]=1815909939;
chr_end_n[11]=1950916455;
chr_end_n[12]=2084768350;
chr_end_n[13]=2199938228;
chr_end_n[14]=2307287768;
chr_end_n[15]=2409819160;
chr_end_n[16]=2500173913;
chr_end_n[17]=2581369123;
chr_end_n[18]=2659446371;
chr_end_n[19]=2718575354;
chr_end_n[20]=2781600874;
chr_end_n[21]=2829730769;
chr_end_n[22]=2881035335;
chr_end_n[23]=3036305895;
chr_end_n[24]=3095679461;
chr_end_n[25]=3095696032;
#endif
if(tree_flag) alt_index_build();
return 0;
}
void alt_index_build()
{
load_vcf_chr(vcf_file_name);
chr_vcf_index();
}
void load_vcf_chr(char* vcf_file_name)
{
FILE* fp_in_vcf = NULL;
//FILE* fp_out_test = fopen("./test.vcf", "w");
char file_d[ROUTE_LENGTH_MAX];
char one_line[VCF_LINE_MAX + 1];
char ref_num[VCF_REF_MAX];
char alt_num[VCF_ALT_MAX];
char vcf_ref[VCF_REF_MAX];
char vcf_alt[VCF_ALT_MAX];
char chr_no_tmp[40];
char* pch = NULL;
char* saveptr = NULL;
char* pch_alt = NULL;
char* saveptr_alt = NULL;
uint8_t field_i = 0;
uint8_t chr_name_f = 0;
uint8_t skip_flag = 0;
uint16_t ref_l = 0;//VCF_REF_MAX
uint16_t alt_l = 0;//VCF_ALT_MAX
uint16_t min_l = 0;
uint16_t file_n = 0;//how many chrs at most?
uint16_t chr_no = 0;
uint16_t alt_field_i = 0;
uint32_t vcf_line_n = 0;
uint32_t vcf_pos = 0;
uint32_t ite_i = 0;
uint32_t vcf_n_tmp = 0;
uint32_t vcf_buff[4];
vcf_bit_shift = 2;
file_n = 60;
#ifdef VCF_ADD
uint32_t chr_start = 0;
uint32_t num_tmp = 0;
uint32_t num_tmp_tmp = 0;
uint64_t a_size = 0;
uint64_t* buffer_ref_seq = NULL;
FILE* fp_ref_seq = NULL;
char char_tmp[VCF_ADD_MAX];//uint8_t
ref_length_p = ref_seq_n;
fp_ref_seq = fopen(ref_seq, "rb");
if (fp_ref_seq == NULL)
{
fputs ("File error opening the seq file\n",stderr);
exit (1);
}
fprintf(stderr, "ref_seq_n: %u\n", ref_seq_n);//3095696031
a_size = (ref_seq_n >> 5) + 1;
buffer_ref_seq = (uint64_t* )calloc(a_size, 8);
num_tmp = fread(buffer_ref_seq, 8, a_size, fp_ref_seq);//63676768
fprintf(stderr, "num_tmp %u %u vcf_rpos %s\n", num_tmp, ref_seq_n, vcf_rpos);//96740501 3095696031
if(fp_ref_seq) fclose(fp_ref_seq);
#endif
fp_in_vcf = fopen(vcf_file_name, "r");
if(fp_in_vcf == NULL) fprintf(stderr, "Error of reading input vcf file\n");
FILE** file_p = (FILE** )malloc(file_n * sizeof(FILE* ));
if(file_p == NULL) fprintf(stderr, "Error of allocating memory file_p\n");
for(ite_i = 0; ite_i < file_n; ite_i++)
file_p[ite_i] = NULL;
FILE** file_p_pos = (FILE** )malloc(file_n * sizeof(FILE* ));
if(file_p_pos == NULL) fprintf(stderr, "Error of allocating memory file_p_pos\n");
for(ite_i = 0; ite_i < file_n; ite_i++)
file_p_pos[ite_i] = NULL;
chr_vcf_n = (uint32_t* )calloc(file_n, 4);
if(chr_vcf_n == NULL) fprintf(stderr, "Error of allocating memory chr_vcf_n\n");
chr_vcf_seq_n = (uint32_t* )calloc(file_n, 4);
if(chr_vcf_seq_n == NULL) fprintf(stderr, "Error of allocating memory chr_vcf_seq_n\n");
chr_no_a = (char** )calloc(file_n, sizeof(char* ));
if(chr_no_a == NULL) fprintf(stderr, "Error of allocating memory chr_no_a\n");
for(ite_i = 0; ite_i < file_n; ite_i++)
if((chr_no_a[ite_i] = (char* )calloc(CHR_NAME_MAX, 1)) == NULL)
fprintf(stderr, "Error of allocating memory chr_no_a\n");
fprintf(stderr, "begin reading and loading vcf file\n");
//debug
FILE* fp_debug_vcf = fopen("./debug_vcf_record","w");
while ((!feof(fp_in_vcf)) && (fgets(one_line, VCF_LINE_MAX + 2, fp_in_vcf) != NULL))
{
if(one_line[0] == '#') continue;
field_i = 0;
pch = strtok_r(one_line,"\t", &saveptr);
while (pch != NULL)
{
//fprintf("%s ", pch);
if(field_i == 0)
{
//sscanf(pch, "%[1-9]", chr_no_tmp);
//vcf chr only has the number
strcpy(chr_no_tmp, pch);// + 3
skip_flag = 0;
if(!(strncmp(chr_no_tmp,"GL",2) && strncmp(chr_no_tmp,"MT",2) && strncmp(chr_no_tmp,"NC",2) && strncmp(chr_no_tmp,"hs",2)))
{
skip_flag = 1;
break;
}
chr_name_f = 0;
for (ite_i = 0; ite_i < chr_no_n; ite_i++)
if(strcmp(chr_no_a[ite_i], chr_no_tmp) == 0)
{
chr_name_f = 1;
break;
}
if(chr_name_f)
{
chr_no = ite_i;
}
else
{
chr_no = chr_no_n;
strcpy(chr_no_a[chr_no_n++], chr_no_tmp);
}
}
else if(field_i == 1)
{
vcf_pos = atoi(pch);
}
else if(field_i == 2)
{
}
else if(field_i == 3) //ref
{
strcpy(vcf_ref, pch);
}
else if(field_i == 4) //alt
{
strcpy(vcf_alt, pch);
}
else
{
break;
}
pch = strtok_r(NULL, "\t", &saveptr);
field_i++;
}
if(skip_flag) continue;
if(file_p[chr_no] == NULL)
{
//should change when put into deBGA, change route
memset(file_d, 0, ROUTE_LENGTH_MAX);
strcpy(file_d, index_route);
strcat(file_d, vcf_seq);
strcat(file_d, chr_no_tmp);
file_p[chr_no] = fopen(file_d, "wb");
if (file_p[chr_no] == NULL)
{
fprintf(stderr, "File error of creating vcf seq file %s %s %s\n",file_d, vcf_seq, chr_no_tmp);
exit(1);
}
}
if(file_p_pos[chr_no] == NULL)
{
//should change when put into deBGA
memset(file_d, 0, ROUTE_LENGTH_MAX);
strcpy(file_d, index_route);
strcat(file_d, vcf_pos_s);
strcat(file_d, chr_no_tmp);
file_p_pos[chr_no] = fopen(file_d, "wb");
if (file_p_pos[chr_no] == NULL)
{
fputs ("File error of creating vcf pos file\n",stderr);
exit(1);
}
}
//fprintf("%u %s %s\n", vcf_pos, vcf_ref, vcf_alt);
vcf_seqs_offset = chr_vcf_seq_n[chr_no];
ref_l = strlen(vcf_ref);
pch_alt = strtok_r(vcf_alt,",", &saveptr_alt);
while (pch_alt != NULL)
{
alt_l = strlen(pch_alt);
for (ite_i = 0; ite_i < alt_l; ite_i++)
{
alt_num[ite_i] = nt_table_u[(uint8_t )pch_alt[ite_i]];
if(alt_num[ite_i] == 4) break;
}
if(ite_i < alt_l)
{
pch_alt = strtok_r(NULL, ",", &saveptr_alt);
continue;
}
for (ite_i = 0; ite_i < ref_l; ite_i++) ref_num[ite_i] = nt_table_u[(uint8_t )vcf_ref[ite_i]];
min_l = (ref_l > alt_l) ? alt_l:ref_l;
//fprintf("L: %u %u\n", ref_l, alt_l);
vcf_n_tmp = 0;
if((ref_l == 1) && (alt_l == 1))//SNP
{
vcf_buff[0] = vcf_pos - 1;//vcf 1-offset
vcf_buff[1] = 0;
vcf_buff[1] |= (1 << vcf_bit_shift);
vcf_buff[2] = vcf_seqs_offset;
vcf_buff[3] = vcf_pos;
fwrite(vcf_buff, 4, 4, file_p_pos[chr_no]);
fwrite(&alt_num[0], 1, 1, file_p[chr_no]);
vcf_seqs_offset++;
vcf_n_tmp++;
//fprintf("one\n");
}
else if((ref_l < alt_l) && (memcmp(ref_num, alt_num, min_l) == 0))//I strncmp
{
vcf_buff[0] = vcf_pos - 1 + min_l;
vcf_buff[1] = 1;
vcf_buff[1] |= ((alt_l - ref_l) << vcf_bit_shift);
vcf_buff[2] = vcf_seqs_offset;
vcf_buff[3] = vcf_buff[0];
fwrite(vcf_buff, 4, 4, file_p_pos[chr_no]);
fwrite(alt_num + ref_l, 1, alt_l - ref_l, file_p[chr_no]);
vcf_seqs_offset += (alt_l - ref_l);
vcf_n_tmp++;
fprintf(fp_debug_vcf, "I: %u\n", vcf_pos);
//fprintf("two\n");
#ifdef VCF_ADD
if(alt_l - ref_l > VCF_ADD_MIN)
{
for (ite_i = 0; ite_i < chr_posend_n; ite_i++)
if(strcmp(chr_no_tmp, chr_names[ite_i] + 3) == 0)
break;
chr_start = chr_end_n[ite_i] - 1;
for (ite_i = 0; ite_i < alt_l - ref_l; ite_i++)
{
num_tmp = chr_start + vcf_pos + ite_i;
if(alt_num[ref_l + ite_i] != ((buffer_ref_seq[num_tmp >> 5] >> ((31 - (num_tmp & 0X1f)) << 1)) & 0X3))
break;
}
fprintf(stderr, "ite_i: %u chr_names: %s pos: %u ref_l: %u alt_l: %u %u %u\n", ite_i, chr_no_tmp, vcf_pos, ref_l, alt_l, ((buffer_ref_seq[(chr_start + vcf_pos) >> 5] >> ((31 - ((chr_start + vcf_pos) & 0X1f)) << 1)) & 0X3), ((buffer_ref_seq[(chr_start + vcf_pos + 1) >> 5] >> ((31 - ((chr_start + vcf_pos + 1) & 0X1f)) << 1)) & 0X3));
if(ite_i)
{
fprintf(stderr, "VCF_ADD INS A\n");
vcf_buff[0] = vcf_buff[0] + ite_i;//vcf_pos - 1 + min_l
vcf_buff[1] = 1;
vcf_buff[1] |= ((alt_l - ref_l) << vcf_bit_shift);
vcf_buff[2] = vcf_seqs_offset;
vcf_buff[3] = vcf_buff[0];
fwrite(vcf_buff, 4, 4, file_p_pos[chr_no]);
fwrite(alt_num + ref_l, 1, alt_l - ref_l, file_p[chr_no]);
vcf_seqs_offset += (alt_l - ref_l);
vcf_n_tmp++;
}
for (ite_i = 0; ite_i < alt_l - ref_l; ite_i++)
{
num_tmp = chr_start + vcf_pos - 1 - ite_i;
if(alt_num[alt_l - 1 - ite_i] != ((buffer_ref_seq[num_tmp >> 5] >> ((31 - (num_tmp & 0X1f)) << 1)) & 0X3))
break;
}
if(ite_i)
{
fprintf(stderr, "VCF_ADD INS B\n");
vcf_buff[0] = vcf_pos - 1 + min_l - ite_i;//vcf_pos - 1 + min_l
vcf_buff[1] = 1;
vcf_buff[1] |= ((alt_l - ref_l) << vcf_bit_shift);
vcf_buff[2] = vcf_seqs_offset;
vcf_buff[3] = vcf_buff[0];
fwrite(vcf_buff, 4, 4, file_p_pos[chr_no]);
fwrite(alt_num + ref_l, 1, alt_l - ref_l, file_p[chr_no]);
vcf_seqs_offset += (alt_l - ref_l);
vcf_n_tmp++;
}
}
#endif
}
else if((ref_l > alt_l) && (memcmp(ref_num, alt_num, min_l) == 0))//D strncmp
{
vcf_buff[0] = vcf_pos - 1 + min_l;
vcf_buff[1] = 2;
vcf_buff[1] |= ((ref_l - alt_l) << vcf_bit_shift);
vcf_buff[2] = vcf_seqs_offset;
vcf_buff[3] = vcf_pos - 1 + ref_l;
fwrite(vcf_buff, 4, 4, file_p_pos[chr_no]);
//no write into vcf seq file
vcf_n_tmp++;
//fprintf("three\n");
fprintf(fp_debug_vcf, "D: %u\n", vcf_pos);
#ifdef VCF_ADD
if((ref_l - alt_l < VCF_ADD_MAX) && (ref_l - alt_l > VCF_ADD_MIN))
{
for (ite_i = 0; ite_i < chr_posend_n; ite_i++)
if(strcmp(chr_no_tmp, chr_names[ite_i] + 3) == 0)
break;
chr_start = chr_end_n[ite_i] - 1;
fprintf(stderr, "ite_i: %u chr_names[ite_i]: %s chr_start: %u\n", ite_i, chr_names[ite_i], chr_start);
for (ite_i = 0; ite_i < ref_l - alt_l; ite_i++)
{
num_tmp = chr_start + vcf_buff[3] + ite_i;
char_tmp[ite_i] = ((buffer_ref_seq[num_tmp >> 5] >> ((31 - (num_tmp & 0X1f)) << 1)) & 0X3);
}
fprintf(stderr, "DEL: chr%u %u %u %u\n", chr_no, vcf_pos, ref_l, alt_l);
//taacatttttatgtgttgctt ca tccagtttgctagagtttttggagatt
//caagtcaataaatgtgatacaccaaa taaac agaatttaaaaaaaactca
for(ite_i = 0; ite_i < ref_l - alt_l; ite_i++)
fprintf(stderr, "%c", Dna5Tochar[char_tmp[ite_i]]);
fprintf(stderr, "\n");
for(ite_i = 0; ite_i < ref_l - alt_l; ite_i++)
fprintf(stderr, "%c", Dna5Tochar[*(ref_num + alt_l + ite_i)]);
fprintf(stderr, "\n");
//ref_num[ref_l] = '\0';
//char_tmp[ref_l - alt_l] = '\0';
if(memcmp(ref_num + alt_l, char_tmp, ref_l - alt_l) == 0)
{
fprintf(stderr, "VCF_ADD A\n");
vcf_buff[0] = vcf_buff[3];//vcf_pos - 1 + min_l
vcf_buff[1] = 2;
vcf_buff[1] |= ((ref_l - alt_l) << vcf_bit_shift);
vcf_buff[2] = vcf_seqs_offset;
vcf_buff[3] = vcf_buff[0] + ref_l - alt_l;
fwrite(vcf_buff, 4, 4, file_p_pos[chr_no]);
vcf_n_tmp++;
}
num_tmp_tmp = vcf_pos - 1 + min_l - ref_l + alt_l;
for (ite_i = 0; ite_i < ref_l - alt_l; ite_i++)
{
num_tmp = chr_start + num_tmp_tmp + ite_i;
char_tmp[ite_i] = ((buffer_ref_seq[num_tmp >> 5] >> ((31 - (num_tmp & 0X1f)) << 1)) & 0X3);
}
for(ite_i = 0; ite_i < ref_l - alt_l; ite_i++)
fprintf(stderr, "%c", Dna5Tochar[char_tmp[ite_i]]);
fprintf(stderr, "\n");
for(ite_i = 0; ite_i < ref_l - alt_l; ite_i++)
fprintf(stderr, "%c", Dna5Tochar[*(ref_num + alt_l + ite_i)]);
fprintf(stderr, "\n");
//ref_num[ref_l] = '\0';
//char_tmp[ref_l - alt_l] = '\0';
if(memcmp(ref_num + alt_l, char_tmp, ref_l - alt_l) == 0)
{
fprintf(stderr, "VCF_ADD B\n");
vcf_buff[0] = num_tmp_tmp;//vcf_pos - 1 + min_l
vcf_buff[1] = 2;
vcf_buff[1] |= ((ref_l - alt_l) << vcf_bit_shift);
vcf_buff[2] = vcf_seqs_offset;
vcf_buff[3] = vcf_pos - 1 + min_l;
fwrite(vcf_buff, 4, 4, file_p_pos[chr_no]);
vcf_n_tmp++;
}
fflush(stderr);
}
#endif
}
else //mnp
{
vcf_buff[0] = vcf_pos - 1;
vcf_buff[1] = 3;
vcf_buff[1] |= (alt_l << vcf_bit_shift);
vcf_buff[2] = vcf_seqs_offset;
vcf_buff[3] = vcf_pos - 1 + ref_l;
fwrite(vcf_buff, 4, 4, file_p_pos[chr_no]);
fwrite(alt_num, 1, alt_l, file_p[chr_no]);
vcf_seqs_offset += alt_l;
vcf_n_tmp++;
fprintf(fp_debug_vcf, "MNP: %u\n", vcf_pos);
}
//chr_vcf_n[chr_no]++;
chr_vcf_n[chr_no] += vcf_n_tmp;
pch_alt = strtok_r(NULL, ",", &saveptr_alt);
}
chr_vcf_seq_n[chr_no] = vcf_seqs_offset;
//fprintf(fp_out_test, "%u ", vcf_line_n);
//fputs(one_line, fp_out_test);
//vcf_line_n++;
}
//fprintf("total number of vcf lines: %u\n", vcf_line_n);
//debug
if(fp_debug_vcf) fclose(fp_debug_vcf);
fclose(fp_in_vcf);
//fclose(fp_out_test);
for(ite_i = 0; ite_i < file_n; ite_i++)
{
if(file_p[ite_i] != NULL)
fclose(file_p[ite_i]);
}
free(file_p);
for(ite_i = 0; ite_i < file_n; ite_i++)
{
if(file_p_pos[ite_i] != NULL)
fclose(file_p_pos[ite_i]);
}
free(file_p_pos);
//if(chr_vcf_n) free(chr_vcf_n);
}
void chr_vcf_index()
{
uint8_t v_type = 0;
uint8_t chr_no_f = 0;
uint8_t ref_c = 0;
uint8_t ref_cv_i = 0;
uint8_t* alt_seq_chr = NULL;
#ifdef INS_SEQ
uint8_t alt_seq_ins[ALT_SEQ_MAX];
#endif
char chr_no_tmp[CHR_NAME_MAX];
char file_d[ROUTE_LENGTH_MAX];
uint16_t chr_i = 0;
uint16_t chr_no_i = 0;
uint16_t chr_no = 0;
uint32_t chr_vcf_total = 0;
uint32_t ref_cv_n = 0;
uint32_t rpos_cnt = 0;
uint32_t apos_cnt = 0;
uint32_t chr_start = 0;
uint32_t chr_end = 0;
uint32_t v_length = 0;
uint32_t v_apos = 0;
uint32_t v_rpos_end = 0;
uint32_t v_rpos = 0;
uint32_t v_rpos_pre = 0Xffffffff;
uint32_t w_v_rpos = 0, w_v_rpos_end = 0, ref_length_p = 0;
int vcf_sort_i = 0;
int64_t ite_i = 0;
uint32_t aseq_cnt = 0;
uint64_t ref_cv = 0;
uint64_t ref_cv_w = 0;
uint64_t ref_cv_w_backup = 0;
uint64_t result_num = 0;
uint64_t a_size = 0;
int64_t v_rpos_pre_snp = -1;
uint64_t* buffer_ref_seq = NULL;
FILE* fp_in_vcf_pos = NULL;
FILE* fp_in_vcf_seq = NULL;
FILE* fp_ref_seq = NULL;
vcf_record_s* vcf_rs = NULL;
//ref_length_p = ref_seq_n - 1;
ref_length_p = ref_seq_n;
fp_ref_seq = fopen(ref_seq, "rb");
if (fp_ref_seq == NULL)
{
fputs ("File error opening the seq file\n",stderr);
exit (1);
}
fprintf(stderr, "ref_seq_n: %u\n", ref_seq_n);//3095696031
a_size = (ref_seq_n >> 5) + 1;
buffer_ref_seq = (uint64_t* )calloc(a_size, 8);
result_num = fread(buffer_ref_seq, 8, a_size, fp_ref_seq);//63676768
fprintf(stderr, "result_num %u %u vcf_rpos %s\n", result_num, ref_seq_n, vcf_rpos);//96740501 3095696031
if(fp_ref_seq) fclose(fp_ref_seq);
bit_shift_value = (1 << vcf_bit_shift) - 1;
FILE* fp_out_rpos = fopen(vcf_rpos,"wb");
if(fp_out_rpos == NULL)
{
fprintf(stderr, "Error of opening vcf rpos file\n");
exit(1);
}
FILE* fp_out_aindex = fopen(vcf_aindex,"wb");
if(fp_out_aindex == NULL)
{
fprintf(stderr, "Error of opening vcf ALT index file\n");
exit(1);
}
FILE* fp_out_apos = fopen(vcf_apos,"wb");
if(fp_out_apos == NULL)
{
fprintf(stderr, "Error of opening vcf ALT pos file\n");
exit(1);
}
FILE* fp_out_aseq = fopen(vcf_aseq,"wb");
if(fp_out_aseq == NULL)
{
fprintf(stderr, "Error of opening vcf aseq file\n");
exit(1);
}
FILE* fp_out_rpos_end = fopen(vcf_rpos_end,"wb");
if(fp_out_rpos_end == NULL)
{
fprintf(stderr, "Error of opening vcf rpos end file\n");
exit(1);
}
FILE* fp_out_ref_seq_v = fopen(ref_vcf,"wb");
if(fp_out_ref_seq_v == NULL)
{
fprintf(stderr, "Error of opening vcf ref seq file\n");
exit(1);
}
FILE* fp_out_size = fopen(vcf_size,"wb");
if(fp_out_size == NULL)
{
fprintf(stderr, "Error of opening vcf size file\n");
exit(1);
}
FILE* fp_out_type = fopen(vcf_type,"wb");
if(fp_out_type == NULL)
{
fprintf(stderr, "Error of opening vcf size file\n");
exit(1);
}
FILE* fp_out_rpos_rec = fopen(vcf_rpos_rev,"wb");
if(fp_out_rpos_rec == NULL)
{
printf("Error of opening vcf rpos rev file\n");
exit(1);
}
FILE* fp_out_aindex_rec = fopen(vcf_aindex_rev,"wb");
if(fp_out_aindex_rec == NULL)
{
printf("Error of opening vcf ALT index rev file\n");
exit(1);
}
FILE* fp_out_apos_rec = fopen(vcf_apos_rev,"wb");
if(fp_out_apos_rec == NULL)
{
printf("Error of opening vcf ALT pos rev file\n");
exit(1);
}
FILE* fp_out_aseq_rec = fopen(vcf_aseq_rev,"wb");
if(fp_out_aseq_rec == NULL)
{
printf("Error of opening vcf aseq rev file\n");
exit(1);
}
FILE* fp_out_rpos_end_rec = fopen(vcf_rpos_end_rev,"wb");
if(fp_out_rpos_end_rec == NULL)
{
printf("Error of opening vcf rpos end rev file\n");
exit(1);
}
FILE* fp_out_ref_seq_v_rec = fopen(ref_vcf_rev,"wb");
if(fp_out_ref_seq_v_rec == NULL)
{
printf("Error of opening vcf ref seq rev file\n");
exit(1);
}
FILE* fp_out_size_rec = fopen(vcf_size_rev,"wb");
if(fp_out_size_rec == NULL)
{
printf("Error of opening vcf size rev file\n");
exit(1);
}
FILE* fp_out_type_rec = fopen(vcf_type_rev,"wb");
if(fp_out_type_rec == NULL)
{
printf("Error of opening vcf size rev file\n");
exit(1);
}
fprintf(stderr, "begin oganizing and indexing forward vcf info\n");
FILE* fp_out_chr_rpos = fopen(vcf_chr_rpos,"wb");
if(fp_out_chr_rpos == NULL)
{
fprintf(stderr, "Error of opening vcf chr rpos file\n");
exit(1);
}
uint32_t* chr_rpos = (uint32_t* )calloc(chr_file_n_vcf, 4);
fprintf(stderr, "There are %u(%u) chrs to index\n", chr_file_n_vcf - 1, chr_no_n);
//debug
for(chr_no_i = 0; chr_no_i < chr_no_n; chr_no_i++)
fprintf(stderr, "%s\n", chr_no_a[chr_no_i]);
chr_rpos[0] = 0;
for(chr_i = 1; chr_i < chr_file_n_vcf; chr_i++)
{
strcpy(chr_no_tmp, chr_names[chr_i - 1] + 3);//extract the number here is only for hg19
chr_no_f = 0;
for(chr_no_i = 0; chr_no_i < chr_no_n; chr_no_i++)
if(strcmp(chr_no_a[chr_no_i], chr_no_tmp) == 0)//chr_file_n_vcf chr_names[] from ref; chr_no_n chr_no_a[] from vcf
{
chr_no_f = 1;
break;
}
chr_start = chr_end_n[chr_i - 1] - 1;
chr_end = chr_end_n[chr_i] - 1;
if(chr_no_f)
{
memset(file_d, 0, ROUTE_LENGTH_MAX);
strcpy(file_d, index_route);
strcat(file_d, vcf_pos_s);
strcat(file_d, chr_no_tmp);
fp_in_vcf_pos = fopen(file_d, "rb");
memset(file_d, 0, ROUTE_LENGTH_MAX);
strcpy(file_d, index_route);
strcat(file_d, vcf_seq);
strcat(file_d, chr_no_tmp);
fp_in_vcf_seq = fopen(file_d, "rb");
//debug
//fprintf(stderr, "5 %u %u %s %s %s\n", chr_no_i, chr_vcf_seq_n[chr_no_i], chr_no_a[chr_no_i], chr_no_tmp, file_d);
alt_seq_chr = (uint8_t* )calloc(chr_vcf_seq_n[chr_no_i], 1);
fread(alt_seq_chr, 1, chr_vcf_seq_n[chr_no_i], fp_in_vcf_seq);
chr_vcf_total = chr_vcf_n[chr_no_i];
vcf_rs = (vcf_record_s* )malloc(chr_vcf_total * sizeof(vcf_record_s));
//may need to change
for(ite_i = 0; ite_i < chr_vcf_total; ite_i++)
fread(vcf_rs[ite_i].vcf_buff_sort, 4, 4, fp_in_vcf_pos);
//debug
//fprintf(stderr, "step00\n");
//fflush(stderr);
qsort(vcf_rs, chr_vcf_total, sizeof(vcf_record_s ), compare_vcf_sort);
//debug
//fprintf(stderr, "step01 %u\n", chr_vcf_total);
//fflush(stderr);
for(vcf_sort_i = 0; vcf_sort_i < chr_vcf_total; vcf_sort_i++)
{
v_rpos = vcf_rs[vcf_sort_i].vcf_buff_sort[0] + chr_start;
v_type = (vcf_rs[vcf_sort_i].vcf_buff_sort[1]) & bit_shift_value;
v_length = vcf_rs[vcf_sort_i].vcf_buff_sort[1] >> vcf_bit_shift;
v_apos = vcf_rs[vcf_sort_i].vcf_buff_sort[2];
v_rpos_end = vcf_rs[vcf_sort_i].vcf_buff_sort[3] + chr_start;
//for test
//fprintf(stderr, "sort: %u %u %u %u %u %u\n", v_rpos, v_type, v_length, v_apos, v_rpos_end, v_rpos_pre_snp);
//fflush(stderr);
//2147484023 0 1 70606 2147484024 2147483601
//67108875 * 8 = 536871005
//773924008
if(v_type)
{
if(v_rpos_pre != v_rpos)
{
fwrite(&v_rpos, 4, 1, fp_out_rpos);
rpos_cnt++;
fwrite(&apos_cnt, 4, 1, fp_out_aindex);
}
fwrite(&aseq_cnt, 4, 1, fp_out_apos);
apos_cnt++;
fwrite(&v_rpos_end, 4, 1, fp_out_rpos_end);
if(v_type != 2)
{
fwrite(alt_seq_chr + v_apos, 1, v_length, fp_out_aseq);
aseq_cnt += v_length;
}
v_rpos_pre = v_rpos;
//write type
fwrite(&v_type, 1, 1, fp_out_type);
}
else //SNP write SNPs and characters between two SNPs into ref_vcf.seq
{
//debug
//fprintf(stderr, "step111\n");
//fflush(stderr);
for(ite_i = v_rpos_pre_snp + 1; ite_i < v_rpos; ite_i++)
{
ref_c = ((buffer_ref_seq[ite_i >> 5] >> ((31 - (ite_i & 0X1f)) << 1)) & 0X3);
ref_cv = (1 << ref_c);
ref_cv_w |= (ref_cv << (((uint64_t )(15 - ref_cv_i)) << 2));
ref_cv_i++;
ref_cv_n++;
if(ref_cv_i == 16)
{
fwrite(&ref_cv_w, 8, 1, fp_out_ref_seq_v);//ref seq between two snps
ref_cv_w = 0;
ref_cv_i = 0;
}
}
//debug
//fprintf(stderr, "step222\n");
if(v_rpos_pre_snp != v_rpos)
{
ref_c = ((buffer_ref_seq[v_rpos >> 5] >> ((31 - (v_rpos & 0X1f)) << 1)) & 0X3);
ref_cv = (1 << ref_c);
//debug
//fprintf(stderr, "%u\n", alt_seq_chr[v_apos]);
//fflush(stderr);
ref_cv |= (1 << alt_seq_chr[v_apos]);
ref_cv_w |= (ref_cv << (((uint64_t )(15 - ref_cv_i)) << 2));
ref_cv_w_backup = ref_cv_w;
ref_cv_i++;
ref_cv_n++;
if(ref_cv_i == 16)
{
fwrite(&ref_cv_w, 8, 1, fp_out_ref_seq_v);
ref_cv_w = 0;
ref_cv_i = 0;
}
}
else
{
if(ref_cv_i)
{
ref_cv_w |= (((uint64_t )(1 << alt_seq_chr[v_apos])) << (((uint64_t )(16 - ref_cv_i)) << 2));
}
else
{
ref_cv_w_backup |= (1 << alt_seq_chr[v_apos]);
fseek(fp_out_ref_seq_v, -8, SEEK_CUR);
fwrite(&ref_cv_w_backup, 8, 1, fp_out_ref_seq_v);
}
}
v_rpos_pre_snp = v_rpos;
}
}
//debug
//fprintf(stderr, "step02\n");
//free
if(vcf_rs) free(vcf_rs);
if(alt_seq_chr) free(alt_seq_chr);
//debug
//fprintf(stderr, "step11\n");
//fflush(stderr);
}
else //write SNP seq directly
{
//debug
//fprintf(stderr, "step33\n");
//fflush(stderr);
for(ite_i = chr_start; ite_i < chr_end; ite_i++)
{
ref_c = ((buffer_ref_seq[ite_i >> 5] >> ((31 - (ite_i & 0X1f)) << 1)) & 0X3);
ref_cv = (1 << ref_c);
ref_cv_w |= (ref_cv << (((uint64_t )(15 - ref_cv_i)) << 2));
ref_cv_i++;
ref_cv_n++;
if(ref_cv_i == 16)
{
fwrite(&ref_cv_w, 8, 1, fp_out_ref_seq_v);
ref_cv_w = 0;
ref_cv_i = 0;
}