Skip to main content

Sequence

Struct Sequence 

Source
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: String

Identifier: 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

Source

pub fn fasta<I: Into<String>>(id: I, seq: impl Into<Vec<u8>>) -> Sequence

A FASTA record.

Source

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.

Source

pub fn with_description<D: Into<String>>(self, description: D) -> Sequence

Builder-style setter for the description.

Source

pub fn len(&self) -> usize

Number of residues.

Source

pub fn is_empty(&self) -> bool

True when the record has no residues.

Source

pub fn has_quality(&self) -> bool

True when the record carries quality scores.

Source

pub fn format(&self) -> Format

The format this record can be written as losslessly.

Source

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");
Source

pub fn seq_str(&self) -> Result<&str>

The residues as &str, if they are valid UTF-8 (ASCII in practice).

Source

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.

Source

pub fn base_counts(&self) -> BaseCounts

Per-base counts.

Source

pub fn gc_content(&self) -> Option<f64>

GC fraction over unambiguous bases, None when there are none.

Source

pub fn ambiguous_count(&self) -> u64

Number of N/ambiguity bases.

Source

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");
Source

pub fn reverse_complement_in_place(&mut self)

Reverse complement in place.

Source

pub fn make_uppercase(&mut self)

Uppercase the residues in place (undoes soft masking).

Source

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.

Source

pub fn trim_to(&mut self, range: Range<usize>) -> Result<()>

Keep only range, discarding the rest, in place.

Source

pub fn translate(&self, frame: usize, stop_at_stop: bool) -> Sequence

Translate the residues into protein using the standard genetic code.

Source

pub fn kmers(&self, k: usize) -> impl Iterator<Item = &[u8]>

Iterator over overlapping k-mers.

Source

pub fn quality_scores(&self) -> Option<Vec<u8>>

Decoded Phred scores assuming the Phred+33 offset.

Source

pub fn quality_scores_with(&self, offset: u8) -> Option<Vec<u8>>

Decoded Phred scores for an explicit offset.

Source

pub fn mean_quality(&self) -> Option<f64>

Error-probability-weighted mean quality (Phred+33).

Source

pub fn expected_errors(&self) -> Option<f64>

Expected number of sequencing errors in the read (Phred+33).

Source

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.

Source

pub fn into_fasta(self) -> Sequence

Drop the quality string, turning a FASTQ record into a FASTA record.

Source

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
Source

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.

Source

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.

Source

pub fn to_string_in(&self, format: Format) -> Result<String>

Render the record in its native format as a String.

Trait Implementations§

Source§

impl Clone for Sequence

Source§

fn clone(&self) -> Sequence

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Sequence

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Sequence

Source§

fn default() -> Sequence

Returns the “default value” for a type. Read more
Source§

impl Display for Sequence

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

FASTA with 60-column wrapping, or FASTQ when quality is present.

Source§

impl Eq for Sequence

Source§

impl Hash for Sequence

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Sequence

Source§

fn eq(&self, other: &Sequence) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialEq<Sequence> for SequenceRef<'_>

Source§

fn eq(&self, other: &Sequence) -> bool

Compare against an owned record, so tests can assert the two read paths agree without copying first.

1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Sequence

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.