bpp-phyl3 3.0.0
PhyloDAG.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: The Bio++ Development Group
2//
3// SPDX-License-Identifier: CECILL-2.1
4
5// #include "../Likelihood/ParametrizablePhyloDAG.h"
6#include "PhyloDAG.h"
7
8using namespace bpp;
9using namespace std;
10
11PhyloDAG::PhyloDAG() :
13 name_("")
14{}
15
18 name_(dag ? dag->name_ : "")
19{}
20
21// PhyloDAG::PhyloDAG(const ParametrizablePhyloDAG& dag) :
22// AssociationDAGlobalGraphObserver<PhyloNode, PhyloBranch>(dag),
23// name_("")
24// {}
25
27{
28 std::vector<shared_ptr<PhyloNode>> nodes = getAllNodes();
29 std::vector<shared_ptr<PhyloBranch>> branches = getAllEdges();
30
31 for (unsigned int i = 0; i < nodes.size(); i++)
32 {
33 setNodeIndex(nodes[i], i);
34
35 // set arbitrary edge node nmber to an incoming edge
36 if (hasFather(nodes[i]))
37 {
38 const auto vInEdges = getIncomingEdges(nodes[i]);
39 setEdgeIndex(vInEdges[0], i);
40 }
41 }
42
43 size_t eid = 0;
44 for (auto j = nodes.size(); j < branches.size(); j++)
45 {
46 while (hasEdgeIndex(branches[eid]))
47 eid++;
48 setEdgeIndex(branches[eid], (uint)j);
49 }
50}
51
52std::shared_ptr<PhyloNode> PhyloDAG::getPhyloNode(const std::string& name) const
53{
54 vector<shared_ptr<PhyloNode>> vpn = getAllNodes();
55
56 for (auto it:vpn)
57 {
58 if (it->hasName() && it->getName() == name)
59 return it;
60 }
61
62 return std::make_shared<PhyloNode>();
63}
64
65std::vector<std::string> PhyloDAG::getAllLeavesNames() const
66{
67 vector<string> vn;
68
69 vector<shared_ptr<PhyloNode>> vpn = getAllLeaves();
70
71 for (vector<shared_ptr<PhyloNode>>::const_iterator it = vpn.begin(); it != vpn.end(); it++)
72 {
73 vn.push_back((*it)->getName());
74 }
75
76 return vn;
77}
78
79void PhyloDAG::scaleDAG(shared_ptr<PhyloNode> node, double factor)
80{
81 vector<shared_ptr<PhyloBranch>> branches = getBelowEdges(node);
82 for (vector<shared_ptr<PhyloBranch>>::iterator currBranch = branches.begin(); currBranch != branches.end(); currBranch++)
83 {
84 if ((*currBranch)->hasLength())
85 (*currBranch)->setLength((*currBranch)->getLength() * factor);
86 else
87 throw PhyloBranchPException("PhyloDAG::scaleDAG : undefined length", (*currBranch).get());
88 }
89}
90
91void PhyloDAG::scaleDAG(double factor)
92{
93 scaleDAG(getRoot(), factor);
94}
95
96void PhyloDAG::pruneDAG(std::vector<string> leaves)
97{
98 vector<shared_ptr<PhyloNode>> vpn = getAllLeaves();
99
100 for (auto& leaf:vpn)
101 {
102 if (std::find(leaves.begin(), leaves.end(), leaf->getName()) == leaves.end())
103 {
104 std::vector<shared_ptr<PhyloNode>> vfat({leaf}); // one vector per leaf removed to avoid too large vector
105
106 std::vector<shared_ptr<PhyloNode>>::iterator vfatit; // one vector per leaf removed to avoid too large vector
107
108 for (vfatit = vfat.begin(); vfatit != vfat.end();)
109 {
110 if (hasNode(*vfatit) && isLeaf(*vfatit))
111 {
112 auto vfat2 = getFathers(*vfatit);
113 vfat.insert(vfat.end(), vfat2.begin(), vfat2.end() );
114 deleteNode(*vfatit);
115 }
116 }
117 }
118 }
119}
120
122{
123 vector<shared_ptr<PhyloBranch>> vpn = getAllEdges();
124
125 for (auto& it: vpn)
126 {
127 it->setLength(l);
128 }
129}
130
132{
133 Vdouble vl;
134
135 vector<shared_ptr<PhyloBranch>> vpn = getAllEdges();
136
137 for (auto& it: vpn)
138 {
139 vl.push_back(it->getLength());
140 }
141 return vl;
142}
143
144
146{
147 vector<shared_ptr<PhyloBranch>> vpn = getAllEdges();
148
149 for (auto& it: vpn)
150 {
151 uint ei = getEdgeIndex(it);
152
153 if (!phylodag.hasEdge(ei))
154 throw Exception("Phylodag::operator+= : argument dag does not have edge " + TextTools::toString(ei));
155 if (!it->hasLength() || !phylodag.getEdge(ei)->hasLength())
156 throw Exception("Phylodag::operator+= : no summing of branches without length.");
157
158 it->setLength(it->getLength() + phylodag.getEdge(ei)->getLength());
159 }
160
161 return *this;
162}
163
165{
166 vector<shared_ptr<PhyloBranch>> vpn = getAllEdges();
167
168 for (auto& it: vpn)
169 {
170 uint ei = getEdgeIndex(it);
171
172 if (!phylodag.hasEdge(ei))
173 throw Exception("Phylodag::operator+= : argument dag does not have edge " + TextTools::toString(ei));
174 if (!it->hasLength() || !phylodag.getEdge(ei)->hasLength())
175 throw Exception("Phylodag::operator+= : no summing of branches without length.");
176
177 it->setLength(it->getLength() - phylodag.getEdge(ei)->getLength());
178 }
179
180 return *this;
181}
182
184{
185 vector<shared_ptr<PhyloBranch>> vpn = getAllEdges();
186
187 for (auto& it: vpn)
188 {
189 uint ei = getEdgeIndex(it);
190
191 if (!phylodag.hasEdge(ei))
192 throw Exception("Phylodag::operator/= : argument dag does not have edge " + TextTools::toString(ei));
193 if (!it->hasLength() || !phylodag.getEdge(ei)->hasLength())
194 throw Exception("Phylodag::operator/= : no summing of branches without length.");
195
196 it->setLength(it->getLength() / phylodag.getEdge(ei)->getLength());
197 }
198
199 return *this;
200}
201
203{
204 vector<shared_ptr<PhyloBranch>> vpn = getAllEdges();
205
206 for (auto& it: vpn)
207 {
208 uint ei = getEdgeIndex(it);
209
210 if (!phylodag.hasEdge(ei))
211 throw Exception("Phylodag::operator/= : argument dag does not have edge " + TextTools::toString(ei));
212 if (!it->hasLength() || !phylodag.getEdge(ei)->hasLength())
213 throw Exception("Phylodag::operator/= : no summing of branches without length.");
214
215 it->setLength(it->getLength() * phylodag.getEdge(ei)->getLength());
216 }
217
218 return *this;
219}
std::vector< std::shared_ptr< N > > getFathers(const std::shared_ptr< N > node) const
bool hasFather(const std::shared_ptr< N > nodeObject) const
std::vector< std::shared_ptr< E > > getBelowEdges(const std::shared_ptr< N > localRoot)
bool isLeaf(const Nref node) const
virtual std::vector< std::shared_ptr< E > > getIncomingEdges(const std::shared_ptr< N > node) const=0
virtual std::vector< std::shared_ptr< N > > getAllLeaves() const=0
virtual std::vector< std::shared_ptr< E > > getAllEdges() const=0
virtual NodeIndex setNodeIndex(const std::shared_ptr< N > nodeObject, NodeIndex index)=0
virtual void deleteNode(std::shared_ptr< N > nodeObject)=0
virtual std::shared_ptr< N > getRoot() const=0
virtual EdgeIndex getEdgeIndex(const std::shared_ptr< E > edgeObject) const=0
virtual std::vector< std::shared_ptr< N > > getAllNodes() const=0
virtual EdgeIndex setEdgeIndex(const std::shared_ptr< E > edgeObject, EdgeIndex index)=0
virtual bool hasEdgeIndex(const std::shared_ptr< E > edgeObject) const=0
virtual bool hasNode(NodeIndex nodeIndex) const=0
virtual std::shared_ptr< E > getEdge(EdgeIndex edgeIndex) const=0
virtual bool hasEdge(EdgeIndex edgeIndex) const=0
General exception thrown when something is wrong with a particular branch.
void scaleDAG(double factor)
Multiply all branch lengths by a given factor.
Definition: PhyloDAG.cpp:91
std::vector< std::string > getAllLeavesNames() const
Definition: PhyloDAG.cpp:65
PhyloDAG & operator+=(const PhyloDAG &phylotree)
Add the lengths of branches of another phylotree to this one. Just branch ids are considered,...
Definition: PhyloDAG.cpp:145
PhyloDAG & operator-=(const PhyloDAG &phylotree)
Subtracts the lengths of branches of another phylotree to this one. Just branch ids are considered,...
Definition: PhyloDAG.cpp:164
Vdouble getBranchLengths() const
Definition: PhyloDAG.cpp:131
PhyloDAG & operator/=(const PhyloDAG &phylotree)
Divides the lengths of branches of this phylotree by the ones of another phylotree....
Definition: PhyloDAG.cpp:183
void resetNodesId()
Definition: PhyloDAG.cpp:26
PhyloDAG & operator*=(const PhyloDAG &phylotree)
Multiplies the lengths of branches of this phylotree by the ones of another phylotree....
Definition: PhyloDAG.cpp:202
std::shared_ptr< PhyloNode > getPhyloNode(const std::string &name) const
Definition: PhyloDAG.cpp:52
void setBranchLengths(double l)
Definition: PhyloDAG.cpp:121
void pruneDAG(std::vector< std::string > leaves)
Prune a tree to a given set of leaf names.
Definition: PhyloDAG.cpp:96
std::string toString(T t)
Defines the basic types of data flow nodes.
std::vector< double > Vdouble