2
// File: BipartitionList.h
3
// Created by: Nicolas Galtier and Julien Dutheil
4
// Created on: Tue Apr 13 15:09 2007
8
Copyright or © or Copr. CNRS, (November 16, 2004)
10
This software is a computer program whose purpose is to provide classes
11
for phylogenetic data analysis.
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".
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
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.
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.
40
#ifndef _BIPARTITIONLIST_H_
41
#define _BIPARTITIONLIST_H_
45
#include <Bpp/Utils/MapTools.h>
46
#include <Bpp/Numeric/Matrix/Matrix.h>
56
template<class N> class TreeTemplate;
59
* @brief This class deals with the bipartitions defined by trees
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.
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).
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).
80
* @see BipartitionTools
83
class BipartitionList:
84
public virtual Clonable
88
std::vector<int*> bitBipartitionList_;
89
std::vector<std::string> elements_;
95
* @brief The main contructor
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.
101
BipartitionList(const Tree& tr, bool sorted = true, std::vector<int>* index = 0);
104
* @brief An alternative constructor in which elements and bipartitions are passed directly
106
* @param elements Leaf names
107
* @param bipl The list of bit-encoded bipartitions
109
BipartitionList(const std::vector<std::string>& elements, const std::vector<int*>& bipl);
112
* @brief Copy-constructor
114
BipartitionList(const BipartitionList& bipl);
117
* @brief Assignment operator
119
BipartitionList& operator=(const BipartitionList& bipl);
121
virtual ~BipartitionList();
123
#ifndef NO_VIRTUAL_COV
128
clone() const { return new BipartitionList(*this); }
132
unsigned int getNumberOfElements() const { return elements_.size(); }
134
const std::vector<std::string>& getElementNames() const { return elements_; }
136
unsigned int getNumberOfBipartitions() const { return bitBipartitionList_.size(); }
138
const std::vector<int*> & getBitBipartitionList() const { return bitBipartitionList_; }
140
std::map<std::string, bool> getBipartition(unsigned int i) const throw (Exception);
142
int* getBitBipartition(unsigned int i) throw (Exception);
144
bool haveSameElementsThan(std::map<std::string, bool>& bipart) const;
146
void addBipartition(std::map<std::string, bool>& bipart, bool checkElements = 1) throw(Exception);
148
void deleteBipartition(unsigned int i) throw(Exception);
150
bool isSorted() const { return sorted_; }
154
bool containsBipartition(std::map<std::string, bool>& bipart, bool checkElements = 1) const throw(Exception);
156
bool areIdentical(unsigned int k1, unsigned int k2) const throw(Exception);
158
void removeRedundantBipartitions();
161
* @brief Tells whether 2 bipartitions from the list are compatible
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.
168
bool areCompatible(unsigned int k1, unsigned int k2) const throw(Exception);
171
* @brief Tells whether all bipartitions in the list are compatible with each other
173
bool areAllCompatible() const;
176
* @brief Tells whether all bipartitions in the list are compatible with a given bipartition
178
* @param bipart A map representing a bipartition.
179
* @param checkElements Check the correspondance of element sets or not.
181
bool areAllCompatibleWith(std::map<std::string, bool>& bipart, bool checkElements = true) const throw (Exception);
184
* @brief Removes bipartitions corresponding to external branches (1 vs n-1)
186
void removeTrivialBipartitions();
189
* @brief Adds bipartitions corresponding to external branches if missing
191
void addTrivialBipartitions(bool checkExisting);
195
* @brief Replaces ones by zeros and zeros by ones in the ith bipartition
197
void flip(unsigned int i) throw(Exception);
200
* @brief Returns the size of the smallest of the two partitions (e.g. 1 for external branches)
202
unsigned int getPartitionSize(unsigned int i) const throw(Exception);
205
* @brief Sort bipartitions by partition size
207
void sortByPartitionSize();
210
* @brief Translate into a tree
212
TreeTemplate<Node>* toTree() const throw(Exception);
215
* @brief Create a matrix representation of the bifurcations.
217
* Each row corresponds to an element, each column to a bipartition.
219
* NB: using RowMatrix<bool> leads to unexplained compilation error...
221
* @return A boolean matrix.
223
RowMatrix<int> toMatrix() const;
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;
231
} //end of namespace bpp.
233
#endif //_BIPARTITIONLIST_H_