Debian Codegraph @

bwa-apache2/kseq.h

raw
1 /* The MIT License
2
3 Copyright (c) 2008, 2009, 2011 Attractive Chaos <[email protected]>
4
5 Permission is hereby granted, free of charge, to any person obtaining
6 a copy of this software and associated documentation files (the
7 "Software"), to deal in the Software without restriction, including
8 without limitation the rights to use, copy, modify, merge, publish,
9 distribute, sublicense, and/or sell copies of the Software, and to
10 permit persons to whom the Software is furnished to do so, subject to
11 the following conditions:
12
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 */
25
26 /* Last Modified: 05MAR2012 */
27
28 #ifndef AC_KSEQ_H
29 #define AC_KSEQ_H
30
31 #include <ctype.h>
32 #include <string.h>
33 #include <stdlib.h>
34
35 #ifdef USE_MALLOC_WRAPPERS
36 # include "malloc_wrap.h"
37 #endif
38
39 #define KS_SEP_SPACE 0 // isspace(): \t, \n, \v, \f, \r
40 #define KS_SEP_TAB 1 // isspace() && !' '
41 #define KS_SEP_LINE 2 // line separator: "\n" (Unix) or "\r\n" (Windows)
42 #define KS_SEP_MAX 2
43
44 #define __KS_TYPE(type_t) \
45 typedef struct __kstream_t { \
46 unsigned char *buf; \
47 int begin, end, is_eof; \
48 type_t f; \
49 } kstream_t;
50
51 #define ks_eof(ks) ((ks)->is_eof && (ks)->begin >= (ks)->end)
52 #define ks_rewind(ks) ((ks)->is_eof = (ks)->begin = (ks)->end = 0)
53
54 #define __KS_BASIC(type_t, __bufsize) \
55 static inline kstream_t *ks_init(type_t f) \
56 { \
57 kstream_t *ks = (kstream_t*)calloc(1, sizeof(kstream_t)); \
58 ks->f = f; \
59 ks->buf = (unsigned char*)malloc(__bufsize); \
60 return ks; \
61 } \
62 static inline void ks_destroy(kstream_t *ks) \
63 { \
64 if (ks) { \
65 free(ks->buf); \
66 free(ks); \
67 } \
68 }
69
70 #define __KS_GETC(__read, __bufsize) \
71 static inline int ks_getc(kstream_t *ks) \
72 { \
73 if (ks->is_eof && ks->begin >= ks->end) return -1; \
74 if (ks->begin >= ks->end) { \
75 ks->begin = 0; \
76 ks->end = __read(ks->f, ks->buf, __bufsize); \
77 if (ks->end == 0) { ks->is_eof = 1; return -1;} \
78 } \
79 return (int)ks->buf[ks->begin++]; \
80 }
81
82 #ifndef KSTRING_T
83 #define KSTRING_T kstring_t
84 typedef struct __kstring_t {
85 size_t l, m;
86 char *s;
87 } kstring_t;
88 #endif
89
90 #ifndef kroundup32
91 #define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
92 #endif
93
94 #define __KS_GETUNTIL(__read, __bufsize) \
95 static int ks_getuntil2(kstream_t *ks, int delimiter, kstring_t *str, int *dret, int append) \
96 { \
97 int gotany = 0; \
98 if (dret) *dret = 0; \
99 str->l = append? str->l : 0; \
100 for (;;) { \
101 int i; \
102 if (ks->begin >= ks->end) { \
103 if (!ks->is_eof) { \
104 ks->begin = 0; \
105 ks->end = __read(ks->f, ks->buf, __bufsize); \
106 if (ks->end == 0) { ks->is_eof = 1; break; } \
107 } else break; \
108 } \
109 if (delimiter == KS_SEP_LINE) { \
110 for (i = ks->begin; i < ks->end; ++i) \
111 if (ks->buf[i] == '\n') break; \
112 } else if (delimiter > KS_SEP_MAX) { \
113 for (i = ks->begin; i < ks->end; ++i) \
114 if (ks->buf[i] == delimiter) break; \
115 } else if (delimiter == KS_SEP_SPACE) { \
116 for (i = ks->begin; i < ks->end; ++i) \
117 if (isspace(ks->buf[i])) break; \
118 } else if (delimiter == KS_SEP_TAB) { \
119 for (i = ks->begin; i < ks->end; ++i) \
120 if (isspace(ks->buf[i]) && ks->buf[i] != ' ') break; \
121 } else i = 0; /* never come to here! */ \
122 if (str->m - str->l < (size_t)(i - ks->begin + 1)) { \
123 str->m = str->l + (i - ks->begin) + 1; \
124 kroundup32(str->m); \
125 str->s = (char*)realloc(str->s, str->m); \
126 } \
127 gotany = 1; \
128 memcpy(str->s + str->l, ks->buf + ks->begin, i - ks->begin); \
129 str->l = str->l + (i - ks->begin); \
130 ks->begin = i + 1; \
131 if (i < ks->end) { \
132 if (dret) *dret = ks->buf[i]; \
133 break; \
134 } \
135 } \
136 if (!gotany && ks_eof(ks)) return -1; \
137 if (str->s == 0) { \
138 str->m = 1; \
139 str->s = (char*)calloc(1, 1); \
140 } else if (delimiter == KS_SEP_LINE && str->l > 1 && str->s[str->l-1] == '\r') --str->l; \
141 str->s[str->l] = '\0'; \
142 return str->l; \
143 } \
144 static inline int ks_getuntil(kstream_t *ks, int delimiter, kstring_t *str, int *dret) \
145 { return ks_getuntil2(ks, delimiter, str, dret, 0); }
146
147 #define KSTREAM_INIT(type_t, __read, __bufsize) \
148 __KS_TYPE(type_t) \
149 __KS_BASIC(type_t, __bufsize) \
150 __KS_GETC(__read, __bufsize) \
151 __KS_GETUNTIL(__read, __bufsize)
152
153 #define kseq_rewind(ks) ((ks)->last_char = (ks)->f->is_eof = (ks)->f->begin = (ks)->f->end = 0)
154
155 #define __KSEQ_BASIC(SCOPE, type_t) \
156 SCOPE kseq_t *kseq_init(type_t fd) \
157 { \
158 kseq_t *s = (kseq_t*)calloc(1, sizeof(kseq_t)); \
159 s->f = ks_init(fd); \
160 return s; \
161 } \
162 SCOPE void kseq_destroy(kseq_t *ks) \
163 { \
164 if (!ks) return; \
165 free(ks->name.s); free(ks->comment.s); free(ks->seq.s); free(ks->qual.s); \
166 ks_destroy(ks->f); \
167 free(ks); \
168 }
169
170 /* Return value:
171 >=0 length of the sequence (normal)
172 -1 end-of-file
173 -2 truncated quality string
174 */
175 #define __KSEQ_READ(SCOPE) \
176 SCOPE int kseq_read(kseq_t *seq) \
177 { \
178 int c; \
179 kstream_t *ks = seq->f; \
180 if (seq->last_char == 0) { /* then jump to the next header line */ \
181 while ((c = ks_getc(ks)) != -1 && c != '>' && c != '@'); \
182 if (c == -1) return -1; /* end of file */ \
183 seq->last_char = c; \
184 } /* else: the first header char has been read in the previous call */ \
185 seq->comment.l = seq->seq.l = seq->qual.l = 0; /* reset all members */ \
186 if (ks_getuntil(ks, 0, &seq->name, &c) < 0) return -1; /* normal exit: EOF */ \
187 if (c != '\n') ks_getuntil(ks, KS_SEP_LINE, &seq->comment, 0); /* read FASTA/Q comment */ \
188 if (seq->seq.s == 0) { /* we can do this in the loop below, but that is slower */ \
189 seq->seq.m = 256; \
190 seq->seq.s = (char*)malloc(seq->seq.m); \
191 } \
192 while ((c = ks_getc(ks)) != -1 && c != '>' && c != '+' && c != '@') { \
193 if (c == '\n') continue; /* skip empty lines */ \
194 seq->seq.s[seq->seq.l++] = c; /* this is safe: we always have enough space for 1 char */ \
195 ks_getuntil2(ks, KS_SEP_LINE, &seq->seq, 0, 1); /* read the rest of the line */ \
196 } \
197 if (c == '>' || c == '@') seq->last_char = c; /* the first header char has been read */ \
198 if (seq->seq.l + 1 >= seq->seq.m) { /* seq->seq.s[seq->seq.l] below may be out of boundary */ \
199 seq->seq.m = seq->seq.l + 2; \
200 kroundup32(seq->seq.m); /* rounded to the next closest 2^k */ \
201 seq->seq.s = (char*)realloc(seq->seq.s, seq->seq.m); \
202 } \
203 seq->seq.s[seq->seq.l] = 0; /* null terminated string */ \
204 if (c != '+') return seq->seq.l; /* FASTA */ \
205 if (seq->qual.m < seq->seq.m) { /* allocate memory for qual in case insufficient */ \
206 seq->qual.m = seq->seq.m; \
207 seq->qual.s = (char*)realloc(seq->qual.s, seq->qual.m); \
208 } \
209 while ((c = ks_getc(ks)) != -1 && c != '\n'); /* skip the rest of '+' line */ \
210 if (c == -1) return -2; /* error: no quality string */ \
211 while (ks_getuntil2(ks, KS_SEP_LINE, &seq->qual, 0, 1) >= 0 && seq->qual.l < seq->seq.l); \
212 seq->last_char = 0; /* we have not come to the next header line */ \
213 if (seq->seq.l != seq->qual.l) return -2; /* error: qual string is of a different length */ \
214 return seq->seq.l; \
215 }
216
217 #define __KSEQ_TYPE(type_t) \
218 typedef struct { \
219 kstring_t name, comment, seq, qual; \
220 int last_char; \
221 kstream_t *f; \
222 } kseq_t;
223
224 #define KSEQ_INIT2(SCOPE, type_t, __read) \
225 KSTREAM_INIT(type_t, __read, 16384) \
226 __KSEQ_TYPE(type_t) \
227 __KSEQ_BASIC(SCOPE, type_t) \
228 __KSEQ_READ(SCOPE)
229
230 #define KSEQ_INIT(type_t, __read) KSEQ_INIT2(static, type_t, __read)
231
232 #define KSEQ_DECLARE(type_t) \
233 __KS_TYPE(type_t) \
234 __KSEQ_TYPE(type_t) \
235 extern kseq_t *kseq_init(type_t fd); \
236 void kseq_destroy(kseq_t *ks); \
237 int kseq_read(kseq_t *seq);
238
239 #endif