/usr/include/libMems-1.6/libMems/PhyloTree.h is in libmems-1.6-dev 1.6.0+4725-4.
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 | #ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifndef __PhyloTree_h__
#define __PhyloTree_h__
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <stack>
//typedef unsigned int node_id_t;
typedef size_t node_id_t;
class TreeNode
{
public:
TreeNode() : distance(0) {};
std::string name; /**< node name */
double distance; /**< distance to parent */
std::vector< node_id_t > parents; /**< if parents.size() == 0 this is a root node */
std::vector< node_id_t > children; /**< if children.size() == 0 this is a leaf node */
};
template< class T >
class PhyloTree
{
public:
PhyloTree();
PhyloTree( const PhyloTree<T>& pt );
PhyloTree<T>& operator=( const PhyloTree<T>& pt );
double weight; /**< Overall tree weight */
node_id_t root; /**< root of the tree */
std::vector< T > nodes; /**< nodes of the tree */
void clear();
/**
* Reads a tree in Newick format. WARNING: only reads rooted trees correctly
*/
void readTree( std::istream& tree_file );
/**
* Writes a tree in Newick format
*/
void writeTree( std::ostream& os ) const;
/**
* Determines the height of the tree along the path from the root to the left-most leaf node
*/
double getHeight() const;
/**
* Determines the height of the tree along the path from nodeI to its left-most descendant leaf node
*/
double getHeight( node_id_t nodeI ) const;
T& operator[]( const unsigned i ){ return nodes[i]; }
const T& operator[]( const unsigned i ) const{ return nodes[i]; }
size_t size() const{ return nodes.size(); }
void push_back( T& t ){ nodes.push_back(t); }
T& back() { return nodes.back(); }
const T& back() const{ return nodes.back(); }
void resize( const unsigned s ){ nodes.resize(s); }
void swap( PhyloTree<T>& other )
{
std::swap( weight, other.weight );
std::swap( root, other.root );
nodes.swap( other.nodes );
}
protected:
};
template< class T >
PhyloTree<T>::PhyloTree()
{
weight = 0;
root = 0;
}
template< class T >
PhyloTree<T>::PhyloTree( const PhyloTree<T>& pt ) :
nodes( pt.nodes ),
weight( pt.weight ),
root( pt.root )
{}
template< class T >
PhyloTree<T>& PhyloTree<T>::operator=( const PhyloTree<T>& pt )
{
nodes = pt.nodes;
weight = pt.weight;
root = pt.root;
return *this;
}
template< class T >
void PhyloTree<T>::clear()
{
nodes.clear();
weight = 0;
root = 0;
}
/**
* readTree version 2.0: read in a phylogenetic tree in the Newick file format.
*
*/
template< class T >
void PhyloTree<T>::readTree( std::istream& tree_file )
{
std::string line;
clear();
if( !std::getline( tree_file, line ) )
return;
// look for either a ; or a matched number of parenthesis, if
// not found then read another line
while(true){
int paren_count = 0;
for( size_t charI = 0; charI < line.size(); charI++ )
{
if( line[charI] == '(' )
paren_count++;
if( line[charI] == ')' )
paren_count--;
}
if( paren_count == 0 )
break;
if( paren_count != 0 ){
std::string another_line;
if( !std::getline( tree_file, another_line ) )
return;
line += another_line;
}
}
std::stringstream line_str( line );
// look for a weight
std::string::size_type open_bracket_pos = line.find( "[" );
std::string::size_type bracket_pos = line.find( "]" );
if( open_bracket_pos != std::string::npos && bracket_pos != std::string::npos &&
open_bracket_pos < bracket_pos && bracket_pos < line.find( "(" ) ){
// read in a weight
getline( line_str, line, '[' );
getline( line_str, line, ']' );
std::stringstream weight_str( line );
weight_str >> weight;
}
// ready to begin parsing the tree data.
std::string tree_line;
std::getline( line_str, tree_line, ';' );
size_t read_state = 0; /**< read_state of 0 indicates nothing has been parsed yet */
size_t section_start = 0;
std::stack< node_id_t > node_stack;
std::stringstream blen_str;
T new_node;
new_node.distance = 0; // default the distance to 0
bool already_read_name = false;
bool blen_found = false;
for( size_t charI = 0; charI < tree_line.size(); charI++ ){
switch( tree_line[ charI ] ){
// if this is an open parens then simply create a new
// parent node and push it on the parent stack
case '(':
if( node_stack.size() > 0 ){
new_node.parents.clear();
new_node.parents.push_back( node_stack.top() );
(*this)[ node_stack.top() ].children.push_back( (node_id_t)(*this).size() );
}
node_stack.push( (node_id_t)(*this).size() );
nodes.push_back( new_node );
read_state = 1;
section_start = charI + 1;
break;
case ')':
if( blen_found )
{
// read off a branch length
blen_str.clear();
blen_str.str( tree_line.substr( section_start, charI - section_start ) );
blen_str >> (*this)[ node_stack.top() ].distance;
}else{
// read off a name, if possible
if( read_state == 1 ){
new_node.parents.clear();
new_node.parents.push_back( node_stack.top() );
(*this)[ node_stack.top() ].children.push_back( (node_id_t)(*this).size() );
node_stack.push( (node_id_t)(*this).size() );
nodes.push_back( new_node );
read_state = 2; // pop this node after reading its branch length
}
(*this)[ node_stack.top() ].name = tree_line.substr( section_start, charI - section_start );
}
if( read_state == 2 )
node_stack.pop();
section_start = charI + 1;
blen_found = false;
// pop off the top of the node stack
read_state = 2;
break;
case ',':
if( blen_found ){
// read off a branch length
blen_str.clear();
blen_str.str( tree_line.substr( section_start, charI - section_start ) );
blen_str >> (*this)[ node_stack.top() ].distance;
}else{
// read off a name, if possible
if( read_state == 1 ){
new_node.parents.clear();
new_node.parents.push_back( node_stack.top() );
(*this)[ node_stack.top() ].children.push_back( (node_id_t)(*this).size() );
node_stack.push( (node_id_t)(*this).size() );
nodes.push_back( new_node );
read_state = 2; // pop this node after reading its name
}
(*this)[ node_stack.top() ].name = tree_line.substr( section_start, charI - section_start );
}
if( read_state == 2 )
node_stack.pop();
section_start = charI + 1;
read_state = 1; // indicates that we'll be creating a new node when we hit :
blen_found = false;
break;
case ':':
// read off a name, if possible
if( read_state == 1 ){
new_node.parents.clear();
new_node.parents.push_back( node_stack.top() );
(*this)[ node_stack.top() ].children.push_back( (node_id_t)(*this).size() );
node_stack.push( (node_id_t)(*this).size() );
nodes.push_back( new_node );
read_state = 2; // pop this node after reading its branch length
}
(*this)[ node_stack.top() ].name = tree_line.substr( section_start, charI - section_start );
section_start = charI + 1;
blen_found = true;
break;
default:
break;
}
}
}
template< class T >
void PhyloTree<T>::writeTree( std::ostream& os ) const{
std::stack< node_id_t > node_stack;
std::stack< size_t > child_stack;
node_stack.push( root );
child_stack.push( 0 );
bool write_branch_lengths = false;
for( size_t nodeI = 0; nodeI < this->size(); nodeI++ )
{
if( (*this)[nodeI].distance != 0 )
{
write_branch_lengths = true;
break;
}
}
if( (*this).weight != 0 )
os << "[" << weight << "]";
os << "(";
while( node_stack.size() > 0 ) {
if( (*this)[ node_stack.top() ].children.size() != 0 ){
// this is a parent node
// if we have scanned all its children then pop it
if( child_stack.top() == (*this)[ node_stack.top() ].children.size() ){
os << ")";
if( node_stack.size() > 1 && write_branch_lengths )
os << ":" << (*this)[ node_stack.top() ].distance;
node_stack.pop();
child_stack.pop();
continue;
}
// try to recurse to its children
// if the child is a parent as well spit out a paren
node_id_t child = (*this)[ node_stack.top() ].children[ child_stack.top() ];
node_stack.push( child );
child_stack.top()++;
// print a comma to separate multiple children
if( child_stack.top() > 1 )
os << ",";
if( (*this)[ child ].children.size() > 0 ){
child_stack.push( 0 );
os << "(";
}
continue;
}
// this is a leaf node
os << (*this)[ node_stack.top() ].name;
if( write_branch_lengths )
os << ":" << (*this)[ node_stack.top() ].distance;
// pop the child
node_stack.pop();
}
os << ";" << std::endl;
}
template< class T >
double PhyloTree<T>::getHeight() const
{
return getHeight( root );
}
template< class T >
double PhyloTree<T>::getHeight( node_id_t nodeI ) const
{
if( (*this)[ nodeI ].children.size() == 0 )
return (*this)[ nodeI ].distance;
return (*this)[ nodeI ].distance + getHeight( (*this)[ nodeI ].children[ 0 ] );
}
/** determine which nodes are descendants of a given node */
template< class TreeType >
void getDescendants( TreeType& alignment_tree, node_id_t node, std::vector< node_id_t >& descendants )
{
// do a depth first search
std::stack< node_id_t > node_stack;
node_stack.push( node );
descendants.clear();
while( node_stack.size() > 0 )
{
node_id_t cur_node = node_stack.top();
node_stack.pop();
if( alignment_tree[cur_node].children.size() > 0 )
{
node_stack.push(alignment_tree[cur_node].children[0]);
node_stack.push(alignment_tree[cur_node].children[1]);
}
descendants.push_back(cur_node);
}
}
/** determine which nodes are leaf nodes below a given node */
template< class TreeType >
void getLeaves( TreeType& tree, node_id_t node, std::vector< node_id_t >& leaves )
{
// do a depth first search
std::stack< node_id_t > node_stack;
node_stack.push( node );
leaves.clear();
while( node_stack.size() > 0 )
{
node_id_t cur_node = node_stack.top();
node_stack.pop();
if( tree[cur_node].children.size() > 0 )
{
node_stack.push(tree[cur_node].children[0]);
node_stack.push(tree[cur_node].children[1]);
}else
leaves.push_back(cur_node);
}
}
namespace std {
template< class T > inline
void swap( PhyloTree<T>& a, PhyloTree<T>& b )
{
a.swap(b);
}
template<> inline void swap( PhyloTree<TreeNode>& a, PhyloTree<TreeNode>& b){ a.swap(b); }
}
#endif // __PhyloTree_h__
|