Skip to main content

SeqStats

Struct SeqStats 

Source
pub struct SeqStats {
    pub count: u64,
    pub total_length: u64,
    pub min_length: Option<u64>,
    pub max_length: Option<u64>,
    pub bases: BaseCounts,
    /* private fields */
}
Expand description

Accumulates length, composition and quality statistics.

use fastx::{FastxReader, SeqStats};

let data = b">a\nACGTACGTAC\n>b\nGGCC\n";
let mut stats = SeqStats::default();
FastxReader::new(&data[..]).for_each_record(|r| { stats.push(r); Ok(()) })?;

assert_eq!(stats.count, 2);
assert_eq!(stats.total_length, 14);
assert_eq!(stats.min_length, Some(4));
assert_eq!(stats.max_length, Some(10));
assert_eq!(stats.n50(), Some(10));

Fields§

§count: u64

Number of records seen.

§total_length: u64

Sum of all sequence lengths.

§min_length: Option<u64>

Shortest record, if any.

§max_length: Option<u64>

Longest record, if any.

§bases: BaseCounts

Base composition across all records.

Implementations§

Source§

impl SeqStats

Source

pub fn new() -> SeqStats

An empty accumulator.

Source

pub fn push(&mut self, record: &Sequence)

Fold one record in.

Source

pub fn merge(&mut self, other: &SeqStats)

Merge another accumulator, for parallel or per-file aggregation.

Source

pub fn is_empty(&self) -> bool

True when no records have been seen.

Source

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

Mean sequence length.

Source

pub fn median_length(&self) -> Option<u64>

Median sequence length (lower median for an even count).

Source

pub fn n50(&self) -> Option<u64>

N50: contigs of at least this length cover half the total.

Source

pub fn n90(&self) -> Option<u64>

N90.

Source

pub fn l50(&self) -> Option<u64>

L50: the number of contigs that make up the N50.

Source

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

GC fraction over unambiguous bases.

Source

pub fn quality_bases(&self) -> u64

Number of quality characters seen across all records.

Source

pub fn quality_histogram(&self) -> &[u64; 94]

Counts per Phred score, indexed by score — the same data FastQC plots.

let mut stats = SeqStats::new();
stats.push(&Sequence::fastq("r", b"ACGT", b"IIII")?);
assert_eq!(stats.quality_histogram()[40], 4); // 'I' is Q40
Source

pub fn expected_errors(&self) -> f64

Expected number of wrong bases across all records.

Source

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

Mean quality as a Phred score, derived from the mean error rate.

Source

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

Fraction of bases at Q20 or better.

Source

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

Fraction of bases at Q30 or better.

Source

pub fn fraction_at_least(&self, score: u8) -> Option<f64>

Fraction of bases whose Phred score is at least score.

Source

pub fn lengths(&self) -> &[u64]

All observed lengths, in the order the records were seen.

Source

pub fn to_json(&self) -> String

Render the statistics as a JSON object, for pipelines that parse output.

The object is on one line, so a run over several files is valid line-delimited JSON and streams straight into jq. Pipe through jq . if you want it laid out.

Absent figures — quality for FASTA, N50 after SeqStats::forget_lengths — come out as null rather than being omitted, so the shape of the object never changes.

let mut stats = SeqStats::new();
stats.push(&Sequence::fasta("a", b"ACGT"));
let json = stats.to_json();
assert!(json.starts_with('{') && json.ends_with('}'));
assert!(!json.contains('\n'), "must stay on one line");
assert!(json.contains("\"records\":1"));
assert!(json.contains("\"mean_quality\":null"));
Source

pub fn forget_lengths(&mut self)

Drop the retained per-record lengths to cap memory on huge inputs.

After this call SeqStats::n50, SeqStats::median_length and SeqStats::l50 return None, but counts and means stay correct.

Trait Implementations§

Source§

impl Clone for SeqStats

Source§

fn clone(&self) -> SeqStats

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 SeqStats

Source§

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

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

impl Default for SeqStats

Source§

fn default() -> SeqStats

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

impl Display for SeqStats

Source§

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

A block of aligned label value lines, without a trailing newline.

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.