seq_io 0.3.4

Fast FASTA and FASTQ readers
Documentation
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


macro_rules! impl_reader_methods {
    ($rdr:ty, $rset:ty, $record:ty, $error:ty) => {
        impl $rdr {
            /// Searches the next FASTQ record and returns a
            ///#[doc = $refrec_link]
            /// exactly like [`next()`](Reader::next), but this function does not
            /// compare the lengths of the sequence and quality scores. The check can
            /// (and should!) be done later using [`check_lengths()`](
            ///#[doc = $record_link]
            /// ).
            ///
            /// **Note**: not checking lengths this with multi-line FASTQ is a very bad
            /// idea, since the parser uses the sequence length as orientation when
            /// parsing the quality scores; quality lengths will never be smaller,
            /// but they can be larger than sequence lengths, and not doing a length
            /// check may lead to confusing errors later, or in the worst case
            /// "swallowing" of a record.
            // TODO: handle...
            #[inline]
            fn next_unchecked(&mut self) -> Option<error::Result<Self::Record<'_>>> {
                // if let Some(res) = self.next_inner() {
                //     res.map(|_| {
                //         let rec: RefRecord<F, P> = rdata.ref_record(buf, self.buf_reader.file_offset());
                //         rec

                //     })
                // }
                self.read_next().map(|res| {
                    res.map(move |_| {
                        self.record_data
                            .get_ref_record(self.buf_reader.buffer(), self.buf_reader.file_offset())
                    })
                })
            }

            /// Updates a
            ///#[doc = $rset_link]
            /// with new data.
            /// The contents of the internal buffer are just copied over to the record
            /// set and the positions of all records are found.
            /// Old data will be erased. Returns `None` if the input reached its end.
            /// Iterating over a record set will yield
            ///#[doc = $refrec_link]
            /// instances.
            ///
            /// # Example:
            ///
            /// ```
            /// use seq_io::prelude::*;
            ///#[doc = $import_rset]
            ///
            ///#[doc = $seq2]
            /// // Read records the normal way (for illustration, actually
            /// // we could also use the Reader::records iterator here)
            /// let mut reader = Reader::new(&seq[..]);
            /// let mut records = vec![];
            /// while let Some(res) = reader.next() {
            ///     records.push(res.unwrap().to_owned_record());
            /// }
            ///
            /// // Use read_record_set
            /// let mut reader = Reader::new(&seq[..]);
            /// let mut record_set = RecordSet::default(); // initialize once, reuse later
            /// let mut rset_records = vec![];
            /// while reader.read_record_set(&mut record_set).unwrap() {
            ///     for record in &record_set {
            ///         rset_records.push(record.to_owned_record());
            ///     }
            /// }
            ///
            /// // The two methods should give the same result
            /// assert_eq!(records, rset_records);
            /// ```
            #[inline]
            fn read_record_set(&mut self, record_set: &mut Self::RecordSet) -> error::Result<bool> {
                self.read_record_set_exact(record_set, None)
            }

            #[inline]
            fn read_record_set_exact(
                &mut self,
                record_set: &mut Self::RecordSet,
                n_records: Option<usize>,
            ) -> error::Result<bool> {
                self._read_record_set(
                    record_set, 
                    |r, d| r.set_next_pos(d), 
                    |r, b, d| {
                        r.set_data(b.buffer(), b.file_offset(), &d.opts, &d.opts_mut);
                    },
                    n_records
                )
            }

            #[inline]
            pub(crate) fn read_next(&mut self) -> Option<error::Result<()>> {
                //println!( "NEXT ========> f: {}, m: {}/{} check: {} {:?}", fasta, multiline_fasta, multiline_fastq, check_lengths, self.state);
        
                macro_rules! try_finish {
                    ($self:ident, $expr: expr) => {
                        match $expr {
                            Ok(item) => item,
                            Err(e) => {
                                $self.state = State::Finished;
                                return Some(Err(::std::convert::From::from(e)));
                            }
                        }
                    };
                }
        
                match self.state {
                    State::New => {
                        try_finish!(self, self.fill_buf());
                        self.state = State::Parsing;
                    }
                    State::Positioned => {
                        self.state = State::Parsing;
                    }
                    State::Finished => {
                        return None;
                    }
                    State::Parsing => {
                        // TODO: reset length_diff only with only with: guess_qual || multiline_fastq && !check_lengths
                        self.increment_record();
                    }
                };
        
                loop {
                    // loops until complete record is found (enlarging buffer / relocating contents if necessary)
                    match try_finish!(self, self.find_record()) {
                        SearchResult::Complete => {
                            return Some(Ok(()));
                        }
                        SearchResult::Incomplete => {
                            //println!("-> found result with {:?} check {} {:?}", search_pos, check_last_byte, self.pos);
                            // not at end -> adjust buffer and try again
                            try_finish!(self, self.adjust_buffer());
                            // -> start over searching in next iteration
                        }
                        SearchResult::End => {
                            return None;
                        }
                    }
                }
            }
        
            // add_fn is used instead of having set_next_pos in RecordSetT trait
            // to prevent the 'static lifetime bound for F and P
            // TODO: better way for this?
            #[inline]
            fn _read_record_set<Fa, Fd>(
                &mut self,
                record_set: &mut $rset,
                add_fn: Fa,
                set_data_fn: Fd,
                n_records: Option<usize>,
            ) -> error::Result<bool>
            where
                Fa: Fn(&mut S, &RecordData<F, P>),
                Fd: Fn(&mut S, &BufReader<R, B>, &RecordData<F, P>),
            {
                record_set.clear();
        
                macro_rules! try_finish {
                    ($self:ident, $expr: expr) => {
                        match $expr {
                            Ok(item) => item,
                            Err(e) => {
                                $self.state = State::Finished;
                                return Err(::std::convert::From::from(e));
                            }
                        }
                    };
                }
        
                let adjust_buffer = match self.state {
                    State::New => {
                        // Parsing not yet started, or a seek() to another position
                        // in the file was done, requiring the buffer to refilled.
                        try_finish!(self, self.fill_buf());
                        self.state = State::Positioned;
                        false
                    }
                    State::Finished => {
                        // End of in put reached, always return false
                        return Ok(false);
                    }
                    State::Parsing => {
                        // next() was previously called, the current record has
                        // already been returned -> go to next record and
                        // make sure it is moved to the start of the buffer if necessary.
                        self.increment_record();
                        self.state = State::Positioned;
                        true
                    }
                    State::Positioned => {
                        // The previous call to read_record_set() left the current record
                        // (the last in the buffer) incomplete.
                        // Or, a seek() to a position reachable from within the current
                        // buffer was done.
                        // In such a case, the current record needs to be moved to the
                        // start of the buffer, and the remaining part needs to be
                        // refilled.
                        true
                    }
                };
        
                // TODO: only make_room here
                // println!("new outer cap {} {:?} {:?} adj {}", self.buf_reader.capacity(), self.record_data, self.search_pos, adjust_buffer);
                if adjust_buffer {
                    // not at end -> adjust buffer and try again
                    try_finish!(self, self.make_room());
                    // println!("made room -> {}, off {}", self.buf_reader.capacity(), self.buf_reader.file_offset());
                }
            // println!(" => read recset at {:?} / buf {} l {} n recs {:?}", self.search_pos, self.buf_reader.file_offset(), self.buf_reader.buffer().len(), n_records);
        
                // search for records
                loop {
                    match try_finish!(self, self.find_record()) {
                        SearchResult::Complete => {
                            // complete record found
                            // record_set.set_next_pos(&self.record_data);
                            add_fn(record_set, &self.record_data);
                            //println!( "[RSET] complete: {:?}, lines {} + {}", self.pos, self.line_idx, self.pos.num_lines());
                            // initiate next record
                            self.increment_record();
                            // if exact number of records requested and number reached
                            // -> stop
                            if let Some(n) = n_records {
                                if record_set.len() == n {
                                    // // TODO: ok?
                                    // self.search_pos = Some(FieldPosition::new(
                                    //     SearchPos::HEAD,
                                    //     self.pos.record_start(),
                                    // ));
                                    break;
                                }
                            }
                        }
                        SearchResult::Incomplete => {
                            // If an incomplete record is found, we usually
                            // return the record set, unless it is still empty, or
                            // the number of requested records has not yet been matched.
                            // In such a case, we enlarge the buffer and try again.
                            let do_grow = if let Some(n) = n_records {
                                // TODO: maybe enlarge more conservatively?
                                record_set.len() < n
                            } else {
                                record_set.len() == 0
                            };
        
                            if do_grow {
                                // Enlarge the buffer and try again in next iteration
                                try_finish!(self, self.grow());
                                continue;
                            }
                            // record set completed -> return
                            break;
                        }
                        SearchResult::End => {
                            // searching finished
                            self.state = State::Finished;
                            break;
                        }
                    }
                };
        
                // println!("set buffer {:?}", self.buf_reader.buffer());
                // self.record_idx += record_set.len() as u64;
                // println!("finished rset {:?}, lines {}",record_set.len(), self.line_idx);
                set_data_fn(record_set, &self.buf_reader, &self.record_data);
                Ok(true)
            }
        
            // Invokes the call to `FormatParser::find_record` and transforms the results
            #[inline]
            fn _find_record(&mut self) -> Result<SearchResult, $error> {
                if search_buf.is_last() && search_pos.cursor == search_buf.data.len() {
                    // TODO: better way? checking lines.pos() >= search_buf.data.len()?
                    return Ok(SearchResult::End);
                }
                let mut at_end = false;  // TODO: can it work without?
                loop {
                    let mut buffer_adj = search_buf.data;
                    if (opts.multi_line_fastq() || !opts.single_line_fasta()) && !at_end {
                        // Ensure that there is always an additional byte at the end of the
                        // buffer, which is not searched (unless at end of input). This
                        // allows peeking one byte ahead if necessary.
                        buffer_adj = buffer_adj.split_last().unwrap().1;
                    }
                    // do the searching
                    let mut lines = search_pos.search(buffer_adj, b'\n');
                    // TODO: side effects of fn?
                    // - resets incomplete_pos
                    // - ?...
                    let res = self._find_record(variant, &mut lines, buf_pos, record_offset, search_pos, search_buf.data, opts);
                    // update the cursor position (also important for constructing errors)
                    search_pos.update(&lines);
                    // check the result and return or continue searching
                    // println!("found {:?} {} {:?} {:?} {:?}", res, false, buf_pos, search_pos, opts);
                    match res {
                        Ok(Complete) => {
                            return Ok(SearchResult::Complete);
                        },
                        // TODO: may be inefficient
                        Ok(Incomplete(ip)) => {
                            self.incomplete_pos = Some(ip);
                            if search_buf.is_last() {
                                // self.done = true;  // there will be max. one additional record
                                if (!opts.single_line_fasta() || opts.multi_line_fastq()) && !at_end {
                                    // In the case of multi-line formats:
                                    // The last byte was not yet checked; we need re-search
                                    // the last chunk including the last byte to check 
                                    // what is there
                                    at_end = true;
                                    search_pos.update(&lines);
                                    continue;
                                }
                                //  check if last record is present and valid
                                let has_last_record = check_end(
                                    buf_pos,
                                    search_pos,
                                    buffer_adj,
                                    ip,
                                    variant.is_fasta() == Some(true),
                                    opts,
                                )?;
                                // reset (later seek is possible)
                                self.incomplete_pos = None;
                                // println!("has last {} {:?}", has_last_record, buf_pos);
                                if has_last_record {
                                    // break,
                                    return Ok(SearchResult::Complete);
                                }
                                return Ok(SearchResult::End);
                            }
                            return Ok(SearchResult::Incomplete);
                        },
                        Err(e) => {
                            // reset the reader state in case of seek
                            self.incomplete_pos = None;
                            return Err(e);
                        }
                    }
        

                let buf = self.buf_reader.buffer();
            // println!("find buflen {} {:?} {:?}", buf.len(), self.record_data, self.search_pos);
            // println!("...buf from cursor {:?}", std::str::from_utf8(&self.buf_reader.buffer()[self.search_pos.cursor..]));
                let at_end = buf.is_empty() || (buf.len() < self.buf_reader.capacity() && self.search_pos.cursor >= buf.len());
            // println!("at end {}", at_end);
                let res = if !at_end {
                    self.find_record(
                        &mut self.record_data.pos.0,
                        &mut self.record_data.pos.1,
                        &mut self.search_pos,
                        &SearchBuffer {
                            data: buf,
                            capacity: self.buf_reader.capacity(),
                            file_offset: self.buf_reader.file_offset(),
                        },
                        &self.record_data.opts,
                        &mut self.record_data.opts_mut,
                    )
                } else {
                    Ok(SearchResult::End)
                };
                match res {
                    Ok(res) => {
                        if res == SearchResult::End {
                            self.state = State::Finished;
                        }
                        Ok(res)
                    }
                    Err(e) => {
                        self.state = State::Finished;
                        Err(e.into_parse_err::<P, F>(
                            buf,
                            self.buf_reader.file_offset(),
                            // &self.record_data,
                            self.record_data.pos(),
                            *self.record_data.record_offset(),
                            self.record_data.read_opts(),
                            self.record_data.mut_read_opts(),
                            Some(self.search_pos),
                            // &self.record_data.header,
                        ))
                    }
                }
            }
        
            // This function is only to be called if the buffer needs to be adjusted,
            // either by making room or by growing
            #[inline]
            fn adjust_buffer(&mut self) -> error::Result<()> {
                // TODO: dangerous, may not be correct, data_start may slow down
                let start = self
                    .record_data
                    .pos()
                    .start::<F::Parser>(self.search_pos, self.record_data.read_opts(), self.record_data.mut_read_opts());
                if start == 0 {
                    // TODO: larger threshold for efficiency?
                    // first record already incomplete -> buffer too small
                    self.grow()?;
                } else {
                    // not the first record -> buffer may be big enough
                    self.make_room()?;
                }
                Ok(())
            }
        
            #[inline]
            fn grow(&mut self) -> error::Result<()> {
                if let Some(limit) = self.buf_reader.grow_limited() {
                    return Err(error::Error::new(error::ErrorKind::BufferLimit(limit)));
                }
                self.fill_buf()?;
                //println!("grow {:?}", self.buf_reader.capacity());
                Ok(())
            }
        
            #[inline]
            fn make_room(&mut self) -> error::Result<()> {
                // move incomplete bytes to start of buffer and retry
                let offset = self
                    .record_data
                    .pos
                    .0
                    .start::<F::Parser>(self.search_pos, self.record_data.read_opts(), self.record_data.mut_read_opts());
                self.buf_reader.make_room(offset);
                // TODO: eventually use self.search_pos
                self.record_data.pos_mut().apply_offset(0 - offset as isize);
                self.search_pos.cursor -= offset;
                self.fill_buf()?;
            // println!("make room {:?} {:?}", self.buf_reader.buffer().len(), offset);
            // println!("new buf {:?}", std::str::from_utf8(self.buf_reader.buffer()));
                Ok(())
            }
        
            /// Fills the internal buffer (if not already full)
            /// Performance tests have shown that it is fastest to not inline this.
            /// There are several calls to this function within this module.
            #[inline(never)]
            fn fill_buf(&mut self) -> error::Result<usize> {
                self.buf_reader
                    .fill_buf()
                    .map_err(|e| error::Error::new(error::ErrorKind::Io(e)))
            }
        }
    };
}