jpayne@69: /* The MIT License jpayne@69: jpayne@69: Copyright (c) 2008 Genome Research Ltd (GRL). jpayne@69: jpayne@69: Permission is hereby granted, free of charge, to any person obtaining jpayne@69: a copy of this software and associated documentation files (the jpayne@69: "Software"), to deal in the Software without restriction, including jpayne@69: without limitation the rights to use, copy, modify, merge, publish, jpayne@69: distribute, sublicense, and/or sell copies of the Software, and to jpayne@69: permit persons to whom the Software is furnished to do so, subject to jpayne@69: the following conditions: jpayne@69: jpayne@69: The above copyright notice and this permission notice shall be jpayne@69: included in all copies or substantial portions of the Software. jpayne@69: jpayne@69: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, jpayne@69: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF jpayne@69: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND jpayne@69: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS jpayne@69: BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN jpayne@69: ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN jpayne@69: CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE jpayne@69: SOFTWARE. jpayne@69: */ jpayne@69: jpayne@69: /* Contact: Heng Li */ jpayne@69: jpayne@69: /* Last Modified: 12APR2009 */ jpayne@69: jpayne@69: #ifndef AC_KSEQ_H jpayne@69: #define AC_KSEQ_H jpayne@69: jpayne@69: #include jpayne@69: #include jpayne@69: #include jpayne@69: jpayne@69: #define KS_SEP_SPACE 0 // isspace(): \t, \n, \v, \f, \r jpayne@69: #define KS_SEP_TAB 1 // isspace() && !' ' jpayne@69: #define KS_SEP_MAX 1 jpayne@69: jpayne@69: #define __KS_TYPE(type_t) \ jpayne@69: typedef struct __kstream_t { \ jpayne@69: char *buf; \ jpayne@69: int begin, end, is_eof; \ jpayne@69: type_t f; \ jpayne@69: } kstream_t; jpayne@69: jpayne@69: #define ks_eof(ks) ((ks)->is_eof && (ks)->begin >= (ks)->end) jpayne@69: #define ks_rewind(ks) ((ks)->is_eof = (ks)->begin = (ks)->end = 0) jpayne@69: jpayne@69: #define __KS_BASIC(type_t, __bufsize) \ jpayne@69: static inline kstream_t *ks_init(type_t f) \ jpayne@69: { \ jpayne@69: kstream_t *ks = (kstream_t*)calloc(1, sizeof(kstream_t)); \ jpayne@69: ks->f = f; \ jpayne@69: ks->buf = (char*)malloc(__bufsize); \ jpayne@69: return ks; \ jpayne@69: } \ jpayne@69: static inline void ks_destroy(kstream_t *ks) \ jpayne@69: { \ jpayne@69: if (ks) { \ jpayne@69: free(ks->buf); \ jpayne@69: free(ks); \ jpayne@69: } \ jpayne@69: } jpayne@69: jpayne@69: #define __KS_GETC(__read, __bufsize) \ jpayne@69: static inline int ks_getc(kstream_t *ks) \ jpayne@69: { \ jpayne@69: if (ks->is_eof && ks->begin >= ks->end) return -1; \ jpayne@69: if (ks->begin >= ks->end) { \ jpayne@69: ks->begin = 0; \ jpayne@69: ks->end = __read(ks->f, ks->buf, __bufsize); \ jpayne@69: if (ks->end < __bufsize) ks->is_eof = 1; \ jpayne@69: if (ks->end == 0) return -1; \ jpayne@69: } \ jpayne@69: return (int)ks->buf[ks->begin++]; \ jpayne@69: } jpayne@69: jpayne@69: #ifndef KSTRING_T jpayne@69: #define KSTRING_T kstring_t jpayne@69: typedef struct __kstring_t { jpayne@69: size_t l, m; jpayne@69: char *s; jpayne@69: } kstring_t; jpayne@69: #endif jpayne@69: jpayne@69: #ifndef kroundup32 jpayne@69: #define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x)) jpayne@69: #endif jpayne@69: jpayne@69: #define __KS_GETUNTIL(__read, __bufsize) \ jpayne@69: static int ks_getuntil(kstream_t *ks, int delimiter, kstring_t *str, int *dret) \ jpayne@69: { \ jpayne@69: if (dret) *dret = 0; \ jpayne@69: str->l = 0; \ jpayne@69: if (ks->begin >= ks->end && ks->is_eof) return -1; \ jpayne@69: for (;;) { \ jpayne@69: int i; \ jpayne@69: if (ks->begin >= ks->end) { \ jpayne@69: if (!ks->is_eof) { \ jpayne@69: ks->begin = 0; \ jpayne@69: ks->end = __read(ks->f, ks->buf, __bufsize); \ jpayne@69: if (ks->end < __bufsize) ks->is_eof = 1; \ jpayne@69: if (ks->end == 0) break; \ jpayne@69: } else break; \ jpayne@69: } \ jpayne@69: if (delimiter > KS_SEP_MAX) { \ jpayne@69: for (i = ks->begin; i < ks->end; ++i) \ jpayne@69: if (ks->buf[i] == delimiter) break; \ jpayne@69: } else if (delimiter == KS_SEP_SPACE) { \ jpayne@69: for (i = ks->begin; i < ks->end; ++i) \ jpayne@69: if (isspace(ks->buf[i])) break; \ jpayne@69: } else if (delimiter == KS_SEP_TAB) { \ jpayne@69: for (i = ks->begin; i < ks->end; ++i) \ jpayne@69: if (isspace(ks->buf[i]) && ks->buf[i] != ' ') break; \ jpayne@69: } else i = 0; /* never come to here! */ \ jpayne@69: if (str->m - str->l < i - ks->begin + 1) { \ jpayne@69: str->m = str->l + (i - ks->begin) + 1; \ jpayne@69: kroundup32(str->m); \ jpayne@69: str->s = (char*)realloc(str->s, str->m); \ jpayne@69: } \ jpayne@69: memcpy(str->s + str->l, ks->buf + ks->begin, i - ks->begin); \ jpayne@69: str->l = str->l + (i - ks->begin); \ jpayne@69: ks->begin = i + 1; \ jpayne@69: if (i < ks->end) { \ jpayne@69: if (dret) *dret = ks->buf[i]; \ jpayne@69: break; \ jpayne@69: } \ jpayne@69: } \ jpayne@69: if (str->l == 0) { \ jpayne@69: str->m = 1; \ jpayne@69: str->s = (char*)calloc(1, 1); \ jpayne@69: } \ jpayne@69: str->s[str->l] = '\0'; \ jpayne@69: return str->l; \ jpayne@69: } jpayne@69: jpayne@69: #define KSTREAM_INIT(type_t, __read, __bufsize) \ jpayne@69: __KS_TYPE(type_t) \ jpayne@69: __KS_BASIC(type_t, __bufsize) \ jpayne@69: __KS_GETC(__read, __bufsize) \ jpayne@69: __KS_GETUNTIL(__read, __bufsize) jpayne@69: jpayne@69: #define __KSEQ_BASIC(type_t) \ jpayne@69: static inline kseq_t *kseq_init(type_t fd) \ jpayne@69: { \ jpayne@69: kseq_t *s = (kseq_t*)calloc(1, sizeof(kseq_t)); \ jpayne@69: s->f = ks_init(fd); \ jpayne@69: return s; \ jpayne@69: } \ jpayne@69: static inline void kseq_rewind(kseq_t *ks) \ jpayne@69: { \ jpayne@69: ks->last_char = 0; \ jpayne@69: ks->f->is_eof = ks->f->begin = ks->f->end = 0; \ jpayne@69: } \ jpayne@69: static inline void kseq_destroy(kseq_t *ks) \ jpayne@69: { \ jpayne@69: if (!ks) return; \ jpayne@69: free(ks->name.s); free(ks->comment.s); free(ks->seq.s); free(ks->qual.s); \ jpayne@69: ks_destroy(ks->f); \ jpayne@69: free(ks); \ jpayne@69: } jpayne@69: jpayne@69: /* Return value: jpayne@69: >=0 length of the sequence (normal) jpayne@69: -1 end-of-file jpayne@69: -2 truncated quality string jpayne@69: */ jpayne@69: #define __KSEQ_READ \ jpayne@69: static int kseq_read(kseq_t *seq) \ jpayne@69: { \ jpayne@69: int c; \ jpayne@69: kstream_t *ks = seq->f; \ jpayne@69: if (seq->last_char == 0) { /* then jump to the next header line */ \ jpayne@69: while ((c = ks_getc(ks)) != -1 && c != '>' && c != '@'); \ jpayne@69: if (c == -1) return -1; /* end of file */ \ jpayne@69: seq->last_char = c; \ jpayne@69: } /* the first header char has been read */ \ jpayne@69: seq->comment.l = seq->seq.l = seq->qual.l = 0; \ jpayne@69: if (ks_getuntil(ks, 0, &seq->name, &c) < 0) return -1; \ jpayne@69: if (c != '\n') ks_getuntil(ks, '\n', &seq->comment, 0); \ jpayne@69: while ((c = ks_getc(ks)) != -1 && c != '>' && c != '+' && c != '@') { \ jpayne@69: if (isgraph(c)) { /* printable non-space character */ \ jpayne@69: if (seq->seq.l + 1 >= seq->seq.m) { /* double the memory */ \ jpayne@69: seq->seq.m = seq->seq.l + 2; \ jpayne@69: kroundup32(seq->seq.m); /* rounded to next closest 2^k */ \ jpayne@69: seq->seq.s = (char*)realloc(seq->seq.s, seq->seq.m); \ jpayne@69: } \ jpayne@69: seq->seq.s[seq->seq.l++] = (char)c; \ jpayne@69: } \ jpayne@69: } \ jpayne@69: if (c == '>' || c == '@') seq->last_char = c; /* the first header char has been read */ \ jpayne@69: seq->seq.s[seq->seq.l] = 0; /* null terminated string */ \ jpayne@69: if (c != '+') return seq->seq.l; /* FASTA */ \ jpayne@69: if (seq->qual.m < seq->seq.m) { /* allocate enough memory */ \ jpayne@69: seq->qual.m = seq->seq.m; \ jpayne@69: seq->qual.s = (char*)realloc(seq->qual.s, seq->qual.m); \ jpayne@69: } \ jpayne@69: while ((c = ks_getc(ks)) != -1 && c != '\n'); /* skip the rest of '+' line */ \ jpayne@69: if (c == -1) return -2; /* we should not stop here */ \ jpayne@69: while ((c = ks_getc(ks)) != -1 && seq->qual.l < seq->seq.l) \ jpayne@69: if (c >= 33 && c <= 127) seq->qual.s[seq->qual.l++] = (unsigned char)c; \ jpayne@69: seq->qual.s[seq->qual.l] = 0; /* null terminated string */ \ jpayne@69: seq->last_char = 0; /* we have not come to the next header line */ \ jpayne@69: if (seq->seq.l != seq->qual.l) return -2; /* qual string is shorter than seq string */ \ jpayne@69: return seq->seq.l; \ jpayne@69: } jpayne@69: jpayne@69: #define __KSEQ_TYPE(type_t) \ jpayne@69: typedef struct { \ jpayne@69: kstring_t name, comment, seq, qual; \ jpayne@69: int last_char; \ jpayne@69: kstream_t *f; \ jpayne@69: } kseq_t; jpayne@69: jpayne@69: #define KSEQ_INIT(type_t, __read) \ jpayne@69: KSTREAM_INIT(type_t, __read, 4096) \ jpayne@69: __KSEQ_TYPE(type_t) \ jpayne@69: __KSEQ_BASIC(type_t) \ jpayne@69: __KSEQ_READ jpayne@69: jpayne@69: #endif