interplane.Get Omega
Read in your language

klib

[Ported from github.com/attractivechaos/klib] added block arena

Brought by drakestapleton

branch master Branches Tags History

klib / kseq.h

242 lines · 9223 bytes Raw Blame History
1/* The MIT License
2
3 Copyright (c) 2008, 2009, 2011 Attractive Chaos <attractor@live.co.uk>
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#define KS_SEP_SPACE 0 // isspace(): \t, \n, \v, \f, \r
36#define KS_SEP_TAB 1 // isspace() && !' '
37#define KS_SEP_LINE 2 // line separator: "\n" (Unix) or "\r\n" (Windows)
38#define KS_SEP_MAX 2
39
40#define __KS_TYPE(type_t) \
41 typedef struct __kstream_t { \
42 unsigned char *buf; \
43 int begin, end, is_eof; \
44 type_t f; \
45 } kstream_t;
46
47#define ks_err(ks) ((ks)->end == -1)
48#define ks_eof(ks) ((ks)->is_eof && (ks)->begin >= (ks)->end)
49#define ks_rewind(ks) ((ks)->is_eof = (ks)->begin = (ks)->end = 0)
50
51#define __KS_BASIC(type_t, __bufsize) \
52 static inline kstream_t *ks_init(type_t f) \
53 { \
54 kstream_t *ks = (kstream_t*)calloc(1, sizeof(kstream_t)); \
55 ks->f = f; \
56 ks->buf = (unsigned char*)malloc(__bufsize); \
57 return ks; \
58 } \
59 static inline void ks_destroy(kstream_t *ks) \
60 { \
61 if (ks) { \
62 free(ks->buf); \
63 free(ks); \
64 } \
65 }
66
67#define __KS_GETC(__read, __bufsize) \
68 static inline int ks_getc(kstream_t *ks) \
69 { \
70 if (ks_err(ks)) return -3; \
71 if (ks->is_eof && ks->begin >= ks->end) return -1; \
72 if (ks->begin >= ks->end) { \
73 ks->begin = 0; \
74 ks->end = __read(ks->f, ks->buf, __bufsize); \
75 if (ks->end == 0) { ks->is_eof = 1; return -1;} \
76 if (ks->end == -1) { ks->is_eof = 1; return -3;}\
77 } \
78 return (int)ks->buf[ks->begin++]; \
79 }
80
81#ifndef KSTRING_T
82#define KSTRING_T kstring_t
83typedef struct __kstring_t {
84 size_t l, m;
85 char *s;
86} kstring_t;
87#endif
88
89#ifndef kroundup32
90#define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
91#endif
92
93#define __KS_GETUNTIL(__read, __bufsize) \
94 static int ks_getuntil2(kstream_t *ks, int delimiter, kstring_t *str, int *dret, int append) \
95 { \
96 int gotany = 0; \
97 if (dret) *dret = 0; \
98 str->l = append? str->l : 0; \
99 for (;;) { \
100 int i; \
101 if (ks_err(ks)) return -3; \
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 if (ks->end == -1) { ks->is_eof = 1; return -3; } \
108 } else break; \
109 } \
110 if (delimiter == KS_SEP_LINE) { \
111 unsigned char *sep = memchr(ks->buf + ks->begin, '\n', ks->end - ks->begin); \
112 i = sep != NULL ? sep - ks->buf : ks->end; \
113 } else if (delimiter > KS_SEP_MAX) { \
114 unsigned char *sep = memchr(ks->buf + ks->begin, delimiter, ks->end - ks->begin); \
115 i = sep != NULL ? sep - ks->buf : ks->end; \
116 } else if (delimiter == KS_SEP_SPACE) { \
117 for (i = ks->begin; i < ks->end; ++i) \
118 if (isspace(ks->buf[i])) break; \
119 } else if (delimiter == KS_SEP_TAB) { \
120 for (i = ks->begin; i < ks->end; ++i) \
121 if (isspace(ks->buf[i]) && ks->buf[i] != ' ') break; \
122 } else i = 0; /* never come to here! */ \
123 if (str->m - str->l < (size_t)(i - ks->begin + 1)) { \
124 str->m = str->l + (i - ks->begin) + 1; \
125 kroundup32(str->m); \
126 str->s = (char*)realloc(str->s, str->m); \
127 } \
128 gotany = 1; \
129 memcpy(str->s + str->l, ks->buf + ks->begin, i - ks->begin); \
130 str->l = str->l + (i - ks->begin); \
131 ks->begin = i + 1; \
132 if (i < ks->end) { \
133 if (dret) *dret = ks->buf[i]; \
134 break; \
135 } \
136 } \
137 if (!gotany && ks_eof(ks)) return -1; \
138 if (str->s == 0) { \
139 str->m = 1; \
140 str->s = (char*)calloc(1, 1); \
141 } else if (delimiter == KS_SEP_LINE && str->l > 1 && str->s[str->l-1] == '\r') --str->l; \
142 str->s[str->l] = '\0'; \
143 return str->l; \
144 } \
145 static inline int ks_getuntil(kstream_t *ks, int delimiter, kstring_t *str, int *dret) \
146 { return ks_getuntil2(ks, delimiter, str, dret, 0); }
147
148#define KSTREAM_INIT(type_t, __read, __bufsize) \
149 __KS_TYPE(type_t) \
150 __KS_BASIC(type_t, __bufsize) \
151 __KS_GETC(__read, __bufsize) \
152 __KS_GETUNTIL(__read, __bufsize)
153
154#define kseq_rewind(ks) ((ks)->last_char = (ks)->f->is_eof = (ks)->f->begin = (ks)->f->end = 0)
155
156#define __KSEQ_BASIC(SCOPE, type_t) \
157 SCOPE kseq_t *kseq_init(type_t fd) \
158 { \
159 kseq_t *s = (kseq_t*)calloc(1, sizeof(kseq_t)); \
160 s->f = ks_init(fd); \
161 return s; \
162 } \
163 SCOPE void kseq_destroy(kseq_t *ks) \
164 { \
165 if (!ks) return; \
166 free(ks->name.s); free(ks->comment.s); free(ks->seq.s); free(ks->qual.s); \
167 ks_destroy(ks->f); \
168 free(ks); \
169 }
170
171/* Return value:
172 >=0 length of the sequence (normal)
173 -1 end-of-file
174 -2 truncated quality string
175 -3 error reading stream
176 */
177#define __KSEQ_READ(SCOPE) \
178 SCOPE int kseq_read(kseq_t *seq) \
179 { \
180 int c,r; \
181 kstream_t *ks = seq->f; \
182 if (seq->last_char == 0) { /* then jump to the next header line */ \
183 while ((c = ks_getc(ks)) >= 0 && c != '>' && c != '@'); \
184 if (c < 0) return c; /* end of file or error*/ \
185 seq->last_char = c; \
186 } /* else: the first header char has been read in the previous call */ \
187 seq->comment.l = seq->seq.l = seq->qual.l = 0; /* reset all members */ \
188 if ((r=ks_getuntil(ks, 0, &seq->name, &c)) < 0) return r; /* normal exit: EOF or error */ \
189 if (c != '\n') ks_getuntil(ks, KS_SEP_LINE, &seq->comment, 0); /* read FASTA/Q comment */ \
190 if (seq->seq.s == 0) { /* we can do this in the loop below, but that is slower */ \
191 seq->seq.m = 256; \
192 seq->seq.s = (char*)malloc(seq->seq.m); \
193 } \
194 while ((c = ks_getc(ks)) >= 0 && c != '>' && c != '+' && c != '@') { \
195 if (c == '\n') continue; /* skip empty lines */ \
196 seq->seq.s[seq->seq.l++] = c; /* this is safe: we always have enough space for 1 char */ \
197 ks_getuntil2(ks, KS_SEP_LINE, &seq->seq, 0, 1); /* read the rest of the line */ \
198 } \
199 if (c == '>' || c == '@') seq->last_char = c; /* the first header char has been read */ \
200 if (seq->seq.l + 1 >= seq->seq.m) { /* seq->seq.s[seq->seq.l] below may be out of boundary */ \
201 seq->seq.m = seq->seq.l + 2; \
202 kroundup32(seq->seq.m); /* rounded to the next closest 2^k */ \
203 seq->seq.s = (char*)realloc(seq->seq.s, seq->seq.m); \
204 } \
205 seq->seq.s[seq->seq.l] = 0; /* null terminated string */ \
206 if (c != '+') return seq->seq.l; /* FASTA */ \
207 if (seq->qual.m < seq->seq.m) { /* allocate memory for qual in case insufficient */ \
208 seq->qual.m = seq->seq.m; \
209 seq->qual.s = (char*)realloc(seq->qual.s, seq->qual.m); \
210 } \
211 while ((c = ks_getc(ks)) >= 0 && c != '\n'); /* skip the rest of '+' line */ \
212 if (c == -1) return -2; /* error: no quality string */ \
213 while ((c = ks_getuntil2(ks, KS_SEP_LINE, &seq->qual, 0, 1) >= 0 && seq->qual.l < seq->seq.l)); \
214 if (c == -3) return -3; /* stream error */ \
215 seq->last_char = 0; /* we have not come to the next header line */ \
216 if (seq->seq.l != seq->qual.l) return -2; /* error: qual string is of a different length */ \
217 return seq->seq.l; \
218 }
219
220#define __KSEQ_TYPE(type_t) \
221 typedef struct { \
222 kstring_t name, comment, seq, qual; \
223 int last_char; \
224 kstream_t *f; \
225 } kseq_t;
226
227#define KSEQ_INIT2(SCOPE, type_t, __read) \
228 KSTREAM_INIT(type_t, __read, 16384) \
229 __KSEQ_TYPE(type_t) \
230 __KSEQ_BASIC(SCOPE, type_t) \
231 __KSEQ_READ(SCOPE)
232
233#define KSEQ_INIT(type_t, __read) KSEQ_INIT2(static, type_t, __read)
234
235#define KSEQ_DECLARE(type_t) \
236 __KS_TYPE(type_t) \
237 __KSEQ_TYPE(type_t) \
238 extern kseq_t *kseq_init(type_t fd); \
239 void kseq_destroy(kseq_t *ks); \
240 int kseq_read(kseq_t *seq);
241
242#endif