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
use std::borrow::Cow;
use std::io;
use std::str;

/// re-export of [`crate::core::PositionStore`](crate::core::PositionStore).
/// Keep in mind that the methods of `PositionStore` are only used internally.
pub use crate::core::PositionStore;

pub trait BaseRecord {
    /// Return the header line of the record as byte slice
    fn head(&self) -> &[u8];

    /// Return the record sequence as byte slice. With FASTA and multi-line
    /// FASTQ, this slice may contain line terminators if called on `RefRecord`.
    /// Use [`full_seq`](#tymethod.full_seq) or
    /// [`full_seq_given`](#tymethod.full_seq_given) instead in
    /// such cases, or iterate over lines with `fasta/q/x::RefRecord::seq_lines()`
    fn seq(&self) -> &[u8];

    /// Returns the full sequence as `Cow<[u8]>`.
    /// If the sequence consists of a single line, then the sequence will be
    /// borrowed from the underlying buffer (equivalent to calling `seq()`).
    /// If there are multiple lines, an owned copy will be created.
    fn full_seq(&self) -> Cow<[u8]>;

    /// Like `full_seq` returns the full sequence as `Cow<[u8]>`, with the
    /// difference that the vector that is used to store the contiguous
    /// sequence (in the case of multiple sequence lines) is provided in
    /// a closure, which is only executed if needed.
    /// This allows e.g. storing the necessary vectors in an arena allocator.
    fn full_seq_given<'s, F>(&'s self, owned_fn: F) -> Cow<'s, [u8]>
    where
        F: FnOnce() -> &'s mut Vec<u8>;

    /// Returns the number of sequence lines.
    /// Calling `record.seq_lines()` gives the same result as
    /// `record.seq_lines().len()`, but the latter may be slow depending on the
    /// type [`PositionStore`](crate::core::PositionStore) used since the line
    /// positions may have to be searched first.
    fn num_seq_lines(&self) -> usize;

    /// Returns a booleany specifying whether there is quality information in
    /// this record or not.
    fn has_quality(&self) -> bool;

    /// Returns the quality information or `None`.
    /// With multi-line FASTQ, this slice may contain line terminators if
    /// called on `RefRecord`.
    /// Use [`opt_full_qual`](#tymethod.opt_full_qual) or
    /// [`opt_full_qual_given`](#tymethod.opt_full_qual_given) instead in
    /// such cases, or iterate over lines with `fasta/q/x::RefRecord::[opt_]qual_lines()`
    fn opt_qual(&self) -> Option<&[u8]>;

    /// Returns the quality scores as contiguous `Cow<[u8]>`, if present. If not
    /// dealing with multi-line FASTQ, this does not involve any copying
    /// (same as if calling `opt_qual()`).
    /// If there are multiple lines, an owned copy will be created.
    fn opt_full_qual(&self) -> Option<Cow<[u8]>>;

    /// Like `opt_full_qual` returns the full quality scores as `Cow<[u8]>`,
    /// with the difference that the vector that is used to store the contiguous
    /// data (in the case of multiple quality lines) is provided in
    /// a closure, which is only executed if needed.
    fn opt_full_qual_given<'s, F>(&'s self, owned_fn: F) -> Option<Cow<'s, [u8]>>
    where
        F: FnOnce() -> &'s mut Vec<u8>;

    /// Returns the number of quality lines. Always > 0 with the current parsers.
    fn num_qual_lines(&self) -> usize;

    /// Writes the record to an output.
    ///
    /// The default for FASTA is to write one sequence line. Otherwise refer to
    /// the method [`fasta::Record::write_wrap`](crate::fasta::Record::write_wrap).
    fn write<W>(&self, writer: W) -> io::Result<()>
    where
        W: io::Write;

    /// Returns the record ID (everything before an optional space) as byte slice.
    #[inline]
    fn id_bytes(&self) -> &[u8] {
        self.head().split(|b| *b == b' ').next().unwrap()
    }

    /// Returns the record ID (everything before an optional space) as `&str`.
    #[inline]
    fn id(&self) -> Result<&str, std::str::Utf8Error> {
        str::from_utf8(self.id_bytes())
    }

    /// Returns the record description (separated from the ID by a space)
    /// as byte slice, if present.
    #[inline]
    fn desc_bytes(&self) -> Option<&[u8]> {
        self.head().splitn(2, |b| *b == b' ').nth(1)
    }

    /// Returns the record description (separated from the ID by a space)
    /// as `&str` slice, if present.
    #[inline]
    fn desc(&self) -> Option<Result<&str, std::str::Utf8Error>> {
        self.desc_bytes().map(str::from_utf8)
    }

    /// Return both the ID and the description of the record (if present)
    /// This should be faster than calling `id()` and `desc()` separately.
    #[inline]
    fn id_desc_bytes(&self) -> (&[u8], Option<&[u8]>) {
        let mut h = self.head().splitn(2, |c| *c == b' ');
        (h.next().unwrap(), h.next())
    }

    /// Returns both the ID and the description of the record (if present)
    /// as `&str` slices.
    /// This should be faster than calling `id()` and `desc()` separately.
    #[inline]
    fn id_desc(&self) -> Result<(&str, Option<&str>), std::str::Utf8Error> {
        let mut h = str::from_utf8(self.head())?.splitn(2, ' ');
        Ok((h.next().unwrap(), h.next()))
    }
}

impl<'a, R> BaseRecord for &'a R
where
    R: BaseRecord,
{
    fn head(&self) -> &[u8] {
        (**self).head()
    }

    fn seq(&self) -> &[u8] {
        (**self).seq()
    }

    fn full_seq(&self) -> Cow<[u8]> {
        (**self).full_seq()
    }

    fn full_seq_given<'s, F>(&'s self, owned_fn: F) -> Cow<'s, [u8]>
    where
        F: FnOnce() -> &'s mut Vec<u8>,
    {
        (**self).full_seq_given(owned_fn)
    }

    fn num_seq_lines(&self) -> usize {
        (**self).num_seq_lines()
    }

    fn id_bytes(&self) -> &[u8] {
        (**self).id_bytes()
    }

    fn id(&self) -> Result<&str, std::str::Utf8Error> {
        (**self).id()
    }

    fn desc_bytes(&self) -> Option<&[u8]> {
        (**self).desc_bytes()
    }

    fn desc(&self) -> Option<Result<&str, std::str::Utf8Error>> {
        (**self).desc()
    }

    fn id_desc_bytes(&self) -> (&[u8], Option<&[u8]>) {
        (**self).id_desc_bytes()
    }

    fn id_desc(&self) -> Result<(&str, Option<&str>), std::str::Utf8Error> {
        (**self).id_desc()
    }

    fn has_quality(&self) -> bool {
        (**self).has_quality()
    }

    fn opt_qual(&self) -> Option<&[u8]> {
        (**self).opt_qual()
    }

    fn opt_full_qual(&self) -> Option<Cow<[u8]>> {
        (**self).opt_full_qual()
    }

    fn opt_full_qual_given<'s, F: FnOnce() -> &'s mut Vec<u8>>(
        &'s self,
        owned_fn: F,
    ) -> Option<Cow<'s, [u8]>> {
        (**self).opt_full_qual_given(owned_fn)
    }

    fn num_qual_lines(&self) -> usize {
        (**self).num_qual_lines()
    }

    fn write<W: io::Write>(&self, writer: W) -> io::Result<()> {
        (**self).write(writer)
    }
}