File: allTrees.cpp

package info (click to toggle)
fastml 3.11-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,772 kB
  • sloc: cpp: 48,522; perl: 3,588; ansic: 819; makefile: 386; python: 83; sh: 55
file content (134 lines) | stat: -rw-r--r-- 3,924 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
// $Id: allTrees.cpp 962 2006-11-07 15:13:34Z privmane $

#include "definitions.h"
#include "allTrees.h"
#include "treeUtil.h"
#include "treeIt.h"
#include "bblEM.h"
#include <algorithm>
#include <iostream>

#include "someUtil.h"

using namespace std;
#ifndef VERBOS
#define VERBOS
#endif


allTrees::allTrees(bool keepAllTrees) : _keepAllTrees(keepAllTrees) {
	_bestScore = VERYSMALL;
}

void get3seqTreeAndIdLeftVec(const sequenceContainer* sc,
							 tree& starT,
							 vector<int>& idList){
	sequenceContainer::constTaxaIterator tIt;
	sequenceContainer::constTaxaIterator tItEnd;
	tIt.begin(*sc);
	tItEnd.end(*sc);
	while(tIt != tItEnd) {
		idList.push_back(tIt->id());
		++tIt;
	}
	if (sc->numberOfSeqs()<3) errorMsg::reportError(" searching a tree for number of sequences < 3 ");
	starT.createRootNode();
	starT.createNode(starT.getRoot(),1);
	starT.createNode(starT.getRoot(),2);
	starT.createNode(starT.getRoot(),3);
	
	const string nameOfSeq1 = (*sc)[idList[idList.size()-1]].name();
	const string nameOfSeq2 = (*sc)[idList[idList.size()-2]].name();
	const string nameOfSeq3 = (*sc)[idList[idList.size()-3]].name();
	idList.pop_back();
	idList.pop_back();
	idList.pop_back();

	starT.getRoot()->getSon(0)->setName(nameOfSeq1);
	starT.getRoot()->getSon(1)->setName(nameOfSeq2);
	starT.getRoot()->getSon(2)->setName(nameOfSeq3);
	starT.createFlatLengthMatrix();
}

void allTrees::recursiveFind(	const sequenceContainer* sc,
								const stochasticProcess* sp,
								const Vdouble * weights,
								const int maxIterations,
								const MDOUBLE epsilon){
	tree starT;
	vector<int> ids;
	get3seqTreeAndIdLeftVec(sc,starT,ids);
	recursiveFind(starT,*sp,*sc,ids,weights,maxIterations,epsilon);
}

tree getAnewTreeFrom(const tree& et, tree::nodeP & mynode,
					 vector<int> & idLeft, const string& nameToAdd) {
	tree newT = et;
	tree::nodeP mynodeInNewTree = newT.findNodeByName(mynode->name());
//	int NameToAdd = idLeft[idLeft.size()-1]; 
	idLeft.pop_back();
	tree::nodeP fatherNode = mynodeInNewTree->father();
	tree::nodeP newInternalNode = newT.createNode(fatherNode, newT.getNodesNum());
	mynodeInNewTree->setFather(newInternalNode);
	newInternalNode->setSon(mynodeInNewTree);

	fatherNode->removeSon(mynodeInNewTree);
	tree::nodeP newOTU= newT.createNode(newInternalNode, newT.getNodesNum());;
	//string nameX = (*sc)[NameToAdd].name();
	newOTU->setName(nameToAdd);
	newOTU->setDisToFather(tree::FLAT_LENGTH_VALUE);
	newInternalNode->setDisToFather(tree::FLAT_LENGTH_VALUE);
	newT.create_names_to_internal_nodes();

	return newT;
}

void allTrees::recursiveFind(tree et,
							 const stochasticProcess& sp,
							 const sequenceContainer& sc,
							 vector<int> idLeft,
							 const Vdouble * weights,
							 const int maxIterations,
							 const MDOUBLE epsilon) {

	if (idLeft.empty()) {
		//static int k=1;	k++;
		MDOUBLE treeScore = evalTree(et,sp,sc,maxIterations,epsilon,weights);
		if (_keepAllTrees) {
			_allPossibleTrees.push_back(et);
			_allPossibleScores.push_back(treeScore);
		}
		LOG(5,<<".");
		//LOG(5,<<"tree: "<<k<<" l= "<<treeScore<<endl);
		if (treeScore > _bestScore) {
			//LOG(5,<<"new Best score!"<<endl);
			_bestTree = et;
			_bestScore = treeScore;
		}
	} else {
		treeIterTopDown tIt(et);
		tree::nodeP mynode = tIt.first();
		mynode = tIt.next(); // skipping the root
		for (; mynode != tIt.end(); mynode = tIt.next()) {
			int NameToAdd = idLeft[idLeft.size()-1]; 
			tree newT = getAnewTreeFrom(et,mynode,idLeft,sc[NameToAdd].name());
			recursiveFind(newT,sp,sc,idLeft,weights,maxIterations,epsilon);
			idLeft.push_back(NameToAdd);
		}
	}
}

MDOUBLE allTrees::evalTree(	tree& et,
							const stochasticProcess& sp,
							const sequenceContainer& sc,
							const int maxIterations,
							const MDOUBLE epsilon,
							const Vdouble * weights) {
	bblEM bblEM1(et,sc,sp,weights,maxIterations,epsilon);
	MDOUBLE res =bblEM1.getTreeLikelihood();
	return res;
}