12 #include "../Tree/PhyloBranch.h"
13 #include "../Tree/PhyloNode.h"
14 #include "../Tree/PhyloTree.h"
15 #include "../Tree/Tree.h"
16 #include "../Tree/TreeTemplate.h"
17 #include "../Tree/TreeTemplateTools.h"
36 return string(
"New hampshire parenthesis format. ") +
37 "See http://evolution.genetics.washington.edu/phylip/newicktree.html for more info.";
49 throw IOException (
"Newick::read: failed to read from stream");
53 string temp, description;
55 while (getline(in, temp,
'\n'))
57 string::size_type index = temp.find(
";");
58 if (index != string::npos)
60 description += temp.substr(0, index + 1);
70 throw IOException(
"Newick::read: no tree was found!");
81 throw IOException (
"Newick::readPhyloTree: failed to read from stream");
85 string temp, description;
87 while (getline(in, temp,
'\n'))
89 string::size_type index = temp.find(
";");
90 if (index != string::npos)
92 description += temp.substr(0, index + 1);
102 throw IOException(
"Newick::read: no tree was found!");
103 return parenthesisToPhyloTree(description, useBootstrap_, bootstrapPropertyName_,
false, verbose_);
113 throw IOException (
"Newick::readTrees(vector): failed to read from stream");
117 string temp, description;
118 string::size_type index;
120 while (getline(in, temp,
'\n'))
122 index = temp.find(
";");
123 if (index != string::npos)
125 description += temp.substr(0, index + 1);
129 description = temp.substr(index + 1);
144 throw IOException (
"Newick::readTrees(vector): failed to read from stream");
148 string temp, description;
149 string::size_type index;
151 while (getline(in, temp,
'\n'))
153 index = temp.find(
";");
154 if (index != string::npos)
156 description += temp.substr(0, index + 1);
159 trees.push_back(parenthesisToPhyloTree(description, useBootstrap_, bootstrapPropertyName_,
false, verbose_));
160 description = temp.substr(index + 1);
178 bool hasColon =
false;
179 for (colonIndex = elt.size(); colonIndex > 0 && elt[colonIndex] !=
')'; colonIndex--)
181 if (elt[colonIndex] ==
':')
193 elt2 = elt.substr(0, colonIndex);
202 string::size_type lastP = elt2.rfind(
')');
203 string::size_type firstP = elt2.find(
'(');
204 if (firstP == string::npos)
214 throw IOException(
"Newick::getElement(). Invalid format: bad closing parenthesis in " + elt2);
220 element.annotation = bootstrap;
233 shared_ptr<PhyloNode>
Newick::parenthesisToNode(
PhyloTree& tree, shared_ptr<PhyloNode> father,
const string& description,
unsigned int& nodeCounter,
bool bootstrap,
const string& propertyName,
bool withId,
bool verbose)
const
239 std::shared_ptr<PhyloNode> node(
new PhyloNode());
241 shared_ptr<PhyloBranch> branch(father ?
new PhyloBranch() : 0);
279 vector<string> elements;
292 ostringstream realName;
301 node->setName(realName.str());
314 for (
size_t i = 0; i < elements.size(); i++)
317 parenthesisToNode(tree, node, elements[i], nodeCounter, bootstrap, propertyName, withId, verbose);
338 string::size_type semi = description.rfind(
';');
339 if (semi == string::npos)
340 throw Exception(
"Newick::parenthesisToTree(). Bad format: no semi-colon found.");
341 string content = description.substr(0, semi);
342 unsigned int nodeCounter = 0;
343 auto tree = make_unique<PhyloTree>();
344 shared_ptr<PhyloNode> root = parenthesisToNode(*tree, 0, content, nodeCounter, bootstrap, propertyName, withId, verbose);
348 (*ApplicationTools::message) <<
" nodes loaded.";
364 throw IOException (
"Newick::writeTree: failed to write to stream");
381 throw IOException (
"Newick::writeTree: failed to write to stream");
385 out << treeToParenthesis(tree, writeId_);
389 out << treeToParenthesis(tree,
false, bootstrapPropertyName_);
401 throw IOException (
"Newick::writeTree: failed to write to stream");
421 throw IOException (
"Newick::write: failed to write to stream");
423 for (
unsigned int i = 0; i < trees.size(); i++)
444 throw IOException (
"Newick::write: failed to write to stream");
446 for (
unsigned int i = 0; i < trees.size(); i++)
466 throw IOException (
"Newick::write: failed to write to stream");
468 for (
unsigned int i = 0; i < trees.size(); i++)
472 out << treeToParenthesis(*trees[i], writeId_);
476 out << treeToParenthesis(*trees[i],
false, bootstrapPropertyName_);
490 s << node->getName();
496 vector<shared_ptr<PhyloNode>> vSons = tree.
getSons(node);
498 for (vector<shared_ptr<PhyloNode>>::const_iterator it = vSons.begin(); it != vSons.end(); it++)
500 if (it != vSons.begin())
503 s << nodeToParenthesis(tree, *it);
517 if (branch && branch->hasProperty(
"bootstrap"))
518 s << (
dynamic_cast<const Number<double>*
>(branch->getProperty(
"bootstrap"))->getValue());
521 if (branch && branch->hasLength())
522 s <<
":" << branch->getLength();
535 s << node->getName();
541 vector<shared_ptr<PhyloNode>> vSons = tree.
getSons(node);
543 for (vector<shared_ptr<PhyloNode>>::const_iterator it = vSons.begin(); it != vSons.end(); it++)
545 if (it != vSons.begin())
548 s << nodeToParenthesis(tree, *it, bootstrap, propertyName);
557 if (branch->hasProperty(
"bootstrap"))
558 s << (
dynamic_cast<const Number<double>*
>(branch->getProperty(
"bootstrap"))->getValue());
562 if (node->hasProperty(propertyName))
563 s << *(
dynamic_cast<const BppString*
>(node->getProperty(propertyName)));
568 if (branch && branch->hasLength())
569 s <<
":" << branch->getLength();
581 shared_ptr<PhyloNode> root = tree.
getRoot();
583 std::vector<shared_ptr<PhyloNode>> rSons = tree.
getSons(root);
587 for (
size_t i = 0; i < rSons.size(); ++i)
591 s << nodeToParenthesis(tree, rSons[i], writeId);
596 s << root->getName();
598 for (
size_t i = 0; i < rSons.size(); ++i)
602 s << nodeToParenthesis(tree, rSons[i], writeId);
610 if (branch && branch->hasLength())
611 s <<
":" << branch->getLength();
624 shared_ptr<PhyloNode> root = tree.
getRoot();
626 std::vector<shared_ptr<PhyloNode>> rSons = tree.
getSons(root);
630 for (
size_t i = 0; i < rSons.size(); ++i)
634 s << nodeToParenthesis(tree, rSons[i], bootstrap, propertyName);
639 s << root->getName();
641 for (
size_t i = 0; i < rSons.size(); ++i)
645 s << nodeToParenthesis(tree, rSons[i], bootstrap, propertyName);
657 if (branch->hasProperty(
"bootstrap"))
658 s << (
dynamic_cast<const Number<double>*
>(branch->getProperty(
"bootstrap"))->getValue());
662 if (branch->hasProperty(propertyName))
663 s << *(
dynamic_cast<const BppString*
>(branch->getProperty(propertyName)));
bool isLeaf(const Nref node) const
AssociationGraphObserver< N, E >::NodeIndex NodeIndex
virtual NodeIndex setNodeIndex(const std::shared_ptr< N > nodeObject, NodeIndex index)=0
virtual std::shared_ptr< N > getRoot() const=0
virtual EdgeIndex setEdgeIndex(const std::shared_ptr< E > edgeObject, EdgeIndex index)=0
virtual NodeIndex getNodeIndex(const std::shared_ptr< N > nodeObject) const=0
virtual void createNode(std::shared_ptr< N > newNodeObject)=0
std::vector< std::shared_ptr< N > > getSons(const std::shared_ptr< N > node) const
bool hasFather(const std::shared_ptr< N > nodeObject) const
std::shared_ptr< E > getEdgeToFather(const std::shared_ptr< N > nodeObject) const
size_t getNumberOfSons(const std::shared_ptr< N > node) const
const std::string & nextToken()
virtual std::unique_ptr< TreeTemplate< Node > > readTreeTemplate(std::istream &in) const=0
virtual void readPhyloTrees(std::istream &in, std::vector< std::unique_ptr< PhyloTree >> &trees) const override=0
std::shared_ptr< PhyloNode > parenthesisToNode(PhyloTree &tree, std::shared_ptr< PhyloNode > father, const std::string &description, unsigned int &nodeCounter, bool bootstrap, const std::string &propertyName, bool withId, bool verbose) const
std::unique_ptr< PhyloTree > parenthesisToPhyloTree(const std::string &description, bool bootstrap=false, const std::string &propertyName="", bool withId=false, bool verbose=false) const
std::string nodeToParenthesis(const PhyloTree &tree, std::shared_ptr< PhyloNode > node, bool writeId=false) const
Get the Newick description of a subtree.
void write_(const Tree &tree, std::ostream &out) const
std::unique_ptr< PhyloTree > readPhyloTree(std::istream &in) const override=0
IOTree::Element getElement(const std::string &elt) const override
const std::string getFormatDescription() const override
virtual void readTrees(std::istream &in, std::vector< std::unique_ptr< Tree >> &trees) const override=0
std::string treeToParenthesis(const PhyloTree &tree, bool writeId=false) const
Get the parenthesis description of a tree.
const std::string getFormatName() const override
size_t numberOfRemainingTokens() const
bool hasMoreToken() const
const std::string & getToken(size_t pos) const
The phylogenetic tree class.
Interface for phylogenetic tree objects.
int toInt(const std::string &s, char scientificNotation='e')
double toDouble(const std::string &s, char dec='.', char scientificNotation='e')
std::string removeSurroundingWhiteSpaces(const std::string &s)
std::string removeSubstrings(const std::string &s, char blockBeginning, char blockEnding)
bool isEmpty(const std::string &s)
Defines the basic types of data flow nodes.