bpp-phyl3 3.0.0
Mapping.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: The Bio++ Development Group
2//
3// SPDX-License-Identifier: CECILL-2.1
4
5#ifndef _LEGACY_MAPPING_H_
6#define _LEGACY_MAPPING_H_
7
8#include <Bpp/Clonable.h>
9
10#include "../../Tree/Tree.h"
11#include "../../Tree/TreeTemplate.h"
12
13// From the STL:
14#include <vector>
15#include <memory>
16
17namespace bpp
18{
23 public virtual Clonable
24{
25public:
28
29 LegacyMappingInterface* clone() const override = 0;
30
31public:
35 virtual const Tree& tree() const = 0;
36
40 virtual bool isEmpty() const = 0;
41
45 virtual size_t getNumberOfSites() const = 0;
46
50 virtual size_t getNumberOfBranches() const = 0;
51
56 virtual int getSitePosition(size_t index) const = 0;
57
61 virtual std::vector<double> getBranchLengths() const = 0;
62
67 virtual size_t getNodeIndex(int nodeId) const = 0;
68
76 virtual void setSitePosition(size_t index, int position) = 0;
77};
78
79
86 public virtual LegacyMappingInterface
87{
88private:
89 std::unique_ptr<const TreeTemplate<Node>> tree_;
90 std::vector<int> sitesPositions_;
91 std::vector<const Node*> nodes_;
92 size_t nbSites_;
94
95public:
97 {
98 nodes_ = tree_->getNodes();
99 nodes_.pop_back(); // remove root node.
100 nbBranches_ = nodes_.size();
101 }
102
104 tree_(dynamic_cast<const TreeTemplate<Node>*>(absm.tree_->clone())),
106 nodes_(),
107 nbSites_(absm.nbSites_),
109 {
110 nodes_ = tree_->getNodes();
111 nodes_.pop_back(); // remove root node.
112 }
113
115 {
116 tree_.reset(dynamic_cast<const TreeTemplate<Node>*>(absm.tree_->clone()));
118 nbSites_ = absm.nbSites_;
120 nodes_ = tree_->getNodes();
121 nodes_.pop_back(); // remove root node.
122 return *this;
123 }
124
125 LegacyAbstractMapping* clone() const override = 0;
126
128
129public:
130 bool isEmpty() const override { return !tree_; }
131
132 const TreeTemplate<Node>& tree() const override
133 {
134 if (isEmpty()) throw Exception("LegacyAbstractSubstitutionMapping::getSitePosition. No tree is assigned to this map yet.");
135 return *tree_;
136 }
137
138 void setTree(const Tree& tree)
139 {
140 tree_.reset(new TreeTemplate<Node>(tree));
141 nodes_ = tree_->getNodes();
142 nodes_.pop_back(); // remove root node.
143 nbBranches_ = nodes_.size();
144 }
145
146 int getSitePosition(size_t index) const override
147 {
148 if (isEmpty()) throw Exception("LegacyAbstractMapping::getSitePosition. No tree is assigned to this map yet.");
149 return sitesPositions_[index];
150 }
151
152 void setSitePosition(size_t index, int position) override
153 {
154 if (isEmpty()) throw Exception("LegacyAbstractMapping::setSitePosition. No tree is assigned to this map yet.");
155 sitesPositions_[index] = position;
156 }
157
158 size_t getNumberOfSites() const override { return nbSites_; }
159
160 size_t getNumberOfBranches() const override { return nbBranches_; }
161
162 virtual const Node* getNode(size_t nodeIndex) const { return nodes_[nodeIndex]; }
163
164 virtual void setNumberOfSites(size_t numberOfSites)
165 {
166 nbSites_ = numberOfSites;
167 sitesPositions_.resize(numberOfSites);
168 for (size_t i = 0; i < numberOfSites; i++)
169 {
170 sitesPositions_[i] = static_cast<int>(i + 1); // Default: all sizes numbered for 1 to n.
171 }
172 }
173
174 virtual std::vector<double> getBranchLengths() const override
175 {
176 std::vector<double> brLen(nbBranches_);
177 for (size_t i = 0; i < nbBranches_; i++)
178 {
179 brLen[i] = nodes_[i]->getDistanceToFather();
180 }
181 return brLen;
182 }
183
184 virtual size_t getNodeIndex(int nodeId) const override
185 {
186 for (size_t i = 0; i < nbBranches_; i++)
187 {
188 if (nodes_[i]->getId() == nodeId) return i; }
189 throw NodeNotFoundException("LegacyAbstractMapping::getNodeIndex(nodeId).", TextTools::toString(nodeId));
190 }
191};
192} // end of namespace bpp.
193
194#endif // _LEGACY_MAPPING_H_
Partial implementation of the mapping interface.
Definition: Mapping.h:87
size_t getNumberOfSites() const override
Definition: Mapping.h:158
std::vector< const Node * > nodes_
Definition: Mapping.h:91
LegacyAbstractMapping * clone() const override=0
LegacyAbstractMapping & operator=(const LegacyAbstractMapping &absm)
Definition: Mapping.h:114
std::vector< int > sitesPositions_
Definition: Mapping.h:90
void setTree(const Tree &tree)
Definition: Mapping.h:138
int getSitePosition(size_t index) const override
Definition: Mapping.h:146
std::unique_ptr< const TreeTemplate< Node > > tree_
Definition: Mapping.h:89
bool isEmpty() const override
Definition: Mapping.h:130
void setSitePosition(size_t index, int position) override
Set the position of a given site.
Definition: Mapping.h:152
virtual std::vector< double > getBranchLengths() const override
Definition: Mapping.h:174
virtual void setNumberOfSites(size_t numberOfSites)
Definition: Mapping.h:164
const TreeTemplate< Node > & tree() const override
Definition: Mapping.h:132
virtual size_t getNodeIndex(int nodeId) const override
Definition: Mapping.h:184
size_t getNumberOfBranches() const override
Definition: Mapping.h:160
LegacyAbstractMapping(const Tree &tree)
Definition: Mapping.h:96
LegacyAbstractMapping(const LegacyAbstractMapping &absm)
Definition: Mapping.h:103
virtual const Node * getNode(size_t nodeIndex) const
Definition: Mapping.h:162
virtual ~LegacyAbstractMapping()
Definition: Mapping.h:127
General interface for storing mapping data.
Definition: Mapping.h:24
virtual ~LegacyMappingInterface()
Definition: Mapping.h:27
virtual bool isEmpty() const =0
virtual int getSitePosition(size_t index) const =0
virtual size_t getNodeIndex(int nodeId) const =0
virtual size_t getNumberOfBranches() const =0
LegacyMappingInterface * clone() const override=0
virtual const Tree & tree() const =0
virtual std::vector< double > getBranchLengths() const =0
virtual size_t getNumberOfSites() const =0
virtual void setSitePosition(size_t index, int position)=0
Set the position of a given site.
Exception thrown when something is wrong with a particular node.
The phylogenetic node class.
Definition: Node.h:59
The phylogenetic tree class.
Definition: TreeTemplate.h:59
Interface for phylogenetic tree objects.
Definition: Tree.h:115
std::string toString(T t)
Defines the basic types of data flow nodes.