| 685 | } |
| 686 | |
| 687 | String ClusterAnalyzer::newickTree(const std::vector<BinaryTreeNode> & tree, const bool include_distance) |
| 688 | { |
| 689 | std::set<Size> leafs; |
| 690 | for (Size i = 0; i < tree.size(); ++i) |
| 691 | { |
| 692 | leafs.insert(tree[i].left_child); |
| 693 | leafs.insert(tree[i].right_child); |
| 694 | } |
| 695 | |
| 696 | std::vector<String> clusters(*(leafs.rbegin()) + 1, ""); |
| 697 | for (std::set<Size>::iterator it = leafs.begin(); it != leafs.end(); ++it) |
| 698 | { |
| 699 | clusters[*it] = String(*it); |
| 700 | } |
| 701 | |
| 702 | //redo clustering till step (original.dimensionsize()-1) |
| 703 | for (Size cluster_step = 0; cluster_step < tree.size(); ++cluster_step) |
| 704 | { |
| 705 | //append string right_child to left_child |
| 706 | clusters[tree[cluster_step].left_child].insert(0, "( "); |
| 707 | if (include_distance) |
| 708 | { |
| 709 | clusters[tree[cluster_step].left_child] += ":"; |
| 710 | clusters[tree[cluster_step].left_child] += String(tree[cluster_step].distance); |
| 711 | } |
| 712 | clusters[tree[cluster_step].left_child] += " , "; |
| 713 | clusters[tree[cluster_step].left_child] += clusters[tree[cluster_step].right_child]; |
| 714 | if (include_distance) |
| 715 | { |
| 716 | clusters[tree[cluster_step].left_child] += ":"; |
| 717 | clusters[tree[cluster_step].left_child] += String(tree[cluster_step].distance); |
| 718 | } |
| 719 | clusters[tree[cluster_step].left_child] += " )"; |
| 720 | |
| 721 | clusters[tree[cluster_step].right_child] = String(""); |
| 722 | } |
| 723 | |
| 724 | Size first_filled(0); |
| 725 | for (Size i = 0; i < clusters.size(); ++i) |
| 726 | { |
| 727 | if (!clusters[i].empty()) |
| 728 | { |
| 729 | first_filled = i; |
| 730 | break; |
| 731 | } |
| 732 | } |
| 733 | for (Size i = first_filled + 1; i < clusters.size(); ++i) |
| 734 | { |
| 735 | if (!clusters[i].empty()) |
| 736 | { |
| 737 | clusters[first_filled].insert(0, "( "); |
| 738 | if (include_distance) |
| 739 | { |
| 740 | clusters[first_filled] += ":"; |
| 741 | clusters[first_filled] += String("1"); |
| 742 | } |
| 743 | clusters[first_filled] += " , "; |
| 744 | clusters[first_filled] += clusters[i]; |