File: pruning.cpp

package info (click to toggle)
iqtree 1.6.12%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 12,140 kB
  • sloc: cpp: 111,752; ansic: 53,619; python: 242; sh: 195; makefile: 52
file content (183 lines) | stat: -rw-r--r-- 4,981 bytes parent folder | download | duplicates (5)
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
/***************************************************************************
 *   Copyright (C) 2006 by BUI Quang Minh, Steffen Klaere, Arndt von Haeseler   *
 *   minh.bui@univie.ac.at   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "pruning.h"
#include <algorithm>

/*********************************************
	class Pruning
*********************************************/
/**
	Steffen Klaere's pruning algorithm
*/
void Pruning::run(Params &params, vector<PDTaxaSet> &taxa_set)
{
	//if (params.min_size < 2) 
		params.min_size = params.sub_size;

	list_size = 2*(leafNum-params.sub_size)-1;
	if (!initialset.empty()) {
		doInitialSet();
	}
	buildLeaves();
	for (int step = leafNum; step > params.sub_size; step--)
	{
		deleteExNode(nearestLeaf());
		list_size -= 2;
	}
	taxa_set.resize(1);
	taxa_set[0].setTree(*this);

}

void Pruning::doInitialSet() {
	for (NodeVector::iterator it = initialset.begin(); it != initialset.end(); it++) {
		(*it)->height = 1;
	}
}

void Pruning::printLeaves()
{
	// print info
	for (LeafSet::iterator it = leaves.begin(); it != leaves.end(); it++)
	{
		Node *node = *it;
		cout << node->id << " " << node->neighbors[0]->length << endl;
	}
}


void Pruning::buildLeaves(Node *node, Node *dad)
{
	if (!node) node = root;
	if (node->isLeaf())
		addLeaf(node);
	FOR_NEIGHBOR_IT(node, dad, it)
		buildLeaves((*it)->node, node);
}

LeafSet::iterator Pruning::findNode(Node *node)
{
	pair<LeafSet::iterator, LeafSet::iterator>
	range = leaves.equal_range(node);
	for (LeafSet::iterator it = range.first; it != range.second; it++)
		if (*it == node)
			return it;
	return leaves.end();
}

void Pruning::deleteExNode(LeafSet::iterator pos)
{
	// delete from the tree
	Node *node = *pos;
	Node *innode = node->neighbors[0]->node;
	Node *othernodes[2] = { NULL, NULL };
	int i;
	NeighborVec::iterator it;
	double length = 0;

	bool should_merge = true;
	//for (it = innode->neighbors.begin(); it != innode->neighbors.end(); it++)
		//if ((*it)->node != node)
	FOR_NEIGHBOR(innode, node, it)	{
		length += (*it)->length;
		if (othernodes[0] == NULL)
			othernodes[0] = (*it)->node;
		else if (othernodes[1] == NULL)
			othernodes[1] = (*it)->node;
		else
			should_merge = false;
	}

	if (should_merge)
	{	
		// merge two branches
		for (i = 0; i < 2; i++)
			if (othernodes[i]->isLeaf()) {
				LeafSet::iterator temp = findNode(othernodes[i]);
				if (temp != leaves.end())
					leaves.erase(temp);
			}

		for (i = 0; i < 2; i++)
			for (it = othernodes[i]->neighbors.begin(); it != othernodes[i]->neighbors.end(); it++)
				if ((*it)->node == innode)
				{
					(*it)->node = othernodes[1-i];
					(*it)->length = length;
				}
	} else {
		// simple delete the neighbor of innode
		for (it = innode->neighbors.begin(); it != innode->neighbors.end(); it++)
			if ((*it)->node == node) {
				innode->neighbors.erase(it);
				break;
			}
	}
	// delete the first element
	leaves.erase(pos);
	if (should_merge)
	for (i = 0; i < 2; i++)
		if (othernodes[i]->isLeaf())
			addLeaf(othernodes[i]);

	// also delete the last element if necessary
	if (leaves.size() > list_size && leaves.size() > 1) {
		LeafSet::iterator last = leaves.end();
		last--;
		leaves.erase(last);
	}	

	if (node == root)
	{	// find another root
		root = *(leaves.begin());
	}
}

LeafSet::iterator Pruning::nearestLeaf()
{
	return leaves.begin();
}


/**
	insert a leaf into the LeafSet
*/
void Pruning::addLeaf(Node* leaf) {

	// if leaf in the initial set
	if (leaf->height == 1) {
		return;
	}

	if (list_size <= 0)
		return;
	if (leaves.size() < list_size) 
		leaves.insert(leaf);
	else {
		LeafSet::iterator last = leaves.end();
		last--;
		Node* endp = *last;
		if (leaf->neighbors[0]->length < endp->neighbors[0]->length) {
			leaves.erase(last);
			leaves.insert(leaf);
		}
	}
}