pub struct Sequence {
pub id: String,
pub description: Option<String>,
pub seq: Vec<u8>,
pub quality: Option<Vec<u8>>,
}Expand description
One FASTA or FASTQ record.
A record with quality == None is a FASTA record; a record with quality is a
FASTQ record whose quality string always has the same length as seq.
Fields are public so that pipelines can build records without ceremony, but
the constructors and Sequence::validate exist to keep the length
invariant intact.
use fastx::Sequence;
let read = Sequence::fastq("read1", b"ACGT", b"IIII")?;
assert_eq!(read.len(), 4);
assert_eq!(read.mean_quality(), Some(40.0));Fields§
§id: StringIdentifier: the header up to the first whitespace, without >/@.
description: Option<String>Everything after the first whitespace in the header, if any.
seq: Vec<u8>Residues as ASCII bytes, without line breaks.
quality: Option<Vec<u8>>Phred quality characters (not scores), FASTQ only.
Implementations§
Source§impl Sequence
impl Sequence
Sourcepub fn fastq<I: Into<String>>(
id: I,
seq: impl Into<Vec<u8>>,
quality: impl Into<Vec<u8>>,
) -> Result<Sequence>
pub fn fastq<I: Into<String>>( id: I, seq: impl Into<Vec<u8>>, quality: impl Into<Vec<u8>>, ) -> Result<Sequence>
A FASTQ record. Fails if sequence and quality lengths differ.
Sourcepub fn with_description<D: Into<String>>(self, description: D) -> Sequence
pub fn with_description<D: Into<String>>(self, description: D) -> Sequence
Builder-style setter for the description.
Sourcepub fn has_quality(&self) -> bool
pub fn has_quality(&self) -> bool
True when the record carries quality scores.
Sourcepub fn header(&self) -> String
pub fn header(&self) -> String
The header line without its leading >/@.
let s = Sequence::fasta("chr1", b"ACGT".to_vec()).with_description("human chromosome 1");
assert_eq!(s.header(), "chr1 human chromosome 1");Sourcepub fn seq_str(&self) -> Result<&str>
pub fn seq_str(&self) -> Result<&str>
The residues as &str, if they are valid UTF-8 (ASCII in practice).
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Reset the record to an empty state while keeping its allocations.
This is what makes crate::FastxReader::read_into allocation-free.
Sourcepub fn base_counts(&self) -> BaseCounts
pub fn base_counts(&self) -> BaseCounts
Per-base counts.
Sourcepub fn gc_content(&self) -> Option<f64>
pub fn gc_content(&self) -> Option<f64>
GC fraction over unambiguous bases, None when there are none.
Sourcepub fn ambiguous_count(&self) -> u64
pub fn ambiguous_count(&self) -> u64
Number of N/ambiguity bases.
Sourcepub fn reverse_complement(&self) -> Sequence
pub fn reverse_complement(&self) -> Sequence
Reverse complement, reversing the quality string as well.
let read = Sequence::fastq("r", b"ACGT", b"ABCD")?.reverse_complement();
assert_eq!(read.seq, b"ACGT");
assert_eq!(read.quality.unwrap(), b"DCBA");Sourcepub fn reverse_complement_in_place(&mut self)
pub fn reverse_complement_in_place(&mut self)
Reverse complement in place.
Sourcepub fn make_uppercase(&mut self)
pub fn make_uppercase(&mut self)
Uppercase the residues in place (undoes soft masking).
Sourcepub fn subseq(&self, range: Range<usize>) -> Result<Sequence>
pub fn subseq(&self, range: Range<usize>) -> Result<Sequence>
A sub-record covering range, carrying the matching quality slice.
Returns Error::OutOfBounds if the range does not fit.
Sourcepub fn trim_to(&mut self, range: Range<usize>) -> Result<()>
pub fn trim_to(&mut self, range: Range<usize>) -> Result<()>
Keep only range, discarding the rest, in place.
Sourcepub fn translate(&self, frame: usize, stop_at_stop: bool) -> Sequence
pub fn translate(&self, frame: usize, stop_at_stop: bool) -> Sequence
Translate the residues into protein using the standard genetic code.
Sourcepub fn quality_scores(&self) -> Option<Vec<u8>>
pub fn quality_scores(&self) -> Option<Vec<u8>>
Decoded Phred scores assuming the Phred+33 offset.
Sourcepub fn quality_scores_with(&self, offset: u8) -> Option<Vec<u8>>
pub fn quality_scores_with(&self, offset: u8) -> Option<Vec<u8>>
Decoded Phred scores for an explicit offset.
Sourcepub fn mean_quality(&self) -> Option<f64>
pub fn mean_quality(&self) -> Option<f64>
Error-probability-weighted mean quality (Phred+33).
Sourcepub fn expected_errors(&self) -> Option<f64>
pub fn expected_errors(&self) -> Option<f64>
Expected number of sequencing errors in the read (Phred+33).
Sourcepub fn convert_quality_offset(&mut self, from: u8, to: u8)
pub fn convert_quality_offset(&mut self, from: u8, to: u8)
Convert Phred+64 quality to Phred+33 in place. No-op for FASTA records.
Sourcepub fn into_fasta(self) -> Sequence
pub fn into_fasta(self) -> Sequence
Drop the quality string, turning a FASTQ record into a FASTA record.
Sourcepub fn validate(&self, alphabet: Alphabet) -> Result<()>
pub fn validate(&self, alphabet: Alphabet) -> Result<()>
Check the invariants that the writers rely on.
- the id is non-empty and free of whitespace/newlines
- the description contains no newlines
- residues are printable and non-whitespace, and in
alphabet - quality, when present, has the same length as the sequence and is printable ASCII
Sourcepub fn write_fasta<W: Write>(
&self,
out: &mut W,
line_width: Option<usize>,
) -> Result<()>
pub fn write_fasta<W: Write>( &self, out: &mut W, line_width: Option<usize>, ) -> Result<()>
Write this record as FASTA, wrapping sequence lines at line_width
(None writes the sequence on a single line).
Fails with Error::InvalidByte if the sequence contains a byte FASTA
cannot represent: \n, \r or >. None of the three survives a
write/read cycle once line wrapping can move it, so writing one would
silently corrupt the record.
Sourcepub fn write_fastq<W: Write>(&self, out: &mut W) -> Result<()>
pub fn write_fastq<W: Write>(&self, out: &mut W) -> Result<()>
Write this record as FASTQ. Fails when the record has no quality, when the lengths disagree, or when a byte cannot be represented.
Sourcepub fn to_string_in(&self, format: Format) -> Result<String>
pub fn to_string_in(&self, format: Format) -> Result<String>
Render the record in its native format as a String.
Trait Implementations§
impl Eq for Sequence
Source§impl PartialEq<Sequence> for SequenceRef<'_>
impl PartialEq<Sequence> for SequenceRef<'_>
impl StructuralPartialEq for Sequence
Auto Trait Implementations§
impl Freeze for Sequence
impl RefUnwindSafe for Sequence
impl Send for Sequence
impl Sync for Sequence
impl Unpin for Sequence
impl UnsafeUnpin for Sequence
impl UnwindSafe for Sequence
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more