annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/include/mash/kseq.h @ 69:33d812a61356

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