-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbwtsearch.c
126 lines (114 loc) · 3.44 KB
/
bwtsearch.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
//
// main.c
// bwtsearch
//
// Created by Zhuang Jia on 25/04/2014.
// Copyright (c) 2014 Zhuang Jia. All rights reserved.
//
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
int main(int argc, const char * argv[])
{
if (argc == 5) {
FILE *fp_bwtfile;
if((fp_bwtfile = fopen(argv[1], "r")) == NULL) {
printf("cannot open bwt file\n");
exit(0);
}
int last_char; //index of last character
fread(&last_char, 1, 4, fp_bwtfile);
//printf("last=%ld", last_char);
int ch;
int array[256] = {0};
FILE *fp_indexfile;
if((fp_indexfile = fopen(argv[2], "w+")) == NULL) {
printf("cannot open index file\n");
exit(0);
}
//open output file
FILE *fp_output;
if((fp_output = fopen(argv[4], "w+")) == NULL) {
printf("cannot open output file\n");
exit(0);
}
bool flag = false;
while (true) {
//read bwt file 2048 bytes by 2048 bytes and put them into an array
int i = 0;
for (i=0; i<2048; i++) {
if ((ch=fgetc(fp_bwtfile)) != EOF) {
array[ch]++;
fprintf(fp_output, "%c", ch);
}
else {
flag = true;
break;
}
}
//wrtie arrays into index file
int j = 0;
for (j=0; j<256; j++) {
fwrite(&array[j], sizeof(int), 1, fp_indexfile);
}
if (flag) {
break;
}
}
int chara;
int index = last_char;
int output_index = -1;
int temp = 999;
int temp_array[1000] = {0};
do {
int b_nb = index/2048;
int r_nb = index%2048;
fseek(fp_bwtfile, 4+index, SEEK_SET); //find the char
chara = fgetc(fp_bwtfile);
//write this char into a file
temp_array[temp] = chara;
temp--;
if (temp < 0) {
output_index = output_index - 1000;
fseek(fp_output, output_index--, SEEK_END);
fprintf(fp_output, "%s", temp_array);
temp = 999;
}
//printf("%d ",chara);
int pre_b_nb = 0;
int cur_r_nb = 0;
int matching_nb = 0;
int less_than_nb = 0;
//get matching number
if (b_nb>0) {
int temp = chara*4+1024*(b_nb-1);
fseek(fp_indexfile, temp, SEEK_SET);
fread(&pre_b_nb, 1, 4, fp_indexfile);
}
int i;
for (i=b_nb*2048; i<b_nb*2048+r_nb; i++) {
fseek(fp_bwtfile, i+4, SEEK_SET);
unsigned char temp = fgetc(fp_bwtfile);
if (chara == temp) {
cur_r_nb++;
}
}
matching_nb = pre_b_nb + cur_r_nb;
int j;
for (j=0; j<chara; j++) {
less_than_nb += array[j];
}
index = matching_nb + less_than_nb;
} while (last_char != index);
if(temp >= 0) {
output_index = output_index - (999-temp);
fseek(fp_output, output_index, SEEK_END);
fprintf(fp_output, "%s", temp_array);
}
}
else {
}
}
//some new things