-
Notifications
You must be signed in to change notification settings - Fork 12
/
read_io.c
429 lines (371 loc) · 11 KB
/
read_io.c
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
/*
* read_io.c: Code for input and output of reads, e.g. from/to FASTQ files.
*/
/*
* Copyright (C) 2012 Tanja Magoc
* Copyright (C) 2012, 2013, 2014 Eric Biggers
*
* This file is part of FLASH, a fast tool to merge overlapping paired-end
* reads.
*
* FLASH is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version.
*
* FLASH is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with FLASH; if not, see http://www.gnu.org/licenses/.
*/
#include "iostream.h"
#include "read.h"
#include "read_io.h"
#include "util.h"
#include <assert.h>
#include <limits.h>
/******************************************
* Read input *
******************************************/
static bool
load_fastq_read(struct input_stream *in, struct read *r,
uint64_t *line_no_p)
{
ssize_t ret;
/* tag <newline>
* sequence <newline>
* + <newline>
* quality <newline> */
ret = input_stream_getline(in, &r->tag, &r->tag_bufsz);
if (ret <= 0)
return false;
if (ret > INT_MAX)
goto too_long;
++*line_no_p;
r->tag_len = ret;
ret = input_stream_getline(in, &r->seq, &r->seq_bufsz);
if (ret <= 0)
goto unexpected_eof;
if (ret > INT_MAX)
goto too_long;
++*line_no_p;
r->seq_len = ret;
ret = input_stream_getline(in, &r->qual, &r->qual_bufsz);
if (ret <= 0)
goto unexpected_eof;
if (r->qual[0] != '+')
goto expected_plus;
++*line_no_p;
ret = input_stream_getline(in, &r->qual, &r->qual_bufsz);
if (ret <= 0)
goto unexpected_eof;
if (ret > INT_MAX)
goto too_long;
++*line_no_p;
r->qual_len = ret;
return true;
unexpected_eof:
fatal_error("Unexpected EOF reading \"%s\" (line %"PRIu64")",
input_stream_get_name(in), *line_no_p);
expected_plus:
fatal_error("Expected '+' character in FASTQ separator in \"%s\" "
"(line %"PRIu64")", input_stream_get_name(in),
*line_no_p);
too_long:
fatal_error("Line %"PRIu64" in \"%s\" is too long",
*line_no_p, input_stream_get_name(in));
}
static bool
load_tab_delimited_read(struct input_stream *in, struct read *r,
uint64_t *line_no_p)
{
ssize_t ret;
const char *delims = "\t\n";
/* tag <tab> sequence <tab> quality <newline> */
ret = input_stream_getdelims(in, &r->tag, &r->tag_bufsz, delims);
if (ret <= 0)
return false;
if (ret > INT_MAX)
goto too_long;
if (r->tag[ret - 1] != '\t')
goto expected_tab;
r->tag_len = ret;
ret = input_stream_getdelims(in, &r->seq, &r->seq_bufsz, delims);
if (ret <= 0)
goto unexpected_eof;
if (ret > INT_MAX)
goto too_long;
if (r->seq[ret - 1] != '\t')
goto expected_tab;
r->seq_len = ret;
ret = input_stream_getdelims(in, &r->qual, &r->qual_bufsz, delims);
if (ret <= 0)
goto unexpected_eof;
if (ret > INT_MAX)
goto too_long;
if (r->qual[ret - 1] == '\t')
goto expected_newline;
r->qual_len = ret;
++*line_no_p;
return true;
too_long:
fatal_error("Field in line %"PRIu64" of \"%s\" is too long",
*line_no_p, input_stream_get_name(in));
unexpected_eof:
fatal_error("Unexpected EOF reading \"%s\" (line %"PRIu64")",
input_stream_get_name(in), *line_no_p);
expected_tab:
fatal_error("Invalid data in \"%s\": "
"expected tab character (line %"PRIu64")",
input_stream_get_name(in), *line_no_p);
expected_newline:
fatal_error("Invalid data in \"%s\": "
"expected newline character (line %"PRIu64")",
input_stream_get_name(in), *line_no_p);
}
static bool
load_tab_delimited_pair(struct input_stream *in,
struct read *r1, struct read *r2, uint64_t *line_no_p)
{
ssize_t ret;
const char *delims = "\t\n";
/* tag <tab> seq_1 <tab> qual_1 <tab> seq_2 <tab> qual_2 <newline> */
ret = input_stream_getdelims(in, &r1->tag, &r1->tag_bufsz, delims);
if (ret <= 0)
return false;
if (ret > INT_MAX)
goto too_long;
if (r1->tag[ret - 1] != '\t')
goto expected_tab;
r1->tag_len = ret;
ret = input_stream_getdelims(in, &r1->seq, &r1->seq_bufsz, delims);
if (ret <= 0)
goto unexpected_eof;
if (ret > INT_MAX)
goto too_long;
if (r1->seq[ret - 1] != '\t')
goto expected_tab;
r1->seq_len = ret;
ret = input_stream_getdelims(in, &r1->qual, &r1->qual_bufsz, delims);
if (ret <= 0)
goto unexpected_eof;
if (ret > INT_MAX)
goto too_long;
r1->qual_len = ret;
if (r1->qual[ret - 1] == '\n') {
/* Actually just a single read; use a void second read. */
r2->tag_len = 0;
r2->seq_len = 0;
r2->qual_len = 0;
++*line_no_p;
return true;
}
/* Set tag of read 2 to be the same as the tag of read 1 */
copy_tag(r2, r1);
ret = input_stream_getdelims(in, &r2->seq, &r2->seq_bufsz, delims);
if (ret <= 0)
goto unexpected_eof;
if (ret > INT_MAX)
goto too_long;
if (r2->seq[ret - 1] != '\t')
goto expected_tab;
r2->seq_len = ret;
ret = input_stream_getdelims(in, &r2->qual, &r2->qual_bufsz, delims);
if (ret <= 0)
goto unexpected_eof;
if (ret > INT_MAX)
goto too_long;
if (r2->qual[ret - 1] == '\t')
goto expected_newline;
r2->qual_len = ret;
++*line_no_p;
return true;
too_long:
fatal_error("Field in line %"PRIu64" of \"%s\" is too long",
*line_no_p, input_stream_get_name(in));
unexpected_eof:
fatal_error("Unexpected EOF reading \"%s\" (line %"PRIu64")",
input_stream_get_name(in), *line_no_p);
expected_tab:
fatal_error("Invalid data in \"%s\": "
"expected tab character (line %"PRIu64")",
input_stream_get_name(in), *line_no_p);
expected_newline:
fatal_error("Invalid data in \"%s\": "
"expected newline character (line %"PRIu64")",
input_stream_get_name(in), *line_no_p);
}
/*
* Loads the next read from the stream @in.
*
* @iparams specifies the format being used; e.g. FASTQ with a certain phred
* offset.
*
* In each resulting read, whitespace is stripped from the end of the sequence,
* tag, and quality scores. The sequence is translated into only the characters
* A, C, G, T, and N, and the quality values are re-scaled to start at 0.
*
* Returns true on success, false on end-of-file. Aborts on read error or if
* the data is invalid.
*/
bool
load_read(struct input_stream *in, const struct read_format_params *iparams,
struct read *r, uint64_t *line_no_p)
{
bool ret;
switch (iparams->fmt) {
case READ_FORMAT_FASTQ:
ret = load_fastq_read(in, r, line_no_p);
break;
case READ_FORMAT_TAB_DELIMITED:
ret = load_tab_delimited_read(in, r, line_no_p);
break;
default:
assert(0);
ret = false;
}
if (ret)
clean_read(r, iparams->phred_offset, in, *line_no_p);
return ret;
}
/*
* Similar to load_read(), but loads a pair of reads from the file instead.
* This is only relevant (and must only be called) for file formats that store
* both reads of the pair in the same sequential file.
*
* As a special case, this function may only fill in @r1, and set @r2->seq_len
* to 0, to indicate that the next record in the file was actually an unpaired
* read, not a read pair. This is possible in formats for which
* read_format_supports_mixed_reads() returns true (e.g. tab-delimited).
*/
bool
load_read_pair(struct input_stream *in, const struct read_format_params *iparams,
struct read *r1, struct read *r2,
uint64_t *line_no_p)
{
bool ret;
switch (iparams->fmt) {
case READ_FORMAT_FASTQ:
ret = load_fastq_read(in, r1, line_no_p);
if (ret && !load_fastq_read(in, r2, line_no_p))
fatal_error("Interleaved FASTQ file \"%s\" has an "
"odd number of reads",
input_stream_get_name(in));
break;
case READ_FORMAT_TAB_DELIMITED:
ret = load_tab_delimited_pair(in, r1, r2, line_no_p);
break;
default:
assert(0);
ret = false;
}
if (ret) {
clean_read(r1, iparams->phred_offset, in, *line_no_p);
clean_read(r2, iparams->phred_offset, in, *line_no_p);
}
return ret;
}
/******************************************
* Read output *
******************************************/
static void
write_fastq_read(struct output_stream *out, const struct read *r)
{
/* Add '@' to tag if missing */
if (r->tag_len == 0 || r->tag[0] != '@')
output_stream_fputc(out, '@');
output_stream_write(out, r->tag, r->tag_len);
output_stream_fputc(out, '\n');
output_stream_write(out, r->seq, r->seq_len);
output_stream_fputc(out, '\n');
output_stream_fputc(out, '+');
output_stream_fputc(out, '\n');
output_stream_write(out, r->qual, r->qual_len);
output_stream_fputc(out, '\n');
}
static void
write_tab_delimited_read(struct output_stream *out, const struct read *r)
{
const char *tag = r->tag;
int tag_len = r->tag_len;
/* Strip '@' from tag */
if (tag_len > 0 && tag[0] == '@')
tag++, tag_len--;
output_stream_write(out, tag, tag_len);
output_stream_fputc(out, '\t');
output_stream_write(out, r->seq, r->seq_len);
output_stream_fputc(out, '\t');
output_stream_write(out, r->qual, r->qual_len);
output_stream_fputc(out, '\n');
}
static void
write_tab_delimited_pair(struct output_stream *out,
const struct read *r1, const struct read *r2)
{
const char *tag = r1->tag;
int tag_len = r1->tag_len;
/* Strip '@' and /1 or /2 from tag */
if (tag_len > 0 && tag[0] == '@')
tag++, tag_len--;
if (tag_len >= 2 && tag[tag_len - 2] == '/' &&
(tag[tag_len - 1] == '1' || tag[tag_len - 1] == '2'))
tag_len -= 2;
output_stream_write(out, tag, tag_len);
output_stream_fputc(out, '\t');
output_stream_write(out, r1->seq, r1->seq_len);
output_stream_fputc(out, '\t');
output_stream_write(out, r1->qual, r1->qual_len);
output_stream_fputc(out, '\t');
output_stream_write(out, r2->seq, r2->seq_len);
output_stream_fputc(out, '\t');
output_stream_write(out, r2->qual, r2->qual_len);
output_stream_fputc(out, '\n');
}
/* Writes a read to the specified output stream in the format specified by
* @oparams.
*
* Modifies the qual string of @r! */
void
write_read(struct output_stream *out, const struct read_format_params *oparams,
struct read *r)
{
clean_read_for_write(r, oparams->phred_offset);
switch (oparams->fmt) {
case READ_FORMAT_FASTQ:
write_fastq_read(out, r);
break;
case READ_FORMAT_TAB_DELIMITED:
write_tab_delimited_read(out, r);
break;
default:
assert(0);
}
}
/* Writes a read pair to the specified output stream in the format specified by
* @oparams.
*
* Modifies the qual string of @r1 and @r2! */
void
write_read_pair(struct output_stream *out,
const struct read_format_params *oparams,
struct read *r1, struct read *r2)
{
clean_read_for_write(r1, oparams->phred_offset);
clean_read_for_write(r2, oparams->phred_offset);
switch (oparams->fmt) {
case READ_FORMAT_FASTQ:
/* Interleaved FASTQ format */
write_fastq_read(out, r1);
write_fastq_read(out, r2);
break;
case READ_FORMAT_TAB_DELIMITED:
/* Tab-delimited format, with two reads in a pair on one line */
write_tab_delimited_pair(out, r1, r2);
break;
default:
assert(0);
}
}