NCBI C++ ToolKit
cuSeqTreeRootedLayout.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: cuSeqTreeRootedLayout.cpp 33815 2007-05-04 17:18:18Z kazimird $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: Charlie Liu
27  *
28  * File Description:
29  * lay out a rooted phylogenetic tree.
30  */
31 #include <ncbi_pch.hpp>
33 
35 BEGIN_SCOPE(cd_utils)
36 
38 {
39  m_yInterval = yInterval;
40 }
41 
43 {
44 }
45 
46 SeqTree::iterator SeqTreeRootedLayout::findEdgeEnd(SeqTree& treeData, int x, int y, int edgeWidth)
47 {
48  SeqTree::iterator cursor = treeData.begin();
49  if (x < cursor->x)
50  {
51  cursor = treeData.end();
52  return cursor;
53  }
54  ++cursor;
55  for (;cursor != treeData.end();++cursor)
56  {
57  SeqTree::iterator parentNode = treeData.parent(cursor);
58  if ( (x >= parentNode->x) &&
59  (x <= cursor->x) &&
60  (y <= cursor->y) &&
61  (y >= cursor->y - edgeWidth))
62  break;
63  }
64  return cursor;
65 }
66 
67 
68 /*
69 void SeqTreeRootedLayout::drawEdge(wxDC& dc, int x1, int y1, int x2, int y2)
70 {
71  dc.DrawLine(x1, y1, x1, y2);
72  dc.DrawLine(x1, y2, x2, y2);
73 }*/
74 
75 
76 void SeqTreeRootedLayout::calculateNodePositions(SeqTree& treeData, int maxX, int maxY)
77 {
78  if (!treeData.isPrepared())
79  treeData.prepare();
80  m_numLeaf = treeData.getNumLeaf();
81  m_maxDist = treeData.getMaxDistanceToRoot();
82  m_maxX = maxX;
83  m_maxY = maxY;
84  //starting at the root, recursively calculate the position for each node
85  calculateNodePositions(treeData.begin());
86 }
87 
89 {
90  int yInterval = getYInterval();
91 
92  // if leaf or collapsed node, calculate itself and return
93  if ((cursor.number_of_children() == 0)||(cursor->collapsed) )
94  {
95  cursor->y = (yInterval)*cursor->id;
96  cursor->x = (cursor->distanceToRoot*m_maxX)/m_maxDist;
97  return;
98  }
99  else
100  {
101  // calculate each child
102  SeqTree::sibling_iterator sib = cursor.begin();
103  while (sib != cursor.end())
104  {
105  calculateNodePositions(sib); //recursive
106  ++sib;
107  }
108  //then calculate itself
109  cursor->x = (cursor->distanceToRoot*m_maxX)/m_maxDist;
110  // the parent's Y is the average y of all childrean
111  // calculate each child
112  SeqTree::sibling_iterator sib2 = cursor.begin();
113  int numChildren = 0;
114  int sumY = 0;
115  while (sib2 != cursor.end())
116  {
117  numChildren++;
118  sumY += sib2->y;
119  ++sib2;
120  }
121  cursor->y = sumY/numChildren;
122  }
123 }
124 
125 
127 {
128  int yInterval = 0;
129  if (m_yInterval > 0)
130  yInterval = m_yInterval;
131  else
132  yInterval = m_maxY/m_numLeaf;
133  if (yInterval <= 0)
134  yInterval = 2;
135  return yInterval;
136 }
137 
138 void SeqTreeRootedLayout::getSize(int& x, int& y)
139 {
140  x = m_maxX;
141  y = m_numLeaf * getYInterval();
142 }
143 
144 END_SCOPE(cd_utils)
146 
SeqTree::iterator findEdgeEnd(SeqTree &treeData, int x, int y, int edgeWidth)
void getSize(int &x, int &y)
void calculateNodePositions(SeqTree &treeData, int maxX, int maxY)
void prepare()
Definition: cuSeqtree.cpp:196
bool isPrepared()
Definition: cuSeqtree.cpp:268
int getNumLeaf()
Definition: cuSeqtree.cpp:272
double getMaxDistanceToRoot()
Definition: cuSeqtree.cpp:279
sibling_iterator end() const
unsigned int number_of_children() const
sibling_iterator begin() const
iter parent(iter) const
Definition: tree_msvc7.hpp:655
pre_order_iterator begin() const
Definition: tree_msvc7.hpp:573
pre_order_iterator end() const
Definition: tree_msvc7.hpp:579
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define END_SCOPE(ns)
End the previously defined scope.
Definition: ncbistl.hpp:75
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define BEGIN_SCOPE(ns)
Define a new scope.
Definition: ncbistl.hpp:72
Modified on Sat Jul 13 13:35:35 2024 by modify_doxy.py rev. 669887