9 uint64_t len:32, line_len:16, line_blen:16;
12 KHASH_MAP_INIT_STR(s, faidx1_t)
18 #define ftello(fp) ftell(fp)
19 #define fseeko(fp, offset, whence) fseek(fp, offset, whence)
21 extern off_t ftello(FILE *stream);
22 extern int fseeko(FILE *stream, off_t offset, int whence);
25 #define razf_read(fp, buf, size) fread(buf, 1, size, fp)
26 #define razf_open(fn, mode) fopen(fn, mode)
27 #define razf_close(fp) fclose(fp)
28 #define razf_seek(fp, offset, whence) fseeko(fp, offset, whence)
29 #define razf_tell(fp) ftello(fp)
40 #define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
43 static inline void fai_insert_index(faidx_t *idx, const char *name, int len, int line_len, int line_blen, uint64_t offset)
48 if (idx->n == idx->m) {
49 idx->m = idx->m? idx->m<<1 : 16;
50 idx->name = (char**)realloc(idx->name, sizeof(void*) * idx->m);
52 idx->name[idx->n] = strdup(name);
53 k = kh_put(s, idx->hash, idx->name[idx->n], &ret);
54 t.len = len; t.line_len = line_len; t.line_blen = line_blen; t.offset = offset;
55 kh_value(idx->hash, k) = t;
59 faidx_t *fai_build_core(RAZF *rz)
62 int l_name, m_name, ret;
63 int len, line_len, line_blen, state;
68 idx = (faidx_t*)calloc(1, sizeof(faidx_t));
69 idx->hash = kh_init(s);
70 name = 0; l_name = m_name = 0;
71 len = line_len = line_blen = -1; state = 0; l1 = l2 = -1; offset = 0;
72 while (razf_read(rz, &c, 1)) {
73 if (c == '\n') { // an empty line
75 offset = razf_tell(rz);
77 } else if ((state == 0 && len < 0) || state == 2) continue;
79 if (c == '>') { // fasta header
81 fai_insert_index(idx, name, len, line_len, line_blen, offset);
83 while ((ret = razf_read(rz, &c, 1)) != 0 && !isspace(c)) {
84 if (m_name < l_name + 2) {
87 name = (char*)realloc(name, m_name);
93 fprintf(stderr, "[fai_build_core] the last entry has no sequence\n");
94 free(name); fai_destroy(idx);
97 if (c != '\n') while (razf_read(rz, &c, 1) && c != '\n');
99 offset = razf_tell(rz);
102 fprintf(stderr, "[fai_build_core] inlined empty line is not allowed in sequence '%s'.\n", name);
103 free(name); fai_destroy(idx);
106 if (state == 2) state = 3;
110 if (isgraph(c)) ++l2;
111 } while ((ret = razf_read(rz, &c, 1)) && c != '\n');
112 if (state == 3 && l2) {
113 fprintf(stderr, "[fai_build_core] different line length in sequence '%s'.\n", name);
114 free(name); fai_destroy(idx);
119 fprintf(stderr, "[fai_build_core] line length exceeds 65535 in sequence '%s'.\n", name);
120 free(name); fai_destroy(idx);
123 if (state == 1) line_len = l1, line_blen = l2, state = 0;
124 else if (state == 0) {
125 if (l1 != line_len || l2 != line_blen) state = 2;
129 fai_insert_index(idx, name, len, line_len, line_blen, offset);
134 void fai_save(const faidx_t *fai, FILE *fp)
138 for (i = 0; i < fai->n; ++i) {
140 k = kh_get(s, fai->hash, fai->name[i]);
141 x = kh_value(fai->hash, k);
143 fprintf(fp, "%s\t%d\t%ld\t%d\t%d\n", fai->name[i], (int)x.len, (long)x.offset, (int)x.line_blen, (int)x.line_len);
145 fprintf(fp, "%s\t%d\t%lld\t%d\t%d\n", fai->name[i], (int)x.len, (long long)x.offset, (int)x.line_blen, (int)x.line_len);
150 faidx_t *fai_read(FILE *fp)
154 int len, line_len, line_blen;
160 fai = (faidx_t*)calloc(1, sizeof(faidx_t));
161 fai->hash = kh_init(s);
162 buf = (char*)calloc(0x10000, 1);
163 while (!feof(fp) && fgets(buf, 0x10000, fp)) {
164 for (p = buf; *p && isgraph(*p); ++p);
167 sscanf(p, "%d%ld%d%d", &len, &offset, &line_blen, &line_len);
169 sscanf(p, "%d%lld%d%d", &len, &offset, &line_blen, &line_len);
171 fai_insert_index(fai, buf, len, line_len, line_blen, offset);
177 void fai_destroy(faidx_t *fai)
180 for (i = 0; i < fai->n; ++i) free(fai->name[i]);
182 kh_destroy(s, fai->hash);
183 if (fai->rz) razf_close(fai->rz);
187 int fai_build(const char *fn)
193 str = (char*)calloc(strlen(fn) + 5, 1);
194 sprintf(str, "%s.fai", fn);
195 rz = razf_open(fn, "r");
197 fprintf(stderr, "[fai_build] fail to open the FASTA file.\n");
201 fai = fai_build_core(rz);
203 fp = fopen(str, "wb");
205 fprintf(stderr, "[fai_build] fail to write FASTA index.\n");
206 fai_destroy(fai); free(str);
216 faidx_t *fai_load(const char *fn)
221 str = (char*)calloc(strlen(fn) + 5, 1);
222 sprintf(str, "%s.fai", fn);
223 fp = fopen(str, "rb");
225 fprintf(stderr, "[fai_load] build FASTA index.\n");
227 fp = fopen(str, "r");
229 fprintf(stderr, "[fai_load] fail to open FASTA index.\n");
236 fai->rz = razf_open(fn, "rb");
239 fprintf(stderr, "[fai_load] fail to open FASTA file.\n");
245 char *fai_fetch(const faidx_t *fai, const char *str, int *len)
257 p = s = (char*)malloc(l+1);
258 /* squeeze out "," */
259 for (i = k = 0; i != l; ++i)
260 if (str[i] != ',' && !isspace(str[i])) s[k++] = str[i];
262 for (i = 0; i != k; ++i) if (s[i] == ':') break;
264 iter = kh_get(s, h, s); /* get the ref_id */
265 if (iter == kh_end(h)) {
269 val = kh_value(h, iter);
270 if (i == k) { /* dump the whole sequence */
271 beg = 0; end = val.len;
273 for (p = s + i + 1; i != k; ++i) if (s[i] == '-') break;
278 } else end = val.len;
281 if (beg >= val.len) beg = val.len;
282 if (end >= val.len) end = val.len;
283 if (beg > end) beg = end;
286 // now retrieve the sequence
288 s = (char*)malloc(end - beg + 2);
289 razf_seek(fai->rz, val.offset + beg / val.line_blen * val.line_len + beg % val.line_blen, SEEK_SET);
290 while (razf_read(fai->rz, &c, 1) == 1 && l < end - beg)
291 if (isgraph(c)) s[l++] = c;
297 int faidx_main(int argc, char *argv[])
300 fprintf(stderr, "Usage: faidx <in.fasta> [<reg> [...]]\n");
303 if (argc == 2) fai_build(argv[1]);
308 fai = fai_load(argv[1]);
309 if (fai == 0) return 1;
310 for (i = 2; i != argc; ++i) {
311 printf(">%s\n", argv[i]);
312 s = fai_fetch(fai, argv[i], &l);
313 for (j = 0; j < l; j += 60) {
314 for (k = 0; k < 60 && k < l - j; ++k)
327 int main(int argc, char *argv[]) { return faidx_main(argc, argv); }