bpp-phyl  2.2.0
PhylogramPlot.cpp
Go to the documentation of this file.
1 //
2 // File: PhylogramPlot.cpp
3 // Created by: Julien Dutheil
4 // Created on: Tue Oct 3 20:52 2006
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
11 graphic components to develop bioinformatics applications.
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 #include "PhylogramPlot.h"
41 #include "../TreeTemplateTools.h"
42 
43 using namespace bpp;
44 
45 #include <string>
46 
47 using namespace std;
48 
49 PhylogramDrawBranchEvent::PhylogramDrawBranchEvent(const TreeDrawing* source, GraphicDevice* gd, const INode* node, const Cursor& cursor, short orientation) :
50  DrawIBranchEvent(source, gd, node, cursor), orientation_()
51 {
53 }
54 
56 {
57  double offset = 0;
58  if (getINode()->hasDistanceToFather())
59  {
60  double l = getINode()->getDistanceToFather();
61  offset = orientation_ * l * position * getTreeDrawing()->getXUnit();
62  }
63  return getCursor().getTranslation(offset, 0);
64 }
65 
66 void PhylogramPlot::setTree(const Tree* tree)
67 {
69  if (tree)
70  {
71  getTree_()->setVoidBranchLengths(0.);
72  }
73 }
74 
75 void PhylogramPlot::drawDendrogram_(GraphicDevice& gDevice) const throw (Exception)
76 {
77  if (hasTree())
78  {
79  DrawTreeEvent treeEvent(this, &gDevice);
80  fireBeforeTreeEvent_(treeEvent);
81  unsigned int* tipCounter = new unsigned int(0);
82  double y;
83  recursivePlot_(gDevice, *const_cast<INode*>(getTree_()->getRootNode()),
84  getHorizontalOrientation() == ORIENTATION_LEFT_TO_RIGHT ? 0 : getWidth() * getXUnit(),
85  y,
86  getHorizontalOrientation() == ORIENTATION_LEFT_TO_RIGHT ? 1. : -1.,
87  getVerticalOrientation() == ORIENTATION_TOP_TO_BOTTOM ? 1. : -1.,
88  tipCounter);
89  fireAfterTreeEvent_(treeEvent);
90  }
91 }
92 
93 void PhylogramPlot::recursivePlot_(GraphicDevice& gDevice, INode& node, double x, double& y, double hDirection, double vDirection, unsigned int* tipCounter) const
94 {
95  double x2;
96  bool drawBranch = true;
97  if (node.hasDistanceToFather())
98  {
99  double length = node.hasDistanceToFather() ? node.getDistanceToFather() : 0.;
100  if (length < -10000000)
101  {
102  x2 = x;
103  drawBranch = false;
104  }
105  else
106  {
107  x2 = x + hDirection * length * getXUnit();
108  }
109  }
110  else
111  {
112  x2 = x;
113  drawBranch = false;
114  }
115 
116  auto_ptr<Cursor> cursor;
117  auto_ptr<DrawINodeEvent> nodeEvent;
118  auto_ptr<DrawIBranchEvent> branchEvent;
119  short hpos = (getHorizontalOrientation() == ORIENTATION_LEFT_TO_RIGHT ? GraphicDevice::TEXT_HORIZONTAL_LEFT : GraphicDevice::TEXT_HORIZONTAL_RIGHT);
120  if (node.isLeaf())
121  {
122  y = ((getVerticalOrientation() == ORIENTATION_TOP_TO_BOTTOM ? 0 : getHeight()) + static_cast<double>(*tipCounter) * vDirection) * getYUnit();
123  (*tipCounter)++;
124  cursor.reset(new Cursor(x2, y, 0, hpos));
125  nodeEvent.reset(new DrawINodeEvent(this, &gDevice, &node, *cursor));
126  fireBeforeNodeEvent_(*nodeEvent);
127  }
128  else if (node.getInfos().isCollapsed())
129  {
130  y = ((getVerticalOrientation() == ORIENTATION_TOP_TO_BOTTOM ? 0 : getHeight()) + static_cast<double>(*tipCounter) * vDirection) * getYUnit();
131  (*tipCounter)++;
132  cursor.reset(new Cursor(x2, y, 0, hpos));
133  nodeEvent.reset(new DrawINodeEvent(this, &gDevice, &node, *cursor));
134  fireBeforeNodeEvent_(*nodeEvent);
135  }
136  else
137  {
138  //Vertical line. Call the method on son nodes first:
139  double miny = 1000000; //(unsigned int)(-log(0));
140  double maxy = 0;
141  for (unsigned int i = 0; i < node.getNumberOfSons(); i++)
142  {
143  double yson;
144  recursivePlot_(gDevice, *node.getSon(i), x2, yson, hDirection, vDirection, tipCounter);
145  if(yson < miny) miny = yson;
146  if(yson > maxy) maxy = yson;
147  }
148  y = (maxy + miny) / 2.;
149  cursor.reset(new Cursor(x2, y, 0, hpos));
150  nodeEvent.reset(new DrawINodeEvent(this, &gDevice, &node, *cursor));
151  fireBeforeNodeEvent_(*nodeEvent);
152  gDevice.drawLine(x2, miny, x2, maxy);
153  }
154 
155  //Actualize node infos:
156  node.getInfos().setX(x2);
157  node.getInfos().setY(y);
158  nodeEvent.reset(new DrawINodeEvent(this, &gDevice, &node, *cursor));
159  fireAfterNodeEvent_(*nodeEvent);
160 
161  if (drawBranch)
162  {
163  //Horizontal line
164  branchEvent.reset(new PhylogramDrawBranchEvent(this, &gDevice, &node, *cursor, getHorizontalOrientation()));
165  fireBeforeBranchEvent_(*branchEvent);
166  gDevice.drawLine(x, y, x2, y);
167  fireAfterBranchEvent_(*branchEvent);
168  }
169 }
170 
171 
Basal interface for tree drawing classes.
Definition: TreeDrawing.h:264
void setTree(const Tree *tree)
double getHeight() const
Definition: PhylogramPlot.h:91
const NodeTemplate< NodeInfos > * getSon(size_t i) const
Definition: NodeTemplate.h:147
Cursor getBranchCursor(double position) const
Event class that uses INode object (more efficient than relying on nodes id, but less generic)...
Cursor getTranslation(double x, double y) const
Definition: TreeDrawing.h:109
PhylogramDrawBranchEvent(const TreeDrawing *source, GraphicDevice *gd, const INode *node, const Cursor &cursor, short orientation)
void fireAfterNodeEvent_(const DrawINodeEvent &event) const
STL namespace.
virtual bool hasDistanceToFather() const
Tell is this node has a distance to the father.
Definition: Node.h:321
Interface for phylogenetic tree objects.
Definition: Tree.h:148
virtual bool isLeaf() const
Definition: Node.h:692
void recursivePlot_(GraphicDevice &gDevice, INode &node, double x, double &y, double hDirection, double vDirection, unsigned int *tipCounter) const
virtual double getDistanceToFather() const
Get the distance to the father node is there is one, otherwise throw a NodeException.
Definition: Node.h:283
virtual const TreeDrawing * getTreeDrawing() const
Definition: TreeDrawing.h:194
virtual const Cursor & getCursor() const
Definition: TreeDrawing.h:197
void fireBeforeBranchEvent_(const DrawIBranchEvent &event) const
void fireAfterBranchEvent_(const DrawIBranchEvent &event) const
void fireBeforeNodeEvent_(const DrawINodeEvent &event) const
Event class that uses INode object (more efficient than relying on nodes id, but less generic)...
virtual double getXUnit() const =0
const INode * getINode() const
virtual size_t getNumberOfSons() const
Definition: Node.h:388
TreeTemplate< INode > * getTree_()
void setTree(const Tree *tree=0)
virtual const NodeInfos & getInfos() const
Definition: NodeTemplate.h:179
void drawDendrogram_(GraphicDevice &gDevice) const
The NodeTemplate class.
Definition: NodeTemplate.h:73
Data structure describing a plotting direction.
Definition: TreeDrawing.h:87
Event class used by TreeDrawing classes.
Definition: TreeDrawing.h:211