bpp-phyl  2.2.0
bpp::Nhx Class Reference

The so-called 'Nhx - New Hampshire eXtended' parenthetic format. More...

#include <Bpp/Phyl/Io/Nhx.h>

+ Inheritance diagram for bpp::Nhx:
+ Collaboration diagram for bpp::Nhx:

Classes

struct  Element
 
struct  Property
 

Public Member Functions

 Nhx (bool useTagsAsPptNames=true)
 Build a new Nhx reader/writer. More...
 
virtual ~Nhx ()
 
TreeTemplate< Node > * parenthesisToTree (const std::string &description) const throw (Exception)
 
std::string treeToParenthesis (const TreeTemplate< Node > &tree) const
 
void registerProperty (const Property &property)
 
void changeTagsToNames (Node &node) const
 Convert property names from tag to names. More...
 
void changeNamesToTags (Node &node) const
 Convert property names from names to tags. More...
 
void useTagsAsPropertyNames (bool yn)
 
bool useTagsAsPropertyNames () const
 
virtual const std::string getDataType () const
 
The IOTree interface
const std::string getFormatName () const
 
const std::string getFormatDescription () const
 
The ITree interface
TreeTemplate< Node > * read (const std::string &path) const throw (Exception)
 Read a tree from a file. More...
 
TreeTemplate< Node > * read (std::istream &in) const throw (Exception)
 Read a tree from a stream. More...
 
The OTree interface
void write (const Tree &tree, const std::string &path, bool overwrite=true) const throw (Exception)
 Write a tree to a file. More...
 
void write (const Tree &tree, std::ostream &out) const throw (Exception)
 Write a tree to a stream. More...
 
The IMultiTree interface
void read (const std::string &path, std::vector< Tree *> &trees) const throw (Exception)
 Read trees from a file. More...
 
void read (std::istream &in, std::vector< Tree *> &trees) const throw (Exception)
 Read trees from a stream. More...
 
The OMultiTree interface
void write (const std::vector< Tree *> &trees, const std::string &path, bool overwrite=true) const throw (Exception)
 Write trees to a file. More...
 
void write (const std::vector< Tree *> &trees, std::ostream &out) const throw (Exception)
 Write trees to a stream. More...
 

Protected Member Functions

void write_ (const Tree &tree, std::ostream &out) const throw (Exception)
 
template<class N >
void write_ (const TreeTemplate< N > &tree, std::ostream &out) const throw (Exception)
 
void write_ (const std::vector< Tree *> &trees, std::ostream &out) const throw (Exception)
 
template<class N >
void write_ (const std::vector< TreeTemplate< N > *> &trees, std::ostream &out) const throw (Exception)
 
Element getElement (const std::string &elt) const throw (IOException)
 
NodeparenthesisToNode (const std::string &description) const
 
std::string propertiesToParenthesis (const Node &node) const
 
std::string nodeToParenthesis (const Node &node) const
 
bool setNodeProperties (Node &node, const std::string properties) const
 

Static Protected Member Functions

static std::string propertyToString_ (const Clonable *pptObject, short type) throw (Exception)
 
static Clonable * stringToProperty_ (const std::string &pptDesc, short type) throw (Exception)
 

Private Attributes

std::set< PropertysupportedProperties_
 
bool useTagsAsPropertyNames_
 
bool hasIds_
 

Detailed Description

The so-called 'Nhx - New Hampshire eXtended' parenthetic format.

See http://www.phylosoft.org/Nhx/ for details.

Branch lengths and node annotations are supported:

ex: ((A:0.1[&&NHX:S=human], B:0.15[&&NHX:S=chimp]):0.2[&&NHX:B=90:D=N:S=primates], C:0.27[&&NHX:S=mouse]);

Where e.g. "S=human" means that the sequence A comes from species "human", "B=90" stands for a support value, and "D=N" means that there was no duplication at the node. Other tags are allowed, see http://www.phylosoft.org/NHX/. By default, comments between "[" and "]" are removed, unless the opening bracket is followed by "&&NHX".

Code example:

#include <Phyl/Nhx.h>
#include <Phyl/Tree.h>
Nhx * NhxReader = new Nhx();
try {
Tree * tree = nhxReader->read("MyTestTree.dnd"); // Tree in file MyTestTree.dnd
cout << "Tree has " << tree->getNumberOfLeaves() << " leaves." << endl;
} catch (Exception e) {
cout << "Error when reading tree." << endl;
}
delete tree;
delete nhxReader;

All node annotations are stored as node properties, with type bppString for all properties except for support values, where a Number is used.

Definition at line 87 of file Nhx.h.

Constructor & Destructor Documentation

◆ Nhx()

Nhx::Nhx ( bool  useTagsAsPptNames = true)

Build a new Nhx reader/writer.

Comments between hooks ('[' ']') are ignored.

Parameters
useTagsAsPptNamesTells if the NHX tag should be used as a property name in the parsed tree.

Definition at line 60 of file Nhx.cpp.

References bpp::TreeTools::BOOTSTRAP, and registerProperty().

◆ ~Nhx()

virtual bpp::Nhx::~Nhx ( )
inlinevirtual

Definition at line 152 of file Nhx.h.

Member Function Documentation

◆ changeNamesToTags()

void Nhx::changeNamesToTags ( Node node) const

Convert property names from names to tags.

If a tree has been parsed using useTagsAsPropertyNames=false, this method allows to convert the tree as is it was parsed using the option set to true.

Parameters
nodeThe root node of the subtree to convert.

Definition at line 553 of file Nhx.cpp.

References bpp::Node::deleteBranchProperty(), bpp::Node::deleteNodeProperty(), bpp::Node::getBranchProperty(), bpp::Node::getNodeProperty(), bpp::Node::getNumberOfSons(), bpp::Node::getSon(), bpp::Node::hasBranchProperty(), bpp::Node::hasNodeProperty(), bpp::Node::setBranchProperty(), bpp::Node::setNodeProperty(), and supportedProperties_.

◆ changeTagsToNames()

void Nhx::changeTagsToNames ( Node node) const

Convert property names from tag to names.

If a tree has been parsed using useTagsAsPropertyNames=true, this method allows to convert the tree as is it was parsed using the option set to false.

Parameters
nodeThe root node of the subtree to convert.

Definition at line 533 of file Nhx.cpp.

References bpp::Node::deleteBranchProperty(), bpp::Node::deleteNodeProperty(), bpp::Node::getBranchProperty(), bpp::Node::getNodeProperty(), bpp::Node::getNumberOfSons(), bpp::Node::getSon(), bpp::Node::hasBranchProperty(), bpp::Node::hasNodeProperty(), bpp::Node::setBranchProperty(), bpp::Node::setNodeProperty(), and supportedProperties_.

◆ getDataType()

virtual const std::string bpp::IOTree::getDataType ( ) const
inlinevirtualinherited

Definition at line 68 of file IoTree.h.

◆ getElement()

Nhx::Element Nhx::getElement ( const std::string &  elt) const
throw (IOException
)
protected

◆ getFormatDescription()

const string Nhx::getFormatDescription ( ) const

Definition at line 91 of file Nhx.cpp.

◆ getFormatName()

const string Nhx::getFormatName ( ) const

Definition at line 87 of file Nhx.cpp.

◆ nodeToParenthesis()

string Nhx::nodeToParenthesis ( const Node node) const
protected

◆ parenthesisToNode()

◆ parenthesisToTree()

TreeTemplate< Node > * Nhx::parenthesisToTree ( const std::string &  description) const
throw (Exception
)

◆ propertiesToParenthesis()

◆ propertyToString_()

string Nhx::propertyToString_ ( const Clonable *  pptObject,
short  type 
)
throw (Exception
)
staticprotected

Definition at line 353 of file Nhx.cpp.

Referenced by propertiesToParenthesis().

◆ read() [1/4]

TreeTemplate<Node>* bpp::Nhx::read ( const std::string &  path) const
throw (Exception
)
inlinevirtual

Read a tree from a file.

Parameters
pathThe file path.
Returns
A new tree object.
Exceptions
ExceptionIf an error occured.

Reimplemented from bpp::AbstractITree.

Definition at line 170 of file Nhx.h.

References bpp::AbstractITree::read().

◆ read() [2/4]

TreeTemplate< Node > * Nhx::read ( std::istream &  in) const
throw (Exception
)
virtual

Read a tree from a stream.

Parameters
inThe input stream.
Returns
A new tree object.
Exceptions
ExceptionIf an error occured.

Implements bpp::AbstractITree.

Definition at line 104 of file Nhx.cpp.

◆ read() [3/4]

void bpp::Nhx::read ( const std::string &  path,
std::vector< Tree *> &  trees 
) const
throw (Exception
)
inlinevirtual

Read trees from a file.

Parameters
pathThe file path.
treesThe output trees container.
Exceptions
ExceptionIf an error occured.

Reimplemented from bpp::AbstractIMultiTree.

Definition at line 199 of file Nhx.h.

References bpp::AbstractIMultiTree::read().

◆ read() [4/4]

void Nhx::read ( std::istream &  in,
std::vector< Tree *> &  trees 
) const
throw (Exception
)
virtual

Read trees from a stream.

Parameters
inThe input stream.
treesThe output trees container.
Exceptions
ExceptionIf an error occured.

Implements bpp::AbstractIMultiTree.

Definition at line 150 of file Nhx.cpp.

◆ registerProperty()

void bpp::Nhx::registerProperty ( const Property property)
inline

Definition at line 225 of file Nhx.h.

References supportedProperties_.

Referenced by Nhx().

◆ setNodeProperties()

bool Nhx::setNodeProperties ( Node node,
const std::string  properties 
) const
protected

◆ stringToProperty_()

Clonable * Nhx::stringToProperty_ ( const std::string &  pptDesc,
short  type 
)
throw (Exception
)
staticprotected

Definition at line 386 of file Nhx.cpp.

Referenced by setNodeProperties().

◆ treeToParenthesis()

◆ useTagsAsPropertyNames() [1/2]

void bpp::Nhx::useTagsAsPropertyNames ( bool  yn)
inline

Definition at line 251 of file Nhx.h.

References useTagsAsPropertyNames_.

◆ useTagsAsPropertyNames() [2/2]

bool bpp::Nhx::useTagsAsPropertyNames ( ) const
inline

Definition at line 252 of file Nhx.h.

References useTagsAsPropertyNames_.

◆ write() [1/4]

void bpp::Nhx::write ( const Tree tree,
const std::string &  path,
bool  overwrite = true 
) const
throw (Exception
)
inlinevirtual

Write a tree to a file.

Parameters
treeA tree object.
pathThe file path.
overwriteTell if existing file must be overwritten. Otherwise append to the file.
Exceptions
ExceptionIf an error occured.

Reimplemented from bpp::AbstractOTree.

Definition at line 183 of file Nhx.h.

References bpp::AbstractOTree::write().

◆ write() [2/4]

void bpp::Nhx::write ( const Tree tree,
std::ostream &  out 
) const
throw (Exception
)
inlinevirtual

Write a tree to a stream.

Parameters
treeA tree object.
outThe output stream.
Exceptions
ExceptionIf an error occured.

Implements bpp::AbstractOTree.

Definition at line 188 of file Nhx.h.

References write_().

◆ write() [3/4]

void bpp::Nhx::write ( const std::vector< Tree *> &  trees,
const std::string &  path,
bool  overwrite = true 
) const
throw (Exception
)
inlinevirtual

Write trees to a file.

Parameters
treesA vector of tree objects.
pathThe file path.
overwriteTell if existing file must be overwritten. Otherwise append to the file.
Exceptions
ExceptionIf an error occured.

Reimplemented from bpp::AbstractOMultiTree.

Definition at line 211 of file Nhx.h.

References bpp::AbstractOMultiTree::write().

◆ write() [4/4]

void bpp::Nhx::write ( const std::vector< Tree *> &  trees,
std::ostream &  out 
) const
throw (Exception
)
inlinevirtual

Write trees to a stream.

Parameters
treesA vector of tree objects.
outThe output stream.
Exceptions
ExceptionIf an error occured.

Implements bpp::AbstractOMultiTree.

Definition at line 215 of file Nhx.h.

References write_().

◆ write_() [1/4]

void Nhx::write_ ( const Tree tree,
std::ostream &  out 
) const
throw (Exception
)
protected

Definition at line 131 of file Nhx.cpp.

Referenced by write().

◆ write_() [2/4]

template<class N >
void Nhx::write_ ( const TreeTemplate< N > &  tree,
std::ostream &  out 
) const
throw (Exception
)
protected

Definition at line 141 of file Nhx.cpp.

◆ write_() [3/4]

void Nhx::write_ ( const std::vector< Tree *> &  trees,
std::ostream &  out 
) const
throw (Exception
)
protected

Definition at line 181 of file Nhx.cpp.

◆ write_() [4/4]

template<class N >
void Nhx::write_ ( const std::vector< TreeTemplate< N > *> &  trees,
std::ostream &  out 
) const
throw (Exception
)
protected

Definition at line 194 of file Nhx.cpp.

Member Data Documentation

◆ hasIds_

bool bpp::Nhx::hasIds_
mutableprivate

Definition at line 140 of file Nhx.h.

Referenced by parenthesisToNode().

◆ supportedProperties_

std::set<Property> bpp::Nhx::supportedProperties_
private

◆ useTagsAsPropertyNames_

bool bpp::Nhx::useTagsAsPropertyNames_
private

Definition at line 139 of file Nhx.h.

Referenced by propertiesToParenthesis(), setNodeProperties(), and useTagsAsPropertyNames().


The documentation for this class was generated from the following files: