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
new(tree_builder, resolver)— generic constructornew_compact_defaults()- uses CompactTreeBuilder and a LeafLabelMap with verbatim label resolution.
new_simple_defaults()- uses SimpleTreeBuilder and a SimpleLabelStorage with verbatim label resolution.
§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. extractpop_sizeand 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>
impl<T: TreeBuilder> NewickParser<T>
Sourcepub fn new(tree_builder: T) -> Self
pub fn new(tree_builder: T) -> Self
Creates a new NewickParser with the given tree builder and verbatim label resolver as default.
Sourcepub fn with_num_leaves(self, num_leaves: usize) -> Self
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.
Sourcepub fn with_resolver(self, resolver: LabelResolver<T::Storage>) -> Self
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.
Sourcepub fn into_parts(self) -> (T, LabelResolver<T::Storage>)
pub fn into_parts(self) -> (T, LabelResolver<T::Storage>)
Consumes the parser and returns the tree builder and resolver.
Sourcepub fn into_label_storage(self) -> T::Storage
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.
Sourcepub fn label_storage(&self) -> &T::Storage
pub fn label_storage(&self) -> &T::Storage
Get ref to LabelStorage of underlying LabelResolver
Source§impl NewickParser<CompactTreeBuilder>
impl NewickParser<CompactTreeBuilder>
Sourcepub fn new_compact_defaults() -> Self
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>
impl NewickParser<SimpleTreeBuilder>
Sourcepub fn new_simple_defaults() -> Self
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>
impl<T: TreeBuilder> NewickParser<T>
Sourcepub fn into_iter<B: ByteSource>(
self,
byte_parser: ByteParser<B>,
) -> NewickIterator<B, T> ⓘ
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.
Sourcepub fn parse_all<B: ByteSource>(
&mut self,
byte_parser: ByteParser<B>,
) -> Result<Vec<T::Tree>, ParsingError>
pub fn parse_all<B: ByteSource>( &mut self, byte_parser: ByteParser<B>, ) -> Result<Vec<T::Tree>, ParsingError>
Sourcepub fn parse_str<B: ByteSource>(
&mut self,
parser: &mut ByteParser<B>,
) -> Result<T::Tree, ParsingError>
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 treeErr(ParsingError)- If the Newick format is invalid