-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathkseq.h
209 lines (166 loc) · 5.49 KB
/
kseq.h
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* The MIT License
Copyright (c) 2008 Genome Research Ltd (GRL).
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/* Contact: Heng Li <[email protected]> */
/* Last Modified: 12APR2009 */
#ifndef KSEQ_H
#define KSEQ_H
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
typedef struct __kstring_t {
size_t l, m;
char *s;
} kstring_t;
typedef struct __kstream_t {
char *buf;
int begin, end, is_eof;
gzFile f;
} kstream_t;
static inline kstream_t *ks_init(gzFile f) {
kstream_t *ks = (kstream_t*)calloc(1, sizeof(kstream_t));
ks->f = f;
ks->buf = (char*)malloc(4096);
return ks;
}
static inline void ks_destroy(kstream_t *ks) {
if (ks) { free(ks->buf); free(ks); }
}
static inline int ks_getc(kstream_t *ks) {
if (ks->is_eof && ks->begin >= ks->end) return -1;
if (ks->begin >= ks->end) {
ks->begin = 0; ks->end = gzread(ks->f, ks->buf, 4096);
if (ks->end < 4096) ks->is_eof = 1;
if (ks->end == 0) return -1;
}
return (int)ks->buf[ks->begin++];
}
static int ks_getuntil(kstream_t *ks, int delimiter, kstring_t *str, int *dret)
{
if (dret)
*dret = 0;
str->l = 0;
if (ks->begin >= ks->end && ks->is_eof)
return -1;
for (;;)
{
int i;
if (ks->begin >= ks->end) {
if (!ks->is_eof) {
ks->begin = 0;
ks->end = gzread(ks->f, ks->buf, 4096);
if (ks->end < 4096)
ks->is_eof = 1;
if (ks->end == 0)
break;
}
else
break;
}
if (delimiter > 1) {
for (i = ks->begin; i < ks->end; ++i)
if (ks->buf[i] == delimiter)
break;
} else if (delimiter == 0) {
for (i = ks->begin; i < ks->end; ++i)
if (isspace(ks->buf[i]))
break;
} else if (delimiter == 1) {
for (i = ks->begin; i < ks->end; ++i)
if (isspace(ks->buf[i]) && ks->buf[i] != ' ')
break;
} else
i = 0;
if (str->m - str->l < i - ks->begin + 1) {
str->m = str->l + (i - ks->begin) + 1;
(--(str->m), (str->m)|=(str->m)>>1, (str->m)|=(str->m)>>2, (str->m)|=(str->m)>>4, (str->m)|=(str->m)>>8, (str->m)|=(str->m)>>16, ++(str->m));
str->s = (char*)realloc(str->s, str->m);
}
memcpy(str->s + str->l, ks->buf + ks->begin, i - ks->begin);
str->l = str->l + (i - ks->begin);
ks->begin = i + 1;
if (i < ks->end) {
if (dret)
*dret = ks->buf[i];
break;
}
}
if (str->l == 0) {
str->m = 1;
str->s = (char*)calloc(1, 1);
}
str->s[str->l] = '\0';
return str->l;
}
typedef struct
{ kstring_t name, comment, seq, qual; int last_char; kstream_t *f; }
kseq_t;
static inline kseq_t *kseq_init(gzFile fd) {
kseq_t *s = (kseq_t*)calloc(1, sizeof(kseq_t)); s->f = ks_init(fd); return s;
}
static inline void kseq_rewind(kseq_t *ks) {
ks->last_char = 0; ks->f->is_eof = ks->f->begin = ks->f->end = 0;
}
static inline void kseq_destroy(kseq_t *ks) {
if (!ks) return; free(ks->name.s); free(ks->comment.s); free(ks->seq.s); free(ks->qual.s); ks_destroy(ks->f); free(ks);
}
static int kseq_read(kseq_t *seq) {
int c; kstream_t *ks = seq->f;
if (seq->last_char == 0) {
while ((c = ks_getc(ks)) != -1 && c != '>' && c != '@');
if (c == -1)
return -1;
seq->last_char = c;
}
seq->comment.l = seq->seq.l = seq->qual.l = 0;
if (ks_getuntil(ks, 0, &seq->name, &c) < 0)
return -1;
if (c != '\n')
ks_getuntil(ks, '\n', &seq->comment, 0);
while ((c = ks_getc(ks)) != -1 && c != '>' && c != '+' && c != '@') {
if (isgraph(c)) {
if (seq->seq.l + 1 >= seq->seq.m) {
seq->seq.m = seq->seq.l + 2;
(--(seq->seq.m), (seq->seq.m)|=(seq->seq.m)>>1, (seq->seq.m)|=(seq->seq.m)>>2, (seq->seq.m)|=(seq->seq.m)>>4, (seq->seq.m)|=(seq->seq.m)>>8, (seq->seq.m)|=(seq->seq.m)>>16, ++(seq->seq.m));
seq->seq.s = (char*)realloc(seq->seq.s, seq->seq.m);
}
seq->seq.s[seq->seq.l++] = (char)c;
}
}
if (c == '>' || c == '@')
seq->last_char = c;
seq->seq.s[seq->seq.l] = 0;
if (c != '+') return seq->seq.l;
if (seq->qual.m < seq->seq.m) {
seq->qual.m = seq->seq.m;
seq->qual.s = (char*)realloc(seq->qual.s, seq->qual.m);
}
while ((c = ks_getc(ks)) != -1 && c != '\n');
if (c == -1)
return -2;
while ((c = ks_getc(ks)) != -1 && seq->qual.l < seq->seq.l)
if (c >= 33 && c <= 127)
seq->qual.s[seq->qual.l++] = (unsigned char)c;
seq->qual.s[seq->qual.l] = 0;
seq->last_char = 0;
if (seq->seq.l != seq->qual.l) return -2;
return seq->seq.l;
};
#endif