-
Notifications
You must be signed in to change notification settings - Fork 0
/
Newick.cpp
358 lines (325 loc) · 11.5 KB
/
Newick.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#ifdef _MSC_VER
#pragma warning(disable : 4512) // warning C4512:
// 'boost::spirit::classic::optional<S>' :
// assignment operator could not be generated
#pragma warning(disable : 4503) // warning C4503: '...' : decorated name length
// exceeded, name was truncated
#endif
#include <iostream>
#include <fstream>
#include <iomanip>
#include "Newick.h"
#include "NewickGrammar.h"
#include "Exceptions.h"
#include "VerbosityLevels.h"
// Access to the Boost::Spirit parse tree
//
// typedef boost::spirit::classic::tree_match<char const*>
// ParseTreeMatchType;
// typedef ParseTreeMatchType::tree_iterator ParseTreeIteratorType;
// has been changed into:
// typedef boost::spirit::classic::tree_match<char
// const*>::tree_iterator ParseTreeIteratorType;
void Newick::printTree(ParseTreeIteratorType const &aTreeIterator,
unsigned int aIndent, unsigned int aIndentIncrement) {
// Indent the level
for (unsigned int k = 0; k < aIndent; ++k)
std::cout << ' ';
// Print the node (after getting the node id)
boost::spirit::classic::parser_id id = aTreeIterator->value.id();
if (id == NewickGrammar::treeID)
std::cout << "TREE";
else if (id == NewickGrammar::nodelistID)
std::cout << "NODELST";
else if (id == NewickGrammar::subtreeID)
std::cout << "SUBTREE";
else if (id == NewickGrammar::fulllabelID)
std::cout << "FLAB";
else if (id == NewickGrammar::labelID)
std::cout << "LABEL";
else if (id == NewickGrammar::markerID)
std::cout << "MARKER";
else if (id == NewickGrammar::branchlenID)
std::cout << "BRANCHLEN";
else if (id == NewickGrammar::cblenID)
std::cout << "CBLEN";
else
std::cout << "????";
// Print the corresponding label if any
if (aTreeIterator->value.begin() != aTreeIterator->value.end()) {
std::string label_name(aTreeIterator->value.begin(),
aTreeIterator->value.end());
std::cout << ' ' << label_name << std::endl;
} else {
std::cout << std::endl;
}
// Recurse on the children
for (ParseTreeIteratorType ic = aTreeIterator->children.begin();
ic != aTreeIterator->children.end(); ++ic)
printTree(ic, aIndent + aIndentIncrement, aIndentIncrement);
}
void Newick::evaluateTreeNode(ParseTreeIteratorType const &aTreeIterator,
TreeNode *aNode) {
unsigned int k;
// Get the node id
boost::spirit::classic::parser_id id = aTreeIterator->value.id();
// if(aTreeIterator->value.id() == NewickGrammar::treeID)
if (id == NewickGrammar::treeID) {
// std::cout << "tree (" << std::endl;
for (k = 0; k < aTreeIterator->children.size(); ++k)
evaluateTreeNode(aTreeIterator->children.begin() + k, aNode);
// std::cout << ")" << std::endl;
} else if (id == NewickGrammar::nodelistID) {
// std::cout << "node_list {" << std::endl;
// TreeNode* x = new TreeNode;
// n->addChild(x);
for (k = 0; k < aTreeIterator->children.size(); ++k)
evaluateTreeNode(aTreeIterator->children.begin() + k, aNode);
// std::cout << "}" << std::endl;
} else if (id == NewickGrammar::subtreeID) {
// std::cout << "subtree [" << std::endl;
TreeNode *x = new TreeNode;
aNode->addChild(x);
x->addParent(aNode);
for (k = 0; k < aTreeIterator->children.size(); ++k)
evaluateTreeNode(aTreeIterator->children.begin() + k, x);
// std::cout << "]" << std::endl;
} else if (id == NewickGrammar::fulllabelID) {
// std::cout << "full_label " << std::endl;
for (k = 0; k < aTreeIterator->children.size(); ++k)
evaluateTreeNode(aTreeIterator->children.begin() + k, aNode);
} else if (id == NewickGrammar::labelID) {
if (aTreeIterator->children.size() == 0) {
std::string label_name(aTreeIterator->value.begin(),
aTreeIterator->value.end());
// std::cout << "label " << label_name << std::endl;
aNode->addLabel(label_name);
} else {
for (k = 0; k < aTreeIterator->children.size(); ++k)
evaluateTreeNode(aTreeIterator->children.begin() + k, aNode);
}
} else if (id == NewickGrammar::markerID) {
if (aTreeIterator->children.size() == 0) {
std::string label_name(aTreeIterator->value.begin(),
aTreeIterator->value.end());
// std::cout << "type " << label_name << std::endl;
aNode->addType(label_name);
} else {
for (k = 0; k < aTreeIterator->children.size(); ++k)
evaluateTreeNode(aTreeIterator->children.begin() + k, aNode);
}
} else if (id == NewickGrammar::branchlenID) {
if (aTreeIterator->children.size() == 0) {
std::string blen(aTreeIterator->value.begin(),
aTreeIterator->value.end());
// std::cout << "branch_length " << atof(blen.c_str())<< std::endl;
aNode->addLen(atof(blen.c_str()));
} else {
for (k = 0; k < aTreeIterator->children.size(); ++k)
evaluateTreeNode(aTreeIterator->children.begin() + k, aNode);
}
} else if (id == NewickGrammar::cblenID) {
// std::cout << "colon_plus_len " << std::endl;
for (k = 0; k < aTreeIterator->children.size(); ++k)
evaluateTreeNode(aTreeIterator->children.begin() + k, aNode);
} else {
std::string label_name(aTreeIterator->value.begin(),
aTreeIterator->value.end());
std::ostringstream o;
o << "*** " << label_name << std::endl;
throw FastCodeMLFatal(o);
}
}
void Newick::readFile(const char *aFilename) {
// Open the tree file
std::ifstream in(aFilename);
if (!in) {
std::ostringstream o;
o << "Cannot open tree file \"" << aFilename << '"' << std::endl;
throw FastCodeMLFatal(o);
}
// Cleaning step. Should not be necessary after fixing the grammar...
// Find the tree definition start
std::string str;
size_t p1 = std::string::npos;
while (getline(in, str)) {
p1 = str.find_first_of('(');
if (p1 != std::string::npos)
break;
}
in.close();
if (p1 == std::string::npos) {
std::ostringstream o;
o << "File \"" << aFilename
<< "\" is empty (cannot find starting parenthesis)." << std::endl;
throw FastCodeMLFatal(o);
}
// Find the tree definition end
size_t p2 = str.rfind(';');
if (p2 == std::string::npos) {
std::ostringstream o;
o << "File \"" << aFilename << "\" is empty (cannot find ending semicolon)."
<< std::endl;
throw FastCodeMLFatal(o);
}
// Clean blanks (the grammar cannot cope with them and I don't know why)
std::string tmp = str.substr(p1, p2 - p1 + 1);
tmp.erase(std::remove_if(tmp.begin(), tmp.end(), ::isspace), tmp.end());
// Pass the tree part to the parser
// loadTreeFromString(str.substr(p1, p2-p1+1));
loadTreeFromString(tmp);
}
void Newick::loadTreeFromString(const std::string &aTreeAsString) {
// Clear the tree and initialize the parser
mTreeRoot.clearNode();
NewickGrammar tree;
// Parse the tree
tree_parse_info<> info = pt_parse(aTreeAsString.c_str(), tree);
if (info.full) {
mParsedPortion.clear(); // To signal no error
evaluateTreeNode(info.trees.begin(), &mTreeRoot);
// Fill the leaves
mLeavesSpecies.clear();
fillSpecies(&mTreeRoot);
// Fill internal branches
mInternalNodes.clear();
fillInternalBranches(&mTreeRoot);
// Dump the tree
if (mVerboseLevel >= VERBOSE_MORE_DEBUG) {
std::cout << "Tree as read in PhyloTree" << std::endl;
printTree(info.trees.begin());
mTreeRoot.printFormatted();
std::vector<TreeNode *>::const_iterator isp(mLeavesSpecies.begin());
const std::vector<TreeNode *>::const_iterator end(mLeavesSpecies.end());
for (; isp != end; ++isp)
std::cout << (*isp)->getLabel() << std::endl;
}
} else {
mParsedPortion.assign(aTreeAsString.c_str(), info.stop);
std::ostringstream o;
o << "Parsing failed\n"
<< "----------------------\n" << mParsedPortion
<< "\n----------------------" << std::endl
<< std::endl;
throw FastCodeMLFatal(o);
}
}
void Newick::printTreeUnformatted(std::ostream &aOut, TreeNode *aNode) const {
TreeNode *m;
unsigned int idx;
// Special case for the root
if (!aNode) {
aOut << '(';
for (idx = 0; (m = mTreeRoot.getChild(idx)) != NULL; ++idx) {
if (idx > 0)
aOut << ',';
printTreeUnformatted(aOut, m);
}
aOut << ')';
mTreeRoot.printNode();
aOut << ';' << std::endl;
} else if (aNode->isLeaf()) {
aNode->printNode();
} else {
aOut << '(';
for (idx = 0; (m = aNode->getChild(idx)) != NULL; ++idx) {
if (idx > 0)
aOut << ',';
printTreeUnformatted(aOut, m);
}
aOut << ')';
aNode->printNode();
}
}
int Newick::printTreeAnnotated(std::ostream &aOut, TreeNode *aNode, int aBranch,
bool wLeaves, bool bNumber) const {
TreeNode *m;
unsigned int idx;
int branch_idx = aBranch;
// Special case for the root
if (!aNode) {
if (bNumber){
if (wLeaves)
aOut << "Annotated Newick Tree (*N marks the branch N)" << std::endl << std::endl;
else
aOut << "Annotated Newick Tree (*N marks the internal branch N)"
<< std::endl << std::endl;
}
aOut << '(';
for (idx = 0; (m = mTreeRoot.getChild(idx)) != NULL; ++idx) {
if (idx > 0)
aOut << ',';
branch_idx = printTreeAnnotated(aOut, m, branch_idx, wLeaves, bNumber);
}
aOut << ')';
mTreeRoot.printNode();
aOut << ';' << std::endl;
} else if (aNode->isLeaf()) {
if (wLeaves)
branch_idx = aBranch + 1;
aNode->printNode();
if (wLeaves && bNumber)
aOut << '*' << aBranch;
} else {
branch_idx = aBranch + 1;
aOut << '(';
for (idx = 0; (m = aNode->getChild(idx)) != NULL; ++idx) {
if (idx > 0)
aOut << ',';
branch_idx = printTreeAnnotated(aOut, m, branch_idx, wLeaves, bNumber);
}
aOut << ')';
aNode->printNode();
if (bNumber) aOut << '*' << aBranch;
}
return branch_idx;
}
int Newick::printTreeAnnotatedWithEstLens(std::ostream &aOut, TreeNode *aNode,
int aBranch, bool wLeaves,
std::vector<double> *mVar, bool bNumber) const {
TreeNode *m;
unsigned int idx;
int branch_idx = aBranch;
// Special case for the root
if (!aNode) {
if (bNumber){
if (wLeaves)
aOut << "Annotated Newick Tree (*N marks the branch N)" << std::endl << std::endl;
else
aOut << "Annotated Newick Tree (*N marks the internal branch N)"
<< std::endl << std::endl;
}
aOut << '(';
for (idx = 0; (m = mTreeRoot.getChild(idx)) != NULL; ++idx) {
if (idx > 0)
aOut << ',';
branch_idx =
printTreeAnnotatedWithEstLens(aOut, m, branch_idx, wLeaves, mVar, bNumber);
}
aOut << ')';
mTreeRoot.printNodeWoutLen();
// std::cout << std::setprecision(6) << ":" << (*mVar)[aBranch];
aOut << ';' << std::endl;
} else if (aNode->isLeaf()) {
if (wLeaves)
branch_idx = aBranch + 1;
aNode->printNodeWoutLen();
std::cout << std::setprecision(6) << ":" << (*mVar)[aBranch];
if (wLeaves && bNumber)
aOut << '*' << aBranch;
} else {
branch_idx = aBranch + 1;
aOut << '(';
for (idx = 0; (m = aNode->getChild(idx)) != NULL; ++idx) {
if (idx > 0)
aOut << ',';
branch_idx =
printTreeAnnotatedWithEstLens(aOut, m, branch_idx, wLeaves, mVar, bNumber);
}
aOut << ')';
aNode->printNodeWoutLen();
std::cout << std::setprecision(6) << ":" << (*mVar)[aBranch];
if (bNumber) aOut << '*' << aBranch;
}
return branch_idx;
}