kseq.h 8.6 KB

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