Imported Debian patch 0.1.5c-1
[samtools.git] / faidx.c
1 #include <ctype.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include "faidx.h"
6 #include "khash.h"
7
8 typedef struct {
9         uint64_t len:32, line_len:16, line_blen:16;
10         uint64_t offset;
11 } faidx1_t;
12 KHASH_MAP_INIT_STR(s, faidx1_t)
13
14 #ifndef _NO_RAZF
15 #include "razf.h"
16 #else
17 extern off_t ftello(FILE *stream);
18 extern int fseeko(FILE *stream, off_t offset, int whence);
19 #define RAZF FILE
20 #define razf_read(fp, buf, size) fread(buf, 1, size, fp)
21 #define razf_open(fn, mode) fopen(fn, mode)
22 #define razf_close(fp) fclose(fp)
23 #define razf_seek(fp, offset, whence) fseeko(fp, offset, whence)
24 #define razf_tell(fp) ftello(fp)
25 #endif
26
27 struct __faidx_t {
28         RAZF *rz;
29         int n, m;
30         char **name;
31         khash_t(s) *hash;
32 };
33
34 #ifndef kroundup32
35 #define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
36 #endif
37
38 static inline void fai_insert_index(faidx_t *idx, const char *name, int len, int line_len, int line_blen, uint64_t offset)
39 {
40         khint_t k;
41         int ret;
42         faidx1_t t;
43         if (idx->n == idx->m) {
44                 idx->m = idx->m? idx->m<<1 : 16;
45                 idx->name = (char**)realloc(idx->name, sizeof(void*) * idx->m);
46         }
47         idx->name[idx->n] = strdup(name);
48         k = kh_put(s, idx->hash, idx->name[idx->n], &ret);
49         t.len = len; t.line_len = line_len; t.line_blen = line_blen; t.offset = offset;
50         kh_value(idx->hash, k) = t;
51         ++idx->n;
52 }
53
54 faidx_t *fai_build_core(RAZF *rz)
55 {
56         char c, *name;
57         int l_name, m_name, ret;
58         int len, line_len, line_blen, state;
59         int l1, l2;
60         faidx_t *idx;
61         uint64_t offset;
62
63         idx = (faidx_t*)calloc(1, sizeof(faidx_t));
64         idx->hash = kh_init(s);
65         name = 0; l_name = m_name = 0;
66         len = line_len = line_blen = -1; state = 0; l1 = l2 = -1; offset = 0;
67         while (razf_read(rz, &c, 1)) {
68                 if (c == '\n') { // an empty line
69                         if (state == 1) {
70                                 offset = razf_tell(rz);
71                                 continue;
72                         } else if ((state == 0 && len < 0) || state == 2) continue;
73                 }
74                 if (c == '>') { // fasta header
75                         if (len >= 0)
76                                 fai_insert_index(idx, name, len, line_len, line_blen, offset);
77                         l_name = 0;
78                         while ((ret = razf_read(rz, &c, 1)) != 0 && !isspace(c)) {
79                                 if (m_name < l_name + 2) {
80                                         m_name = l_name + 2;
81                                         kroundup32(m_name);
82                                         name = (char*)realloc(name, m_name);
83                                 }
84                                 name[l_name++] = c;
85                         }
86                         name[l_name] = '\0';
87                         if (ret == 0) {
88                                 fprintf(stderr, "[fai_build_core] the last entry has no sequence\n");
89                                 free(name); fai_destroy(idx);
90                                 return 0;
91                         }
92                         if (c != '\n') while (razf_read(rz, &c, 1) && c != '\n');
93                         state = 1; len = 0;
94                         offset = razf_tell(rz);
95                 } else {
96                         if (state == 3) {
97                                 fprintf(stderr, "[fai_build_core] inlined empty line is not allowed in sequence '%s'.\n", name);
98                                 free(name); fai_destroy(idx);
99                                 return 0;
100                         }
101                         if (state == 2) state = 3;
102                         l1 = l2 = 0;
103                         do {
104                                 ++l1;
105                                 if (isgraph(c)) ++l2;
106                         } while ((ret = razf_read(rz, &c, 1)) && c != '\n');
107                         if (state == 3 && l2) {
108                                 fprintf(stderr, "[fai_build_core] different line length in sequence '%s'.\n", name);
109                                 free(name); fai_destroy(idx);
110                                 return 0;
111                         }
112                         ++l1; len += l2;
113                         if (l2 >= 0x10000) {
114                                 fprintf(stderr, "[fai_build_core] line length exceeds 65535 in sequence '%s'.\n", name);
115                                 free(name); fai_destroy(idx);
116                                 return 0;
117                         }
118                         if (state == 1) line_len = l1, line_blen = l2, state = 0;
119                         else if (state == 0) {
120                                 if (l1 != line_len || l2 != line_blen) state = 2;
121                         }
122                 }
123         }
124         fai_insert_index(idx, name, len, line_len, line_blen, offset);
125         free(name);
126         return idx;
127 }
128
129 void fai_save(const faidx_t *fai, FILE *fp)
130 {
131         khint_t k;
132         int i;
133         for (i = 0; i < fai->n; ++i) {
134                 faidx1_t x;
135                 k = kh_get(s, fai->hash, fai->name[i]);
136                 x = kh_value(fai->hash, k);
137                 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);
138         }
139 }
140
141 faidx_t *fai_read(FILE *fp)
142 {
143         faidx_t *fai;
144         char *buf, *p;
145         int len, line_len, line_blen;
146         long long offset;
147         fai = (faidx_t*)calloc(1, sizeof(faidx_t));
148         fai->hash = kh_init(s);
149         buf = (char*)calloc(0x10000, 1);
150         while (!feof(fp) && fgets(buf, 0x10000, fp)) {
151                 for (p = buf; *p && isgraph(*p); ++p);
152                 *p = 0; ++p;
153                 sscanf(p, "%d%lld%d%d", &len, &offset, &line_blen, &line_len);
154                 fai_insert_index(fai, buf, len, line_len, line_blen, offset);
155         }
156         free(buf);
157         return fai;
158 }
159
160 void fai_destroy(faidx_t *fai)
161 {
162         int i;
163         for (i = 0; i < fai->n; ++i) free(fai->name[i]);
164         free(fai->name);
165         kh_destroy(s, fai->hash);
166         if (fai->rz) razf_close(fai->rz);
167         free(fai);
168 }
169
170 int fai_build(const char *fn)
171 {
172         char *str;
173         RAZF *rz;
174         FILE *fp;
175         faidx_t *fai;
176         str = (char*)calloc(strlen(fn) + 5, 1);
177         sprintf(str, "%s.fai", fn);
178         rz = razf_open(fn, "r");
179         if (rz == 0) {
180                 fprintf(stderr, "[fai_build] fail to open the FASTA file.\n");
181                 free(str);
182                 return -1;
183         }
184         fai = fai_build_core(rz);
185         razf_close(rz);
186         fp = fopen(str, "w");
187         if (fp == 0) {
188                 fprintf(stderr, "[fai_build] fail to write FASTA index.\n");
189                 fai_destroy(fai); free(str);
190                 return -1;
191         }
192         fai_save(fai, fp);
193         fclose(fp);
194         free(str);
195         fai_destroy(fai);
196         return 0;
197 }
198
199 faidx_t *fai_load(const char *fn)
200 {
201         char *str;
202         FILE *fp;
203         faidx_t *fai;
204         str = (char*)calloc(strlen(fn) + 5, 1);
205         sprintf(str, "%s.fai", fn);
206         fp = fopen(str, "r");
207         if (fp == 0) {
208                 fprintf(stderr, "[fai_load] build FASTA index.\n");
209                 fai_build(fn);
210                 fp = fopen(str, "r");
211                 if (fp == 0) {
212                         fprintf(stderr, "[fai_load] fail to open FASTA index.\n");
213                         free(str);
214                         return 0;
215                 }
216         }
217         fai = fai_read(fp);
218         fclose(fp);
219         fai->rz = razf_open(fn, "r");
220         free(str);
221         if (fai->rz == 0) {
222                 fprintf(stderr, "[fai_load] fail to open FASTA file.\n");
223                 return 0;
224         }
225         return fai;
226 }
227
228 char *fai_fetch(const faidx_t *fai, const char *str, int *len)
229 {
230         char *s, *p, c;
231         int i, l, k;
232         khiter_t iter;
233         faidx1_t val;
234         khash_t(s) *h;
235         int beg, end;
236
237         beg = end = -1;
238         h = fai->hash;
239         l = strlen(str);
240         p = s = (char*)malloc(l+1);
241         /* squeeze out "," */
242         for (i = k = 0; i != l; ++i)
243                 if (str[i] != ',' && !isspace(str[i])) s[k++] = str[i];
244         s[k] = 0;
245         for (i = 0; i != k; ++i) if (s[i] == ':') break;
246         s[i] = 0;
247         iter = kh_get(s, h, s); /* get the ref_id */
248         if (iter == kh_end(h)) {
249                 *len = 0;
250                 free(s); return 0;
251         }
252         val = kh_value(h, iter);
253         if (i == k) { /* dump the whole sequence */
254                 beg = 0; end = val.len;
255         } else {
256                 for (p = s + i + 1; i != k; ++i) if (s[i] == '-') break;
257                 beg = atoi(p);
258                 if (i < k) {
259                         p = s + i + 1;
260                         end = atoi(p);
261                 } else end = val.len;
262         }
263         if (beg > 0) --beg;
264         if (beg >= val.len) beg = val.len;
265         if (end >= val.len) end = val.len;
266         if (beg > end) beg = end;
267         free(s);
268
269         // now retrieve the sequence
270         l = 0;
271         s = (char*)malloc(end - beg + 2);
272         razf_seek(fai->rz, val.offset + beg / val.line_blen * val.line_len + beg % val.line_blen, SEEK_SET);
273         while (razf_read(fai->rz, &c, 1) == 1 && l < end - beg)
274                 if (isgraph(c)) s[l++] = c;
275         s[l] = '\0';
276         *len = l;
277         return s;
278 }
279
280 int faidx_main(int argc, char *argv[])
281 {
282         if (argc == 1) {
283                 fprintf(stderr, "Usage: faidx <in.fasta> [<reg> [...]]\n");
284                 return 1;
285         } else {
286                 if (argc == 2) fai_build(argv[1]);
287                 else {
288                         int i, j, k, l;
289                         char *s;
290                         faidx_t *fai;
291                         fai = fai_load(argv[1]);
292                         if (fai == 0) return 1;
293                         for (i = 2; i != argc; ++i) {
294                                 printf(">%s\n", argv[i]);
295                                 s = fai_fetch(fai, argv[i], &l);
296                                 for (j = 0; j < l; j += 60) {
297                                         for (k = 0; k < 60 && k < l - j; ++k)
298                                                 putchar(s[j + k]);
299                                         putchar('\n');
300                                 }
301                                 free(s);
302                         }
303                         fai_destroy(fai);
304                 }
305         }
306         return 0;
307 }
308
309 #ifdef FAIDX_MAIN
310 int main(int argc, char *argv[]) { return faidx_main(argc, argv); }
311 #endif