/usr/include/Bpp/Phyl/Node.h is in libbpp-phyl-dev 2.0.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 | //
// File: Node.h
// Created by: Julien Dutheil
// Created on: Thu Mar 13 12:03:18 2003
//
/*
Copyright or © or Copr. Bio++ Development Team, (November 16, 2004)
This software is a computer program whose purpose is to provide classes
for phylogenetic data analysis.
This software is governed by the CeCILL license under French law and
abiding by the rules of distribution of free software. You can use,
modify and/ or redistribute the software under the terms of the CeCILL
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info".
As a counterpart to the access to the source code and rights to copy,
modify and redistribute granted by the license, users are provided only
with a limited warranty and the software's author, the holder of the
economic rights, and the successive licensors have only limited
liability.
In this respect, the user's attention is drawn to the risks associated
with loading, using, modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean that it is complicated to manipulate, and that also
therefore means that it is reserved for developers and experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or
data to be ensured and, more generally, to use and operate it in the
same conditions as regards security.
The fact that you are presently reading this means that you have had
knowledge of the CeCILL license and that you accept its terms.
*/
#ifndef _NODE_H_
#define _NODE_H_
#include "TreeExceptions.h"
#include <Bpp/Clonable.h>
#include <Bpp/Utils/MapTools.h>
#include <Bpp/BppString.h>
#include <Bpp/Numeric/Number.h>
// From the STL:
#include <string>
#include <vector>
#include <map>
#include <iostream>
#include <algorithm>
namespace bpp
{
/**
* @brief The phylogenetic node class.
*
* This class is for use with the TreeTemplate class, an implementation of the Tree interface.
* TreeTemplates are made made of nodes, instances of this class.
* Since trees are oriented (rooted), each node has one <i>father node</i> and possibly
* many <i>son nodes</i>. Leaves are nodes without descendant and root is defined has the without
* father. Inner nodes will generally contain two descendants (the tree is then called
* <i>bifurcating</i>), but mutlifurcating trees are also allowed with this kind of description.
* In the rooted case, each inner node also defines a <i>subtree</i>.
* This allows to work recursively on trees, which is very convenient in most cases.</p>
*
* This class is made the more general as possible, while keeping it very simple. It contains:</p>
* - An identity tag, to identity it in the tree;
* - A name, necessary for leaf nodes, optionnal else;
* - A pointer toward the father node;
* - A std::vector of pointer toward son nodes;
* - The distance from the father node:
* - A property map, that may contain any information to link to each node, e.g. bootstrap
* value or GC content.
*
* Methods are provided to help the building of trees from scratch.
* Trees are more easily built from root to leaves:
* The addSon(Node) method adds a node to the list of direct descendants of a
* given node. The son node will also have its father set to the current node.
* It is also possible to build a tree starting from the leaves using the setFather method.
* Changing the parent node will automatically append the current node to the son nodes of the new father.
*
* @see Tree, TreeTemplate
*/
class Node :
public Clonable
{
protected:
int id_;
std::string* name_;
std::vector<Node*> sons_;
Node* father_;
double* distanceToFather_;
mutable std::map<std::string, Clonable*> nodeProperties_;
mutable std::map<std::string, Clonable*> branchProperties_;
public:
/**
* @brief Build a new void Node object.
*/
Node() :
id_(0),
name_(0),
sons_(),
father_(0),
distanceToFather_(0),
nodeProperties_(),
branchProperties_()
{}
/**
* @brief Build a new Node with specified id.
*/
Node(int id) :
id_(id),
name_(0),
sons_(),
father_(0),
distanceToFather_(0),
nodeProperties_(),
branchProperties_()
{}
/**
* @brief Build a new Node with specified name.
*/
Node(const std::string& name) :
id_(0),
name_(new std::string(name)),
sons_(),
father_(0),
distanceToFather_(0),
nodeProperties_(),
branchProperties_()
{}
/**
* @brief Build a new Node with specified id and name.
*/
Node(int id, const std::string& name) :
id_(id),
name_(new std::string(name)),
sons_(),
father_(0),
distanceToFather_(0),
nodeProperties_(),
branchProperties_()
{}
/**
* @brief Copy constructor.
*
* @param node The node to copy.
*/
Node(const Node& node);
/**
* @brief Assignation operator.
*
* @param node the node to copy.
* @return A reference toward this node.
*/
Node& operator=(const Node& node);
virtual ~Node()
{
if (name_) delete name_;
if (distanceToFather_) delete distanceToFather_;
for (std::map<std::string, Clonable*>::iterator i = nodeProperties_.begin(); i != nodeProperties_.end(); i++)
{
delete i->second;
}
for (std::map<std::string, Clonable*>::iterator i = branchProperties_.begin(); i != branchProperties_.end(); i++)
{
delete i->second;
}
}
Node* clone() const { return new Node(*this); }
public:
/**
* @name Identity
*
* @{
*/
/**
* @brief Get the node's id.
*
* @return The identity tag of this node.
*/
virtual int getId() const { return id_; }
/**
* @brief Set this node's id.
*
* @param id The new identity tag.
*/
virtual void setId(int id) { id_ = id; }
virtual std::vector<int> getSonsId() const
{
std::vector<int> sonsId(sons_.size());
for (unsigned int i = 0; i < sons_.size(); i++)
{
sonsId[i] = sons_[i]->getId();
}
return sonsId;
}
/** @} */
/**
* @name Name:
*
* @{
*/
/**
* @brief Get the name associated to this node, if there is one,
* otherwise throw a NodeException.
*
* @return The name associated to this node.
*/
virtual std::string getName() const throw (NodePException)
{
if (!hasName()) throw NodePException("Node::getName: no name associated to this node.", this);
return *name_;
}
/**
* @brief Give a name or update the name associated to the node.
*
* @param name The name to give to the node.
*/
virtual void setName(const std::string& name)
{
if (name_) delete name_;
name_ = new std::string(name);
}
/**
* @brief Delete the name associated to this node (do nothing if there is no name).
*/
virtual void deleteName()
{
if (name_) delete name_;
name_ = 0;
}
/**
* @brief Tell is this node has a name.
*
* @return True if name != 0.
*/
virtual bool hasName() const { return name_ != 0; }
/** @} */
/**
* @name Distances:
*
* @{
*/
/**
* @brief Get the distance to the father node is there is one,
* otherwise throw a NodeException.
*
* @return The distance to the father node.
*/
virtual double getDistanceToFather() const
{
if (!hasDistanceToFather())
throw NodePException("Node::getDistanceToFather: Node has no distance.", this);
return *distanceToFather_;
}
/**
* @brief Set or update the distance toward the father node.
*
* Warning: a distance to the father node may be set even if no father node is specified.
* This is used by several tree reconstruction methods.
* It may also be useful for manipulating subtrees.
*
* @param distance The new distance to the father node.
*/
virtual void setDistanceToFather(double distance)
{
if (distanceToFather_)
delete distanceToFather_;
distanceToFather_ = new double(distance);
}
/**
* @brief Delete the distance to the father node.
*/
virtual void deleteDistanceToFather()
{
if (distanceToFather_)
delete distanceToFather_;
distanceToFather_ = 0;
}
/**
* @brief Tell is this node has a distance to the father.
*
* @return True if distanceToFather != 0.
*/
virtual bool hasDistanceToFather() const
{
return distanceToFather_ != 0;
}
/** @} */
/**
* @name Father:
*
* @{
*/
/**
* @brief Get the father of this node is there is one.
*
* @return A pointer toward the father node, 0 if there is not.
*/
virtual const Node* getFather() const { return father_; }
/**
* @brief Get the father of this node is there is one.
*
* @return A pointer toward the father node, 0 if there is not.
*/
virtual Node* getFather() { return father_; }
virtual int getFatherId() const { return father_->getId(); }
/**
* @brief Set the father node of this node.
*
* @param node The father node.
*/
virtual void setFather(Node* node) throw (NullPointerException)
{
if (!node)
throw NullPointerException("Node::setFather(). Empty node given as input.");
father_ = node;
if (find(node->sons_.begin(), node->sons_.end(), this) == node->sons_.end())
node->sons_.push_back(this);
else // Otherwise node is already present.
std::cerr << "DEVEL warning: Node::setFather. Son node already registered! No pb here, but could be a bug in your implementation..." << std::endl;
}
/**
* @brief Remove the father of this node.
*/
virtual Node* removeFather()
{
Node* f = father_;
father_ = 0;
return f;
}
/**
* @brief Tell if this node has a father node.
*/
virtual bool hasFather() const { return father_ != 0; }
/** @} */
/**
* @name Sons:
*
* @{
*/
virtual unsigned int getNumberOfSons() const { return sons_.size(); }
virtual std::vector<Node*>& getSons()
{
return sons_;
}
virtual const Node* getSon(unsigned int pos) const throw (IndexOutOfBoundsException)
{
if (pos >= sons_.size()) throw IndexOutOfBoundsException("Node::getSon().", pos, 0, sons_.size() - 1);
return sons_[pos];
}
virtual Node* getSon(unsigned int pos) throw (IndexOutOfBoundsException)
{
if (pos >= sons_.size()) throw IndexOutOfBoundsException("Node::getSon().", pos, 0, sons_.size() - 1);
return sons_[pos];
}
virtual void addSon(unsigned int pos, Node* node) throw (NullPointerException, NodePException)
{
if (!node)
throw NullPointerException("Node::addSon(). Empty node given as input.");
if (find(sons_.begin(), sons_.end(), node) == sons_.end())
sons_.insert(sons_.begin() + pos, node);
else // Otherwise node is already present.
std::cerr << "DEVEL warning: Node::addSon. Son node already registered! No pb here, but could be a bug in your implementation..." << std::endl;
node->father_ = this;
}
virtual void addSon(Node* node) throw (NullPointerException, NodePException)
{
if (!node)
throw NullPointerException("Node::addSon(). Empty node given as input.");
if (find(sons_.begin(), sons_.end(), node) == sons_.end())
sons_.push_back(node);
else // Otherwise node is already present.
throw NodePException("Node::addSon. Trying to add a node which is already present.");
node->father_ = this;
}
virtual void setSon(unsigned int pos, Node* node) throw (IndexOutOfBoundsException, NullPointerException, NodePException)
{
if (!node)
throw NullPointerException("Node::setSon(). Empty node given as input.");
if (pos >= sons_.size())
throw IndexOutOfBoundsException("Node::setSon(). Invalid node position.", pos, 0, sons_.size() - 1);
std::vector<Node*>::iterator search = find(sons_.begin(), sons_.end(), node);
if (search == sons_.end() || search == sons_.begin() + pos)
sons_[pos] = node;
else
throw NodePException("Node::setSon. Trying to set a node which is already present.");
node->father_ = this;
}
virtual Node* removeSon(unsigned int pos) throw (IndexOutOfBoundsException)
{
if (pos >= sons_.size())
throw IndexOutOfBoundsException("Node::removeSon(). Invalid node position.", pos, 0, sons_.size() - 1);
Node* node = sons_[pos];
sons_.erase(sons_.begin() + pos);
node->removeFather();
return node;
}
virtual void removeSon(Node* node) throw (NodeNotFoundException, NullPointerException)
{
if (!node)
throw NullPointerException("Node::removeSon(). Empty node given as input.");
for (unsigned int i = 0; i < sons_.size(); i++)
{
if (sons_[i] == node)
{
sons_.erase(sons_.begin() + i);
node->removeFather();
return;
}
}
throw NodeNotFoundException("Node::removeSon.", node->getId());
}
virtual void removeSons()
{
while (sons_.size() != 0)
removeSon(static_cast<unsigned int>(0));
}
virtual void swap(unsigned int branch1, unsigned int branch2) throw (IndexOutOfBoundsException);
virtual unsigned int getSonPosition(const Node* son) const throw (NodeNotFoundException, NullPointerException);
/** @} */
// These functions must not be declared as virtual!!
std::vector<const Node*> getNeighbors() const;
std::vector<Node*> getNeighbors();
virtual unsigned int degree() const { return getNumberOfSons() + (hasFather() ? 1 : 0); }
/**
* @name Operators:
*
* - a positive value send the corresponding son;
* - a negative value send the father.
*
* @{
*/
Node* operator[](int i) { return (i < 0) ? father_ : sons_[i]; }
const Node* operator[](int i) const { return (i < 0) ? father_ : sons_[i]; }
/** @} */
/**
* @name Node properties:
*
* @{
*/
/**
* @brief Set/add a node property.
*
* If no property with the same name is found, the new property will be added to the list.
* Conversely, the property will be deleted and replaced by the new one.
* If you want to keep a copy of the old property, consider using the removeNodeProperty function before.
*
* @param name The name of the property to set.
* @param property The property object (will be cloned).
*/
virtual void setNodeProperty(const std::string& name, const Clonable& property)
{
if (hasNodeProperty(name))
delete nodeProperties_[name];
nodeProperties_[name] = property.clone();
}
virtual Clonable* getNodeProperty(const std::string& name) throw (PropertyNotFoundException)
{
if (hasNodeProperty(name))
return nodeProperties_[name];
else
throw PropertyNotFoundException("", name, this);
}
virtual const Clonable* getNodeProperty(const std::string& name) const throw (PropertyNotFoundException)
{
if (hasNodeProperty(name))
return const_cast<const Clonable*>(nodeProperties_[name]);
else
throw PropertyNotFoundException("", name, this);
}
virtual Clonable* removeNodeProperty(const std::string& name) throw (PropertyNotFoundException)
{
if (hasNodeProperty(name))
{
Clonable* removed = nodeProperties_[name];
nodeProperties_.erase(name);
return removed;
}
else
throw PropertyNotFoundException("", name, this);
}
virtual void deleteNodeProperty(const std::string& name) throw (PropertyNotFoundException)
{
if (hasNodeProperty(name))
{
delete nodeProperties_[name];
nodeProperties_.erase(name);
}
else
throw PropertyNotFoundException("", name, this);
}
/**
* @brief Remove all node properties.
*
* Attached objects will not be deleted.
*/
virtual void removeNodeProperties()
{
nodeProperties_.clear();
}
/**
* @brief Delete all node properties.
*/
virtual void deleteNodeProperties()
{
for (std::map<std::string, Clonable*>::iterator i = nodeProperties_.begin(); i != nodeProperties_.end(); i++)
{
delete i->second;
}
nodeProperties_.clear();
}
virtual bool hasNodeProperty(const std::string& name) const { return nodeProperties_.find(name) != nodeProperties_.end(); }
virtual std::vector<std::string> getNodePropertyNames() const { return MapTools::getKeys(nodeProperties_); }
/** @} */
/**
* @name Branch properties:
*
* @{
*/
/**
* @brief Set/add a branch property.
*
* If no property with the same name is found, the new property will be added to the list.
* Conversely, the property will be deleted and replaced by the new one.
* If you want to keep a copy of the old property, consider using the removeBranchProperty function before.
*
* @param name The name of the property to set.
* @param property The property object (will be cloned).
*/
virtual void setBranchProperty(const std::string& name, const Clonable& property)
{
if (hasBranchProperty(name))
delete branchProperties_[name];
branchProperties_[name] = property.clone();
}
virtual Clonable* getBranchProperty(const std::string& name) throw (PropertyNotFoundException)
{
if (hasBranchProperty(name))
return branchProperties_[name];
else
throw PropertyNotFoundException("", name, this);
}
virtual const Clonable* getBranchProperty(const std::string& name) const throw (PropertyNotFoundException)
{
if (hasBranchProperty(name))
return const_cast<const Clonable*>(branchProperties_[name]);
else
throw PropertyNotFoundException("", name, this);
}
virtual Clonable* removeBranchProperty(const std::string& name) throw (PropertyNotFoundException)
{
if (hasBranchProperty(name))
{
Clonable* removed = branchProperties_[name];
branchProperties_.erase(name);
return removed;
}
else
throw PropertyNotFoundException("", name, this);
}
virtual void deleteBranchProperty(const std::string& name) throw (PropertyNotFoundException)
{
if (hasBranchProperty(name))
{
delete branchProperties_[name];
branchProperties_.erase(name);
}
else
throw PropertyNotFoundException("", name, this);
}
/**
* @brief Remove all branch properties.
*
* Attached objects will not be deleted.
*/
virtual void removeBranchProperties()
{
branchProperties_.clear();
}
/**
* @brief Delete all branch properties.
*/
virtual void deleteBranchProperties()
{
for (std::map<std::string, Clonable*>::iterator i = branchProperties_.begin(); i != branchProperties_.end(); i++)
{
delete i->second;
}
branchProperties_.clear();
}
virtual bool hasBranchProperty(const std::string& name) const { return branchProperties_.find(name) != branchProperties_.end(); }
virtual std::vector<std::string> getBranchPropertyNames() const { return MapTools::getKeys(branchProperties_); }
virtual bool hasBootstrapValue() const;
virtual double getBootstrapValue() const throw (PropertyNotFoundException);
/** @} */
// Equality operator:
virtual bool operator==(const Node& node) const { return id_ == node.id_; }
// Tests:
virtual bool isLeaf() const { return degree() <= 1; }
};
} // end of namespace bpp.
#endif // _NODE_H_
|