-
Notifications
You must be signed in to change notification settings - Fork 8
/
index_build.c
2658 lines (2146 loc) · 67.5 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"
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();
#ifdef LAST_DEBUG
load_reffile_kmer_fa();
#endif
file_kmer_qsort();
return 0;
}
void upper_string(char *string)
{
while(*string)
{
if ( *string >= 'a' && *string <= 'z' )
{
*string = *string - 32;
}
string++;
}
}
/*
void load_reffile_kmer()
{
fp_n = fopen(N_route, "w");
if(fp_n == NULL) printf("cannot open N statistical file\n");
uint32_t n_cnt = 0;
uint32_t n_tr = 0;
uint8_t n_end_f = 0;
uint8_t n_w_f = 0;
//read one line
char one_line[REF_FASTA_LINE + 1] = "";
char kmer_pre[KT_LENGTH_MAX + 1] = "";
char kmer_com[KT_LENGTH_MAX + REF_FASTA_LINE + 1] = "";
char kmer[KT_LENGTH_MAX + 1];
char input = charN;
char output;
uint8_t line_i = 0;
uint8_t kmer_i = 0;
uint8_t line_l = 0;
uint8_t l_p = 0;
uint32_t line_t_c = 0;
uint32_t i = 0;
FILE* fpin_ref = NULL;
DIR *directory_pointer;
struct dirent *entry;
char file_ref[ROUTE_LENGTH_MAX];
//file creating
uint32_t add = 0;
char kmer_f[KF_LENGTH_MAX] = "";
char kmer_s[KT_LENGTH_MAX] = "";
uint32_t file_n = 0;
char file_d[KF_LENGTH_MAX + ROUTE_LENGTH_MAX] = "";
file_n = (1 << (f << 1));
//modification
//file_n = ((1 << (f << 1)) << 1);
FILE** file_p = (FILE** )malloc(file_n * sizeof(FILE* ));
if(file_p == NULL)
{
printf("fail to allocate memory\n");
exit(1);
}
for(i = 0; i < file_n; i++)
file_p[i] = NULL;
uint32_t line_tol = 0;
uint32_t* file_line_cnt = (uint32_t* )calloc(file_n, 4);
if(file_line_cnt == NULL)
{
printf("fail to allocate memory\n");
exit(1);
}
FILE* file_sta = NULL;
file_sta = fopen(filename_sta,"w");
if (file_sta == NULL)
{
fputs ("File error of creating statistical file\n",stderr);
exit(1);
}
#ifdef UNPIPATH_OFF_K20
uint32_t write_buff[4];
uint64_t pos = 0;
#else
uint32_t write_buff[3];
uint32_t pos = 0;
#endif
uint8_t last_kmer_f = 0;
chr_end_n[0] = 1;
uint32_t chr_i = 0;
//ref seq load
uint64_t ref_seq_buffer[128];
fp_ref_seq = fopen(ref_seq, "wb");
if((directory_pointer = opendir(filename_ref))==NULL)
printf( "Error opening \n ");
else
{
while((entry = readdir(directory_pointer))!=NULL)
{
//begin with each chr file
if((strstr(entry->d_name,".fna")!= NULL) && (strstr(entry->d_name,"NC")!= NULL))
{
memset(file_ref, 0, sizeof(file_ref));
strcpy(file_ref,filename_ref);
strcat(file_ref, entry->d_name);
//if file is not empty
strcpy(chr_names[chr_file_n], entry->d_name);
//begin loading file and loading kmer into graph
fpin_ref = fopen(file_ref, "r");
if (fpin_ref == NULL)
{
fputs ("File error of reading ref file\n",stderr);
exit(1);
}
l_p = 0;
while ((!feof(fpin_ref)) && (fgets(one_line, REF_FASTA_LINE + 2, fpin_ref) != NULL))
{
//line_c++;
if(strstr(one_line,identifier) != NULL)//>
{
continue;
}
line_t_c++;
line_l = strlen(one_line);
if(one_line[line_l - 1] == '\n')
{
one_line[line_l - 1] = '\0';
line_l--;
}
//ref_load
for(line_i = 0; line_i < line_l; line_i++)
{
ref_seq_buffer[(ref_seq_n & 0Xfff) >> 5] |= (((uint64_t )charToDna5_N2[(uint8_t )one_line[line_i]]) << ((31 - ((ref_seq_n & 0Xfff) & 0X1f)) << 1));
++ref_seq_n;
if((ref_seq_n & 0Xfff) == 0)
{
fwrite(ref_seq_buffer, 8, 128, fp_ref_seq);
memset(ref_seq_buffer, 0, 1024);
}
}
memset(kmer_com, 0, KT_LENGTH_MAX + REF_FASTA_LINE);
seq_exact_2(kmer_pre,kmer_com,kmer_i,0)
seq_exact_2(one_line,kmer_com,kmer_i,l_p)
for(line_i = 0; line_i < line_l - k_t + l_p; line_i++,pos++)
{
seq_exact(kmer_com,kmer,kmer_i,line_i,line_i + k_t)
if((strstr(kmer, char_N) != NULL) || (strstr(kmer, char_n) != NULL))
{
memset(kmer_pre, 0, KT_LENGTH_MAX + 1);
kmer_pre[0] = '\0';
input = charN;
//N number
++n_cnt;
n_w_f = 1;
continue;
}
//N position and number
if(n_w_f)
{
if(n_end_f)
{
n_tr = n_cnt;
n_end_f = 0;
}
else n_tr = n_cnt - (k_t - 1);
n_cnt = 0;
n_w_f = 0;
}
output = kmer_com[line_i + k_t];
//do something here, kmer[], pos + 1
strncpy(kmer_f, (const char* )kmer, f);
kmer_address(kmer_f, f, kmer_i, add)
upper_string(kmer_f);
memset(file_d, 0, KF_LENGTH_MAX + ROUTE_LENGTH_MAX);
strcpy(file_d, (const char* )filename_div);
strcat(file_d, (const char* )kmer_f);
strcat(file_d, suff);
if(file_p[add] == NULL)
{
file_p[add] = fopen(file_d, "wb");
if (file_p[add] == NULL)
{
fputs ("File error of creating divisional file\n",stderr);
exit(1);
}
}
seq_exact(kmer, kmer_s, kmer_i, f, strlen(kmer));
//binary file
//must assign in this order
kmer_bit32a(kmer_s, k_t - f, kmer_i, write_buff, 3)
write_buff[1] |= (input << 24);
write_buff[1] |= (output << 16);
#ifdef UNPIPATH_OFF_K20
write_buff[3] = (pos + 1) >> 32;
write_buff[0] = (pos + 1) & 0Xffffffff;
fwrite(write_buff, 4, 4, file_p[add]);
#else
write_buff[0] = pos + 1;
fwrite(write_buff, 4, 3, file_p[add]);
#endif
//
file_line_cnt[add]++;
line_tol++;
//
input = kmer_com[line_i];
}
seq_exact(kmer_com, kmer_pre, kmer_i, line_l - k_t + l_p, line_l+l_p)
l_p = k_t;
};
//write the last kmer
if(kmer_pre[0] != '\0')
{
seq_exact(kmer_pre,kmer,kmer_i,0,k_t)
last_kmer_f = 1;
if((strstr(kmer, char_N) != NULL) || (strstr(kmer, char_n) != NULL))
{
input = charN;
last_kmer_f = 0;
//N number
++n_cnt;
n_w_f = 1;
n_end_f = 1;
}
if(last_kmer_f)
{
output = charN;
//do something here, kmer[], pos + 1
strncpy(kmer_f, (const char* )kmer, f);
kmer_address(kmer_f, f, kmer_i, add)
upper_string(kmer_f);
memset(file_d, 0, KF_LENGTH_MAX + ROUTE_LENGTH_MAX);
strcpy(file_d, (const char* )filename_div);
strcat(file_d, (const char* )kmer_f);
strcat(file_d, suff);
if(file_p[add] == NULL)
{
file_p[add] = fopen(file_d, "wb");
if (file_p[add] == NULL)
{
fputs ("File error of creating divisional file\n",stderr);
exit(1);
}
}
seq_exact(kmer, kmer_s, kmer_i, f, strlen(kmer));
//binary file
//must assign in this order
kmer_bit32a(kmer_s, k_t - f, kmer_i, write_buff, 3)
write_buff[1] |= (input << 24);
write_buff[1] |= (output << 16);
write_buff[0] = pos + 1;
#ifdef UNPIPATH_OFF_K20
write_buff[3] = (pos + 1) >> 32;
write_buff[0] = (pos + 1) & 0Xffffffff;
fwrite(write_buff, 4, 4, file_p[add]);
#else
write_buff[0] = pos + 1;
fwrite(write_buff, 4, 3, file_p[add]);
#endif
//
file_line_cnt[add]++;
line_tol++;
}
}
memset(kmer_pre, 0, KT_LENGTH_MAX + 1);
kmer_pre[0] = '\0';
input = charN;
pos += k_t;
//end last kmer
fclose(fpin_ref);
chr_end_n[chr_file_n] = pos + 1;
chr_file_n++;
//
printf("Has finished loading %s\n", entry->d_name);//, pos+1
}
}
closedir(directory_pointer);
}
fwrite(ref_seq_buffer, 8, ((ref_seq_n & 0Xfff) >> 5) + 1, fp_ref_seq);
fclose(fp_ref_seq);
printf("number chars of ref seq: %"PRId64"\n", (((ref_seq_n >> 5) + 1) << 3));
FILE* fp_chr = fopen(unichr,"w");
if (fp_chr == NULL)
{
fputs ("File error of creating chr info file\n",stderr);
exit(1);
}
#ifdef UNPIPATH_OFF_K20
for(chr_i = 1; chr_i < chr_file_n; chr_i++)
{
fprintf(fp_chr,"%s\t%"PRId64"\n",chr_names[chr_i], chr_end_n[chr_i]);
}
for(i = 0; i < file_n; i++)
fprintf(file_sta,"%"PRId64"\n",file_line_cnt[i]);
fprintf(file_sta,"\n%"PRId64"",line_tol);
printf("total lines of ref file: %"PRId64"\n",line_tol);
#else
for(chr_i = 1; chr_i < chr_file_n; chr_i++)
fprintf(fp_chr,"%s\t%u\n",chr_names[chr_i], chr_end_n[chr_i]);
for(i = 0; i < file_n; i++)
fprintf(file_sta,"%u\n",file_line_cnt[i]);
fprintf(file_sta,"\n%u",line_tol);
printf("total lines of ref file: %u\n",line_tol);
#endif
fclose(fp_chr);
for(i = 0; i < file_n; i++)
{
if(file_p[i] != NULL)
fclose(file_p[i]);
}
if(file_p) free(file_p);
if(file_line_cnt) free(file_line_cnt);
fclose(file_sta);
fclose(fp_n);
}
*/
void load_reffile_kmer_fa()
{
fp_n = fopen(N_route, "w");
if(fp_n == NULL) printf("cannot open N statistical file\n");
ref_seq_n = START_POS_REF;
uint32_t n_cnt = 0;
uint32_t n_tr = 0;
uint8_t n_end_f = 0;
uint8_t n_w_f = 0;
//read one line
char one_line[REF_FASTA_LINE + 1] = "";
char kmer_pre[KT_LENGTH_MAX + 1] = "";
char kmer_com[KT_LENGTH_MAX + REF_FASTA_LINE + 1] = "";
char kmer[KT_LENGTH_MAX + 1];
char input = charN;
char output;
uint8_t line_i = 0;
uint32_t kmer_i = 0;
uint8_t line_l = 0;
uint8_t l_p = 0;
uint32_t line_t_c = 0;
uint32_t i = 0;
FILE* fpin_ref = NULL;
//file creating
uint32_t add = 0;
char kmer_f[KF_LENGTH_MAX] = "";
char kmer_s[KT_LENGTH_MAX] = "";
uint32_t file_n = 0;
char file_d[KF_LENGTH_MAX + ROUTE_LENGTH_MAX] = "";
file_n = ((uint32_t )1 << (f << 1));
//modification
//file_n = ((1 << (f << 1)) << 1);
printf("%u files to be written in folder div\n", file_n);
fflush(stdout);
FILE** file_p = (FILE** )malloc(file_n * sizeof(FILE* ));
if(file_p == NULL)
{
printf("Fail to allocate memory for file_p\n");
fflush(stdout);
exit(1);
}
for(i = 0; i < file_n; i++)
file_p[i] = NULL;
FILE* file_sta = NULL;
file_sta = fopen(filename_sta,"w");
if (file_sta == NULL)
{
fputs ("File error of creating statistical file\n",stderr);
fflush(stdout);
exit(1);
}
#ifdef UNPIPATH_OFF_K20
uint32_t write_buff[4];
uint64_t pos = START_POS_REF;
uint64_t pos_tr = 0;
uint64_t line_tol = 0;
uint64_t* file_line_cnt = (uint64_t* )calloc(file_n, 8);
#else
uint32_t write_buff[3];
uint32_t pos = START_POS_REF;
uint32_t pos_tr = 0;
uint32_t line_tol = 0;
uint32_t* file_line_cnt = (uint32_t* )calloc(file_n, 4);
#endif
if(file_line_cnt == NULL)
{
printf("Fail to allocate memory for file_line_cnt\n");
fflush(stdout);
exit(1);
}
//read file name under path
uint8_t last_kmer_f = 0;
chr_end_n[0] = 1;
uint32_t chr_i = 0;
//ref seq load
uint64_t ref_seq_buffer[128];
fp_ref_seq = fopen(ref_seq, "wb");
//begin loading file and loading kmer into graph
fpin_ref = fopen(filename_ref, "r");
if (fpin_ref == NULL)
{
fputs ("File error of reading ref file and wrong ref file name or route\n",stderr);
exit(1);
}
l_p = 0;
uint8_t fisrt_chr = 1;
uint32_t chr_name_n = 0;
uint32_t chr_posend_n = 0;
#ifdef CHR_NAME_SPLIT
char* pch = NULL;
char* saveptr = NULL;
char tmp_chr_name[REF_FASTA_LINE + 1];
#endif
while ((!feof(fpin_ref)) && (fgets(one_line, REF_FASTA_LINE + 2, fpin_ref) != NULL))
{
if(strstr(one_line,identifier) != NULL)
{
one_line[strlen(one_line) - 1] = '\0';
#ifdef CHR_NAME_SPLIT
strcpy(tmp_chr_name, one_line + 1);
pch = strtok_r(tmp_chr_name, " ", &saveptr);
strcpy(chr_names[chr_name_n++], pch);
#else
strcpy(chr_names[chr_name_n++], one_line + 1);
#endif
if(fisrt_chr)
{
fisrt_chr = 0;
continue;
}
//write the last kmer
if(kmer_pre[0] != '\0')
{
seq_exact(kmer_pre,kmer,kmer_i,0,k_t)
last_kmer_f = 1;
if((strstr(kmer, char_N) != NULL) || (strstr(kmer, char_n) != NULL))
{
input = charN;
last_kmer_f = 0;
//N number
++n_cnt;
n_w_f = 1;
n_end_f = 1;
}
if(last_kmer_f)
{
output = charN;
//do something here, kmer[], pos + 1
strncpy(kmer_f, (const char* )kmer, f);
kmer_address(kmer_f, f, kmer_i, add)
upper_string(kmer_f);
memset(file_d, 0, KF_LENGTH_MAX + ROUTE_LENGTH_MAX);
strcpy(file_d, (const char* )filename_div);
strcat(file_d, (const char* )kmer_f);
strcat(file_d, suff);
if(file_p[add] == NULL)
{
file_p[add] = fopen(file_d, "wb");
if (file_p[add] == NULL)
{
fputs ("File error of creating divisional file\n",stderr);
exit(1);
}
}
seq_exact(kmer, kmer_s, kmer_i, f, strlen(kmer));
//binary file
//must assign in this order
kmer_bit32a(kmer_s, k_t - f, kmer_i, write_buff, 3)
write_buff[1] |= (input << 24);
write_buff[1] |= (output << 16);
#ifdef UNPIPATH_OFF_K20
write_buff[3] = (pos + 1) >> 32;
write_buff[0] = (pos + 1) & 0Xffffffff;
fwrite(write_buff, 4, 4, file_p[add]);
#else
write_buff[0] = pos + 1;
fwrite(write_buff, 4, 3, file_p[add]);
//fwrite(write_buff, sizeof(write_buff ), 1, file_p[add]);
#endif
#ifdef DEBUG_INPUT
if(write_buff[2] == 8258096)
printf("\n\n2. %s:\nline: %s\n%s\n%u\n\n\n", tmp_chr_name, one_line, kmer, (uint16_t )write_buff[1]);
#endif
//
file_line_cnt[add]++;
line_tol++;
}
}
memset(kmer_pre, 0, KT_LENGTH_MAX + 1);
kmer_pre[0] = '\0';
input = charN;
pos += k_t;
//end last kmer
chr_end_n[chr_posend_n++] = pos + 1;
printf("Has finished loading %s\n", chr_names[chr_name_n - 2]);
fflush(stdout);
l_p = 0;
}
else
{
line_t_c++;
line_l = strlen(one_line);
if(one_line[line_l - 1] == '\n')
{
one_line[line_l - 1] = '\0';
line_l--;
}
//ref_load
for(line_i = 0; line_i < line_l; line_i++)
{
//modification
one_line[line_i] = charTochar[(uint8_t )one_line[line_i]];
ref_seq_buffer[(ref_seq_n & 0Xfff) >> 5] |= (((uint64_t )charToDna5_N2[(uint8_t )one_line[line_i]]) << ((31 - ((ref_seq_n & 0Xfff) & 0X1f)) << 1));
++ref_seq_n;
if((ref_seq_n & 0Xfff) == 0)
{
fwrite(ref_seq_buffer, 8, 128, fp_ref_seq);
memset(ref_seq_buffer, 0, 1024);
}
}
memset(kmer_com, 0, KT_LENGTH_MAX + REF_FASTA_LINE);
seq_exact_2(kmer_pre,kmer_com,kmer_i,0)
seq_exact_2(one_line,kmer_com,kmer_i,l_p)
for(line_i = 0; line_i < line_l - k_t + l_p; line_i++,pos++)
{
seq_exact(kmer_com,kmer,kmer_i,line_i,line_i + k_t)
if((strstr(kmer, char_N) != NULL) || (strstr(kmer, char_n) != NULL))
{
memset(kmer_pre, 0, KT_LENGTH_MAX + 1);
kmer_pre[0] = '\0';
input = charN;
//N number
++n_cnt;
n_w_f = 1;
continue;
}
//N position and number
if(n_w_f)
{
pos_tr = pos + 1;
if(n_end_f)
{
n_tr = n_cnt;
n_end_f = 0;
}
else n_tr = n_cnt - (k_t - 1);
if(n_tr <= 0)
{
printf("wrong n %u %u\n", n_tr, n_cnt);
exit(1);
}
fwrite(&pos_tr, 4, 1, fp_n);
fwrite(&n_tr, 4, 1, fp_n);
n_cnt = 0;
n_w_f = 0;
}
output = kmer_com[line_i + k_t];
//do something here, kmer[], pos + 1
strncpy(kmer_f, (const char* )kmer, f);
kmer_address(kmer_f, f, kmer_i, add)
upper_string(kmer_f);
memset(file_d, 0, KF_LENGTH_MAX + ROUTE_LENGTH_MAX);
strcpy(file_d, (const char* )filename_div);
strcat(file_d, (const char* )kmer_f);
strcat(file_d, suff);
if(file_p[add] == NULL)
{
file_p[add] = fopen(file_d, "wb");
if (file_p[add] == NULL)
{
fputs ("File error of creating divisional file\n",stderr);
exit(1);
}
}
seq_exact(kmer, kmer_s, kmer_i, f, strlen(kmer));
//binary file
//must assign in this order
kmer_bit32a(kmer_s, k_t - f, kmer_i, write_buff, 3)
write_buff[1] |= (input << 24);
write_buff[1] |= (output << 16);
#ifdef UNPIPATH_OFF_K20
write_buff[3] = (pos + 1) >> 32;
write_buff[0] = (pos + 1) & 0Xffffffff;
fwrite(write_buff, 4, 4, file_p[add]);
#else
write_buff[0] = pos + 1;
fwrite(write_buff, 4, 3, file_p[add]);
//fwrite(write_buff, sizeof(write_buff ), 1, file_p[add]);
#endif
#ifdef DEBUG_INPUT
if(write_buff[2] == 8258096)
printf("\n\n%2. s:\nline: %s\n%s\n%u\n\n\n", tmp_chr_name, one_line, kmer, write_buff[1]);
#endif
//
file_line_cnt[add]++;
line_tol++;
//
input = kmer_com[line_i];
}
seq_exact(kmer_com, kmer_pre, kmer_i, line_l - k_t + l_p, line_l+l_p)
l_p = k_t;
}
}
//write the last kmer
if(kmer_pre[0] != '\0')
{
seq_exact(kmer_pre,kmer,kmer_i,0,k_t)
last_kmer_f = 1;
if((strstr(kmer, char_N) != NULL) || (strstr(kmer, char_n) != NULL))
{
input = charN;
last_kmer_f = 0;
//N number
++n_cnt;
n_w_f = 1;
n_end_f = 1;
}
if(last_kmer_f)
{
output = charN;
//do something here, kmer[], pos + 1
strncpy(kmer_f, (const char* )kmer, f);
kmer_address(kmer_f, f, kmer_i, add)
upper_string(kmer_f);
memset(file_d, 0, KF_LENGTH_MAX + ROUTE_LENGTH_MAX);
strcpy(file_d, (const char* )filename_div);
strcat(file_d, (const char* )kmer_f);
strcat(file_d, suff);
if(file_p[add] == NULL)
{
file_p[add] = fopen(file_d, "wb");
if (file_p[add] == NULL)
{
fputs ("File error of creating divisional file\n",stderr);
exit(1);
}
}
seq_exact(kmer, kmer_s, kmer_i, f, strlen(kmer));
//binary file
//must assign in this order
kmer_bit32a(kmer_s, k_t - f, kmer_i, write_buff, 3)
write_buff[1] |= (input << 24);
write_buff[1] |= (output << 16);
#ifdef UNPIPATH_OFF_K20
write_buff[3] = (pos + 1) >> 32;
write_buff[0] = (pos + 1) & 0Xffffffff;
fwrite(write_buff, 4, 4, file_p[add]);
#else
write_buff[0] = pos + 1;
fwrite(write_buff, 4, 3, file_p[add]);
//fwrite(write_buff, sizeof(write_buff ), 1, file_p[add]);
#endif
#ifdef DEBUG_INPUT
if(write_buff[2] == 8258096)
printf("\n\n2. %s:\nline: %s\n%s\n%u\n\n\n", tmp_chr_name, one_line, kmer, write_buff[1]);
#endif
//
file_line_cnt[add]++;
line_tol++;
}
}
pos += k_t;
chr_end_n[chr_posend_n++] = pos + 1;
printf("Has finished loading %s\n", chr_names[chr_name_n - 1]);
#ifdef UNPIPATH_OFF_K20
printf("total number of lines: %"PRId64"\n",line_tol);
printf("total number of positions on ref seq: %"PRId64"\n", pos+1);
#else
printf("total number of lines: %u\n",line_tol);
printf("total number of positions on ref seq: %u\n", pos+1);
#endif
fclose(fpin_ref);
fwrite(ref_seq_buffer, 8, ((ref_seq_n & 0Xfff) >> 5) + 1, fp_ref_seq);
fclose(fp_ref_seq);
printf("total number of chars to allocate for ref seq: %"PRId64"\n", (((ref_seq_n >> 5) + 1) << 3));
fflush(stdout);
FILE* fp_chr = fopen(unichr,"w");
if (fp_chr == NULL)
{
fputs ("File error of creating chr info file\n",stderr);
exit(1);
}
#ifdef UNPIPATH_OFF_K20
for(chr_i = 0; chr_i < chr_posend_n; chr_i++)
{
fprintf(fp_chr,"%s\n%"PRId64"\n",chr_names[chr_i], chr_end_n[chr_i]);
}
fclose(fp_chr);
for(i = 0; i < file_n; i++)
fprintf(file_sta,"%"PRId64"\n",file_line_cnt[i]);
fprintf(file_sta,"\n%"PRId64"",line_tol);
fflush(file_sta);
#else
for(chr_i = 0; chr_i < chr_posend_n; chr_i++)
{
fprintf(fp_chr,"%s\n%u\n",chr_names[chr_i], chr_end_n[chr_i]);
}
fclose(fp_chr);
for(i = 0; i < file_n; i++)
fprintf(file_sta,"%u\n",file_line_cnt[i]);
fprintf(file_sta,"\n%u",line_tol);
fflush(file_sta);
#endif
for(i = 0; i < file_n; i++)
{
if(file_p[i] != NULL)
fclose(file_p[i]);
}
free(file_p);
free(file_line_cnt);
fclose(file_sta);
fclose(fp_n);
}
int compare (const void * a, const void * b)
{
k_p* kp1 = (k_p* )a;
k_p* kp2 = (k_p* )b;
uint16_t kmerf1 = (uint16_t )((kp1->kp)[1]);
uint16_t kmerf2 = (uint16_t )((kp2->kp)[1]);
uint32_t kmers1 = (uint32_t )((kp1->kp)[2]);
uint32_t kmers2 = (uint32_t )((kp2->kp)[2]);
if (kmerf1 > kmerf2)
return 1;
else if (kmerf1 < kmerf2)
return -1;
else
{
if (kmers1 > kmers2)
return 1;
else if (kmers1 < kmers2)
return -1;
else
{
#ifdef UNPIPATH_OFF_K20
/*
uint64_t tmp_kp_pos1 = (kp1->kp)[3];
uint64_t tmp_kp_pos2 = (kp2->kp)[3];
tmp_kp_pos1 = ((tmp_kp_pos1 << 32) | ((kp1->kp)[0]));
tmp_kp_pos2 = ((tmp_kp_pos2 << 32) | ((kp2->kp)[0]));
if (tmp_kp_pos1 > tmp_kp_pos2)
return 1;
else if (tmp_kp_pos1 < tmp_kp_pos2)
return -1;
else
{
printf("same pos\n");
exit(2);
return 0;
}
*/
if ((kp1->kp)[3] > (kp2->kp)[3])
return 1;
else if ((kp1->kp)[3] < (kp2->kp)[3])
return -1;
else
{
if ((kp1->kp)[0] > (kp2->kp)[0])
return 1;
else if ((kp1->kp)[0] < (kp2->kp)[0])
return -1;
else
{
printf("same pos\n");
fflush(stdout);
return 0;
}
}
#else
if ((kp1->kp)[0] > (kp2->kp)[0])
return 1;
else if ((kp1->kp)[0] < (kp2->kp)[0])
return -1;
else
{
printf("same pos\n");
fflush(stdout);
return 0;
}
#endif
}
}
}
int compare_pu (const void * a, const void * b)
{
p_u* pu1 = (p_u *)a;
p_u* pu2 = (p_u *)b;
if(pu1->pos > pu2->pos)
return 1;
else if(pu1->pos < pu2->pos)
return -1;
else return 0;
}
int compare_us (const void * a, const void * b)
{
k_u* ku1 = (k_u *)a;
k_u* ku2 = (k_u *)b;
if(ku1->kmer > ku2->kmer)