~ubuntu-branches/ubuntu/raring/libbpp-phyl/raring

« back to all changes in this revision

Viewing changes to src/Bpp/Phyl/BipartitionList.h

  • Committer: Bazaar Package Importer
  • Author(s): Julien Dutheil
  • Date: 2011-06-09 11:00:00 UTC
  • Revision ID: james.westby@ubuntu.com-20110609110000-yvx78svv6w7xxgph
Tags: upstream-2.0.2
Import upstream version 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// File: BipartitionList.h
 
3
// Created by: Nicolas Galtier and Julien Dutheil
 
4
// Created on: Tue Apr 13 15:09 2007
 
5
//
 
6
 
 
7
/*
 
8
Copyright or © or Copr. CNRS, (November 16, 2004)
 
9
 
 
10
This software is a computer program whose purpose is to provide classes
 
11
for phylogenetic data analysis.
 
12
 
 
13
This software is governed by the CeCILL  license under French law and
 
14
abiding by the rules of distribution of free software.  You can  use,
 
15
modify and/ or redistribute the software under the terms of the CeCILL
 
16
license as circulated by CEA, CNRS and INRIA at the following URL
 
17
"http://www.cecill.info".
 
18
 
 
19
As a counterpart to the access to the source code and  rights to copy,
 
20
modify and redistribute granted by the license, users are provided only
 
21
with a limited warranty  and the software's author,  the holder of the
 
22
economic rights,  and the successive licensors  have only  limited
 
23
liability.
 
24
 
 
25
In this respect, the user's attention is drawn to the risks associated
 
26
with loading,  using,  modifying and/or developing or reproducing the
 
27
software by the user in light of its specific status of free software,
 
28
that may mean  that it is complicated to manipulate,  and  that  also
 
29
therefore means  that it is reserved for developers  and  experienced
 
30
professionals having in-depth computer knowledge. Users are therefore
 
31
encouraged to load and test the software's suitability as regards their
 
32
requirements in conditions enabling the security of their systems and/or
 
33
data to be ensured and,  more generally, to use and operate it in the
 
34
same conditions as regards security.
 
35
 
 
36
The fact that you are presently reading this means that you have had
 
37
knowledge of the CeCILL license and that you accept its terms.
 
38
*/
 
39
 
 
40
#ifndef _BIPARTITIONLIST_H_
 
41
#define _BIPARTITIONLIST_H_
 
42
 
 
43
#include "Tree.h"
 
44
 
 
45
#include <Bpp/Utils/MapTools.h>
 
46
#include <Bpp/Numeric/Matrix/Matrix.h>
 
47
 
 
48
// From the STL:
 
49
#include <map>
 
50
#include <algorithm>
 
51
 
 
52
namespace bpp
 
53
{
 
54
 
 
55
class Node;
 
56
template<class N> class TreeTemplate;
 
57
 
 
58
/**
 
59
 * @brief This class deals with the bipartitions defined by trees
 
60
 *
 
61
 * Any branch of a tree defines a bipartition, i.e. two non-overlapping subsets of leaves
 
62
 * whose union is the whole set of leaves. A tree topology can therefore be represented by
 
63
 * the list of bipartitions its branches define - the so-called matrix representation.
 
64
 * Coding trees this way is useful for comparing topologies, calculating topological distances,
 
65
 * producing consensus trees or super-trees, calculating bootstrap support.
 
66
 *
 
67
 * A BipartitionList includes a set of element names (typically leaf names) and a vector of arrays of bits
 
68
 * (int*, not bitsets, are used to allow for dynamic memory allocation). Each array of bit codes for one bipartition.
 
69
 * Each bit in an array of bit corresponds to one element, so the order of element names matter.
 
70
 * Bits set to zero versus bits set to one define the two partitions of elements.
 
71
 * A BipartitionList is called sorted if its elements (leaf names) are in alphabetic order (recommended).
 
72
 *
 
73
 * BipartitionList objects are typically created from a tree, in which case elements are leaf names.
 
74
 * Note that BipartitionList is an unrooted object: a rooted or unrooted versions of the same tree will
 
75
 * yield the same BipartitionList in which the root location is ignored. Bipartitions can be accessed as
 
76
 * arrays of bits (e.g. getBitBipartition), or as map<string, bool>, in which keys are leaf names and
 
77
 * true/false values define the two partitions (e.g. getBipartition, addBipartition).
 
78
 *
 
79
 * @see Tree
 
80
 * @see BipartitionTools
 
81
 * @see TreeTools
 
82
 */
 
83
class BipartitionList:
 
84
  public virtual Clonable
 
85
{
 
86
  private:
 
87
 
 
88
    std::vector<int*> bitBipartitionList_;
 
89
    std::vector<std::string> elements_;
 
90
    bool sorted_;
 
91
 
 
92
  public:
 
93
 
 
94
    /**
 
95
     * @brief The main contructor
 
96
     *
 
97
     * @param tr The tree to be coded as bipartitions
 
98
     * @param sorted Tells whether leave names should be alphabetically sorted (recommended)
 
99
     * @param index An output optional vector to keep trace of the nodes id underlying each bipartition.
 
100
     */
 
101
    BipartitionList(const Tree& tr, bool sorted = true, std::vector<int>* index = 0);
 
102
 
 
103
    /**
 
104
     * @brief An alternative constructor in which elements and bipartitions are passed directly
 
105
     *
 
106
     * @param elements Leaf names
 
107
     * @param bipl The list of bit-encoded bipartitions
 
108
     */
 
109
    BipartitionList(const std::vector<std::string>& elements, const std::vector<int*>& bipl);
 
110
 
 
111
    /**
 
112
     * @brief Copy-constructor
 
113
     */
 
114
    BipartitionList(const BipartitionList& bipl);
 
115
 
 
116
    /**
 
117
     * @brief Assignment operator
 
118
     */
 
119
    BipartitionList& operator=(const BipartitionList& bipl);
 
120
 
 
121
    virtual ~BipartitionList();
 
122
 
 
123
#ifndef NO_VIRTUAL_COV
 
124
    BipartitionList*
 
125
#else
 
126
    Clonable*
 
127
#endif
 
128
    clone() const { return new BipartitionList(*this); }
 
129
 
 
130
  public:
 
131
 
 
132
    unsigned int getNumberOfElements() const { return elements_.size(); }
 
133
 
 
134
    const std::vector<std::string>& getElementNames() const { return elements_; }
 
135
 
 
136
    unsigned int getNumberOfBipartitions() const { return bitBipartitionList_.size(); }
 
137
 
 
138
    const std::vector<int*> & getBitBipartitionList() const { return bitBipartitionList_; }
 
139
 
 
140
    std::map<std::string, bool> getBipartition(unsigned int i) const throw (Exception);
 
141
 
 
142
    int* getBitBipartition(unsigned int i) throw (Exception);
 
143
 
 
144
    bool haveSameElementsThan(std::map<std::string, bool>& bipart) const;
 
145
 
 
146
    void addBipartition(std::map<std::string, bool>& bipart, bool checkElements = 1) throw(Exception);
 
147
 
 
148
    void deleteBipartition(unsigned int i) throw(Exception);
 
149
 
 
150
    bool isSorted() const { return sorted_; }
 
151
 
 
152
    void sortElements();
 
153
 
 
154
    bool containsBipartition(std::map<std::string, bool>& bipart, bool checkElements = 1) const throw(Exception);
 
155
 
 
156
    bool areIdentical(unsigned int k1, unsigned int k2) const throw(Exception);
 
157
 
 
158
    void removeRedundantBipartitions();
 
159
 
 
160
    /**
 
161
     * @brief Tells whether 2 bipartitions from the list are compatible
 
162
     *
 
163
     * Let A=A1|A2 and B=B1|B2 be two bipartitions (such that A1 U A2 = B1 U B2 = set of leaves)
 
164
     * A and B are said compatible if (A1 contains B1 and B2 contains A2) or (A1 contains B2
 
165
     * and B1 contains A2) or (B1 contains A1 and A2 contains B2) or (B2 contains A1 and A2
 
166
     * contains B1). Only compatible bipartitions can belong to the same tree.
 
167
     */
 
168
    bool areCompatible(unsigned int k1, unsigned int k2) const throw(Exception);
 
169
 
 
170
    /**
 
171
     * @brief Tells whether all bipartitions in the list are compatible with each other
 
172
     */
 
173
    bool areAllCompatible() const;
 
174
 
 
175
    /**
 
176
     * @brief Tells whether all bipartitions in the list are compatible with a given bipartition
 
177
     *
 
178
     * @param bipart A map representing a bipartition.
 
179
     * @param checkElements Check the correspondance of element sets or not.
 
180
     */
 
181
    bool areAllCompatibleWith(std::map<std::string, bool>& bipart, bool checkElements = true) const throw (Exception);
 
182
 
 
183
    /**
 
184
     * @brief Removes bipartitions corresponding to external branches (1 vs n-1)
 
185
     */
 
186
    void removeTrivialBipartitions();
 
187
 
 
188
    /**
 
189
     * @brief Adds bipartitions corresponding to external branches if missing
 
190
     */
 
191
    void addTrivialBipartitions(bool checkExisting);
 
192
 
 
193
 
 
194
    /**
 
195
     * @brief Replaces ones by zeros and zeros by ones in the ith bipartition
 
196
     */
 
197
    void flip(unsigned int i) throw(Exception);
 
198
 
 
199
    /**
 
200
     * @brief Returns the size of the smallest of the two partitions (e.g. 1 for external branches)
 
201
     */
 
202
    unsigned int getPartitionSize(unsigned int i) const throw(Exception);
 
203
 
 
204
    /**
 
205
     * @brief Sort bipartitions by partition size
 
206
     */
 
207
    void sortByPartitionSize();
 
208
 
 
209
    /**
 
210
     * @brief Translate into a tree
 
211
     */
 
212
    TreeTemplate<Node>* toTree() const throw(Exception);
 
213
 
 
214
    /**
 
215
     * @brief Create a matrix representation of the bifurcations.
 
216
     *
 
217
     * Each row corresponds to an element, each column to a bipartition.
 
218
     *
 
219
     * NB: using RowMatrix<bool> leads to unexplained compilation error...
 
220
     *
 
221
     * @return A boolean matrix.
 
222
     */
 
223
    RowMatrix<int> toMatrix() const;
 
224
 
 
225
  private:
 
226
 
 
227
    std::vector<std::string> buildBitBipartitions(const Node* nd, std::vector<int*>& bitbip, const std::vector<std::string> & elements, unsigned int* cpt, std::vector<int>* index) const;
 
228
 
 
229
};
 
230
 
 
231
} //end of namespace bpp.
 
232
 
 
233
#endif //_BIPARTITIONLIST_H_
 
234