root/trunk/CONSENSUS_TREE/CT_part.hxx

Revision 8507, 5.0 KB (checked in by westram, 2 months ago)
  • use double for weights ([0..1] instead of [0..10000])
  • added tests using weights != 1
    • fixed weight/length calculation to make generated consense tree independent from used weight (as long as the same weight is used for all added trees)
  • no longer generates zero-bootstrap at root
    • strictly speaking it is wrong now (since the edge from virtual root to tree cant exist, ergo it's probability should be zero)
    • practically
      • that zero probability would be a special case (and is an error at all other edges)
      • a probability of 100% is used (which gets skipped when saving the tree)
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1// ============================================================= //
2//                                                               //
3//   File      : CT_part.hxx                                     //
4//   Purpose   :                                                 //
5//                                                               //
6//   Institute of Microbiology (Technical University Munich)     //
7//   http://www.arb-home.de/                                     //
8//                                                               //
9// ============================================================= //
10
11#ifndef CT_PART_HXX
12#define CT_PART_HXX
13
14#ifndef ARBTOOLS_H
15#include <arbtools.h>
16#endif
17#ifndef ARBDB_BASE_H
18#include <arbdb_base.h>
19#endif
20#ifndef CT_DEF_HXX
21#include "CT_def.hxx"
22#endif
23#ifndef CT_MEM_HXX
24#include "CT_mem.hxx"
25#endif
26#ifndef ARB_ASSERT_H
27#include <arb_assert.h>
28#endif
29
30
31typedef unsigned int PELEM;
32class PART;
33
34class PartitionSize {
35    PELEM cutmask;  // this mask is used to zero unused bits from the last long (in PART::p)
36    int   longs;    // number of longs per part
37    int   bits;     // number of bits per part ( = overall number of species)
38   
39    mutable size_t id;      // unique id (depending on part creation order, which depends on the added trees)
40
41public:
42    PartitionSize(int len);
43
44    int get_longs() const { return longs; }
45    int get_bits() const { return bits; }
46    size_t get_unique_id() const { id++; return id; }
47    PELEM get_cutmask() const { return cutmask; }
48
49    PART *create_root() const;
50    PELEM *alloc_mem() const { return (PELEM*)getmem(get_longs()*sizeof(PELEM)); }
51};
52
53class PART {
54    const class PartitionSize *info;
55
56    PELEM   *p;
57    GBT_LEN  len;               // length between two nodes (weighted by weight)
58    double   weight;            // sum of weights
59    size_t   id;
60
61    int members;
62
63    PART(const PART& other)
64        : info(other.info),
65          p(info->alloc_mem()),
66          len(other.len),
67          weight(other.weight),
68          id(info->get_unique_id()), 
69          members(other.members)
70    {
71        int longs = get_longs();
72        for (int i = 0; i<longs; ++i) {
73            p[i] = other.p[i];
74        }
75        arb_assert(is_valid());
76    }
77    DECLARE_ASSIGNMENT_OPERATOR(PART);
78
79    int count_members() const;
80
81    void set_weight(double pc) {
82        double lenScale  = pc/weight;
83        weight           = pc;
84        len             *= lenScale;
85    }
86
87public:
88
89    PART(const PartitionSize* size_info, double weight_)
90        : info(size_info),
91          p(info->alloc_mem()),
92          len(0),
93          weight(weight_),
94          id(info->get_unique_id()),
95          members(0)
96    {
97        arb_assert(is_valid());
98    }
99    ~PART() {
100        free(p);
101    }
102
103    PART *clone() const { return new PART(*this); }
104
105    int get_longs() const { return info->get_longs(); }
106    int get_maxsize() const { return info->get_bits(); }
107    PELEM get_cutmask() const { return info->get_cutmask(); }
108
109    bool is_valid() const {
110        PELEM last = p[get_longs()-1];
111
112        bool only_valid_bits    = (last&get_cutmask()) == last;
113        bool valid_member_count = members >= 0 && members <= get_maxsize();
114        bool valid_weight       = weight>0.0;
115
116        return only_valid_bits && valid_member_count && valid_weight;
117    }
118
119    void setbit(int pos) {
120        /*! set the bit of the part 'p' at the position 'pos'
121         */
122        arb_assert(is_valid());
123
124        int   idx = pos / sizeof(PELEM) / 8;
125        PELEM bit = 1 << (pos % (sizeof(PELEM)*8));
126
127        if (!(p[idx]&bit)) {
128            p[idx] |= bit;
129            members++;
130        }
131
132        arb_assert(is_valid());
133    }
134    void set_len(GBT_LEN length) {
135        arb_assert(is_valid());
136        len = length*weight;
137    }
138    GBT_LEN get_len() const {
139        return len/weight;
140    }
141
142    double get_weight() const { return weight; }
143
144    void add(const PART* other) {
145        weight += other->weight;
146        len    += other->len;
147    }
148
149    void takeMean(double overall_weight) {
150        set_weight(get_weight()/overall_weight);
151        arb_assert(is_valid());
152        arb_assert(weight <= 1.0);
153    }
154
155    void invert();
156    bool is_standardized() const;
157    void standardize();
158
159    void add_from(const PART *source);
160
161    bool is_son_of(const PART *father) const;
162
163    bool overlaps_with(const PART *other) const;
164    bool disjunct_from(const PART *other) const { return !overlaps_with(other); }
165
166    bool equals(const PART *other) const;
167    bool differs(const PART *other) const { return !equals(other); }
168
169    int index() const;
170    unsigned key() const;
171
172    int get_members() const { return members; }
173
174    bool is_leaf_edge() const { int size = members; return size == 1 || size == (get_maxsize()-1); }
175    int distance_to_tree_center() const { return abs(get_maxsize()/2 - members); }
176
177    int insertionOrder_cmp(const PART *other) const;
178    int topological_cmp(const PART *other) const;
179   
180#if defined(NTREE_DEBUG_FUNCTIONS)
181    void print() const;
182#endif
183};
184
185#else
186#error CT_part.hxx included twice
187#endif // CT_PART_HXX
Note: See TracBrowser for help on using the browser.