-
Notifications
You must be signed in to change notification settings - Fork 3
/
utree.h
599 lines (541 loc) · 15.6 KB
/
utree.h
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/*******************************************************************************
utree.h
Unrooted tree data structure
Copyright 2018 Chris Whidden
cwhidden@fredhutch.org
https://github.com/cwhidden/uspr
May 1, 2018
Version 1.0.1
This file is part of uspr.
uspr is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
uspr is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with uspr. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
#ifndef INCLUDE_UTREE
#define INCLUDE_UTREE
#include <vector>
#include <iostream>
#include "unode.h"
using namespace std;
class utree;
// options
bool KEEP_LABELS = false;
// prototypes
bool build_utree(utree &t, string &s, map<string, int> *label_map = NULL, map<int, string> *reverse_label_map = NULL);
int build_utree_helper(utree &t, string &s, int start, unode *parent, bool &valid, map<string, int> *label_map = NULL, map<int, string> *reverse_label_map = NULL);
void find_sibling_pairs_hlpr(utree &t, map<int, int> &sibling_pairs);
map<int, int> distances_from_leaf(utree &T1, int leaf);
void distances_from_leaf_hlpr(utree &T1, map<int, int> &distances, unode *prev, unode *current, int distance);
void distances_from_leaf_decorator(utree &T1, int leaf);
void distances_from_leaf_decorator_hlpr(utree &T1, unode *prev, unode *current, int distance);
class utree {
protected:
vector <unode*> internal_nodes;
vector <unode*> leaves;
int smallest_leaf;
public:
// create the tree
utree(string &newick, map<string, int> *label_map = NULL, map<int, string> *reverse_label_map = NULL) {
internal_nodes = vector<unode *>();
leaves = vector<unode *>();
build_utree(*this, newick, label_map, reverse_label_map);
}
utree(const utree &T) {
// copy vectors of pointers
int internal_nodes_size = T.internal_nodes.size();
int leaves_size = T.leaves.size();
internal_nodes = vector<unode *>(internal_nodes_size);
leaves = vector<unode *>(leaves_size);
smallest_leaf = T.smallest_leaf;
// create new nodes
for(int i = 0; i < internal_nodes_size; i++) {
if (T.internal_nodes[i] != NULL) {
internal_nodes[i] = new unode(*(T.internal_nodes[i]), false);
}
}
for(int i = 0; i < leaves_size; i++) {
if (T.leaves[i] != NULL) {
leaves[i] = new unode(*(T.leaves[i]), false);
}
}
// update neighbor pointers
for(int i = 0; i < internal_nodes_size; i++) {
if (internal_nodes[i] != NULL) {
list<unode *> &old_neighbors = T.internal_nodes[i]->get_neighbors();
for (unode *u : old_neighbors) {
internal_nodes[i]->add_neighbor(get_node(u->get_label()));
}
list<unode *> &old_contracted_neighbors = T.internal_nodes[i]->get_contracted_neighbors();
for (unode *u : old_contracted_neighbors) {
internal_nodes[i]->add_contracted_neighbor(get_node(u->get_label()));
}
}
}
for(int i = 0; i < leaves_size; i++) {
if (leaves[i] != NULL) {
list<unode *> &old_neighbors = T.leaves[i]->get_neighbors();
for (unode *u : old_neighbors) {
leaves[i]->add_neighbor(get_node(u->get_label()));
}
list<unode *> &old_contracted_neighbors = T.leaves[i]->get_contracted_neighbors();
for (unode *u : old_contracted_neighbors) {
leaves[i]->add_contracted_neighbor(get_node(u->get_label()));
}
}
}
}
~utree() {
int end = internal_nodes.size();
for(int i = 0; i < end; i++) {
if (internal_nodes[i] != NULL) {
delete internal_nodes[i];
}
}
end = leaves.size();
for(int i = 0; i < end; i++) {
if (leaves[i] != NULL) {
delete leaves[i];
}
}
}
utree& operator=(utree T) {
swap(*this, T);
return *this;
}
friend void swap(utree &first, utree &second) {
swap(first.internal_nodes, second.internal_nodes);
swap(first.leaves, second.leaves);
swap(first.smallest_leaf, second.smallest_leaf);
}
friend ostream& operator<<(ostream &os, const utree& t);
int add_internal_node() {
int label = -(internal_nodes.size() + 2);
internal_nodes.push_back( new unode(label));
return label;
}
int add_leaf(int label) {
int start = leaves.size();
if (leaves.size() <= label) {
leaves.resize(label+1);
}
for(int i = start; i < label; i++) {
leaves[i] = NULL;
}
leaves[label] = new unode(label);
return label;
}
int add_phi_node() {
int new_node_label = add_leaf(leaves.size());
unode *new_node = get_node(new_node_label);
new_node->set_phi(true);
return new_node_label;
}
unode *get_internal_node(int label) const {
return internal_nodes[-(label) - 2];
}
vector<unode *> &get_internal_nodes() {
return internal_nodes;
}
vector<unode *> &get_leaves() {
return leaves;
}
list<unode *> get_node_list() {
list<unode *> L = list<unode *>();
for (unode *i: leaves) {
if (i != NULL) {
L.push_back(i);
}
}
for (unode *i: internal_nodes) {
if (i != NULL) {
L.push_back(i);
}
}
return L;
}
unode *get_leaf(int label) const {
return leaves[label];
}
unode *get_node(int label) const {
if (label < 0) {
return get_internal_node(label);
}
else {
return get_leaf (label);
}
}
int num_leaves() const {
return leaves.size();
}
string str(bool print_internal = false, map<int, string> *reverse_label_map = NULL) const{
stringstream s;
int start = smallest_leaf;
if (start == -1) {
return "empty tree";
}
unode *root = leaves[start]->get_neighbors().front();
str_subtree(s, root, root, print_internal, reverse_label_map);
return s.str();
}
string str(int start, string contracted_sep = ",", bool print_internal = false, map<int, string> *reverse_label_map = NULL) const{
stringstream s;
if (start == -1) {
return "empty tree";
}
unode *root = get_node(start);
str_subtree(s, root, root, contracted_sep, print_internal, reverse_label_map);
return s.str();
}
list<int> find_leaves() {
list<int> leaf_list = list<int>();
for (unode *i : leaves) {
if (i != NULL) {
leaf_list.push_back(i->get_label());
}
}
return leaf_list;
}
map<int, int> find_sibling_pairs() {
map<int, int> sibling_pairs = map<int, int>();
find_sibling_pairs_hlpr(*this, sibling_pairs);
return sibling_pairs;
}
void set_smallest_leaf(int l) {
smallest_leaf = l;
}
int get_smallest_leaf() {
return smallest_leaf;
}
void root() {
root(smallest_leaf);
}
void root(int l) {
unode *n = get_node(l);
if (n != NULL) {
n->root(n->get_label());
}
}
void root(unode *n) {
if (n != NULL) {
n->root(n->get_label());
}
}
string str_subtree(unode *n, bool print_internal_labels = false, map<int, string> *reverse_label_map = NULL) {
stringstream ss;
str_subtree(ss, n, n->get_parent(), print_internal_labels, reverse_label_map);
return ss.str();
}
string str_subtree(unode *n, unode *p, bool print_internal_labels = false, map<int, string> *reverse_label_map = NULL) {
stringstream ss;
str_subtree(ss, n, p, print_internal_labels, reverse_label_map);
return ss.str();
}
string str_subtree(unode *n, unode *p, string contracted_sep, bool print_internal_labels = false, map<int, string> *reverse_label_map = NULL) {
stringstream ss;
str_subtree(ss, n, p, contracted_sep, print_internal_labels, reverse_label_map);
return ss.str();
}
void str_subtree(stringstream &s, unode *n, unode *prev, bool print_internal_labels = false, map<int, string> *reverse_label_map = NULL) const {
// only leaf labels
if (print_internal_labels || n->get_label() >= 0) {
s << n->str(reverse_label_map);
}
list<unode *>::const_iterator i;
const list<unode *> &cn = n->const_neighbors();
int count = 0;
bool has_contracted = false;
for(unode *i : n->const_neighbors()) {
if (prev == NULL || (*i).get_label() != prev->get_label()) {
if (count == 0) {
s << "(";
}
else {
s << ",";
}
count++;
str_subtree(s, i, n, print_internal_labels, reverse_label_map);
}
}
for(unode *i : n->const_contracted_neighbors()) {
if (prev == NULL || (*i).get_label() != prev->get_label()) {
if (count == 0) {
s << "<";
}
else {
s << ",";
}
count++;
has_contracted = true;
str_subtree(s, i, n, print_internal_labels, reverse_label_map);
}
}
if (has_contracted) {
s << ">";
}
else if (count > 0) {
s << ")";
}
}
void str_subtree(stringstream &s, unode *n, unode *prev, string contracted_sep, bool print_internal_labels = false, map<int, string> *reverse_label_map = NULL) const {
// only leaf labels
if (print_internal_labels || n->get_label() >= 0) {
s << n->str(reverse_label_map);
}
list<unode *>::const_iterator i;
const list<unode *> &cn = n->const_neighbors();
int count = 0;
bool has_contracted = false;
for(unode *i : n->const_neighbors()) {
if (prev == NULL || (*i).get_label() != prev->get_label()) {
if (count == 0) {
s << "(";
}
else {
s << ",";
}
count++;
str_subtree(s, i, n, contracted_sep, print_internal_labels, reverse_label_map);
}
}
for(unode *i : n->const_contracted_neighbors()) {
if (prev == NULL || (*i).get_label() != prev->get_label()) {
if (count == 0) {
s << "<";
}
else {
s << contracted_sep;
}
count++;
has_contracted = true;
str_subtree(s, i, n, contracted_sep, print_internal_labels, reverse_label_map);
}
}
if (has_contracted) {
s << ">";
}
else if (count > 0) {
s << ")";
}
}
void str_subtree_with_depths(stringstream &s, unode *n, unode *prev, bool print_internal_labels = false) const {
// only leaf labels
if (print_internal_labels || n->get_label() >= 0) {
s << n->str();
}
list<unode *>::const_iterator i;
const list<unode *> &cn = n->const_neighbors();
int count = 0;
bool has_contracted = false;
for(unode *i : n->const_neighbors()) {
if (prev == NULL || (*i).get_label() != prev->get_label()) {
if (count == 0) {
s << "(";
}
else {
s << ",";
}
count++;
str_subtree_with_depths(s, i, n, print_internal_labels);
}
}
for(unode *i : n->const_contracted_neighbors()) {
if (prev == NULL || (*i).get_label() != prev->get_label()) {
if (count == 0) {
s << "<";
}
else {
s << ",";
}
count++;
has_contracted = true;
str_subtree_with_depths(s, i, n, print_internal_labels);
}
}
if (has_contracted) {
s << ">";
}
else if (count > 0) {
s << ")";
}
s << ":" << n->get_distance();
}
void normalize_order() {
get_node(get_smallest_leaf())->get_parent()->normalize_order();
}
void normalize_order(int n) {
get_node(n)->normalize_order();
}
// apply a USPR operation moving (x,y) to (x,yprime) where yprime is adjacent to x, w, and z
bool uspr(unode *x, unode *y, unode *w, unode *z, unode **yprime = NULL, unode **y1 = NULL, unode **y2 = NULL) {
// y must have 3 neighbors
if (y->get_num_neighbors() != 3) {
return false;
}
// remove (x,y)
x->remove_neighbor(y);
y->remove_neighbor(x);
// remove y's first other neighbor
unode *y1_real = y->get_neighbors().front();
y->remove_neighbor(y1_real);
y1_real->remove_neighbor(y);
if (y1 != NULL) {
*y1 = y1_real;
}
unode *y2_real = y->get_neighbors().front();
y->remove_neighbor(y2_real);
y2_real->remove_neighbor(y);
if (y2 != NULL) {
*y2 = y2_real;
}
// connect y's previous neighbors
y1_real->add_neighbor(y2_real);
y2_real->add_neighbor(y1_real);
//remove (w,z)
w->remove_neighbor(z);
z->remove_neighbor(w);
// add the nodes adjacent to y
y->add_neighbor(x);
x->add_neighbor(y);
y->add_neighbor(w);
w->add_neighbor(y);
y->add_neighbor(z);
z->add_neighbor(y);
if (yprime != NULL) {
*yprime = y;
}
// cleanup the tree
// TODO: optional? this is probably slow
// unode *root = get_node(get_smallest_leaf());
// distances_from_leaf_decorator(this, root);
// normalize_order();
return true;
}
};
ostream& operator<<(ostream &os, const utree& t) {
os << t.str() << ";";
return os;
}
bool build_utree(utree &t, string &s, map<string, int> *label_map, map<int, string> *reverse_label_map) {
bool valid = true;
unode dummy = unode(-1);
build_utree_helper(t, s, 0, &dummy, valid, label_map, reverse_label_map);
unode *root = dummy.get_parent();
root->remove_neighbor(&dummy);
root->contract();
int end = t.num_leaves();
int start = -1;
for(int i = 0; i < end; i++) {
if (t.get_leaf(i) != NULL) {
start = i;
break;
}
}
t.set_smallest_leaf(start);
return valid;
}
int build_utree_helper(utree &t, string &s, int start, unode *parent, bool &valid, map<string, int> *label_map, map<int, string> *reverse_label_map) {
// next special char
int loc = s.find_first_of("(,)", start);
if (loc == string::npos) {
return loc;
}
if (s[loc] != '(') {
// leaf
while(s[start] == ' ' || s[start] == '\t')
start++;
int end = loc;
while(s[end] == ' ' || s[end] == '\t')
end--;
string name = s.substr(start, end - start);
int label = -1;
if (label_map != NULL) {
map<string, int>::iterator m = label_map->find(name);
if (m != label_map->end()) {
label = m->second;
}
else {
label = label_map->size();
if (KEEP_LABELS) {
label = atoi(name.c_str());
}
label_map->insert(make_pair(name, label));
reverse_label_map->insert(make_pair(label, name));
}
}
else {
// auto keep labels when no label map is given
label = atoi(name.c_str());
}
unode *new_node = t.get_leaf(t.add_leaf(label));
parent->add_neighbor(new_node);
new_node->add_neighbor(parent);
}
else {
// internal node
int l = t.add_internal_node();
unode *new_node = t.get_internal_node(l);
loc = build_utree_helper(t, s, loc + 1, new_node, valid, label_map, reverse_label_map);
while(s[loc] == ',') {
loc = build_utree_helper(t, s, loc + 1, new_node, valid, label_map, reverse_label_map);
}
if (s[loc] != ')') {
valid = false;
return s.size()-1;
}
// if (parent->get_label() != -1) {
new_node->add_neighbor(parent);
parent->add_neighbor(new_node);
// }
loc++;
}
return loc;
}
void find_sibling_pairs_hlpr(utree &t, map<int, int> &sibling_pairs) {
int i = 3;
for(int l : t.find_leaves()) {
unode *n = t.get_leaf(l);
unode *p = n->get_neighbors().front();
for (unode *u : p->get_neighbors()) {
int ul = u->get_label();
if (u->is_leaf() && ul > l) {
sibling_pairs.insert(make_pair(l,ul));
sibling_pairs.insert(make_pair(ul,l));
}
}
}
return;
}
map<int, int> distances_from_leaf(utree &T1, int leaf) {
map<int, int> distances = map<int, int>();
unode *node = T1.get_leaf(leaf);
distances_from_leaf_hlpr(T1, distances, node, node, 0);
return distances;
}
void distances_from_leaf_hlpr(utree &T1, map<int, int> &distances, unode *prev, unode *current, int distance) {
distances.insert(make_pair(current->get_label(), distance));
for(unode *n : current->get_neighbors()) {
if (n != prev) {
distances_from_leaf_hlpr(T1, distances, current, n, distance+1);
}
}
}
void distances_from_leaf_decorator(utree &T1, int leaf) {
unode *node = T1.get_leaf(leaf);
distances_from_leaf_decorator_hlpr(T1, node, node, 0);
}
void distances_from_leaf_decorator_hlpr(utree &T1, unode *prev, unode *current, int distance) {
current->set_distance(distance);
for(unode *n : current->get_neighbors()) {
if (n != prev) {
distances_from_leaf_decorator_hlpr(T1, current, n, distance+1);
}
}
}
#endif