bpp-phyl  2.2.0
CladogramPlot.cpp
Go to the documentation of this file.
1 //
2 // File: CladogramPlot.cpp
3 // Created by: Julien Dutheil
4 // Created on: Tue Oct 9 17:22 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 "CladogramPlot.h"
41 #include "../TreeTemplateTools.h"
42 
43 //From the STL:
44 #include <memory>
45 
46 using namespace bpp;
47 using namespace std;
48 
49 CladogramDrawBranchEvent::CladogramDrawBranchEvent(const TreeDrawing* source, GraphicDevice* gd, const INode* node, double length, const Cursor& cursor, short orientation) :
50  DrawIBranchEvent(source, gd, node, cursor), orientation_(), length_(length)
51 {
53 }
54 
56 {
57  double offset = 0;
58  if (getINode()->hasDistanceToFather())
59  {
60  offset = orientation_ * length_ * position;
61  }
62  return getCursor().getTranslation(offset, 0);
63 }
64 
65 void CladogramPlot::setTree(const Tree* tree)
66 {
68  if (hasTree())
69  totalDepth_ = static_cast<double>(TreeTemplateTools::getDepth(*getTree_()->getRootNode()));
70 }
71 
72 void CladogramPlot::drawDendrogram_(GraphicDevice& gDevice) const throw (Exception)
73 {
74  if (hasTree())
75  {
76  DrawTreeEvent treeEvent(this, &gDevice);
77  fireBeforeTreeEvent_(treeEvent);
78  unsigned int* tipCounter = new unsigned int(0);
79  double y;
80  recursivePlot_(gDevice, *const_cast<INode*>(getTree_()->getRootNode()),
81  getHorizontalOrientation() == ORIENTATION_LEFT_TO_RIGHT ? 0 : getWidth() * getXUnit(),
82  y,
83  getHorizontalOrientation() == ORIENTATION_LEFT_TO_RIGHT ? 1. : -1.,
84  getVerticalOrientation() == ORIENTATION_TOP_TO_BOTTOM ? 1. : -1.,
85  tipCounter);
86  fireAfterTreeEvent_(treeEvent);
87  }
88 }
89 
90 void CladogramPlot::recursivePlot_(GraphicDevice& gDevice, INode& node, double x, double& y, double hDirection, double vDirection, unsigned int* tipCounter) const
91 {
92  double depth = static_cast<double>(TreeTemplateTools::getDepth(node));
93  double x2 = ((getHorizontalOrientation() == ORIENTATION_LEFT_TO_RIGHT ? totalDepth_ : 0) - depth) * getXUnit() * hDirection;
94  auto_ptr<Cursor> cursor;
95  auto_ptr<DrawINodeEvent> nodeEvent;
96  auto_ptr<DrawIBranchEvent> branchEvent;
97  short hpos = (getHorizontalOrientation() == ORIENTATION_LEFT_TO_RIGHT ? GraphicDevice::TEXT_HORIZONTAL_LEFT : GraphicDevice::TEXT_HORIZONTAL_RIGHT);
98  if (node.isLeaf())
99  {
100  y = ((getVerticalOrientation() == ORIENTATION_TOP_TO_BOTTOM ? 0 : getHeight()) + static_cast<double>(*tipCounter) * vDirection) * getYUnit();
101  (*tipCounter)++;
102  cursor.reset(new Cursor(x2, y, 0, hpos));
103  nodeEvent.reset(new DrawINodeEvent(this, &gDevice, &node, *cursor));
104  fireBeforeNodeEvent_(*nodeEvent);
105  }
106  else if (node.getInfos().isCollapsed())
107  {
108  y = ((getVerticalOrientation() == ORIENTATION_TOP_TO_BOTTOM ? 0 : getHeight()) + static_cast<double>(*tipCounter) * vDirection) * getYUnit();
109  (*tipCounter)++;
110  cursor.reset(new Cursor(x2, y, 0, hpos));
111  nodeEvent.reset(new DrawINodeEvent(this, &gDevice, &node, *cursor));
112  fireBeforeNodeEvent_(*nodeEvent);
113  }
114  else
115  {
116  //Vertical line. Call the method on son nodes first:
117  double miny = 1000000; //(unsigned int)(-log(0));
118  double maxy = 0;
119  for(unsigned int i = 0; i < node.getNumberOfSons(); i++)
120  {
121  double yson;
122  recursivePlot_(gDevice, *node.getSon(i), x2, yson, hDirection, vDirection, tipCounter);
123  if(yson < miny) miny = yson;
124  if(yson > maxy) maxy = yson;
125  }
126  y = (maxy + miny) / 2.;
127  cursor.reset(new Cursor(x2, y, 0, hpos));
128  nodeEvent.reset(new DrawINodeEvent(this, &gDevice, &node, *cursor));
129  fireBeforeNodeEvent_(*nodeEvent);
130  gDevice.drawLine(x2, miny, x2, maxy);
131  }
132  //Actualize node infos:
133  node.getInfos().setX(x2);
134  node.getInfos().setY(y);
135  nodeEvent.reset(new DrawINodeEvent(this, &gDevice, &node, *cursor));
136  fireAfterNodeEvent_(*nodeEvent);
137 
138  //Horizontal line
139  branchEvent.reset(new CladogramDrawBranchEvent(this, &gDevice, &node, x2 - x, *cursor, getHorizontalOrientation()));
140  fireBeforeBranchEvent_(*branchEvent);
141  gDevice.drawLine(x, y, x2, y);
142  fireAfterBranchEvent_(*branchEvent);
143 }
144 
Basal interface for tree drawing classes.
Definition: TreeDrawing.h:264
void setTree(const Tree *tree)
static unsigned int getDepth(const Node &node)
Get the depth of the subtree defined by node &#39;node&#39;, i.e. the maximum number of sons &#39;generations&#39;...
const NodeTemplate< NodeInfos > * getSon(size_t i) const
Definition: NodeTemplate.h:147
void setTree(const Tree *tree=0)
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
void fireAfterNodeEvent_(const DrawINodeEvent &event) const
STL namespace.
Interface for phylogenetic tree objects.
Definition: Tree.h:148
virtual bool isLeaf() const
Definition: Node.h:692
virtual const Cursor & getCursor() const
Definition: TreeDrawing.h:197
void fireBeforeBranchEvent_(const DrawIBranchEvent &event) const
void fireAfterBranchEvent_(const DrawIBranchEvent &event) const
CladogramDrawBranchEvent(const TreeDrawing *source, GraphicDevice *gd, const INode *node, double length_, const Cursor &cursor, short orientation)
void fireBeforeNodeEvent_(const DrawINodeEvent &event) const
Event class that uses INode object (more efficient than relying on nodes id, but less generic)...
void drawDendrogram_(GraphicDevice &gDevice) const
const INode * getINode() const
virtual size_t getNumberOfSons() const
Definition: Node.h:388
TreeTemplate< INode > * getTree_()
virtual const NodeInfos & getInfos() const
Definition: NodeTemplate.h:179
The NodeTemplate class.
Definition: NodeTemplate.h:73
double getHeight() const
Definition: CladogramPlot.h:94
Data structure describing a plotting direction.
Definition: TreeDrawing.h:87
void recursivePlot_(GraphicDevice &gDevice, INode &node, double x, double &y, double hDirection, double vDirection, unsigned int *tipCounter) const
Event class used by TreeDrawing classes.
Definition: TreeDrawing.h:211