root/trunk/CONSENSUS_TREE/CT_ctree.hxx

Revision 8585, 3.9 KB (checked in by westram, 6 weeks ago)
  • moved code from arb_consensus_tree -> class ConsensusTreeBuilder
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1// ============================================================= //
2//                                                               //
3//   File      : CT_ctree.hxx                                    //
4//   Purpose   :                                                 //
5//                                                               //
6//   Institute of Microbiology (Technical University Munich)     //
7//   http://www.arb-home.de/                                     //
8//                                                               //
9// ============================================================= //
10
11#ifndef CT_CTREE_HXX
12#define CT_CTREE_HXX
13
14#ifndef ARBTOOLS_H
15#include <arbtools.h>
16#endif
17#ifndef ARBDBT_H
18#include <arbdbt.h>
19#endif
20#ifndef ARB_STRARRAY_H
21#include <arb_strarray.h>
22#endif
23#ifndef _GLIBCXX_MAP
24#include <map>
25#endif
26#ifndef _GLIBCXX_VECTOR
27#include <vector>
28#endif
29#ifndef _GLIBCXX_STRING
30#include <string>
31#endif
32
33class PART;
34class NT_NODE;
35class PartitionSize;
36class PartRegistry;
37
38class ConsensusTree : virtual Noncopyable {
39    double   overall_weight;
40    GB_HASH *Name_hash;
41
42    PartitionSize *size;
43    PartRegistry  *registry;
44
45    const CharPtrArray& names;
46
47    PART *dtree(const GBT_TREE *tree, double weight, GBT_LEN len);
48    void remember_subtrees(const GBT_TREE *tree, double weight);
49
50    int get_species_index(const char *name) const {
51        int idx = GBS_read_hash(Name_hash, name);
52        arb_assert(idx>0); // given 'name' is unknown
53        return idx-1;
54    }
55
56    const char *get_species_name(int idx) const {
57        return names[idx];
58    }
59
60    class RB_INFO *rbtree(const NT_NODE *tree, GBT_TREE *father);
61    GBT_TREE      *rb_gettree(const NT_NODE *tree);
62
63
64public:
65    ConsensusTree(const CharPtrArray& names_);
66    ~ConsensusTree();
67
68    void insert(GBT_TREE *tree, double weight);
69
70    GBT_TREE *get_consensus_tree();
71
72};
73
74
75class ConsensusTreeBuilder {
76    // wrapper for ConsensusTree
77    // - automatically collects species occurring in added trees (has to be done by caller of ConsensusTree)
78    // - uses much more memory than using ConsensusTree directly, since it stores all added trees
79    //
80    // Not helpful for consensing thousands of trees like bootstrapping does
81
82    typedef std::map<std::string, size_t> OccurCount;
83    typedef std::vector<GBT_TREE*>        Trees;
84    typedef std::vector<double>           Weights;
85
86    OccurCount species_occurred;
87    Trees      trees;
88    Weights    weights;
89
90public:
91    ~ConsensusTreeBuilder() {
92        for (size_t i = 0; i<trees.size(); ++i) {
93            GBT_delete_tree(trees[i]);
94        }
95    }
96
97    void add(GBT_TREE*& tree, double weight) {
98        trees.push_back(tree);
99        weights.push_back(weight);
100
101        size_t   name_count;
102        GB_CSTR *names = GBT_get_names_of_species_in_tree(tree, &name_count);
103
104        for (size_t n = 0; n<name_count; ++n) {
105            const char *name = names[n];
106            if (species_occurred.find(name) == species_occurred.end()) {
107                species_occurred[name] = 1;
108            }
109            else {
110                species_occurred[name]++;
111            }
112        }
113
114        free(names);
115        tree = NULL;
116    }
117
118    GBT_TREE *get(size_t& different_species) {
119        ConstStrArray species_names;
120
121        for (OccurCount::iterator s = species_occurred.begin(); s != species_occurred.end(); ++s) {
122            species_names.put(s->first.c_str());
123        }
124
125        different_species = species_occurred.size();
126        ConsensusTree ctree(species_names);
127        for (size_t i = 0; i<trees.size(); ++i) {
128            ctree.insert(trees[i], weights[i]);
129        }
130
131#if defined(DEBUG)
132        // if class ConsensusTree does depend on any local data,
133        // instanciating another instance will interfere:
134        ConsensusTree influence(species_names);
135#endif
136
137        return ctree.get_consensus_tree();
138    }
139};
140
141
142#else
143#error CT_ctree.hxx included twice
144#endif // CT_CTREE_HXX
Note: See TracBrowser for help on using the browser.