File: seq_iterator_fastq.c

package info (click to toggle)
genometools 1.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 57,988 kB
  • ctags: 45,574
  • sloc: ansic: 475,937; ruby: 24,092; python: 4,519; sh: 3,014; perl: 2,523; makefile: 1,839; java: 158; haskell: 37; xml: 6; sed: 5
file content (502 lines) | stat: -rw-r--r-- 16,224 bytes parent folder | download | duplicates (4)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*
  Copyright (c) 2009 Sascha Steinbiss <steinbiss@zbh.uni-hamburg.de>
  Copyright (c) 2009 Center for Bioinformatics, University of Hamburg

  Permission to use, copy, modify, and distribute this software for any
  purpose with or without fee is hereby granted, provided that the above
  copyright notice and this permission notice appear in all copies.

  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include "core/chardef.h"
#include "core/class_alloc_lock.h"
#include "core/colorspace.h"
#include "core/cstr_api.h"
#include "core/file.h"
#include "core/filelengthvalues.h"
#include "core/seq_iterator_fastq_api.h"
#include "core/seq_iterator_rep.h"
#include "core/str_array.h"
#include "core/unused_api.h"

#define GT_SEQIT_QUAL_INBUFSIZE  8192

struct GtSeqIteratorFastQ
{
  const GtSeqIterator parent_instance;
  unsigned int filenum;
  uint64_t linenum;
  GtFilelengthvalues *filelengthtab;
  bool complete,
       use_ungetchar,
       is_color_space,
       relax_qualdesc_check;
  GtStr *sequencebuffer,
        *descbuffer,
        *qualsbuffer;
  GtStr *qdescbuffer;
  GtFile *curfile;
  GtUword *chardisttab,
                currentfillpos,
                currentinpos,
                curline,
                reference_count;
  uint64_t lastspeciallength;
  GtUint64 maxread,
                     currentread;
  const GtStrArray *filenametab;
  unsigned char ungetchar,
                inbuf[GT_SEQIT_QUAL_INBUFSIZE];
  const GtUchar *symbolmap, **qualities;
};

#define gt_seq_iterator_fastq_cast(SI)\
        gt_seq_iterator_cast(gt_seq_iterator_fastq_class(), SI);

const GtSeqIteratorClass* gt_seq_iterator_fastq_class(void);

#define GT_FASTQ_BLOCK_START_CHAR      '@'
#define GT_FASTQ_QUAL_SEPARATOR_CHAR   '+'
#define GT_FASTQ_NEWLINESYMBOL         '\n'

static inline int fastq_buf_getchar(GtSeqIteratorFastQ *seqit)
{
  if (seqit->use_ungetchar) {
    seqit->use_ungetchar = false;
    return seqit->ungetchar;
  } else {
    if (seqit->currentinpos >= seqit->currentfillpos) {
      seqit->currentfillpos = gt_file_xread(seqit->curfile, seqit->inbuf,
                                             GT_SEQIT_QUAL_INBUFSIZE);
      if (seqit->currentfillpos == 0)
         return EOF;
      seqit->currentinpos = 0;
    }
    seqit->ungetchar = seqit->inbuf[seqit->currentinpos++];
    return seqit->ungetchar;
  }
}

static inline void fastq_buf_ungetchar(GtSeqIteratorFastQ *seqit)
{
  gt_assert(!seqit->use_ungetchar);
  seqit->use_ungetchar = true;
}

static inline int parse_fastq_seqname(GtSeqIteratorFastQ *seqit,
                                      GtStr *buffer,
                                      char startchar,
                                      GtError *err)
{
  char currentchar;
  bool firstsymbol = true;
  gt_error_check(err);
  gt_assert(seqit && buffer);
  gt_assert(gt_str_length(buffer) == 0);
  if ((currentchar = fastq_buf_getchar(seqit)) == EOF)
    return EOF;
  seqit->currentread++;
  if (currentchar != startchar) {
    gt_error_set(err, "'%c' expected, '%c' encountered instead in line "GT_WU"",
                      startchar,
                      currentchar,
                      seqit->curline);
    return -2;
  }
  while (currentchar != GT_FASTQ_NEWLINESYMBOL) {
    if (!firstsymbol)
      gt_str_append_char(buffer, currentchar);
    else
      firstsymbol = false;
    if ((currentchar = fastq_buf_getchar(seqit)) == EOF)
      return EOF;
    seqit->currentread++;
  }
  seqit->curline++;
  return 0;
}

static int parse_fastq_sequence(GtSeqIteratorFastQ *seqit,
                                GtError *err)
{
  int had_err = 0;
  char currentchar;
  GtStr *tmp_str = gt_str_new();

  gt_error_check(err);
  gt_assert(seqit);
  gt_assert(gt_str_length(seqit->sequencebuffer) == 0);
  /* read sequence */
  if ((currentchar = fastq_buf_getchar(seqit)) == EOF) {
    gt_str_delete(tmp_str);
    return EOF;
  }
  while (currentchar != GT_FASTQ_QUAL_SEPARATOR_CHAR) {
    if (currentchar != '\n' && currentchar != ' ') {
      gt_str_append_char(tmp_str, currentchar);
    } else if (currentchar == '\n') {
      seqit->curline++;
    }
    if ((currentchar = fastq_buf_getchar(seqit)) == EOF) {
      gt_str_delete(tmp_str);
      return EOF;
    }
    seqit->currentread++;
  }
  if (!gt_str_length(tmp_str)) {
    gt_error_set(err, "empty sequence given in file '%s', line "GT_WU"",
                      gt_str_array_get(seqit->filenametab,
                                       seqit->filenum),
                      seqit->curline-1);
    had_err = -2;
  }
  if (!had_err && seqit->is_color_space)
  {
    GtStr *translated = gt_str_new();
    had_err = gt_colorspace_decode_string(tmp_str,
                                          translated,
                                          err);
    gt_str_delete(tmp_str);
    tmp_str = translated;
  }
  if (!had_err)
  {
    if (seqit->symbolmap)
    {
      int charcode;
      char *input_str = gt_str_get(tmp_str);
      GtUword str_len = gt_str_length(tmp_str),
                    idx;
      for (idx = 0; !had_err && idx < str_len; idx++)
      {
        charcode = seqit->symbolmap[(unsigned int) input_str[idx]];
        if (charcode == UNDEFCHAR) {
          gt_error_set(err, "illegal character '%c': file \"%s\", line "GT_WU"",
                            input_str[idx],
                            gt_str_array_get(seqit->filenametab,
                                             seqit->filenum),
                            (GtUword) seqit->curline);
          had_err = -2;
        }
        if (ISSPECIAL(charcode)) {
          seqit->lastspeciallength++;
        } else {
          if (seqit->lastspeciallength > 0)
            seqit->lastspeciallength = 0;
          if (seqit->chardisttab)
            seqit->chardisttab[(int) charcode]++;
        }
        gt_str_append_char(seqit->sequencebuffer, charcode);
      }
    } else {
      gt_str_set(seqit->sequencebuffer, gt_str_get(tmp_str));
    }
  }
  fastq_buf_ungetchar(seqit);
  gt_str_delete(tmp_str);
  return had_err;
}

static inline int parse_fastq_qualities(GtSeqIteratorFastQ *seqit,
                                        GT_UNUSED GtError *err)
{
  char currentchar;
  GtUword i = 0;
  gt_assert(gt_str_length(seqit->sequencebuffer) > 0);
  if ((currentchar = fastq_buf_getchar(seqit)) == EOF)
    return EOF;
  seqit->currentread++;

  for (i=0;i<gt_str_length(seqit->sequencebuffer);i++) {
    if (currentchar != '\n' && currentchar != ' ') {
      gt_str_append_char(seqit->qualsbuffer, currentchar);
    } else if (currentchar == '\n') {
      seqit->curline++;
      i--;
    } else {
      i--;
    }
    if (i+1 == gt_str_length(seqit->sequencebuffer)) {
      seqit->curline++;
    }
    if ((currentchar = fastq_buf_getchar(seqit)) == EOF)
      return EOF;
    seqit->currentread++;
  }
  /* expect newline at end of qualities */
  if (currentchar != GT_FASTQ_NEWLINESYMBOL) {
    gt_error_set(err, "qualities string of sequence length " GT_WU
                 " is not ended by newline in file '%s', line "
                 GT_WU " -- this may be a sign for sequence and qualities "
                 "strings of different length",
                 gt_str_length(seqit->sequencebuffer),
                 gt_str_array_get(seqit->filenametab, seqit->filenum),
                 seqit->curline-1);
    return -2;
  }
  return 0;
}

#define gt_fastq_premature_end_check(had_err, seqit) \
  if (had_err == EOF) { \
    gt_error_set(err, "premature end of file '%s' in line "GT_WU": " \
                 "file ended before end of block", \
                 gt_str_array_get((seqit)->filenametab, \
                                  (seqit)->filenum), \
                 (GtUword) (seqit)->curline-1); \
    return -2; \
  }

static inline int parse_fastq_block(GtSeqIteratorFastQ *seqit, GtError *err)
{
  int had_err = 0;
  gt_assert(seqit);
  gt_error_check(err);

  /* parse @<seqname> */
  had_err = parse_fastq_seqname(seqit,
                                seqit->descbuffer,
                                GT_FASTQ_BLOCK_START_CHAR,
                                err);
  if (!had_err) {
    /* parse sequence */
    had_err = parse_fastq_sequence(seqit, err);
    gt_fastq_premature_end_check(had_err, seqit);
  }
  if (!had_err) {
    /* parse +[seqname] */
    had_err = parse_fastq_seqname(seqit,
                                  seqit->qdescbuffer,
                                  GT_FASTQ_QUAL_SEPARATOR_CHAR,
                                  err);
    gt_fastq_premature_end_check(had_err, seqit);
  }
  if (!had_err
      && !seqit->relax_qualdesc_check
      && gt_str_length(seqit->qdescbuffer)
      && gt_str_cmp(seqit->descbuffer, seqit->qdescbuffer) != 0)
  {
      gt_error_set(err, "sequence description '%s' is not equal to "
                        "qualities description '%s' in line "GT_WU"",
                        gt_str_get(seqit->descbuffer),
                        gt_str_get(seqit->qdescbuffer),
                        seqit->curline-1);
      return -2;
  }
  if (!had_err) {
    /* parse qualities */
    had_err = parse_fastq_qualities(seqit, err);
    if (gt_str_length(seqit->qualsbuffer)
          != gt_str_length(seqit->sequencebuffer))
    {
      gt_error_set(err, "lengths of character sequence and qualities "
                        "sequence differ ("GT_WU" <-> "GT_WU")",
                        gt_str_length(seqit->qualsbuffer),
                        gt_str_length(seqit->sequencebuffer));
      return -2;
    }
  }
  return had_err;
}

GtUword gt_seq_iterator_fastq_get_file_index(GtSeqIteratorFastQ *seqit)
{
  gt_assert(seqit);
  return seqit->filenum;
}

void gt_seq_iterator_fastq_set_quality_buffer(GtSeqIterator *si,
                                             const GtUchar **qualities)
{
  GtSeqIteratorFastQ *seqit;
  gt_assert(si);
  seqit = gt_seq_iterator_fastq_cast(si);
  seqit->qualities = qualities;
}

void gt_seq_iterator_fastq_set_symbolmap(GtSeqIterator *si,
                                        const GtUchar *symbolmap)
{
  GtSeqIteratorFastQ *seqit;
  gt_assert(si);
  seqit = gt_seq_iterator_fastq_cast(si);
  seqit->symbolmap = symbolmap;
}

void gt_seq_iterator_fastq_set_chardisttab(GtSeqIterator *si,
                                          GtUword *chardist)
{
  GtSeqIteratorFastQ *seqit;
  gt_assert(si && chardist);
  seqit = gt_seq_iterator_fastq_cast(si);
  seqit->chardisttab = chardist;
}

uint64_t gt_seq_iterator_fastq_get_lastspeciallength(const GtSeqIterator *si)
{
  GtSeqIteratorFastQ *seqit;
  gt_assert(si);
  seqit = gt_seq_iterator_fastq_cast((GtSeqIterator*) si);
  return seqit->lastspeciallength;
}

int gt_seq_iterator_fastq_next(GtSeqIterator *seqit,
                              const GtUchar **sequence,
                              GtUword *len,
                              char **desc,
                              GtError *err)
{
  int errstatus = 0;
  GtSeqIteratorFastQ *seqitf;
  gt_assert(seqit && len && desc);

  seqitf = gt_seq_iterator_fastq_cast(seqit);
  gt_str_reset(seqitf->qualsbuffer);
  gt_str_reset(seqitf->qdescbuffer);
  gt_str_reset(seqitf->sequencebuffer);
  gt_str_reset(seqitf->descbuffer);

  /* parse file */
  errstatus = parse_fastq_block(seqitf, err);

  if (!errstatus) {
    *sequence = (GtUchar*) gt_str_get(seqitf->sequencebuffer);
    *len = gt_str_length(seqitf->sequencebuffer);
    *desc = gt_str_get(seqitf->descbuffer);
    if (seqitf->qualities)
      *seqitf->qualities = (GtUchar*) gt_str_get(seqitf->qualsbuffer);
    errstatus = 1;
  } else {
    if (errstatus == EOF) {
      /* we could not get a next entry from this file */
      /* can we open another? */
      if (seqitf->filenum+1 < gt_str_array_size(seqitf->filenametab)) {
        const char *filename;
        filename = gt_str_array_get(seqitf->filenametab, ++seqitf->filenum);
        gt_file_delete(seqitf->curfile);
        seqitf->curfile = gt_file_xopen(filename, "r");
        seqitf->curline = 1;
        /* get first entry from next file*/
        errstatus = parse_fastq_block(seqitf, err);
        if (!errstatus) {
          *sequence = (GtUchar*) gt_str_get(seqitf->sequencebuffer);
          *len = gt_str_length(seqitf->sequencebuffer);
          *desc = gt_str_get(seqitf->descbuffer);
          if (seqitf->qualities)
            *seqitf->qualities = (GtUchar*) gt_str_get(seqitf->qualsbuffer);
          errstatus = 1;
        } else {
          errstatus = -1;
        }
      } else {
        /* all entries read from all files */
        errstatus = 0;
      }
    } else {
      errstatus = -1;
    }
  }
  return errstatus;
}

const GtUint64*
gt_seq_iterator_fastq_getcurrentcounter(GtSeqIterator *si,
                                       GtUint64 maxread)
{
  GtSeqIteratorFastQ *seqit;
  gt_assert(si);
  seqit = gt_seq_iterator_fastq_cast(si);
  seqit->maxread = maxread;
  return &seqit->currentread;
}

void gt_seq_iterator_fastq_delete(GtSeqIterator *si)
{
  GtSeqIteratorFastQ *seqit;
  if (!si) return;
  seqit = gt_seq_iterator_fastq_cast(si);
  gt_str_delete(seqit->qdescbuffer);
  gt_str_delete(seqit->sequencebuffer);
  gt_str_delete(seqit->qualsbuffer);
  gt_str_delete(seqit->descbuffer);
  if (seqit->curfile)
    gt_file_delete(seqit->curfile);
  seqit->currentread = seqit->maxread;
}

const GtSeqIteratorClass* gt_seq_iterator_fastq_class(void)
{
  static const GtSeqIteratorClass *sic = NULL;
  gt_class_alloc_lock_enter();
  if (!sic) {
    sic = gt_seq_iterator_class_new(sizeof (GtSeqIteratorFastQ),
                             gt_seq_iterator_fastq_set_symbolmap,
                             NULL,
                             gt_seq_iterator_fastq_next,
                             gt_seq_iterator_fastq_getcurrentcounter,
                             gt_seq_iterator_fastq_set_quality_buffer,
                             gt_seq_iterator_fastq_delete);
  }
  gt_class_alloc_lock_leave();
  return sic;
}

static GtSeqIterator* seqiterator_fastq_new_gen(const GtStrArray *filenametab,
                                                bool is_color_space,
                                                GT_UNUSED GtError *err)
{
  GtSeqIterator *seqit;
  GtSeqIteratorFastQ *seqitf;
  gt_assert(filenametab);
  seqit = gt_seq_iterator_create(gt_seq_iterator_fastq_class());
  seqitf = gt_seq_iterator_fastq_cast(seqit);
  seqitf->qdescbuffer = gt_str_new();
  seqitf->curfile = gt_file_xopen(gt_str_array_get(filenametab, 0), "r");
  seqitf->filenametab = filenametab;
  seqitf->curline = 1;
  seqitf->sequencebuffer = gt_str_new();
  seqitf->qualsbuffer = gt_str_new();
  seqitf->descbuffer = gt_str_new();
  seqitf->is_color_space = is_color_space;
  seqitf->relax_qualdesc_check = false;
  return seqit;
}

void gt_seq_iterator_fastq_relax_check_of_quality_description(
    GtSeqIteratorFastQ *seqitf)
{
  gt_assert(seqitf != NULL);
  seqitf->relax_qualdesc_check = true;
}

GtSeqIterator* gt_seq_iterator_fastq_new(const GtStrArray *filenametab,
                                     GtError *err)
{
  return seqiterator_fastq_new_gen(filenametab,
                                   false,
                                   err);
}

GtSeqIterator* gt_seq_iterator_fastq_new_colorspace(
                                                const GtStrArray *filenametab,
                                                GtError *err)
{
  return seqiterator_fastq_new_gen(filenametab,
                                   true,
                                   err);
}

/* wrapper for ABI compatibility */
GtSeqIterator* gt_seq_iterator_colorspace_fastq_new(
                                                const GtStrArray *filenametab,
                                                GtError *err)
{
  return gt_seq_iterator_fastq_new_colorspace(filenametab, err);
}