11 #include "../Tree/PhyloTree.h"
12 #include "../Tree/Tree.h"
13 #include "../Tree/TreeTemplate.h"
14 #include "../Tree/TreeTemplateTools.h"
38 return string(
"Nexus format (trees only). ");
47 vector<unique_ptr<Tree>> trees;
49 if (trees.size() == 0)
50 throw IOException(
"NexusIOTree::readTree(). No tree found in file.");
51 unique_ptr<TreeTemplate<Node>> tree(
dynamic_cast<TreeTemplate<Node>*
>(trees[0].release()));
62 throw IOException (
"NexusIOTree::readTrees(). Failed to read from stream");
70 throw Exception(
"NexusIOTree::readTrees(). No trees block was found.");
74 string cmdName =
"", cmdArgs =
"";
77 throw Exception(
"NexusIOTree::readTrees(). Missing tree command.");
81 map<string, string> translation;
82 bool hasTranslation =
false;
83 if (cmdName ==
"TRANSLATE")
92 throw Exception(
"NexusIOTree::readTrees(). Invalid translation description.");
95 translation[name] = tln;
97 hasTranslation =
true;
100 throw Exception(
"NexusIOTree::readTrees(). Missing tree command.");
106 while (cmdFound && cmdName !=
"END")
108 if (cmdName !=
"TREE")
109 throw Exception(
"NexusIOTree::readTrees(). Invalid command found: " + cmdName);
110 string::size_type pos = cmdArgs.find(
"=");
111 if (pos == string::npos)
112 throw Exception(
"NexusIOTree::readTrees(). invalid format, should be tree-name=tree-description");
113 string description = cmdArgs.substr(pos + 1);
120 vector<Node*> leaves = tree->getLeaves();
121 for (
size_t i = 0; i < leaves.size(); i++)
123 string name = leaves[i]->getName();
124 if (translation.find(name) == translation.end())
126 throw Exception(
"NexusIOTree::readTrees(). No translation was given for this leaf: " + name);
128 leaves[i]->setName(translation[name]);
131 trees.push_back(std::move(tree));
142 vector<unique_ptr<PhyloTree>> trees;
143 readPhyloTrees(in, trees);
144 if (trees.size() == 0)
145 throw IOException(
"NexusIOTree::readPhyloTree(). No tree found in file.");
146 return std::move(trees[0]);
156 throw IOException (
"NexusIOTree::readPhyloTrees(). Failed to read from stream");
164 throw Exception(
"NexusIOTree::readPhyloTrees(). No trees block was found.");
168 string cmdName =
"", cmdArgs =
"";
171 throw Exception(
"NexusIOTree::readPhyloTrees(). Missing tree command.");
175 map<string, string> translation;
176 bool hasTranslation =
false;
177 if (cmdName ==
"TRANSLATE")
186 throw Exception(
"NexusIOTree::readTrees(). Invalid translation description.");
189 translation[name] = tln;
191 hasTranslation =
true;
194 throw Exception(
"NexusIOTree::readPhyloTrees(). Missing tree command.");
200 while (cmdFound && cmdName !=
"END")
202 if (cmdName !=
"TREE")
203 throw Exception(
"NexusIOTree::readTrees(). Invalid command found: " + cmdName);
204 string::size_type pos = cmdArgs.find(
"=");
205 if (pos == string::npos)
206 throw Exception(
"NexusIOTree::readTrees(). invalid format, should be tree-name=tree-description");
207 string description = cmdArgs.substr(pos + 1);
211 istringstream ss(description +
";");
218 vector<shared_ptr<PhyloNode>> leaves = tree->getAllLeaves();
219 for (
size_t i = 0; i < leaves.size(); i++)
221 string name = leaves[i]->getName();
222 if (translation.find(name) == translation.end())
224 throw Exception(
"NexusIOTree::readTrees(). No translation was given for this leaf: " + name);
226 leaves[i]->setName(translation[name]);
229 trees.push_back(std::move(tree));
242 vector<const Tree*> trees;
243 trees.push_back(&
const_cast<Tree&
>(tree));
244 writeTrees(trees, out);
251 vector<const PhyloTree*> trees;
252 trees.push_back(&
const_cast<PhyloTree&
>(tree));
253 writePhyloTrees(trees, out);
261 vector<const Tree*> trees;
262 trees.push_back(&
const_cast<Tree&
>(tree));
263 writeTrees(trees, out);
273 throw IOException (
"NexusIOTree::write: failed to write to stream");
276 out <<
"#NEXUS" << endl;
278 out <<
"BEGIN TREES;" << endl;
281 vector<string> names;
282 for (
size_t i = 0; i < trees.size(); i++)
287 map<string, size_t> translation;
289 for (
size_t i = 0; i < names.size(); i++)
291 translation[names[i]] = code++;
295 vector<Tree*> translatedTrees(trees.size());
296 for (
size_t i = 0; i < trees.size(); i++)
298 vector<int> leavesId = trees[i]->getLeavesId();
299 Tree* tree =
dynamic_cast<Tree*
>(trees[i]->clone());
300 for (
size_t j = 0; j < leavesId.size(); j++)
304 translatedTrees[i] = tree;
310 for (map<string, size_t>::iterator it = translation.begin(); it != translation.end(); it++)
312 out << endl <<
" " << it->second <<
"\t" << it->first;
314 if (
count < translation.size())
320 for (
size_t i = 0; i < trees.size(); i++)
324 out <<
"END;" << endl;
327 for (
size_t i = 0; i < translatedTrees.size(); i++)
329 delete translatedTrees[i];
341 throw IOException (
"NexusIOTree::write: failed to write to stream");
344 out <<
"#NEXUS" << endl;
346 out <<
"BEGIN TREES;" << endl;
349 vector<string> names;
350 for (
size_t i = 0; i < trees.size(); i++)
355 map<string, size_t> translation;
357 for (
size_t i = 0; i < names.size(); i++)
359 translation[names[i]] = code++;
363 vector<PhyloTree*> translatedTrees;
364 for (
size_t i = 0; i < trees.size(); i++)
366 vector<shared_ptr<PhyloNode>> leaves = trees[i]->getAllLeaves();
370 for (
size_t j = 0; j < leaves.size(); j++)
374 translatedTrees.push_back(tree);
380 for (map<string, size_t>::iterator it = translation.begin(); it != translation.end(); it++)
382 out << endl <<
" " << it->second <<
"\t" << it->first;
384 if (
count < translation.size())
392 for (
size_t i = 0; i < trees.size(); i++)
394 out << endl <<
" TREE tree" << (i + 1) <<
" = ";
397 out <<
"END;" << endl;
400 for (
size_t i = 0; i < translatedTrees.size(); i++)
402 delete translatedTrees[i];
414 throw IOException (
"NexusIOTree::write: failed to write to stream");
417 out <<
"#NEXUS" << endl;
419 out <<
"BEGIN TREES;" << endl;
422 vector<string> names;
423 for (
size_t i = 0; i < trees.size(); i++)
428 map<string, size_t> translation;
430 for (
size_t i = 0; i < names.size(); i++)
432 translation[names[i]] = code++;
436 vector<Tree*> translatedTrees(trees.size());
437 for (
size_t i = 0; i < trees.size(); i++)
439 vector<int> leavesId = trees[i]->getLeavesId();
440 Tree* tree =
dynamic_cast<Tree*
>(trees[i]->clone());
441 for (
size_t j = 0; j < leavesId.size(); j++)
445 translatedTrees[i] = tree;
451 for (map<string, size_t>::iterator it = translation.begin(); it != translation.end(); it++)
453 out << endl <<
" " << it->second <<
"\t" << it->first;
455 if (
count < translation.size())
461 for (
size_t i = 0; i < trees.size(); i++)
465 out <<
"END;" << endl;
468 for (
size_t i = 0; i < translatedTrees.size(); i++)
470 delete translatedTrees[i];
virtual std::shared_ptr< N > getNode(NodeIndex nodeIndex) const=0
const std::string & nextToken()
The so-called 'newick' parenthetic format.
void writePhyloTree(const PhyloTree &tree, std::ostream &out) const override
Write a tree to a stream.
std::unique_ptr< PhyloTree > readPhyloTree(std::istream &in) const override
Read a tree from a stream.
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
const std::string getFormatDescription() const override
std::unique_ptr< PhyloTree > readPhyloTree(std::istream &in) const override=0
void write_(const Tree &tree, std::ostream &out) const
virtual void readTrees(std::istream &in, std::vector< std::unique_ptr< Tree >> &trees) const override=0
const std::string getFormatName() const override
PhyloTree * clone() const
size_t numberOfRemainingTokens() const
const std::string & nextToken()
bool hasMoreToken() const
The phylogenetic tree class.
Interface for phylogenetic tree objects.
virtual std::string getNodeName(int nodeId) const =0
virtual void setNodeName(int nodeId, const std::string &name)=0
std::string removeSurroundingWhiteSpaces(const std::string &s)
std::string toUpper(const std::string &s)
std::string toString(T t)
std::size_t count(const std::string &s, const std::string &pattern)
Defines the basic types of data flow nodes.