Skip to main content

NewickParser

Struct NewickParser 

Source
pub struct NewickParser<T: TreeBuilder> { /* private fields */ }
Expand description

Parser (configuration) for single/multiple Newick format (binary) phylogenetic trees.

Generic over TreeBuilder (construction). Uses a LabelResolver (with in turn uses the builders LabelStorage to resolve any mapping, e.g. as necessary when parsing a Nexus file with a TRANSLATE command.

§Construction

§Configuration

  • with_num_leaves(num_leaves)
    • Can be configured with number of leaves in trees to parse, otherwise it is inferred from the first parsed tree and then stored.
  • Plan to include in the future with_annotations(), so that it can be configured to parse vertex annotation instead of considering them comments, (e.g. extract pop_size and value from “A[&pop_size=0.123]”). For now, annotation remains unsupported.

§Parsing

§Example

use nexwick::newick::NewickParser;
use nexwick::parser::byte_parser::ByteParser;

let input = "((A_meleagrides:1.0,A_vulturinum:1.0):0.5,(N_meleagris:1.0,G_plumifera:1.0):0.5);";
let mut byte_parser = ByteParser::for_str(input);
let mut newick_parser = NewickParser::new_compact_defaults();

let tree = newick_parser.parse_str(&mut byte_parser).unwrap();
let labels = newick_parser.into_label_storage();

Implementations§

Source§

impl<T: TreeBuilder> NewickParser<T>

Source

pub fn new(tree_builder: T) -> Self

Creates a new NewickParser with the given tree builder and verbatim label resolver as default.

Source

pub fn with_num_leaves(self, num_leaves: usize) -> Self

Sets the expected number of leaves in each parsed tree.

This allows pre-allocation of data structures for better performance. If not set, the parser will count leaves during parsing.

Source

pub fn with_resolver(self, resolver: LabelResolver<T::Storage>) -> Self

Replaces the resolver with a custom one.

Used by NexusParser to provide resolvers configured from TRANSLATE blocks.

Source

pub fn into_parts(self) -> (T, LabelResolver<T::Storage>)

Consumes the parser and returns the tree builder and resolver.

Source

pub fn into_label_storage(self) -> T::Storage

Consumes the parser and returns the underlying LabelStorage.

This should be called after all trees have been parsed to retrieve the mapping of leaf labels to indices.

Source

pub fn label_storage(&self) -> &T::Storage

Get ref to LabelStorage of underlying LabelResolver

Source§

impl NewickParser<CompactTreeBuilder>

Source

pub fn new_compact_defaults() -> Self

Creates a new NewickParser for CompactTree with default settings:

  • Number of leaves is unknown (will be counted during parsing)
  • Verbatim label resolution
Source§

impl NewickParser<SimpleTreeBuilder>

Source

pub fn new_simple_defaults() -> Self

Creates a new NewickParser for SimpleTree with default settings:

  • Number of leaves is unknown (will be counted during parsing)
  • Verbatim label resolution
Source§

impl<T: TreeBuilder> NewickParser<T>

Source

pub fn into_iter<B: ByteSource>( self, byte_parser: ByteParser<B>, ) -> NewickIterator<B, T>

Consumes the parser and returns an iterator over trees from the byte source.

The parser can be retrieved again via NewickIterator::into_parser.

§Arguments
  • byte_parser - A byte parser with underlying source containing only Newick strings, except for whitespace and [...] comments.
§Returns

A NewickIterator allowing lazy parsing of trees.

Source

pub fn parse_all<B: ByteSource>( &mut self, byte_parser: ByteParser<B>, ) -> Result<Vec<T::Tree>, ParsingError>

Parses all Newick trees from the byte source until EOF.

§Arguments
  • byte_parser - A byte parser with underlying source containing only Newick strings, except for whitespace and [...] comments.
§Returns
  • Ok(Vec<T::Tree>) - All parsed trees
  • Err(ParsingError) - If any tree fails to parse
Source

pub fn parse_str<B: ByteSource>( &mut self, parser: &mut ByteParser<B>, ) -> Result<T::Tree, ParsingError>

Parses a single Newick tree from the given ByteParser.

§Arguments
  • parser - The byte parser positioned at the start of a Newick tree string
§Returns
  • Ok(T::Tree) - The parsed phylogenetic tree
  • Err(ParsingError) - If the Newick format is invalid

Trait Implementations§

Source§

impl Default for NewickParser<CompactTreeBuilder>

Source§

fn default() -> Self

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

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> 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, 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.