-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicTree.cpp
More file actions
executable file
·558 lines (469 loc) · 16.4 KB
/
BasicTree.cpp
File metadata and controls
executable file
·558 lines (469 loc) · 16.4 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/***************************************************************************************************
* The PolyMoSim project is distributed under the following license:
*
* Copyright (c) 2006-2022, Christoph Mayer, Forschungsmuseum Alexander Koenig, Bonn, Germany
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code (complete or in parts) must retain
* the above copyright notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or any use of this software
* e.g. in publications must display the following acknowledgement:
* This product includes software developed by Christoph Mayer, Forschungsmuseum
* Alexander Koenig, Bonn, Germany.
* 4. Neither the name of the organization nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY CHRISTOPH MAYER ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHTHOLDER OR ITS ORGANISATION BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* IMPORTANT (needs to be included, if code is redistributed):
* Please not that this license is not compatible with the GNU Public License (GPL)
* due to paragraph 3 in the copyright. It is not allowed under any
* circumstances to use the code of this software in projects distributed under the GPL.
* Furthermore, it is not allowed to redistribute the code in projects which are
* distributed under a license which is incompatible with one of the 4 paragraphs above.
*
* This project makes use of code coming from other projects. What follows is a complete
* list of files which make use of external code. Please refer to the copyright within
* these files.
*
* Files in tclap foler: Copyright (c) 2003 Michael E. Smoot
* See copyright in tclap/COPYRIGHT file for details.
* discrete_gamma.c: Copyright 1993-2004 by Ziheng Yang.
* See copyright in this file for details.
* CRandom.h: Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura
* See copyright in this file for details.
***************************************************************************************************/
#include "BasicTree.h"
#include "PolyMoSim.h"
using namespace std;
class tree_parser_assistent { // treat file read fail xxxxxxxxxxxxxxxxxxxxxxxx
private:
istream& is;
// int last_char;
vector<faststring> special_comments_vec;
public:
tree_parser_assistent(istream& pis):is(pis) {}
int getchar(bool look_for_special_comments = false) {
int ch;
ignore_spaces(is);
ch = is.get();
while (ch == '[' ) {
if (look_for_special_comments)
{
ch = is.get();
if (ch == '&')
{
faststring s;
ignore_spaces(is);
ch = is.get();
while (ch != EOF && ch != ']' ) {
s.push_back(ch);
ch = is.get();
}
// remove trailing spaces from s xxxxxxxxxxxxxxxxxxxxxxxxxx
special_comments_vec.push_back(s);
}
else {
// is.unget();
skip_comment(is);
}
}
else {
skip_comment(is);
}
ignore_spaces(is);
ch = is.get();
}
return ch;
}
void ungetchar()
{
is.unget();
}
int getrawchar()
{
return is.get();
}
void clear_special_comments()
{
special_comments_vec.clear();
}
faststring get_special_comment()
{
if (special_comments_vec.size() != 1)
return "";
else
return special_comments_vec[0];
}
int get_number_of_special_comments()
{
return (int) special_comments_vec.size();
}
double read_double() // treat file read fail xxxxxxxxxxxxxxxxxxxxxxxx
{
double lf;
is >> lf;
return lf;
}
int read_int()
{
int d;
is >> d;
return d;
}
};
BasicTree::BasicTree() {
treeroot = NULL;
OTU = 0;
nodes = 0;
read_model_status = false;
root_model = NULL; // "default";
}
BasicTree::~BasicTree() {
if(treeroot)
destroy_subtree(treeroot);
}
void BasicTree::read_tree(istream& is, double s) { // istream can also be a stringstream
int ch;
tree_parser_assistent tpa(is);
scalefactor = s;
ch = tpa.getchar(); // ignores leading spaces and comments, ignore special comments
if(ch == '(' )
treeroot = new_node(tpa);
}
void BasicTree::push_child(BasicNode *parent, BasicNode *child) {
parent->push_back_child(child);
child->set_parent(parent);
}
// Can it parse that one: ??
// ( ## new_node: root, 2 children
// ( ## new_node: d, 1 child
// ( ## new_node: c, 1 child
// ( ## new_node: b, 1 child
// a:0.1 ## new_OTU: a, 0 children
// )b:0.1
// )c:0.1
// )d:0.1
// ,
// A:0.3 ## new_OTU: A, 0 children
// )root ## End of tree
//
//
// If an OTU has braces arround it (a:0.1) this is treated as 2 nodes
// equivalent to (a:0.1):0.
BasicNode* BasicTree::new_node(tree_parser_assistent &tpa) {
nodes++;
int children = 0;
BasicNode *newnode;
BasicNode *newchild = NULL;
int ch;
newnode = new BasicNode;
do {
ch = tpa.getchar(); // ignores spaces and comments in front of a node
if (ch == '(' ) {
++children;
newchild = new_node(tpa);
push_child(newnode,newchild);
newchild = NULL;
} else {
++children;
tpa.ungetchar();
newchild = new_OTU(tpa);
push_child(newnode, newchild);
newchild = NULL;
}
ch = tpa.getchar(); // ignores spaces and comments behind node
} while (ch == ',' );
if (ch == ')' ) {
/* ch = mygetchar(is);
if(ch == '(') {
is.unget();
newchild = new_node(is);
push_child(newnode->get_parent(), newchild);
newchild = NULL;
}else{*/
get_name_and_branchlength(tpa, newnode);
if (read_model_status) {
read_model_name(tpa, newnode);
}
}
return newnode;
}
BasicNode* BasicTree::new_OTU(tree_parser_assistent& tpa) {
BasicNode *newnode;
newnode = new BasicNode;
get_name_and_branchlength(tpa, newnode);
if (read_model_status) {
read_model_name(tpa, newnode);
}
// save OTU as a taxon in the Taxa-Class
taxa_list.add_taxon( newnode->get_name() );
++OTU;
++nodes; // An OTU is also a node xxxx
return newnode;
}
void BasicTree::get_name_and_branchlength(tree_parser_assistent &tpa, BasicNode *node) {
int ch;
get_name(tpa, node);
ch = tpa.getchar(true); // Check whether special comment has been found
if(ch == ':' ) {
node->set_branchlength(tpa.read_double() );
} else {
// default branch length is 0
tpa.ungetchar();
}
}
void BasicTree::get_name(tree_parser_assistent &tpa, BasicNode *node) {
int ch;
ch = tpa.getchar(true); // Check whether special comment has been found
// if (tpa.get_number_of_special_comments() > 0 )
// {
// tpa.ungetchar();
// return;
// }
if (ch == '"' ) {
do {
node->push_back_to_name(ch);
ch = tpa.getrawchar(); // No true comments are allowed in "..."
} while (ch != EOF && ch != '"' );
if (ch != EOF) node->push_back_to_name(ch);
} else if (ch == EOF) {
tpa.ungetchar();
} else {
while (ch != EOF && !isspace(ch) && ch != ':' && ch != '(' && ch != '[' && ch != ',' && ch != ']' && ch != ')' && ch != ';' ) {
node->push_back_to_name(ch);
ch = tpa.getrawchar(); // comments and spaces are not allowed within the node name
}
tpa.ungetchar();
}
}
void BasicTree::read_model_name(tree_parser_assistent &tpa, BasicNode* node) {
tpa.getchar(true);
tpa.ungetchar();
if (tpa.get_number_of_special_comments() == 0)
{
return;
}
if (tpa.get_number_of_special_comments() > 1)
{
// Error xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
cerr << "Found multiple models on one node. Could be an internal problem. Please report this problem." << endl;
exit(0);
// return;
}
faststring s = tpa.get_special_comment();
tpa.clear_special_comments();
// if ( tolower(s[0]) == 'm')
{
node->get_model_name() = s;
node->set_new_model_status(true);
}
// else
// return;
}
void BasicTree::link_models_to_tree(model_admin *ma, const basic_model *r_model) {
model_master = ma;
root_model = r_model;
link_next_model(treeroot);
}
// When reading the tree, some nodes had been assigned a new model, which so far is only
// saved in the node in form of the model name.
// This recursive routine does the following:
// - nodes which are not marked as "new model" get the model name of their parent. So all nodes get the correct model name.
// - all nodes get a pointer their model object.
void BasicTree::link_next_model(BasicNode* node) {
const basic_model *temp;
BasicNode *parent;
faststring dummy;
if (node->get_parent() == NULL) { // root
if ( node->get_new_model_status() ) {
dummy = node->get_model_name();
temp = model_master->get_model(dummy);
// Check correct data type for each new model:
if ( root_model->get_datatype() != temp->get_datatype() )
{
cerr << "Error: The data type of the model on this node is not compatible with the data type of the sequences that evolves here." << endl;
throw 1;
}
}
else
{
temp = root_model;
}
}
else if (node->get_new_model_status() ) {
dummy = node->get_model_name();
temp = model_master->get_model(dummy);
// Check correct data type for each new model:
if ( root_model->get_datatype() != temp->get_datatype() ) {
cerr << "Error: The data type of the model on this node is not compatible with the data type of the sequences that evolves here." << endl;
throw 1;
}
}
else
{
parent = node->get_parent();
temp = parent->get_model();
}
node->set_model(temp);
for (unsigned i = 0; i < node->get_num_children(); i++) {
link_next_model(node->get_childlist()[i]);
}
}
void BasicTree::evolve_tree() {
evolve_next_node(treeroot);
}
void BasicTree::evolve_next_node(BasicNode* node) {
// char* start_pos_new;
// char* end_pos_new;
// faststring new_seq;
double br_length;
if (node->get_parent() == NULL)
{
if (global_verbosity >= 100)
{
cerr << "Wurzel: " << endl; cerr << node->get_sequence() << endl << endl;
}
}
else
{
faststring &parent_seq = node->get_parent()->get_sequence();
faststring &new_seq = node->get_sequence();
// start_pos_new = node->get_sequence().begin();
// end_pos_new = node-> get_sequence().end();
// char * blub;
if (global_verbosity >= 200)
{
cerr << "scalefactor: " << scalefactor << endl;
cerr << "node->get_branchlength(): " << node->get_branchlength() << endl;
}
br_length = node->get_branchlength()*scalefactor;
node->get_model()->evolve(parent_seq, new_seq, br_length);
if (global_verbosity >= 200)
{
cerr << "Evolve branch before node:" << endl;
cerr << "Node name: " << node->get_name() << endl;
cerr << "Model name: " << node->get_model()->get_modelname() << endl;
cerr << "Node: " << node->get_name() << endl; cerr << "Evolved seq.: " << new_seq << endl;
}
}
for(unsigned i = 0; i < node->get_num_children(); ++i) {
evolve_next_node(node->get_childlist()[i]);
}
}
//This recursive routine finds the 2n-3 splits of the tree.
//It must get a split_admin object as a parameter which is then allocated with all splits of the tree.
void BasicTree::get_tree_splits(split_admin& sp_ad) {
// Wurzel muss vorerst nicht besonders behandelt werden
for(unsigned i = 0; i < treeroot->get_num_children(); i++) {
get_next_split(sp_ad, treeroot->get_childlist()[i]);
}
}
void BasicTree::get_next_split(split_admin& sp_ad, BasicNode* node) {
BSplit *temp;
unsigned tax_pos;
// Neuer Split - auf Vektor-Stack
temp = new BSplit(taxa_list.GetTaxaNum());
split_vec.push_back(*temp);
// Hat keine Kinder
// - Taxon Nummer beschaffen
// - Bit in allen splits auf Vektor-Stack setzten
if( node->get_num_children() == 0 ) { // Hat keine Kinder
tax_pos = taxa_list.GetTaxonPosition( node->get_name() );
DEBUGCODE( cerr << "neuer Taxon Name: " << node->get_name() << endl; )
DEBUGCODE( cerr << "neue Bits: " << endl; )
for(unsigned j = 0; j < split_vec.size(); j++) {
split_vec[j].get_split().set(tax_pos);
DEBUGCODE( cerr << j << ": " << split_vec[j].get_split() << endl; )
}
}
// Hat Kinder
for(unsigned i = 0; i < node->get_num_children(); i++) {
get_next_split(sp_ad, node->get_childlist()[i]);
}
sp_ad.add_split(split_vec[split_vec.size() - 1].get_split(), node->get_branchlength() );
DEBUGCODE( cerr << "Neuer Split auf split_admin: " << split_vec[split_vec.size() - 1].get_split() << endl; )
split_vec.pop_back();
}
void BasicTree::output(ostream& os, unsigned flag) {
if (flag == 1) // Debug mode
{
faststring root_model_name;
if (root_model != NULL)
root_model_name = root_model->get_modelname();
else
root_model_name = "Not specified jet.";
os << " scalefactor: " << scalefactor
<< " OTUs: " << OTU
<< " nodes: " << nodes
<< " read_model_status: " << read_model_status
<< " root_model: " << root_model_name << endl;
}
output_node(treeroot, os);
}
void BasicTree::output_node(BasicNode *node, ostream& os) {
if(node->get_num_children() != 0) {
os << "(";
}
for(unsigned i = 0; i < node->get_num_children(); i++) {
output_node(node->get_childlist()[i], os);
if(i < node->get_num_children() - 1) {
os << ",";
}
}
if(node->get_num_children() != 0) {
os << ")";
}
os << node->get_name() << ":" << node->get_branchlength();
if ( read_model_status && node->get_model_name() != "")
os << "[&" << node->get_model_name() << "]";
}
void BasicTree::destroy_subtree(BasicNode *node) {
unsigned i;
for(i=0; i < node->get_num_children() ; ++i)
destroy_subtree(node->get_childlist()[i]);
delete node;
}
// void BasicTree::set_root_model(faststring r_m) {
// root_model = r_m;
// }
const basic_model* BasicTree::get_root_model() {
return root_model;
}
void BasicTree::get_map_of_OTUs(map_of_OTUs &m) {
get_map_of_OTUs_intern(m, treeroot);
}
void BasicTree::get_map_of_OTUs_intern(map_of_OTUs &m, BasicNode *node) {
if(node->get_num_children() == 0) {
m.insert(make_pair(&node->get_name(), &node->get_sequence()) );
}
else for (unsigned i = 0; i < node->get_num_children(); ++i) {
get_map_of_OTUs_intern(m, node->get_childlist()[i]);
}
}
void BasicTree::get_set_of_OTUs(set_of_OTUs &m) {
get_set_of_OTUs_intern(m, treeroot);
}
void BasicTree::get_set_of_OTUs_intern(set_of_OTUs &m, BasicNode *node) {
if(node->get_num_children() == 0) {
m.insert( &node->get_name() );
}
else for (unsigned i = 0; i < node->get_num_children(); ++i) {
get_set_of_OTUs_intern(m, node->get_childlist()[i]);
}
}